Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / ServiceWorkerGlobalScope.cpp
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30 #include "config.h"
31 #include "ServiceWorkerGlobalScope.h"
32
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"
55
56 namespace blink {
57
58 PassRefPtrWillBeRawPtr<ServiceWorkerGlobalScope> ServiceWorkerGlobalScope::create(ServiceWorkerThread* thread, PassOwnPtrWillBeRawPtr<WorkerThreadStartupData> startupData)
59 {
60     RefPtrWillBeRawPtr<ServiceWorkerGlobalScope> context = adoptRefWillBeRefCountedGarbageCollected(new ServiceWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, thread, monotonicallyIncreasingTime(), startupData->m_workerClients.release()));
61
62     context->applyContentSecurityPolicyFromString(startupData->m_contentSecurityPolicy, startupData->m_contentSecurityPolicyType);
63
64     context->script()->evaluate(String(cachePolyfillJs, sizeof(cachePolyfillJs)));
65     context->script()->evaluate(String(cacheStoragePolyfillJs, sizeof(cacheStoragePolyfillJs)));
66
67     return context.release();
68 }
69
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)))
73 {
74     ScriptWrappable::init(this);
75 }
76
77 ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
78 {
79 }
80
81 void ServiceWorkerGlobalScope::stopFetch()
82 {
83     m_fetchManager.clear();
84 }
85
86 String ServiceWorkerGlobalScope::scope(ExecutionContext* context)
87 {
88     return ServiceWorkerGlobalScopeClient::from(context)->scope().string();
89 }
90
91 PassRefPtrWillBeRawPtr<CacheStorage> ServiceWorkerGlobalScope::caches(ExecutionContext* context)
92 {
93     if (!m_caches)
94         m_caches = CacheStorage::create(ServiceWorkerGlobalScopeClient::from(context)->cacheStorage());
95     return m_caches;
96 }
97
98 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, Request* request)
99 {
100     if (!m_fetchManager)
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()));
110     }
111     return m_fetchManager->fetch(scriptState, r->request());
112 }
113
114 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, Request* request, const Dictionary& requestInit)
115 {
116     if (!m_fetchManager)
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()));
126     }
127     return m_fetchManager->fetch(scriptState, r->request());
128 }
129
130 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, const String& urlstring)
131 {
132     if (!m_fetchManager)
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()));
142     }
143     return m_fetchManager->fetch(scriptState, r->request());
144 }
145
146 ScriptPromise ServiceWorkerGlobalScope::fetch(ScriptState* scriptState, const String& urlstring, const Dictionary& requestInit)
147 {
148     if (!m_fetchManager)
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()));
158     }
159     return m_fetchManager->fetch(scriptState, r->request());
160 }
161
162 PassRefPtrWillBeRawPtr<ServiceWorkerClients> ServiceWorkerGlobalScope::clients()
163 {
164     if (!m_clients)
165         m_clients = ServiceWorkerClients::create();
166     return m_clients;
167 }
168
169 const AtomicString& ServiceWorkerGlobalScope::interfaceName() const
170 {
171     return EventTargetNames::ServiceWorkerGlobalScope;
172 }
173
174 void ServiceWorkerGlobalScope::trace(Visitor* visitor)
175 {
176     visitor->trace(m_clients);
177     visitor->trace(m_caches);
178     WorkerGlobalScope::trace(visitor);
179 }
180
181 void ServiceWorkerGlobalScope::importScripts(const Vector<String>& urls, ExceptionState& exceptionState)
182 {
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);
189 }
190
191 void ServiceWorkerGlobalScope::logExceptionToConsole(const String& errorMessage, const String& sourceURL, int lineNumber, int columnNumber, PassRefPtrWillBeRawPtr<ScriptCallStack> callStack)
192 {
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());
197 }
198
199 } // namespace blink