1/*
2 * Copyright (C) 2017 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ServiceWorkerInspectorProxy.h"
28
29#if ENABLE(SERVICE_WORKER)
30
31#include "ScriptExecutionContext.h"
32#include "ServiceWorkerGlobalScope.h"
33#include "ServiceWorkerThreadProxy.h"
34#include "WorkerInspectorController.h"
35#include "WorkerRunLoop.h"
36#include <JavaScriptCore/InspectorAgentBase.h>
37#include <JavaScriptCore/InspectorFrontendChannel.h>
38
39namespace WebCore {
40
41using namespace Inspector;
42
43ServiceWorkerInspectorProxy::ServiceWorkerInspectorProxy(ServiceWorkerThreadProxy& serviceWorkerThreadProxy)
44 : m_serviceWorkerThreadProxy(serviceWorkerThreadProxy)
45{
46 ASSERT(isMainThread());
47}
48
49ServiceWorkerInspectorProxy::~ServiceWorkerInspectorProxy()
50{
51 ASSERT(isMainThread());
52 ASSERT(!m_channel);
53}
54
55void ServiceWorkerInspectorProxy::serviceWorkerTerminated()
56{
57 m_channel = nullptr;
58}
59
60void ServiceWorkerInspectorProxy::connectToWorker(FrontendChannel& channel)
61{
62 m_channel = &channel;
63
64 m_serviceWorkerThreadProxy.thread().runLoop().postDebuggerTask([] (ScriptExecutionContext& context) {
65 downcast<WorkerGlobalScope>(context).inspectorController().connectFrontend();
66 });
67}
68
69void ServiceWorkerInspectorProxy::disconnectFromWorker(FrontendChannel& channel)
70{
71 ASSERT_UNUSED(channel, &channel == m_channel);
72 m_channel = nullptr;
73
74 m_serviceWorkerThreadProxy.thread().runLoop().postDebuggerTask([] (ScriptExecutionContext& context) {
75 downcast<WorkerGlobalScope>(context).inspectorController().disconnectFrontend(DisconnectReason::InspectorDestroyed);
76
77 // In case the worker is paused running debugger tasks, ensure we break out of
78 // the pause since this will be the last debugger task we send to the worker.
79 downcast<WorkerGlobalScope>(context).thread().stopRunningDebuggerTasks();
80 });
81}
82
83void ServiceWorkerInspectorProxy::sendMessageToWorker(const String& message)
84{
85 m_serviceWorkerThreadProxy.thread().runLoop().postDebuggerTask([message = message.isolatedCopy()] (ScriptExecutionContext& context) {
86 downcast<WorkerGlobalScope>(context).inspectorController().dispatchMessageFromFrontend(message);
87 });
88}
89
90void ServiceWorkerInspectorProxy::sendMessageFromWorkerToFrontend(const String& message)
91{
92 if (!m_channel)
93 return;
94
95 m_channel->sendMessageToFrontend(message);
96}
97
98} // namespace WebCore
99
100#endif // ENABLE(SERVICE_WORKER)
101