Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerGlobalScope.cpp

Issue 2118243002: [proof-of-concept] SW thread independent of the main thread Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" 31 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h"
32 32
33 #include "bindings/core/v8/CallbackPromiseAdapter.h" 33 #include "bindings/core/v8/CallbackPromiseAdapter.h"
34 #include "bindings/core/v8/ScriptSourceCode.h"
34 #include "bindings/core/v8/ScriptPromise.h" 35 #include "bindings/core/v8/ScriptPromise.h"
35 #include "bindings/core/v8/ScriptPromiseResolver.h" 36 #include "bindings/core/v8/ScriptPromiseResolver.h"
36 #include "bindings/core/v8/ScriptState.h" 37 #include "bindings/core/v8/ScriptState.h"
37 #include "bindings/core/v8/SourceLocation.h" 38 #include "bindings/core/v8/SourceLocation.h"
38 #include "bindings/core/v8/V8ThrowException.h" 39 #include "bindings/core/v8/V8ThrowException.h"
39 #include "core/dom/ExceptionCode.h" 40 #include "core/dom/ExceptionCode.h"
40 #include "core/events/Event.h" 41 #include "core/events/Event.h"
42 #include "core/fetch/CachedMetadata.h"
41 #include "core/fetch/MemoryCache.h" 43 #include "core/fetch/MemoryCache.h"
42 #include "core/fetch/ResourceLoaderOptions.h" 44 #include "core/fetch/ResourceLoaderOptions.h"
43 #include "core/inspector/ConsoleMessage.h" 45 #include "core/inspector/ConsoleMessage.h"
44 #include "core/inspector/WorkerInspectorController.h" 46 #include "core/inspector/WorkerInspectorController.h"
45 #include "core/inspector/WorkerThreadDebugger.h" 47 #include "core/inspector/WorkerThreadDebugger.h"
46 #include "core/loader/ThreadableLoader.h" 48 #include "core/loader/ThreadableLoader.h"
47 #include "core/origin_trials/OriginTrialContext.h" 49 #include "core/origin_trials/OriginTrialContext.h"
48 #include "core/workers/WorkerClients.h" 50 #include "core/workers/WorkerClients.h"
51 #include "core/workers/WorkerReportingProxy.h"
49 #include "core/workers/WorkerThreadStartupData.h" 52 #include "core/workers/WorkerThreadStartupData.h"
50 #include "modules/EventTargetModules.h" 53 #include "modules/EventTargetModules.h"
51 #include "modules/cachestorage/CacheStorage.h" 54 #include "modules/cachestorage/CacheStorage.h"
52 #include "modules/cachestorage/InspectorCacheStorageAgent.h" 55 #include "modules/cachestorage/InspectorCacheStorageAgent.h"
53 #include "modules/fetch/GlobalFetch.h" 56 #include "modules/fetch/GlobalFetch.h"
57 #include "modules/serviceworkers/IsolatedWorkerReportingProxy.h"
58 #include "modules/serviceworkers/IsolatedWorkerThread.h"
54 #include "modules/serviceworkers/ServiceWorkerClients.h" 59 #include "modules/serviceworkers/ServiceWorkerClients.h"
55 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" 60 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
56 #include "modules/serviceworkers/ServiceWorkerRegistration.h" 61 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
57 #include "modules/serviceworkers/ServiceWorkerScriptCachedMetadataHandler.h" 62 #include "modules/serviceworkers/ServiceWorkerScriptCachedMetadataHandler.h"
58 #include "modules/serviceworkers/ServiceWorkerThread.h" 63 #include "modules/serviceworkers/ServiceWorkerThread.h"
59 #include "modules/serviceworkers/WaitUntilObserver.h" 64 #include "modules/serviceworkers/WaitUntilObserver.h"
60 #include "platform/Histogram.h" 65 #include "platform/Histogram.h"
61 #include "platform/network/ResourceRequest.h" 66 #include "platform/network/ResourceRequest.h"
67 #include "platform/TraceEvent.h"
62 #include "platform/weborigin/KURL.h" 68 #include "platform/weborigin/KURL.h"
63 #include "public/platform/Platform.h" 69 #include "public/platform/Platform.h"
64 #include "public/platform/WebURL.h" 70 #include "public/platform/WebURL.h"
65 #include "wtf/CurrentTime.h" 71 #include "wtf/CurrentTime.h"
66 #include "wtf/PtrUtil.h" 72 #include "wtf/PtrUtil.h"
67 #include <memory> 73 #include <memory>
68 74
69 namespace blink { 75 namespace blink {
76 namespace {
77
78 class IsolatedWorkerScriptCachedMetadataHandler : public CachedMetadataHandl er {
79 public:
80 static IsolatedWorkerScriptCachedMetadataHandler* create(WorkerGlobalSco pe* workerGlobalScope, const KURL& scriptURL, const Vector<char>* metaData)
81 {
82 return new IsolatedWorkerScriptCachedMetadataHandler(workerGlobalSco pe, scriptURL, metaData);
83 }
84 ~IsolatedWorkerScriptCachedMetadataHandler() override;
85 DECLARE_VIRTUAL_TRACE();
86 void setCachedMetadata(unsigned dataTypeID, const char*, size_t, CacheTy pe) override;
87 void clearCachedMetadata(CacheType) override;
88 PassRefPtr<CachedMetadata> cachedMetadata(unsigned dataTypeID) const ove rride;
89 String encoding() const override;
90
91 private:
92 IsolatedWorkerScriptCachedMetadataHandler(WorkerGlobalScope*, const KURL & scriptURL, const Vector<char>* metaData);
93
94 Member<WorkerGlobalScope> m_workerGlobalScope;
95 KURL m_scriptURL;
96 RefPtr<CachedMetadata> m_cachedMetadata;
97 };
98
99 IsolatedWorkerScriptCachedMetadataHandler::IsolatedWorkerScriptCachedMetadat aHandler(WorkerGlobalScope* workerGlobalScope, const KURL& scriptURL, const Vect or<char>* metaData)
100 : m_workerGlobalScope(workerGlobalScope)
101 , m_scriptURL(scriptURL)
102 {
103 if (metaData)
104 m_cachedMetadata = CachedMetadata::deserialize(metaData->data(), met aData->size());
105 }
106
107 IsolatedWorkerScriptCachedMetadataHandler::~IsolatedWorkerScriptCachedMetada taHandler()
108 {
109 }
110
111 DEFINE_TRACE(IsolatedWorkerScriptCachedMetadataHandler)
112 {
113 visitor->trace(m_workerGlobalScope);
114 CachedMetadataHandler::trace(visitor);
115 }
116
117 void IsolatedWorkerScriptCachedMetadataHandler::setCachedMetadata(unsigned d ataTypeID, const char* data, size_t size, CacheType type)
118 {
119 if (type != SendToPlatform)
120 return;
121 //TODO
122 LOG(ERROR) << "IsolatedWorkerScriptCachedMetadataHandler::setCachedMetad ata size: " << size;
123 }
124
125 void IsolatedWorkerScriptCachedMetadataHandler::clearCachedMetadata(CacheTyp e type)
126 {
127 if (type != SendToPlatform)
128 return;
129 m_cachedMetadata = nullptr;
130 //TODO
131 LOG(ERROR) << "IsolatedWorkerScriptCachedMetadataHandler::setCachedMetad ata";
132 }
133
134 PassRefPtr<CachedMetadata> IsolatedWorkerScriptCachedMetadataHandler::cached Metadata(unsigned dataTypeID) const
135 {
136 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
137 return nullptr;
138 return m_cachedMetadata.get();
139 }
140
141 String IsolatedWorkerScriptCachedMetadataHandler::encoding() const
142 {
143 return emptyString();
144 }
145
146 } // namespace
147
148 ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(IsolatedWorkerThread* thread, std::unique_ptr<WorkerThreadStartupData> startupData)
149 {
150 // Note: startupData is finalized on return. After the relevant parts has be en
151 // passed along to the created 'context'.
152 ServiceWorkerGlobalScope* context = new ServiceWorkerGlobalScope(startupData ->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), std::move(startupData->m_starterOriginPrivilegeData));
153
154 context->setV8CacheOptions(startupData->m_v8CacheOptions);
155 context->applyContentSecurityPolicyFromVector(*startupData->m_contentSecurit yPolicyHeaders);
156 if (!startupData->m_referrerPolicy.isNull())
157 context->parseAndSetReferrerPolicy(startupData->m_referrerPolicy);
158 context->setAddressSpace(startupData->m_addressSpace);
159 OriginTrialContext::addTokens(context, startupData->m_originTrialTokens.get( ));
160
161 return context;
162 }
70 163
71 ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(ServiceWorkerThread* thread, std::unique_ptr<WorkerThreadStartupData> startupData) 164 ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::create(ServiceWorkerThread* thread, std::unique_ptr<WorkerThreadStartupData> startupData)
72 { 165 {
73 // Note: startupData is finalized on return. After the relevant parts has be en 166 // Note: startupData is finalized on return. After the relevant parts has be en
74 // passed along to the created 'context'. 167 // passed along to the created 'context'.
75 ServiceWorkerGlobalScope* context = new ServiceWorkerGlobalScope(startupData ->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), std::move(startupData->m_starterOriginPrivilegeData), startupData->m_workerClien ts.release()); 168 ServiceWorkerGlobalScope* context = new ServiceWorkerGlobalScope(startupData ->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), std::move(startupData->m_starterOriginPrivilegeData), startupData->m_workerClien ts.release());
76 169
77 context->setV8CacheOptions(startupData->m_v8CacheOptions); 170 context->setV8CacheOptions(startupData->m_v8CacheOptions);
78 context->applyContentSecurityPolicyFromVector(*startupData->m_contentSecurit yPolicyHeaders); 171 context->applyContentSecurityPolicyFromVector(*startupData->m_contentSecurit yPolicyHeaders);
79 if (!startupData->m_referrerPolicy.isNull()) 172 if (!startupData->m_referrerPolicy.isNull())
80 context->parseAndSetReferrerPolicy(startupData->m_referrerPolicy); 173 context->parseAndSetReferrerPolicy(startupData->m_referrerPolicy);
81 context->setAddressSpace(startupData->m_addressSpace); 174 context->setAddressSpace(startupData->m_addressSpace);
82 OriginTrialContext::addTokens(context, startupData->m_originTrialTokens.get( )); 175 OriginTrialContext::addTokens(context, startupData->m_originTrialTokens.get( ));
83 176
84 return context; 177 return context;
85 } 178 }
86 179
180 ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String & userAgent, IsolatedWorkerThread* thread, double timeOrigin, std::unique_ptr<Se curityOrigin::PrivilegeData> starterOriginPrivilegeData)
181 : WorkerGlobalScope(url, userAgent, thread, timeOrigin, std::move(starterOri ginPrivilegeData), WorkerClients::create())
182 , m_isIsolatedWorkerGlobalScope(true)
183 , m_didEvaluateScript(false)
184 , m_hadErrorInTopLevelEventHandler(false)
185 , m_eventNestingLevel(0)
186 , m_scriptCount(0)
187 , m_scriptTotalSize(0)
188 , m_scriptCachedMetadataTotalSize(0)
189 {
190 }
191
87 ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String & userAgent, ServiceWorkerThread* thread, double timeOrigin, std::unique_ptr<Sec urityOrigin::PrivilegeData> starterOriginPrivilegeData, WorkerClients* workerCli ents) 192 ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String & userAgent, ServiceWorkerThread* thread, double timeOrigin, std::unique_ptr<Sec urityOrigin::PrivilegeData> starterOriginPrivilegeData, WorkerClients* workerCli ents)
88 : WorkerGlobalScope(url, userAgent, thread, timeOrigin, std::move(starterOri ginPrivilegeData), workerClients) 193 : WorkerGlobalScope(url, userAgent, thread, timeOrigin, std::move(starterOri ginPrivilegeData), workerClients)
194 , m_isIsolatedWorkerGlobalScope(false)
89 , m_didEvaluateScript(false) 195 , m_didEvaluateScript(false)
90 , m_hadErrorInTopLevelEventHandler(false) 196 , m_hadErrorInTopLevelEventHandler(false)
91 , m_eventNestingLevel(0) 197 , m_eventNestingLevel(0)
92 , m_scriptCount(0) 198 , m_scriptCount(0)
93 , m_scriptTotalSize(0) 199 , m_scriptTotalSize(0)
94 , m_scriptCachedMetadataTotalSize(0) 200 , m_scriptCachedMetadataTotalSize(0)
95 { 201 {
96 } 202 }
97 203
98 ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope() 204 ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 303
198 DEFINE_TRACE(ServiceWorkerGlobalScope) 304 DEFINE_TRACE(ServiceWorkerGlobalScope)
199 { 305 {
200 visitor->trace(m_clients); 306 visitor->trace(m_clients);
201 visitor->trace(m_registration); 307 visitor->trace(m_registration);
202 WorkerGlobalScope::trace(visitor); 308 WorkerGlobalScope::trace(visitor);
203 } 309 }
204 310
205 void ServiceWorkerGlobalScope::importScripts(const Vector<String>& urls, Excepti onState& exceptionState) 311 void ServiceWorkerGlobalScope::importScripts(const Vector<String>& urls, Excepti onState& exceptionState)
206 { 312 {
207 // Bust the MemoryCache to ensure script requests reach the browser-side 313 if (!m_isIsolatedWorkerGlobalScope) {
208 // and get added to and retrieved from the ServiceWorker's script cache. 314 // Bust the MemoryCache to ensure script requests reach the browser-side
209 // FIXME: Revisit in light of the solution to crbug/388375. 315 // and get added to and retrieved from the ServiceWorker's script cache.
210 for (Vector<String>::const_iterator it = urls.begin(); it != urls.end(); ++i t) 316 // FIXME: Revisit in light of the solution to crbug/388375.
211 getExecutionContext()->removeURLFromMemoryCache(completeURL(*it)); 317 for (Vector<String>::const_iterator it = urls.begin(); it != urls.end(); ++it)
212 WorkerGlobalScope::importScripts(urls, exceptionState); 318 getExecutionContext()->removeURLFromMemoryCache(completeURL(*it));
319 WorkerGlobalScope::importScripts(urls, exceptionState);
320 return;
321 }
322
323 TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::importScripts");
324
325 ExecutionContext& executionContext = *this->getExecutionContext();
326 Vector<KURL> completedURLs;
327 for (const String& urlString : urls) {
328 const KURL& url = executionContext.completeURL(urlString);
329 if (!url.isValid()) {
330 exceptionState.throwDOMException(SyntaxError, "The URL '" + urlStrin g + "' is invalid.");
331 return;
332 }
333 completedURLs.append(url);
334 }
335
336 for (const auto& scriptURL : completedURLs) {
337 std::unique_ptr<String> alternativeCode(thread()->workerReportingProxy() .takeAlternativeCode(scriptURL));
338 if (!alternativeCode) {
339 return;
340 }
341 ErrorEvent* errorEvent = nullptr;
342 CachedMetadataHandler* handler(createWorkerScriptCachedMetadataHandler(s criptURL, nullptr));
343 {
344 TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::importScri pts evaluate");
345 scriptController()->evaluate(ScriptSourceCode(*alternativeCode, scri ptURL), &errorEvent, handler, V8CacheOptionsAlways);
346 }
347 if (errorEvent) {
348 scriptController()->rethrowExceptionFromImportedScript(errorEvent, e xceptionState);
349 return;
350 }
351 }
213 } 352 }
214 353
215 CachedMetadataHandler* ServiceWorkerGlobalScope::createWorkerScriptCachedMetadat aHandler(const KURL& scriptURL, const Vector<char>* metaData) 354 CachedMetadataHandler* ServiceWorkerGlobalScope::createWorkerScriptCachedMetadat aHandler(const KURL& scriptURL, const Vector<char>* metaData)
216 { 355 {
217 return ServiceWorkerScriptCachedMetadataHandler::create(this, scriptURL, met aData); 356 if (!m_isIsolatedWorkerGlobalScope)
357 return ServiceWorkerScriptCachedMetadataHandler::create(this, scriptURL, metaData);
358 TRACE_EVENT0("ServiceWorker", "IsolatedWorkerGlobalScope::createWorkerScript CachedMetadataHandler");
359 IsolatedWorkerReportingProxy* workerReportingProxy = static_cast<IsolatedWor kerReportingProxy*>(&thread()->workerReportingProxy());
360 std::unique_ptr<WTF::Vector<char>> cachedMetadata(workerReportingProxy->take AlternativeCachedMetadata(scriptURL));
361 if (cachedMetadata && !cachedMetadata->isEmpty())
362 return IsolatedWorkerScriptCachedMetadataHandler::create(this, scriptURL , cachedMetadata.get());
363 return nullptr;
218 } 364 }
219 365
220 void ServiceWorkerGlobalScope::exceptionThrown(ErrorEvent* event) 366 void ServiceWorkerGlobalScope::exceptionThrown(ErrorEvent* event)
221 { 367 {
222 WorkerGlobalScope::exceptionThrown(event); 368 WorkerGlobalScope::exceptionThrown(event);
223 if (WorkerThreadDebugger* debugger = WorkerThreadDebugger::from(thread()->is olate())) 369 if (WorkerThreadDebugger* debugger = WorkerThreadDebugger::from(thread()->is olate()))
224 debugger->exceptionThrown(event); 370 debugger->exceptionThrown(event);
225 } 371 }
226 372
227 void ServiceWorkerGlobalScope::scriptLoaded(size_t scriptSize, size_t cachedMeta dataSize) 373 void ServiceWorkerGlobalScope::scriptLoaded(size_t scriptSize, size_t cachedMeta dataSize)
228 { 374 {
229 ++m_scriptCount; 375 ++m_scriptCount;
230 m_scriptTotalSize += scriptSize; 376 m_scriptTotalSize += scriptSize;
231 m_scriptCachedMetadataTotalSize += cachedMetadataSize; 377 m_scriptCachedMetadataTotalSize += cachedMetadataSize;
232 } 378 }
233 379
234 } // namespace blink 380 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698