Bug 1414935. Hide window.print entirely when it's disabled by pref. r=mystor
☠☠ backed out by 0fb0e62aadff ☠ ☠
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 13 Nov 2017 10:38:50 -0500
changeset 391508 b9aa0fc4f179dbe92419b3b153df170bd1eb697d
parent 391507 9ef61399999ef17a696071735db2fbab8c4bf502
child 391509 fc56f1bc49b2d15ee229009bc11a46f041fc9e42
push id97288
push userbzbarsky@mozilla.com
push dateMon, 13 Nov 2017 15:49:35 +0000
treeherdermozilla-inbound@01a5361e9460 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmystor
bugs1414935
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1414935. Hide window.print entirely when it's disabled by pref. r=mystor MozReview-Commit-ID: ArDw85eQYeh
dom/base/nsGlobalWindow.cpp
dom/base/nsGlobalWindow.h
dom/tests/mochitest/bugs/test_bug739038.html
dom/webidl/Window.webidl
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -8110,27 +8110,35 @@ nsGlobalWindow::StopOuter(ErrorResult& a
 }
 
 void
 nsGlobalWindow::Stop(ErrorResult& aError)
 {
   FORWARD_TO_OUTER_OR_THROW(StopOuter, (aError), aError, );
 }
 
+/* static */
+bool
+nsGlobalWindow::IsWindowPrintEnabled(JSContext*, JSObject*)
+{
+  static bool called = false;
+  static bool printDisabled = false;
+  if (!called) {
+    called = true;
+    Preferences::AddBoolVarCache(&printDisabled, "dom.disable_window_print");
+  }
+  return !printDisabled;
+}
+
 void
 nsGlobalWindow::PrintOuter(ErrorResult& aError)
 {
   MOZ_RELEASE_ASSERT(IsOuterWindow());
 
 #ifdef NS_PRINTING
-  if (Preferences::GetBool("dom.disable_window_print", false)) {
-    aError.Throw(NS_ERROR_NOT_AVAILABLE);
-    return;
-  }
-
   if (!AreDialogsEnabled()) {
     // We probably want to keep throwing here; silently doing nothing is a bit
     // weird given the typical use cases of print().
     aError.Throw(NS_ERROR_NOT_AVAILABLE);
     return;
   }
 
   if (ShouldPromptToBlockDialogs() && !ConfirmDialogIfNeeded()) {
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -451,16 +451,18 @@ public:
   // WebIDL interface.
   already_AddRefed<nsPIDOMWindowOuter> IndexedGetterOuter(uint32_t aIndex);
   already_AddRefed<nsPIDOMWindowOuter> IndexedGetter(uint32_t aIndex);
 
   static bool IsPrivilegedChromeWindow(JSContext* /* unused */, JSObject* aObj);
 
   static bool IsRequestIdleCallbackEnabled(JSContext* aCx, JSObject* /* unused */);
 
+  static bool IsWindowPrintEnabled(JSContext* /* unused */, JSObject* /* unused */);
+
   bool DoResolve(JSContext* aCx, JS::Handle<JSObject*> aObj,
                  JS::Handle<jsid> aId,
                  JS::MutableHandle<JS::PropertyDescriptor> aDesc);
   // The return value is whether DoResolve might end up resolving the given id.
   // If in doubt, return true.
   static bool MayResolve(jsid aId);
 
   void GetOwnPropertyNames(JSContext* aCx, JS::AutoIdVector& aNames,
--- a/dom/tests/mochitest/bugs/test_bug739038.html
+++ b/dom/tests/mochitest/bugs/test_bug739038.html
@@ -23,16 +23,17 @@ SimpleTest.waitForExplicitFinish();
 
 SpecialPowers.pushPrefEnv({"set": [["dom.disable_window_print", true]]}, function() {
   var caughtException = false;
   try {
     window.print();
   } catch (e) {
     caughtException = true;
   }
-  ok(caughtException, "window.print should throw an exception");
+  ok(caughtException, "window.print() should throw an exception");
+  is(window.print, undefined, "window.print shouldn't even exist");
   SimpleTest.finish();
 });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/webidl/Window.webidl
+++ b/dom/webidl/Window.webidl
@@ -74,17 +74,18 @@ interface XULControllers;
 #endif
   [Throws, Pref="browser.cache.offline.enable"] readonly attribute ApplicationCache applicationCache;
 
   // user prompts
   [Throws, UnsafeInPrerendering, NeedsSubjectPrincipal] void alert();
   [Throws, UnsafeInPrerendering, NeedsSubjectPrincipal] void alert(DOMString message);
   [Throws, UnsafeInPrerendering, NeedsSubjectPrincipal] boolean confirm(optional DOMString message = "");
   [Throws, UnsafeInPrerendering, NeedsSubjectPrincipal] DOMString? prompt(optional DOMString message = "", optional DOMString default = "");
-  [Throws, UnsafeInPrerendering] void print();
+  [Throws, UnsafeInPrerendering, Func="nsGlobalWindow::IsWindowPrintEnabled"]
+  void print();
 
   [Throws, CrossOriginCallable, NeedsSubjectPrincipal]
   void postMessage(any message, DOMString targetOrigin, optional sequence<object> transfer = []);
 
   // also has obsolete members
 };
 Window implements GlobalEventHandlers;
 Window implements WindowEventHandlers;