diff --git a/browser/base/content/test/sanitize/browser.ini b/browser/base/content/test/sanitize/browser.ini index 15003c85889a..72557f4b0d9a 100644 --- a/browser/base/content/test/sanitize/browser.ini +++ b/browser/base/content/test/sanitize/browser.ini @@ -11,9 +11,6 @@ support-files= [browser_sanitize-sitepermissions.js] [browser_sanitize-timespans.js] [browser_sanitizeDialog.js] -prefs= - browser.cache.offline.enable=true - browser.cache.offline.storage.enable=true [browser_cookiePermission.js] [browser_cookiePermission_aboutURL.js] [browser_cookiePermission_containers.js] diff --git a/browser/base/content/test/sanitize/browser_sanitizeDialog.js b/browser/base/content/test/sanitize/browser_sanitizeDialog.js index 0649c54f5d8c..bc4aa6a0f0f3 100644 --- a/browser/base/content/test/sanitize/browser_sanitizeDialog.js +++ b/browser/base/content/test/sanitize/browser_sanitizeDialog.js @@ -490,6 +490,8 @@ add_task(async function test_form_entries() { // Test for offline cache deletion add_task(async function test_offline_cache() { + Services.prefs.setBoolPref("browser.cache.offline.enable", true); + Services.prefs.setBoolPref("browser.cache.offline.storage.enable", true); // Prepare stuff, we will work with www.example.com var URL = "http://www.example.com"; var URI = makeURI(URL); @@ -568,6 +570,8 @@ add_task(async function test_offline_cache() { cacheListener ); await wh.promiseClosed; + Services.prefs.clearUserPref("browser.cache.offline.enable"); + Services.prefs.clearUserPref("browser.cache.offline.storage.enable"); }); // Test for offline apps permission deletion diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index 75887a7c1724..f40fceaaa299 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -2982,6 +2982,12 @@ bool nsGlobalWindowInner::IsPrivilegedChromeWindow(JSContext* aCx, nsContentUtils::GetSystemPrincipal(); } +/* static */ +bool nsGlobalWindowInner::OfflineCacheAllowedForContext(JSContext* aCx, + JSObject* aObj) { + return IsSecureContextOrObjectIsFromSecureContext(aCx, aObj); +} + /* static */ bool nsGlobalWindowInner::IsRequestIdleCallbackEnabled(JSContext* aCx, JSObject* aObj) { diff --git a/dom/base/nsGlobalWindowInner.h b/dom/base/nsGlobalWindowInner.h index 2d1b687d0ace..852cc02d8cd3 100644 --- a/dom/base/nsGlobalWindowInner.h +++ b/dom/base/nsGlobalWindowInner.h @@ -387,6 +387,9 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget, static bool IsPrivilegedChromeWindow(JSContext* /* unused */, JSObject* aObj); + static bool OfflineCacheAllowedForContext(JSContext* /* unused */, + JSObject* aObj); + static bool IsRequestIdleCallbackEnabled(JSContext* aCx, JSObject* /* unused */); diff --git a/dom/webidl/OfflineResourceList.webidl b/dom/webidl/OfflineResourceList.webidl index 35a017d78f12..75df4e2f296d 100644 --- a/dom/webidl/OfflineResourceList.webidl +++ b/dom/webidl/OfflineResourceList.webidl @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -[SecureContext, Pref="browser.cache.offline.enable", +[Pref="browser.cache.offline.enable", Func="nsGlobalWindowInner::OfflineCacheAllowedForContext", Exposed=Window] interface OfflineResourceList : EventTarget { /** diff --git a/dom/webidl/Window.webidl b/dom/webidl/Window.webidl index 92e22bc1fc58..cfe78606fea9 100644 --- a/dom/webidl/Window.webidl +++ b/dom/webidl/Window.webidl @@ -72,7 +72,7 @@ typedef OfflineResourceList ApplicationCache; #ifdef HAVE_SIDEBAR [Replaceable, Throws] readonly attribute External external; #endif - [Throws, SecureContext, Pref="browser.cache.offline.enable"] readonly attribute ApplicationCache applicationCache; + [Throws, Pref="browser.cache.offline.enable", Func="nsGlobalWindowInner::OfflineCacheAllowedForContext"] readonly attribute ApplicationCache applicationCache; // user prompts [Throws, NeedsSubjectPrincipal] void alert(); diff --git a/netwerk/cache/nsCacheService.cpp b/netwerk/cache/nsCacheService.cpp index 9019c2fd2524..0949a98a10e2 100644 --- a/netwerk/cache/nsCacheService.cpp +++ b/netwerk/cache/nsCacheService.cpp @@ -39,7 +39,6 @@ #include "mozilla/Services.h" #include "nsITimer.h" #include "mozIStorageService.h" -#include "mozilla/StaticPrefs_browser.h" #include "mozilla/net/NeckoCommon.h" #include @@ -50,6 +49,8 @@ using namespace mozilla::net; /****************************************************************************** * nsCacheProfilePrefObserver *****************************************************************************/ +#define OFFLINE_CACHE_ENABLE_PREF "browser.cache.offline.enable" +#define OFFLINE_CACHE_STORAGE_ENABLE_PREF "browser.cache.offline.storage.enable" #define OFFLINE_CACHE_DIR_PREF "browser.cache.offline.parent_directory" #define OFFLINE_CACHE_CAPACITY_PREF "browser.cache.offline.capacity" #define OFFLINE_CACHE_CAPACITY 512000 @@ -61,6 +62,14 @@ static const char* observerList[] = { NS_XPCOM_SHUTDOWN_OBSERVER_ID, "last-pb-context-exited", "suspend_process_notification", "resume_process_notification"}; +static const char* prefList[] = { + OFFLINE_CACHE_ENABLE_PREF, + OFFLINE_CACHE_STORAGE_ENABLE_PREF, + OFFLINE_CACHE_CAPACITY_PREF, + OFFLINE_CACHE_DIR_PREF, + nullptr, +}; + class nsCacheProfilePrefObserver : public nsIObserver { virtual ~nsCacheProfilePrefObserver() = default; @@ -95,6 +104,8 @@ class nsCacheProfilePrefObserver : public nsIObserver { return mSanitizeOnShutdown && mClearCacheOnShutdown; } + void PrefChanged(const char* aPref); + private: bool mHaveProfile; @@ -142,6 +153,10 @@ nsresult nsCacheProfilePrefObserver::Install() { nsCOMPtr branch = do_GetService(NS_PREFSERVICE_CONTRACTID); if (!branch) return NS_ERROR_FAILURE; + Preferences::RegisterCallbacks( + PREF_CHANGE_METHOD(nsCacheProfilePrefObserver::PrefChanged), prefList, + this); + // Determine if we have a profile already // Install() is called *after* the profile-after-change notification // when there is only a single profile, or it is specified on the @@ -168,6 +183,13 @@ void nsCacheProfilePrefObserver::Remove() { obs->RemoveObserver(this, observer); } } + + // remove Pref Service observers + nsCOMPtr prefs = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (!prefs) return; + Preferences::UnregisterCallbacks( + PREF_CHANGE_METHOD(nsCacheProfilePrefObserver::PrefChanged), prefList, + this); } NS_IMETHODIMP @@ -214,6 +236,40 @@ nsCacheProfilePrefObserver::Observe(nsISupports* subject, const char* topic, return NS_OK; } +void nsCacheProfilePrefObserver::PrefChanged(const char* aPref) { + // ignore pref changes until we're done switch profiles + if (!mHaveProfile) return; + // which preference changed? + nsresult rv; + if (!strcmp(OFFLINE_CACHE_ENABLE_PREF, aPref) || + !strcmp(OFFLINE_CACHE_STORAGE_ENABLE_PREF, aPref)) { + rv = Preferences::GetBool(OFFLINE_CACHE_ENABLE_PREF, &mOfflineCacheEnabled); + if (NS_FAILED(rv)) { + return; + } + rv = Preferences::GetBool(OFFLINE_CACHE_STORAGE_ENABLE_PREF, + &mOfflineStorageCacheEnabled); + if (NS_FAILED(rv)) { + return; + } + nsCacheService::SetOfflineCacheEnabled(OfflineCacheEnabled()); + + } else if (!strcmp(OFFLINE_CACHE_CAPACITY_PREF, aPref)) { + int32_t capacity = 0; + rv = Preferences::GetInt(OFFLINE_CACHE_CAPACITY_PREF, &capacity); + if (NS_FAILED(rv)) return; + mOfflineCacheCapacity = std::max(0, capacity); + nsCacheService::SetOfflineCacheCapacity(mOfflineCacheCapacity); +#if 0 + } else if (!strcmp(OFFLINE_CACHE_DIR_PREF, aPref)) { + // XXX We probaby don't want to respond to this pref except after + // XXX profile changes. Ideally, there should be some kind of user + // XXX notification that the pref change won't take effect until + // XXX the next time the profile changes (browser launch) +#endif + } +} + nsresult nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch) { nsresult rv = NS_OK; @@ -247,9 +303,13 @@ nsresult nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch) { } // read offline cache device prefs - mOfflineCacheEnabled = StaticPrefs::browser_cache_offline_enable(); + mOfflineCacheEnabled = true; // presume offline cache is enabled + (void)branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF, &mOfflineCacheEnabled); + mOfflineStorageCacheEnabled = - StaticPrefs::browser_cache_offline_storage_enable(); + true; // presume offline storage cache is enabled + (void)branch->GetBoolPref(OFFLINE_CACHE_STORAGE_ENABLE_PREF, + &mOfflineStorageCacheEnabled); mOfflineCacheCapacity = OFFLINE_CACHE_CAPACITY; (void)branch->GetIntPref(OFFLINE_CACHE_CAPACITY_PREF, &mOfflineCacheCapacity); diff --git a/testing/profiles/web-platform/user.js b/testing/profiles/web-platform/user.js index 674af99a8199..541e936499a7 100644 --- a/testing/profiles/web-platform/user.js +++ b/testing/profiles/web-platform/user.js @@ -40,7 +40,3 @@ user_pref("dom.animations-api.implicit-keyframes.enabled", true); // sometime wpt runs test even before the document becomes visible, which would // delay video.play() and cause play() running in wrong order. user_pref("media.block-autoplay-until-in-foreground", false); -user_pref("media.block-autoplay-until-in-foreground", false); -// Enable AppCache globally for now whilst it's being removed in Bug 1584984 -user_pref("browser.cache.offline.storage.enable", true); -user_pref("browser.cache.offline.enable", true); diff --git a/toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js b/toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js index c41622fbf9bf..ec10df438401 100644 --- a/toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js +++ b/toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js @@ -406,6 +406,8 @@ async function test_push_cleared() { const { PushService } = serviceExports; const userAgentID = "bd744428-f125-436a-b6d0-dd0c9845837f"; const channelID = "0ef2ad4a-6c49-41ad-af6e-95d2425276bf"; + Services.prefs.setBoolPref("browser.cache.offline.storage.enable", true); + Services.prefs.setBoolPref("browser.cache.offline.enable", true); let db = PushServiceWebSocket.newPushDB(); @@ -555,18 +557,10 @@ var tests = [ // Storage test_storage_cleared, -]; -// Cache -// -// Due to these prefs being static, setting them doesn't make a difference in time for the test -// As we are removing AppCache in Bug 1584984 this will just be removed soon. -if ( - Services.prefs.getBoolPref("browser.cache.offline.enable") && - Services.prefs.getBoolPref("browser.cache.offline.storage.enable") -) { - tests.push(test_cache_cleared); -} + // Cache + test_cache_cleared, +]; function run_test() { for (let i = 0; i < tests.length; i++) { diff --git a/toolkit/forgetaboutsite/test/unit/xpcshell.ini b/toolkit/forgetaboutsite/test/unit/xpcshell.ini index 5267de336e2b..4d53f0286a16 100644 --- a/toolkit/forgetaboutsite/test/unit/xpcshell.ini +++ b/toolkit/forgetaboutsite/test/unit/xpcshell.ini @@ -1,4 +1,5 @@ [DEFAULT] +prefs = [browser.cache.offline.enable = true, browser.cache.offline.storage.enable=true] head = head_forgetaboutsite.js ../../../../dom/push/test/xpcshell/head.js skip-if = toolkit == 'android' support-files =