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 #include "modules/cachestorage/InspectorCacheStorageAgent.h" | 5 #include "modules/cachestorage/InspectorCacheStorageAgent.h" |
6 | 6 |
7 #include "platform/heap/Handle.h" | 7 #include "platform/heap/Handle.h" |
8 #include "platform/inspector_protocol/DispatcherBase.h" | 8 #include "platform/inspector_protocol/DispatcherBase.h" |
9 #include "platform/inspector_protocol/Values.h" | 9 #include "platform/inspector_protocol/Values.h" |
10 #include "platform/weborigin/KURL.h" | 10 #include "platform/weborigin/KURL.h" |
11 #include "platform/weborigin/SecurityOrigin.h" | 11 #include "platform/weborigin/SecurityOrigin.h" |
12 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
13 #include "public/platform/WebSecurityOrigin.h" | 13 #include "public/platform/WebSecurityOrigin.h" |
14 #include "public/platform/WebString.h" | 14 #include "public/platform/WebString.h" |
15 #include "public/platform/WebURL.h" | 15 #include "public/platform/WebURL.h" |
16 #include "public/platform/WebVector.h" | 16 #include "public/platform/WebVector.h" |
17 #include "public/platform/modules/serviceworker/WebServiceWorkerCache.h" | 17 #include "public/platform/modules/serviceworker/WebServiceWorkerCache.h" |
18 #include "public/platform/modules/serviceworker/WebServiceWorkerCacheError.h" | 18 #include "public/platform/modules/serviceworker/WebServiceWorkerCacheError.h" |
19 #include "public/platform/modules/serviceworker/WebServiceWorkerCacheStorage.h" | 19 #include "public/platform/modules/serviceworker/WebServiceWorkerCacheStorage.h" |
20 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" | 20 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" |
21 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" | 21 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
22 #include "wtf/Noncopyable.h" | 22 #include "wtf/Noncopyable.h" |
23 #include "wtf/OwnPtr.h" | |
24 #include "wtf/PassRefPtr.h" | 23 #include "wtf/PassRefPtr.h" |
| 24 #include "wtf/PtrUtil.h" |
25 #include "wtf/RefCounted.h" | 25 #include "wtf/RefCounted.h" |
26 #include "wtf/RefPtr.h" | 26 #include "wtf/RefPtr.h" |
27 #include "wtf/Vector.h" | 27 #include "wtf/Vector.h" |
28 #include "wtf/text/StringBuilder.h" | 28 #include "wtf/text/StringBuilder.h" |
29 | |
30 #include <algorithm> | 29 #include <algorithm> |
31 #include <memory> | 30 #include <memory> |
32 | 31 |
33 using blink::protocol::Array; | 32 using blink::protocol::Array; |
34 using blink::protocol::CacheStorage::Cache; | 33 using blink::protocol::CacheStorage::Cache; |
35 using blink::protocol::CacheStorage::DataEntry; | 34 using blink::protocol::CacheStorage::DataEntry; |
36 | 35 |
37 typedef blink::protocol::CacheStorage::Backend::DeleteCacheCallback DeleteCacheC
allback; | 36 typedef blink::protocol::CacheStorage::Backend::DeleteCacheCallback DeleteCacheC
allback; |
38 typedef blink::protocol::CacheStorage::Backend::DeleteEntryCallback DeleteEntryC
allback; | 37 typedef blink::protocol::CacheStorage::Backend::DeleteEntryCallback DeleteEntryC
allback; |
39 typedef blink::protocol::CacheStorage::Backend::RequestCacheNamesCallback Reques
tCacheNamesCallback; | 38 typedef blink::protocol::CacheStorage::Backend::RequestCacheNamesCallback Reques
tCacheNamesCallback; |
(...skipping 17 matching lines...) Expand all Loading... |
57 size_t pipe = id.find('|'); | 56 size_t pipe = id.find('|'); |
58 if (pipe == WTF::kNotFound) { | 57 if (pipe == WTF::kNotFound) { |
59 *errorString = "Invalid cache id."; | 58 *errorString = "Invalid cache id."; |
60 return false; | 59 return false; |
61 } | 60 } |
62 *securityOrigin = id.substring(0, pipe); | 61 *securityOrigin = id.substring(0, pipe); |
63 *cacheName = id.substring(pipe + 1); | 62 *cacheName = id.substring(pipe + 1); |
64 return true; | 63 return true; |
65 } | 64 } |
66 | 65 |
67 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt
ring, const String& securityOrigin) | 66 std::unique_ptr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* er
rorString, const String& securityOrigin) |
68 { | 67 { |
69 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security
Origin); | 68 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security
Origin); |
70 | 69 |
71 // Cache Storage API is restricted to trustworthy origins. | 70 // Cache Storage API is restricted to trustworthy origins. |
72 if (!secOrigin->isPotentiallyTrustworthy()) { | 71 if (!secOrigin->isPotentiallyTrustworthy()) { |
73 *errorString = secOrigin->isPotentiallyTrustworthyErrorMessage(); | 72 *errorString = secOrigin->isPotentiallyTrustworthyErrorMessage(); |
74 return nullptr; | 73 return nullptr; |
75 } | 74 } |
76 | 75 |
77 OwnPtr<WebServiceWorkerCacheStorage> cache = adoptPtr(Platform::current()->c
acheStorage(WebSecurityOrigin(secOrigin))); | 76 std::unique_ptr<WebServiceWorkerCacheStorage> cache = wrapUnique(Platform::c
urrent()->cacheStorage(WebSecurityOrigin(secOrigin))); |
78 if (!cache) | 77 if (!cache) |
79 *errorString = "Could not find cache storage."; | 78 *errorString = "Could not find cache storage."; |
80 return cache; | 79 return cache; |
81 } | 80 } |
82 | 81 |
83 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorageAndNameForId(ErrorStr
ing* errorString, const String& cacheId, String* cacheName) | 82 std::unique_ptr<WebServiceWorkerCacheStorage> assertCacheStorageAndNameForId(Err
orString* errorString, const String& cacheId, String* cacheName) |
84 { | 83 { |
85 String securityOrigin; | 84 String securityOrigin; |
86 if (!parseCacheId(errorString, cacheId, &securityOrigin, cacheName)) { | 85 if (!parseCacheId(errorString, cacheId, &securityOrigin, cacheName)) { |
87 return nullptr; | 86 return nullptr; |
88 } | 87 } |
89 return assertCacheStorage(errorString, securityOrigin); | 88 return assertCacheStorage(errorString, securityOrigin); |
90 } | 89 } |
91 | 90 |
92 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError error) | 91 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError error) |
93 { | 92 { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 private: | 241 private: |
243 DataRequestParams m_params; | 242 DataRequestParams m_params; |
244 WebServiceWorkerRequest m_request; | 243 WebServiceWorkerRequest m_request; |
245 RefPtr<ResponsesAccumulator> m_accumulator; | 244 RefPtr<ResponsesAccumulator> m_accumulator; |
246 }; | 245 }; |
247 | 246 |
248 class GetCacheKeysForRequestData : public WebServiceWorkerCache::CacheWithReques
tsCallbacks { | 247 class GetCacheKeysForRequestData : public WebServiceWorkerCache::CacheWithReques
tsCallbacks { |
249 WTF_MAKE_NONCOPYABLE(GetCacheKeysForRequestData); | 248 WTF_MAKE_NONCOPYABLE(GetCacheKeysForRequestData); |
250 | 249 |
251 public: | 250 public: |
252 GetCacheKeysForRequestData(const DataRequestParams& params, PassOwnPtr<WebSe
rviceWorkerCache> cache, std::unique_ptr<RequestEntriesCallback> callback) | 251 GetCacheKeysForRequestData(const DataRequestParams& params, std::unique_ptr<
WebServiceWorkerCache> cache, std::unique_ptr<RequestEntriesCallback> callback) |
253 : m_params(params) | 252 : m_params(params) |
254 , m_cache(std::move(cache)) | 253 , m_cache(std::move(cache)) |
255 , m_callback(std::move(callback)) | 254 , m_callback(std::move(callback)) |
256 { | 255 { |
257 } | 256 } |
258 ~GetCacheKeysForRequestData() override { } | 257 ~GetCacheKeysForRequestData() override { } |
259 | 258 |
260 WebServiceWorkerCache* cache() { return m_cache.get(); } | 259 WebServiceWorkerCache* cache() { return m_cache.get(); } |
261 void onSuccess(const WebVector<WebServiceWorkerRequest>& requests) override | 260 void onSuccess(const WebVector<WebServiceWorkerRequest>& requests) override |
262 { | 261 { |
(...skipping 11 matching lines...) Expand all Loading... |
274 } | 273 } |
275 } | 274 } |
276 | 275 |
277 void onError(WebServiceWorkerCacheError error) override | 276 void onError(WebServiceWorkerCacheError error) override |
278 { | 277 { |
279 m_callback->sendFailure(String::format("Error requesting requests for ca
che %s: %s", m_params.cacheName.utf8().data(), serviceWorkerCacheErrorString(err
or).data())); | 278 m_callback->sendFailure(String::format("Error requesting requests for ca
che %s: %s", m_params.cacheName.utf8().data(), serviceWorkerCacheErrorString(err
or).data())); |
280 } | 279 } |
281 | 280 |
282 private: | 281 private: |
283 DataRequestParams m_params; | 282 DataRequestParams m_params; |
284 OwnPtr<WebServiceWorkerCache> m_cache; | 283 std::unique_ptr<WebServiceWorkerCache> m_cache; |
285 std::unique_ptr<RequestEntriesCallback> m_callback; | 284 std::unique_ptr<RequestEntriesCallback> m_callback; |
286 }; | 285 }; |
287 | 286 |
288 class GetCacheForRequestData | 287 class GetCacheForRequestData |
289 : public WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks { | 288 : public WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks { |
290 WTF_MAKE_NONCOPYABLE(GetCacheForRequestData); | 289 WTF_MAKE_NONCOPYABLE(GetCacheForRequestData); |
291 | 290 |
292 public: | 291 public: |
293 GetCacheForRequestData(const DataRequestParams& params, std::unique_ptr<Requ
estEntriesCallback> callback) | 292 GetCacheForRequestData(const DataRequestParams& params, std::unique_ptr<Requ
estEntriesCallback> callback) |
294 : m_params(params) | 293 : m_params(params) |
295 , m_callback(std::move(callback)) | 294 , m_callback(std::move(callback)) |
296 { | 295 { |
297 } | 296 } |
298 ~GetCacheForRequestData() override { } | 297 ~GetCacheForRequestData() override { } |
299 | 298 |
300 void onSuccess(std::unique_ptr<WebServiceWorkerCache> cache) override | 299 void onSuccess(std::unique_ptr<WebServiceWorkerCache> cache) override |
301 { | 300 { |
302 auto* cacheRequest = new GetCacheKeysForRequestData(m_params, adoptPtr(c
ache.release()), std::move(m_callback)); | 301 auto* cacheRequest = new GetCacheKeysForRequestData(m_params, wrapUnique
(cache.release()), std::move(m_callback)); |
303 cacheRequest->cache()->dispatchKeys(cacheRequest, nullptr, WebServiceWor
kerCache::QueryParams()); | 302 cacheRequest->cache()->dispatchKeys(cacheRequest, nullptr, WebServiceWor
kerCache::QueryParams()); |
304 } | 303 } |
305 | 304 |
306 void onError(WebServiceWorkerCacheError error) override | 305 void onError(WebServiceWorkerCacheError error) override |
307 { | 306 { |
308 m_callback->sendFailure(String::format("Error requesting cache %s: %s",
m_params.cacheName.utf8().data(), serviceWorkerCacheErrorString(error).data())); | 307 m_callback->sendFailure(String::format("Error requesting cache %s: %s",
m_params.cacheName.utf8().data(), serviceWorkerCacheErrorString(error).data())); |
309 } | 308 } |
310 | 309 |
311 private: | 310 private: |
312 DataRequestParams m_params; | 311 DataRequestParams m_params; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 { | 410 { |
412 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security
Origin); | 411 RefPtr<SecurityOrigin> secOrigin = SecurityOrigin::createFromString(security
Origin); |
413 | 412 |
414 // Cache Storage API is restricted to trustworthy origins. | 413 // Cache Storage API is restricted to trustworthy origins. |
415 if (!secOrigin->isPotentiallyTrustworthy()) { | 414 if (!secOrigin->isPotentiallyTrustworthy()) { |
416 // Don't treat this as an error, just don't attempt to open and enumerat
e the caches. | 415 // Don't treat this as an error, just don't attempt to open and enumerat
e the caches. |
417 callback->sendSuccess(Array<protocol::CacheStorage::Cache>::create()); | 416 callback->sendSuccess(Array<protocol::CacheStorage::Cache>::create()); |
418 return; | 417 return; |
419 } | 418 } |
420 | 419 |
421 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString,
securityOrigin); | 420 std::unique_ptr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(err
orString, securityOrigin); |
422 if (!cache) { | 421 if (!cache) { |
423 callback->sendFailure(*errorString); | 422 callback->sendFailure(*errorString); |
424 return; | 423 return; |
425 } | 424 } |
426 cache->dispatchKeys(new RequestCacheNames(securityOrigin, std::move(callback
))); | 425 cache->dispatchKeys(new RequestCacheNames(securityOrigin, std::move(callback
))); |
427 } | 426 } |
428 | 427 |
429 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const
String& cacheId, int skipCount, int pageSize, std::unique_ptr<RequestEntriesCall
back> callback) | 428 void InspectorCacheStorageAgent::requestEntries(ErrorString* errorString, const
String& cacheId, int skipCount, int pageSize, std::unique_ptr<RequestEntriesCall
back> callback) |
430 { | 429 { |
431 String cacheName; | 430 String cacheName; |
432 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndNameForId(
errorString, cacheId, &cacheName); | 431 std::unique_ptr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndN
ameForId(errorString, cacheId, &cacheName); |
433 if (!cache) { | 432 if (!cache) { |
434 callback->sendFailure(*errorString); | 433 callback->sendFailure(*errorString); |
435 return; | 434 return; |
436 } | 435 } |
437 DataRequestParams params; | 436 DataRequestParams params; |
438 params.cacheName = cacheName; | 437 params.cacheName = cacheName; |
439 params.pageSize = pageSize; | 438 params.pageSize = pageSize; |
440 params.skipCount = skipCount; | 439 params.skipCount = skipCount; |
441 cache->dispatchOpen(new GetCacheForRequestData(params, std::move(callback)),
WebString(cacheName)); | 440 cache->dispatchOpen(new GetCacheForRequestData(params, std::move(callback)),
WebString(cacheName)); |
442 } | 441 } |
443 | 442 |
444 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str
ing& cacheId, std::unique_ptr<DeleteCacheCallback> callback) | 443 void InspectorCacheStorageAgent::deleteCache(ErrorString* errorString, const Str
ing& cacheId, std::unique_ptr<DeleteCacheCallback> callback) |
445 { | 444 { |
446 String cacheName; | 445 String cacheName; |
447 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndNameForId(
errorString, cacheId, &cacheName); | 446 std::unique_ptr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndN
ameForId(errorString, cacheId, &cacheName); |
448 if (!cache) { | 447 if (!cache) { |
449 callback->sendFailure(*errorString); | 448 callback->sendFailure(*errorString); |
450 return; | 449 return; |
451 } | 450 } |
452 cache->dispatchDelete(new DeleteCache(std::move(callback)), WebString(cacheN
ame)); | 451 cache->dispatchDelete(new DeleteCache(std::move(callback)), WebString(cacheN
ame)); |
453 } | 452 } |
454 | 453 |
455 void InspectorCacheStorageAgent::deleteEntry(ErrorString* errorString, const Str
ing& cacheId, const String& request, std::unique_ptr<DeleteEntryCallback> callba
ck) | 454 void InspectorCacheStorageAgent::deleteEntry(ErrorString* errorString, const Str
ing& cacheId, const String& request, std::unique_ptr<DeleteEntryCallback> callba
ck) |
456 { | 455 { |
457 String cacheName; | 456 String cacheName; |
458 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndNameForId(
errorString, cacheId, &cacheName); | 457 std::unique_ptr<WebServiceWorkerCacheStorage> cache = assertCacheStorageAndN
ameForId(errorString, cacheId, &cacheName); |
459 if (!cache) { | 458 if (!cache) { |
460 callback->sendFailure(*errorString); | 459 callback->sendFailure(*errorString); |
461 return; | 460 return; |
462 } | 461 } |
463 cache->dispatchOpen(new GetCacheForDeleteEntry(request, cacheName, std::move
(callback)), WebString(cacheName)); | 462 cache->dispatchOpen(new GetCacheForDeleteEntry(request, cacheName, std::move
(callback)), WebString(cacheName)); |
464 } | 463 } |
465 | 464 |
466 | 465 |
467 } // namespace blink | 466 } // namespace blink |
OLD | NEW |