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 "bindings/v8/ScriptState.h"
36 #include "core/inspector/InspectorController.h"
37 #include "core/inspector/InspectorFrontendHost.h"
38 #include "core/page/Page.h"
39 #include "core/frame/Settings.h"
40 #include "platform/Timer.h"
41 #include "public/platform/Platform.h"
42 #include "public/platform/WebThread.h"
43 #include "wtf/Deque.h"
44
45 namespace WebCore {
46
47 class InspectorBackendMessageQueue : public RefCounted<InspectorBackendMessageQueue> {
48 WTF_MAKE_FAST_ALLOCATED;
49 public:
InspectorBackendMessageQueue(InspectorController & inspectorController)50 explicit InspectorBackendMessageQueue(InspectorController& inspectorController)
51 : m_inspectorController(&inspectorController)
52 {
53 }
54
dispatch(const String & message)55 void dispatch(const String& message)
56 {
57 ASSERT(m_inspectorController);
58 m_messages.append(message);
59 schedule();
60 }
61
stop()62 void stop()
63 {
64 m_inspectorController = 0;
65 m_messages.clear();
66 }
67
68 private:
schedule()69 void schedule()
70 {
71 class TaskImpl : public blink::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 blink::Platform::current()->currentThread()->postTask(taskImpl);
82 }
83
deliver()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
InspectorFrontendClientLocal(InspectorController & inspectorController,Page * frontendPage)99 InspectorFrontendClientLocal::InspectorFrontendClientLocal(InspectorController& inspectorController, Page* frontendPage)
100 : m_frontendPage(frontendPage)
101 {
102 m_frontendPage->settings().setAllowFileAccessFromFileURLs(true);
103 m_messageQueue = adoptRef(new InspectorBackendMessageQueue(inspectorController));
104 }
105
~InspectorFrontendClientLocal()106 InspectorFrontendClientLocal::~InspectorFrontendClientLocal()
107 {
108 m_messageQueue->stop();
109 if (m_frontendHost)
110 m_frontendHost->disconnectClient();
111 m_frontendPage = 0;
112 }
113
windowObjectCleared()114 void InspectorFrontendClientLocal::windowObjectCleared()
115 {
116 if (m_frontendHost)
117 m_frontendHost->disconnectClient();
118 ScriptState* frontendScriptState = mainWorldScriptState(m_frontendPage->mainFrame());
119 m_frontendHost = InspectorFrontendHost::create(this, m_frontendPage);
120 ScriptGlobalObject::set(frontendScriptState, "InspectorFrontendHost", m_frontendHost.get());
121 }
122
sendMessageToBackend(const String & message)123 void InspectorFrontendClientLocal::sendMessageToBackend(const String& message)
124 {
125 m_messageQueue->dispatch(message);
126 }
127
128 } // namespace WebCore
129
130