WebScriptWorld.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2009 Apple 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
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  17. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "config.h"
  25. #include "WebKitDLL.h"
  26. #include "WebScriptWorld.h"
  27. #include <JavaScriptCore/APICast.h>
  28. #include <WebCore/JSDOMBinding.h>
  29. #include <WebCore/ScriptController.h>
  30. using namespace WebCore;
  31. typedef HashMap<DOMWrapperWorld*, WebScriptWorld*> WorldMap;
  32. static WorldMap& allWorlds()
  33. {
  34. static WorldMap& map = *new WorldMap;
  35. return map;
  36. }
  37. inline WebScriptWorld::WebScriptWorld(PassRefPtr<DOMWrapperWorld> world)
  38. : m_refCount(0)
  39. , m_world(world)
  40. {
  41. ASSERT_ARG(world, m_world);
  42. ASSERT_ARG(world, !allWorlds().contains(m_world.get()));
  43. allWorlds().add(m_world.get(), this);
  44. ++gClassCount;
  45. gClassNameCount.add("WebScriptWorld");
  46. }
  47. WebScriptWorld::~WebScriptWorld()
  48. {
  49. ASSERT(allWorlds().contains(m_world.get()));
  50. allWorlds().remove(m_world.get());
  51. --gClassCount;
  52. gClassNameCount.remove("WebScriptWorld");
  53. }
  54. WebScriptWorld* WebScriptWorld::standardWorld()
  55. {
  56. static WebScriptWorld* standardWorld = createInstance(mainThreadNormalWorld()).leakRef();
  57. return standardWorld;
  58. }
  59. COMPtr<WebScriptWorld> WebScriptWorld::createInstance()
  60. {
  61. return createInstance(ScriptController::createWorld());
  62. }
  63. COMPtr<WebScriptWorld> WebScriptWorld::createInstance(PassRefPtr<DOMWrapperWorld> world)
  64. {
  65. return new WebScriptWorld(world);
  66. }
  67. COMPtr<WebScriptWorld> WebScriptWorld::findOrCreateWorld(DOMWrapperWorld* world)
  68. {
  69. if (world == mainThreadNormalWorld())
  70. return standardWorld();
  71. if (WebScriptWorld* existingWorld = allWorlds().get(world))
  72. return existingWorld;
  73. return createInstance(world);
  74. }
  75. ULONG WebScriptWorld::AddRef()
  76. {
  77. return ++m_refCount;
  78. }
  79. ULONG WebScriptWorld::Release()
  80. {
  81. ULONG newRefCount = --m_refCount;
  82. if (!newRefCount)
  83. delete this;
  84. return newRefCount;
  85. }
  86. HRESULT WebScriptWorld::QueryInterface(REFIID riid, void** ppvObject)
  87. {
  88. if (!ppvObject)
  89. return E_POINTER;
  90. *ppvObject = 0;
  91. if (IsEqualIID(riid, __uuidof(WebScriptWorld)))
  92. *ppvObject = this;
  93. else if (IsEqualIID(riid, __uuidof(IWebScriptWorld)))
  94. *ppvObject = static_cast<IWebScriptWorld*>(this);
  95. else if (IsEqualIID(riid, IID_IUnknown))
  96. *ppvObject = static_cast<IUnknown*>(this);
  97. else
  98. return E_NOINTERFACE;
  99. AddRef();
  100. return S_OK;
  101. }
  102. HRESULT WebScriptWorld::standardWorld(IWebScriptWorld** outWorld)
  103. {
  104. if (!outWorld)
  105. return E_POINTER;
  106. *outWorld = standardWorld();
  107. (*outWorld)->AddRef();
  108. return S_OK;
  109. }
  110. HRESULT WebScriptWorld::scriptWorldForGlobalContext(JSGlobalContextRef context, IWebScriptWorld** outWorld)
  111. {
  112. if (!outWorld)
  113. return E_POINTER;
  114. return findOrCreateWorld(currentWorld(toJS(context))).copyRefTo(outWorld);
  115. }
  116. HRESULT WebScriptWorld::unregisterWorld()
  117. {
  118. m_world->clearWrappers();
  119. return S_OK;
  120. }