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