Home | History | Annotate | Download | only in web
      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 "WebDevToolsFrontendImpl.h"
     33 
     34 #include "InspectorFrontendClientImpl.h"
     35 #include "V8InspectorFrontendHost.h"
     36 #include "V8MouseEvent.h"
     37 #include "V8Node.h"
     38 #include "WebDevToolsFrontendClient.h"
     39 #include "WebFrameImpl.h"
     40 #include "WebScriptSource.h"
     41 #include "WebViewImpl.h"
     42 #include "bindings/v8/ScriptController.h"
     43 #include "bindings/v8/V8Binding.h"
     44 #include "bindings/v8/V8DOMWrapper.h"
     45 #include "bindings/v8/V8Utilities.h"
     46 #include "core/dom/Document.h"
     47 #include "core/events/Event.h"
     48 #include "core/dom/Node.h"
     49 #include "core/inspector/InspectorController.h"
     50 #include "core/inspector/InspectorFrontendHost.h"
     51 #include "core/page/ContextMenuController.h"
     52 #include "core/frame/DOMWindow.h"
     53 #include "core/frame/Frame.h"
     54 #include "core/page/Page.h"
     55 #include "core/frame/Settings.h"
     56 #include "core/platform/Pasteboard.h"
     57 #include "platform/ContextMenuItem.h"
     58 #include "platform/weborigin/SecurityOrigin.h"
     59 #include "wtf/OwnPtr.h"
     60 
     61 using namespace WebCore;
     62 
     63 namespace blink {
     64 
     65 class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDOMObject {
     66     WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver);
     67 public:
     68     InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontendImpl, Document* document)
     69         : ActiveDOMObject(document)
     70         , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl)
     71     {
     72         suspendIfNeeded();
     73     }
     74 
     75 private:
     76     virtual void resume() OVERRIDE
     77     {
     78         m_webDevToolsFrontendImpl->resume();
     79     }
     80 
     81     WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl;
     82 };
     83 
     84 WebDevToolsFrontend* WebDevToolsFrontend::create(
     85     WebView* view,
     86     WebDevToolsFrontendClient* client,
     87     const WebString& applicationLocale)
     88 {
     89     return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationLocale);
     90 }
     91 
     92 WebDevToolsFrontendImpl::WebDevToolsFrontendImpl(
     93     WebViewImpl* webViewImpl,
     94     WebDevToolsFrontendClient* client,
     95     const String& applicationLocale)
     96     : m_webViewImpl(webViewImpl)
     97     , m_client(client)
     98     , m_applicationLocale(applicationLocale)
     99     , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDispatch)
    100 {
    101     m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adoptPtr(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this)));
    102 }
    103 
    104 WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl()
    105 {
    106 }
    107 
    108 void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message)
    109 {
    110     m_messages.append(message);
    111     maybeDispatch(0);
    112 }
    113 
    114 void WebDevToolsFrontendImpl::resume()
    115 {
    116     // We should call maybeDispatch asynchronously here because we are not allowed to update activeDOMObjects list in
    117     // resume (See ExecutionContext::resumeActiveDOMObjects).
    118     if (!m_inspectorFrontendDispatchTimer.isActive())
    119         m_inspectorFrontendDispatchTimer.startOneShot(0);
    120 }
    121 
    122 void WebDevToolsFrontendImpl::maybeDispatch(WebCore::Timer<WebDevToolsFrontendImpl>*)
    123 {
    124     while (!m_messages.isEmpty()) {
    125         Document* document = m_webViewImpl->page()->mainFrame()->document();
    126         if (document->activeDOMObjectsAreSuspended()) {
    127             m_inspectorFrontendResumeObserver = adoptPtr(new InspectorFrontendResumeObserver(this, document));
    128             return;
    129         }
    130         m_inspectorFrontendResumeObserver.clear();
    131         doDispatchOnInspectorFrontend(m_messages.takeFirst());
    132     }
    133 }
    134 
    135 void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& message)
    136 {
    137     WebFrameImpl* frame = m_webViewImpl->mainFrameImpl();
    138     if (!frame->frame())
    139         return;
    140     v8::Isolate* isolate = toIsolate(frame->frame());
    141     v8::HandleScope scope(isolate);
    142     v8::Handle<v8::Context> frameContext = frame->frame()->script().currentWorldContext();
    143     v8::Context::Scope contextScope(frameContext);
    144     v8::Handle<v8::Value> inspectorFrontendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI"));
    145     if (!inspectorFrontendApiValue->IsObject())
    146         return;
    147     v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorFrontendApiValue);
    148     v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatchMessage"));
    149     // The frame might have navigated away from the front-end page (which is still weird),
    150     // OR the older version of frontend might have a dispatch method in a different place.
    151     // FIXME(kaznacheev): Remove when Chrome for Android M18 is retired.
    152     if (!dispatchFunction->IsFunction()) {
    153         v8::Handle<v8::Value> inspectorBackendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend"));
    154         if (!inspectorBackendApiValue->IsObject())
    155             return;
    156         dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue);
    157         dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatch"));
    158         if (!dispatchFunction->IsFunction())
    159             return;
    160     }
    161     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction);
    162     Vector< v8::Handle<v8::Value> > args;
    163     args.append(v8String(isolate, message));
    164     v8::TryCatch tryCatch;
    165     tryCatch.SetVerbose(true);
    166     ScriptController::callFunction(frame->frame()->document(), function, dispatcherObject, args.size(), args.data(), isolate);
    167 }
    168 
    169 } // namespace blink
    170