| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "content/common/service_worker/service_worker_types.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 #include "net/disk_cache/disk_cache.h" | |
| 17 | |
| 18 namespace net { | |
| 19 class URLRequestContext; | |
| 20 class IOBufferWithSize; | |
| 21 } | |
| 22 | |
| 23 namespace storage { | |
| 24 class BlobDataBuilder; | |
| 25 class BlobDataHandle; | |
| 26 class BlobStorageContext; | |
| 27 class QuotaManagerProxy; | |
| 28 } | |
| 29 | |
| 30 namespace content { | |
| 31 class ChromeBlobStorageContext; | |
| 32 class ServiceWorkerCacheMetadata; | |
| 33 class ServiceWorkerCacheScheduler; | |
| 34 class TestServiceWorkerCache; | |
| 35 | |
| 36 // Represents a ServiceWorker Cache as seen in | |
| 37 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | |
| 38 // The asynchronous methods are executed serially. Callbacks to the | |
| 39 // public functions will be called so long as the cache object lives. | |
| 40 class CONTENT_EXPORT ServiceWorkerCache | |
| 41 : public base::RefCounted<ServiceWorkerCache> { | |
| 42 public: | |
| 43 // This enum is used in histograms, so do not change the ordering and always | |
| 44 // append new types to the end. | |
| 45 enum ErrorType { | |
| 46 ERROR_TYPE_OK = 0, | |
| 47 ERROR_TYPE_EXISTS, | |
| 48 ERROR_TYPE_STORAGE, | |
| 49 ERROR_TYPE_NOT_FOUND, | |
| 50 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND | |
| 51 }; | |
| 52 | |
| 53 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | |
| 54 typedef base::Callback<void(ErrorType)> ErrorCallback; | |
| 55 typedef base::Callback<void(ErrorType, | |
| 56 scoped_ptr<ServiceWorkerResponse>, | |
| 57 scoped_ptr<storage::BlobDataHandle>)> | |
| 58 ResponseCallback; | |
| 59 typedef std::vector<ServiceWorkerFetchRequest> Requests; | |
| 60 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> | |
| 61 RequestsCallback; | |
| 62 | |
| 63 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache( | |
| 64 const GURL& origin, | |
| 65 net::URLRequestContext* request_context, | |
| 66 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | |
| 67 base::WeakPtr<storage::BlobStorageContext> blob_context); | |
| 68 static scoped_refptr<ServiceWorkerCache> CreatePersistentCache( | |
| 69 const GURL& origin, | |
| 70 const base::FilePath& path, | |
| 71 net::URLRequestContext* request_context, | |
| 72 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | |
| 73 base::WeakPtr<storage::BlobStorageContext> blob_context); | |
| 74 | |
| 75 // Returns ERROR_TYPE_NOT_FOUND if not found. | |
| 76 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 77 const ResponseCallback& callback); | |
| 78 | |
| 79 // Puts the request and response object in the cache. The response body (if | |
| 80 // present) is stored in the cache, but not the request body. Returns | |
| 81 // ERROR_TYPE_OK on success. | |
| 82 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 83 scoped_ptr<ServiceWorkerResponse> response, | |
| 84 const ResponseCallback& callback); | |
| 85 | |
| 86 // Returns ErrorNotFound if not found. Otherwise deletes and returns | |
| 87 // ERROR_TYPE_OK. | |
| 88 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 89 const ErrorCallback& callback); | |
| 90 | |
| 91 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | |
| 92 // Returns ErrorTypeOK and a vector of requests if there are no errors. | |
| 93 void Keys(const RequestsCallback& callback); | |
| 94 | |
| 95 // Closes the backend. Future operations that require the backend | |
| 96 // will exit early. Close should only be called once per ServiceWorkerCache. | |
| 97 void Close(const base::Closure& callback); | |
| 98 | |
| 99 // The size of the cache contents in memory. Returns 0 if the cache backend is | |
| 100 // not a memory cache backend. | |
| 101 int64 MemoryBackedSize() const; | |
| 102 | |
| 103 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | |
| 104 | |
| 105 private: | |
| 106 friend class base::RefCounted<ServiceWorkerCache>; | |
| 107 friend class TestServiceWorkerCache; | |
| 108 | |
| 109 class BlobReader; | |
| 110 struct KeysContext; | |
| 111 struct MatchContext; | |
| 112 struct PutContext; | |
| 113 | |
| 114 // The backend progresses from uninitialized, to open, to closed, and cannot | |
| 115 // reverse direction. The open step may be skipped. | |
| 116 enum BackendState { | |
| 117 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. | |
| 118 BACKEND_OPEN, // Backend can be used. | |
| 119 BACKEND_CLOSED // Backend cannot be used. All ops should fail. | |
| 120 }; | |
| 121 | |
| 122 typedef std::vector<disk_cache::Entry*> Entries; | |
| 123 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; | |
| 124 | |
| 125 ServiceWorkerCache( | |
| 126 const GURL& origin, | |
| 127 const base::FilePath& path, | |
| 128 net::URLRequestContext* request_context, | |
| 129 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | |
| 130 base::WeakPtr<storage::BlobStorageContext> blob_context); | |
| 131 | |
| 132 // Async operations in progress will cancel and not run their callbacks. | |
| 133 virtual ~ServiceWorkerCache(); | |
| 134 | |
| 135 // Match callbacks | |
| 136 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 137 const ResponseCallback& callback); | |
| 138 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); | |
| 139 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, | |
| 140 scoped_ptr<ServiceWorkerCacheMetadata> headers); | |
| 141 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, | |
| 142 int rv); | |
| 143 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); | |
| 144 | |
| 145 // Put callbacks. | |
| 146 void PutImpl(scoped_ptr<PutContext> put_context); | |
| 147 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); | |
| 148 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); | |
| 149 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | |
| 150 int expected_bytes, | |
| 151 int rv); | |
| 152 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | |
| 153 scoped_ptr<BlobReader> blob_reader, | |
| 154 disk_cache::ScopedEntryPtr entry, | |
| 155 bool success); | |
| 156 | |
| 157 // Delete callbacks | |
| 158 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 159 const ErrorCallback& callback); | |
| 160 void DeleteDidOpenEntry( | |
| 161 const GURL& origin, | |
| 162 scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 163 const ServiceWorkerCache::ErrorCallback& callback, | |
| 164 scoped_ptr<disk_cache::Entry*> entryptr, | |
| 165 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | |
| 166 int rv); | |
| 167 | |
| 168 // Keys callbacks. | |
| 169 void KeysImpl(const RequestsCallback& callback); | |
| 170 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); | |
| 171 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | |
| 172 const Entries::iterator& iter); | |
| 173 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, | |
| 174 const Entries::iterator& iter, | |
| 175 scoped_ptr<ServiceWorkerCacheMetadata> metadata); | |
| 176 | |
| 177 void CloseImpl(const base::Closure& callback); | |
| 178 | |
| 179 // Loads the backend and calls the callback with the result (true for | |
| 180 // success). The callback will always be called. Virtual for tests. | |
| 181 virtual void CreateBackend(const ErrorCallback& callback); | |
| 182 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, | |
| 183 scoped_ptr<ScopedBackendPtr> backend_ptr, | |
| 184 int rv); | |
| 185 | |
| 186 void InitBackend(); | |
| 187 void InitDone(ErrorType error); | |
| 188 | |
| 189 void PendingClosure(const base::Closure& callback); | |
| 190 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | |
| 191 void PendingResponseCallback( | |
| 192 const ResponseCallback& callback, | |
| 193 ErrorType error, | |
| 194 scoped_ptr<ServiceWorkerResponse> response, | |
| 195 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | |
| 196 void PendingRequestsCallback(const RequestsCallback& callback, | |
| 197 ErrorType error, | |
| 198 scoped_ptr<Requests> requests); | |
| 199 | |
| 200 // Be sure to check |backend_state_| before use. | |
| 201 scoped_ptr<disk_cache::Backend> backend_; | |
| 202 | |
| 203 GURL origin_; | |
| 204 base::FilePath path_; | |
| 205 net::URLRequestContext* request_context_; | |
| 206 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | |
| 207 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | |
| 208 BackendState backend_state_; | |
| 209 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_; | |
| 210 bool initializing_; | |
| 211 | |
| 212 // Whether or not to store data in disk or memory. | |
| 213 bool memory_only_; | |
| 214 | |
| 215 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | |
| 216 | |
| 217 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | |
| 218 }; | |
| 219 | |
| 220 } // namespace content | |
| 221 | |
| 222 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | |
| OLD | NEW |