Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / InspectorFrontendClientImpl.cpp
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() ? toV8Context(isolate, m_frontendPage->mainFrame(), DOMWrapperWorld::mainWorld()) : 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     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)"
85             "    {"
86             "        var callback = host._callbacks[id];"
87             "        delete host._callbacks[id];"
88             "        if (callback)"
89             "            callback(error);"
90             "    };"
91             "    function dispatch(methodName)"
92             "    {"
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\") {"
97             "            argsArray.pop();"
98             "            host._callbacks[callId] = callback;"
99             "        }"
100             "        var message = { \"id\": callId, \"method\": methodName };"
101             "        if (argsArray.length)"
102             "            message.params = argsArray;"
103             "        host.sendMessageToEmbedder(JSON.stringify(message));"
104             "    };"
105             "    methodNames.forEach(function(methodName) { host[methodName] = dispatch.bind(null, methodName); });"
106             "})(InspectorFrontendHost,"
107             "    ['addFileSystem',"
108             "     'append',"
109             "     'bringToFront',"
110             "     'closeWindow',"
111             "     'indexPath',"
112             "     'inspectElementCompleted',"
113             "     'moveWindowBy',"
114             "     'openInNewTab',"
115             "     'removeFileSystem',"
116             "     'requestFileSystems',"
117             "     'resetZoom',"
118             "     'save',"
119             "     'searchInPath',"
120             "     'setContentsResizingStrategy',"
121             "     'setIsDocked',"
122             "     'stopIndexing',"
123             "     'zoomIn',"
124             "     'zoomOut']);"
125             ""
126             "" // Support for legacy front-ends (<M34). Do not add items here.
127             "InspectorFrontendHost.requestSetDockSide = function(dockSide)"
128             "{"
129             "    InspectorFrontendHost.setIsDocked(dockSide !== \"undocked\");"
130             "};"
131             "InspectorFrontendHost.supportsFileSystems = function() { return true; };"
132             ""
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);
142     }
143 }
144
145 void InspectorFrontendClientImpl::inspectedURLChanged(const String& url)
146 {
147     m_frontendPage->mainFrame()->document()->setTitle("Developer Tools - " + url);
148 }
149
150 void InspectorFrontendClientImpl::sendMessageToBackend(const String& message)
151 {
152     m_client->sendMessageToBackend(message);
153 }
154
155 void InspectorFrontendClientImpl::sendMessageToEmbedder(const String& message)
156 {
157     m_client->sendMessageToEmbedder(message);
158 }
159
160 bool InspectorFrontendClientImpl::isUnderTest()
161 {
162     return m_client->isUnderTest();
163 }
164
165 } // namespace blink