Chromium Code Reviews| Index: Source/modules/serviceworkers/Cache.cpp |
| diff --git a/Source/modules/serviceworkers/Cache.cpp b/Source/modules/serviceworkers/Cache.cpp |
| index 81d757ee77a7b3755f09fd316ab910fa42b78fdd..0575378255a83e2e3c0ac9fcd2946e514c416a98 100644 |
| --- a/Source/modules/serviceworkers/Cache.cpp |
| +++ b/Source/modules/serviceworkers/Cache.cpp |
| @@ -5,13 +5,114 @@ |
| #include "config.h" |
| #include "modules/serviceworkers/Cache.h" |
| +#include "bindings/core/v8/Dictionary.h" |
| #include "bindings/core/v8/ScriptPromiseResolver.h" |
| #include "bindings/core/v8/ScriptState.h" |
| +#include "bindings/core/v8/V8ThrowException.h" |
| +#include "modules/serviceworkers/Request.h" |
| +#include "modules/serviceworkers/Response.h" |
| +#include "public/platform/WebServiceWorkerCache.h" |
| namespace blink { |
| namespace { |
| +const char* cacheErrorToString(WebServiceWorkerCacheError reason) |
| +{ |
| + // FIXME: Construct correct DOM error objects rather than returning strings. |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
One way to avoid the problem with cut-and-paste an
|
| + switch (reason) { |
| + case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented: |
| + return "not implemented"; |
| + case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound: |
| + return "not found"; |
| + case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists: |
| + return "entry already exists"; |
| + default: |
| + ASSERT_NOT_REACHED(); |
| + return "unknown error"; |
| + } |
| +} |
| + |
| +WebServiceWorkerCache::QueryParams queryParamsFromDictionary(const Dictionary& dictionary) |
| +{ |
| + WebServiceWorkerCache::QueryParams queryParams; |
| + DictionaryHelper::get(dictionary, "ignoreSearch", queryParams.ignoreSearch); |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Where did we end up with the idea of making these
|
| + DictionaryHelper::get(dictionary, "ignoreMethod", queryParams.ignoreMethod); |
| + DictionaryHelper::get(dictionary, "ignoreVary", queryParams.ignoreVary); |
| + DictionaryHelper::get(dictionary, "prefixMatch", queryParams.prefixMatch); |
| + // FIXME: Add cacheName. |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Given that these other parameters are not hooked u
|
| + return queryParams; |
| +} |
| + |
| +class CacheMatchCallbacks : public WebServiceWorkerCache::CacheMatchCallbacks { |
| + WTF_MAKE_NONCOPYABLE(CacheMatchCallbacks); |
| +public: |
| + CacheMatchCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { } |
| + |
| + virtual void onSuccess(WebServiceWorkerResponse* webResponse) OVERRIDE |
| + { |
| + m_resolver->resolve(Response::create(*webResponse)); |
| + m_resolver.clear(); |
| + } |
| + |
| + virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE |
| + { |
| + m_resolver->reject(cacheErrorToString(*reason)); |
| + m_resolver.clear(); |
| + } |
| + |
| +private: |
| + RefPtr<ScriptPromiseResolver> m_resolver; |
| +}; |
| + |
| +class CacheWithResponsesCallbacks : public WebServiceWorkerCache::CacheWithResponsesCallbacks { |
| + WTF_MAKE_NONCOPYABLE(CacheWithResponsesCallbacks); |
| +public: |
| + CacheWithResponsesCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { } |
| + |
| + virtual void onSuccess(WebVector<WebServiceWorkerResponse>* webResponses) OVERRIDE |
| + { |
| + Vector<RefPtrWillBeRawPtr<Response> > responses; |
| + for (size_t i = 0; i < webResponses->size(); ++i) |
| + responses.append(Response::create((*webResponses)[i])); |
| + m_resolver->resolve(responses); |
| + m_resolver.clear(); |
| + } |
| + |
| + virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE |
| + { |
| + m_resolver->reject(cacheErrorToString(*reason)); |
| + m_resolver.clear(); |
| + } |
| + |
| +private: |
| + RefPtr<ScriptPromiseResolver> m_resolver; |
| +}; |
| + |
| +class CacheWithRequestsCallbacks : public WebServiceWorkerCache::CacheWithRequestsCallbacks { |
| + WTF_MAKE_NONCOPYABLE(CacheWithRequestsCallbacks); |
| +public: |
| + CacheWithRequestsCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { } |
| + |
| + virtual void onSuccess(WebVector<WebServiceWorkerRequest>* webRequests) OVERRIDE |
| + { |
| + Vector<RefPtrWillBeRawPtr<Request> > requests; |
| + for (size_t i = 0; i < webRequests->size(); ++i) |
| + requests.append(Request::create((*webRequests)[i])); |
| + m_resolver->resolve(requests); |
| + m_resolver.clear(); |
| + } |
| + |
| + virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE |
| + { |
| + m_resolver->reject(cacheErrorToString(*reason)); |
| + m_resolver.clear(); |
| + } |
| + |
| +private: |
| + RefPtr<ScriptPromiseResolver> m_resolver; |
| +}; |
| + |
| ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) |
| { |
| RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| @@ -21,42 +122,79 @@ ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) |
| return promise; |
| } |
| -} |
| +} // namespace |
| PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerCache* webCache) |
| { |
| - return adoptRefWillBeNoop(new Cache(webCache)); |
| + if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface())) |
| + return cache; |
| + return create(webCache); |
| } |
| -// FIXME: Implement these methods. |
| -ScriptPromise Cache::match(ScriptState* scriptState, Request* request, const Dictionary& queryParams) |
| +Cache::~Cache() |
| { |
| - return rejectAsNotImplemented(scriptState); |
| } |
| -ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) |
| +ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
I think you can extract two helpers, one for Strin
|
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) |
| + return ScriptPromise::reject(scriptState, DOMException::create(NotFoundError, "The specified Service Worker resource was not found.")->newLocalWrapper(scriptState->isolate())); |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
rejectWithDOMException
ditto below
|
| + return matchImpl(scriptState, request, queryParamsDict); |
| } |
| -ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* request, const Dictionary& queryParams) |
| +ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) |
| + return ScriptPromise::reject(scriptState, DOMException::create(NotFoundError, "The specified Service Worker resource was not found.")->newLocalWrapper(scriptState->isolate())); |
| + return matchImpl(scriptState, request, queryParamsDict); |
| } |
| -ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) |
| +ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* originalRequest, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Just do it?
Ditto below.
|
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return matchAllImpl(scriptState, request, queryParamsDict); |
| } |
| -ScriptPromise Cache::add(ScriptState* scriptState, Request* request) |
| +ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return matchAllImpl(scriptState, request, queryParamsDict); |
| +} |
| + |
| +ScriptPromise Cache::add(ScriptState* scriptState, Request* originalRequest) |
| +{ |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return addImpl(scriptState, request); |
| } |
| ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return addImpl(scriptState, request); |
| } |
| ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& rawRequests) |
| @@ -64,44 +202,164 @@ ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& |
| return rejectAsNotImplemented(scriptState); |
| } |
| -ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* request, const Dictionary& queryParams) |
| +ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* originalRequest, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return deleteImpl(scriptState, request, queryParamsDict); |
| } |
| -ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) |
| +ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return deleteImpl(scriptState, request, queryParamsDict); |
| } |
| -ScriptPromise Cache::put(ScriptState* scriptState, Request* request, Response*) |
| +ScriptPromise Cache::put(ScriptState* scriptState, Request* originalRequest, Response* response) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return putImpl(scriptState, request, response); |
| } |
| -ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response*) |
| +ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response* response) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return putImpl(scriptState, request, response); |
| } |
| ScriptPromise Cache::keys(ScriptState* scriptState) |
| { |
| - return rejectAsNotImplemented(scriptState); |
| + return keysImpl(scriptState, RefPtrWillBeRawPtr<Request>(), Dictionary()); |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
If you have keysImpl(ScriptState) as line 262 indi
|
| +} |
| + |
| +ScriptPromise Cache::keys(ScriptState* scriptState, Request* originalRequest, const Dictionary& queryParamsDict) |
| +{ |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), originalRequest, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return keysImpl(scriptState); |
| +} |
| + |
| +ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) |
| +{ |
| + TrackExceptionState exceptionState; |
| + RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->executionContext(), requestString, exceptionState); |
| + if (exceptionState.hadException()) { |
| + // FIXME: We should throw the caught error. |
| + return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(exceptionState.message(), scriptState->isolate())); |
| + } |
| + return keysImpl(scriptState, request, queryParamsDict); |
| +} |
| + |
| +Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(adoptPtr(webCache)) |
| +{ |
| + ScriptWrappable::init(this); |
| + ASSERT(m_webCache->proxyInterface() == 0); |
| + m_webCache->setProxyInterface(this); |
| } |
| -ScriptPromise Cache::keys(ScriptState* scriptState, Request* request, const Dictionary& queryParams) |
| +PassRefPtrWillBeRawPtr<Cache> Cache::create(WebServiceWorkerCache* webCache) |
| +{ |
| + return adoptRefWillBeNoop(new Cache(webCache)); |
| +} |
| + |
| +ScriptPromise Cache::matchImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request> request, const Dictionary& queryParamsDict) |
| +{ |
| + WebServiceWorkerRequest webRequest; |
| + request->populateWebServiceWorkerRequest(webRequest); |
| + |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchMatch(new CacheMatchCallbacks(resolver), webRequest, queryParamsFromDictionary(queryParamsDict)); |
| + return promise; |
| +} |
| + |
| +ScriptPromise Cache::matchAllImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request> request, const Dictionary& queryParamsDict) |
| +{ |
| + WebServiceWorkerRequest webRequest; |
| + request->populateWebServiceWorkerRequest(webRequest); |
| + |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchMatchAll(new CacheWithResponsesCallbacks(resolver), webRequest, queryParamsFromDictionary(queryParamsDict)); |
| + return promise; |
| +} |
| + |
| +ScriptPromise Cache::addImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request>) |
| { |
| return rejectAsNotImplemented(scriptState); |
| } |
| -ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) |
| +ScriptPromise Cache::addAllImpl(ScriptState* scriptState, Vector<RefPtrWillBeRawPtr<Request> >) |
| { |
| return rejectAsNotImplemented(scriptState); |
| } |
| -Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(webCache) |
| +ScriptPromise Cache::deleteImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request> request, const Dictionary& queryParamsDict) |
| { |
| - ScriptWrappable::init(this); |
| + WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1)); |
| + batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCacheOperationTypeDelete; |
| + request->populateWebServiceWorkerRequest(batchOperations[0].request); |
| + batchOperations[0].matchParams = queryParamsFromDictionary(queryParamsDict); |
| + |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOperations); |
| + return promise; |
| +} |
| + |
| +ScriptPromise Cache::putImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request> request, Response* response) |
| +{ |
| + WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1)); |
| + batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCacheOperationTypePut; |
| + request->populateWebServiceWorkerRequest(batchOperations[0].request); |
| + response->populateWebServiceWorkerResponse(batchOperations[0].response); |
| + |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOperations); |
| + return promise; |
| +} |
| + |
| +ScriptPromise Cache::keysImpl(ScriptState* scriptState) |
| +{ |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebServiceWorkerCache::QueryParams()); |
| + return promise; |
| +} |
| + |
| +ScriptPromise Cache::keysImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Request> request, const Dictionary& queryParamsDict) |
| +{ |
| + WebServiceWorkerRequest webRequest; |
|
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Why go to the trouble of creating the WebRequest a
|
| + request->populateWebServiceWorkerRequest(webRequest); |
| + |
| + RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState); |
| + const ScriptPromise promise = resolver->promise(); |
| + m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebServiceWorkerCache::QueryParams()); |
| + return promise; |
| } |
| } // namespace blink |