Unreviewed, try to fix the build on recent SDKs after r226274.
[WebKit-https.git] / Source / WebCore / workers / service / context / SWContextManager.cpp
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
28 #if ENABLE(SERVICE_WORKER)
29 #include "SWContextManager.h"
30 #include "ServiceWorkerClientIdentifier.h"
31 #include "ServiceWorkerGlobalScope.h"
32
33 namespace WebCore {
34
35 SWContextManager& SWContextManager::singleton()
36 {
37     static SWContextManager* sharedManager = new SWContextManager;
38     return *sharedManager;
39 }
40
41 void SWContextManager::setConnection(std::unique_ptr<Connection>&& connection)
42 {
43     ASSERT(!m_connection);
44     m_connection = WTFMove(connection);
45 }
46
47 auto SWContextManager::connection() const -> Connection*
48 {
49     return m_connection.get();
50 }
51
52 void SWContextManager::registerServiceWorkerThreadForInstall(Ref<ServiceWorkerThreadProxy>&& serviceWorkerThreadProxy)
53 {
54     auto serviceWorkerIdentifier = serviceWorkerThreadProxy->identifier();
55     auto jobDataIdentifier = serviceWorkerThreadProxy->thread().contextData().jobDataIdentifier;
56     auto* threadProxy = serviceWorkerThreadProxy.ptr();
57     auto result = m_workerMap.add(serviceWorkerIdentifier, WTFMove(serviceWorkerThreadProxy));
58     ASSERT_UNUSED(result, result.isNewEntry);
59     
60     threadProxy->thread().start([jobDataIdentifier, serviceWorkerIdentifier](const String& exceptionMessage) {
61         SWContextManager::singleton().connection()->serviceWorkerStartedWithMessage(jobDataIdentifier, serviceWorkerIdentifier, exceptionMessage);
62     });
63 }
64
65 ServiceWorkerThreadProxy* SWContextManager::serviceWorkerThreadProxy(ServiceWorkerIdentifier identifier) const
66 {
67     return m_workerMap.get(identifier);
68 }
69
70 void SWContextManager::postMessageToServiceWorker(ServiceWorkerIdentifier destination, Ref<SerializedScriptValue>&& message, ServiceWorkerOrClientData&& sourceData)
71 {
72     auto* serviceWorker = m_workerMap.get(destination);
73     ASSERT(serviceWorker);
74     ASSERT(!serviceWorker->isTerminatingOrTerminated());
75
76     // FIXME: We should pass valid MessagePortChannels.
77     serviceWorker->thread().postMessageToServiceWorker(WTFMove(message), nullptr, WTFMove(sourceData));
78 }
79
80 void SWContextManager::fireInstallEvent(ServiceWorkerIdentifier identifier)
81 {
82     auto* serviceWorker = m_workerMap.get(identifier);
83     if (!serviceWorker)
84         return;
85
86     serviceWorker->thread().fireInstallEvent();
87 }
88
89 void SWContextManager::fireActivateEvent(ServiceWorkerIdentifier identifier)
90 {
91     auto* serviceWorker = m_workerMap.get(identifier);
92     if (!serviceWorker)
93         return;
94
95     serviceWorker->thread().fireActivateEvent();
96 }
97
98 void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier, Function<void()>&& completionHandler)
99 {
100     auto serviceWorker = m_workerMap.take(identifier);
101     if (!serviceWorker)
102         return;
103
104     serviceWorker->setTerminatingOrTerminated(true);
105
106     serviceWorker->thread().stop([identifier, serviceWorker = WTFMove(serviceWorker), completionHandler = WTFMove(completionHandler)]() mutable {
107         if (auto* connection = SWContextManager::singleton().connection())
108             connection->workerTerminated(identifier);
109
110         if (completionHandler)
111             completionHandler();
112         
113         // Spin the runloop before releasing the worker thread proxy, as there would otherwise be
114         // a race towards its destruction.
115         callOnMainThread([serviceWorker = WTFMove(serviceWorker)] { });
116     });
117 }
118
119 void SWContextManager::forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>& apply)
120 {
121     for (auto& workerThread : m_workerMap.values())
122         apply(*workerThread);
123 }
124
125 bool SWContextManager::postTaskToServiceWorker(ServiceWorkerIdentifier identifier, WTF::Function<void(ServiceWorkerGlobalScope&)>&& task)
126 {
127     auto* serviceWorker = m_workerMap.get(identifier);
128     if (!serviceWorker)
129         return false;
130
131     serviceWorker->thread().runLoop().postTask([task = WTFMove(task)] (auto& context) {
132         task(downcast<ServiceWorkerGlobalScope>(context));
133     });
134     return true;
135 }
136
137 } // namespace WebCore
138
139 #endif