| 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/common/service_worker/service_worker_types.h" | 11 #include "content/common/service_worker/service_worker_types.h" |
| 12 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
| 13 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 class URLRequestContext; | 16 class URLRequestContext; |
| 17 class IOBufferWithSize; | 17 class IOBufferWithSize; |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace storage { | 20 namespace storage { |
| 21 class BlobData; | 21 class BlobData; |
| 22 class BlobDataHandle; | 22 class BlobDataHandle; |
| 23 class BlobStorageContext; | 23 class BlobStorageContext; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 class ChromeBlobStorageContext; | 27 class ChromeBlobStorageContext; |
| 28 class ServiceWorkerRequestResponseHeaders; | |
| 29 | 28 |
| 30 // TODO(jkarlin): Unload cache backend from memory once the cache object is no | 29 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
| 31 // longer referenced in javascript. | 30 // longer referenced in javascript. |
| 32 | 31 |
| 33 // Represents a ServiceWorker Cache as seen in | 32 // Represents a ServiceWorker Cache as seen in |
| 34 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 33 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
| 35 // InitializeIfNeeded must be called before calling the other public members. | 34 // InitializeIfNeeded must be called before calling the other public members. |
| 36 class CONTENT_EXPORT ServiceWorkerCache { | 35 class CONTENT_EXPORT ServiceWorkerCache { |
| 37 public: | 36 public: |
| 38 enum ErrorType { | 37 enum ErrorType { |
| 39 ErrorTypeOK = 0, | 38 ErrorTypeOK = 0, |
| 40 ErrorTypeExists, | 39 ErrorTypeExists, |
| 41 ErrorTypeStorage, | 40 ErrorTypeStorage, |
| 42 ErrorTypeNotFound | 41 ErrorTypeNotFound |
| 43 }; | 42 }; |
| 44 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 43 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 45 typedef base::Callback<void(ErrorType)> ErrorCallback; | 44 typedef base::Callback<void(ErrorType)> ErrorCallback; |
| 46 typedef base::Callback<void(ErrorType, | 45 typedef base::Callback<void(ErrorType, |
| 47 scoped_ptr<ServiceWorkerResponse>, | 46 scoped_ptr<ServiceWorkerResponse>, |
| 48 scoped_ptr<storage::BlobDataHandle>)> | 47 scoped_ptr<storage::BlobDataHandle>)> |
| 49 ResponseCallback; | 48 ResponseCallback; |
| 50 typedef std::vector<ServiceWorkerFetchRequest> Requests; | |
| 51 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> | |
| 52 RequestsCallback; | |
| 53 | |
| 54 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 49 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
| 55 net::URLRequestContext* request_context, | 50 net::URLRequestContext* request_context, |
| 56 base::WeakPtr<storage::BlobStorageContext> blob_context); | 51 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 57 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 52 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
| 58 const base::FilePath& path, | 53 const base::FilePath& path, |
| 59 net::URLRequestContext* request_context, | 54 net::URLRequestContext* request_context, |
| 60 base::WeakPtr<storage::BlobStorageContext> blob_context); | 55 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 61 | 56 |
| 62 virtual ~ServiceWorkerCache(); | 57 virtual ~ServiceWorkerCache(); |
| 63 | 58 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 void Put(ServiceWorkerFetchRequest* request, | 73 void Put(ServiceWorkerFetchRequest* request, |
| 79 ServiceWorkerResponse* response, | 74 ServiceWorkerResponse* response, |
| 80 const ErrorCallback& callback); | 75 const ErrorCallback& callback); |
| 81 | 76 |
| 82 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 77 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
| 83 // ErrorTypeOK. The callback will always be called. |request| must remain | 78 // ErrorTypeOK. The callback will always be called. |request| must remain |
| 84 // valid until the callback is called. | 79 // valid until the callback is called. |
| 85 void Delete(ServiceWorkerFetchRequest* request, | 80 void Delete(ServiceWorkerFetchRequest* request, |
| 86 const ErrorCallback& callback); | 81 const ErrorCallback& callback); |
| 87 | 82 |
| 88 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | |
| 89 // Returns ErrorTypeOK and a vector of requests if there are no errors. The | |
| 90 // callback will always be called. | |
| 91 void Keys(const RequestsCallback& callback); | |
| 92 | |
| 93 // Call to determine if CreateBackend must be called. | 83 // Call to determine if CreateBackend must be called. |
| 94 bool HasCreatedBackend() const; | 84 bool HasCreatedBackend() const; |
| 95 | 85 |
| 96 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | 86 void set_backend(scoped_ptr<disk_cache::Backend> backend) { |
| 97 backend_ = backend.Pass(); | 87 backend_ = backend.Pass(); |
| 98 } | 88 } |
| 99 | 89 |
| 100 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 90 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
| 101 | 91 |
| 102 private: | 92 private: |
| 103 struct KeysContext; | |
| 104 typedef std::vector<disk_cache::Entry*> Entries; | |
| 105 | |
| 106 ServiceWorkerCache(const base::FilePath& path, | 93 ServiceWorkerCache(const base::FilePath& path, |
| 107 net::URLRequestContext* request_context, | 94 net::URLRequestContext* request_context, |
| 108 base::WeakPtr<storage::BlobStorageContext> blob_context); | 95 base::WeakPtr<storage::BlobStorageContext> blob_context); |
| 109 | 96 |
| 110 // Static callbacks for the Keys function. | |
| 111 static void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, | |
| 112 int rv); | |
| 113 static void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | |
| 114 const Entries::iterator& iter); | |
| 115 static void KeysDidReadHeaders( | |
| 116 scoped_ptr<KeysContext> keys_context, | |
| 117 const Entries::iterator& iter, | |
| 118 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); | |
| 119 | |
| 120 scoped_ptr<disk_cache::Backend> backend_; | 97 scoped_ptr<disk_cache::Backend> backend_; |
| 121 base::FilePath path_; | 98 base::FilePath path_; |
| 122 net::URLRequestContext* request_context_; | 99 net::URLRequestContext* request_context_; |
| 123 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 100 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
| 124 | 101 |
| 125 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 102 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
| 126 | 103 |
| 127 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 104 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
| 128 }; | 105 }; |
| 129 | 106 |
| 130 } // namespace content | 107 } // namespace content |
| 131 | 108 |
| 132 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 109 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
| OLD | NEW |