1 /* 2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. 3 * Copyright (C) 2008 Matt Lilek <webkit (at) 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 31 #include "config.h" 32 #include "core/inspector/InspectorInspectorAgent.h" 33 34 #include "bindings/v8/DOMWrapperWorld.h" 35 #include "bindings/v8/ScriptController.h" 36 #include "core/InspectorFrontend.h" 37 #include "core/dom/Document.h" 38 #include "core/frame/LocalFrame.h" 39 #include "core/inspector/InjectedScriptHost.h" 40 #include "core/inspector/InjectedScriptManager.h" 41 #include "core/inspector/InspectorController.h" 42 #include "core/inspector/InspectorState.h" 43 #include "core/inspector/InstrumentingAgents.h" 44 #include "core/loader/DocumentLoader.h" 45 #include "core/page/Page.h" 46 #include "platform/weborigin/SecurityOrigin.h" 47 #include "wtf/text/StringBuilder.h" 48 49 namespace WebCore { 50 51 namespace InspectorAgentState { 52 static const char inspectorAgentEnabled[] = "inspectorAgentEnabled"; 53 } 54 55 InspectorInspectorAgent::InspectorInspectorAgent(Page* page, InjectedScriptManager* injectedScriptManager) 56 : InspectorBaseAgent<InspectorInspectorAgent>("Inspector") 57 , m_inspectedPage(page) 58 , m_frontend(0) 59 , m_injectedScriptManager(injectedScriptManager) 60 { 61 ASSERT_ARG(page, page); 62 } 63 64 InspectorInspectorAgent::~InspectorInspectorAgent() 65 { 66 m_instrumentingAgents->setInspectorInspectorAgent(0); 67 } 68 69 void InspectorInspectorAgent::didClearDocumentOfWindowObject(LocalFrame* frame) 70 { 71 if (m_injectedScriptForOrigin.isEmpty()) 72 return; 73 74 String origin = frame->document()->securityOrigin()->toRawString(); 75 String script = m_injectedScriptForOrigin.get(origin); 76 if (script.isEmpty()) 77 return; 78 int injectedScriptId = m_injectedScriptManager->injectedScriptIdFor(ScriptState::forMainWorld(frame)); 79 StringBuilder scriptSource; 80 scriptSource.append(script); 81 scriptSource.append("("); 82 scriptSource.appendNumber(injectedScriptId); 83 scriptSource.append(")"); 84 frame->script().executeScriptInMainWorld(scriptSource.toString()); 85 } 86 87 void InspectorInspectorAgent::init() 88 { 89 m_instrumentingAgents->setInspectorInspectorAgent(this); 90 } 91 92 void InspectorInspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend) 93 { 94 m_frontend = inspectorFrontend; 95 } 96 97 void InspectorInspectorAgent::clearFrontend() 98 { 99 m_pendingEvaluateTestCommands.clear(); 100 m_frontend = 0; 101 m_injectedScriptManager->discardInjectedScripts(); 102 ErrorString error; 103 disable(&error); 104 } 105 106 void InspectorInspectorAgent::enable(ErrorString*) 107 { 108 m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, true); 109 110 if (m_pendingInspectData.first) 111 inspect(m_pendingInspectData.first, m_pendingInspectData.second); 112 113 for (Vector<pair<long, String> >::iterator it = m_pendingEvaluateTestCommands.begin(); m_frontend && it != m_pendingEvaluateTestCommands.end(); ++it) 114 m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>((*it).first), (*it).second); 115 m_pendingEvaluateTestCommands.clear(); 116 } 117 118 void InspectorInspectorAgent::disable(ErrorString*) 119 { 120 m_state->setBoolean(InspectorAgentState::inspectorAgentEnabled, false); 121 } 122 123 void InspectorInspectorAgent::reset(ErrorString*) 124 { 125 m_inspectedPage->inspectorController().reconnectFrontend(); 126 } 127 128 void InspectorInspectorAgent::domContentLoadedEventFired(LocalFrame* frame) 129 { 130 if (frame->page()->mainFrame() != frame) 131 return; 132 133 m_injectedScriptManager->injectedScriptHost()->clearInspectedObjects(); 134 } 135 136 void InspectorInspectorAgent::evaluateForTestInFrontend(long callId, const String& script) 137 { 138 if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled)) 139 m_frontend->inspector()->evaluateForTestInFrontend(static_cast<int>(callId), script); 140 else 141 m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script)); 142 } 143 144 void InspectorInspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source) 145 { 146 m_injectedScriptForOrigin.set(origin, source); 147 } 148 149 void InspectorInspectorAgent::inspect(PassRefPtr<TypeBuilder::Runtime::RemoteObject> objectToInspect, PassRefPtr<JSONObject> hints) 150 { 151 if (m_state->getBoolean(InspectorAgentState::inspectorAgentEnabled) && m_frontend) { 152 m_frontend->inspector()->inspect(objectToInspect, hints); 153 m_pendingInspectData.first = nullptr; 154 m_pendingInspectData.second = nullptr; 155 return; 156 } 157 m_pendingInspectData.first = objectToInspect; 158 m_pendingInspectData.second = hints; 159 } 160 161 } // namespace WebCore 162