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 "InspectorFrontendClientImpl.h" 33 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" 45 46 using namespace WebCore; 47 48 namespace blink { 49 50 InspectorFrontendClientImpl::InspectorFrontendClientImpl(Page* frontendPage, WebDevToolsFrontendClient* client, WebDevToolsFrontendImpl* frontend) 51 : m_frontendPage(frontendPage) 52 , m_client(client) 53 { 54 } 55 56 InspectorFrontendClientImpl::~InspectorFrontendClientImpl() 57 { 58 if (m_frontendHost) 59 m_frontendHost->disconnectClient(); 60 m_client = 0; 61 } 62 63 void InspectorFrontendClientImpl::windowObjectCleared() 64 { 65 v8::Isolate* isolate = v8::Isolate::GetCurrent(); 66 v8::HandleScope handleScope(isolate); 67 v8::Handle<v8::Context> frameContext = m_frontendPage->mainFrame() ? m_frontendPage->mainFrame()->script().currentWorldContext() : v8::Local<v8::Context>(); 68 v8::Context::Scope contextScope(frameContext); 69 70 if (m_frontendHost) 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(); 75 76 global->Set(v8::String::NewFromUtf8(isolate, "InspectorFrontendHost"), frontendHostObj); 77 78 ScriptController* scriptController = m_frontendPage->mainFrame() ? &m_frontendPage->mainFrame()->script() : 0; 79 if (scriptController) { 80 String installLegacyOverrides = 81 "" // Support for legacy front-ends (<M31). Do not add items here. 82 "(function(host, methodNames) {" 83 " var callId = 0;" 84 " function dispatch(methodName)" 85 " {" 86 " var argsArray = Array.prototype.slice.call(arguments, 1);" 87 " var message = {\"method\": methodName, \"id\": ++callId};" 88 " if (argsArray.length)" 89 " message.params = argsArray;" 90 " this.sendMessageToEmbedder(JSON.stringify(message));" 91 " };" 92 " methodNames.forEach(function(methodName) { host[methodName] = dispatch.bind(host, methodName); });" 93 "})(InspectorFrontendHost," 94 " ['addFileSystem'," 95 " 'append'," 96 " 'bringToFront'," 97 " 'indexPath'," 98 " 'moveWindowBy'," 99 " 'openInNewTab'," 100 " 'removeFileSystem'," 101 " 'requestFileSystems'," 102 " 'requestSetDockSide'," 103 " 'save'," 104 " 'searchInPath'," 105 " 'stopIndexing']);" 106 "" 107 "" // Support for legacy front-ends (<M28). Do not add items here. 108 "InspectorFrontendHost.canInspectWorkers = function() { return true; };" 109 "InspectorFrontendHost.canSaveAs = function() { return true; };" 110 "InspectorFrontendHost.canSave = function() { return true; };" 111 "InspectorFrontendHost.supportsFileSystems = function() { return true; };" 112 "InspectorFrontendHost.loaded = function() {};" 113 "InspectorFrontendHost.hiddenPanels = function() { return ""; };" 114 "InspectorFrontendHost.localizedStringsURL = function() { return ""; };" 115 "InspectorFrontendHost.close = function(url) { };"; 116 scriptController->executeScriptInMainWorld(installLegacyOverrides, ScriptController::ExecuteScriptWhenScriptsDisabled); 117 } 118 } 119 120 void InspectorFrontendClientImpl::inspectedURLChanged(const String& url) 121 { 122 m_frontendPage->mainFrame()->document()->setTitle("Developer Tools - " + url); 123 } 124 125 void InspectorFrontendClientImpl::sendMessageToBackend(const String& message) 126 { 127 m_client->sendMessageToBackend(message); 128 } 129 130 void InspectorFrontendClientImpl::sendMessageToEmbedder(const String& message) 131 { 132 m_client->sendMessageToEmbedder(message); 133 } 134 135 bool InspectorFrontendClientImpl::isUnderTest() 136 { 137 return m_client->isUnderTest(); 138 } 139 140 } // namespace blink 141