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 "web/WebDevToolsFrontendImpl.h"
34 #include "V8InspectorFrontendHost.h"
35 #include "V8MouseEvent.h"
37 #include "bindings/v8/ScriptController.h"
38 #include "bindings/v8/V8Binding.h"
39 #include "bindings/v8/V8DOMWrapper.h"
40 #include "core/clipboard/Pasteboard.h"
41 #include "core/dom/Document.h"
42 #include "core/dom/Node.h"
43 #include "core/events/Event.h"
44 #include "core/frame/DOMWindow.h"
45 #include "core/frame/LocalFrame.h"
46 #include "core/frame/Settings.h"
47 #include "core/inspector/InspectorController.h"
48 #include "core/inspector/InspectorFrontendHost.h"
49 #include "core/page/ContextMenuController.h"
50 #include "core/page/Page.h"
51 #include "platform/ContextMenuItem.h"
52 #include "platform/weborigin/SecurityOrigin.h"
53 #include "public/web/WebDevToolsFrontendClient.h"
54 #include "public/web/WebScriptSource.h"
55 #include "web/InspectorFrontendClientImpl.h"
56 #include "web/WebLocalFrameImpl.h"
57 #include "web/WebViewImpl.h"
58 #include "wtf/OwnPtr.h"
60 using namespace WebCore;
64 class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDOMObject {
65 WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver);
67 InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontendImpl, Document* document)
68 : ActiveDOMObject(document)
69 , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl)
75 virtual void resume() OVERRIDE
77 m_webDevToolsFrontendImpl->resume();
80 WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl;
83 WebDevToolsFrontend* WebDevToolsFrontend::create(
85 WebDevToolsFrontendClient* client,
86 const WebString& applicationLocale)
88 return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationLocale);
91 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl(
92 WebViewImpl* webViewImpl,
93 WebDevToolsFrontendClient* client,
94 const String& applicationLocale)
95 : m_webViewImpl(webViewImpl)
97 , m_applicationLocale(applicationLocale)
98 , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDispatch)
100 m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adoptPtr(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this)));
103 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl()
107 void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message)
109 m_messages.append(message);
113 void WebDevToolsFrontendImpl::resume()
115 // We should call maybeDispatch asynchronously here because we are not allowed to update activeDOMObjects list in
116 // resume (See ExecutionContext::resumeActiveDOMObjects).
117 if (!m_inspectorFrontendDispatchTimer.isActive())
118 m_inspectorFrontendDispatchTimer.startOneShot(0, FROM_HERE);
121 void WebDevToolsFrontendImpl::maybeDispatch(WebCore::Timer<WebDevToolsFrontendImpl>*)
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));
129 m_inspectorFrontendResumeObserver.clear();
130 doDispatchOnInspectorFrontend(m_messages.takeFirst());
134 void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& message)
136 WebLocalFrameImpl* frame = m_webViewImpl->mainFrameImpl();
139 v8::Isolate* isolate = toIsolate(frame->frame());
140 v8::HandleScope scope(isolate);
141 v8::Handle<v8::Context> frameContext = toV8Context(isolate, frame->frame(), DOMWrapperWorld::mainWorld());
142 if (frameContext.IsEmpty())
144 v8::Context::Scope contextScope(frameContext);
145 v8::Handle<v8::Value> inspectorFrontendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI"));
146 if (!inspectorFrontendApiValue->IsObject())
148 v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorFrontendApiValue);
149 v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatchMessage"));
150 // 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()) {
154 v8::Handle<v8::Value> inspectorBackendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend"));
155 if (!inspectorBackendApiValue->IsObject())
157 dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue);
158 dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatch"));
159 if (!dispatchFunction->IsFunction())
162 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
163 Vector< v8::Handle<v8::Value> > args;
164 args.append(v8String(isolate, message));
165 v8::TryCatch tryCatch;
166 tryCatch.SetVerbose(true);
167 ScriptController::callFunction(frame->frame()->document(), function, dispatcherObject, args.size(), args.data(), isolate);