Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: chrome/browser/media/media_permission.cc

Issue 1318173002: Integrate MediaPermission with PermissionManager by using PermissionContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/media/media_permission.h" 5 #include "chrome/browser/media/media_permission.h"
6 6
7 #include "chrome/browser/media/media_capture_devices_dispatcher.h" 7 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
8 #include "chrome/browser/media/media_stream_device_permission_context.h"
8 #include "chrome/browser/media/media_stream_device_permissions.h" 9 #include "chrome/browser/media/media_stream_device_permissions.h"
10 #include "chrome/browser/permissions/permission_context.h"
11 #include "chrome/browser/permissions/permission_context_base.h"
9 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h" 14 #include "content/public/browser/permission_manager.h"
15 #include "content/public/browser/permission_type.h"
12 #include "content/public/common/url_constants.h" 16 #include "content/public/common/url_constants.h"
13 #include "extensions/common/constants.h" 17 #include "extensions/common/constants.h"
14 18
19 namespace {
20
21 content::PermissionType ContentSettingsTypeToPermission(
22 ContentSettingsType content_setting) {
23 switch (content_setting) {
24 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
25 return content::PermissionType::AUDIO_CAPTURE;
26 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
27 return content::PermissionType::VIDEO_CAPTURE;
28 default:
29 // This will hit the NOTREACHED below.
30 break;
31 }
raymes 2015/09/02 04:15:33 Can you make this not a switch statement? I'm look
guoweis_left_chromium 2015/09/02 05:54:11 Done.
32
33 NOTREACHED() << "Unexpected content setting for permission "
34 << static_cast<int>(content_setting);
35 return content::PermissionType::NUM;
36 }
37
38 } // namespace
39
15 MediaPermission::MediaPermission(ContentSettingsType content_type, 40 MediaPermission::MediaPermission(ContentSettingsType content_type,
16 content::MediaStreamRequestType request_type, 41 content::MediaStreamRequestType request_type,
17 const GURL& origin, 42 const GURL& requesting_origin,
43 const GURL& embedding_origin,
18 Profile* profile) 44 Profile* profile)
19 : content_type_(content_type), 45 : content_type_(content_type),
20 request_type_(request_type), 46 request_type_(request_type),
21 origin_(origin), 47 requesting_origin_(requesting_origin),
22 profile_(profile) { 48 embedding_origin_(embedding_origin),
23 } 49 profile_(profile) {}
24 50
25 ContentSetting MediaPermission::GetPermissionStatus( 51 ContentSetting MediaPermission::GetPermissionStatus(
26 content::MediaStreamRequestResult* denial_reason) const { 52 content::MediaStreamRequestResult* denial_reason) const {
27 // Deny the request if the security origin is empty, this happens with 53 // Deny the request if the security origin is empty, this happens with
28 // file access without |--allow-file-access-from-files| flag. 54 // file access without |--allow-file-access-from-files| flag.
29 if (origin_.is_empty()) { 55 if (requesting_origin_.is_empty()) {
30 *denial_reason = content::MEDIA_DEVICE_INVALID_SECURITY_ORIGIN; 56 *denial_reason = content::MEDIA_DEVICE_INVALID_SECURITY_ORIGIN;
31 return CONTENT_SETTING_BLOCK; 57 return CONTENT_SETTING_BLOCK;
32 } 58 }
33 59
34 // Check policy and content settings. 60 // Check policy and content settings.
35 ContentSetting result = GetStoredContentSetting(); 61 ContentSetting result = GetStoredContentSetting();
36 if (result == CONTENT_SETTING_BLOCK) 62 if (result == CONTENT_SETTING_BLOCK)
37 *denial_reason = content::MEDIA_DEVICE_PERMISSION_DENIED; 63 *denial_reason = content::MEDIA_DEVICE_PERMISSION_DENIED;
38 64
39 return result; 65 return result;
40 } 66 }
41 67
42 ContentSetting MediaPermission::GetPermissionStatusWithDeviceRequired( 68 ContentSetting MediaPermission::GetPermissionStatusWithDeviceRequired(
43 const std::string& device_id, 69 const std::string& device_id,
44 content::MediaStreamRequestResult* denial_reason) const { 70 content::MediaStreamRequestResult* denial_reason) const {
45 // Deny the request if there is no device attached to the OS of the requested 71 // Deny the request if there is no device attached to the OS of the requested
46 // type. 72 // type.
47 if (!HasAvailableDevices(device_id)) { 73 if (!HasAvailableDevices(device_id)) {
48 *denial_reason = content::MEDIA_DEVICE_NO_HARDWARE; 74 *denial_reason = content::MEDIA_DEVICE_NO_HARDWARE;
49 return CONTENT_SETTING_BLOCK; 75 return CONTENT_SETTING_BLOCK;
50 } 76 }
51 77
52 return GetPermissionStatus(denial_reason); 78 return GetPermissionStatus(denial_reason);
53 } 79 }
54 80
55 ContentSetting MediaPermission::GetStoredContentSetting() const { 81 ContentSetting MediaPermission::GetStoredContentSetting() const {
56 // TODO(raymes): Merge this policy check into content settings 82 content::PermissionType permission_type =
57 // crbug.com/244389. 83 ContentSettingsTypeToPermission(content_type_);
58 const char* policy_name = nullptr; 84 PermissionContextBase* permission_context =
59 const char* urls_policy_name = nullptr; 85 PermissionContext::Get(profile_, permission_type);
60 if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
61 policy_name = prefs::kAudioCaptureAllowed;
62 urls_policy_name = prefs::kAudioCaptureAllowedUrls;
63 } else if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
64 policy_name = prefs::kVideoCaptureAllowed;
65 urls_policy_name = prefs::kVideoCaptureAllowedUrls;
66 } else {
67 NOTREACHED();
68 }
69 86
70 MediaStreamDevicePolicy policy = 87 if (!permission_context)
71 GetDevicePolicy(profile_, origin_, policy_name, urls_policy_name);
72
73 if (policy == ALWAYS_DENY)
74 return CONTENT_SETTING_BLOCK; 88 return CONTENT_SETTING_BLOCK;
75 89
76 if (policy == ALWAYS_ALLOW) 90 MediaStreamDevicePermissionContext* media_device_permission_context =
77 return CONTENT_SETTING_ALLOW; 91 static_cast<MediaStreamDevicePermissionContext*>(permission_context);
78 92
79 DCHECK(policy == POLICY_NOT_SET); 93 if (request_type_ == content::MEDIA_OPEN_DEVICE) {
80 // Check the content setting. 94 return media_device_permission_context->GetPermissionStatusForPepper(
81 ContentSetting setting = 95 requesting_origin_, embedding_origin_);
82 profile_->GetHostContentSettingsMap()->GetContentSetting( 96 } else {
83 origin_, origin_, content_type_, 97 return media_device_permission_context->GetPermissionStatus(
84 content_settings::ResourceIdentifier()); 98 requesting_origin_, embedding_origin_);
85
86 if (setting == CONTENT_SETTING_DEFAULT)
87 return CONTENT_SETTING_ASK;
88
89 // TODO(raymes): This is here for safety to ensure that we always ask the user
90 // even if a content setting is set to "allow" if the origin is insecure. In
91 // reality we shouldn't really need to check this here as we should respect
92 // the user's content setting. The problem is that pepper requests allow
93 // insecure origins to be persisted. We should stop allowing this, do some
94 // sort of migration and remove this check. See crbug.com/512301.
95 if (!ShouldPersistContentSetting(setting, origin_, request_type_) &&
96 !origin_.SchemeIs(extensions::kExtensionScheme) &&
97 !origin_.SchemeIs(content::kChromeUIScheme) &&
98 !origin_.SchemeIs(content::kChromeDevToolsScheme)) {
99 return CONTENT_SETTING_ASK;
100 } 99 }
101
102 return setting;
103 } 100 }
104 101
105 bool MediaPermission::HasAvailableDevices(const std::string& device_id) const { 102 bool MediaPermission::HasAvailableDevices(const std::string& device_id) const {
106 const content::MediaStreamDevices* devices = nullptr; 103 const content::MediaStreamDevices* devices = nullptr;
107 if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) { 104 if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
108 devices = 105 devices =
109 &MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices(); 106 &MediaCaptureDevicesDispatcher::GetInstance()->GetAudioCaptureDevices();
110 } else if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) { 107 } else if (content_type_ == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
111 devices = 108 devices =
112 &MediaCaptureDevicesDispatcher::GetInstance()->GetVideoCaptureDevices(); 109 &MediaCaptureDevicesDispatcher::GetInstance()->GetVideoCaptureDevices();
(...skipping 10 matching lines...) Expand all
123 return false; 120 return false;
124 121
125 // Note: we check device_id before dereferencing devices. If the requested 122 // Note: we check device_id before dereferencing devices. If the requested
126 // device id is non-empty, then the corresponding device list must not be 123 // device id is non-empty, then the corresponding device list must not be
127 // NULL. 124 // NULL.
128 if (!device_id.empty() && !devices->FindById(device_id)) 125 if (!device_id.empty() && !devices->FindById(device_id))
129 return false; 126 return false;
130 127
131 return true; 128 return true;
132 } 129 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698