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

Side by Side Diff: content/browser/cache_storage/cache_storage_listener.cc

Issue 1039763002: Cache Storage: Move files to content/*/cache_storage, rename classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: GN fix Created 5 years, 9 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 unified diff | Download patch
OLDNEW
(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 #include "content/browser/cache_storage/cache_storage_listener.h"
6
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/trace_event/trace_event.h"
10 #include "content/browser/cache_storage/cache_storage_cache.h"
11 #include "content/browser/cache_storage/cache_storage_context_impl.h"
12 #include "content/browser/cache_storage/cache_storage_manager.h"
13 #include "content/common/cache_storage/cache_storage_messages.h"
14 #include "content/common/cache_storage/cache_storage_types.h"
15 #include "storage/browser/blob/blob_data_handle.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h"
17
18 namespace content {
19
20 using blink::WebServiceWorkerCacheError;
21
22 namespace {
23
24 WebServiceWorkerCacheError ToWebServiceWorkerCacheError(
25 CacheStorage::CacheStorageError err) {
26 switch (err) {
27 case CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR:
28 NOTREACHED();
29 return blink::WebServiceWorkerCacheErrorNotImplemented;
30 case CacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED:
31 return blink::WebServiceWorkerCacheErrorNotImplemented;
32 case CacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND:
33 return blink::WebServiceWorkerCacheErrorNotFound;
34 case CacheStorage::CACHE_STORAGE_ERROR_EXISTS:
35 return blink::WebServiceWorkerCacheErrorExists;
36 case CacheStorage::CACHE_STORAGE_ERROR_STORAGE:
37 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's
38 // added.
39 return blink::WebServiceWorkerCacheErrorNotFound;
40 case CacheStorage::CACHE_STORAGE_ERROR_CLOSING:
41 // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_CLOSING once that's
42 // added.
43 return blink::WebServiceWorkerCacheErrorNotFound;
44 }
45 NOTREACHED();
46 return blink::WebServiceWorkerCacheErrorNotImplemented;
47 }
48
49 // TODO(jkarlin): CacheStorageCache and CacheStorage should share
50 // an error enum type.
51 WebServiceWorkerCacheError CacheErrorToWebServiceWorkerCacheError(
52 CacheStorageCache::ErrorType err) {
53 switch (err) {
54 case CacheStorageCache::ERROR_TYPE_OK:
55 NOTREACHED();
56 return blink::WebServiceWorkerCacheErrorNotImplemented;
57 case CacheStorageCache::ERROR_TYPE_EXISTS:
58 return blink::WebServiceWorkerCacheErrorExists;
59 case CacheStorageCache::ERROR_TYPE_STORAGE:
60 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's
61 // added.
62 return blink::WebServiceWorkerCacheErrorNotFound;
63 case CacheStorageCache::ERROR_TYPE_NOT_FOUND:
64 return blink::WebServiceWorkerCacheErrorNotFound;
65 }
66 NOTREACHED();
67 return blink::WebServiceWorkerCacheErrorNotImplemented;
68 }
69
70 } // namespace
71
72 CacheStorageListener::CacheStorageListener(
73 CacheStorageDispatcherHost* dispatcher,
74 CacheStorageContextImpl* context)
75 : dispatcher_(dispatcher), context_(context), weak_factory_(this) {
76 }
77
78 CacheStorageListener::~CacheStorageListener() {
79 }
80
81 bool CacheStorageListener::OnMessageReceived(const IPC::Message& message) {
82 bool handled = true;
83 IPC_BEGIN_MESSAGE_MAP(CacheStorageListener, message)
84 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageHas,
85 OnCacheStorageHas)
86 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageOpen,
87 OnCacheStorageOpen)
88 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageDelete,
89 OnCacheStorageDelete)
90 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageKeys,
91 OnCacheStorageKeys)
92 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheStorageMatch,
93 OnCacheStorageMatch)
94 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheMatch, OnCacheMatch)
95 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheMatchAll, OnCacheMatchAll)
96 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheKeys, OnCacheKeys)
97 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheBatch, OnCacheBatch)
98 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_CacheClosed, OnCacheClosed)
99 IPC_MESSAGE_HANDLER(CacheStorageHostMsg_BlobDataHandled,
100 OnBlobDataHandled)
101 IPC_MESSAGE_UNHANDLED(handled = false)
102 IPC_END_MESSAGE_MAP()
103
104 return handled;
105 }
106
107 void CacheStorageListener::OnCacheStorageHas(int thread_id,
108 int request_id,
109 const GURL& origin,
110 const base::string16& cache_name) {
111 TRACE_EVENT0("CacheStorage", "CacheStorageListener::OnCacheStorageHas");
112 context_->cache_manager()->HasCache(
113 origin, base::UTF16ToUTF8(cache_name),
114 base::Bind(&CacheStorageListener::OnCacheStorageHasCallback,
115 weak_factory_.GetWeakPtr(), thread_id, request_id));
116 }
117
118 void CacheStorageListener::OnCacheStorageOpen(
119 int thread_id,
120 int request_id,
121 const GURL& origin,
122 const base::string16& cache_name) {
123 TRACE_EVENT0("CacheStorage", "CacheStorageListener::OnCacheStorageOpen");
124 context_->cache_manager()->OpenCache(
125 origin, base::UTF16ToUTF8(cache_name),
126 base::Bind(&CacheStorageListener::OnCacheStorageOpenCallback,
127 weak_factory_.GetWeakPtr(), thread_id, request_id));
128 }
129
130 void CacheStorageListener::OnCacheStorageDelete(
131 int thread_id,
132 int request_id,
133 const GURL& origin,
134 const base::string16& cache_name) {
135 TRACE_EVENT0("CacheStorage", "CacheStorageListener::OnCacheStorageDelete");
136 context_->cache_manager()->DeleteCache(
137 origin, base::UTF16ToUTF8(cache_name),
138 base::Bind(&CacheStorageListener::OnCacheStorageDeleteCallback,
139 weak_factory_.GetWeakPtr(), thread_id, request_id));
140 }
141
142 void CacheStorageListener::OnCacheStorageKeys(int thread_id,
143 int request_id,
144 const GURL& origin) {
145 TRACE_EVENT0("CacheStorage", "CacheStorageListener::OnCacheStorageKeys");
146 context_->cache_manager()->EnumerateCaches(
147 origin, base::Bind(&CacheStorageListener::OnCacheStorageKeysCallback,
148 weak_factory_.GetWeakPtr(), thread_id, request_id));
149 }
150
151 void CacheStorageListener::OnCacheStorageMatch(
152 int thread_id,
153 int request_id,
154 const GURL& origin,
155 const ServiceWorkerFetchRequest& request,
156 const CacheStorageCacheQueryParams& match_params) {
157 TRACE_EVENT0("CacheStorage", "CacheStorageListener::OnCacheStorageMatch");
158
159 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
160 new ServiceWorkerFetchRequest(request.url, request.method,
161 request.headers, request.referrer,
162 request.is_reload));
163
164 if (match_params.cache_name.empty()) {
165 context_->cache_manager()->MatchAllCaches(
166 origin, scoped_request.Pass(),
167 base::Bind(&CacheStorageListener::OnCacheStorageMatchCallback,
168 weak_factory_.GetWeakPtr(), thread_id, request_id));
169 return;
170 }
171 context_->cache_manager()->MatchCache(
172 origin, base::UTF16ToUTF8(match_params.cache_name), scoped_request.Pass(),
173 base::Bind(&CacheStorageListener::OnCacheStorageMatchCallback,
174 weak_factory_.GetWeakPtr(), thread_id, request_id));
175 }
176
177 void CacheStorageListener::OnCacheMatch(
178 int thread_id,
179 int request_id,
180 int cache_id,
181 const ServiceWorkerFetchRequest& request,
182 const CacheStorageCacheQueryParams& match_params) {
183 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
184 if (it == id_to_cache_map_.end()) {
185 Send(new CacheStorageMsg_CacheMatchError(
186 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
187 return;
188 }
189
190 scoped_refptr<CacheStorageCache> cache = it->second;
191 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
192 new ServiceWorkerFetchRequest(request.url, request.method,
193 request.headers, request.referrer,
194 request.is_reload));
195 cache->Match(
196 scoped_request.Pass(),
197 base::Bind(&CacheStorageListener::OnCacheMatchCallback,
198 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
199 }
200
201 void CacheStorageListener::OnCacheMatchAll(
202 int thread_id,
203 int request_id,
204 int cache_id,
205 const ServiceWorkerFetchRequest& request,
206 const CacheStorageCacheQueryParams& match_params) {
207 // TODO(gavinp,jkarlin): Implement this method.
208 Send(new CacheStorageMsg_CacheMatchAllError(
209 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotImplemented));
210 }
211
212 void CacheStorageListener::OnCacheKeys(
213 int thread_id,
214 int request_id,
215 int cache_id,
216 const ServiceWorkerFetchRequest& request,
217 const CacheStorageCacheQueryParams& match_params) {
218 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
219 if (it == id_to_cache_map_.end()) {
220 Send(new CacheStorageMsg_CacheKeysError(
221 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
222 return;
223 }
224
225 scoped_refptr<CacheStorageCache> cache = it->second;
226
227 cache->Keys(base::Bind(&CacheStorageListener::OnCacheKeysCallback,
228 weak_factory_.GetWeakPtr(), thread_id, request_id,
229 cache));
230 }
231
232 void CacheStorageListener::OnCacheBatch(
233 int thread_id,
234 int request_id,
235 int cache_id,
236 const std::vector<CacheStorageBatchOperation>& operations) {
237 if (operations.size() != 1u) {
238 Send(new CacheStorageMsg_CacheBatchError(
239 thread_id, request_id,
240 blink::WebServiceWorkerCacheErrorNotImplemented));
241 return;
242 }
243
244 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
245 if (it == id_to_cache_map_.end()) {
246 Send(new CacheStorageMsg_CacheBatchError(
247 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
248 return;
249 }
250
251 const CacheStorageBatchOperation& operation = operations[0];
252
253 scoped_refptr<CacheStorageCache> cache = it->second;
254 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
255 new ServiceWorkerFetchRequest(
256 operation.request.url, operation.request.method,
257 operation.request.headers, operation.request.referrer,
258 operation.request.is_reload));
259
260 if (operation.operation_type == CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE) {
261 cache->Delete(
262 scoped_request.Pass(),
263 base::Bind(&CacheStorageListener::OnCacheDeleteCallback,
264 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
265 return;
266 }
267
268 if (operation.operation_type == CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT) {
269 // We don't support streaming for cache.
270 DCHECK(operation.response.stream_url.is_empty());
271 scoped_ptr<ServiceWorkerResponse> scoped_response(new ServiceWorkerResponse(
272 operation.response.url, operation.response.status_code,
273 operation.response.status_text, operation.response.response_type,
274 operation.response.headers, operation.response.blob_uuid,
275 operation.response.blob_size, operation.response.stream_url));
276 cache->Put(
277 scoped_request.Pass(), scoped_response.Pass(),
278 base::Bind(&CacheStorageListener::OnCachePutCallback,
279 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
280
281 return;
282 }
283
284 Send(new CacheStorageMsg_CacheBatchError(
285 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotImplemented));
286 }
287
288 void CacheStorageListener::OnCacheClosed(int cache_id) {
289 DropCacheReference(cache_id);
290 }
291
292 void CacheStorageListener::OnBlobDataHandled(const std::string& uuid) {
293 DropBlobDataHandle(uuid);
294 }
295
296 void CacheStorageListener::Send(IPC::Message* message) {
297 dispatcher_->Send(message);
298 }
299
300 void CacheStorageListener::OnCacheStorageHasCallback(
301 int thread_id,
302 int request_id,
303 bool has_cache,
304 CacheStorage::CacheStorageError error) {
305 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
306 Send(new CacheStorageMsg_CacheStorageHasError(
307 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
308 return;
309 }
310 if (!has_cache) {
311 Send(new CacheStorageMsg_CacheStorageHasError(
312 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
313 return;
314 }
315 Send(new CacheStorageMsg_CacheStorageHasSuccess(thread_id, request_id));
316 }
317
318 void CacheStorageListener::OnCacheStorageOpenCallback(
319 int thread_id,
320 int request_id,
321 const scoped_refptr<CacheStorageCache>& cache,
322 CacheStorage::CacheStorageError error) {
323 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
324 Send(new CacheStorageMsg_CacheStorageOpenError(
325 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
326 return;
327 }
328 CacheID cache_id = StoreCacheReference(cache);
329 Send(new CacheStorageMsg_CacheStorageOpenSuccess(thread_id, request_id,
330 cache_id));
331 }
332
333 void CacheStorageListener::OnCacheStorageDeleteCallback(
334 int thread_id,
335 int request_id,
336 bool deleted,
337 CacheStorage::CacheStorageError error) {
338 if (!deleted || error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
339 Send(new CacheStorageMsg_CacheStorageDeleteError(
340 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
341 return;
342 }
343 Send(new CacheStorageMsg_CacheStorageDeleteSuccess(thread_id, request_id));
344 }
345
346 void CacheStorageListener::OnCacheStorageKeysCallback(
347 int thread_id,
348 int request_id,
349 const std::vector<std::string>& strings,
350 CacheStorage::CacheStorageError error) {
351 if (error != CacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
352 Send(new CacheStorageMsg_CacheStorageKeysError(
353 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
354 return;
355 }
356
357 std::vector<base::string16> string16s;
358 for (size_t i = 0, max = strings.size(); i < max; ++i) {
359 string16s.push_back(base::UTF8ToUTF16(strings[i]));
360 }
361 Send(new CacheStorageMsg_CacheStorageKeysSuccess(thread_id, request_id,
362 string16s));
363 }
364
365 void CacheStorageListener::OnCacheStorageMatchCallback(
366 int thread_id,
367 int request_id,
368 CacheStorageCache::ErrorType error,
369 scoped_ptr<ServiceWorkerResponse> response,
370 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
371 if (error != CacheStorageCache::ERROR_TYPE_OK) {
372 Send(new CacheStorageMsg_CacheStorageMatchError(
373 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
374 return;
375 }
376
377 if (blob_data_handle)
378 StoreBlobDataHandle(blob_data_handle.Pass());
379
380 Send(new CacheStorageMsg_CacheStorageMatchSuccess(thread_id, request_id,
381 *response));
382 }
383
384 void CacheStorageListener::OnCacheMatchCallback(
385 int thread_id,
386 int request_id,
387 const scoped_refptr<CacheStorageCache>& cache,
388 CacheStorageCache::ErrorType error,
389 scoped_ptr<ServiceWorkerResponse> response,
390 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
391 if (error != CacheStorageCache::ERROR_TYPE_OK) {
392 Send(new CacheStorageMsg_CacheMatchError(
393 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
394 return;
395 }
396
397 if (blob_data_handle)
398 StoreBlobDataHandle(blob_data_handle.Pass());
399
400 Send(new CacheStorageMsg_CacheMatchSuccess(thread_id, request_id, *response));
401 }
402
403 void CacheStorageListener::OnCacheKeysCallback(
404 int thread_id,
405 int request_id,
406 const scoped_refptr<CacheStorageCache>& cache,
407 CacheStorageCache::ErrorType error,
408 scoped_ptr<CacheStorageCache::Requests> requests) {
409 if (error != CacheStorageCache::ERROR_TYPE_OK) {
410 Send(new CacheStorageMsg_CacheKeysError(
411 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
412 return;
413 }
414
415 CacheStorageCache::Requests out;
416
417 for (CacheStorageCache::Requests::const_iterator it = requests->begin();
418 it != requests->end(); ++it) {
419 ServiceWorkerFetchRequest request(it->url, it->method, it->headers,
420 it->referrer, it->is_reload);
421 out.push_back(request);
422 }
423
424 Send(new CacheStorageMsg_CacheKeysSuccess(thread_id, request_id, out));
425 }
426
427 void CacheStorageListener::OnCacheDeleteCallback(
428 int thread_id,
429 int request_id,
430 const scoped_refptr<CacheStorageCache>& cache,
431 CacheStorageCache::ErrorType error) {
432 if (error != CacheStorageCache::ERROR_TYPE_OK) {
433 Send(new CacheStorageMsg_CacheBatchError(
434 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
435 return;
436 }
437
438 Send(new CacheStorageMsg_CacheBatchSuccess(
439 thread_id, request_id, std::vector<ServiceWorkerResponse>()));
440 }
441
442 void CacheStorageListener::OnCachePutCallback(
443 int thread_id,
444 int request_id,
445 const scoped_refptr<CacheStorageCache>& cache,
446 CacheStorageCache::ErrorType error,
447 scoped_ptr<ServiceWorkerResponse> response,
448 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
449 if (error != CacheStorageCache::ERROR_TYPE_OK) {
450 Send(new CacheStorageMsg_CacheBatchError(
451 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
452 return;
453 }
454
455 if (blob_data_handle)
456 StoreBlobDataHandle(blob_data_handle.Pass());
457
458 std::vector<ServiceWorkerResponse> responses;
459 responses.push_back(*response);
460 Send(new CacheStorageMsg_CacheBatchSuccess(thread_id, request_id, responses));
461 }
462
463 CacheStorageListener::CacheID CacheStorageListener::StoreCacheReference(
464 const scoped_refptr<CacheStorageCache>& cache) {
465 int cache_id = next_cache_id_++;
466 id_to_cache_map_[cache_id] = cache;
467 return cache_id;
468 }
469
470 void CacheStorageListener::DropCacheReference(CacheID cache_id) {
471 id_to_cache_map_.erase(cache_id);
472 }
473
474 void CacheStorageListener::StoreBlobDataHandle(
475 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
476 DCHECK(blob_data_handle);
477 std::pair<UUIDToBlobDataHandleList::iterator, bool> rv =
478 blob_handle_store_.insert(std::make_pair(
479 blob_data_handle->uuid(), std::list<storage::BlobDataHandle>()));
480 rv.first->second.push_front(storage::BlobDataHandle(*blob_data_handle));
481 }
482
483 void CacheStorageListener::DropBlobDataHandle(std::string uuid) {
484 UUIDToBlobDataHandleList::iterator it = blob_handle_store_.find(uuid);
485 if (it == blob_handle_store_.end())
486 return;
487 DCHECK(!it->second.empty());
488 it->second.pop_front();
489 if (it->second.empty())
490 blob_handle_store_.erase(it);
491 }
492
493 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_listener.h ('k') | content/browser/cache_storage/cache_storage_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698