From bc355cdce260575c41a2180fa5f7b11d87f951b3 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Mon, 2 Mar 2015 14:22:00 +0100 Subject: [PATCH] Bug 940273 - Part 5 - Expose Service Worker Cache as window.caches when pref is enabled. r=ehsan --- dom/base/nsGlobalWindow.cpp | 16 ++++++++++++++++ dom/base/nsGlobalWindow.h | 5 +++++ dom/tests/mochitest/general/test_interfaces.html | 4 ++++ dom/webidl/Window.webidl | 6 ++++++ 4 files changed, 31 insertions(+) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 28dc05ba7fd4..4efc7af62e98 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -207,6 +207,7 @@ #include "mozilla/dom/AudioContext.h" #include "mozilla/dom/BlobBinding.h" #include "mozilla/dom/BrowserElementDictionariesBinding.h" +#include "mozilla/dom/cache/CacheStorage.h" #include "mozilla/dom/Console.h" #include "mozilla/dom/Fetch.h" #include "mozilla/dom/FunctionBinding.h" @@ -260,6 +261,7 @@ using namespace mozilla::dom; using namespace mozilla::dom::ipc; using mozilla::TimeStamp; using mozilla::TimeDuration; +using mozilla::dom::cache::CacheStorage; using mozilla::dom::indexedDB::IDBFactory; nsGlobalWindow::WindowByIdTable *nsGlobalWindow::sWindowsById = nullptr; @@ -1786,6 +1788,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsGlobalWindow) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGamepads) #endif + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCacheStorage) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mVRDevices) // Traverse stuff from nsPIDOMWindow @@ -1849,6 +1852,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGlobalWindow) NS_IMPL_CYCLE_COLLECTION_UNLINK(mGamepads) #endif + NS_IMPL_CYCLE_COLLECTION_UNLINK(mCacheStorage) NS_IMPL_CYCLE_COLLECTION_UNLINK(mVRDevices) // Unlink stuff from nsPIDOMWindow @@ -10719,6 +10723,18 @@ nsGlobalWindow::GetInterface(JSContext* aCx, nsIJSID* aIID, dom::GetInterface(aCx, this, aIID, aRetval, aError); } +already_AddRefed +nsGlobalWindow::GetCaches(ErrorResult& aRv) +{ + if (!mCacheStorage) { + mCacheStorage = CacheStorage::CreateOnMainThread(cache::DEFAULT_NAMESPACE, + this, GetPrincipal(), aRv); + } + + nsRefPtr ref = mCacheStorage; + return ref.forget(); +} + void nsGlobalWindow::FireOfflineStatusEventIfChanged() { diff --git a/dom/base/nsGlobalWindow.h b/dom/base/nsGlobalWindow.h index 2ca048f0b506..7b0254a4b799 100644 --- a/dom/base/nsGlobalWindow.h +++ b/dom/base/nsGlobalWindow.h @@ -115,6 +115,9 @@ class RequestOrUSVString; class Selection; class SpeechSynthesis; class WakeLock; +namespace cache { +class CacheStorage; +} // namespace cache namespace indexedDB { class IDBFactory; } // namespace indexedDB @@ -878,6 +881,7 @@ protected: public: void Alert(mozilla::ErrorResult& aError); void Alert(const nsAString& aMessage, mozilla::ErrorResult& aError); + already_AddRefed GetCaches(mozilla::ErrorResult& aRv); bool Confirm(const nsAString& aMessage, mozilla::ErrorResult& aError); already_AddRefed Fetch(const mozilla::dom::RequestOrUSVString& aInput, const mozilla::dom::RequestInit& aInit, @@ -1552,6 +1556,7 @@ protected: nsString mDefaultStatus; nsGlobalWindowObserver* mObserver; // Inner windows only. nsRefPtr mCrypto; + nsRefPtr mCacheStorage; nsRefPtr mConsole; // We need to store an nsISupports pointer to this object because the // mozilla::dom::External class doesn't exist on b2g and using the type diff --git a/dom/tests/mochitest/general/test_interfaces.html b/dom/tests/mochitest/general/test_interfaces.html index ca962016b45c..65fc19b778e3 100644 --- a/dom/tests/mochitest/general/test_interfaces.html +++ b/dom/tests/mochitest/general/test_interfaces.html @@ -196,6 +196,10 @@ var interfaceNamesInGlobalScope = {name: "BoxObject", xbl: true}, // IMPORTANT: Do not change this list without review from a DOM peer! {name: "BroadcastChannel", pref: "dom.broadcastChannel.enabled"}, +// IMPORTANT: Do not change this list without review from a DOM peer! + {name: "Cache", pref: "dom.caches.enabled"}, +// IMPORTANT: Do not change this list without review from a DOM peer! + {name: "CacheStorage", pref: "dom.caches.enabled"}, // IMPORTANT: Do not change this list without review from a DOM peer! {name: "CallEvent", b2g: true, pref: "dom.telephony.enabled"}, // IMPORTANT: Do not change this list without review from a DOM peer! diff --git a/dom/webidl/Window.webidl b/dom/webidl/Window.webidl index 7f30b556494f..a32af1ee69fd 100644 --- a/dom/webidl/Window.webidl +++ b/dom/webidl/Window.webidl @@ -250,6 +250,12 @@ interface WindowModal { }; Window implements WindowModal; +// https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#self-caches +partial interface Window { +[Throws, Func="mozilla::dom::cache::CacheStorage::PrefEnabled"] +readonly attribute CacheStorage caches; +}; + // Mozilla-specific stuff partial interface Window { //[NewObject, Throws] CSSStyleDeclaration getDefaultComputedStyle(Element elt, optional DOMString pseudoElt = "");