2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "InspectorFrontendClientImpl.h"
34 #include "V8InspectorFrontendHost.h"
35 #include "WebDevToolsFrontendClient.h"
36 #include "WebDevToolsFrontendImpl.h"
37 #include "bindings/v8/ScriptController.h"
38 #include "core/dom/Document.h"
39 #include "core/inspector/InspectorFrontendHost.h"
40 #include "core/frame/Frame.h"
41 #include "core/page/Page.h"
42 #include "public/platform/WebFloatPoint.h"
43 #include "public/platform/WebString.h"
44 #include "wtf/text/WTFString.h"
46 using namespace WebCore;
50 InspectorFrontendClientImpl::InspectorFrontendClientImpl(Page* frontendPage, WebDevToolsFrontendClient* client, WebDevToolsFrontendImpl* frontend)
51 : m_frontendPage(frontendPage)
56 InspectorFrontendClientImpl::~InspectorFrontendClientImpl()
59 m_frontendHost->disconnectClient();
63 void InspectorFrontendClientImpl::windowObjectCleared()
65 v8::Isolate* isolate = v8::Isolate::GetCurrent();
66 v8::HandleScope handleScope(isolate);
67 v8::Handle<v8::Context> frameContext = m_frontendPage->mainFrame() ? toV8Context(isolate, m_frontendPage->mainFrame(), DOMWrapperWorld::mainWorld()) : v8::Local<v8::Context>();
68 v8::Context::Scope contextScope(frameContext);
71 m_frontendHost->disconnectClient();
72 m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
73 v8::Handle<v8::Value> frontendHostObj = toV8(m_frontendHost.get(), v8::Handle<v8::Object>(), frameContext->GetIsolate());
74 v8::Handle<v8::Object> global = frameContext->Global();
76 global->Set(v8::String::NewFromUtf8(isolate, "InspectorFrontendHost"), frontendHostObj);
77 ScriptController* scriptController = m_frontendPage->mainFrame() ? &m_frontendPage->mainFrame()->script() : 0;
78 if (scriptController) {
79 String installAdditionalAPI =
80 "" // Wrap messages that go to embedder.
81 "(function(host, methodNames) {"
82 " host._lastCallId = 0;"
83 " host._callbacks = [];"
84 " host.embedderMessageAck = function(id, error)"
86 " var callback = host._callbacks[id];"
87 " delete host._callbacks[id];"
91 " function dispatch(methodName)"
93 " var callId = ++host._lastCallId;"
94 " var argsArray = Array.prototype.slice.call(arguments, 1);"
95 " var callback = argsArray[argsArray.length - 1];"
96 " if (typeof callback === \"function\") {"
98 " host._callbacks[callId] = callback;"
100 " var message = { \"id\": callId, \"method\": methodName };"
101 " if (argsArray.length)"
102 " message.params = argsArray;"
103 " host.sendMessageToEmbedder(JSON.stringify(message));"
105 " methodNames.forEach(function(methodName) { host[methodName] = dispatch.bind(null, methodName); });"
106 "})(InspectorFrontendHost,"
112 " 'inspectElementCompleted',"
115 " 'removeFileSystem',"
116 " 'requestFileSystems',"
120 " 'setContentsResizingStrategy',"
126 "" // Support for legacy front-ends (<M34). Do not add items here.
127 "InspectorFrontendHost.requestSetDockSide = function(dockSide)"
129 " InspectorFrontendHost.setIsDocked(dockSide !== \"undocked\");"
131 "InspectorFrontendHost.supportsFileSystems = function() { return true; };"
133 "" // Support for legacy front-ends (<M28). Do not add items here.
134 "InspectorFrontendHost.canInspectWorkers = function() { return true; };"
135 "InspectorFrontendHost.canSaveAs = function() { return true; };"
136 "InspectorFrontendHost.canSave = function() { return true; };"
137 "InspectorFrontendHost.loaded = function() {};"
138 "InspectorFrontendHost.hiddenPanels = function() { return ""; };"
139 "InspectorFrontendHost.localizedStringsURL = function() { return ""; };"
140 "InspectorFrontendHost.close = function(url) { };";
141 scriptController->executeScriptInMainWorld(installAdditionalAPI, ScriptController::ExecuteScriptWhenScriptsDisabled);
145 void InspectorFrontendClientImpl::inspectedURLChanged(const String& url)
147 m_frontendPage->mainFrame()->document()->setTitle("Developer Tools - " + url);
150 void InspectorFrontendClientImpl::sendMessageToBackend(const String& message)
152 m_client->sendMessageToBackend(message);
155 void InspectorFrontendClientImpl::sendMessageToEmbedder(const String& message)
157 m_client->sendMessageToEmbedder(message);
160 bool InspectorFrontendClientImpl::isUnderTest()
162 return m_client->isUnderTest();