blob: c36baf89fbf06fc799465f39371bd5140613f73e [file] [log] [blame]
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +00001/*
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 "WebDevToolsFrontendImpl.h"
33
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000034#include "InspectorFrontendClientImpl.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000035#include "V8InspectorFrontendHost.h"
36#include "V8MouseEvent.h"
37#include "V8Node.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000038#include "WebDevToolsFrontendClient.h"
39#include "WebFrameImpl.h"
40#include "WebScriptSource.h"
41#include "WebViewImpl.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010042#include "bindings/v8/ScriptController.h"
43#include "bindings/v8/V8Binding.h"
44#include "bindings/v8/V8DOMWrapper.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000045#include "core/clipboard/Pasteboard.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010046#include "core/dom/Document.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010047#include "core/dom/Node.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000048#include "core/events/Event.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010049#include "core/inspector/InspectorController.h"
50#include "core/inspector/InspectorFrontendHost.h"
51#include "core/page/ContextMenuController.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010052#include "core/frame/DOMWindow.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000053#include "core/frame/LocalFrame.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010054#include "core/page/Page.h"
Torne (Richard Coles)09380292014-02-21 12:17:33 +000055#include "core/frame/Settings.h"
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000056#include "platform/ContextMenuItem.h"
57#include "platform/weborigin/SecurityOrigin.h"
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010058#include "wtf/OwnPtr.h"
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000059
60using namespace WebCore;
61
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000062namespace blink {
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000063
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010064class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDOMObject {
65 WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver);
66public:
67 InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontendImpl, Document* document)
68 : ActiveDOMObject(document)
69 , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl)
70 {
71 suspendIfNeeded();
72 }
73
74private:
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010075 virtual void resume() OVERRIDE
76 {
77 m_webDevToolsFrontendImpl->resume();
78 }
79
80 WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl;
81};
82
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000083WebDevToolsFrontend* WebDevToolsFrontend::create(
84 WebView* view,
85 WebDevToolsFrontendClient* client,
86 const WebString& applicationLocale)
87{
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +010088 return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationLocale);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000089}
90
91WebDevToolsFrontendImpl::WebDevToolsFrontendImpl(
92 WebViewImpl* webViewImpl,
93 WebDevToolsFrontendClient* client,
94 const String& applicationLocale)
95 : m_webViewImpl(webViewImpl)
96 , m_client(client)
97 , m_applicationLocale(applicationLocale)
Torne (Richard Coles)5267f702013-06-11 10:57:24 +010098 , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDispatch)
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +000099{
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100100 m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adoptPtr(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this)));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000101}
102
103WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl()
104{
105}
106
107void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message)
108{
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100109 m_messages.append(message);
110 maybeDispatch(0);
111}
112
113void WebDevToolsFrontendImpl::resume()
114{
115 // We should call maybeDispatch asynchronously here because we are not allowed to update activeDOMObjects list in
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100116 // resume (See ExecutionContext::resumeActiveDOMObjects).
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100117 if (!m_inspectorFrontendDispatchTimer.isActive())
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000118 m_inspectorFrontendDispatchTimer.startOneShot(0, FROM_HERE);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100119}
120
121void WebDevToolsFrontendImpl::maybeDispatch(WebCore::Timer<WebDevToolsFrontendImpl>*)
122{
123 while (!m_messages.isEmpty()) {
124 Document* document = m_webViewImpl->page()->mainFrame()->document();
125 if (document->activeDOMObjectsAreSuspended()) {
126 m_inspectorFrontendResumeObserver = adoptPtr(new InspectorFrontendResumeObserver(this, document));
127 return;
128 }
129 m_inspectorFrontendResumeObserver.clear();
130 doDispatchOnInspectorFrontend(m_messages.takeFirst());
131 }
132}
133
134void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& message)
135{
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000136 WebFrameImpl* frame = m_webViewImpl->mainFrameImpl();
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100137 if (!frame->frame())
138 return;
Torne (Richard Coles)9bbd2f52013-09-19 22:37:05 +0100139 v8::Isolate* isolate = toIsolate(frame->frame());
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +0100140 v8::HandleScope scope(isolate);
Torne (Richard Coles)09380292014-02-21 12:17:33 +0000141 v8::Handle<v8::Context> frameContext = toV8Context(isolate, frame->frame(), DOMWrapperWorld::mainWorld());
142 if (frameContext.IsEmpty())
143 return;
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000144 v8::Context::Scope contextScope(frameContext);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000145 v8::Handle<v8::Value> inspectorFrontendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI"));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000146 if (!inspectorFrontendApiValue->IsObject())
147 return;
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100148 v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorFrontendApiValue);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000149 v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatchMessage"));
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100150 // The frame might have navigated away from the front-end page (which is still weird),
151 // OR the older version of frontend might have a dispatch method in a different place.
152 // FIXME(kaznacheev): Remove when Chrome for Android M18 is retired.
153 if (!dispatchFunction->IsFunction()) {
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000154 v8::Handle<v8::Value> inspectorBackendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend"));
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100155 if (!inspectorBackendApiValue->IsObject())
156 return;
157 dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue);
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000158 dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatch"));
Torne (Richard Coles)c0e19a62013-08-30 15:15:11 +0100159 if (!dispatchFunction->IsFunction())
160 return;
161 }
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000162 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
163 Vector< v8::Handle<v8::Value> > args;
Torne (Richard Coles)a854de02013-12-18 16:25:25 +0000164 args.append(v8String(isolate, message));
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000165 v8::TryCatch tryCatch;
166 tryCatch.SetVerbose(true);
Torne (Richard Coles)1e202182013-10-18 15:46:42 +0100167 ScriptController::callFunction(frame->frame()->document(), function, dispatcherObject, args.size(), args.data(), isolate);
Torne (Richard Coles)5c87bf82012-11-14 11:46:17 +0000168}
169
Torne (Richard Coles)51b29062013-11-28 11:56:03 +0000170} // namespace blink