InjectedScriptManager.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
  4. * Copyright (C) 2012 Google Inc. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  16. * its contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #if ENABLE(INSPECTOR)
  32. #include "InjectedScriptManager.h"
  33. #include "InjectedScript.h"
  34. #include "InjectedScriptHost.h"
  35. #include "InjectedScriptSource.h"
  36. #include "InspectorValues.h"
  37. #include "ScriptObject.h"
  38. #include <wtf/PassOwnPtr.h>
  39. namespace WebCore {
  40. PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForPage()
  41. {
  42. return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWindow));
  43. }
  44. PassOwnPtr<InjectedScriptManager> InjectedScriptManager::createForWorker()
  45. {
  46. return adoptPtr(new InjectedScriptManager(&InjectedScriptManager::canAccessInspectedWorkerContext));
  47. }
  48. InjectedScriptManager::InjectedScriptManager(InspectedStateAccessCheck accessCheck)
  49. : m_nextInjectedScriptId(1)
  50. , m_injectedScriptHost(InjectedScriptHost::create())
  51. , m_inspectedStateAccessCheck(accessCheck)
  52. {
  53. }
  54. InjectedScriptManager::~InjectedScriptManager()
  55. {
  56. }
  57. void InjectedScriptManager::disconnect()
  58. {
  59. m_injectedScriptHost->disconnect();
  60. m_injectedScriptHost.clear();
  61. }
  62. InjectedScriptHost* InjectedScriptManager::injectedScriptHost()
  63. {
  64. return m_injectedScriptHost.get();
  65. }
  66. InjectedScript InjectedScriptManager::injectedScriptForId(int id)
  67. {
  68. IdToInjectedScriptMap::iterator it = m_idToInjectedScript.find(id);
  69. if (it != m_idToInjectedScript.end())
  70. return it->value;
  71. for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
  72. if (it->value == id)
  73. return injectedScriptFor(it->key);
  74. }
  75. return InjectedScript();
  76. }
  77. int InjectedScriptManager::injectedScriptIdFor(ScriptState* scriptState)
  78. {
  79. ScriptStateToId::iterator it = m_scriptStateToId.find(scriptState);
  80. if (it != m_scriptStateToId.end())
  81. return it->value;
  82. int id = m_nextInjectedScriptId++;
  83. m_scriptStateToId.set(scriptState, id);
  84. return id;
  85. }
  86. InjectedScript InjectedScriptManager::injectedScriptForObjectId(const String& objectId)
  87. {
  88. RefPtr<InspectorValue> parsedObjectId = InspectorValue::parseJSON(objectId);
  89. if (parsedObjectId && parsedObjectId->type() == InspectorValue::TypeObject) {
  90. long injectedScriptId = 0;
  91. bool success = parsedObjectId->asObject()->getNumber("injectedScriptId", &injectedScriptId);
  92. if (success)
  93. return m_idToInjectedScript.get(injectedScriptId);
  94. }
  95. return InjectedScript();
  96. }
  97. void InjectedScriptManager::discardInjectedScripts()
  98. {
  99. m_idToInjectedScript.clear();
  100. m_scriptStateToId.clear();
  101. }
  102. void InjectedScriptManager::discardInjectedScriptsFor(DOMWindow* window)
  103. {
  104. if (m_scriptStateToId.isEmpty())
  105. return;
  106. Vector<long> idsToRemove;
  107. IdToInjectedScriptMap::iterator end = m_idToInjectedScript.end();
  108. for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != end; ++it) {
  109. ScriptState* scriptState = it->value.scriptState();
  110. if (window != domWindowFromScriptState(scriptState))
  111. continue;
  112. m_scriptStateToId.remove(scriptState);
  113. idsToRemove.append(it->key);
  114. }
  115. for (size_t i = 0; i < idsToRemove.size(); i++)
  116. m_idToInjectedScript.remove(idsToRemove[i]);
  117. // Now remove script states that have id but no injected script.
  118. Vector<ScriptState*> scriptStatesToRemove;
  119. for (ScriptStateToId::iterator it = m_scriptStateToId.begin(); it != m_scriptStateToId.end(); ++it) {
  120. ScriptState* scriptState = it->key;
  121. if (window == domWindowFromScriptState(scriptState))
  122. scriptStatesToRemove.append(scriptState);
  123. }
  124. for (size_t i = 0; i < scriptStatesToRemove.size(); i++)
  125. m_scriptStateToId.remove(scriptStatesToRemove[i]);
  126. }
  127. bool InjectedScriptManager::canAccessInspectedWorkerContext(ScriptState*)
  128. {
  129. return true;
  130. }
  131. void InjectedScriptManager::releaseObjectGroup(const String& objectGroup)
  132. {
  133. for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != m_idToInjectedScript.end(); ++it)
  134. it->value.releaseObjectGroup(objectGroup);
  135. }
  136. String InjectedScriptManager::injectedScriptSource()
  137. {
  138. return String(reinterpret_cast<const char*>(InjectedScriptSource_js), sizeof(InjectedScriptSource_js));
  139. }
  140. InjectedScript InjectedScriptManager::injectedScriptFor(ScriptState* inspectedScriptState)
  141. {
  142. ScriptStateToId::iterator it = m_scriptStateToId.find(inspectedScriptState);
  143. if (it != m_scriptStateToId.end()) {
  144. IdToInjectedScriptMap::iterator it1 = m_idToInjectedScript.find(it->value);
  145. if (it1 != m_idToInjectedScript.end())
  146. return it1->value;
  147. }
  148. if (!m_inspectedStateAccessCheck(inspectedScriptState))
  149. return InjectedScript();
  150. int id = injectedScriptIdFor(inspectedScriptState);
  151. ScriptObject injectedScriptObject = createInjectedScript(injectedScriptSource(), inspectedScriptState, id);
  152. InjectedScript result(injectedScriptObject, m_inspectedStateAccessCheck);
  153. m_idToInjectedScript.set(id, result);
  154. return result;
  155. }
  156. } // namespace WebCore
  157. #endif // ENABLE(INSPECTOR)