Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/media/media_devices_permission_checker.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "content/browser/frame_host/render_frame_host_delegate.h" | |
| 13 #include "content/browser/frame_host/render_frame_host_impl.h" | |
| 14 #include "content/common/media/media_devices.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/render_process_host.h" | |
| 18 #include "content/public/common/content_switches.h" | |
| 19 #include "url/gurl.h" | |
| 20 #include "url/origin.h" | |
| 21 | |
| 22 namespace content { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 MediaDevicesManager::BoolDeviceTypes CheckPermissionsOnUIThread( | |
| 27 MediaDevicesManager::BoolDeviceTypes requested_device_types, | |
| 28 int render_process_id, | |
| 29 int routing_id, | |
| 30 const url::Origin& security_origin) { | |
| 31 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 32 RenderFrameHostImpl* frame_host = | |
| 33 RenderFrameHostImpl::FromID(render_process_id, routing_id); | |
| 34 | |
| 35 // If there is no |frame_host|, return false for all permissions. | |
| 36 if (!frame_host) | |
| 37 return MediaDevicesManager::BoolDeviceTypes(); | |
| 38 | |
| 39 RenderFrameHostDelegate* delegate = frame_host->delegate(); | |
| 40 GURL origin = security_origin.GetURL(); | |
| 41 | |
| 42 // Currently, the MEDIA_DEVICE_AUDIO_CAPTURE permission is used for | |
| 43 // both audio input and output. | |
| 44 // TODO(guidou): use specific permission for audio output when it becomes | |
| 45 // available. See http://crbug.com/556542. | |
| 46 bool has_audio_permission = | |
| 47 (requested_device_types[MEDIA_DEVICE_TYPE_AUDIO_INPUT] || | |
| 48 requested_device_types[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT]) && | |
| 49 delegate->CheckMediaAccessPermission(origin, MEDIA_DEVICE_AUDIO_CAPTURE); | |
| 50 | |
| 51 MediaDevicesManager::BoolDeviceTypes result; | |
| 52 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT] = has_audio_permission; | |
| 53 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT] = has_audio_permission; | |
| 54 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT] = | |
| 55 requested_device_types[MEDIA_DEVICE_TYPE_VIDEO_INPUT] && | |
| 56 delegate->CheckMediaAccessPermission(origin, MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 57 | |
| 58 return result; | |
| 59 } | |
| 60 | |
| 61 bool CheckSinglePermissionOnUIThread(MediaDeviceType device_type, | |
| 62 int render_process_id, | |
| 63 int routing_id, | |
| 64 const url::Origin& security_origin) { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 66 MediaDevicesManager::BoolDeviceTypes requested; | |
| 67 requested[device_type] = true; | |
| 68 MediaDevicesManager::BoolDeviceTypes result = CheckPermissionsOnUIThread( | |
| 69 requested, render_process_id, routing_id, security_origin); | |
| 70 return result[device_type]; | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 MediaDevicesPermissionChecker::MediaDevicesPermissionChecker() | |
| 76 : use_override_(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 77 switches::kUseFakeUIForMediaStream)), | |
| 78 override_value_( | |
| 79 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 80 switches::kUseFakeUIForMediaStream) != "deny") {} | |
| 81 | |
| 82 bool MediaDevicesPermissionChecker::CheckPermission( | |
| 83 MediaDeviceType device_type, | |
| 84 int render_process_id, | |
| 85 int routing_id, | |
| 86 const url::Origin& security_origin) { | |
| 87 if (use_override_) | |
| 88 return override_value_; | |
| 89 | |
| 90 return CheckSinglePermissionOnUIThread(device_type, render_process_id, | |
| 91 routing_id, security_origin); | |
| 92 } | |
| 93 | |
| 94 void MediaDevicesPermissionChecker::CheckPermission( | |
| 95 MediaDeviceType device_type, | |
| 96 int render_process_id, | |
| 97 int routing_id, | |
| 98 const url::Origin& security_origin, | |
| 99 const base::Callback<void(bool)>& callback) { | |
| 100 if (use_override_) { | |
| 101 callback.Run(override_value_); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 BrowserThread::PostTaskAndReplyWithResult( | |
| 106 BrowserThread::UI, FROM_HERE, | |
| 107 base::Bind(&CheckSinglePermissionOnUIThread, device_type, | |
| 108 render_process_id, routing_id, security_origin), | |
| 109 callback); | |
| 110 } | |
| 111 | |
| 112 MediaDevicesManager::BoolDeviceTypes | |
| 113 MediaDevicesPermissionChecker::CheckPermissions( | |
| 114 MediaDevicesManager::BoolDeviceTypes requested_device_types, | |
| 115 int render_process_id, | |
| 116 int routing_id, | |
| 117 const url::Origin& security_origin) { | |
| 118 if (use_override_) { | |
| 119 MediaDevicesManager::BoolDeviceTypes result; | |
| 120 result.fill(override_value_); | |
| 121 return result; | |
| 122 } | |
| 123 | |
| 124 return CheckPermissionsOnUIThread(requested_device_types, render_process_id, | |
| 125 routing_id, security_origin); | |
| 126 } | |
| 127 | |
| 128 void MediaDevicesPermissionChecker::CheckPermissions( | |
| 129 MediaDevicesManager::BoolDeviceTypes requested, | |
| 130 int render_process_id, | |
| 131 int routing_id, | |
| 132 const url::Origin& security_origin, | |
| 133 const base::Callback<void(const MediaDevicesManager::BoolDeviceTypes&)>& | |
| 134 callback) { | |
| 135 if (use_override_) { | |
| 136 MediaDevicesManager::BoolDeviceTypes result; | |
| 137 result.fill(override_value_); | |
| 138 callback.Run(result); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 BrowserThread::PostTaskAndReplyWithResult( | |
| 143 BrowserThread::UI, FROM_HERE, | |
| 144 base::Bind(&CheckPermissionsOnUIThread, requested, render_process_id, | |
| 145 routing_id, security_origin), | |
| 146 callback); | |
| 147 } | |
| 148 | |
| 149 void MediaDevicesPermissionChecker::OverridePermissions(bool override_value) { | |
| 150 use_override_ = true; | |
|
tommi (sloooow) - chröme
2016/10/28 09:36:52
nit: would it make sense to use Optional?
Guido Urdaneta
2016/10/28 10:06:55
I would work, but I think the code would become le
| |
| 151 override_value_ = override_value; | |
| 152 } | |
| 153 | |
| 154 } // namespace content | |
| OLD | NEW |