InspectorAgent.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
  4. * Copyright (C) 2011 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 "InspectorAgent.h"
  33. #include "Document.h"
  34. #include "DocumentLoader.h"
  35. #include "Frame.h"
  36. #include "GraphicsContext.h"
  37. #include "InjectedScriptHost.h"
  38. #include "InjectedScriptManager.h"
  39. #include "InspectorController.h"
  40. #include "InspectorFrontend.h"
  41. #include "InspectorInstrumentation.h"
  42. #include "InspectorState.h"
  43. #include "InspectorValues.h"
  44. #include "InstrumentingAgents.h"
  45. #include "Page.h"
  46. #include "ResourceRequest.h"
  47. #include "ScriptController.h"
  48. #include "ScriptFunctionCall.h"
  49. #include "ScriptObject.h"
  50. #include "SecurityOrigin.h"
  51. #include "Settings.h"
  52. #include <wtf/PassRefPtr.h>
  53. #include <wtf/RefPtr.h>
  54. namespace WebCore {
  55. namespace InspectorAgentState {
  56. static const char inspectorAgentEnabled[] = "inspectorAgentEnabled";
  57. }
  58. InspectorAgent::InspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager, InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
  59. : InspectorBaseAgent<InspectorAgent>("Inspector", instrumentingAgents, state)
  60. , m_inspectedPage(page)
  61. , m_frontend(0)
  62. , m_injectedScriptManager(injectedScriptManager)
  63. {
  64. ASSERT_ARG(page, page);
  65. m_instrumentingAgents->setInspectorAgent(this);
  66. }
  67. InspectorAgent::~InspectorAgent()
  68. {
  69. m_instrumentingAgents->setInspectorAgent(0);
  70. }
  71. void InspectorAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld* world)
  72. {
  73. if (world != mainThreadNormalWorld())
  74. return;
  75. if (m_injectedScriptForOrigin.isEmpty())
  76. return;
  77. String origin = frame->document()->securityOrigin()->toRawString();
  78. String script = m_injectedScriptForOrigin.get(origin);
  79. if (script.isEmpty())
  80. return;
  81. int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(mainWorldScriptState(frame));
  82. StringBuilder scriptSource;
  83. scriptSource.append(script);
  84. scriptSource.append("(");
  85. scriptSource.appendNumber(injectedScriptId);
  86. scriptSource.append(")");
  87. frame->script()->executeScript(scriptSource.toString());
  88. }
  89. void InspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend)
  90. {
  91. m_frontend = inspectorFrontend;
  92. #if PLATFORM(MANX)
  93. if (m_inspectedPage->inspectorController()->hasInspectorFrontendClient())
  94. m_pendingEvaluateTestCommands.clear();
  95. #endif
  96. }
  97. void InspectorAgent::clearFrontend()
  98. {
  99. #if !PLATFORM(MANX)
  100. m_pendingEvaluateTestCommands.clear();
  101. #endif
  102. m_frontend = 0;
  103. m_injectedScriptManager->discardInjectedScripts();
  104. ErrorString error;
  105. disable(&error);
  106. }
  107. void InspectorAgent::didCommitLoad()
  108. {
  109. m_injectedScriptManager->discardInjectedScripts();
  110. }
  111. void InspectorAgent::enable(ErrorString*)
  112. {
  113. m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, true);
  114. if (m_pendingInspectData.first)
  115. inspect(m_pendingInspectData.first, m_pendingInspectData.second);
  116. for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it)
  117. m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second);
  118. m_pendingEvaluateTestCommands.clear();
  119. }
  120. void InspectorAgent::disable(ErrorString*)
  121. {
  122. m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, false);
  123. }
  124. void InspectorAgent::domContentLoadedEventFired()
  125. {
  126. m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects();
  127. }
  128. bool InspectorAgent::isMainResourceLoader(DocumentLoader* loader, const KURL& requestUrl)
  129. {
  130. return loader->frame() == m_inspectedPage->mainFrame() && requestUrl == loader->requestURL();
  131. }
  132. void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script)
  133. {
  134. if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled))
  135. m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>(callId), script);
  136. else
  137. m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
  138. }
  139. void InspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
  140. {
  141. m_injectedScriptForOrigin.set(origin, source);
  142. }
  143. void InspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<InspectorObject> hints)
  144. {
  145. if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled) && m_frontend) {
  146. m_frontend->inspector()->inspect(objectToInspect, hints);
  147. m_pendingInspectData.first = 0;
  148. m_pendingInspectData.second = 0;
  149. return;
  150. }
  151. m_pendingInspectData.first = objectToInspect;
  152. m_pendingInspectData.second = hints;
  153. }
  154. KURL InspectorAgent::inspectedURL() const
  155. {
  156. return m_inspectedPage->mainFrame()->document()->url();
  157. }
  158. KURL InspectorAgent::inspectedURLWithoutFragment() const
  159. {
  160. KURL url = inspectedURL();
  161. url.removeFragmentIdentifier();
  162. return url;
  163. }
  164. bool InspectorAgent::developerExtrasEnabled() const
  165. {
  166. if (!m_inspectedPage)
  167. return false;
  168. return m_inspectedPage->settings()->developerExtrasEnabled();
  169. }
  170. } // namespace WebCore
  171. #endif // ENABLE(INSPECTOR)