Index: content/browser/service_worker/service_worker_fetch_stores.cc |
diff --git a/content/browser/service_worker/service_worker_fetch_stores.cc b/content/browser/service_worker/service_worker_fetch_stores.cc |
index 643ed4493953533f54d5e43c06b3a1ded40013ea..4b601c4004ae91a9e22e604106f3e0f76b4cbc5c 100644 |
--- a/content/browser/service_worker/service_worker_fetch_stores.cc |
+++ b/content/browser/service_worker/service_worker_fetch_stores.cc |
@@ -6,17 +6,98 @@ |
#include <string> |
+#include "base/file_util.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "content/browser/service_worker/service_worker_fetch_store.h" |
#include "content/public/browser/browser_thread.h" |
+#include "net/base/directory_lister.h" |
namespace content { |
+// Handles the loading of ServiceWorkerFetchStore and any extra clean up other |
+// than deleting the ServiceWorkerFetchStore object. |
+class ServiceWorkerFetchStores::StoresLoader { |
+ public: |
+ virtual ~StoresLoader() {}; |
+ virtual ServiceWorkerFetchStore* CreateStore(const std::string& key) = 0; |
+ virtual bool CleanUpDeletedStore(const std::string& key) = 0; |
+ virtual void LoadPersistentStores( |
+ std::vector<content::ServiceWorkerFetchStore*>* stores) = 0; |
+}; |
+ |
+class ServiceWorkerFetchStores::MemoryLoader |
+ : public ServiceWorkerFetchStores::StoresLoader { |
+ public: |
+ virtual content::ServiceWorkerFetchStore* CreateStore( |
+ const std::string& key) OVERRIDE { |
+ return ServiceWorkerFetchStore::CreateMemoryStore(key); |
+ } |
+ |
+ virtual bool CleanUpDeletedStore(const std::string& key) OVERRIDE { |
+ return true; |
+ } |
+ |
+ virtual void LoadPersistentStores( |
+ std::vector<ServiceWorkerFetchStore*>* stores) OVERRIDE { |
+ return; |
+ } |
+}; |
+ |
+class ServiceWorkerFetchStores::SimpleCacheLoader |
+ : public ServiceWorkerFetchStores::StoresLoader { |
+ public: |
+ explicit SimpleCacheLoader(const base::FilePath& origin_path) |
+ : origin_path_(origin_path) {} |
+ |
+ virtual ServiceWorkerFetchStore* CreateStore( |
+ const std::string& key) OVERRIDE { |
+ base::CreateDirectory(CreatePersistentStorePath(origin_path_, key)); |
+ return ServiceWorkerFetchStore::CreatePersistentStore( |
+ CreatePersistentStorePath(origin_path_, key), key); |
+ } |
+ |
+ virtual bool CleanUpDeletedStore(const std::string& key) OVERRIDE { |
+ return base::DeleteFile(CreatePersistentStorePath(origin_path_, key), true); |
+ } |
+ |
+ virtual void LoadPersistentStores( |
+ std::vector<ServiceWorkerFetchStore*>* stores) OVERRIDE { |
+ base::FileEnumerator files( |
+ origin_path_, false, base::FileEnumerator::DIRECTORIES); |
+ for (base::FilePath path = files.Next(); !path.empty(); |
+ path = files.Next()) { |
+ std::vector<uint8> bytes; |
+ base::HexStringToBytes(path.BaseName().MaybeAsASCII(), &bytes); |
+ std::string name(bytes.begin(), bytes.end()); |
+ |
+ stores->push_back( |
+ ServiceWorkerFetchStore::CreatePersistentStore(path, name)); |
michaeln
2014/08/05 23:34:30
This assumes that directories in here are all legi
jkarlin
2014/08/06 18:56:00
I'm not aware of a definition of legitimate store
|
+ } |
+ return; |
+ } |
+ |
+ private: |
+ base::FilePath CreatePersistentStorePath(const base::FilePath& origin_path, |
+ const std::string& store_name) { |
+ // Hexlify the store name for encoding and sanitization purposes. |
+ std::string hex = StringToLowerASCII( |
+ base::HexEncode(store_name.c_str(), store_name.length())); |
michaeln
2014/08/05 23:34:30
I'm not sure that having the length of store_name
jkarlin
2014/08/06 18:56:00
Yes, the plan was to patch that up after MVP but h
|
+ return origin_path.AppendASCII(hex); |
+ } |
+ |
+ const base::FilePath origin_path_; |
+}; |
+ |
ServiceWorkerFetchStores::ServiceWorkerFetchStores( |
const base::FilePath& path, |
- BackendType backend_type, |
+ bool memory_only, |
const scoped_refptr<base::MessageLoopProxy>& callback_loop) |
- : origin_path_(path), |
- backend_type_(backend_type), |
- callback_loop_(callback_loop) { |
+ : initialized_(false), origin_path_(path), callback_loop_(callback_loop) { |
+ if (memory_only) |
+ stores_loader_.reset(new MemoryLoader()); |
+ else |
+ stores_loader_.reset(new SimpleCacheLoader(origin_path_)); |
} |
ServiceWorkerFetchStores::~ServiceWorkerFetchStores() { |
@@ -25,47 +106,141 @@ ServiceWorkerFetchStores::~ServiceWorkerFetchStores() { |
void ServiceWorkerFetchStores::CreateStore( |
const std::string& key, |
const StoreAndErrorCallback& callback) { |
- // TODO(jkarlin): Implement this. |
+ LazyInit(); |
+ |
+ if (key.empty()) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EMPTY_KEY)); |
+ return; |
+ } |
+ |
+ if (GetLoadedStore(key)) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EXISTS)); |
+ return; |
+ } |
+ |
+ ServiceWorkerFetchStore* store = stores_loader_->CreateStore(key); |
- callback_loop_->PostTask(FROM_HERE, |
- base::Bind(callback, 0, FETCH_STORES_ERROR_EXISTS)); |
- return; |
+ if (!store) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_STORAGE)); |
+ return; |
+ } |
+ |
+ InitStore(store); |
+ |
+ callback_loop_->PostTask( |
+ FROM_HERE, |
+ base::Bind(callback, store->id(), FETCH_STORES_ERROR_NO_ERROR)); |
} |
void ServiceWorkerFetchStores::Get(const std::string& key, |
const StoreAndErrorCallback& callback) { |
- // TODO(jkarlin): Implement this. |
- |
- callback_loop_->PostTask(FROM_HERE, |
- base::Bind(callback, 0, FETCH_STORES_ERROR_EXISTS)); |
- return; |
+ LazyInit(); |
+ |
+ if (key.empty()) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EMPTY_KEY)); |
+ return; |
+ } |
+ |
+ ServiceWorkerFetchStore* store = GetLoadedStore(key); |
+ if (!store) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_NOT_FOUND)); |
+ return; |
+ } |
+ callback_loop_->PostTask( |
+ FROM_HERE, |
+ base::Bind(callback, store->id(), FETCH_STORES_ERROR_NO_ERROR)); |
} |
void ServiceWorkerFetchStores::Has(const std::string& key, |
- const BoolAndErrorCallback& callback) const { |
- // TODO(jkarlin): Implement this. |
+ const BoolAndErrorCallback& callback) { |
+ LazyInit(); |
+ |
+ if (key.empty()) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EMPTY_KEY)); |
+ return; |
+ } |
callback_loop_->PostTask( |
- FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_EXISTS)); |
- return; |
+ FROM_HERE, |
+ base::Bind(callback, GetLoadedStore(key), FETCH_STORES_ERROR_NO_ERROR)); |
} |
void ServiceWorkerFetchStores::Delete(const std::string& key, |
- const StoreAndErrorCallback& callback) { |
- // TODO(jkarlin): Implement this. |
+ const BoolAndErrorCallback& callback) { |
+ LazyInit(); |
+ |
+ if (key.empty()) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_EMPTY_KEY)); |
+ return; |
+ } |
+ |
+ ServiceWorkerFetchStore* store = GetLoadedStore(key); |
+ if (!store) { |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_NOT_FOUND)); |
+ return; |
+ } |
+ |
+ loaded_store_names_.erase(key); |
+ loaded_store_ids_.Remove(store->id()); // deletes store |
+ |
+ stores_loader_->CleanUpDeletedStore(key); |
- callback_loop_->PostTask(FROM_HERE, |
- base::Bind(callback, 0, FETCH_STORES_ERROR_EXISTS)); |
- return; |
+ callback_loop_->PostTask( |
+ FROM_HERE, base::Bind(callback, true, FETCH_STORES_ERROR_NO_ERROR)); |
} |
-void ServiceWorkerFetchStores::Keys( |
- const StringsAndErrorCallback& callback) const { |
- // TODO(jkarlin): Implement this. |
- std::vector<std::string> out; |
+void ServiceWorkerFetchStores::Keys(const StringsAndErrorCallback& callback) { |
+ LazyInit(); |
+ |
+ std::vector<std::string> names; |
+ for (NameMap::const_iterator it = loaded_store_names_.begin(); |
+ it != loaded_store_names_.end(); |
+ ++it) { |
+ names.push_back(it->first); |
+ } |
+ |
callback_loop_->PostTask( |
- FROM_HERE, base::Bind(callback, out, FETCH_STORES_ERROR_EXISTS)); |
- return; |
+ FROM_HERE, base::Bind(callback, names, FETCH_STORES_ERROR_NO_ERROR)); |
+} |
+ |
+void ServiceWorkerFetchStores::LazyInit() { |
+ if (initialized_) |
+ return; |
+ |
+ // Load up all of the existing caches. |
+ std::vector<ServiceWorkerFetchStore*> stores; |
+ stores_loader_->LoadPersistentStores(&stores); |
+ |
+ for (size_t i = 0, size = stores.size(); i < size; ++i) |
+ InitStore(stores[i]); |
+ |
+ initialized_ = true; |
+} |
+ |
+void ServiceWorkerFetchStores::InitStore(ServiceWorkerFetchStore* store) { |
+ StoreID id = loaded_store_ids_.Add(store); |
+ loaded_store_names_.insert(std::make_pair(store->name(), id)); |
+ store->set_id(id); |
michaeln
2014/08/05 23:34:30
What is the purpose if the store id and having two
jkarlin
2014/08/06 18:55:59
Yep yep.
|
+} |
+ |
+ServiceWorkerFetchStore* ServiceWorkerFetchStores::GetLoadedStore( |
+ const std::string& key) const { |
+ DCHECK(initialized_); |
+ NameMap::const_iterator it = loaded_store_names_.find(key); |
+ if (it == loaded_store_names_.end()) |
+ return NULL; |
+ |
+ ServiceWorkerFetchStore* store = loaded_store_ids_.Lookup(it->second); |
+ DCHECK(store); |
+ return store; |
} |
} // namespace content |