2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #include "ServiceWorkerGlobalScope.h"
33 #include "bindings/core/v8/ScriptPromise.h"
34 #include "bindings/core/v8/ScriptState.h"
35 #include "bindings/core/v8/V8ThrowException.h"
36 #include "core/fetch/MemoryCache.h"
37 #include "core/fetch/ResourceLoaderOptions.h"
38 #include "core/inspector/ScriptCallStack.h"
39 #include "core/loader/ThreadableLoader.h"
40 #include "core/workers/WorkerClients.h"
41 #include "core/workers/WorkerThreadStartupData.h"
42 #include "modules/CachePolyfill.h"
43 #include "modules/CacheStoragePolyfill.h"
44 #include "modules/EventTargetModules.h"
45 #include "modules/serviceworkers/CacheStorage.h"
46 #include "modules/serviceworkers/FetchManager.h"
47 #include "modules/serviceworkers/Request.h"
48 #include "modules/serviceworkers/ServiceWorkerClients.h"
49 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
50 #include "modules/serviceworkers/ServiceWorkerThread.h"
51 #include "platform/network/ResourceRequest.h"
52 #include "platform/weborigin/KURL.h"
53 #include "public/platform/WebURL.h"
54 #include "wtf/CurrentTime.h"
58 PassRefPtrWillBeRawPtr<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::create(ServiceWorkerThread* thread, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData)
60 RefPtrWillBeRawPtr<ServiceWorkerGlobalScope> context = adoptRefWillBeRefCountedGarbageCollected(new ServiceWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), startupData->m_workerClients.release()));
62 context->applyContentSecurityPolicyFromString(startupData->m_contentSecurityPolicy, startupData->m_contentSecurityPolicyType);
64 context->script()->evaluate(String(cachePolyfillJs, sizeof(cachePolyfillJs)));
65 context->script()->evaluate(String(cacheStoragePolyfillJs, sizeof(cacheStoragePolyfillJs)));
67 return context.release();
70 ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(const KURL& url, const String& userAgent, ServiceWorkerThread* thread, double timeOrigin, PassOwnPtrWillBeRawPtr<WorkerClients> workerClients)
71 : WorkerGlobalScope(url, userAgent, thread, timeOrigin, workerClients)
72 , m_fetchManager(adoptPtr(new FetchManager(this)))
74 ScriptWrappable::init(this);
77 ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
81 void ServiceWorkerGlobalScope::stopFetch()
83 m_fetchManager.clear();
86 String ServiceWorkerGlobalScope::scope(ExecutionContext* context)
88 return ServiceWorkerGlobalScopeClient::from(context)->scope().string();
91 PassRefPtrWillBeRawPtr<CacheStorage> ServiceWorkerGlobalScope::caches(ExecutionContext* context)
94 m_caches = CacheStorage::create(ServiceWorkerGlobalScopeClient::from(context)->cacheStorage());
98 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, Request* request)
101 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError("ServiceWorkerGlobalScope is shutting down.", scriptState->isolate()));
102 // "Let |r| be the associated request of the result of invoking the initial
103 // value of Request as constructor with |input| and |init| as arguments. If
104 // this throws an exception, reject |p| with it."
105 TrackExceptionState exceptionState;
106 RefPtrWillBeRawPtr<Request> r = Request::create(this, request, exceptionState);
107 if (exceptionState.hadException()) {
108 // FIXME: We should throw the caught error.
109 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate()));
111 return m_fetchManager->fetch(scriptState, r->request());
114 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, Request* request, const Dictionary& requestInit)
117 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError("ServiceWorkerGlobalScope is shutting down.", scriptState->isolate()));
118 // "Let |r| be the associated request of the result of invoking the initial
119 // value of Request as constructor with |input| and |init| as arguments. If
120 // this throws an exception, reject |p| with it."
121 TrackExceptionState exceptionState;
122 RefPtrWillBeRawPtr<Request> r = Request::create(this, request, requestInit, exceptionState);
123 if (exceptionState.hadException()) {
124 // FIXME: We should throw the caught error.
125 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate()));
127 return m_fetchManager->fetch(scriptState, r->request());
130 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, const String& urlstring)
133 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError("ServiceWorkerGlobalScope is shutting down.", scriptState->isolate()));
134 // "Let |r| be the associated request of the result of invoking the initial
135 // value of Request as constructor with |input| and |init| as arguments. If
136 // this throws an exception, reject |p| with it."
137 TrackExceptionState exceptionState;
138 RefPtrWillBeRawPtr<Request> r = Request::create(this, urlstring, exceptionState);
139 if (exceptionState.hadException()) {
140 // FIXME: We should throw the caught error.
141 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate()));
143 return m_fetchManager->fetch(scriptState, r->request());
146 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, const String& urlstring, const Dictionary& requestInit)
149 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError("ServiceWorkerGlobalScope is shutting down.", scriptState->isolate()));
150 // "Let |r| be the associated request of the result of invoking the initial
151 // value of Request as constructor with |input| and |init| as arguments. If
152 // this throws an exception, reject |p| with it."
153 TrackExceptionState exceptionState;
154 RefPtrWillBeRawPtr<Request> r = Request::create(this, urlstring, requestInit, exceptionState);
155 if (exceptionState.hadException()) {
156 // FIXME: We should throw the caught error.
157 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate()));
159 return m_fetchManager->fetch(scriptState, r->request());
162 PassRefPtrWillBeRawPtr<ServiceWorkerClients> ServiceWorkerGlobalScope::clients()
165 m_clients = ServiceWorkerClients::create();
169 const AtomicString& ServiceWorkerGlobalScope::interfaceName() const
171 return EventTargetNames::ServiceWorkerGlobalScope;
174 void ServiceWorkerGlobalScope::trace(Visitor* visitor)
176 visitor->trace(m_clients);
177 visitor->trace(m_caches);
178 WorkerGlobalScope::trace(visitor);
181 void ServiceWorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState& exceptionState)
183 // Bust the MemoryCache to ensure script requests reach the browser-side
184 // and get added to and retrieved from the ServiceWorker's script cache.
185 // FIXME: Revisit in light of the solution to crbug/388375.
186 for (Vector<String>::const_iterator it = urls.begin(); it != urls.end(); ++it)
187 MemoryCache::removeURLFromCache(this->executionContext(), completeURL(*it));
188 WorkerGlobalScope::importScripts(urls, exceptionState);
191 void ServiceWorkerGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
193 WorkerGlobalScope::logExceptionToConsole(errorMessage, sourceURL, lineNumber, columnNumber, callStack);
194 RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(JSMessageSource, ErrorMessageLevel, errorMessage, sourceURL, lineNumber);
195 consoleMessage->setCallStack(callStack);
196 addMessageToWorkerConsole(consoleMessage.release());