Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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_stream_devices_prefs.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/content_settings/content_settings_provider.h" | |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/ui/browser.h" | |
| 13 #include "chrome/common/content_settings.h" | |
| 14 #include "chrome/common/pref_names.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 | |
| 18 MediaStreamDevicesPrefs::MediaStreamDevicesPrefs(Profile* profile) | |
| 19 : profile_(profile) { | |
| 20 } | |
| 21 | |
| 22 void MediaStreamDevicesPrefs::RegisterUserPrefs(PrefService* user_prefs) { | |
| 23 if (!user_prefs->FindPreference(prefs::kMediaStreamAudioDevicesWhitelist)) | |
|
Bernhard Bauer
2012/06/11 18:16:00
You shouldn't need to check whether the preference
| |
| 24 user_prefs->RegisterDictionaryPref( | |
| 25 prefs::kMediaStreamAudioDevicesWhitelist, PrefService::UNSYNCABLE_PREF); | |
| 26 if (!user_prefs->FindPreference(prefs::kMediaStreamVideoDevicesWhitelist)) | |
|
tommi (sloooow) - chröme
2012/06/11 20:59:21
{}
| |
| 27 user_prefs->RegisterDictionaryPref( | |
| 28 prefs::kMediaStreamVideoDevicesWhitelist, PrefService::UNSYNCABLE_PREF); | |
| 29 } | |
| 30 | |
| 31 std::string MediaStreamDevicesPrefs::GetAlwaysAllowedAudioDevice( | |
| 32 const GURL& origin, | |
| 33 const content::MediaStreamDevices& devices) { | |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 35 DCHECK(!origin.is_empty()); | |
| 36 if (!devices.size()) | |
| 37 return std::string(); | |
| 38 | |
| 39 return GetAlwaysAllowedDevice(prefs::kMediaStreamAudioDevicesWhitelist, | |
| 40 origin, | |
| 41 devices); | |
| 42 } | |
| 43 | |
| 44 std::string MediaStreamDevicesPrefs::GetAlwaysAllowedVideoDevice( | |
| 45 const GURL& origin, | |
| 46 const content::MediaStreamDevices& devices) { | |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 48 DCHECK(!origin.is_empty()); | |
| 49 if (!devices.size()) | |
| 50 return std::string(); | |
| 51 | |
| 52 return GetAlwaysAllowedDevice(prefs::kMediaStreamVideoDevicesWhitelist, | |
| 53 origin, | |
| 54 devices); | |
| 55 } | |
| 56 | |
| 57 void MediaStreamDevicesPrefs::WhitelistOriginAndDevices( | |
| 58 const GURL& origin, | |
| 59 const content::MediaStreamDevices& devices) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 61 DCHECK(!origin.is_empty()); | |
| 62 DCHECK(devices.size()); | |
| 63 content::MediaStreamDevices::const_iterator dev = devices.begin(); | |
| 64 for (; dev != devices.end(); ++dev) { | |
| 65 if (dev->type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) { | |
| 66 AddOrigin(prefs::kMediaStreamAudioDevicesWhitelist, origin, dev->name); | |
| 67 } else { | |
| 68 DCHECK_EQ(dev->type, content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE); | |
| 69 AddOrigin(prefs::kMediaStreamVideoDevicesWhitelist, origin, dev->name); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 AddExceptionToContentSettings(origin); | |
| 74 } | |
| 75 | |
| 76 void MediaStreamDevicesPrefs::RemoveOriginFromWhitelists( | |
| 77 const GURL& origin) { | |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 79 DCHECK(!origin.is_empty()); | |
| 80 RemoveOrigin(prefs::kMediaStreamAudioDevicesWhitelist, origin); | |
| 81 RemoveOrigin(prefs::kMediaStreamVideoDevicesWhitelist, origin); | |
| 82 } | |
| 83 | |
| 84 bool MediaStreamDevicesPrefs::IsMediaDeviceBlocked() { | |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 86 return (profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( | |
| 87 CONTENT_SETTINGS_TYPE_MEDIASTREAM, NULL) == CONTENT_SETTING_BLOCK); | |
|
Bernhard Bauer
2012/06/11 18:16:00
This line is a bit awkwardly broken. You could ext
| |
| 88 } | |
| 89 | |
| 90 bool MediaStreamDevicesPrefs::IsOriginAlwaysAllowed(const GURL& origin) { | |
| 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 92 return (profile_->GetHostContentSettingsMap()->GetContentSetting( | |
| 93 origin, origin, CONTENT_SETTINGS_TYPE_MEDIASTREAM, | |
| 94 NO_RESOURCE_IDENTIFIER) == CONTENT_SETTING_ALLOW); | |
| 95 } | |
| 96 | |
| 97 void MediaStreamDevicesPrefs::AddExceptionToContentSettings( | |
| 98 const GURL& origin) { | |
| 99 ContentSettingsPattern primary_pattern = | |
| 100 ContentSettingsPattern::FromURLNoWildcard(origin); | |
| 101 profile_->GetHostContentSettingsMap()->SetContentSetting( | |
| 102 primary_pattern, | |
| 103 ContentSettingsPattern::Wildcard(), | |
| 104 CONTENT_SETTINGS_TYPE_MEDIASTREAM, | |
| 105 NO_RESOURCE_IDENTIFIER, | |
| 106 CONTENT_SETTING_ALLOW); | |
| 107 } | |
| 108 | |
| 109 std::string MediaStreamDevicesPrefs::GetAlwaysAllowedDevice( | |
| 110 const char* pref_id, | |
| 111 const GURL& origin, | |
| 112 const content::MediaStreamDevices& devices) { | |
| 113 content::MediaStreamDevices::const_iterator dev = devices.begin(); | |
| 114 for (; dev != devices.end(); ++dev) { | |
| 115 if (IsDevicePairedWithOrigin(pref_id, origin, dev->name)) | |
|
Bernhard Bauer
2012/06/11 18:16:00
You could simplify this by first getting the devic
| |
| 116 return dev->device_id; | |
| 117 } | |
| 118 | |
| 119 return std::string(); | |
| 120 } | |
| 121 | |
| 122 bool MediaStreamDevicesPrefs::IsDevicePairedWithOrigin( | |
| 123 const char* pref_id, | |
| 124 const GURL& origin, | |
| 125 const std::string& device) { | |
| 126 const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(pref_id); | |
| 127 if (!dict || dict->empty()) | |
|
Bernhard Bauer
2012/06/11 18:16:00
PrefService::GetDictionary will never return NULL
| |
| 128 return false; | |
| 129 | |
| 130 std::string auto_target_device; | |
| 131 if (dict->GetStringWithoutPathExpansion(origin.spec(), &auto_target_device) && | |
| 132 auto_target_device == device) { | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 void MediaStreamDevicesPrefs::AddOrigin(const char* pref_id, | |
| 140 const GURL& origin, | |
| 141 const std::string& device) { | |
| 142 DictionaryPrefUpdate update(profile_->GetPrefs(), pref_id); | |
| 143 DictionaryValue* whitelist = update.Get(); | |
| 144 if (!whitelist) { | |
|
Bernhard Bauer
2012/06/11 18:16:00
This will also never be NULL if the preference is
| |
| 145 NOTREACHED() << "Unregistered media stream device whitelist pref " | |
| 146 << pref_id; | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 whitelist->SetWithoutPathExpansion(origin.spec(), | |
| 151 Value::CreateStringValue(device)); | |
| 152 } | |
| 153 | |
| 154 void MediaStreamDevicesPrefs::RemoveOrigin( | |
| 155 const char* pref_id, | |
| 156 const GURL& origin) { | |
| 157 DictionaryPrefUpdate update(profile_->GetPrefs(), pref_id); | |
| 158 DictionaryValue* whitelist = update.Get(); | |
| 159 if (!whitelist) { | |
| 160 NOTREACHED() << "Unregistered media stream device whitelist pref" | |
| 161 << pref_id; | |
| 162 return; | |
| 163 } | |
| 164 | |
| 165 std::string val; | |
| 166 // Remove the origin if it is in the whitelist. | |
| 167 if (whitelist->GetStringWithoutPathExpansion(origin.spec(), &val)) { | |
| 168 whitelist->RemoveWithoutPathExpansion(origin.spec(), NULL); | |
| 169 } | |
| 170 } | |
| OLD | NEW |