Chromium Code Reviews| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/serviceworkers/InspectorServiceWorkerCacheAgent.h" | 6 #include "modules/serviceworkers/InspectorServiceWorkerCacheAgent.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/ScriptState.h" | |
| 8 #include "core/InspectorBackendDispatcher.h" | 9 #include "core/InspectorBackendDispatcher.h" |
| 9 #include "core/InspectorTypeBuilder.h" | 10 #include "core/InspectorTypeBuilder.h" |
| 11 #include "core/dom/Document.h" | |
| 12 #include "core/frame/DOMWindow.h" | |
| 13 #include "core/frame/LocalFrame.h" | |
| 14 #include "core/page/Page.h" | |
| 15 #include "modules/serviceworkers/CacheStorage.h" | |
| 16 #include "modules/serviceworkers/GlobalCacheStorage.h" | |
| 10 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 17 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
| 11 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 18 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 12 #include "platform/JSONValues.h" | 19 #include "platform/JSONValues.h" |
| 13 #include "platform/heap/Handle.h" | 20 #include "platform/heap/Handle.h" |
| 14 #include "platform/weborigin/DatabaseIdentifier.h" | 21 #include "platform/weborigin/DatabaseIdentifier.h" |
| 15 #include "public/platform/Platform.h" | 22 #include "public/platform/Platform.h" |
| 16 #include "public/platform/WebServiceWorkerCache.h" | 23 #include "public/platform/WebServiceWorkerCache.h" |
| 17 #include "public/platform/WebServiceWorkerCacheError.h" | 24 #include "public/platform/WebServiceWorkerCacheError.h" |
| 18 #include "public/platform/WebServiceWorkerCacheStorage.h" | 25 #include "public/platform/WebServiceWorkerCacheStorage.h" |
| 19 #include "public/platform/WebServiceWorkerRequest.h" | 26 #include "public/platform/WebServiceWorkerRequest.h" |
| 20 #include "public/platform/WebServiceWorkerResponse.h" | 27 #include "public/platform/WebServiceWorkerResponse.h" |
| 21 #include "public/platform/WebString.h" | 28 #include "public/platform/WebString.h" |
| 22 #include "public/platform/WebURL.h" | 29 #include "public/platform/WebURL.h" |
| 23 #include "public/platform/WebVector.h" | 30 #include "public/platform/WebVector.h" |
| 24 #include "wtf/Noncopyable.h" | 31 #include "wtf/Noncopyable.h" |
| 25 #include "wtf/OwnPtr.h" | 32 #include "wtf/OwnPtr.h" |
| 26 #include "wtf/PassRefPtr.h" | 33 #include "wtf/PassRefPtr.h" |
| 27 #include "wtf/RefCounted.h" | 34 #include "wtf/RefCounted.h" |
| 28 #include "wtf/RefPtr.h" | 35 #include "wtf/RefPtr.h" |
| 29 #include "wtf/Vector.h" | 36 #include "wtf/Vector.h" |
| 30 #include "wtf/text/StringBuilder.h" | 37 #include "wtf/text/StringBuilder.h" |
| 31 | 38 |
| 32 #include <algorithm> | 39 #include <algorithm> |
| 33 | 40 |
| 34 using blink::TypeBuilder::Array; | 41 using blink::TypeBuilder::Array; |
| 35 using blink::TypeBuilder::ServiceWorkerCache::DataEntry; | 42 using blink::TypeBuilder::CacheStorage::DataEntry; |
| 36 | 43 |
| 37 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Del eteCacheCallback DeleteCacheCallback; | 44 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::DeleteCac heCallback DeleteCacheCallback; |
| 38 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestCacheNamesCallback RequestCacheNamesCallback; | 45 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestCa cheNamesCallback RequestCacheNamesCallback; |
| 39 typedef blink::InspectorBackendDispatcher::ServiceWorkerCacheCommandHandler::Req uestEntriesCallback RequestEntriesCallback; | 46 typedef blink::InspectorBackendDispatcher::CacheStorageCommandHandler::RequestEn triesCallback RequestEntriesCallback; |
| 40 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; | 47 typedef blink::InspectorBackendDispatcher::CallbackBase RequestCallback; |
| 41 | 48 |
| 42 namespace blink { | 49 namespace blink { |
| 43 | 50 |
| 44 namespace { | 51 namespace { |
| 45 | 52 |
| 46 PassOwnPtr<WebServiceWorkerCacheStorage> assertCacheStorage(ErrorString* errorSt ring, ServiceWorkerGlobalScope* globalScope) | 53 LocalFrame* findFrameWithSecurityOrigin(Page* page, const String& securityOrigin ) |
|
pfeldman
2015/04/02 21:18:58
Sorry, did I miss a reply here? I was suggesting t
| |
| 47 { | 54 { |
| 48 String identifier = createDatabaseIdentifierFromSecurityOrigin(globalScope-> securityOrigin()); | 55 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse Next()) { |
| 49 OwnPtr<WebServiceWorkerCacheStorage> caches = adoptPtr(Platform::current()-> cacheStorage(identifier)); | 56 if (!frame->isLocalFrame()) |
| 50 if (!caches) { | 57 continue; |
| 51 *errorString = "Cache Storage not available in global scope."; | 58 RefPtr<SecurityOrigin> documentOrigin = toLocalFrame(frame)->document()- >securityOrigin(); |
| 59 if (documentOrigin->toRawString() == securityOrigin) | |
| 60 return toLocalFrame(frame); | |
| 61 } | |
| 62 return 0; | |
| 63 } | |
| 64 | |
| 65 DOMWindow* assertWindow(ErrorString* errorString, LocalFrame* frame) | |
|
pfeldman
2015/04/02 21:18:58
And this method
| |
| 66 { | |
| 67 DOMWindow* window = frame ? frame->domWindow() : 0; | |
| 68 | |
| 69 if (!window) | |
| 70 *errorString = "No window for given frame found."; | |
| 71 | |
| 72 return window; | |
| 73 } | |
| 74 | |
| 75 WebServiceWorkerCacheStorage* assertCacheStorage(ErrorString* errorString, DOMWi ndow& window) | |
| 76 { | |
| 77 TrackExceptionState exceptionState; | |
|
pfeldman
2015/04/02 21:18:58
And go straight to WebServiceWorkerCacheStorage he
dmurph
2015/04/02 23:23:18
Ah. So calling Platform::current()->cacheStorage(
| |
| 78 CacheStorage* cache = GlobalCacheStorage::caches(window, exceptionState); | |
| 79 if (exceptionState.hadException()) { | |
| 80 *errorString = "Exception while retrieving Cache Storage."; | |
| 52 return nullptr; | 81 return nullptr; |
| 53 } | 82 } |
| 54 return caches.release(); | 83 if (!cache) { |
| 84 *errorString = "Cache Storage not available."; | |
| 85 return nullptr; | |
| 86 } | |
| 87 WebServiceWorkerCacheStorage* webCache = cache->webCacheStorage(); | |
| 88 if (!webCache) { | |
| 89 *errorString = "Web cache not found on Cache Storage."; | |
| 90 return nullptr; | |
| 91 } | |
| 92 return webCache; | |
| 93 } | |
| 94 | |
| 95 WebServiceWorkerCacheStorage* assertCacheStorage(ErrorString* errorString, Worke rGlobalScope& scope) | |
| 96 { | |
| 97 TrackExceptionState exceptionState; | |
| 98 CacheStorage* cache = GlobalCacheStorage::caches(scope, exceptionState); | |
| 99 if (exceptionState.hadException()) { | |
| 100 *errorString = "Exception while retrieving Cache Storage."; | |
| 101 return nullptr; | |
| 102 } | |
| 103 if (!cache) { | |
| 104 *errorString = "Cache Storage not available."; | |
| 105 return nullptr; | |
| 106 } | |
| 107 WebServiceWorkerCacheStorage* webCache = cache->webCacheStorage(); | |
| 108 if (!webCache) { | |
| 109 *errorString = "Web cache not found on Cache Storage."; | |
| 110 return nullptr; | |
| 111 } | |
| 112 return webCache; | |
| 55 } | 113 } |
| 56 | 114 |
| 57 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) | 115 CString serviceWorkerCacheErrorString(WebServiceWorkerCacheError* error) |
| 58 { | 116 { |
| 59 switch (*error) { | 117 switch (*error) { |
| 60 case WebServiceWorkerCacheErrorNotImplemented: | 118 case WebServiceWorkerCacheErrorNotImplemented: |
| 61 return CString("not implemented."); | 119 return CString("not implemented."); |
| 62 break; | 120 break; |
| 63 case WebServiceWorkerCacheErrorNotFound: | 121 case WebServiceWorkerCacheErrorNotFound: |
| 64 return CString("not found."); | 122 return CString("not found."); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 { | 341 { |
| 284 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); | 342 m_callback->sendFailure(String::format("Error requesting cache names: %s ", serviceWorkerCacheErrorString(error).data())); |
| 285 } | 343 } |
| 286 | 344 |
| 287 private: | 345 private: |
| 288 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; | 346 RefPtrWillBePersistent<DeleteCacheCallback> m_callback; |
| 289 }; | 347 }; |
| 290 | 348 |
| 291 } // namespace | 349 } // namespace |
| 292 | 350 |
| 293 InspectorServiceWorkerCacheAgent::InspectorServiceWorkerCacheAgent(ServiceWorker GlobalScope* scope) | 351 InspectorServiceWorkerCacheAgent::InspectorServiceWorkerCacheAgent(Page* page) |
| 294 : InspectorBaseAgent<blink::InspectorServiceWorkerCacheAgent, InspectorFront end::ServiceWorkerCache>("ServiceWorkerCache") | 352 : InspectorBaseAgent<InspectorServiceWorkerCacheAgent, InspectorFrontend::Ca cheStorage>("CacheStorage") |
| 295 , m_globalScope(scope) | 353 , m_page(page) |
| 354 , m_serviceWorkerGlobalScope(nullptr) | |
| 296 { | 355 { |
| 297 } | 356 } |
| 298 | 357 |
| 358 InspectorServiceWorkerCacheAgent::InspectorServiceWorkerCacheAgent(ServiceWorker GlobalScope* scope) | |
| 359 : InspectorBaseAgent<InspectorServiceWorkerCacheAgent, InspectorFrontend::Ca cheStorage>("CacheStorage") | |
| 360 , m_page(nullptr) | |
| 361 , m_serviceWorkerGlobalScope(scope) | |
| 362 { | |
| 363 } | |
| 364 | |
| 365 | |
| 299 InspectorServiceWorkerCacheAgent::~InspectorServiceWorkerCacheAgent() { } | 366 InspectorServiceWorkerCacheAgent::~InspectorServiceWorkerCacheAgent() { } |
| 300 | 367 |
| 301 DEFINE_TRACE(InspectorServiceWorkerCacheAgent) | 368 DEFINE_TRACE(InspectorServiceWorkerCacheAgent) |
| 302 { | 369 { |
| 303 InspectorBaseAgent::trace(visitor); | 370 InspectorBaseAgent::trace(visitor); |
| 304 } | 371 } |
| 305 | 372 |
| 306 void InspectorServiceWorkerCacheAgent::requestCacheNames(ErrorString* errorStrin g, PassRefPtrWillBeRawPtr<RequestCacheNamesCallback> callback) | 373 void InspectorServiceWorkerCacheAgent::requestCacheNames(ErrorString* errorStrin g, const String& securityOrigin, PassRefPtrWillBeRawPtr<RequestCacheNamesCallbac k> callback) |
| 307 { | 374 { |
| 308 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 375 WebServiceWorkerCacheStorage* cache; |
| 376 if (!m_serviceWorkerGlobalScope) { | |
| 377 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); | |
| 378 DOMWindow* window = assertWindow(errorString, frame); | |
| 379 if (!window) { | |
| 380 callback->sendFailure(*errorString); | |
| 381 return; | |
| 382 } | |
| 383 cache = assertCacheStorage(errorString, *window); | |
| 384 } else { | |
| 385 cache = assertCacheStorage(errorString, *m_serviceWorkerGlobalScope); | |
| 386 } | |
| 309 if (!cache) { | 387 if (!cache) { |
| 310 callback->sendFailure(*errorString); | 388 callback->sendFailure(*errorString); |
| 311 return; | 389 return; |
| 312 } | 390 } |
| 313 cache->dispatchKeys(new RequestCacheNames(callback)); | 391 cache->dispatchKeys(new RequestCacheNames(callback)); |
| 314 } | 392 } |
| 315 | 393 |
| 316 | 394 void InspectorServiceWorkerCacheAgent::requestEntries(ErrorString* errorString, const String& securityOrigin, const String& cacheName, int skipCount, int pageSi ze, PassRefPtrWillBeRawPtr<RequestEntriesCallback> callback) |
| 317 void InspectorServiceWorkerCacheAgent::requestEntries(ErrorString* errorString, const String& cacheName, int skipCount, int pageSize, PassRefPtrWillBeRawPtr<Req uestEntriesCallback> callback) | |
| 318 { | 395 { |
| 319 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 396 WebServiceWorkerCacheStorage* cache; |
| 397 if (!m_serviceWorkerGlobalScope) { | |
| 398 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); | |
| 399 DOMWindow* window = assertWindow(errorString, frame); | |
| 400 if (!window) { | |
| 401 callback->sendFailure(*errorString); | |
| 402 return; | |
| 403 } | |
| 404 cache = assertCacheStorage(errorString, *window); | |
| 405 } else { | |
| 406 cache = assertCacheStorage(errorString, *m_serviceWorkerGlobalScope); | |
| 407 } | |
| 320 if (!cache) { | 408 if (!cache) { |
| 321 callback->sendFailure(*errorString); | 409 callback->sendFailure(*errorString); |
| 322 return; | 410 return; |
| 323 } | 411 } |
| 324 DataRequestParams params; | 412 DataRequestParams params; |
| 325 params.cacheName = cacheName; | 413 params.cacheName = cacheName; |
| 326 params.pageSize = pageSize; | 414 params.pageSize = pageSize; |
| 327 params.skipCount = skipCount; | 415 params.skipCount = skipCount; |
| 328 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); | 416 cache->dispatchOpen(new GetCacheForRequestData(params, callback), WebString( cacheName)); |
| 329 } | 417 } |
| 330 | 418 |
| 331 void InspectorServiceWorkerCacheAgent::deleteCache(ErrorString* errorString, con st String& cacheName, PassRefPtrWillBeRawPtr<DeleteCacheCallback> callback) | 419 void InspectorServiceWorkerCacheAgent::deleteCache(ErrorString* errorString, con st String& securityOrigin, const String& cacheName, PassRefPtrWillBeRawPtr<Delet eCacheCallback> callback) |
| 332 { | 420 { |
| 333 OwnPtr<WebServiceWorkerCacheStorage> cache = assertCacheStorage(errorString, m_globalScope); | 421 WebServiceWorkerCacheStorage* cache; |
| 422 if (!m_serviceWorkerGlobalScope) { | |
| 423 LocalFrame* frame = findFrameWithSecurityOrigin(m_page, securityOrigin); | |
| 424 DOMWindow* window = assertWindow(errorString, frame); | |
| 425 if (!window) { | |
| 426 callback->sendFailure(*errorString); | |
| 427 return; | |
| 428 } | |
| 429 cache = assertCacheStorage(errorString, *window); | |
| 430 } else { | |
| 431 cache = assertCacheStorage(errorString, *m_serviceWorkerGlobalScope); | |
| 432 } | |
| 334 if (!cache) { | 433 if (!cache) { |
| 335 callback->sendFailure(*errorString); | 434 callback->sendFailure(*errorString); |
| 336 return; | 435 return; |
| 337 } | 436 } |
| 338 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); | 437 cache->dispatchDelete(new DeleteCache(callback), WebString(cacheName)); |
| 339 } | 438 } |
| 340 | 439 |
| 341 } // namespace blink | 440 } // namespace blink |
| OLD | NEW |