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
33#include "core/inspector/WorkerRuntimeAgent.h"
34
35#include "bindings/core/v8/ScriptState.h"
36#include "core/inspector/InjectedScript.h"
37#include "core/inspector/InstrumentingAgents.h"
38#include "core/inspector/WorkerDebuggerAgent.h"
39#include "core/workers/WorkerGlobalScope.h"
40#include "core/workers/WorkerThread.h"
41
42namespace blink {
43
44WorkerRuntimeAgent::WorkerRuntimeAgent(InjectedScriptManager* injectedScriptManager, ScriptDebugServer* scriptDebugServer, WorkerGlobalScope* workerGlobalScope)
45    : InspectorRuntimeAgent(injectedScriptManager, scriptDebugServer)
46    , m_workerGlobalScope(workerGlobalScope)
47    , m_paused(false)
48{
49}
50
51WorkerRuntimeAgent::~WorkerRuntimeAgent()
52{
53#if !ENABLE(OILPAN)
54    m_instrumentingAgents->setWorkerRuntimeAgent(0);
55#endif
56}
57
58void WorkerRuntimeAgent::trace(Visitor* visitor)
59{
60    visitor->trace(m_workerGlobalScope);
61    InspectorRuntimeAgent::trace(visitor);
62}
63
64void WorkerRuntimeAgent::init()
65{
66    m_instrumentingAgents->setWorkerRuntimeAgent(this);
67}
68
69void WorkerRuntimeAgent::enable(ErrorString* errorString)
70{
71    if (m_enabled)
72        return;
73
74    InspectorRuntimeAgent::enable(errorString);
75    addExecutionContextToFrontend(m_workerGlobalScope->script()->scriptState(), true, m_workerGlobalScope->url(), "");
76}
77
78InjectedScript WorkerRuntimeAgent::injectedScriptForEval(ErrorString* error, const int* executionContextId)
79{
80    if (!executionContextId)
81        return injectedScriptManager()->injectedScriptFor(m_workerGlobalScope->script()->scriptState());
82
83    InjectedScript injectedScript = injectedScriptManager()->injectedScriptForId(*executionContextId);
84    if (injectedScript.isEmpty())
85        *error = "Execution context with given id not found.";
86    return injectedScript;
87}
88
89void WorkerRuntimeAgent::muteConsole()
90{
91    // We don't need to mute console for workers.
92}
93
94void WorkerRuntimeAgent::unmuteConsole()
95{
96    // We don't need to mute console for workers.
97}
98
99void WorkerRuntimeAgent::run(ErrorString*)
100{
101    m_paused = false;
102}
103
104void WorkerRuntimeAgent::isRunRequired(ErrorString*, bool* out_result)
105{
106    *out_result = m_paused;
107}
108
109void WorkerRuntimeAgent::willEvaluateWorkerScript(WorkerGlobalScope* context, int workerThreadStartMode)
110{
111    if (workerThreadStartMode != PauseWorkerGlobalScopeOnStart)
112        return;
113
114    m_paused = true;
115    MessageQueueWaitResult result;
116    context->thread()->willEnterNestedLoop();
117    do {
118        result = context->thread()->runDebuggerTask();
119    // Keep waiting until execution is resumed.
120    } while (result == MessageQueueMessageReceived && m_paused);
121    context->thread()->didLeaveNestedLoop();
122}
123
124} // namespace blink
125