Commit acb4e9e2 authored by jkarlin@chromium.org's avatar jkarlin@chromium.org

Fills in ServiceWorkerFetchStores. Adds a stub ServiceWorkerFetchStore class...

Fills in ServiceWorkerFetchStores.  Adds a stub ServiceWorkerFetchStore class which will need to be filled in. Also adds some tests.

A CL to refactor ServiceWorkerFetchStores -> ServiceWorkerCacheStorage is upcoming.

BUG=392621

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

Cr-Commit-Position: refs/heads/master@{#288379}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288379 0039d316-1c4b-4281-b951-d872f2087c98
parent 8c271870
...@@ -24,7 +24,7 @@ source_set("browser") { ...@@ -24,7 +24,7 @@ source_set("browser") {
deps = [ deps = [
"//base", "//base",
"//content:resources", "//content:resources",
"//content/browser/service_worker:database_proto", "//content/browser/service_worker:proto",
"//content/browser/speech/proto", "//content/browser/speech/proto",
"//crypto", "//crypto",
"//google_apis", "//google_apis",
......
...@@ -4,8 +4,9 @@ ...@@ -4,8 +4,9 @@
import("//third_party/protobuf/proto_library.gni") import("//third_party/protobuf/proto_library.gni")
proto_library("database_proto") { proto_library("proto") {
sources = [ sources = [
"service_worker_cache.proto",
"service_worker_database.proto", "service_worker_database.proto",
] ]
} }
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
syntax = "proto2";
option optimize_for = LITE_RUNTIME;
package content;
message ServiceWorkerCacheStorageIndex {
message Cache {
required string name = 1;
required int32 size = 2;
}
repeated Cache cache = 1;
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/service_worker/service_worker_fetch_store.h"
#include <string>
#include "base/files/file_path.h"
namespace content {
// static
ServiceWorkerFetchStore* ServiceWorkerFetchStore::CreateMemoryStore(
const std::string& name) {
return new ServiceWorkerFetchStore(base::FilePath(), name);
}
// static
ServiceWorkerFetchStore* ServiceWorkerFetchStore::CreatePersistentStore(
const base::FilePath& path,
const std::string& name) {
return new ServiceWorkerFetchStore(path, name);
}
void ServiceWorkerFetchStore::CreateBackend(
const base::Callback<void(bool)>& callback) {
callback.Run(true);
}
ServiceWorkerFetchStore::ServiceWorkerFetchStore(const base::FilePath& path,
const std::string& name)
: path_(path), name_(name), id_(0) {
}
ServiceWorkerFetchStore::~ServiceWorkerFetchStore() {
}
} // namespace content
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORE_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORE_H_
#include "base/callback.h"
#include "base/files/file_path.h"
namespace content {
// TODO(jkarlin): Fill this in with a real FetchStore implementation as
// specified in
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
// TODO(jkarlin): Unload store backend from memory once the store object is no
// longer referenced in javascript.
// Represents a ServiceWorker FetchStore as seen in
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
// InitializeIfNeeded must be called before calling the other public members.
class ServiceWorkerFetchStore {
public:
static ServiceWorkerFetchStore* CreateMemoryStore(const std::string& name);
static ServiceWorkerFetchStore* CreatePersistentStore(
const base::FilePath& path,
const std::string& name);
virtual ~ServiceWorkerFetchStore();
// Loads the backend and calls the callback with the result (true for
// success). This must be called before member functions that require a
// backend are called.
void CreateBackend(const base::Callback<void(bool)>& callback);
void set_name(const std::string& name) { name_ = name; }
const std::string& name() const { return name_; }
int32 id() const { return id_; }
void set_id(int32 id) { id_ = id; }
private:
ServiceWorkerFetchStore(const base::FilePath& path, const std::string& name);
base::FilePath path_;
std::string name_;
int32 id_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStore);
};
} // namespace content
#endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORE_H_
...@@ -5,10 +5,12 @@ ...@@ -5,10 +5,12 @@
#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_ #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_FETCH_STORES_H_
#include <map>
#include <string> #include <string>
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/id_map.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
namespace base { namespace base {
...@@ -19,7 +21,8 @@ namespace content { ...@@ -19,7 +21,8 @@ namespace content {
class ServiceWorkerFetchStore; class ServiceWorkerFetchStore;
// The set of stores for a given ServiceWorker. It is owned by the // TODO(jkarlin): Constrain the total bytes used per origin.
// The set of stores for a given origin. It is owned by the
// ServiceWorkerFetchStoresManager. Provided callbacks are called on the // ServiceWorkerFetchStoresManager. Provided callbacks are called on the
// |callback_loop| provided in the constructor. // |callback_loop| provided in the constructor.
class ServiceWorkerFetchStores { class ServiceWorkerFetchStores {
...@@ -29,11 +32,10 @@ class ServiceWorkerFetchStores { ...@@ -29,11 +32,10 @@ class ServiceWorkerFetchStores {
FETCH_STORES_ERROR_NOT_IMPLEMENTED, FETCH_STORES_ERROR_NOT_IMPLEMENTED,
FETCH_STORES_ERROR_NOT_FOUND, FETCH_STORES_ERROR_NOT_FOUND,
FETCH_STORES_ERROR_EXISTS, FETCH_STORES_ERROR_EXISTS,
FETCH_STORES_ERROR_STORAGE FETCH_STORES_ERROR_STORAGE,
FETCH_STORES_ERROR_EMPTY_KEY,
}; };
enum BackendType { BACKEND_TYPE_SIMPLE_CACHE, BACKEND_TYPE_MEMORY };
typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback; typedef base::Callback<void(bool, FetchStoresError)> BoolAndErrorCallback;
typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback; typedef base::Callback<void(int, FetchStoresError)> StoreAndErrorCallback;
typedef base::Callback<void(const std::vector<std::string>&, typedef base::Callback<void(const std::vector<std::string>&,
...@@ -41,22 +43,60 @@ class ServiceWorkerFetchStores { ...@@ -41,22 +43,60 @@ class ServiceWorkerFetchStores {
ServiceWorkerFetchStores( ServiceWorkerFetchStores(
const base::FilePath& origin_path, const base::FilePath& origin_path,
BackendType backend, bool memory_only,
const scoped_refptr<base::MessageLoopProxy>& callback_loop); const scoped_refptr<base::MessageLoopProxy>& callback_loop);
virtual ~ServiceWorkerFetchStores(); virtual ~ServiceWorkerFetchStores();
void CreateStore(const std::string& key, // Create a ServiceWorkerFetchStore if it doesn't already exist and call the
// callback with the cache's id. If it already
// exists the callback is called with FETCH_STORES_ERROR_EXISTS.
void CreateStore(const std::string& store_name,
const StoreAndErrorCallback& callback);
// Get the cache id for the given key. If not found returns
// FETCH_STORES_ERROR_NOT_FOUND.
void GetStore(const std::string& store_name,
const StoreAndErrorCallback& callback); const StoreAndErrorCallback& callback);
void Get(const std::string& key, const StoreAndErrorCallback& callback);
void Has(const std::string& key, const BoolAndErrorCallback& callback) const; // Calls the callback with whether or not the cache exists.
void Delete(const std::string& key, const StoreAndErrorCallback& callback); void HasStore(const std::string& store_name,
void Keys(const StringsAndErrorCallback& callback) const; const BoolAndErrorCallback& callback);
// Deletes the cache if it exists. If it doesn't exist,
// FETCH_STORES_ERROR_NOT_FOUND is returned.
void DeleteStore(const std::string& store_name,
const BoolAndErrorCallback& callback);
// Calls the callback with a vector of cache names (keys) available.
void EnumerateStores(const StringsAndErrorCallback& callback);
// TODO(jkarlin): Add match() function. // TODO(jkarlin): Add match() function.
void InitializeStoreCallback(const ServiceWorkerFetchStore* store,
const StoreAndErrorCallback& callback,
bool success);
private: private:
class MemoryLoader;
class SimpleCacheLoader;
class StoresLoader;
typedef IDMap<ServiceWorkerFetchStore, IDMapOwnPointer> StoreMap;
typedef StoreMap::KeyType StoreID;
typedef std::map<std::string, StoreID> NameMap;
ServiceWorkerFetchStore* GetLoadedStore(const std::string& key) const;
void LazyInit();
void InitStore(ServiceWorkerFetchStore* store);
bool initialized_;
StoreMap store_map_;
NameMap name_map_;
base::FilePath origin_path_; base::FilePath origin_path_;
BackendType backend_type_;
scoped_refptr<base::MessageLoopProxy> callback_loop_; scoped_refptr<base::MessageLoopProxy> callback_loop_;
scoped_ptr<StoresLoader> stores_loader_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores); DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFetchStores);
}; };
......
...@@ -65,7 +65,7 @@ ServiceWorkerFetchStoresManager::~ServiceWorkerFetchStoresManager() { ...@@ -65,7 +65,7 @@ ServiceWorkerFetchStoresManager::~ServiceWorkerFetchStoresManager() {
void ServiceWorkerFetchStoresManager::CreateStore( void ServiceWorkerFetchStoresManager::CreateStore(
const GURL& origin, const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback) { const ServiceWorkerFetchStores::StoreAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -76,56 +76,57 @@ void ServiceWorkerFetchStoresManager::CreateStore( ...@@ -76,56 +76,57 @@ void ServiceWorkerFetchStoresManager::CreateStore(
FROM_HERE, FROM_HERE,
base::Bind(&ServiceWorkerFetchStores::CreateStore, base::Bind(&ServiceWorkerFetchStores::CreateStore,
base::Unretained(stores), base::Unretained(stores),
key, store_name,
callback)); callback));
} }
void ServiceWorkerFetchStoresManager::Get( void ServiceWorkerFetchStoresManager::GetStore(
const GURL& origin, const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback) { const ServiceWorkerFetchStores::StoreAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerFetchStores* stores = ServiceWorkerFetchStores* stores =
FindOrCreateServiceWorkerFetchStores(origin); FindOrCreateServiceWorkerFetchStores(origin);
stores_task_runner_->PostTask(FROM_HERE, stores_task_runner_->PostTask(FROM_HERE,
base::Bind(&ServiceWorkerFetchStores::Get, base::Bind(&ServiceWorkerFetchStores::GetStore,
base::Unretained(stores), base::Unretained(stores),
key, store_name,
callback)); callback));
} }
void ServiceWorkerFetchStoresManager::Has( void ServiceWorkerFetchStoresManager::HasStore(
const GURL& origin, const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::BoolAndErrorCallback& callback) { const ServiceWorkerFetchStores::BoolAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerFetchStores* stores = ServiceWorkerFetchStores* stores =
FindOrCreateServiceWorkerFetchStores(origin); FindOrCreateServiceWorkerFetchStores(origin);
stores_task_runner_->PostTask(FROM_HERE, stores_task_runner_->PostTask(FROM_HERE,
base::Bind(&ServiceWorkerFetchStores::Has, base::Bind(&ServiceWorkerFetchStores::HasStore,
base::Unretained(stores), base::Unretained(stores),
key, store_name,
callback)); callback));
} }
void ServiceWorkerFetchStoresManager::Delete( void ServiceWorkerFetchStoresManager::DeleteStore(
const GURL& origin, const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback) { const ServiceWorkerFetchStores::BoolAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerFetchStores* stores = ServiceWorkerFetchStores* stores =
FindOrCreateServiceWorkerFetchStores(origin); FindOrCreateServiceWorkerFetchStores(origin);
stores_task_runner_->PostTask(FROM_HERE, stores_task_runner_->PostTask(
base::Bind(&ServiceWorkerFetchStores::Delete, FROM_HERE,
base::Bind(&ServiceWorkerFetchStores::DeleteStore,
base::Unretained(stores), base::Unretained(stores),
key, store_name,
callback)); callback));
} }
void ServiceWorkerFetchStoresManager::Keys( void ServiceWorkerFetchStoresManager::EnumerateStores(
const GURL& origin, const GURL& origin,
const ServiceWorkerFetchStores::StringsAndErrorCallback& callback) { const ServiceWorkerFetchStores::StringsAndErrorCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
...@@ -135,8 +136,9 @@ void ServiceWorkerFetchStoresManager::Keys( ...@@ -135,8 +136,9 @@ void ServiceWorkerFetchStoresManager::Keys(
stores_task_runner_->PostTask( stores_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind( base::Bind(&ServiceWorkerFetchStores::EnumerateStores,
&ServiceWorkerFetchStores::Keys, base::Unretained(stores), callback)); base::Unretained(stores),
callback));
} }
ServiceWorkerFetchStoresManager::ServiceWorkerFetchStoresManager( ServiceWorkerFetchStoresManager::ServiceWorkerFetchStoresManager(
...@@ -148,16 +150,15 @@ ServiceWorkerFetchStoresManager::ServiceWorkerFetchStoresManager( ...@@ -148,16 +150,15 @@ ServiceWorkerFetchStoresManager::ServiceWorkerFetchStoresManager(
ServiceWorkerFetchStores* ServiceWorkerFetchStores*
ServiceWorkerFetchStoresManager::FindOrCreateServiceWorkerFetchStores( ServiceWorkerFetchStoresManager::FindOrCreateServiceWorkerFetchStores(
const GURL& origin) { const GURL& origin) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerFetchStoresMap::const_iterator it = ServiceWorkerFetchStoresMap::const_iterator it =
service_worker_fetch_stores_.find(origin); service_worker_fetch_stores_.find(origin);
if (it == service_worker_fetch_stores_.end()) { if (it == service_worker_fetch_stores_.end()) {
ServiceWorkerFetchStores::BackendType backend = bool memory_only = root_path_.empty();
root_path_.empty()
? ServiceWorkerFetchStores::BACKEND_TYPE_MEMORY
: ServiceWorkerFetchStores::BACKEND_TYPE_SIMPLE_CACHE;
ServiceWorkerFetchStores* fetch_stores = ServiceWorkerFetchStores* fetch_stores =
new ServiceWorkerFetchStores(ConstructOriginPath(root_path_, origin), new ServiceWorkerFetchStores(ConstructOriginPath(root_path_, origin),
backend, memory_only,
base::MessageLoopProxy::current()); base::MessageLoopProxy::current());
// The map owns fetch_stores. // The map owns fetch_stores.
service_worker_fetch_stores_.insert(std::make_pair(origin, fetch_stores)); service_worker_fetch_stores_.insert(std::make_pair(origin, fetch_stores));
......
...@@ -22,6 +22,8 @@ namespace content { ...@@ -22,6 +22,8 @@ namespace content {
// Keeps track of a ServiceWorkerFetchStores per origin. There is one // Keeps track of a ServiceWorkerFetchStores per origin. There is one
// ServiceWorkerFetchStoresManager per ServiceWorkerContextCore. // ServiceWorkerFetchStoresManager per ServiceWorkerContextCore.
// TODO(jkarlin): Remove ServiceWorkerFetchStores from memory once they're no
// longer in active use.
class CONTENT_EXPORT ServiceWorkerFetchStoresManager { class CONTENT_EXPORT ServiceWorkerFetchStoresManager {
public: public:
static scoped_ptr<ServiceWorkerFetchStoresManager> Create( static scoped_ptr<ServiceWorkerFetchStoresManager> Create(
...@@ -37,18 +39,21 @@ class CONTENT_EXPORT ServiceWorkerFetchStoresManager { ...@@ -37,18 +39,21 @@ class CONTENT_EXPORT ServiceWorkerFetchStoresManager {
// corresponding ServiceWorkerFetchStores method on the appropriate thread. // corresponding ServiceWorkerFetchStores method on the appropriate thread.
void CreateStore( void CreateStore(
const GURL& origin, const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback); const ServiceWorkerFetchStores::StoreAndErrorCallback& callback);
void Get(const GURL& origin, void GetStore(
const std::string& key, const GURL& origin,
const std::string& store_name,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback); const ServiceWorkerFetchStores::StoreAndErrorCallback& callback);
void Has(const GURL& origin, void HasStore(const GURL& origin,
const std::string& key, const std::string& store_name,
const ServiceWorkerFetchStores::BoolAndErrorCallback& callback); const ServiceWorkerFetchStores::BoolAndErrorCallback& callback);
void Delete(const GURL& origin, void DeleteStore(
const std::string& key, const GURL& origin,
const ServiceWorkerFetchStores::StoreAndErrorCallback& callback); const std::string& store_name,
void Keys(const GURL& origin, const ServiceWorkerFetchStores::BoolAndErrorCallback& callback);
void EnumerateStores(
const GURL& origin,
const ServiceWorkerFetchStores::StringsAndErrorCallback& callback); const ServiceWorkerFetchStores::StringsAndErrorCallback& callback);
// TODO(jkarlin): Add match() function. // TODO(jkarlin): Add match() function.
......
{ {
'targets': [ 'targets': [
{ {
# GN version: //content/browser/service_worker:database_proto # GN version: //content/browser/service_worker:proto
'target_name': 'database_proto', 'target_name': 'proto',
'type': 'static_library', 'type': 'static_library',
'sources': [ 'sources': [
'service_worker_cache.proto',
'service_worker_database.proto', 'service_worker_database.proto',
], ],
'variables': { 'variables': {
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
'../ui/gfx/gfx.gyp:gfx_geometry', '../ui/gfx/gfx.gyp:gfx_geometry',
'../ui/resources/ui_resources.gyp:ui_resources', '../ui/resources/ui_resources.gyp:ui_resources',
'../ui/snapshot/snapshot.gyp:snapshot', '../ui/snapshot/snapshot.gyp:snapshot',
'browser/service_worker/service_worker_proto.gyp:database_proto', 'browser/service_worker/service_worker_proto.gyp:proto',
'browser/speech/proto/speech_proto.gyp:speech_proto', 'browser/speech/proto/speech_proto.gyp:speech_proto',
], ],
'export_dependent_settings': [ 'export_dependent_settings': [
...@@ -1137,6 +1137,8 @@ ...@@ -1137,6 +1137,8 @@
'browser/service_worker/service_worker_dispatcher_host.h', 'browser/service_worker/service_worker_dispatcher_host.h',
'browser/service_worker/service_worker_fetch_dispatcher.cc', 'browser/service_worker/service_worker_fetch_dispatcher.cc',
'browser/service_worker/service_worker_fetch_dispatcher.h', 'browser/service_worker/service_worker_fetch_dispatcher.h',
'browser/service_worker/service_worker_fetch_store.cc',
'browser/service_worker/service_worker_fetch_store.h',
'browser/service_worker/service_worker_fetch_stores.cc', 'browser/service_worker/service_worker_fetch_stores.cc',
'browser/service_worker/service_worker_fetch_stores.h', 'browser/service_worker/service_worker_fetch_stores.h',
'browser/service_worker/service_worker_fetch_stores_manager.cc', 'browser/service_worker/service_worker_fetch_stores_manager.cc',
......
...@@ -352,7 +352,7 @@ ...@@ -352,7 +352,7 @@
'target_name': 'content_unittests', 'target_name': 'content_unittests',
'type': '<(gtest_target_type)', 'type': '<(gtest_target_type)',
'dependencies': [ 'dependencies': [
'browser/service_worker/service_worker_proto.gyp:database_proto', 'browser/service_worker/service_worker_proto.gyp:proto',
'browser/speech/proto/speech_proto.gyp:speech_proto', 'browser/speech/proto/speech_proto.gyp:speech_proto',
'content.gyp:content_browser', 'content.gyp:content_browser',
'content.gyp:content_common', 'content.gyp:content_common',
...@@ -595,7 +595,7 @@ ...@@ -595,7 +595,7 @@
'browser/service_worker/service_worker_controllee_request_handler_unittest.cc', 'browser/service_worker/service_worker_controllee_request_handler_unittest.cc',
'browser/service_worker/service_worker_database_unittest.cc', 'browser/service_worker/service_worker_database_unittest.cc',
'browser/service_worker/service_worker_dispatcher_host_unittest.cc', 'browser/service_worker/service_worker_dispatcher_host_unittest.cc',
'browser/service_worker/service_worker_dispatcher_host_unittest.cc', 'browser/service_worker/service_worker_fetch_stores_manager_unittest.cc',
'browser/service_worker/service_worker_handle_unittest.cc', 'browser/service_worker/service_worker_handle_unittest.cc',
'browser/service_worker/service_worker_job_unittest.cc', 'browser/service_worker/service_worker_job_unittest.cc',
'browser/service_worker/service_worker_provider_host_unittest.cc', 'browser/service_worker/service_worker_provider_host_unittest.cc',
......
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