Bug 1414935. Hide window.print entirely when it's disabled by pref. r=mystor
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 13 Nov 2017 10:38:50 -0500
changeset 391609 24a55985eeba01aeb5bab87ee1588bcb2b3811a5
parent 391608 c256e7ec78df07f25754538ce084304893528887
child 391610 a14612501cb6c198ed7f1e318a79f0452dcdaf01
push id97300
push userbzbarsky@mozilla.com
push dateTue, 14 Nov 2017 00:11:22 +0000
treeherdermozilla-inbound@24a55985eeba [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
@@ -17,22 +17,28 @@ https://bugzilla.mozilla.org/show_bug.cg
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 /** Test for Bug 739038 **/
 
 SimpleTest.waitForExplicitFinish();
 
 SpecialPowers.pushPrefEnv({"set": [["dom.disable_window_print", true]]}, function() {
-  var caughtException = false;
-  try {
-    window.print();
-  } catch (e) {
-    caughtException = true;
+  var ifr = document.createElement("iframe");
+  ifr.onload = function() {
+    var win = ifr.contentWindow;
+    var caughtException = false;
+    try {
+      win.print();
+    } catch (e) {
+      caughtException = true;
+    }
+    ok(caughtException, "window.print() should throw an exception");
+    is(win.print, undefined, "window.print shouldn't even exist");
+    SimpleTest.finish();
   }
-  ok(caughtException, "window.print should throw an exception");
-  SimpleTest.finish();
+  document.body.appendChild(ifr);
 });
 
 </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;