Implement Same-Site cookies
[WebKit-https.git] / Source / WebCore / workers / service / context / ServiceWorkerThreadProxy.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 #include "ServiceWorkerThreadProxy.h"
28
29 #if ENABLE(SERVICE_WORKER)
30
31 #include "CacheStorageProvider.h"
32 #include "Frame.h"
33 #include "FrameLoader.h"
34 #include "LoaderStrategy.h"
35 #include "PlatformStrategies.h"
36 #include "Settings.h"
37 #include <pal/SessionID.h>
38 #include <wtf/MainThread.h>
39 #include <wtf/RunLoop.h>
40
41 namespace WebCore {
42
43 URL static inline topOriginURL(const SecurityOrigin& origin)
44 {
45     URL url;
46     url.setProtocol(origin.protocol());
47     url.setHost(origin.host());
48     if (origin.port())
49         url.setPort(*origin.port());
50     return url;
51 }
52
53 static inline UniqueRef<Page> createPageForServiceWorker(PageConfiguration&& configuration, const ServiceWorkerContextData& data, SecurityOrigin::StorageBlockingPolicy storageBlockingPolicy, PAL::SessionID sessionID)
54 {
55     auto page = makeUniqueRef<Page>(WTFMove(configuration));
56     page->setSessionID(sessionID);
57
58     auto& mainFrame = page->mainFrame();
59     mainFrame.loader().initForSynthesizedDocument({ });
60     auto document = Document::createNonRenderedPlaceholder(&mainFrame, data.scriptURL);
61     document->createDOMWindow();
62
63     document->mutableSettings().setStorageBlockingPolicy(storageBlockingPolicy);
64     document->storageBlockingStateDidChange();
65
66     auto origin = data.registration.key.topOrigin().securityOrigin();
67     origin->setStorageBlockingPolicy(storageBlockingPolicy);
68
69     document->setFirstPartyForSameSiteCookies(topOriginURL(origin));
70     document->setFirstPartyForCookies(data.scriptURL);
71     document->setDomainForCachePartition(origin->domainForCachePartition());
72     mainFrame.setDocument(WTFMove(document));
73     return page;
74 }
75
76 static inline IDBClient::IDBConnectionProxy* idbConnectionProxy(Document& document)
77 {
78 #if ENABLE(INDEXED_DATABASE)
79     return document.idbConnectionProxy();
80 #else
81     return nullptr;
82 #endif
83 }
84
85 static HashSet<ServiceWorkerThreadProxy*>& allServiceWorkerThreadProxies()
86 {
87     static NeverDestroyed<HashSet<ServiceWorkerThreadProxy*>> set;
88     return set;
89 }
90
91 ServiceWorkerThreadProxy::ServiceWorkerThreadProxy(PageConfiguration&& pageConfiguration, const ServiceWorkerContextData& data, PAL::SessionID sessionID, String&& userAgent, CacheStorageProvider& cacheStorageProvider, SecurityOrigin::StorageBlockingPolicy storageBlockingPolicy)
92     : m_page(createPageForServiceWorker(WTFMove(pageConfiguration), data, storageBlockingPolicy, data.sessionID))
93     , m_document(*m_page->mainFrame().document())
94     , m_serviceWorkerThread(ServiceWorkerThread::create(data, sessionID, WTFMove(userAgent), *this, *this, idbConnectionProxy(m_document), m_document->socketProvider()))
95     , m_cacheStorageProvider(cacheStorageProvider)
96     , m_sessionID(sessionID)
97     , m_inspectorProxy(*this)
98 {
99     static bool addedListener;
100     if (!addedListener) {
101         platformStrategies()->loaderStrategy()->addOnlineStateChangeListener(&networkStateChanged);
102         addedListener = true;
103     }
104
105     ASSERT(!allServiceWorkerThreadProxies().contains(this));
106     allServiceWorkerThreadProxies().add(this);
107
108 #if ENABLE(REMOTE_INSPECTOR)
109     m_remoteDebuggable = std::make_unique<ServiceWorkerDebuggable>(*this, data);
110     m_remoteDebuggable->setRemoteDebuggingAllowed(true);
111     m_remoteDebuggable->init();
112 #endif
113 }
114
115 ServiceWorkerThreadProxy::~ServiceWorkerThreadProxy()
116 {
117     ASSERT(allServiceWorkerThreadProxies().contains(this));
118     allServiceWorkerThreadProxies().remove(this);
119 }
120
121 bool ServiceWorkerThreadProxy::postTaskForModeToWorkerGlobalScope(ScriptExecutionContext::Task&& task, const String& mode)
122 {
123     if (m_isTerminatingOrTerminated)
124         return false;
125
126     m_serviceWorkerThread->runLoop().postTaskForMode(WTFMove(task), mode);
127     return true;
128 }
129
130 void ServiceWorkerThreadProxy::postTaskToLoader(ScriptExecutionContext::Task&& task)
131 {
132     callOnMainThread([task = WTFMove(task), this, protectedThis = makeRef(*this)] () mutable {
133         task.performTask(m_document.get());
134     });
135 }
136
137 void ServiceWorkerThreadProxy::postMessageToDebugger(const String& message)
138 {
139     RunLoop::main().dispatch([this, protectedThis = makeRef(*this), message = message.isolatedCopy()] {
140         // FIXME: Handle terminated case.
141         m_inspectorProxy.sendMessageFromWorkerToFrontend(message);
142     });
143 }
144
145 void ServiceWorkerThreadProxy::setResourceCachingDisabled(bool disabled)
146 {
147     postTaskToLoader([this, protectedThis = makeRef(*this), disabled] (ScriptExecutionContext&) {
148         ASSERT(isMainThread());
149         m_page->setResourceCachingDisabled(disabled);
150     });   
151 }
152
153 Ref<CacheStorageConnection> ServiceWorkerThreadProxy::createCacheStorageConnection()
154 {
155     ASSERT(isMainThread());
156     if (!m_cacheStorageConnection)
157         m_cacheStorageConnection = m_cacheStorageProvider.createCacheStorageConnection(m_sessionID);
158     return *m_cacheStorageConnection;
159 }
160
161 std::unique_ptr<FetchLoader> ServiceWorkerThreadProxy::createBlobLoader(FetchLoaderClient& client, const URL& blobURL)
162 {
163     auto loader = std::make_unique<FetchLoader>(client, nullptr);
164     loader->startLoadingBlobURL(m_document, blobURL);
165     if (!loader->isStarted())
166         return nullptr;
167     return loader;
168 }
169
170 void ServiceWorkerThreadProxy::networkStateChanged(bool isOnLine)
171 {
172     for (auto* proxy : allServiceWorkerThreadProxies())
173         proxy->notifyNetworkStateChange(isOnLine);
174 }
175
176 void ServiceWorkerThreadProxy::notifyNetworkStateChange(bool isOnline)
177 {
178     if (m_isTerminatingOrTerminated)
179         return;
180
181     postTaskForModeToWorkerGlobalScope([isOnline] (ScriptExecutionContext& context) {
182         auto& globalScope = downcast<WorkerGlobalScope>(context);
183         globalScope.setIsOnline(isOnline);
184         globalScope.dispatchEvent(Event::create(isOnline ? eventNames().onlineEvent : eventNames().offlineEvent, false, false));
185     }, WorkerRunLoop::defaultMode());
186 }
187
188 } // namespace WebCore
189
190 #endif // ENABLE(SERVICE_WORKER)