Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / inspector / WorkerDebuggerAgent.cpp
1 /*
2  * Copyright (C) 2011 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 "core/inspector/WorkerDebuggerAgent.h"
33
34 #include "bindings/v8/ScriptDebugServer.h"
35 #include "core/workers/WorkerGlobalScope.h"
36 #include "core/workers/WorkerThread.h"
37 #include "wtf/MessageQueue.h"
38
39 namespace WebCore {
40
41 namespace {
42
43 Mutex& workerDebuggerAgentsMutex()
44 {
45     AtomicallyInitializedStatic(Mutex&, mutex = *new Mutex);
46     return mutex;
47 }
48
49 typedef HashMap<WorkerThread*, WorkerDebuggerAgent*> WorkerDebuggerAgents;
50
51 WorkerDebuggerAgents& workerDebuggerAgents()
52 {
53     DEFINE_STATIC_LOCAL(WorkerDebuggerAgents, agents, ());
54     return agents;
55 }
56
57
58 class RunInspectorCommandsTask FINAL : public ScriptDebugServer::Task {
59 public:
60     explicit RunInspectorCommandsTask(WorkerThread* thread)
61         : m_thread(thread) { }
62     virtual ~RunInspectorCommandsTask() { }
63     virtual void run() OVERRIDE
64     {
65         // Process all queued debugger commands. WorkerThread is certainly
66         // alive if this task is being executed.
67         while (MessageQueueMessageReceived == m_thread->runLoop().runDebuggerTask(WorkerRunLoop::DontWaitForMessage)) { }
68     }
69
70 private:
71     WorkerThread* m_thread;
72 };
73
74 } // namespace
75
76 PassOwnPtr<WorkerDebuggerAgent> WorkerDebuggerAgent::create(WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager)
77 {
78     return adoptPtr(new WorkerDebuggerAgent(scriptDebugServer, inspectedWorkerGlobalScope, injectedScriptManager));
79 }
80
81 WorkerDebuggerAgent::WorkerDebuggerAgent(WorkerScriptDebugServer* scriptDebugServer, WorkerGlobalScope* inspectedWorkerGlobalScope, InjectedScriptManager* injectedScriptManager)
82     : InspectorDebuggerAgent(injectedScriptManager)
83     , m_scriptDebugServer(scriptDebugServer)
84     , m_inspectedWorkerGlobalScope(inspectedWorkerGlobalScope)
85 {
86     MutexLocker lock(workerDebuggerAgentsMutex());
87     workerDebuggerAgents().set(inspectedWorkerGlobalScope->thread(), this);
88 }
89
90 WorkerDebuggerAgent::~WorkerDebuggerAgent()
91 {
92     MutexLocker lock(workerDebuggerAgentsMutex());
93     ASSERT(workerDebuggerAgents().contains(m_inspectedWorkerGlobalScope->thread()));
94     workerDebuggerAgents().remove(m_inspectedWorkerGlobalScope->thread());
95 }
96
97 void WorkerDebuggerAgent::interruptAndDispatchInspectorCommands(WorkerThread* thread)
98 {
99     MutexLocker lock(workerDebuggerAgentsMutex());
100     WorkerDebuggerAgent* agent = workerDebuggerAgents().get(thread);
101     if (agent)
102         agent->m_scriptDebugServer->interruptAndRunTask(adoptPtr(new RunInspectorCommandsTask(thread)));
103 }
104
105 void WorkerDebuggerAgent::startListeningScriptDebugServer()
106 {
107     scriptDebugServer().addListener(this);
108 }
109
110 void WorkerDebuggerAgent::stopListeningScriptDebugServer()
111 {
112     scriptDebugServer().removeListener(this);
113 }
114
115 WorkerScriptDebugServer& WorkerDebuggerAgent::scriptDebugServer()
116 {
117     return *m_scriptDebugServer;
118 }
119
120 InjectedScript WorkerDebuggerAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId)
121 {
122     if (executionContextId) {
123         *error = "Execution context id is not supported for workers as there is only one execution context.";
124         return InjectedScript();
125     }
126     return injectedScriptManager()->injectedScriptFor(m_inspectedWorkerGlobalScope->script()->scriptState());
127 }
128
129 void WorkerDebuggerAgent::muteConsole()
130 {
131     // We don't need to mute console for workers.
132 }
133
134 void WorkerDebuggerAgent::unmuteConsole()
135 {
136     // We don't need to mute console for workers.
137 }
138
139 } // namespace WebCore