Bug 783500 - Hook up IdleAPI to permissions manager, r=jlebar
authorSteven Lee <slee@mozilla.com>
Tue, 02 Oct 2012 10:08:02 +0100
changeset 108991 c25274efa6f68beea223adb7b100beeced7728c2
parent 108990 dbc7e1bc48d0ca6e85fc0e15a8b0d3767d66c6be
child 108992 64fce23853dfa5b88b9b4be9c8fd0815d68cd26d
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersjlebar
bugs783500
milestone18.0a1
Bug 783500 - Hook up IdleAPI to permissions manager, r=jlebar
dom/base/Navigator.cpp
dom/base/Navigator.h
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -667,26 +667,18 @@ Navigator::AddIdleObserver(nsIIdleObserv
     return NS_OK;
   }
 
   NS_ENSURE_ARG_POINTER(aIdleObserver);
 
   nsCOMPtr<nsPIDOMWindow> win = do_QueryReferent(mWindow);
   NS_ENSURE_TRUE(win, NS_ERROR_UNEXPECTED);
 
-  nsCOMPtr<nsIDocument> doc = win->GetExtantDoc();
-  NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
-
-  nsIPrincipal* principal = doc->NodePrincipal();
-  if (!nsContentUtils::IsSystemPrincipal(principal)) {
-    uint16_t appStatus = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
-    principal->GetAppStatus(&appStatus);
-    if (appStatus != nsIPrincipal::APP_STATUS_CERTIFIED) {
-      return NS_ERROR_DOM_SECURITY_ERR;
-    }
+  if (!CheckPermission("idle")) {
+    return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   if (NS_FAILED(win->RegisterIdleObserver(aIdleObserver))) {
     NS_WARNING("Failed to add idle observer.");
   }
 
   return NS_OK;
 }
@@ -1161,24 +1153,17 @@ NS_IMETHODIMP
 Navigator::GetMozVoicemail(nsIDOMMozVoicemail** aVoicemail)
 {
   *aVoicemail = nullptr;
 
   if (!mVoicemail) {
     nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
     NS_ENSURE_TRUE(window, NS_OK);
 
-    nsCOMPtr<nsIPermissionManager> permMgr =
-      do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
-    NS_ENSURE_TRUE(permMgr, NS_OK);
-
-    uint32_t permission = nsIPermissionManager::DENY_ACTION;
-    permMgr->TestPermissionFromWindow(window, "voicemail", &permission);
-
-    if (permission != nsIPermissionManager::ALLOW_ACTION) {
+    if (!CheckPermission("voicemail")) {
       return NS_OK;
     }
 
     nsresult rv = NS_NewVoicemail(window, getter_AddRefs(mVoicemail));
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   NS_ADDREF(*aVoicemail = mVoicemail);
@@ -1216,24 +1201,17 @@ NS_IMETHODIMP
 Navigator::GetMozMobileConnection(nsIDOMMozMobileConnection** aMobileConnection)
 {
   *aMobileConnection = nullptr;
 
   if (!mMobileConnection) {
     nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
     NS_ENSURE_TRUE(window, NS_OK);
 
-    nsCOMPtr<nsIPermissionManager> permMgr =
-      do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
-    NS_ENSURE_TRUE(permMgr, NS_OK);
-
-    uint32_t permission = nsIPermissionManager::DENY_ACTION;
-    permMgr->TestPermissionFromWindow(window, "mobileconnection", &permission);
-
-    if (permission != nsIPermissionManager::ALLOW_ACTION) {
+    if (!CheckPermission("mobileconnection")) {
       return NS_OK;
     }
 
     mMobileConnection = new network::MobileConnection();
     mMobileConnection->Init(window);
   }
 
   NS_ADDREF(*aMobileConnection = mMobileConnection);
@@ -1329,30 +1307,18 @@ Navigator::MozSetMessageHandler(const ns
 
 //*****************************************************************************
 //    Navigator::nsIDOMNavigatorTime
 //*****************************************************************************
 NS_IMETHODIMP
 Navigator::GetMozTime(nsIDOMMozTimeManager** aTime)
 {
   *aTime = nullptr;
-  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
-  NS_ENSURE_TRUE(window, NS_OK);
 
-  nsCOMPtr<nsIDocument> document = do_QueryInterface(window->GetExtantDocument());
-  NS_ENSURE_TRUE(document, NS_OK);
-  nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
-  nsCOMPtr<nsIPermissionManager> permMgr =
-    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
-  NS_ENSURE_TRUE(permMgr, NS_OK);
-  
-  uint32_t permission = nsIPermissionManager::DENY_ACTION;
-  permMgr->TestPermissionFromPrincipal(principal, "time", &permission);
-
-  if (permission != nsIPermissionManager::ALLOW_ACTION) {
+  if (!CheckPermission("time")) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   if (!mTimeManager) {
     mTimeManager = new time::TimeManager();
   }
 
   NS_ADDREF(*aTime = mTimeManager);
@@ -1418,16 +1384,31 @@ Navigator::OnNavigation()
   MediaManager *manager = MediaManager::Get();
   manager->OnNavigation(win->WindowID());
 #endif
   if (mCameraManager) {
     mCameraManager->OnNavigation(win->WindowID());
   }
 }
 
+bool
+Navigator::CheckPermission(const char* type)
+{
+  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
+  NS_ENSURE_TRUE(window, false);
+
+  nsCOMPtr<nsIPermissionManager> permMgr =
+    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
+  NS_ENSURE_TRUE(permMgr, false);
+
+  uint32_t permission = nsIPermissionManager::DENY_ACTION;
+  permMgr->TestPermissionFromWindow(window, type, &permission);
+  return permission == nsIPermissionManager::ALLOW_ACTION;
+}
+
 } // namespace dom
 } // namespace mozilla
 
 nsresult
 NS_GetNavigatorUserAgent(nsAString& aUserAgent)
 {
   nsresult rv;
 
--- a/dom/base/Navigator.h
+++ b/dom/base/Navigator.h
@@ -157,16 +157,18 @@ public:
 
 #ifdef MOZ_SYS_MSG
   // Helper to initialize mMessagesManager.
   nsresult EnsureMessagesManager();
 #endif
   NS_DECL_NSIDOMNAVIGATORCAMERA
 
 private:
+  bool CheckPermission(const char* type);
+
   nsRefPtr<nsMimeTypeArray> mMimeTypes;
   nsRefPtr<nsPluginArray> mPlugins;
   nsRefPtr<nsGeolocation> mGeolocation;
   nsRefPtr<nsDesktopNotificationCenter> mNotification;
   nsRefPtr<battery::BatteryManager> mBatteryManager;
   nsRefPtr<power::PowerManager> mPowerManager;
   nsRefPtr<sms::SmsManager> mSmsManager;
 #ifdef MOZ_B2G_RIL