Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / workers / WorkerInspectorProxy.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6
7 #include "core/workers/WorkerInspectorProxy.h"
8
9 #include "core/dom/CrossThreadTask.h"
10 #include "core/inspector/InspectorInstrumentation.h"
11 #include "core/inspector/InspectorTraceEvents.h"
12 #include "core/inspector/WorkerInspectorController.h"
13 #include "core/workers/WorkerThread.h"
14 #include "platform/TraceEvent.h"
15 #include "platform/weborigin/KURL.h"
16
17 namespace blink {
18
19 WorkerInspectorProxy::WorkerInspectorProxy()
20     : m_workerThread(nullptr)
21     , m_executionContext(nullptr)
22     , m_pageInspector(nullptr)
23 {
24 }
25
26 PassOwnPtr<WorkerInspectorProxy> WorkerInspectorProxy::create()
27 {
28     return adoptPtr(new WorkerInspectorProxy());
29 }
30
31 WorkerInspectorProxy::~WorkerInspectorProxy()
32 {
33 }
34
35 void WorkerInspectorProxy::workerThreadCreated(ExecutionContext* context, WorkerThread* workerThread, const KURL& url)
36 {
37     m_workerThread = workerThread;
38     m_executionContext = context;
39     InspectorInstrumentation::didStartWorker(context, this, url);
40 }
41
42 void WorkerInspectorProxy::workerThreadTerminated()
43 {
44     if (m_workerThread)
45         InspectorInstrumentation::workerTerminated(m_executionContext, this);
46     m_workerThread = nullptr;
47     m_pageInspector = nullptr;
48 }
49
50 static void connectToWorkerGlobalScopeInspectorTask(ExecutionContext* context, bool)
51 {
52     toWorkerGlobalScope(context)->workerInspectorController()->connectFrontend();
53 }
54
55 void WorkerInspectorProxy::connectToInspector(WorkerInspectorProxy::PageInspector* pageInspector)
56 {
57     if (!m_workerThread)
58         return;
59     ASSERT(!m_pageInspector);
60     m_pageInspector = pageInspector;
61     m_workerThread->postDebuggerTask(createCrossThreadTask(connectToWorkerGlobalScopeInspectorTask, true));
62 }
63
64 static void disconnectFromWorkerGlobalScopeInspectorTask(ExecutionContext* context, bool)
65 {
66     toWorkerGlobalScope(context)->workerInspectorController()->disconnectFrontend();
67 }
68
69 void WorkerInspectorProxy::disconnectFromInspector()
70 {
71     m_pageInspector = nullptr;
72     if (!m_workerThread)
73         return;
74     m_workerThread->postDebuggerTask(createCrossThreadTask(disconnectFromWorkerGlobalScopeInspectorTask, true));
75 }
76
77 static void dispatchOnInspectorBackendTask(ExecutionContext* context, const String& message)
78 {
79     toWorkerGlobalScope(context)->workerInspectorController()->dispatchMessageFromFrontend(message);
80 }
81
82 void WorkerInspectorProxy::sendMessageToInspector(const String& message)
83 {
84     if (!m_workerThread)
85         return;
86     m_workerThread->postDebuggerTask(createCrossThreadTask(dispatchOnInspectorBackendTask, String(message)));
87     m_workerThread->interruptAndDispatchInspectorCommands();
88 }
89
90 void WorkerInspectorProxy::writeTimelineStartedEvent(const String& sessionId)
91 {
92     if (!m_workerThread)
93         return;
94     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "TracingSessionIdForWorker", "data", InspectorTracingSessionIdForWorkerEvent::data(sessionId, m_workerThread));
95 }
96
97 } // namespace blink