15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2010 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions of source code must retain the above copyright 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions in binary form must reproduce the above 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission. 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h" 325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "WebDevToolsFrontendImpl.h" 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "InspectorFrontendClientImpl.h" 355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "V8InspectorFrontendHost.h" 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "V8MouseEvent.h" 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "V8Node.h" 385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "WebDevToolsFrontendClient.h" 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "WebFrameImpl.h" 405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "WebScriptSource.h" 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "WebViewImpl.h" 4253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/ScriptController.h" 4353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/V8Binding.h" 4453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/V8DOMWrapper.h" 4553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/V8Utilities.h" 4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/Document.h" 4706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#include "core/events/Event.h" 4853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/Node.h" 4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/inspector/InspectorController.h" 5053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/inspector/InspectorFrontendHost.h" 5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/page/ContextMenuController.h" 521e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/frame/DOMWindow.h" 531e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/frame/Frame.h" 5453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/page/Page.h" 55e08f70592b3fc0d5e68b9b914c9196e813720070Torne (Richard Coles)#include "core/frame/Settings.h" 5653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/platform/Pasteboard.h" 5751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/ContextMenuItem.h" 5851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/weborigin/SecurityOrigin.h" 595267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "wtf/OwnPtr.h" 605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using namespace WebCore; 625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)namespace blink { 645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 655267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)class WebDevToolsFrontendImpl::InspectorFrontendResumeObserver : public ActiveDOMObject { 665267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) WTF_MAKE_NONCOPYABLE(InspectorFrontendResumeObserver); 675267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)public: 685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) InspectorFrontendResumeObserver(WebDevToolsFrontendImpl* webDevToolsFrontendImpl, Document* document) 695267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) : ActiveDOMObject(document) 705267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) , m_webDevToolsFrontendImpl(webDevToolsFrontendImpl) 715267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) { 725267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) suspendIfNeeded(); 735267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)private: 765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) virtual void resume() OVERRIDE 775267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) { 785267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_webDevToolsFrontendImpl->resume(); 795267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 805267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 815267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) WebDevToolsFrontendImpl* m_webDevToolsFrontendImpl; 825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}; 835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)WebDevToolsFrontend* WebDevToolsFrontend::create( 855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WebView* view, 865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WebDevToolsFrontendClient* client, 875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) const WebString& applicationLocale) 885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 89c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) return new WebDevToolsFrontendImpl(toWebViewImpl(view), client, applicationLocale); 905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)WebDevToolsFrontendImpl::WebDevToolsFrontendImpl( 935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WebViewImpl* webViewImpl, 945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WebDevToolsFrontendClient* client, 955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) const String& applicationLocale) 965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) : m_webViewImpl(webViewImpl) 975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) , m_client(client) 985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) , m_applicationLocale(applicationLocale) 995267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) , m_inspectorFrontendDispatchTimer(this, &WebDevToolsFrontendImpl::maybeDispatch) 1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 101e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles) m_webViewImpl->page()->inspectorController().setInspectorFrontendClient(adoptPtr(new InspectorFrontendClientImpl(m_webViewImpl->page(), m_client, this))); 1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)WebDevToolsFrontendImpl::~WebDevToolsFrontendImpl() 1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void WebDevToolsFrontendImpl::dispatchOnInspectorFrontend(const WebString& message) 1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 1105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_messages.append(message); 1115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) maybeDispatch(0); 1125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void WebDevToolsFrontendImpl::resume() 1155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 1165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) // We should call maybeDispatch asynchronously here because we are not allowed to update activeDOMObjects list in 1171e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) // resume (See ExecutionContext::resumeActiveDOMObjects). 1185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (!m_inspectorFrontendDispatchTimer.isActive()) 1195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_inspectorFrontendDispatchTimer.startOneShot(0); 1205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void WebDevToolsFrontendImpl::maybeDispatch(WebCore::Timer<WebDevToolsFrontendImpl>*) 1235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 1245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) while (!m_messages.isEmpty()) { 1255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) Document* document = m_webViewImpl->page()->mainFrame()->document(); 1265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) if (document->activeDOMObjectsAreSuspended()) { 1275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_inspectorFrontendResumeObserver = adoptPtr(new InspectorFrontendResumeObserver(this, document)); 1285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) return; 1295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 1305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) m_inspectorFrontendResumeObserver.clear(); 1315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) doDispatchOnInspectorFrontend(m_messages.takeFirst()); 1325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) } 1335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)} 1345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) 1355267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void WebDevToolsFrontendImpl::doDispatchOnInspectorFrontend(const WebString& message) 1365267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles){ 1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) WebFrameImpl* frame = m_webViewImpl->mainFrameImpl(); 1388abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) if (!frame->frame()) 1398abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) return; 1409bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles) v8::Isolate* isolate = toIsolate(frame->frame()); 1418abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles) v8::HandleScope scope(isolate); 142f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles) v8::Handle<v8::Context> frameContext = frame->frame()->script().currentWorldContext(); 1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) v8::Context::Scope contextScope(frameContext); 144a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) v8::Handle<v8::Value> inspectorFrontendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorFrontendAPI")); 1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!inspectorFrontendApiValue->IsObject()) 1465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return; 147c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) v8::Handle<v8::Object> dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorFrontendApiValue); 148a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) v8::Handle<v8::Value> dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatchMessage")); 149c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) // The frame might have navigated away from the front-end page (which is still weird), 150c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) // OR the older version of frontend might have a dispatch method in a different place. 151c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) // FIXME(kaznacheev): Remove when Chrome for Android M18 is retired. 152c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) if (!dispatchFunction->IsFunction()) { 153a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) v8::Handle<v8::Value> inspectorBackendApiValue = frameContext->Global()->Get(v8::String::NewFromUtf8(isolate, "InspectorBackend")); 154c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) if (!inspectorBackendApiValue->IsObject()) 155c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) return; 156c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) dispatcherObject = v8::Handle<v8::Object>::Cast(inspectorBackendApiValue); 157a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) dispatchFunction = dispatcherObject->Get(v8::String::NewFromUtf8(isolate, "dispatch")); 158c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) if (!dispatchFunction->IsFunction()) 159c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) return; 160c0e19a689c8ac22cdc96b291a8d33a5d3b0b34a4Torne (Richard Coles) } 1615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(dispatchFunction); 1625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) Vector< v8::Handle<v8::Value> > args; 163a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) args.append(v8String(isolate, message)); 1645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) v8::TryCatch tryCatch; 1655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) tryCatch.SetVerbose(true); 1661e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) ScriptController::callFunction(frame->frame()->document(), function, dispatcherObject, args.size(), args.data(), isolate); 1675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 1685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 16951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)} // namespace blink 170