Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / media_stream_devices_controller.cc
index dd9ca2c..d613661 100644 (file)
@@ -4,36 +4,31 @@
 
 #include "chrome/browser/media/media_stream_devices_controller.h"
 
-#include "base/command_line.h"
 #include "base/metrics/histogram.h"
-#include "base/prefs/pref_service.h"
 #include "base/prefs/scoped_user_pref_update.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/values.h"
-#include "chrome/browser/content_settings/content_settings_provider.h"
 #include "chrome/browser/content_settings/host_content_settings_map.h"
 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
 #include "chrome/browser/media/media_stream_capture_indicator.h"
+#include "chrome/browser/media/media_stream_device_permissions.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/common/chrome_switches.h"
-#include "chrome/common/content_settings.h"
-#include "chrome/common/content_settings_pattern.h"
 #include "chrome/common/pref_names.h"
+#include "chrome/grit/generated_resources.h"
+#include "components/content_settings/core/browser/content_settings_provider.h"
+#include "components/content_settings/core/common/content_settings.h"
+#include "components/content_settings/core/common/content_settings_pattern.h"
 #include "components/pref_registry/pref_registry_syncable.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/render_widget_host_view.h"
 #include "content/public/common/media_stream_request.h"
 #include "extensions/common/constants.h"
-#include "grit/generated_resources.h"
 #include "grit/theme_resources.h"
 #include "ui/base/l10n/l10n_util.h"
 
-#if defined(OS_CHROMEOS)
-#include "components/user_manager/user_manager.h"
-#endif
-
 using content::BrowserThread;
 
 namespace {
@@ -81,19 +76,6 @@ bool HasAvailableDevicesForRequest(const content::MediaStreamRequest& request) {
   return true;
 }
 
-bool IsInKioskMode() {
-  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKioskMode))
-    return true;
-
-#if defined(OS_CHROMEOS)
-  const user_manager::UserManager* user_manager =
-      user_manager::UserManager::Get();
-  return user_manager && user_manager->IsLoggedInAsKioskApp();
-#else
-  return false;
-#endif
-}
-
 enum DevicePermissionActions {
   kAllowHttps = 0,
   kAllowHttp,
@@ -134,7 +116,9 @@ MediaStreamDevicesController::MediaStreamDevicesController(
   // case take a ride on the MEDIA_DEVICE_*_CAPTURE permission. Should be fixed.
   if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE ||
       request.request_type == content::MEDIA_OPEN_DEVICE) {
-    if (GetDevicePolicy(prefs::kAudioCaptureAllowed,
+    if (GetDevicePolicy(profile_,
+                        request_.security_origin,
+                        prefs::kAudioCaptureAllowed,
                         prefs::kAudioCaptureAllowedUrls) == ALWAYS_DENY) {
       request_permissions_.insert(std::make_pair(
           content::MEDIA_DEVICE_AUDIO_CAPTURE,
@@ -149,7 +133,9 @@ MediaStreamDevicesController::MediaStreamDevicesController(
   }
   if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE ||
       request.request_type == content::MEDIA_OPEN_DEVICE) {
-    if (GetDevicePolicy(prefs::kVideoCaptureAllowed,
+    if (GetDevicePolicy(profile_,
+                        request_.security_origin,
+                        prefs::kVideoCaptureAllowed,
                         prefs::kVideoCaptureAllowedUrls) == ALWAYS_DENY) {
       request_permissions_.insert(std::make_pair(
           content::MEDIA_DEVICE_VIDEO_CAPTURE,
@@ -167,7 +153,7 @@ MediaStreamDevicesController::MediaStreamDevicesController(
 MediaStreamDevicesController::~MediaStreamDevicesController() {
   if (!callback_.is_null()) {
     callback_.Run(content::MediaStreamDevices(),
-                  content::MEDIA_DEVICE_INVALID_STATE,
+                  content::MEDIA_DEVICE_FAILED_DUE_TO_SHUTDOWN,
                   scoped_ptr<content::MediaStreamUI>());
   }
 }
@@ -456,52 +442,12 @@ void MediaStreamDevicesController::RequestFinished() {
   delete this;
 }
 
-MediaStreamDevicesController::DevicePolicy
-MediaStreamDevicesController::GetDevicePolicy(
-    const char* policy_name,
-    const char* whitelist_policy_name) const {
-  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-
-  // If the security origin policy matches a value in the whitelist, allow it.
-  // Otherwise, check the |policy_name| master switch for the default behavior.
-
-  PrefService* prefs = profile_->GetPrefs();
-
-  // TODO(tommi): Remove the kiosk mode check when the whitelist below
-  // is visible in the media exceptions UI.
-  // See discussion here: https://codereview.chromium.org/15738004/
-  if (IsInKioskMode()) {
-    const base::ListValue* list = prefs->GetList(whitelist_policy_name);
-    std::string value;
-    for (size_t i = 0; i < list->GetSize(); ++i) {
-      if (list->GetString(i, &value)) {
-        ContentSettingsPattern pattern =
-            ContentSettingsPattern::FromString(value);
-        if (pattern == ContentSettingsPattern::Wildcard()) {
-          DLOG(WARNING) << "Ignoring wildcard URL pattern: " << value;
-          continue;
-        }
-        DLOG_IF(ERROR, !pattern.IsValid()) << "Invalid URL pattern: " << value;
-        if (pattern.IsValid() && pattern.Matches(request_.security_origin))
-          return ALWAYS_ALLOW;
-      }
-    }
-  }
-
-  // If a match was not found, check if audio capture is otherwise disallowed
-  // or if the user should be prompted.  Setting the policy value to "true"
-  // is equal to not setting it at all, so from hereon out, we will return
-  // either POLICY_NOT_SET (prompt) or ALWAYS_DENY (no prompt, no access).
-  if (!prefs->GetBoolean(policy_name))
-    return ALWAYS_DENY;
-
-  return POLICY_NOT_SET;
-}
-
 bool MediaStreamDevicesController::IsRequestAllowedByDefault() const {
   // The request from internal objects like chrome://URLs is always allowed.
-  if (ShouldAlwaysAllowOrigin())
+  if (CheckAllowAllMediaStreamContentForOrigin(profile_,
+                                               request_.security_origin)) {
     return true;
+  }
 
   struct {
     bool has_capability;
@@ -520,8 +466,11 @@ bool MediaStreamDevicesController::IsRequestAllowedByDefault() const {
     if (!device_checks[i].has_capability)
       continue;
 
-    DevicePolicy policy = GetDevicePolicy(device_checks[i].policy_name,
-                                          device_checks[i].list_policy_name);
+    MediaStreamDevicePolicy policy =
+        GetDevicePolicy(profile_,
+                        request_.security_origin,
+                        device_checks[i].policy_name,
+                        device_checks[i].list_policy_name);
 
     if (policy == ALWAYS_DENY)
       return false;
@@ -595,15 +544,6 @@ bool MediaStreamDevicesController::IsSchemeSecure() const {
       request_.security_origin.SchemeIs(extensions::kExtensionScheme);
 }
 
-bool MediaStreamDevicesController::ShouldAlwaysAllowOrigin() const {
-  // TODO(markusheintz): Replace CONTENT_SETTINGS_TYPE_MEDIA_STREAM with the
-  // appropriate new CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC and
-  // CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA.
-  return profile_->GetHostContentSettingsMap()->ShouldAllowAllContent(
-      request_.security_origin, request_.security_origin,
-      CONTENT_SETTINGS_TYPE_MEDIASTREAM);
-}
-
 void MediaStreamDevicesController::SetPermission(bool allowed) const {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   ContentSettingsPattern primary_pattern =