From 1a7b13aab4c551a18bf3f53e836e28f9c78ecead Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 8 Jan 2018 14:05:03 +0100 Subject: [PATCH] Bug 1419771 - Introduce DOMPrefs, a thread-safe access to preferences for DOM - part 1 - dump enabled, r=asuth --- dom/base/DOMPrefs.cpp | 51 ++++++++++++++++++++ dom/base/DOMPrefs.h | 23 +++++++++ dom/base/moz.build | 2 + dom/base/nsContentUtils.cpp | 22 --------- dom/base/nsContentUtils.h | 8 --- dom/base/nsFrameMessageManager.cpp | 3 +- dom/base/nsGlobalWindowInner.cpp | 3 +- dom/workers/WorkerPrefs.h | 3 -- dom/workers/WorkerScope.cpp | 3 +- dom/worklet/WorkletGlobalScope.cpp | 4 +- js/xpconnect/loader/mozJSComponentLoader.cpp | 3 +- js/xpconnect/src/Sandbox.cpp | 3 +- js/xpconnect/src/XPCWrappedJSClass.cpp | 4 +- js/xpconnect/src/nsXPConnect.cpp | 5 +- 14 files changed, 93 insertions(+), 44 deletions(-) create mode 100644 dom/base/DOMPrefs.cpp create mode 100644 dom/base/DOMPrefs.h diff --git a/dom/base/DOMPrefs.cpp b/dom/base/DOMPrefs.cpp new file mode 100644 index 000000000000..9bcd59ce9aac --- /dev/null +++ b/dom/base/DOMPrefs.cpp @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +#include "DOMPrefs.h" +#include "mozilla/Atomics.h" +#include "mozilla/Preferences.h" + +namespace mozilla { +namespace dom { + +#define PREF(name, pref) \ + /* static */ bool \ + DOMPrefs::name() \ + { \ + static bool initialized = false; \ + static Atomic cachedValue; \ + if (!initialized) { \ + initialized = true; \ + Preferences::AddAtomicBoolVarCache(&cachedValue, pref, false); \ + } \ + return cachedValue; \ + } + +#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) +PREF(DumpEnabled, "browser.dom.window.dump.enabled") +#else +/* static */ bool +DOMPrefs::DumpEnabled() +{ + return true; +} +#endif + +#undef PREF + +#define PREF_WEBIDL(name) \ + /* static */ bool \ + DOMPrefs::name(JSContext* aCx, JSObject* aObj) \ + { \ + return DOMPrefs::name(); \ + } + +// It will be useful, eventually. + +#undef PREF_WEBIDL + +} // dom namespace +} // mozilla namespace diff --git a/dom/base/DOMPrefs.h b/dom/base/DOMPrefs.h new file mode 100644 index 000000000000..96ea5fa23ee1 --- /dev/null +++ b/dom/base/DOMPrefs.h @@ -0,0 +1,23 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +#ifndef mozilla_dom_DOMPrefs_h +#define mozilla_dom_DOMPrefs_h + +namespace mozilla { +namespace dom { + +class DOMPrefs final +{ +public: + // Returns true if the browser.dom.window.dump.enabled pref is set. + static bool DumpEnabled(); +}; + +} // dom namespace +} // mozilla namespace + +#endif // mozilla_dom_DOMPrefs_h diff --git a/dom/base/moz.build b/dom/base/moz.build index 43cd83b83b6a..880a541bf811 100644 --- a/dom/base/moz.build +++ b/dom/base/moz.build @@ -172,6 +172,7 @@ EXPORTS.mozilla.dom += [ 'DOMMatrix.h', 'DOMParser.h', 'DOMPoint.h', + 'DOMPrefs.h', 'DOMQuad.h', 'DOMRect.h', 'DOMRequest.h', @@ -250,6 +251,7 @@ UNIFIED_SOURCES += [ 'DOMMatrix.cpp', 'DOMParser.cpp', 'DOMPoint.cpp', + 'DOMPrefs.cpp', 'DOMQuad.cpp', 'DOMRect.cpp', 'DOMRequest.cpp', diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index c399b8c7c504..c66fac00e605 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -334,10 +334,6 @@ nsIParser* nsContentUtils::sXMLFragmentParser = nullptr; nsIFragmentContentSink* nsContentUtils::sXMLFragmentSink = nullptr; bool nsContentUtils::sFragmentParsingActive = false; -#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) -bool nsContentUtils::sDOMWindowDumpEnabled; -#endif - bool nsContentUtils::sDoNotTrackEnabled = false; mozilla::LazyLogModule nsContentUtils::sDOMDumpLog("Dump"); @@ -694,11 +690,6 @@ nsContentUtils::Init() "network.cookie.cookieBehavior", nsICookieService::BEHAVIOR_ACCEPT); -#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) - Preferences::AddBoolVarCache(&sDOMWindowDumpEnabled, - "browser.dom.window.dump.enabled"); -#endif - Preferences::AddBoolVarCache(&sDoNotTrackEnabled, "privacy.donottrackheader.enabled", false); @@ -7586,19 +7577,6 @@ nsContentUtils::IsAllowedNonCorsContentType(const nsACString& aHeaderValue) contentType.LowerCaseEqualsLiteral("multipart/form-data"); } -bool -nsContentUtils::DOMWindowDumpEnabled() -{ -#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) - // In optimized builds we check a pref that controls if we should - // enable output from dump() or not, in debug builds it's always - // enabled. - return nsContentUtils::sDOMWindowDumpEnabled; -#else - return true; -#endif -} - bool nsContentUtils::DoNotTrackEnabled() { diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 8464dd864eb5..ec024f934288 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -2695,11 +2695,6 @@ public: */ static mozilla::HTMLEditor* GetHTMLEditor(nsPresContext* aPresContext); - /** - * Returns true if the browser.dom.window.dump.enabled pref is set. - */ - static bool DOMWindowDumpEnabled(); - /** * Returns true if the privacy.donottrackheader.enabled pref is set. */ @@ -3472,9 +3467,6 @@ private: // bytecode out of the nsCacheInfoChannel. static nsCString* sJSBytecodeMimeType; -#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) - static bool sDOMWindowDumpEnabled; -#endif static bool sDoNotTrackEnabled; static mozilla::LazyLogModule sDOMDumpLog; diff --git a/dom/base/nsFrameMessageManager.cpp b/dom/base/nsFrameMessageManager.cpp index 7b379ea5b655..e63db36a9937 100644 --- a/dom/base/nsFrameMessageManager.cpp +++ b/dom/base/nsFrameMessageManager.cpp @@ -36,6 +36,7 @@ #include "mozilla/Preferences.h" #include "mozilla/ScriptPreloader.h" #include "mozilla/Telemetry.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/File.h" #include "mozilla/dom/MessagePort.h" #include "mozilla/dom/ContentParent.h" @@ -805,7 +806,7 @@ nsFrameMessageManager::ReleaseCachedProcesses() NS_IMETHODIMP nsFrameMessageManager::Dump(const nsAString& aStr) { - if (!nsContentUtils::DOMWindowDumpEnabled()) { + if (!DOMPrefs::DumpEnabled()) { return NS_OK; } diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index 61db0e0ce110..bc07bc255047 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -17,6 +17,7 @@ #include "nsHistory.h" #include "nsDOMNavigationTiming.h" #include "nsIDOMStorageManager.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/LocalStorage.h" #include "mozilla/dom/Storage.h" #include "mozilla/dom/IdleRequest.h" @@ -3520,7 +3521,7 @@ nsGlobalWindowInner::GetFullScreen() void nsGlobalWindowInner::Dump(const nsAString& aStr) { - if (!nsContentUtils::DOMWindowDumpEnabled()) { + if (!DOMPrefs::DumpEnabled()) { return; } diff --git a/dom/workers/WorkerPrefs.h b/dom/workers/WorkerPrefs.h index 1fcaaa07a528..947471192b0b 100644 --- a/dom/workers/WorkerPrefs.h +++ b/dom/workers/WorkerPrefs.h @@ -21,9 +21,6 @@ // * First argument is the name of the pref. // * The name of the function that updates the new value of a pref. -#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) -WORKER_SIMPLE_PREF("browser.dom.window.dump.enabled", DumpEnabled, DUMP) -#endif WORKER_SIMPLE_PREF("canvas.imagebitmap_extensions.enabled", ImageBitmapExtensionsEnabled, IMAGEBITMAP_EXTENSIONS_ENABLED) WORKER_SIMPLE_PREF("dom.caches.enabled", DOMCachesEnabled, DOM_CACHES) WORKER_SIMPLE_PREF("dom.caches.testing.enabled", DOMCachesTestingEnabled, DOM_CACHES_TESTING) diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 0f7d6afa888a..3841b648f25e 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -12,6 +12,7 @@ #include "mozilla/dom/Clients.h" #include "mozilla/dom/Console.h" #include "mozilla/dom/DedicatedWorkerGlobalScopeBinding.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/Fetch.h" #include "mozilla/dom/FunctionBinding.h" #include "mozilla/dom/IDBFactory.h" @@ -371,7 +372,7 @@ WorkerGlobalScope::Dump(const Optional& aString) const } #if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP)) - if (!mWorkerPrivate->DumpEnabled()) { + if (!DOMPrefs::DumpEnabled()) { return; } #endif diff --git a/dom/worklet/WorkletGlobalScope.cpp b/dom/worklet/WorkletGlobalScope.cpp index 25637431ad65..30c011942f0b 100644 --- a/dom/worklet/WorkletGlobalScope.cpp +++ b/dom/worklet/WorkletGlobalScope.cpp @@ -7,7 +7,7 @@ #include "WorkletGlobalScope.h" #include "mozilla/dom/WorkletGlobalScopeBinding.h" #include "mozilla/dom/Console.h" -#include "nsContentUtils.h" +#include "mozilla/dom/DOMPrefs.h" namespace mozilla { namespace dom { @@ -71,7 +71,7 @@ WorkletGlobalScope::GetConsole(ErrorResult& aRv) void WorkletGlobalScope::Dump(const Optional& aString) const { - if (!nsContentUtils::DOMWindowDumpEnabled()) { + if (!DOMPrefs::DumpEnabled()) { return; } diff --git a/js/xpconnect/loader/mozJSComponentLoader.cpp b/js/xpconnect/loader/mozJSComponentLoader.cpp index f006db5c7aad..95a0fc448149 100644 --- a/js/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/xpconnect/loader/mozJSComponentLoader.cpp @@ -50,6 +50,7 @@ #include "mozilla/MacroForEach.h" #include "mozilla/Preferences.h" #include "mozilla/ScriptPreloader.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/ScriptSettings.h" #include "mozilla/UniquePtrExtensions.h" #include "mozilla/Unused.h" @@ -88,7 +89,7 @@ static LazyLogModule gJSCLLog("JSComponentLoader"); static bool Dump(JSContext* cx, unsigned argc, Value* vp) { - if (!nsContentUtils::DOMWindowDumpEnabled()) { + if (!mozilla::dom::DOMPrefs::DumpEnabled()) { return true; } diff --git a/js/xpconnect/src/Sandbox.cpp b/js/xpconnect/src/Sandbox.cpp index c47f71c5bd6b..3e78f6eebc3f 100644 --- a/js/xpconnect/src/Sandbox.cpp +++ b/js/xpconnect/src/Sandbox.cpp @@ -32,6 +32,7 @@ #include "mozilla/dom/cache/CacheStorage.h" #include "mozilla/dom/CSSBinding.h" #include "mozilla/dom/DirectoryBinding.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/IndexedDatabaseManager.h" #include "mozilla/dom/Fetch.h" #include "mozilla/dom/FileBinding.h" @@ -114,7 +115,7 @@ xpc::NewSandboxConstructor() static bool SandboxDump(JSContext* cx, unsigned argc, Value* vp) { - if (!nsContentUtils::DOMWindowDumpEnabled()) { + if (!DOMPrefs::DumpEnabled()) { return true; } diff --git a/js/xpconnect/src/XPCWrappedJSClass.cpp b/js/xpconnect/src/XPCWrappedJSClass.cpp index 65b47a413654..c9e0930e6bf5 100644 --- a/js/xpconnect/src/XPCWrappedJSClass.cpp +++ b/js/xpconnect/src/XPCWrappedJSClass.cpp @@ -9,7 +9,6 @@ #include "xpcprivate.h" #include "jsprf.h" #include "nsArrayEnumerator.h" -#include "nsContentUtils.h" #include "nsINamed.h" #include "nsIScriptError.h" #include "nsWrapperCache.h" @@ -20,6 +19,7 @@ #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/DOMException.h" #include "mozilla/dom/DOMExceptionBinding.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/jsipc/CrossProcessObjectWrappers.h" #include "jsapi.h" @@ -948,7 +948,7 @@ nsXPCWrappedJSClass::CheckForException(XPCCallContext & ccx, } if (reportable) { - if (nsContentUtils::DOMWindowDumpEnabled()) { + if (DOMPrefs::DumpEnabled()) { static const char line[] = "************************************************************\n"; static const char preamble[] = diff --git a/js/xpconnect/src/nsXPConnect.cpp b/js/xpconnect/src/nsXPConnect.cpp index 8236cd3f5d26..9e322effaf49 100644 --- a/js/xpconnect/src/nsXPConnect.cpp +++ b/js/xpconnect/src/nsXPConnect.cpp @@ -23,6 +23,7 @@ #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/DOMException.h" +#include "mozilla/dom/DOMPrefs.h" #include "mozilla/dom/Exceptions.h" #include "mozilla/dom/Promise.h" @@ -269,7 +270,7 @@ xpc::ErrorBase::AppendErrorDetailsTo(nsCString& error) void xpc::ErrorNote::LogToStderr() { - if (!nsContentUtils::DOMWindowDumpEnabled()) + if (!DOMPrefs::DumpEnabled()) return; nsAutoCString error; @@ -283,7 +284,7 @@ xpc::ErrorNote::LogToStderr() void xpc::ErrorReport::LogToStderr() { - if (!nsContentUtils::DOMWindowDumpEnabled()) + if (!DOMPrefs::DumpEnabled()) return; nsAutoCString error;