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

Side by Side Diff: Source/modules/serviceworkers/Cache.cpp

Issue 521513002: Service Worker: Simplify rejection code in Cache implementation (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « Source/modules/serviceworkers/Cache.h ('k') | Source/modules/serviceworkers/Cache.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "modules/serviceworkers/Cache.h" 6 #include "modules/serviceworkers/Cache.h"
7 7
8 #include "bindings/core/v8/Dictionary.h" 8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/ExceptionState.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 10 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "bindings/core/v8/ScriptState.h" 11 #include "bindings/core/v8/ScriptState.h"
11 #include "bindings/core/v8/V8ThrowException.h"
12 #include "core/dom/DOMException.h" 12 #include "core/dom/DOMException.h"
13 #include "modules/serviceworkers/Request.h" 13 #include "modules/serviceworkers/Request.h"
14 #include "modules/serviceworkers/Response.h" 14 #include "modules/serviceworkers/Response.h"
15 #include "public/platform/WebServiceWorkerCache.h" 15 #include "public/platform/WebServiceWorkerCache.h"
16 16
17 namespace blink { 17 namespace blink {
18 18
19 namespace { 19 namespace {
20 20
21 const char* cacheErrorToString(WebServiceWorkerCacheError reason) 21 const char* cacheErrorToString(WebServiceWorkerCacheError reason)
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE 107 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE
108 { 108 {
109 m_resolver->reject(cacheErrorToString(*reason)); 109 m_resolver->reject(cacheErrorToString(*reason));
110 m_resolver.clear(); 110 m_resolver.clear();
111 } 111 }
112 112
113 private: 113 private:
114 RefPtr<ScriptPromiseResolver> m_resolver; 114 RefPtr<ScriptPromiseResolver> m_resolver;
115 }; 115 };
116 116
117 ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) 117 ScriptPromise rejectAsNotImplemented(ExceptionState& exceptionState)
118 { 118 {
119 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); 119 exceptionState.throwDOMException(NotSupportedError, "Cache is not implemente d");
120 const ScriptPromise promise = resolver->promise(); 120 return ScriptPromise();
121 resolver->reject(DOMException::create(NotSupportedError, "Cache is not imple mented"));
122 return promise;
123 } 121 }
124 122
125 } // namespace 123 } // namespace
126 124
127 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache) 125 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache)
128 { 126 {
129 if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface())) 127 if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface()))
130 return cache; 128 return cache;
131 return create(webCache); 129 return create(webCache);
132 } 130 }
133 131
134 Cache::~Cache() 132 Cache::~Cache()
135 { 133 {
136 } 134 }
137 135
138 ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, c onst Dictionary& queryParamsDict) 136 ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, c onst Dictionary& queryParamsDict, ExceptionState& exceptionState)
139 { 137 {
140 TrackExceptionState exceptionState;
141 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 138 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
142 if (exceptionState.hadException()) 139 if (exceptionState.hadException())
143 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); 140 return ScriptPromise();
144 // return ScriptPromise::reject(DOMException::create(NotFoundError, " The specified Service Worker resource was not found."));
145 return matchImpl(scriptState, request, queryParamsDict); 141 return matchImpl(scriptState, request, queryParamsDict);
146 } 142 }
147 143
148 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParamsDict) 144 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParamsDict, ExceptionState& exceptionState)
149 { 145 {
150 TrackExceptionState exceptionState;
151 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 146 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
152 if (exceptionState.hadException()) 147 if (exceptionState.hadException())
153 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); 148 return ScriptPromise();
154 // return ScriptPromise::reject(DOMException::create(NotFoundError, " The specified Service Worker resource was not found."));
155 return matchImpl(scriptState, request, queryParamsDict); 149 return matchImpl(scriptState, request, queryParamsDict);
156 } 150 }
157 151
158 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* originalRequest , const Dictionary& queryParamsDict) 152 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* originalRequest , const Dictionary& queryParamsDict, ExceptionState& exceptionState)
159 { 153 {
160 TrackExceptionState exceptionState;
161 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 154 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
162 if (exceptionState.hadException()) { 155 if (exceptionState.hadException())
163 // FIXME: We should throw the caught error. 156 return ScriptPromise();
164 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
165 }
166 return matchAllImpl(scriptState, request, queryParamsDict); 157 return matchAllImpl(scriptState, request, queryParamsDict);
167 } 158 }
168 159
169 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParamsDict) 160 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParamsDict, ExceptionState& exceptionState)
170 { 161 {
171 TrackExceptionState exceptionState;
172 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 162 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
173 if (exceptionState.hadException()) { 163 if (exceptionState.hadException())
174 // FIXME: We should throw the caught error. 164 return ScriptPromise();
175 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
176 }
177 return matchAllImpl(scriptState, request, queryParamsDict); 165 return matchAllImpl(scriptState, request, queryParamsDict);
178 } 166 }
179 167
180 ScriptPromise Cache::add(ScriptState* scriptState, Request* originalRequest) 168 ScriptPromise Cache::add(ScriptState* scriptState, Request* originalRequest, Exc eptionState& exceptionState)
181 { 169 {
182 TrackExceptionState exceptionState;
183 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 170 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
184 if (exceptionState.hadException()) { 171 if (exceptionState.hadException())
185 // FIXME: We should throw the caught error. 172 return ScriptPromise();
186 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); 173 return addImpl(scriptState, request, exceptionState);
187 }
188 return addImpl(scriptState, request);
189 } 174 }
190 175
191 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString) 176 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString, ExceptionState& exceptionState)
192 { 177 {
193 TrackExceptionState exceptionState;
194 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 178 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
195 if (exceptionState.hadException()) { 179 if (exceptionState.hadException())
196 // FIXME: We should throw the caught error. 180 return ScriptPromise();
197 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); 181 return addImpl(scriptState, request, exceptionState);
198 }
199 return addImpl(scriptState, request);
200 } 182 }
201 183
202 ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& rawRequests) 184 ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& rawRequests, ExceptionState& exceptionState)
203 { 185 {
204 return rejectAsNotImplemented(scriptState); 186 return rejectAsNotImplemented(exceptionState);
205 } 187 }
206 188
207 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* originalR equest, const Dictionary& queryParamsDict) 189 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* originalR equest, const Dictionary& queryParamsDict, ExceptionState& exceptionState)
208 { 190 {
209 TrackExceptionState exceptionState;
210 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 191 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
211 if (exceptionState.hadException()) { 192 if (exceptionState.hadException())
212 // FIXME: We should throw the caught error. 193 return ScriptPromise();
213 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
214 }
215 return deleteImpl(scriptState, request, queryParamsDict); 194 return deleteImpl(scriptState, request, queryParamsDict);
216 } 195 }
217 196
218 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParamsDict) 197 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParamsDict, ExceptionState& exceptionState)
219 { 198 {
220 TrackExceptionState exceptionState;
221 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 199 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
222 if (exceptionState.hadException()) { 200 if (exceptionState.hadException())
223 // FIXME: We should throw the caught error. 201 return ScriptPromise();
224 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
225 }
226 return deleteImpl(scriptState, request, queryParamsDict); 202 return deleteImpl(scriptState, request, queryParamsDict);
227 } 203 }
228 204
229 ScriptPromise Cache::put(ScriptState* scriptState, Request* originalRequest, Res ponse* response) 205 ScriptPromise Cache::put(ScriptState* scriptState, Request* originalRequest, Res ponse* response, ExceptionState& exceptionState)
230 { 206 {
231 TrackExceptionState exceptionState;
232 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 207 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
233 if (exceptionState.hadException()) { 208 if (exceptionState.hadException())
234 // FIXME: We should throw the caught error. 209 return ScriptPromise();
235 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
236 }
237 return putImpl(scriptState, request, response); 210 return putImpl(scriptState, request, response);
238 } 211 }
239 212
240 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response* response) 213 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response* response, ExceptionState& exceptionState)
241 { 214 {
242 TrackExceptionState exceptionState;
243 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 215 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
244 if (exceptionState.hadException()) { 216 if (exceptionState.hadException())
245 // FIXME: We should throw the caught error. 217 return ScriptPromise();
246 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
247 }
248 return putImpl(scriptState, request, response); 218 return putImpl(scriptState, request, response);
249 } 219 }
250 220
251 ScriptPromise Cache::keys(ScriptState* scriptState) 221 ScriptPromise Cache::keys(ScriptState* scriptState, ExceptionState& exceptionSta te)
252 { 222 {
253 return keysImpl(scriptState, RefPtrWillBeRawPtr<Request>(), Dictionary()); 223 return keysImpl(scriptState, RefPtrWillBeRawPtr<Request>(), Dictionary());
254 } 224 }
255 225
256 ScriptPromise Cache::keys(ScriptState* scriptState, Request* originalRequest, co nst Dictionary& queryParamsDict) 226 ScriptPromise Cache::keys(ScriptState* scriptState, Request* originalRequest, co nst Dictionary& queryParamsDict, ExceptionState& exceptionState)
257 { 227 {
258 TrackExceptionState exceptionState;
259 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); 228 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
260 if (exceptionState.hadException()) { 229 if (exceptionState.hadException())
261 // FIXME: We should throw the caught error. 230 return ScriptPromise();
262 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
263 }
264 return keysImpl(scriptState); 231 return keysImpl(scriptState);
265 } 232 }
266 233
267 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) 234 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict, ExceptionState& exceptionState)
268 { 235 {
269 TrackExceptionState exceptionState;
270 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); 236 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
271 if (exceptionState.hadException()) { 237 if (exceptionState.hadException())
272 // FIXME: We should throw the caught error. 238 return ScriptPromise();
273 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
274 }
275 return keysImpl(scriptState, request, queryParamsDict); 239 return keysImpl(scriptState, request, queryParamsDict);
276 } 240 }
277 241
278 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(adoptPtr(webCache)) 242 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(adoptPtr(webCache))
279 { 243 {
280 ScriptWrappable::init(this); 244 ScriptWrappable::init(this);
281 ASSERT(m_webCache->proxyInterface() == 0); 245 ASSERT(m_webCache->proxyInterface() == 0);
282 m_webCache->setProxyInterface(this); 246 m_webCache->setProxyInterface(this);
283 } 247 }
284 248
(...skipping 17 matching lines...) Expand all
302 { 266 {
303 WebServiceWorkerRequest webRequest; 267 WebServiceWorkerRequest webRequest;
304 request->populateWebServiceWorkerRequest(webRequest); 268 request->populateWebServiceWorkerRequest(webRequest);
305 269
306 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); 270 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
307 const ScriptPromise promise = resolver->promise(); 271 const ScriptPromise promise = resolver->promise();
308 m_webCache->dispatchMatchAll(new CacheWithResponsesCallbacks(resolver), webR equest, queryParamsFromDictionary(queryParamsDict)); 272 m_webCache->dispatchMatchAll(new CacheWithResponsesCallbacks(resolver), webR equest, queryParamsFromDictionary(queryParamsDict));
309 return promise; 273 return promise;
310 } 274 }
311 275
312 ScriptPromise Cache::addImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest>) 276 ScriptPromise Cache::addImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest>, ExceptionState& exceptionState)
313 { 277 {
314 return rejectAsNotImplemented(scriptState); 278 return rejectAsNotImplemented(exceptionState);
315 } 279 }
316 280
317 ScriptPromise Cache::addAllImpl(ScriptState* scriptState, Vector<RefPtrWillBeRaw Ptr<Request> >) 281 ScriptPromise Cache::addAllImpl(ScriptState* scriptState, Vector<RefPtrWillBeRaw Ptr<Request> >, ExceptionState& exceptionState)
318 { 282 {
319 return rejectAsNotImplemented(scriptState); 283 return rejectAsNotImplemented(exceptionState);
320 } 284 }
321 285
322 PassRefPtrWillBeRawPtr<DOMException> Cache::domExceptionForCacheError(WebService WorkerCacheError reason) 286 PassRefPtrWillBeRawPtr<DOMException> Cache::domExceptionForCacheError(WebService WorkerCacheError reason)
323 { 287 {
324 switch (reason) { 288 switch (reason) {
325 case WebServiceWorkerCacheErrorNotImplemented: 289 case WebServiceWorkerCacheErrorNotImplemented:
326 return DOMException::create(NotSupportedError, "Method is not implemente d."); 290 return DOMException::create(NotSupportedError, "Method is not implemente d.");
327 case WebServiceWorkerCacheErrorNotFound: 291 case WebServiceWorkerCacheErrorNotFound:
328 return DOMException::create(NotFoundError, "Entry was not found."); 292 return DOMException::create(NotFoundError, "Entry was not found.");
329 case WebServiceWorkerCacheErrorExists: 293 case WebServiceWorkerCacheErrorExists:
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 WebServiceWorkerRequest webRequest; 337 WebServiceWorkerRequest webRequest;
374 request->populateWebServiceWorkerRequest(webRequest); 338 request->populateWebServiceWorkerRequest(webRequest);
375 339
376 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); 340 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
377 const ScriptPromise promise = resolver->promise(); 341 const ScriptPromise promise = resolver->promise();
378 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams()); 342 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams());
379 return promise; 343 return promise;
380 } 344 }
381 345
382 } // namespace blink 346 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/Cache.h ('k') | Source/modules/serviceworkers/Cache.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698