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 "web/WebDevToolsFrontendImpl.h" 33 34 #include "V8InspectorFrontendHost.h" 35 #include "V8MouseEvent.h" 36 #include "V8Node.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/LocalDOMWindow.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" 59 60 using namespace WebCore; 61 62 namespace blink { 63 64 class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDOMObject { 65 WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver); 66 public: 67 InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontendImpl, Document* document) 68 : ActiveDOMObject(document) 69 , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl) 70 { 71 suspendIfNeeded(); 72 } 73 74 private: 75 virtual void resume() OVERRIDE 76 { 77 m_webDevToolsFrontendImpl->resume(); 78 } 79 80 WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl; 81 }; 82 83 WebDevToolsFrontend* WebDevToolsFrontend::create( 84 WebView* view, 85 WebDevToolsFrontendClient* client, 86 const WebString& applicationLocale) 87 { 88 return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationLocale); 89 } 90 91 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl( 92 WebViewImpl* webViewImpl, 93 WebDevToolsFrontendClient* client, 94 const String& applicationLocale) 95 : m_webViewImpl(webViewImpl) 96 , m_client(client) 97 , m_applicationLocale(applicationLocale) 98 , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDispatch) 99 { 100 m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adoptPtr(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this))); 101 } 102 103 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl() 104 { 105 } 106 107 void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message) 108 { 109 m_messages.append(message); 110 maybeDispatch(0); 111 } 112 113 void WebDevToolsFrontendImpl::resume() 114 { 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); 119 } 120 121 void WebDevToolsFrontendImpl::maybeDispatch(WebCore::Timer<WebDevToolsFrontendImpl>*) 122 { 123 while (!m_messages.isEmpty()) { 124 Document* document = m_webViewImpl->page()->deprecatedLocalMainFrame()->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 134 void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& message) 135 { 136 WebLocalFrameImpl* frame = m_webViewImpl->mainFrameImpl(); 137 if (!frame->frame()) 138 return; 139 v8::Isolate* isolate = toIsolate(frame->frame()); 140 ScriptState* scriptState = ScriptState::forMainWorld(frame->frame()); 141 ScriptState::Scope scope(scriptState); 142 v8::Handle<v8::Value> inspectorFrontendApiValue = scriptState->context()->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI")); 143 if (!inspectorFrontendApiValue->IsObject()) 144 return; 145 v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorFrontendApiValue); 146 v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatchMessage")); 147 // The frame might have navigated away from the front-end page (which is still weird), 148 // OR the older version of frontend might have a dispatch method in a different place. 149 // FIXME(kaznacheev): Remove when Chrome for Android M18 is retired. 150 if (!dispatchFunction->IsFunction()) { 151 v8::Handle<v8::Value> inspectorBackendApiValue = scriptState->context()->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend")); 152 if (!inspectorBackendApiValue->IsObject()) 153 return; 154 dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue); 155 dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatch")); 156 if (!dispatchFunction->IsFunction()) 157 return; 158 } 159 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction); 160 Vector< v8::Handle<v8::Value> > args; 161 args.append(v8String(isolate, message)); 162 v8::TryCatch tryCatch; 163 tryCatch.SetVerbose(true); 164 ScriptController::callFunction(frame->frame()->document(), function, dispatcherObject, args.size(), args.data(), isolate); 165 } 166 167 } // namespace blink 168