Chromium Code Reviews| Index: content/browser/service_worker/service_worker_cache_listener.cc |
| diff --git a/content/browser/service_worker/service_worker_cache_listener.cc b/content/browser/service_worker/service_worker_cache_listener.cc |
| index ffd55a4880edb472cb11a69a1bed697b4e63e5c4..bc33d5b13ca69af57f87566e876330f8f6515828 100644 |
| --- a/content/browser/service_worker/service_worker_cache_listener.cc |
| +++ b/content/browser/service_worker/service_worker_cache_listener.cc |
| @@ -4,21 +4,63 @@ |
| #include "content/browser/service_worker/service_worker_cache_listener.h" |
| +#include "base/bind.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/browser/service_worker/service_worker_cache_storage_manager.h" |
| +#include "content/browser/service_worker/service_worker_context_core.h" |
| #include "content/browser/service_worker/service_worker_version.h" |
| #include "content/common/service_worker/service_worker_messages.h" |
| #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h" |
| namespace content { |
| -ServiceWorkerStoresListener::ServiceWorkerStoresListener( |
| - ServiceWorkerVersion* version) : version_(version) {} |
| +using blink::WebServiceWorkerCacheError; |
| -ServiceWorkerStoresListener::~ServiceWorkerStoresListener() {} |
| +namespace { |
| -bool ServiceWorkerStoresListener::OnMessageReceived( |
| +WebServiceWorkerCacheError ToWebServiceWorkerCacheError( |
| + content::ServiceWorkerCacheStorage::CacheStorageError err) { |
|
michaeln
2014/08/12 03:59:23
nit: i think content:: not needed here and below
jkarlin
2014/08/12 15:17:48
Done.
|
| + switch (err) { |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR: |
| + NOTREACHED(); |
| + return WebServiceWorkerCacheError:: |
| + WebServiceWorkerCacheErrorNotImplemented; |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED |
| + : |
| + return WebServiceWorkerCacheError:: |
| + WebServiceWorkerCacheErrorNotImplemented; |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND: |
| + return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EXISTS: |
| + return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists; |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_STORAGE: |
| + // TODO(jkarlin): Changethis to CACHE_STORAGE_ERROR_STORAGE once that's |
| + // added. |
| + return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| + case content::ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EMPTY_KEY: |
| + // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_EMPTY_KEY once that's |
| + // added. |
| + return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound; |
| + } |
| + NOTREACHED(); |
| + return WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented; |
| +} |
| + |
| +} // namespace |
| + |
| +ServiceWorkerCacheListener::ServiceWorkerCacheListener( |
| + ServiceWorkerVersion* version, |
| + base::WeakPtr<ServiceWorkerContextCore> context) |
| + : version_(version), context_(context), weak_factory_(this) { |
| +} |
| + |
| +ServiceWorkerCacheListener::~ServiceWorkerCacheListener() { |
| +} |
| + |
| +bool ServiceWorkerCacheListener::OnMessageReceived( |
| const IPC::Message& message) { |
| bool handled = true; |
| - IPC_BEGIN_MESSAGE_MAP(ServiceWorkerStoresListener, message) |
| + IPC_BEGIN_MESSAGE_MAP(ServiceWorkerCacheListener, message) |
| IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageGet, |
| OnCacheStorageGet) |
| IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas, |
| @@ -35,50 +77,127 @@ bool ServiceWorkerStoresListener::OnMessageReceived( |
| return handled; |
| } |
| -void ServiceWorkerStoresListener::OnCacheStorageGet( |
| +void ServiceWorkerCacheListener::OnCacheStorageGet( |
| int request_id, |
| - const base::string16& fetch_store_name) { |
| - // TODO(gavinp,jkarlin): Implement this method. |
| - Send(ServiceWorkerMsg_CacheStorageGetError( |
| - request_id, |
| - blink::WebServiceWorkerCacheErrorNotImplemented)); |
| + const base::string16& cache_name) { |
| + context_->cache_manager()->GetCache( |
| + version_->scope().GetOrigin(), |
| + base::UTF16ToUTF8(cache_name), |
| + base::Bind(&ServiceWorkerCacheListener::OnCacheStorageGetCallback, |
| + weak_factory_.GetWeakPtr(), |
| + request_id)); |
| } |
| -void ServiceWorkerStoresListener::OnCacheStorageHas( |
| +void ServiceWorkerCacheListener::OnCacheStorageHas( |
| int request_id, |
| - const base::string16& fetch_store_name) { |
| - // TODO(gavinp,jkarlin): Implement this method. |
| - Send(ServiceWorkerMsg_CacheStorageHasError( |
| - request_id, |
| - blink::WebServiceWorkerCacheErrorNotImplemented)); |
| + const base::string16& cache_name) { |
| + context_->cache_manager()->HasCache( |
| + version_->scope().GetOrigin(), |
| + base::UTF16ToUTF8(cache_name), |
| + base::Bind(&ServiceWorkerCacheListener::OnCacheStorageHasCallback, |
| + weak_factory_.GetWeakPtr(), |
| + request_id)); |
| } |
| -void ServiceWorkerStoresListener::OnCacheStorageCreate( |
| +void ServiceWorkerCacheListener::OnCacheStorageCreate( |
| int request_id, |
| - const base::string16& fetch_store_name) { |
| - // TODO(gavinp,jkarlin): Implement this method. |
| - Send(ServiceWorkerMsg_CacheStorageCreateError( |
| - request_id, |
| - blink::WebServiceWorkerCacheErrorNotImplemented)); |
| + const base::string16& cache_name) { |
| + context_->cache_manager()->CreateCache( |
| + version_->scope().GetOrigin(), |
| + base::UTF16ToUTF8(cache_name), |
| + base::Bind(&ServiceWorkerCacheListener::OnCacheStorageCreateCallback, |
| + weak_factory_.GetWeakPtr(), |
| + request_id)); |
| } |
| -void ServiceWorkerStoresListener::OnCacheStorageDelete( |
| +void ServiceWorkerCacheListener::OnCacheStorageDelete( |
| int request_id, |
| - const base::string16& fetch_store_name) { |
| - // TODO(gavinp,jkarlin): Implement this method. |
| - Send(ServiceWorkerMsg_CacheStorageDeleteError( |
| - request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); |
| + const base::string16& cache_name) { |
| + context_->cache_manager()->DeleteCache( |
| + version_->scope().GetOrigin(), |
| + base::UTF16ToUTF8(cache_name), |
| + base::Bind(&ServiceWorkerCacheListener::OnCacheStorageDeleteCallback, |
| + weak_factory_.GetWeakPtr(), |
| + request_id)); |
| } |
| -void ServiceWorkerStoresListener::OnCacheStorageKeys( |
| - int request_id) { |
| - // TODO(gavinp,jkarlin): Implement this method. |
| - Send(ServiceWorkerMsg_CacheStorageKeysError( |
| - request_id, blink::WebServiceWorkerCacheErrorNotImplemented)); |
| +void ServiceWorkerCacheListener::OnCacheStorageKeys(int request_id) { |
| + context_->cache_manager()->EnumerateCaches( |
| + version_->scope().GetOrigin(), |
| + base::Bind(&ServiceWorkerCacheListener::OnCacheStorageKeysCallback, |
| + weak_factory_.GetWeakPtr(), |
| + request_id)); |
| } |
| -void ServiceWorkerStoresListener::Send(const IPC::Message& message) { |
| +void ServiceWorkerCacheListener::Send(const IPC::Message& message) { |
| version_->embedded_worker()->SendMessage(message); |
| } |
| +void ServiceWorkerCacheListener::OnCacheStorageGetCallback( |
| + int request_id, |
| + int cache_id, |
| + ServiceWorkerCacheStorage::CacheStorageError error) { |
| + if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| + Send(ServiceWorkerMsg_CacheStorageGetError( |
| + request_id, ToWebServiceWorkerCacheError(error))); |
| + return; |
| + } |
| + Send(ServiceWorkerMsg_CacheStorageGetSuccess(request_id, cache_id)); |
| +} |
| + |
| +void ServiceWorkerCacheListener::OnCacheStorageHasCallback( |
| + int request_id, |
| + bool has_cache, |
| + ServiceWorkerCacheStorage::CacheStorageError error) { |
| + if (!has_cache || |
| + error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| + Send(ServiceWorkerMsg_CacheStorageHasError( |
| + request_id, ToWebServiceWorkerCacheError(error))); |
|
falken
2014/08/12 01:37:33
Looks like HasCache returns false with NO_ERROR wh
jkarlin
2014/08/12 15:17:48
Great catch! Thanks. Done.
|
| + return; |
| + } |
| + Send(ServiceWorkerMsg_CacheStorageHasSuccess(request_id)); |
| +} |
| + |
| +void ServiceWorkerCacheListener::OnCacheStorageCreateCallback( |
| + int request_id, |
| + int cache_id, |
| + ServiceWorkerCacheStorage::CacheStorageError error) { |
| + if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| + Send(ServiceWorkerMsg_CacheStorageCreateError( |
| + request_id, ToWebServiceWorkerCacheError(error))); |
| + return; |
| + } |
| + Send(ServiceWorkerMsg_CacheStorageCreateSuccess(request_id, cache_id)); |
| +} |
| + |
| +void ServiceWorkerCacheListener::OnCacheStorageDeleteCallback( |
| + int request_id, |
| + bool deleted, |
| + ServiceWorkerCacheStorage::CacheStorageError error) { |
| + if (!deleted || |
| + error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| + Send(ServiceWorkerMsg_CacheStorageDeleteError( |
| + request_id, ToWebServiceWorkerCacheError(error))); |
| + return; |
| + } |
| + Send(ServiceWorkerMsg_CacheStorageDeleteSuccess(request_id)); |
| +} |
| + |
| +void ServiceWorkerCacheListener::OnCacheStorageKeysCallback( |
| + int request_id, |
| + const std::vector<std::string>& strings, |
| + ServiceWorkerCacheStorage::CacheStorageError error) { |
| + if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) { |
| + Send(ServiceWorkerMsg_CacheStorageKeysError( |
| + request_id, ToWebServiceWorkerCacheError(error))); |
| + return; |
| + } |
| + |
| + std::vector<base::string16> string16s; |
| + for (size_t i = 0, max = strings.size(); i < max; ++i) { |
| + string16s.push_back(base::UTF8ToUTF16(strings[i])); |
|
michaeln
2014/08/12 03:59:23
nit: the type translation to blink'ish speak (WebS
jkarlin
2014/08/12 15:17:48
Looking at service_worker_messages.h it looks like
|
| + } |
| + Send(ServiceWorkerMsg_CacheStorageKeysSuccess(request_id, string16s)); |
| +} |
| + |
| } // namespace content |