Commit 56f997e3 authored by jkarlin's avatar jkarlin Committed by Commit bot

[ServiceWorkerCacheStorage] Add support for new Open method.

Create/Get have been replaced by Open which will first attempt to Get() followed by Create() if Get() failed.

CLs in this three-legged-patch:

*1. Add open to browser - https://codereview.chromium.org/664433003/
2. Add open (remove get/create) to Blink - https://codereview.chromium.org/638023003
3. Remove get/create from browser - https://codereview.chromium.org/663503002/

Note that the three CLs have been tested together and pass tests locally.

BUG=423942

Review URL: https://codereview.chromium.org/664433003

Cr-Commit-Position: refs/heads/master@{#300047}
parent a401ea2a
......@@ -93,6 +93,8 @@ bool ServiceWorkerCacheListener::OnMessageReceived(
OnCacheStorageHas)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageCreate,
OnCacheStorageCreate)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageOpen,
OnCacheStorageOpen)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete,
OnCacheStorageDelete)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys,
......@@ -153,6 +155,19 @@ void ServiceWorkerCacheListener::OnCacheStorageCreate(
request_id));
}
void ServiceWorkerCacheListener::OnCacheStorageOpen(
int request_id,
const base::string16& cache_name) {
TRACE_EVENT0("ServiceWorker",
"ServiceWorkerCacheListener::OnCacheStorageOpen");
context_->cache_manager()->OpenCache(
version_->scope().GetOrigin(),
base::UTF16ToUTF8(cache_name),
base::Bind(&ServiceWorkerCacheListener::OnCacheStorageOpenCallback,
weak_factory_.GetWeakPtr(),
request_id));
}
void ServiceWorkerCacheListener::OnCacheStorageDelete(
int request_id,
const base::string16& cache_name) {
......@@ -344,6 +359,19 @@ void ServiceWorkerCacheListener::OnCacheStorageCreateCallback(
Send(ServiceWorkerMsg_CacheStorageCreateSuccess(request_id, cache_id));
}
void ServiceWorkerCacheListener::OnCacheStorageOpenCallback(
int request_id,
const scoped_refptr<ServiceWorkerCache>& cache,
ServiceWorkerCacheStorage::CacheStorageError error) {
if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
Send(ServiceWorkerMsg_CacheStorageOpenError(
request_id, ToWebServiceWorkerCacheError(error)));
return;
}
CacheID cache_id = StoreCacheReference(cache);
Send(ServiceWorkerMsg_CacheStorageOpenSuccess(request_id, cache_id));
}
void ServiceWorkerCacheListener::OnCacheStorageDeleteCallback(
int request_id,
bool deleted,
......
......@@ -39,6 +39,7 @@ class ServiceWorkerCacheListener : public EmbeddedWorkerInstance::Listener {
void OnCacheStorageHas(int request_id, const base::string16& cache_name);
void OnCacheStorageCreate(int request_id,
const base::string16& cache_name);
void OnCacheStorageOpen(int request_id, const base::string16& cache_name);
void OnCacheStorageDelete(int request_id,
const base::string16& cache_name);
void OnCacheStorageKeys(int request_id);
......@@ -83,6 +84,10 @@ class ServiceWorkerCacheListener : public EmbeddedWorkerInstance::Listener {
int request_id,
const scoped_refptr<ServiceWorkerCache>& cache,
ServiceWorkerCacheStorage::CacheStorageError error);
void OnCacheStorageOpenCallback(
int request_id,
const scoped_refptr<ServiceWorkerCache>& cache,
ServiceWorkerCacheStorage::CacheStorageError error);
void OnCacheStorageDeleteCallback(
int request_id,
bool deleted,
......
......@@ -376,6 +376,33 @@ void ServiceWorkerCacheStorage::CreateCache(
callback));
}
void ServiceWorkerCacheStorage::OpenCache(
const std::string& cache_name,
const CacheAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!initialized_) {
LazyInit(base::Bind(&ServiceWorkerCacheStorage::OpenCache,
weak_factory_.GetWeakPtr(),
cache_name,
callback));
return;
}
scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name);
if (cache.get()) {
callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR);
return;
}
cache_loader_->CreateCache(
cache_name,
base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache,
weak_factory_.GetWeakPtr(),
cache_name,
callback));
}
void ServiceWorkerCacheStorage::GetCache(
const std::string& cache_name,
const CacheAndErrorCallback& callback) {
......
......@@ -59,16 +59,21 @@ class CONTENT_EXPORT ServiceWorkerCacheStorage {
virtual ~ServiceWorkerCacheStorage();
// Create a ServiceWorkerCache if it doesn't already exist and call the
// callback with the cache's id. If it already
// callback with the cache. If it already
// exists the callback is called with CACHE_STORAGE_ERROR_EXISTS.
void CreateCache(const std::string& cache_name,
const CacheAndErrorCallback& callback);
// Get the cache id for the given key. If not found returns
// Get the cache for the given key. If not found returns
// CACHE_STORAGE_ERROR_NOT_FOUND.
void GetCache(const std::string& cache_name,
const CacheAndErrorCallback& callback);
// Get the cache for the given key. If the cache is not found it is
// created.
void OpenCache(const std::string& cache_name,
const CacheAndErrorCallback& callback);
// Calls the callback with whether or not the cache exists.
void HasCache(const std::string& cache_name,
const BoolAndErrorCallback& callback);
......
......@@ -92,6 +92,18 @@ void ServiceWorkerCacheStorageManager::GetCache(
cache_storage->GetCache(cache_name, callback);
}
void ServiceWorkerCacheStorageManager::OpenCache(
const GURL& origin,
const std::string& cache_name,
const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerCacheStorage* cache_storage =
FindOrCreateServiceWorkerCacheManager(origin);
cache_storage->OpenCache(cache_name, callback);
}
void ServiceWorkerCacheStorageManager::HasCache(
const GURL& origin,
const std::string& cache_name,
......
......@@ -53,6 +53,10 @@ class CONTENT_EXPORT ServiceWorkerCacheStorageManager {
const GURL& origin,
const std::string& cache_name,
const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback);
void OpenCache(
const GURL& origin,
const std::string& cache_name,
const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback);
void HasCache(
const GURL& origin,
const std::string& cache_name,
......
......@@ -140,6 +140,25 @@ class ServiceWorkerCacheStorageManagerTest : public testing::Test {
return !error;
}
bool Open(const GURL& origin, const std::string& cache_name) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_manager_->OpenCache(
origin,
cache_name,
base::Bind(&ServiceWorkerCacheStorageManagerTest::CacheAndErrorCallback,
base::Unretained(this),
base::Unretained(loop.get())));
loop->Run();
bool error = callback_error_ !=
ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR;
if (error)
EXPECT_TRUE(!callback_cache_.get());
else
EXPECT_TRUE(callback_cache_.get());
return !error;
}
bool Has(const GURL& origin, const std::string& cache_name) {
scoped_ptr<base::RunLoop> loop(new base::RunLoop());
cache_manager_->HasCache(
......@@ -295,6 +314,17 @@ TEST_P(ServiceWorkerCacheStorageManagerTestP, GetNonExistent) {
callback_error_);
}
TEST_P(ServiceWorkerCacheStorageManagerTestP, OpenNewCache) {
EXPECT_TRUE(Open(origin1_, "foo"));
}
TEST_P(ServiceWorkerCacheStorageManagerTestP, OpenExistingCache) {
EXPECT_TRUE(CreateCache(origin1_, "foo"));
scoped_refptr<ServiceWorkerCache> cache = callback_cache_;
EXPECT_TRUE(Open(origin1_, "foo"));
EXPECT_TRUE(cache.get() == callback_cache_.get());
}
TEST_P(ServiceWorkerCacheStorageManagerTestP, HasCache) {
EXPECT_TRUE(CreateCache(origin1_, "foo"));
EXPECT_TRUE(Has(origin1_, "foo"));
......
......@@ -201,6 +201,10 @@ IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_CacheStorageCreate,
int /* request_id */,
base::string16 /* fetch_store_name */)
IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_CacheStorageOpen,
int /* request_id */,
base::string16 /* fetch_store_name */)
IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_CacheStorageDelete,
int /* request_id */,
base::string16 /* fetch_store_name */)
......@@ -369,6 +373,9 @@ IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_CacheStorageHasSuccess,
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageCreateSuccess,
int /* request_id */,
int /* fetch_store_id */)
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageOpenSuccess,
int /* request_id */,
int /* fetch_store_id */)
IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_CacheStorageDeleteSuccess,
int /* request_id */)
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageKeysSuccess,
......@@ -385,6 +392,9 @@ IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageHasError,
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageCreateError,
int /* request_id */,
blink::WebServiceWorkerCacheError /* reason */)
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageOpenError,
int /* request_id */,
blink::WebServiceWorkerCacheError /* reason */)
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_CacheStorageDeleteError,
int /* request_id */,
blink::WebServiceWorkerCacheError /* reason */)
......
......@@ -207,6 +207,7 @@ ServiceWorkerCacheStorageDispatcher::~ServiceWorkerCacheStorageDispatcher() {
ClearCallbacksMapWithErrors(&get_callbacks_);
ClearCallbacksMapWithErrors(&has_callbacks_);
ClearCallbacksMapWithErrors(&create_callbacks_);
ClearCallbacksMapWithErrors(&open_callbacks_);
ClearCallbacksMapWithErrors(&delete_callbacks_);
ClearCallbacksMapWithErrors(&keys_callbacks_);
......@@ -226,6 +227,8 @@ bool ServiceWorkerCacheStorageDispatcher::OnMessageReceived(
OnCacheStorageHasSuccess)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageCreateSuccess,
OnCacheStorageCreateSuccess)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageOpenSuccess,
OnCacheStorageOpenSuccess)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageDeleteSuccess,
OnCacheStorageDeleteSuccess)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageKeysSuccess,
......@@ -236,6 +239,8 @@ bool ServiceWorkerCacheStorageDispatcher::OnMessageReceived(
OnCacheStorageHasError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageCreateError,
OnCacheStorageCreateError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageOpenError,
OnCacheStorageOpenError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageDeleteError,
OnCacheStorageDeleteError)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageKeysError,
......@@ -290,6 +295,17 @@ void ServiceWorkerCacheStorageDispatcher::OnCacheStorageCreateSuccess(
create_callbacks_.Remove(request_id);
}
void ServiceWorkerCacheStorageDispatcher::OnCacheStorageOpenSuccess(
int request_id,
int cache_id) {
WebCache* web_cache = new WebCache(weak_factory_.GetWeakPtr(), cache_id);
web_caches_.AddWithID(web_cache, cache_id);
CacheStorageWithCacheCallbacks* callbacks =
open_callbacks_.Lookup(request_id);
callbacks->onSuccess(web_cache);
open_callbacks_.Remove(request_id);
}
void ServiceWorkerCacheStorageDispatcher::OnCacheStorageDeleteSuccess(
int request_id) {
CacheStorageCallbacks* callbacks = delete_callbacks_.Lookup(request_id);
......@@ -335,6 +351,15 @@ void ServiceWorkerCacheStorageDispatcher::OnCacheStorageCreateError(
create_callbacks_.Remove(request_id);
}
void ServiceWorkerCacheStorageDispatcher::OnCacheStorageOpenError(
int request_id,
blink::WebServiceWorkerCacheError reason) {
CacheStorageWithCacheCallbacks* callbacks =
open_callbacks_.Lookup(request_id);
callbacks->onError(&reason);
open_callbacks_.Remove(request_id);
}
void ServiceWorkerCacheStorageDispatcher::OnCacheStorageDeleteError(
int request_id,
blink::WebServiceWorkerCacheError reason) {
......@@ -460,6 +485,14 @@ void ServiceWorkerCacheStorageDispatcher::dispatchCreate(
script_context_->GetRoutingID(), request_id, cacheName));
}
void ServiceWorkerCacheStorageDispatcher::dispatchOpen(
CacheStorageWithCacheCallbacks* callbacks,
const blink::WebString& cacheName) {
int request_id = open_callbacks_.Add(callbacks);
script_context_->Send(new ServiceWorkerHostMsg_CacheStorageOpen(
script_context_->GetRoutingID(), request_id, cacheName));
}
void ServiceWorkerCacheStorageDispatcher::dispatchDelete(
CacheStorageCallbacks* callbacks,
const blink::WebString& cacheName) {
......
......@@ -41,6 +41,7 @@ class ServiceWorkerCacheStorageDispatcher
void OnCacheStorageGetSuccess(int request_id, int cache_id);
void OnCacheStorageHasSuccess(int request_id);
void OnCacheStorageCreateSuccess(int request_id, int cache_id);
void OnCacheStorageOpenSuccess(int request_id, int cache_id);
void OnCacheStorageDeleteSuccess(int request_id);
void OnCacheStorageKeysSuccess(int request_id,
const std::vector<base::string16>& keys);
......@@ -51,6 +52,8 @@ class ServiceWorkerCacheStorageDispatcher
blink::WebServiceWorkerCacheError reason);
void OnCacheStorageCreateError(int request_id,
blink::WebServiceWorkerCacheError reason);
void OnCacheStorageOpenError(int request_id,
blink::WebServiceWorkerCacheError reason);
void OnCacheStorageDeleteError(int request_id,
blink::WebServiceWorkerCacheError reason);
void OnCacheStorageKeysError(int request_id,
......@@ -84,6 +87,8 @@ class ServiceWorkerCacheStorageDispatcher
const blink::WebString& cacheName);
virtual void dispatchCreate(CacheStorageWithCacheCallbacks* callbacks,
const blink::WebString& cacheName);
virtual void dispatchOpen(CacheStorageWithCacheCallbacks* callbacks,
const blink::WebString& cacheName);
virtual void dispatchDelete(CacheStorageCallbacks* callbacks,
const blink::WebString& cacheName);
virtual void dispatchKeys(CacheStorageKeysCallbacks* callbacks);
......@@ -142,6 +147,7 @@ class ServiceWorkerCacheStorageDispatcher
WithCacheCallbacksMap get_callbacks_;
CallbacksMap has_callbacks_;
WithCacheCallbacksMap create_callbacks_;
WithCacheCallbacksMap open_callbacks_;
CallbacksMap delete_callbacks_;
KeysCallbacksMap keys_callbacks_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment