| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "chrome/browser/media/media_permission.h" | |
| 6 | |
| 7 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | |
| 8 #include "chrome/browser/media/media_stream_device_permission_context.h" | |
| 9 #include "chrome/browser/media/media_stream_device_permissions.h" | |
| 10 #include "chrome/browser/permissions/permission_context_base.h" | |
| 11 #include "chrome/browser/permissions/permission_manager.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "content/public/browser/permission_manager.h" | |
| 15 #include "content/public/browser/permission_type.h" | |
| 16 #include "content/public/common/url_constants.h" | |
| 17 #include "extensions/common/constants.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 content::PermissionType ContentSettingsTypeToPermission( | |
| 22 ContentSettingsType content_setting) { | |
| 23 if (content_setting == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) { | |
| 24 return content::PermissionType::AUDIO_CAPTURE; | |
| 25 } else { | |
| 26 DCHECK_EQ(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, content_setting); | |
| 27 return content::PermissionType::VIDEO_CAPTURE; | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 MediaPermission::MediaPermission(ContentSettingsType content_type, | |
| 34 const GURL& requesting_origin, | |
| 35 const GURL& embedding_origin, | |
| 36 Profile* profile) | |
| 37 : content_type_(content_type), | |
| 38 requesting_origin_(requesting_origin), | |
| 39 embedding_origin_(embedding_origin), | |
| 40 profile_(profile) {} | |
| 41 | |
| 42 ContentSetting MediaPermission::GetPermissionStatus( | |
| 43 content::MediaStreamRequestResult* denial_reason) const { | |
| 44 // Deny the request if the security origin is empty, this happens with | |
| 45 // file access without |--allow-file-access-from-files| flag. | |
| 46 if (requesting_origin_.is_empty()) { | |
| 47 *denial_reason = content::MEDIA_DEVICE_INVALID_SECURITY_ORIGIN; | |
| 48 return CONTENT_SETTING_BLOCK; | |
| 49 } | |
| 50 | |
| 51 // Use the Permission Context to find out if the kill switch is on. Set the | |
| 52 // denial reason to kill switch. | |
| 53 content::PermissionType permission_type = | |
| 54 ContentSettingsTypeToPermission(content_type_); | |
| 55 // TODO(raymes): This calls into GetPermissionContext which is a private | |
| 56 // member of PermissionManager. Remove this call when this class is refactored | |
| 57 // into a PermissionContext. See crbug.com/596786. | |
| 58 PermissionContextBase* permission_context = | |
| 59 PermissionManager::Get(profile_)->GetPermissionContext(permission_type); | |
| 60 | |
| 61 if (!permission_context) { | |
| 62 *denial_reason = content::MEDIA_DEVICE_PERMISSION_DENIED; | |
| 63 return CONTENT_SETTING_BLOCK; | |
| 64 } | |
| 65 | |
| 66 MediaStreamDevicePermissionContext* media_device_permission_context = | |
| 67 static_cast<MediaStreamDevicePermissionContext*>(permission_context); | |
| 68 | |
| 69 if (media_device_permission_context->IsPermissionKillSwitchOn()) { | |
| 70 *denial_reason = content::MEDIA_DEVICE_KILL_SWITCH_ON; | |
| 71 return CONTENT_SETTING_BLOCK; | |
| 72 } | |
| 73 | |
| 74 // Check policy and content settings. | |
| 75 ContentSetting result = | |
| 76 GetStoredContentSetting(media_device_permission_context); | |
| 77 if (result == CONTENT_SETTING_BLOCK) | |
| 78 *denial_reason = content::MEDIA_DEVICE_PERMISSION_DENIED; | |
| 79 | |
| 80 return result; | |
| 81 } | |
| 82 | |
| 83 ContentSetting MediaPermission::GetPermissionStatusWithDeviceRequired( | |
| 84 const std::string& device_id, | |
| 85 content::MediaStreamRequestResult* denial_reason) const { | |
| 86 // Deny the request if there is no device attached to the OS of the requested | |
| 87 // type. | |
| 88 if (!HasAvailableDevices(device_id)) { | |
| 89 *denial_reason = content::MEDIA_DEVICE_NO_HARDWARE; | |
| 90 return CONTENT_SETTING_BLOCK; | |
| 91 } | |
| 92 | |
| 93 return GetPermissionStatus(denial_reason); | |
| 94 } | |
| 95 | |
| 96 ContentSetting MediaPermission::GetStoredContentSetting( | |
| 97 MediaStreamDevicePermissionContext* media_device_permission_context) const { | |
| 98 return media_device_permission_context->GetPermissionStatus( | |
| 99 requesting_origin_, embedding_origin_); | |
| 100 } | |
| 101 | |
| 102 bool MediaPermission::HasAvailableDevices(const std::string& device_id) const { | |
| 103 const content::MediaStreamDevices* devices = nullptr; | |
| 104 if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) { | |
| 105 devices = | |
| 106 &MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices(); | |
| 107 } else if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) { | |
| 108 devices = | |
| 109 &MediaCaptureDevicesDispatcher::GetInstance()->GetVideoCaptureDevices(); | |
| 110 } else { | |
| 111 NOTREACHED(); | |
| 112 } | |
| 113 | |
| 114 // TODO(tommi): It's kind of strange to have this here since if we fail this | |
| 115 // test, there'll be a UI shown that indicates to the user that access to | |
| 116 // non-existing audio/video devices has been denied. The user won't have | |
| 117 // any way to change that but there will be a UI shown which indicates that | |
| 118 // access is blocked. | |
| 119 if (devices->empty()) | |
| 120 return false; | |
| 121 | |
| 122 // Note: we check device_id before dereferencing devices. If the requested | |
| 123 // device id is non-empty, then the corresponding device list must not be | |
| 124 // NULL. | |
| 125 if (!device_id.empty() && !devices->FindById(device_id)) | |
| 126 return false; | |
| 127 | |
| 128 return true; | |
| 129 } | |
| OLD | NEW |