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

Unified Diff: Source/modules/serviceworkers/GlobalCacheStorage.cpp

Issue 1032623008: Expose Cache Storage API in global window/worker scope (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Update webexposed expectations 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 side-by-side diff with in-line comments
Download patch
Index: Source/modules/serviceworkers/GlobalCacheStorage.cpp
diff --git a/Source/modules/serviceworkers/GlobalCacheStorage.cpp b/Source/modules/serviceworkers/GlobalCacheStorage.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..9b1a047220c494e042647734629a8f73c2e2b268
--- /dev/null
+++ b/Source/modules/serviceworkers/GlobalCacheStorage.cpp
@@ -0,0 +1,84 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "modules/serviceworkers/GlobalCacheStorage.h"
+
+#include "core/frame/LocalDOMWindow.h"
+#include "core/frame/UseCounter.h"
+#include "core/workers/WorkerGlobalScope.h"
+#include "modules/serviceworkers/CacheStorage.h"
+#include "platform/Supplementable.h"
+#include "platform/heap/Handle.h"
+#include "platform/weborigin/DatabaseIdentifier.h"
+#include "public/platform/Platform.h"
+
+namespace blink {
+
+namespace {
+
+template <typename T>
+class GlobalCacheStorageImpl final : public NoBaseWillBeGarbageCollectedFinalized<GlobalCacheStorageImpl<T>>, public WillBeHeapSupplement<T> {
+ WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(GlobalCacheStorageImpl);
+public:
+ static GlobalCacheStorageImpl& from(T& supplementable, ExecutionContext* executionContext)
+ {
+ GlobalCacheStorageImpl* supplement = static_cast<GlobalCacheStorageImpl*>(WillBeHeapSupplement<T>::from(supplementable, name()));
+ if (!supplement) {
+ supplement = new GlobalCacheStorageImpl();
+ WillBeHeapSupplement<T>::provideTo(supplementable, name(), adoptPtrWillBeNoop(supplement));
+ }
+ return *supplement;
+ }
+
+ CacheStorage* caches(ScriptState* scriptState, ExceptionState& exceptionState)
+ {
+ ExecutionContext* context = scriptState->executionContext();
+ if (!context->securityOrigin()->canAccessCacheStorage()) {
+ if (context->securityContext().isSandboxed(SandboxOrigin))
+ exceptionState.throwSecurityError("Cache storage is disabled because the context is sandboxed and lacks the 'allow-same-origin' flag.");
+ else if (context->url().protocolIs("data"))
+ exceptionState.throwSecurityError("Cache storage is disabled inside 'data:' URLs.");
+ else
+ exceptionState.throwSecurityError("Access to cache storage is denied.");
+ return nullptr;
+ }
+
+ if (!m_caches) {
+ String identifier = createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin());
+ ASSERT(!identifier.isEmpty());
+ m_caches = CacheStorage::create(Platform::current()->cacheStorage(identifier));
+ }
+ return m_caches;
+ }
+
+ DEFINE_INLINE_VIRTUAL_TRACE()
+ {
+ visitor->trace(m_caches);
+ WillBeHeapSupplement<T>::trace(visitor);
+ }
+
+private:
+ GlobalCacheStorageImpl()
+ {
+ }
+
+ static const char* name() { return "CacheStorage"; }
+
+ PersistentWillBeMember<CacheStorage> m_caches;
+};
+
+} // namespace
+
+CacheStorage* GlobalCacheStorage::caches(ScriptState* scriptState, DOMWindow& window, ExceptionState& exceptionState)
+{
+ return GlobalCacheStorageImpl<LocalDOMWindow>::from(toLocalDOMWindow(window), window.executionContext()).caches(scriptState, exceptionState);
+}
+
+CacheStorage* GlobalCacheStorage::caches(ScriptState* scriptState, WorkerGlobalScope& worker, ExceptionState& exceptionState)
+{
+ return GlobalCacheStorageImpl<WorkerGlobalScope>::from(worker, worker.executionContext()).caches(scriptState, exceptionState);
+}
+
+} // namespace blink
« no previous file with comments | « Source/modules/serviceworkers/GlobalCacheStorage.h ('k') | Source/modules/serviceworkers/WindowCacheStorage.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698