| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "content/browser/service_worker/service_worker_cache.pb.h" |
| 12 #include "content/common/service_worker/service_worker_types.h" |
| 13 #include "net/base/completion_callback.h" |
| 14 |
| 15 namespace disk_cache { |
| 16 class Backend; |
| 17 class Entry; |
| 18 } |
| 11 | 19 |
| 12 namespace net { | 20 namespace net { |
| 13 class URLRequestContext; | 21 class URLRequestContext; |
| 22 class IOBufferWithSize; |
| 14 } | 23 } |
| 15 | 24 |
| 16 namespace webkit_blob { | 25 namespace webkit_blob { |
| 26 class BlobData; |
| 27 class BlobDataHandle; |
| 17 class BlobStorageContext; | 28 class BlobStorageContext; |
| 18 } | 29 } |
| 19 | 30 |
| 20 namespace content { | 31 namespace content { |
| 32 class ChromeBlobStorageContext; |
| 21 | 33 |
| 22 // TODO(jkarlin): Fill this in with a real Cache implementation as | |
| 23 // specified in | |
| 24 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | |
| 25 // TODO(jkarlin): Unload cache backend from memory once the cache object is no | 34 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
| 26 // longer referenced in javascript. | 35 // longer referenced in javascript. |
| 27 | 36 |
| 28 // Represents a ServiceWorker Cache as seen in | 37 // Represents a ServiceWorker Cache as seen in |
| 29 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 38 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
| 30 // InitializeIfNeeded must be called before calling the other public members. | 39 // InitializeIfNeeded must be called before calling the other public members. |
| 31 class ServiceWorkerCache { | 40 class CONTENT_EXPORT ServiceWorkerCache { |
| 32 public: | 41 public: |
| 42 enum ErrorType { |
| 43 ErrorTypeOK = 0, |
| 44 ErrorTypeExists, |
| 45 ErrorTypeStorage, |
| 46 ErrorNotFound |
| 47 }; |
| 48 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 49 typedef base::Callback<void(ErrorType)> ErrorCallback; |
| 50 typedef base::Callback<void(bool)> BoolCallback; |
| 51 typedef base::Callback<void(ErrorType, |
| 52 scoped_ptr<ServiceWorkerResponse>, |
| 53 scoped_ptr<webkit_blob::BlobDataHandle>)> |
| 54 ResponseCallback; |
| 55 class BlobReader; |
| 56 |
| 33 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 57 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
| 34 const std::string& name, | 58 const std::string& name, |
| 35 net::URLRequestContext* request_context, | 59 net::URLRequestContext* request_context, |
| 36 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 60 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
| 37 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 61 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
| 38 const base::FilePath& path, | 62 const base::FilePath& path, |
| 39 const std::string& name, | 63 const std::string& name, |
| 40 net::URLRequestContext* request_context, | 64 net::URLRequestContext* request_context, |
| 41 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 65 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
| 42 | 66 |
| 43 virtual ~ServiceWorkerCache(); | 67 virtual ~ServiceWorkerCache(); |
| 44 | 68 |
| 45 // Loads the backend and calls the callback with the result (true for | 69 // Loads the backend and calls the callback with the result (true for |
| 46 // success). This must be called before member functions that require a | 70 // success). This must be called before member functions that require a |
| 47 // backend are called. | 71 // backend are called. |
| 48 void CreateBackend(const base::Callback<void(bool)>& callback); | 72 void CreateBackend(const ErrorCallback& callback); |
| 73 |
| 74 // ServiceWorker Cache API |
| 75 void Put(ServiceWorkerFetchRequest* request, |
| 76 ServiceWorkerResponse* response, |
| 77 const ErrorCallback& callback); |
| 78 |
| 79 // Returns ErrorNotFound if not found. It is the responsibility of the caller |
| 80 // to call RemoveBlob() once the returned blob uuid is no longer needed. |
| 81 void Match(ServiceWorkerFetchRequest* request, |
| 82 const ResponseCallback& callback); |
| 49 | 83 |
| 50 void set_name(const std::string& name) { name_ = name; } | 84 void set_name(const std::string& name) { name_ = name; } |
| 51 const std::string& name() const { return name_; } | 85 const std::string& name() const { return name_; } |
| 52 int32 id() const { return id_; } | 86 int32 id() const { return id_; } |
| 53 void set_id(int32 id) { id_ = id; } | 87 void set_id(int32 id) { id_ = id; } |
| 54 | 88 |
| 55 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 89 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
| 56 | 90 |
| 57 private: | 91 private: |
| 92 struct ResponseReadContext; |
| 93 |
| 58 ServiceWorkerCache( | 94 ServiceWorkerCache( |
| 59 const base::FilePath& path, | 95 const base::FilePath& path, |
| 60 const std::string& name, | 96 const std::string& name, |
| 61 net::URLRequestContext* request_context, | 97 net::URLRequestContext* request_context, |
| 62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 98 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
| 63 | 99 |
| 100 // CreateBackend callbacks |
| 101 void CreateBackendDidCreate(const ErrorCallback& callback, int rv); |
| 102 |
| 103 // Put callbacks |
| 104 void PutDidCreateEntry(ServiceWorkerFetchRequest* request, |
| 105 ServiceWorkerResponse* response, |
| 106 const ErrorCallback& callback, |
| 107 scoped_ptr<disk_cache::Entry*> entry, |
| 108 int error); |
| 109 void PutDidWriteHeaders(ServiceWorkerFetchRequest* request, |
| 110 ServiceWorkerResponse* response, |
| 111 const ErrorCallback& callback, |
| 112 disk_cache::Entry* entry, |
| 113 int expected_bytes, |
| 114 int rv); |
| 115 void PutDidWriteBlobToCache(const ErrorCallback& callback, |
| 116 disk_cache::Entry* entry, |
| 117 scoped_ptr<BlobReader> blob_reader, |
| 118 bool success); |
| 119 |
| 120 // Match callbacks |
| 121 void MatchDidOpenEntry(ServiceWorkerFetchRequest* request, |
| 122 const ResponseCallback& callback, |
| 123 scoped_ptr<disk_cache::Entry*> entryptr, |
| 124 int rv); |
| 125 void MatchDidReadHeaderData( |
| 126 ServiceWorkerFetchRequest* request, |
| 127 const ResponseCallback& callback, |
| 128 disk_cache::Entry* entry, |
| 129 const scoped_refptr<net::IOBufferWithSize>& buffer, |
| 130 int rv); |
| 131 void MatchDidReadResponseBodyData( |
| 132 ServiceWorkerFetchRequest* request, |
| 133 const ResponseCallback& callback, |
| 134 disk_cache::Entry* entry, |
| 135 scoped_ptr<ServiceWorkerResponse> response, |
| 136 scoped_ptr<ResponseReadContext> response_context, |
| 137 int rv); |
| 138 void MatchDoneWithBody(ServiceWorkerFetchRequest* request, |
| 139 const ResponseCallback& callback, |
| 140 scoped_ptr<ServiceWorkerResponse> response, |
| 141 scoped_ptr<ResponseReadContext> response_context); |
| 142 |
| 143 scoped_ptr<disk_cache::Backend> backend_; |
| 64 base::FilePath path_; | 144 base::FilePath path_; |
| 65 std::string name_; | 145 std::string name_; |
| 66 net::URLRequestContext* request_context_; | 146 net::URLRequestContext* request_context_; |
| 67 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; | 147 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; |
| 68 int32 id_; | 148 int32 id_; |
| 69 | 149 |
| 70 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 150 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 71 | 151 |
| 72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 152 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 73 }; | 153 }; |
| 74 | 154 |
| 75 } // namespace content | 155 } // namespace content |
| 76 | 156 |
| 77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 157 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |