Bug 779384: Hook bluetooth up to permissions manager; r=mrbkap
authorKyle Machulis <kyle@nonpolynomial.com>
Tue, 07 Aug 2012 21:10:36 -0700
changeset 104890 8f7045786fbf27ace25c28e653313340420f43e2
parent 104889 ad4cd965a1a3ab38fe0cc20c5af46317dcc1b031
child 104891 9215610e05a1123175ea49dab834ba7d8e9ffd3f
push idunknown
push userunknown
push dateunknown
reviewersmrbkap
bugs779384
milestone17.0a1
Bug 779384: Hook bluetooth up to permissions manager; r=mrbkap
dom/bluetooth/BluetoothManager.cpp
--- a/dom/bluetooth/BluetoothManager.cpp
+++ b/dom/bluetooth/BluetoothManager.cpp
@@ -8,29 +8,26 @@
 #include "BluetoothManager.h"
 #include "BluetoothCommon.h"
 #include "BluetoothAdapter.h"
 #include "BluetoothService.h"
 #include "BluetoothTypes.h"
 #include "BluetoothReplyRunnable.h"
 
 #include "nsContentUtils.h"
-#include "mozilla/Preferences.h"
 #include "nsIDOMDOMRequest.h"
+#include "nsIPermissionManager.h"
 #include "nsDOMClassInfo.h"
 #include "nsDOMEvent.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOMCIDInternal.h"
 #include "mozilla/LazyIdleThread.h"
 #include "mozilla/Util.h"
 
-#define DOM_BLUETOOTH_URL_PREF "dom.mozBluetooth.whitelist"
-
 using namespace mozilla;
-using mozilla::Preferences;
 
 USING_BLUETOOTH_NAMESPACE
 
 DOMCI_DATA(BluetoothManager, BluetoothManager)
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(BluetoothManager)
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BluetoothManager,
@@ -271,30 +268,47 @@ BluetoothManager::Create(nsPIDOMWindow* 
 }
 
 nsresult
 NS_NewBluetoothManager(nsPIDOMWindow* aWindow,
                        nsIDOMBluetoothManager** aBluetoothManager)
 {
   NS_ASSERTION(aWindow, "Null pointer!");
 
-  bool allowed;
-  nsresult rv = nsContentUtils::IsOnPrefWhitelist(aWindow, DOM_BLUETOOTH_URL_PREF, &allowed);
+  nsPIDOMWindow* innerWindow = aWindow->IsInnerWindow() ?
+    aWindow :
+    aWindow->GetCurrentInnerWindow();
+
+  // Need the document for security check.
+  nsCOMPtr<nsIDocument> document = innerWindow->GetExtantDoc();
+  NS_ENSURE_TRUE(document, NS_NOINTERFACE);
+
+  nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
+  NS_ENSURE_TRUE(principal, NS_ERROR_UNEXPECTED);
+
+  nsCOMPtr<nsIPermissionManager> permMgr =
+    do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
+  NS_ENSURE_TRUE(permMgr, NS_ERROR_UNEXPECTED);
+
+  PRUint32 permission;
+  nsresult rv =
+    permMgr->TestPermissionFromPrincipal(principal, "mozBluetooth", &permission);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  if (!allowed) {
+  if (permission != nsIPermissionManager::ALLOW_ACTION) {
     *aBluetoothManager = nullptr;
     return NS_OK;
   }
 
   nsRefPtr<BluetoothManager> bluetoothManager = BluetoothManager::Create(aWindow);
   if (!bluetoothManager) {
     NS_ERROR("Cannot create bluetooth manager!");
     return NS_ERROR_FAILURE;
   }
+
   bluetoothManager.forget(aBluetoothManager);
   return NS_OK;
 }
 
 void
 BluetoothManager::Notify(const BluetoothSignal& aData)
 {
 #ifdef DEBUG