Bug 1218337 - Part 1 of 1: Introduced permission 'speech-recognition' and used it in place of the app-check. r=smaug
authorKelly Davis <kdavis@mozilla.com>
Fri, 06 Nov 2015 09:59:00 +0100
changeset 308022 8d8eb725a4d977944fa4a660d5405fbcb26beb6b
parent 308021 2b997fda5610dd8fd0af5011c719669de1662e9d
child 308023 7cd6a77d330ccc5a38c334b6a4503adc0f3a4d4d
push id7422
push userpehrsons@gmail.com
push dateWed, 11 Nov 2015 04:19:53 +0000
reviewerssmaug
bugs1218337
milestone45.0a1
Bug 1218337 - Part 1 of 1: Introduced permission 'speech-recognition' and used it in place of the app-check. r=smaug
dom/apps/PermissionsTable.jsm
dom/media/webspeech/recognition/SpeechRecognition.cpp
--- a/dom/apps/PermissionsTable.jsm
+++ b/dom/apps/PermissionsTable.jsm
@@ -111,16 +111,21 @@ this.PermissionsTable =  { geolocation: 
                              certified: ALLOW_ACTION,
                              access: ["read", "write", "create"]
                            },
                            sms: {
                              app: DENY_ACTION,
                              privileged: DENY_ACTION,
                              certified: ALLOW_ACTION
                            },
+                           "speech-recognition": {
+                             app: DENY_ACTION,
+                             privileged: ALLOW_ACTION,
+                             certified: ALLOW_ACTION
+                           },
                            telephony: {
                              app: DENY_ACTION,
                              privileged: DENY_ACTION,
                              certified: ALLOW_ACTION
                            },
                            browser: {
                              app: DENY_ACTION,
                              privileged: ALLOW_ACTION,
--- a/dom/media/webspeech/recognition/SpeechRecognition.cpp
+++ b/dom/media/webspeech/recognition/SpeechRecognition.cpp
@@ -16,18 +16,21 @@
 #include "mozilla/dom/MediaStreamError.h"
 #include "mozilla/MediaManager.h"
 #include "mozilla/Services.h"
 
 #include "AudioSegment.h"
 #include "endpointer.h"
 
 #include "mozilla/dom/SpeechRecognitionEvent.h"
+#include "nsContentUtils.h"
 #include "nsIDocument.h"
 #include "nsIObserverService.h"
+#include "nsIPermissionManager.h"
+#include "nsIPrincipal.h"
 #include "nsPIDOMWindow.h"
 #include "nsServiceManagerUtils.h"
 #include "nsQueryObject.h"
 
 #include <algorithm>
 
 // Undo the windows.h damage
 #if defined(XP_WIN) && defined(GetMessage)
@@ -155,21 +158,36 @@ JSObject*
 SpeechRecognition::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return SpeechRecognitionBinding::Wrap(aCx, this, aGivenProto);
 }
 
 bool
 SpeechRecognition::IsAuthorized(JSContext* aCx, JSObject* aGlobal)
 {
-  bool inPrivilegedApp = IsInPrivilegedApp(aCx, aGlobal);
+  nsCOMPtr<nsIPrincipal> principal = nsContentUtils::ObjectPrincipal(aGlobal);
+  
+  nsresult rv;
+  nsCOMPtr<nsIPermissionManager> mgr = do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return false;
+  }
+
+  uint32_t speechRecognition = nsIPermissionManager::UNKNOWN_ACTION;
+  rv = mgr->TestExactPermissionFromPrincipal(principal, "speech-recognition", &speechRecognition);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return false;
+  }
+
+  bool hasPermission = (speechRecognition == nsIPermissionManager::ALLOW_ACTION);
+
   bool enableTests = Preferences::GetBool(TEST_PREFERENCE_ENABLE);
   bool enableRecognitionEnable = Preferences::GetBool(TEST_PREFERENCE_RECOGNITION_ENABLE);
   bool enableRecognitionForceEnable = Preferences::GetBool(TEST_PREFERENCE_RECOGNITION_FORCE_ENABLE);
-  return (inPrivilegedApp || enableRecognitionForceEnable || enableTests) && enableRecognitionEnable;
+  return (hasPermission || enableRecognitionForceEnable || enableTests) && enableRecognitionEnable;
 }
 
 already_AddRefed<SpeechRecognition>
 SpeechRecognition::Constructor(const GlobalObject& aGlobal,
                                ErrorResult& aRv)
 {
   nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(aGlobal.GetAsSupports());
   if (!win) {