Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(760)

Unified Diff: content/browser/service_worker/service_worker_cache.h

Issue 465463002: Initial implementation of ServiceWorkerCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cache2
Patch Set: Nits Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/service_worker/service_worker_cache.h
diff --git a/content/browser/service_worker/service_worker_cache.h b/content/browser/service_worker/service_worker_cache.h
index d5ee7071e5cc07cb53c8cab493aca6c46d3a186b..874ae5e2b8f08f30fc65f6309f2696fe72313c2d 100644
--- a/content/browser/service_worker/service_worker_cache.h
+++ b/content/browser/service_worker/service_worker_cache.h
@@ -8,28 +8,48 @@
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
+#include "content/browser/service_worker/service_worker_cache.pb.h"
+#include "content/common/service_worker/service_worker_types.h"
+#include "net/base/completion_callback.h"
+#include "net/disk_cache/disk_cache.h"
namespace net {
class URLRequestContext;
+class IOBufferWithSize;
}
namespace webkit_blob {
+class BlobData;
+class BlobDataHandle;
class BlobStorageContext;
}
namespace content {
+class ChromeBlobStorageContext;
-// TODO(jkarlin): Fill this in with a real Cache implementation as
-// specified in
-// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
// TODO(jkarlin): Unload cache backend from memory once the cache object is no
// longer referenced in javascript.
// Represents a ServiceWorker Cache as seen in
// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
// InitializeIfNeeded must be called before calling the other public members.
-class ServiceWorkerCache {
+class CONTENT_EXPORT ServiceWorkerCache {
public:
+ enum ErrorType {
+ ErrorTypeOK = 0,
+ ErrorTypeExists,
+ ErrorTypeStorage,
+ ErrorTypeNotFound
+ };
+ enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY };
+ typedef base::Callback<void(ErrorType)> ErrorCallback;
+ typedef base::Callback<void(bool)> BoolCallback;
+ typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)>
+ EntryBoolCallback;
+ typedef base::Callback<void(ErrorType, scoped_ptr<ServiceWorkerResponse>)>
+ ResponseCallback;
+ class BlobReader;
+
static scoped_ptr<ServiceWorkerCache> CreateMemoryCache(
const std::string& name,
net::URLRequestContext* request_context,
@@ -45,7 +65,26 @@ class ServiceWorkerCache {
// 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 CreateBackend(const ErrorCallback& callback);
+
+ // Returns ErrorTypeNotFound if not found. A refptr to the BlobDataHandle is
+ // held
+ // by ServiceWorkerCache until DerefBlob() is called.
+ void Match(ServiceWorkerFetchRequest* request,
+ const ResponseCallback& callback);
+
+ // ServiceWorker Cache API
+ void Put(ServiceWorkerFetchRequest* request,
+ ServiceWorkerResponse* response,
+ const ErrorCallback& callback);
+
+ // Returns ErrorNotFound if not found. Otherwise deletes and returns
+ // ErrorTypeOK.
+ void Delete(ServiceWorkerFetchRequest* request,
+ const ErrorCallback& callback);
+
+ // Drops the reference to the blob's data handle.
+ void DerefBlob(const std::string& uuid);
void set_name(const std::string& name) { name_ = name; }
const std::string& name() const { return name_; }
@@ -55,18 +94,84 @@ class ServiceWorkerCache {
base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
private:
+ struct ResponseReadContext;
+
ServiceWorkerCache(
const base::FilePath& path,
const std::string& name,
net::URLRequestContext* request_context,
base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
+ // CreateBackend callbacks
+ void CreateBackendDidCreate(const ErrorCallback& callback, int rv);
+
+ // Put callbacks
+ void PutDidCreateEntry(
+ ServiceWorkerFetchRequest* request,
+ ServiceWorkerResponse* response,
+ const ErrorCallback& callback,
+ scoped_ptr<disk_cache::Entry*> entry,
+ scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle,
+ int error);
+ void PutDidWriteHeaders(
+ ServiceWorkerResponse* response,
+ const ErrorCallback& callback,
+ disk_cache::ScopedEntryPtr entry,
+ scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle,
+ int expected_bytes,
+ int rv);
+ void PutDidWriteBlobToCache(const ErrorCallback& callback,
+ scoped_ptr<BlobReader> blob_reader,
+ disk_cache::ScopedEntryPtr entry,
+ bool success);
+
+ // Match callbacks
+ void MatchDidOpenEntry(ServiceWorkerFetchRequest* request,
+ const ResponseCallback& callback,
+ scoped_ptr<disk_cache::Entry*> entryptr,
+ int rv);
+ void MatchDidReadHeaderData(
+ ServiceWorkerFetchRequest* request,
+ const ResponseCallback& callback,
+ disk_cache::ScopedEntryPtr entry,
+ const scoped_refptr<net::IOBufferWithSize>& buffer,
+ int rv);
+ void MatchDidReadResponseBodyData(
+ ServiceWorkerFetchRequest* request,
+ const ResponseCallback& callback,
+ disk_cache::ScopedEntryPtr entry,
+ scoped_ptr<ServiceWorkerResponse> response,
+ scoped_ptr<ResponseReadContext> response_context,
+ int rv);
+ void MatchDoneWithBody(ServiceWorkerFetchRequest* request,
+ const ResponseCallback& callback,
+ scoped_ptr<ServiceWorkerResponse> response,
+ scoped_ptr<ResponseReadContext> response_context);
+
+ // Delete callbacks
+ void DeleteDidOpenEntry(ServiceWorkerFetchRequest* request,
+ const ErrorCallback& callback,
+ scoped_ptr<disk_cache::Entry*> entryptr,
+ int rv);
+
+ void HoldBlobDataHandle(
+ const std::string& uuid,
+ scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle);
+ void DropBlobDataHandle(const std::string& uuid);
+
+ scoped_ptr<disk_cache::Backend> backend_;
base::FilePath path_;
std::string name_;
net::URLRequestContext* request_context_;
base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_;
int32 id_;
+ typedef std::map<std::string, webkit_blob::BlobDataHandle*> BlobDataHandleMap;
+
+ // Holds onto blob handles while waiting for acknowledgement of receipt from
+ // the service worker.
+ BlobDataHandleMap blob_data_handle_map_;
michaeln 2014/08/14 22:59:00 Can the ServiceWorkerCachebe deleted prior to rece
jkarlin 2014/08/15 11:49:44 Done. One would expect that deleting a cache while
+
base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache);

Powered by Google App Engine
This is Rietveld 408576698