Bug 884897 - Add permission check to navigator.push load. r=bz
authorNikhil Marathe <nsm.nikhil@gmail.com>
Wed, 07 Aug 2013 08:04:10 -0700
changeset 154600 7583b9cff5401582fadeca7c269baef4bdb9e105
parent 154599 f33f0ea6bba346f612528d1a7b514973e4fb01b5
child 154601 d10581fd820204c09e6124a1d53e4bff9a2fd88d
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs884897
milestone26.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 884897 - Add permission check to navigator.push load. r=bz
dom/base/Navigator.cpp
dom/base/Navigator.h
dom/push/src/Push.js
dom/webidl/PushManager.webidl
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -1732,16 +1732,24 @@ bool Navigator::HasUserMediaSupport(JSCo
 {
   // Make enabling peerconnection enable getUserMedia() as well
   return Preferences::GetBool("media.navigator.enabled", false) ||
          Preferences::GetBool("media.peerconnection.enabled", false);
 }
 #endif // MOZ_MEDIA_NAVIGATOR
 
 /* static */
+bool Navigator::HasPushNotificationsSupport(JSContext* /* unused */,
+                                            JSObject* aGlobal)
+{
+  nsCOMPtr<nsPIDOMWindow> win = GetWindowFromGlobal(aGlobal);
+  return win && Preferences::GetBool("services.push.enabled", false) && CheckPermission(win, "push");
+}
+
+/* static */
 already_AddRefed<nsPIDOMWindow>
 Navigator::GetWindowFromGlobal(JSObject* aGlobal)
 {
   nsCOMPtr<nsPIDOMWindow> win =
     do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(aGlobal));
   MOZ_ASSERT(!win || win->IsInnerWindow());
   return win.forget();
 }
--- a/dom/base/Navigator.h
+++ b/dom/base/Navigator.h
@@ -278,16 +278,19 @@ public:
 #ifdef MOZ_TIME_MANAGER
   static bool HasTimeSupport(JSContext* /* unused */, JSObject* aGlobal);
 #endif // MOZ_TIME_MANAGER
 #ifdef MOZ_MEDIA_NAVIGATOR
   static bool HasUserMediaSupport(JSContext* /* unused */,
                                   JSObject* /* unused */);
 #endif // MOZ_MEDIA_NAVIGATOR
 
+  static bool HasPushNotificationsSupport(JSContext* /* unused */,
+                                          JSObject* aGlobal);
+
   nsPIDOMWindow* GetParentObject() const
   {
     return GetWindow();
   }
 
   virtual JSObject* WrapObject(JSContext* cx,
                                JS::Handle<JSObject*> scope) MOZ_OVERRIDE;
 
--- a/dom/push/src/Push.js
+++ b/dom/push/src/Push.js
@@ -37,36 +37,21 @@ Push.prototype = {
 
   QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
                                           Ci.nsISupportsWeakReference]),
 
   init: function(aWindow) {
     debug("init()");
 
     let principal = aWindow.document.nodePrincipal;
-
-    this._pageURL = principal.URI;
-
     let appsService = Cc["@mozilla.org/AppsService;1"]
                         .getService(Ci.nsIAppsService);
-    this._app = appsService.getAppByLocalId(principal.appId);
+
     this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
-    if (!this._manifestURL) {
-	// Now what?  XXXbz should this be tested in a Func for this
-	// interface so it wouldn't appear at all?
-	return;
-    }
-
-    let perm = Services.perms.testExactPermissionFromPrincipal(principal,
-                                                               "push");
-    if (perm != Ci.nsIPermissionManager.ALLOW_ACTION) {
-	// Now what?  XXXbz should this be tested in a Func for this
-	// interface so it wouldn't appear at all?
-	return;
-    }
+    this._pageURL = principal.URI;
 
     this.initDOMRequestHelper(aWindow, [
       "PushService:Register:OK",
       "PushService:Register:KO",
       "PushService:Unregister:OK",
       "PushService:Unregister:KO",
       "PushService:Registrations:OK",
       "PushService:Registrations:KO"
--- a/dom/webidl/PushManager.webidl
+++ b/dom/webidl/PushManager.webidl
@@ -1,12 +1,12 @@
 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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/.
 */
 
-[NoInterfaceObject, NavigatorProperty="push", JSImplementation="@mozilla.org/push/PushManager;1", Pref="services.push.enabled"]
+[NoInterfaceObject, NavigatorProperty="push", JSImplementation="@mozilla.org/push/PushManager;1", Func="Navigator::HasPushNotificationsSupport"]
 interface PushManager {
     DOMRequest register();
     DOMRequest unregister(DOMString pushEndpoint);
     DOMRequest registrations();
 };