/platform/external/webkit/WebCore/bindings/v8/DOMData.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 129 lines · 75 code · 21 blank · 33 comment · 8 complexity · 69d38093dc703e797b9e7f3eb762e5cd MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "DOMData.h"
  32. #include "ChildThreadDOMData.h"
  33. #include "MainThreadDOMData.h"
  34. #include "WebGLContextAttributes.h"
  35. #include "WebGLUniformLocation.h"
  36. namespace WebCore {
  37. DOMData::DOMData()
  38. : m_delayedProcessingScheduled(false)
  39. , m_isMainThread(WTF::isMainThread())
  40. , m_owningThread(WTF::currentThread())
  41. {
  42. }
  43. DOMData::~DOMData()
  44. {
  45. }
  46. DOMData* DOMData::getCurrent()
  47. {
  48. if (WTF::isMainThread())
  49. return MainThreadDOMData::getCurrent();
  50. DEFINE_STATIC_LOCAL(WTF::ThreadSpecific<ChildThreadDOMData>, childThreadDOMData, ());
  51. return childThreadDOMData;
  52. }
  53. void DOMData::ensureDeref(V8ClassIndex::V8WrapperType type, void* domObject)
  54. {
  55. if (m_owningThread == WTF::currentThread()) {
  56. // No need to delay the work. We can deref right now.
  57. derefObject(type, domObject);
  58. return;
  59. }
  60. // We need to do the deref on the correct thread.
  61. m_delayedObjectMap.set(domObject, type);
  62. // Post a task to the owning thread in order to process the delayed queue.
  63. // FIXME: For now, we can only post to main thread due to WTF task posting limitation. We will fix this when we work on nested worker.
  64. if (!m_delayedProcessingScheduled) {
  65. m_delayedProcessingScheduled = true;
  66. if (isMainThread())
  67. WTF::callOnMainThread(&derefDelayedObjectsInCurrentThread, 0);
  68. }
  69. }
  70. void DOMData::derefObject(V8ClassIndex::V8WrapperType type, void* domObject)
  71. {
  72. switch (type) {
  73. case V8ClassIndex::NODE:
  74. static_cast<Node*>(domObject)->deref();
  75. break;
  76. #define MakeCase(type, name) \
  77. case V8ClassIndex::type: static_cast<name*>(domObject)->deref(); break;
  78. DOM_OBJECT_TYPES(MakeCase) // This includes both active and non-active.
  79. #undef MakeCase
  80. #if ENABLE(SVG)
  81. #define MakeCase(type, name) \
  82. case V8ClassIndex::type: static_cast<name*>(domObject)->deref(); break;
  83. SVG_OBJECT_TYPES(MakeCase) // This also includes SVGElementInstance.
  84. #undef MakeCase
  85. #define MakeCase(type, name) \
  86. case V8ClassIndex::type: \
  87. static_cast<V8SVGPODTypeWrapper<name>*>(domObject)->deref(); break;
  88. SVG_POD_NATIVE_TYPES(MakeCase)
  89. #undef MakeCase
  90. #endif
  91. default:
  92. ASSERT_NOT_REACHED();
  93. break;
  94. }
  95. }
  96. void DOMData::derefDelayedObjects()
  97. {
  98. WTF::MutexLocker locker(DOMDataStore::allStoresMutex());
  99. m_delayedProcessingScheduled = false;
  100. for (DelayedObjectMap::iterator iter(m_delayedObjectMap.begin()); iter != m_delayedObjectMap.end(); ++iter)
  101. derefObject(iter->second, iter->first);
  102. m_delayedObjectMap.clear();
  103. }
  104. void DOMData::derefDelayedObjectsInCurrentThread(void*)
  105. {
  106. getCurrent()->derefDelayedObjects();
  107. }
  108. } // namespace WebCore