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 "InspectorFrontendClientLocal.h" 33 34#include "bindings/v8/ScriptObject.h" 35#include "core/inspector/InspectorController.h" 36#include "core/inspector/InspectorFrontendHost.h" 37#include "core/page/Page.h" 38#include "core/page/Settings.h" 39#include "core/platform/Timer.h" 40#include "public/platform/Platform.h" 41#include "public/platform/WebThread.h" 42#include <wtf/Deque.h> 43#include <wtf/text/WTFString.h> 44 45namespace WebCore { 46 47class InspectorBackendMessageQueue : public RefCounted<InspectorBackendMessageQueue> { 48 WTF_MAKE_FAST_ALLOCATED; 49public: 50 explicit InspectorBackendMessageQueue(InspectorController* inspectorController) 51 : m_inspectorController(inspectorController) 52 { 53 } 54 55 void dispatch(const String& message) 56 { 57 ASSERT(m_inspectorController); 58 m_messages.append(message); 59 schedule(); 60 } 61 62 void stop() 63 { 64 m_inspectorController = 0; 65 m_messages.clear(); 66 } 67 68private: 69 void schedule() 70 { 71 class TaskImpl : public WebKit::WebThread::Task { 72 public: 73 RefPtr<InspectorBackendMessageQueue> owner; 74 virtual void run() 75 { 76 owner->deliver(); 77 } 78 }; 79 TaskImpl* taskImpl = new TaskImpl; 80 taskImpl->owner = this; 81 WebKit::Platform::current()->currentThread()->postTask(taskImpl); 82 } 83 84 void deliver() 85 { 86 if (!m_inspectorController) 87 return; 88 if (!m_messages.isEmpty()) { 89 // Dispatch can lead to the timer destruction -> schedule the next shot first. 90 schedule(); 91 m_inspectorController->dispatchMessageFromFrontend(m_messages.takeFirst()); 92 } 93 } 94 95 InspectorController* m_inspectorController; 96 Deque<String> m_messages; 97}; 98 99InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController* inspectorController, Page* frontendPage) 100 : m_inspectorController(inspectorController) 101 , m_frontendPage(frontendPage) 102{ 103 m_frontendPage->settings()->setAllowFileAccessFromFileURLs(true); 104 m_messageQueue = adoptRef(new InspectorBackendMessageQueue(inspectorController)); 105} 106 107InspectorFrontendClientLocal::~InspectorFrontendClientLocal() 108{ 109 m_messageQueue->stop(); 110 if (m_frontendHost) 111 m_frontendHost->disconnectClient(); 112 m_frontendPage = 0; 113 m_inspectorController = 0; 114} 115 116void InspectorFrontendClientLocal::windowObjectCleared() 117{ 118 if (m_frontendHost) 119 m_frontendHost->disconnectClient(); 120 ScriptState* frontendScriptState = mainWorldScriptState(m_frontendPage->mainFrame()); 121 m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage); 122 ScriptGlobalObject::set(frontendScriptState, "InspectorFrontendHost", m_frontendHost.get()); 123} 124 125void InspectorFrontendClientLocal::sendMessageToBackend(const String& message) 126{ 127 m_messageQueue->dispatch(message); 128} 129 130} // namespace WebCore 131 132