OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "config.h" |
| 6 #include "modules/serviceworkers/GlobalCacheStorage.h" |
| 7 |
| 8 #include "core/frame/LocalDOMWindow.h" |
| 9 #include "core/frame/UseCounter.h" |
| 10 #include "core/workers/WorkerGlobalScope.h" |
| 11 #include "modules/serviceworkers/CacheStorage.h" |
| 12 #include "platform/Supplementable.h" |
| 13 #include "platform/heap/Handle.h" |
| 14 #include "platform/weborigin/DatabaseIdentifier.h" |
| 15 #include "public/platform/Platform.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 namespace { |
| 20 |
| 21 template <typename T> |
| 22 class GlobalCacheStorageImpl final : public NoBaseWillBeGarbageCollectedFinalize
d<GlobalCacheStorageImpl<T>>, public WillBeHeapSupplement<T> { |
| 23 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(GlobalCacheStorageImpl); |
| 24 public: |
| 25 static GlobalCacheStorageImpl& from(T& supplementable, ExecutionContext* exe
cutionContext) |
| 26 { |
| 27 GlobalCacheStorageImpl* supplement = static_cast<GlobalCacheStorageImpl*
>(WillBeHeapSupplement<T>::from(supplementable, name())); |
| 28 if (!supplement) { |
| 29 supplement = new GlobalCacheStorageImpl(); |
| 30 WillBeHeapSupplement<T>::provideTo(supplementable, name(), adoptPtrW
illBeNoop(supplement)); |
| 31 } |
| 32 return *supplement; |
| 33 } |
| 34 |
| 35 CacheStorage* caches(ScriptState* scriptState, ExceptionState& exceptionStat
e) |
| 36 { |
| 37 ExecutionContext* context = scriptState->executionContext(); |
| 38 if (!context->securityOrigin()->canAccessCacheStorage()) { |
| 39 if (context->securityContext().isSandboxed(SandboxOrigin)) |
| 40 exceptionState.throwSecurityError("Cache storage is disabled bec
ause the context is sandboxed and lacks the 'allow-same-origin' flag."); |
| 41 else if (context->url().protocolIs("data")) |
| 42 exceptionState.throwSecurityError("Cache storage is disabled ins
ide 'data:' URLs."); |
| 43 else |
| 44 exceptionState.throwSecurityError("Access to cache storage is de
nied."); |
| 45 return nullptr; |
| 46 } |
| 47 |
| 48 if (!m_caches) { |
| 49 String identifier = createDatabaseIdentifierFromSecurityOrigin(conte
xt->securityOrigin()); |
| 50 ASSERT(!identifier.isEmpty()); |
| 51 m_caches = CacheStorage::create(Platform::current()->cacheStorage(id
entifier)); |
| 52 } |
| 53 return m_caches; |
| 54 } |
| 55 |
| 56 DEFINE_INLINE_VIRTUAL_TRACE() |
| 57 { |
| 58 visitor->trace(m_caches); |
| 59 WillBeHeapSupplement<T>::trace(visitor); |
| 60 } |
| 61 |
| 62 private: |
| 63 GlobalCacheStorageImpl() |
| 64 { |
| 65 } |
| 66 |
| 67 static const char* name() { return "CacheStorage"; } |
| 68 |
| 69 PersistentWillBeMember<CacheStorage> m_caches; |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 CacheStorage* GlobalCacheStorage::caches(ScriptState* scriptState, DOMWindow& wi
ndow, ExceptionState& exceptionState) |
| 75 { |
| 76 return GlobalCacheStorageImpl<LocalDOMWindow>::from(toLocalDOMWindow(window)
, window.executionContext()).caches(scriptState, exceptionState); |
| 77 } |
| 78 |
| 79 CacheStorage* GlobalCacheStorage::caches(ScriptState* scriptState, WorkerGlobalS
cope& worker, ExceptionState& exceptionState) |
| 80 { |
| 81 return GlobalCacheStorageImpl<WorkerGlobalScope>::from(worker, worker.execut
ionContext()).caches(scriptState, exceptionState); |
| 82 } |
| 83 |
| 84 } // namespace blink |
OLD | NEW |