PageRuntimeAgent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2011 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. #if ENABLE(INSPECTOR)
  32. #include "PageRuntimeAgent.h"
  33. #include "Document.h"
  34. #include "InjectedScript.h"
  35. #include "InjectedScriptManager.h"
  36. #include "InspectorPageAgent.h"
  37. #include "InspectorState.h"
  38. #include "InstrumentingAgents.h"
  39. #include "Page.h"
  40. #include "PageConsole.h"
  41. #include "ScriptController.h"
  42. #include "SecurityOrigin.h"
  43. using WebCore::TypeBuilder::Runtime::ExecutionContextDescription;
  44. namespace WebCore {
  45. namespace PageRuntimeAgentState {
  46. static const char runtimeEnabled[] = "runtimeEnabled";
  47. };
  48. PageRuntimeAgent::PageRuntimeAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state, InjectedScriptManager* injectedScriptManager, Page* page, InspectorPageAgent* pageAgent)
  49. : InspectorRuntimeAgent(instrumentingAgents, state, injectedScriptManager)
  50. , m_inspectedPage(page)
  51. , m_pageAgent(pageAgent)
  52. , m_frontend(0)
  53. , m_mainWorldContextCreated(false)
  54. {
  55. m_instrumentingAgents->setPageRuntimeAgent(this);
  56. }
  57. PageRuntimeAgent::~PageRuntimeAgent()
  58. {
  59. m_instrumentingAgents->setPageRuntimeAgent(0);
  60. }
  61. void PageRuntimeAgent::setFrontend(InspectorFrontend* frontend)
  62. {
  63. m_frontend = frontend->runtime();
  64. }
  65. void PageRuntimeAgent::clearFrontend()
  66. {
  67. m_frontend = 0;
  68. String errorString;
  69. disable(&errorString);
  70. }
  71. void PageRuntimeAgent::restore()
  72. {
  73. if (m_state->getBoolean(PageRuntimeAgentState::runtimeEnabled)) {
  74. String error;
  75. enable(&error);
  76. }
  77. }
  78. void PageRuntimeAgent::enable(ErrorString* errorString)
  79. {
  80. if (m_enabled)
  81. return;
  82. InspectorRuntimeAgent::enable(errorString);
  83. m_state->setBoolean(PageRuntimeAgentState::runtimeEnabled, true);
  84. // Only report existing contexts if the page did commit load, otherwise we may
  85. // unintentionally initialize contexts in the frames which may trigger some listeners
  86. // that are expected to be triggered only after the load is committed, see http://crbug.com/131623
  87. if (m_mainWorldContextCreated)
  88. reportExecutionContextCreation();
  89. }
  90. void PageRuntimeAgent::disable(ErrorString* errorString)
  91. {
  92. if (!m_enabled)
  93. return;
  94. InspectorRuntimeAgent::disable(errorString);
  95. m_state->setBoolean(PageRuntimeAgentState::runtimeEnabled, false);
  96. }
  97. void PageRuntimeAgent::didCreateMainWorldContext(Frame* frame)
  98. {
  99. m_mainWorldContextCreated = true;
  100. if (!m_enabled)
  101. return;
  102. ASSERT(m_frontend);
  103. String frameId = m_pageAgent->frameId(frame);
  104. ScriptState* scriptState = mainWorldScriptState(frame);
  105. notifyContextCreated(frameId, scriptState, 0, true);
  106. }
  107. void PageRuntimeAgent::didCreateIsolatedContext(Frame* frame, ScriptState* scriptState, SecurityOrigin* origin)
  108. {
  109. if (!m_enabled)
  110. return;
  111. ASSERT(m_frontend);
  112. String frameId = m_pageAgent->frameId(frame);
  113. notifyContextCreated(frameId, scriptState, origin, false);
  114. }
  115. InjectedScript PageRuntimeAgent::injectedScriptForEval(ErrorString* errorString, const int* executionContextId)
  116. {
  117. if (!executionContextId) {
  118. ScriptState* scriptState = mainWorldScriptState(m_inspectedPage->mainFrame());
  119. InjectedScript result = injectedScriptManager()->injectedScriptFor(scriptState);
  120. if (result.hasNoValue())
  121. *errorString = "Internal error: main world execution context not found.";
  122. return result;
  123. }
  124. InjectedScript injectedScript = injectedScriptManager()->injectedScriptForId(*executionContextId);
  125. if (injectedScript.hasNoValue())
  126. *errorString = "Execution context with given id not found.";
  127. return injectedScript;
  128. }
  129. void PageRuntimeAgent::muteConsole()
  130. {
  131. PageConsole::mute();
  132. }
  133. void PageRuntimeAgent::unmuteConsole()
  134. {
  135. PageConsole::unmute();
  136. }
  137. void PageRuntimeAgent::reportExecutionContextCreation()
  138. {
  139. Vector<std::pair<ScriptState*, SecurityOrigin*> > isolatedContexts;
  140. for (Frame* frame = m_inspectedPage->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
  141. if (!frame->script()->canExecuteScripts(NotAboutToExecuteScript))
  142. continue;
  143. String frameId = m_pageAgent->frameId(frame);
  144. ScriptState* scriptState = mainWorldScriptState(frame);
  145. notifyContextCreated(frameId, scriptState, 0, true);
  146. frame->script()->collectIsolatedContexts(isolatedContexts);
  147. if (isolatedContexts.isEmpty())
  148. continue;
  149. for (size_t i = 0; i< isolatedContexts.size(); i++)
  150. notifyContextCreated(frameId, isolatedContexts[i].first, isolatedContexts[i].second, false);
  151. isolatedContexts.clear();
  152. }
  153. }
  154. void PageRuntimeAgent::notifyContextCreated(const String& frameId, ScriptState* scriptState, SecurityOrigin* securityOrigin, bool isPageContext)
  155. {
  156. ASSERT(securityOrigin || isPageContext);
  157. int executionContextId = injectedScriptManager()->injectedScriptIdFor(scriptState);
  158. String name = securityOrigin ? securityOrigin->toRawString() : "";
  159. m_frontend->executionContextCreated(ExecutionContextDescription::create()
  160. .setId(executionContextId)
  161. .setIsPageContext(isPageContext)
  162. .setName(name)
  163. .setFrameId(frameId)
  164. .release());
  165. }
  166. } // namespace WebCore
  167. #endif // ENABLE(INSPECTOR)