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 #include "net/disk_cache/disk_cache.h" | |
11 | 15 |
12 namespace net { | 16 namespace net { |
13 class URLRequestContext; | 17 class URLRequestContext; |
18 class IOBufferWithSize; | |
14 } | 19 } |
15 | 20 |
16 namespace webkit_blob { | 21 namespace webkit_blob { |
22 class BlobData; | |
23 class BlobDataHandle; | |
17 class BlobStorageContext; | 24 class BlobStorageContext; |
18 } | 25 } |
19 | 26 |
20 namespace content { | 27 namespace content { |
28 class ChromeBlobStorageContext; | |
21 | 29 |
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 | 30 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
26 // longer referenced in javascript. | 31 // longer referenced in javascript. |
27 | 32 |
28 // Represents a ServiceWorker Cache as seen in | 33 // Represents a ServiceWorker Cache as seen in |
29 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 34 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
30 // InitializeIfNeeded must be called before calling the other public members. | 35 // InitializeIfNeeded must be called before calling the other public members. |
31 class ServiceWorkerCache { | 36 class CONTENT_EXPORT ServiceWorkerCache { |
32 public: | 37 public: |
38 enum ErrorType { | |
39 ErrorTypeOK = 0, | |
40 ErrorTypeExists, | |
41 ErrorTypeStorage, | |
42 ErrorTypeNotFound | |
43 }; | |
44 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | |
45 typedef base::Callback<void(ErrorType)> ErrorCallback; | |
46 typedef base::Callback<void(bool)> BoolCallback; | |
47 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | |
48 EntryBoolCallback; | |
49 typedef base::Callback<void(ErrorType, scoped_ptr<ServiceWorkerResponse>)> | |
50 ResponseCallback; | |
51 class BlobReader; | |
52 | |
33 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 53 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
34 const std::string& name, | 54 const std::string& name, |
35 net::URLRequestContext* request_context, | 55 net::URLRequestContext* request_context, |
36 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 56 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
37 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 57 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
38 const base::FilePath& path, | 58 const base::FilePath& path, |
39 const std::string& name, | 59 const std::string& name, |
40 net::URLRequestContext* request_context, | 60 net::URLRequestContext* request_context, |
41 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 61 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
42 | 62 |
43 virtual ~ServiceWorkerCache(); | 63 virtual ~ServiceWorkerCache(); |
44 | 64 |
45 // Loads the backend and calls the callback with the result (true for | 65 // Loads the backend and calls the callback with the result (true for |
46 // success). This must be called before member functions that require a | 66 // success). This must be called before member functions that require a |
47 // backend are called. | 67 // backend are called. |
48 void CreateBackend(const base::Callback<void(bool)>& callback); | 68 void CreateBackend(const ErrorCallback& callback); |
69 | |
70 // Returns ErrorTypeNotFound if not found. A refptr to the BlobDataHandle is | |
71 // held | |
72 // by ServiceWorkerCache until DerefBlob() is called. | |
73 void Match(ServiceWorkerFetchRequest* request, | |
74 const ResponseCallback& callback); | |
75 | |
76 // ServiceWorker Cache API | |
77 void Put(ServiceWorkerFetchRequest* request, | |
78 ServiceWorkerResponse* response, | |
79 const ErrorCallback& callback); | |
80 | |
81 // Returns ErrorNotFound if not found. Otherwise deletes and returns | |
82 // ErrorTypeOK. | |
83 void Delete(ServiceWorkerFetchRequest* request, | |
84 const ErrorCallback& callback); | |
85 | |
86 // Drops the reference to the blob's data handle. | |
87 void DerefBlob(const std::string& uuid); | |
49 | 88 |
50 void set_name(const std::string& name) { name_ = name; } | 89 void set_name(const std::string& name) { name_ = name; } |
51 const std::string& name() const { return name_; } | 90 const std::string& name() const { return name_; } |
52 int32 id() const { return id_; } | 91 int32 id() const { return id_; } |
53 void set_id(int32 id) { id_ = id; } | 92 void set_id(int32 id) { id_ = id; } |
54 | 93 |
55 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 94 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
56 | 95 |
57 private: | 96 private: |
97 struct ResponseReadContext; | |
98 | |
58 ServiceWorkerCache( | 99 ServiceWorkerCache( |
59 const base::FilePath& path, | 100 const base::FilePath& path, |
60 const std::string& name, | 101 const std::string& name, |
61 net::URLRequestContext* request_context, | 102 net::URLRequestContext* request_context, |
62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 103 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
63 | 104 |
105 // CreateBackend callbacks | |
106 void CreateBackendDidCreate(const ErrorCallback& callback, int rv); | |
107 | |
108 // Put callbacks | |
109 void PutDidCreateEntry( | |
110 ServiceWorkerFetchRequest* request, | |
111 ServiceWorkerResponse* response, | |
112 const ErrorCallback& callback, | |
113 scoped_ptr<disk_cache::Entry*> entry, | |
114 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle, | |
115 int error); | |
116 void PutDidWriteHeaders( | |
117 ServiceWorkerResponse* response, | |
118 const ErrorCallback& callback, | |
119 disk_cache::ScopedEntryPtr entry, | |
120 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle, | |
121 int expected_bytes, | |
122 int rv); | |
123 void PutDidWriteBlobToCache(const ErrorCallback& callback, | |
124 scoped_ptr<BlobReader> blob_reader, | |
125 disk_cache::ScopedEntryPtr entry, | |
126 bool success); | |
127 | |
128 // Match callbacks | |
129 void MatchDidOpenEntry(ServiceWorkerFetchRequest* request, | |
130 const ResponseCallback& callback, | |
131 scoped_ptr<disk_cache::Entry*> entryptr, | |
132 int rv); | |
133 void MatchDidReadHeaderData( | |
134 ServiceWorkerFetchRequest* request, | |
135 const ResponseCallback& callback, | |
136 disk_cache::ScopedEntryPtr entry, | |
137 const scoped_refptr<net::IOBufferWithSize>& buffer, | |
138 int rv); | |
139 void MatchDidReadResponseBodyData( | |
140 ServiceWorkerFetchRequest* request, | |
141 const ResponseCallback& callback, | |
142 disk_cache::ScopedEntryPtr entry, | |
143 scoped_ptr<ServiceWorkerResponse> response, | |
144 scoped_ptr<ResponseReadContext> response_context, | |
145 int rv); | |
146 void MatchDoneWithBody(ServiceWorkerFetchRequest* request, | |
147 const ResponseCallback& callback, | |
148 scoped_ptr<ServiceWorkerResponse> response, | |
149 scoped_ptr<ResponseReadContext> response_context); | |
150 | |
151 // Delete callbacks | |
152 void DeleteDidOpenEntry(ServiceWorkerFetchRequest* request, | |
153 const ErrorCallback& callback, | |
154 scoped_ptr<disk_cache::Entry*> entryptr, | |
155 int rv); | |
156 | |
157 void HoldBlobDataHandle( | |
158 const std::string& uuid, | |
159 scoped_ptr<webkit_blob::BlobDataHandle> blob_data_handle); | |
160 void DropBlobDataHandle(const std::string& uuid); | |
161 | |
162 scoped_ptr<disk_cache::Backend> backend_; | |
64 base::FilePath path_; | 163 base::FilePath path_; |
65 std::string name_; | 164 std::string name_; |
66 net::URLRequestContext* request_context_; | 165 net::URLRequestContext* request_context_; |
67 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; | 166 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; |
68 int32 id_; | 167 int32 id_; |
69 | 168 |
169 typedef std::map<std::string, webkit_blob::BlobDataHandle*> BlobDataHandleMap; | |
170 | |
171 // Holds onto blob handles while waiting for acknowledgement of receipt from | |
172 // the service worker. | |
173 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
| |
174 | |
70 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 175 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
71 | 176 |
72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 177 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
73 }; | 178 }; |
74 | 179 |
75 } // namespace content | 180 } // namespace content |
76 | 181 |
77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 182 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
OLD | NEW |