1 /*
2 * Copyright (C) 2010 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
31 #include "config.h"
32 #include "web/InspectorFrontendClientImpl.h"
33
34 #include "V8InspectorFrontendHost.h"
35 #include "bindings/v8/ScriptController.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/inspector/InspectorFrontendHost.h"
38 #include "core/page/Page.h"
39 #include "public/platform/WebFloatPoint.h"
40 #include "public/platform/WebString.h"
41 #include "public/web/WebDevToolsFrontendClient.h"
42 #include "web/WebDevToolsFrontendImpl.h"
43 #include "wtf/text/WTFString.h"
44
45 using namespace WebCore;
46
47 namespace blink {
48
InspectorFrontendClientImpl(Page * frontendPage,WebDevToolsFrontendClient * client,WebDevToolsFrontendImpl * frontend)49 InspectorFrontendClientImpl::InspectorFrontendClientImpl(Page* frontendPage, WebDevToolsFrontendClient* client, WebDevToolsFrontendImpl* frontend)
50 : m_frontendPage(frontendPage)
51 , m_client(client)
52 {
53 }
54
~InspectorFrontendClientImpl()55 InspectorFrontendClientImpl::~InspectorFrontendClientImpl()
56 {
57 if (m_frontendHost)
58 m_frontendHost->disconnectClient();
59 m_client = 0;
60 }
61
windowObjectCleared()62 void InspectorFrontendClientImpl::windowObjectCleared()
63 {
64 v8::Isolate* isolate = v8::Isolate::GetCurrent();
65 ASSERT(m_frontendPage->mainFrame());
66 ScriptState* scriptState = ScriptState::forMainWorld(m_frontendPage->deprecatedLocalMainFrame());
67 ScriptState::Scope scope(scriptState);
68
69 if (m_frontendHost)
70 m_frontendHost->disconnectClient();
71 m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
72 v8::Handle<v8::Object> global = scriptState->context()->Global();
73 v8::Handle<v8::Value> frontendHostObj = toV8(m_frontendHost.get(), global, scriptState->isolate());
74
75 global->Set(v8::String::NewFromUtf8(isolate, "InspectorFrontendHost"), frontendHostObj);
76 ScriptController* scriptController = m_frontendPage->mainFrame() ? &m_frontendPage->deprecatedLocalMainFrame()->script() : 0;
77 if (scriptController) {
78 String installAdditionalAPI =
79 "" // Wrap messages that go to embedder.
80 "(function(host, methodEntries) {"
81 " host._lastCallId = 0;"
82 " host._callbacks = [];"
83 " host.embedderMessageAck = function(id, error)"
84 " {"
85 " var callback = host._callbacks[id];"
86 " delete host._callbacks[id];"
87 " if (callback)"
88 " callback(error);"
89 " };"
90 " function dispatch(methodName, argumentCount)"
91 " {"
92 " var callId = ++host._lastCallId;"
93 " var argsArray = Array.prototype.slice.call(arguments, 2);"
94 " var callback = argsArray[argsArray.length - 1];"
95 " if (typeof callback === \"function\") {"
96 " argsArray.pop();"
97 " host._callbacks[callId] = callback;"
98 " }"
99 " var message = { \"id\": callId, \"method\": methodName };"
100 " argsArray = argsArray.slice(0, argumentCount);"
101 " if (argsArray.length)"
102 " message.params = argsArray;"
103 " host.sendMessageToEmbedder(JSON.stringify(message));"
104 " };"
105 " methodEntries.forEach(function(methodEntry) { host[methodEntry[0]] = dispatch.bind(null, methodEntry[0], methodEntry[1]); });"
106 "})(InspectorFrontendHost,"
107 " [['addFileSystem', 0],"
108 " ['append', 2],"
109 " ['bringToFront', 0],"
110 " ['closeWindow', 0],"
111 " ['indexPath', 2],"
112 " ['inspectElementCompleted', 0],"
113 " ['inspectedURLChanged', 1],"
114 " ['moveWindowBy', 2],"
115 " ['openInNewTab', 1],"
116 " ['openUrlOnRemoteDeviceAndInspect', 2],"
117 " ['removeFileSystem', 1],"
118 " ['requestFileSystems', 0],"
119 " ['resetZoom', 0],"
120 " ['save', 3],"
121 " ['searchInPath', 3],"
122 " ['setWhitelistedShortcuts', 1],"
123 " ['setContentsResizingStrategy', 2],"
124 " ['setInspectedPageBounds', 1],"
125 " ['setIsDocked', 1],"
126 " ['subscribe', 1],"
127 " ['stopIndexing', 1],"
128 " ['unsubscribe', 1],"
129 " ['zoomIn', 0],"
130 " ['zoomOut', 0]]);"
131 ""
132 "" // Support for legacy front-ends (<M34). Do not add items here.
133 "InspectorFrontendHost.requestSetDockSide = function(dockSide)"
134 "{"
135 " InspectorFrontendHost.setIsDocked(dockSide !== \"undocked\");"
136 "};"
137 "InspectorFrontendHost.supportsFileSystems = function() { return true; };"
138 ""
139 "" // Support for legacy front-ends (<M28). Do not add items here.
140 "InspectorFrontendHost.canInspectWorkers = function() { return true; };"
141 "InspectorFrontendHost.canSaveAs = function() { return true; };"
142 "InspectorFrontendHost.canSave = function() { return true; };"
143 "InspectorFrontendHost.loaded = function() {};"
144 "InspectorFrontendHost.hiddenPanels = function() { return ""; };"
145 "InspectorFrontendHost.localizedStringsURL = function() { return ""; };"
146 "InspectorFrontendHost.close = function(url) { };";
147 scriptController->executeScriptInMainWorld(installAdditionalAPI, ScriptController::ExecuteScriptWhenScriptsDisabled);
148 }
149 }
150
sendMessageToBackend(const String & message)151 void InspectorFrontendClientImpl::sendMessageToBackend(const String& message)
152 {
153 m_client->sendMessageToBackend(message);
154 }
155
sendMessageToEmbedder(const String & message)156 void InspectorFrontendClientImpl::sendMessageToEmbedder(const String& message)
157 {
158 m_client->sendMessageToEmbedder(message);
159 }
160
isUnderTest()161 bool InspectorFrontendClientImpl::isUnderTest()
162 {
163 return m_client->isUnderTest();
164 }
165
166 } // namespace blink
167