• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4  * Copyright (C) 2009 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 "InjectedScriptHost.h"
33 
34 #if ENABLE(INSPECTOR)
35 
36 
37 #include "Element.h"
38 #include "Frame.h"
39 #include "FrameLoader.h"
40 #include "HTMLFrameOwnerElement.h"
41 #include "InjectedScript.h"
42 #include "InspectorClient.h"
43 #include "InspectorController.h"
44 #include "InspectorDOMAgent.h"
45 #include "InspectorFrontend.h"
46 #include "InspectorResource.h"
47 #include "Pasteboard.h"
48 
49 #if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
50 #include "JavaScriptCallFrame.h"
51 #include "JavaScriptDebugServer.h"
52 using namespace JSC;
53 #endif
54 
55 #if ENABLE(DATABASE)
56 #include "Database.h"
57 #endif
58 
59 #if ENABLE(DOM_STORAGE)
60 #include "Storage.h"
61 #endif
62 
63 #include "markup.h"
64 
65 #include <wtf/RefPtr.h>
66 #include <wtf/StdLibExtras.h>
67 
68 using namespace std;
69 
70 namespace WebCore {
71 
InjectedScriptHost(InspectorController * inspectorController)72 InjectedScriptHost::InjectedScriptHost(InspectorController* inspectorController)
73     : m_inspectorController(inspectorController)
74     , m_nextInjectedScriptId(1)
75 {
76 }
77 
~InjectedScriptHost()78 InjectedScriptHost::~InjectedScriptHost()
79 {
80 }
81 
clearConsoleMessages()82 void InjectedScriptHost::clearConsoleMessages()
83 {
84     if (m_inspectorController)
85         m_inspectorController->clearConsoleMessages();
86 }
87 
copyText(const String & text)88 void InjectedScriptHost::copyText(const String& text)
89 {
90     Pasteboard::generalPasteboard()->writePlainText(text);
91 }
92 
nodeForId(long nodeId)93 Node* InjectedScriptHost::nodeForId(long nodeId)
94 {
95     if (InspectorDOMAgent* domAgent = inspectorDOMAgent())
96         return domAgent->nodeForId(nodeId);
97     return 0;
98 }
99 
pushNodePathToFrontend(Node * node,bool withChildren,bool selectInUI)100 long InjectedScriptHost::pushNodePathToFrontend(Node* node, bool withChildren, bool selectInUI)
101 {
102     InspectorFrontend* frontend = inspectorFrontend();
103     InspectorDOMAgent* domAgent = inspectorDOMAgent();
104     if (!domAgent || !frontend)
105         return 0;
106     long id = domAgent->pushNodePathToFrontend(node);
107     if (withChildren)
108         domAgent->pushChildNodesToFrontend(id);
109     if (selectInUI)
110         frontend->updateFocusedNode(id);
111     return id;
112 }
113 
addNodesToSearchResult(const String & nodeIds)114 void InjectedScriptHost::addNodesToSearchResult(const String& nodeIds)
115 {
116     if (InspectorFrontend* frontend = inspectorFrontend())
117         frontend->addNodesToSearchResult(nodeIds);
118 }
119 
pushNodeByPathToFrontend(const String & path)120 long InjectedScriptHost::pushNodeByPathToFrontend(const String& path)
121 {
122     InspectorDOMAgent* domAgent = inspectorDOMAgent();
123     if (!domAgent)
124         return 0;
125 
126     Node* node = domAgent->nodeForPath(path);
127     if (!node)
128         return 0;
129 
130     return domAgent->pushNodePathToFrontend(node);
131 }
132 
133 #if ENABLE(JAVASCRIPT_DEBUGGER) && USE(JSC)
currentCallFrame() const134 JavaScriptCallFrame* InjectedScriptHost::currentCallFrame() const
135 {
136     return JavaScriptDebugServer::shared().currentCallFrame();
137 }
138 #endif
139 
140 #if ENABLE(DATABASE)
databaseForId(long databaseId)141 Database* InjectedScriptHost::databaseForId(long databaseId)
142 {
143     if (m_inspectorController)
144         return m_inspectorController->databaseForId(databaseId);
145     return 0;
146 }
147 
selectDatabase(Database * database)148 void InjectedScriptHost::selectDatabase(Database* database)
149 {
150     if (m_inspectorController)
151         m_inspectorController->selectDatabase(database);
152 }
153 #endif
154 
155 #if ENABLE(DOM_STORAGE)
selectDOMStorage(Storage * storage)156 void InjectedScriptHost::selectDOMStorage(Storage* storage)
157 {
158     if (m_inspectorController)
159         m_inspectorController->selectDOMStorage(storage);
160 }
161 #endif
162 
reportDidDispatchOnInjectedScript(long callId,SerializedScriptValue * result,bool isException)163 void InjectedScriptHost::reportDidDispatchOnInjectedScript(long callId, SerializedScriptValue* result, bool isException)
164 {
165     if (InspectorFrontend* frontend = inspectorFrontend())
166         frontend->didDispatchOnInjectedScript(callId, result, isException);
167 }
168 
injectedScriptForId(long id)169 InjectedScript InjectedScriptHost::injectedScriptForId(long id)
170 {
171     return m_idToInjectedScript.get(id);
172 }
173 
discardInjectedScripts()174 void InjectedScriptHost::discardInjectedScripts()
175 {
176     m_idToInjectedScript.clear();
177 }
178 
releaseWrapperObjectGroup(long injectedScriptId,const String & objectGroup)179 void InjectedScriptHost::releaseWrapperObjectGroup(long injectedScriptId, const String& objectGroup)
180 {
181     if (injectedScriptId) {
182          InjectedScript injectedScript = m_idToInjectedScript.get(injectedScriptId);
183          if (!injectedScript.hasNoValue())
184              injectedScript.releaseWrapperObjectGroup(objectGroup);
185     } else {
186          // Iterate over all injected scripts if injectedScriptId is not specified.
187          for (IdToInjectedScriptMap::iterator it = m_idToInjectedScript.begin(); it != m_idToInjectedScript.end(); ++it)
188               it->second.releaseWrapperObjectGroup(objectGroup);
189     }
190 }
191 
inspectorDOMAgent()192 InspectorDOMAgent* InjectedScriptHost::inspectorDOMAgent()
193 {
194     if (!m_inspectorController)
195         return 0;
196     return m_inspectorController->domAgent();
197 }
198 
inspectorFrontend()199 InspectorFrontend* InjectedScriptHost::inspectorFrontend()
200 {
201     if (!m_inspectorController)
202         return 0;
203     return m_inspectorController->m_frontend.get();
204 }
205 
206 } // namespace WebCore
207 
208 #endif // ENABLE(INSPECTOR)
209