Bug 1284680 - Skip origin-checks for browser-chrome code so it may call getUserMedia again. draft
authorJan-Ivar Bruaroey <jib@mozilla.com>
Wed, 17 Aug 2016 19:47:19 -0400
changeset 402010 7c694505f47f7d44b3ddbf70701aaddeb5d6e882
parent 400825 054d4856cea6150a6638e5daf7913713281af97d
child 402403 8630603f176b789eaa2bc1a958a843eaca03dca0
push id26608
push userjbruaroey@mozilla.com
push dateWed, 17 Aug 2016 23:50:27 +0000
bugs1284680
milestone51.0a1
Bug 1284680 - Skip origin-checks for browser-chrome code so it may call getUserMedia again. MozReview-Commit-ID: Ka3kdT3LVvJ
dom/media/MediaManager.cpp
dom/media/MediaManager.h
dom/media/systemservices/CamerasParent.cpp
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -1960,27 +1960,16 @@ MediaManager::NotifyRecordingStatusChang
                                                                    requestURL,
                                                                    aIsAudio,
                                                                    aIsVideo);
   }
 
   return NS_OK;
 }
 
-bool MediaManager::IsPrivileged()
-{
-  bool permission = nsContentUtils::IsCallerChrome();
-
-  // Developer preference for turning off permission check.
-  if (Preferences::GetBool("media.navigator.permission.disabled", false)) {
-    permission = true;
-  }
-  return permission;
-}
-
 bool MediaManager::IsLoop(nsIURI* aDocURI)
 {
   MOZ_ASSERT(aDocURI);
 
   nsCOMPtr<nsIURI> loopURI;
   nsresult rv = NS_NewURI(getter_AddRefs(loopURI), "about:loopconversation");
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return false;
@@ -2069,17 +2058,19 @@ MediaManager::GetUserMedia(nsPIDOMWindow
 
   // Determine permissions early (while we still have a stack).
 
   nsIURI* docURI = aWindow->GetDocumentURI();
   if (!docURI) {
     return NS_ERROR_UNEXPECTED;
   }
   bool loop = IsLoop(docURI);
-  bool privileged = IsPrivileged();
+  bool isChrome = nsContentUtils::IsCallerChrome();
+  bool privileged = isChrome ||
+      Preferences::GetBool("media.navigator.permission.disabled", false);
   bool isHTTPS = false;
   docURI->SchemeIs("https", &isHTTPS);
   nsCString host;
   nsresult rv = docURI->GetHost(host);
   // Test for some other schemes that ServiceWorker recognizes
   bool isFile;
   docURI->SchemeIs("file", &isFile);
   bool isApp;
@@ -2112,19 +2103,21 @@ MediaManager::GetUserMedia(nsPIDOMWindow
     Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
                           (uint32_t) GetUserMediaSecurityState::Localhost);
   } else {
     Telemetry::Accumulate(Telemetry::WEBRTC_GET_USER_MEDIA_SECURE_ORIGIN,
                           (uint32_t) GetUserMediaSecurityState::Other);
   }
 
   nsCString origin;
-  rv = nsPrincipal::GetOriginForURI(docURI, origin);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
+  if (!isChrome) {
+    rv = nsPrincipal::GetOriginForURI(docURI, origin);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
   }
 
   if (!Preferences::GetBool("media.navigator.video.enabled", true)) {
     c.mVideo.SetAsBoolean() = false;
   }
 
   MediaSourceEnum videoType = MediaSourceEnum::Other; // none
   MediaSourceEnum audioType = MediaSourceEnum::Other; // none
--- a/dom/media/MediaManager.h
+++ b/dom/media/MediaManager.h
@@ -256,17 +256,16 @@ public:
 
   typedef nsTArray<RefPtr<MediaDevice>> SourceSet;
   static bool IsPrivateBrowsing(nsPIDOMWindowInner* window);
 private:
   typedef media::Pledge<SourceSet*, dom::MediaStreamError*> PledgeSourceSet;
   typedef media::Pledge<const char*, dom::MediaStreamError*> PledgeChar;
   typedef media::Pledge<bool, dom::MediaStreamError*> PledgeVoid;
 
-  static bool IsPrivileged();
   static bool IsLoop(nsIURI* aDocURI);
   static nsresult GenerateUUID(nsAString& aResult);
   static nsresult AnonymizeId(nsAString& aId, const nsACString& aOriginKey);
 public: // TODO: make private once we upgrade to GCC 4.8+ on linux.
   static void AnonymizeDevices(SourceSet& aDevices, const nsACString& aOriginKey);
   static already_AddRefed<nsIWritableVariant> ToJSArray(SourceSet& aDevices);
 private:
   already_AddRefed<PledgeSourceSet>
--- a/dom/media/systemservices/CamerasParent.cpp
+++ b/dom/media/systemservices/CamerasParent.cpp
@@ -663,16 +663,20 @@ GetPrincipalFromOrigin(const nsACString&
 }
 
 // Find out whether the given origin has permission to use the
 // camera. If the permission is not persistent, we'll make it
 // a one-shot by removing the (session) permission.
 static bool
 HasCameraPermission(const nsCString& aOrigin)
 {
+  // Treat lack of origin as coming from browser chrome code, and allow.
+  if (!aOrigin.Length()) {
+    return true;
+  }
   // Name used with nsIPermissionManager
   static const char* cameraPermission = "camera";
   bool allowed = false;
   bool permanent = false;
   nsresult rv;
   nsCOMPtr<nsIPermissionManager> mgr =
     do_GetService(NS_PERMISSIONMANAGER_CONTRACTID, &rv);
   if (NS_SUCCEEDED(rv)) {