Chromium Code Reviews| Index: chrome/browser/media/media_stream_devices_prefs.cc |
| diff --git a/chrome/browser/media/media_stream_devices_prefs.cc b/chrome/browser/media/media_stream_devices_prefs.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fedfc3a955dc61ddcb33e9ec53b31a4b3a3f2dd7 |
| --- /dev/null |
| +++ b/chrome/browser/media/media_stream_devices_prefs.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/media_stream_devices_prefs.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/prefs/scoped_user_pref_update.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/common/content_settings.h" |
| +#include "chrome/common/pref_names.h" |
| + |
| +using content::BrowserThread; |
| + |
| +MediaStreamDevicesPrefs::MediaStreamDevicesPrefs(Profile* profile) |
| + : profile_(profile) { |
| +} |
| + |
| +void MediaStreamDevicesPrefs::RegisterUserPrefs(PrefService* user_prefs) { |
| + if (!user_prefs->FindPreference(prefs::kMediaStreamAudioDevicesWhitelist)) |
|
Bernhard Bauer
2012/06/11 18:16:00
You shouldn't need to check whether the preference
|
| + user_prefs->RegisterDictionaryPref( |
| + prefs::kMediaStreamAudioDevicesWhitelist, PrefService::UNSYNCABLE_PREF); |
| + if (!user_prefs->FindPreference(prefs::kMediaStreamVideoDevicesWhitelist)) |
|
tommi (sloooow) - chröme
2012/06/11 20:59:21
{}
|
| + user_prefs->RegisterDictionaryPref( |
| + prefs::kMediaStreamVideoDevicesWhitelist, PrefService::UNSYNCABLE_PREF); |
| +} |
| + |
| +std::string MediaStreamDevicesPrefs::GetAlwaysAllowedAudioDevice( |
| + const GURL& origin, |
| + const content::MediaStreamDevices& devices) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(!origin.is_empty()); |
| + if (!devices.size()) |
| + return std::string(); |
| + |
| + return GetAlwaysAllowedDevice(prefs::kMediaStreamAudioDevicesWhitelist, |
| + origin, |
| + devices); |
| +} |
| + |
| +std::string MediaStreamDevicesPrefs::GetAlwaysAllowedVideoDevice( |
| + const GURL& origin, |
| + const content::MediaStreamDevices& devices) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(!origin.is_empty()); |
| + if (!devices.size()) |
| + return std::string(); |
| + |
| + return GetAlwaysAllowedDevice(prefs::kMediaStreamVideoDevicesWhitelist, |
| + origin, |
| + devices); |
| +} |
| + |
| +void MediaStreamDevicesPrefs::WhitelistOriginAndDevices( |
| + const GURL& origin, |
| + const content::MediaStreamDevices& devices) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(!origin.is_empty()); |
| + DCHECK(devices.size()); |
| + content::MediaStreamDevices::const_iterator dev = devices.begin(); |
| + for (; dev != devices.end(); ++dev) { |
| + if (dev->type == content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) { |
| + AddOrigin(prefs::kMediaStreamAudioDevicesWhitelist, origin, dev->name); |
| + } else { |
| + DCHECK_EQ(dev->type, content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE); |
| + AddOrigin(prefs::kMediaStreamVideoDevicesWhitelist, origin, dev->name); |
| + } |
| + } |
| + |
| + AddExceptionToContentSettings(origin); |
| +} |
| + |
| +void MediaStreamDevicesPrefs::RemoveOriginFromWhitelists( |
| + const GURL& origin) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(!origin.is_empty()); |
| + RemoveOrigin(prefs::kMediaStreamAudioDevicesWhitelist, origin); |
| + RemoveOrigin(prefs::kMediaStreamVideoDevicesWhitelist, origin); |
| +} |
| + |
| +bool MediaStreamDevicesPrefs::IsMediaDeviceBlocked() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + return (profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( |
| + 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
|
| +} |
| + |
| +bool MediaStreamDevicesPrefs::IsOriginAlwaysAllowed(const GURL& origin) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + return (profile_->GetHostContentSettingsMap()->GetContentSetting( |
| + origin, origin, CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
| + NO_RESOURCE_IDENTIFIER) == CONTENT_SETTING_ALLOW); |
| +} |
| + |
| +void MediaStreamDevicesPrefs::AddExceptionToContentSettings( |
| + const GURL& origin) { |
| + ContentSettingsPattern primary_pattern = |
| + ContentSettingsPattern::FromURLNoWildcard(origin); |
| + profile_->GetHostContentSettingsMap()->SetContentSetting( |
| + primary_pattern, |
| + ContentSettingsPattern::Wildcard(), |
| + CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
| + NO_RESOURCE_IDENTIFIER, |
| + CONTENT_SETTING_ALLOW); |
| +} |
| + |
| +std::string MediaStreamDevicesPrefs::GetAlwaysAllowedDevice( |
| + const char* pref_id, |
| + const GURL& origin, |
| + const content::MediaStreamDevices& devices) { |
| + content::MediaStreamDevices::const_iterator dev = devices.begin(); |
| + for (; dev != devices.end(); ++dev) { |
| + if (IsDevicePairedWithOrigin(pref_id, origin, dev->name)) |
|
Bernhard Bauer
2012/06/11 18:16:00
You could simplify this by first getting the devic
|
| + return dev->device_id; |
| + } |
| + |
| + return std::string(); |
| +} |
| + |
| +bool MediaStreamDevicesPrefs::IsDevicePairedWithOrigin( |
| + const char* pref_id, |
| + const GURL& origin, |
| + const std::string& device) { |
| + const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary(pref_id); |
| + if (!dict || dict->empty()) |
|
Bernhard Bauer
2012/06/11 18:16:00
PrefService::GetDictionary will never return NULL
|
| + return false; |
| + |
| + std::string auto_target_device; |
| + if (dict->GetStringWithoutPathExpansion(origin.spec(), &auto_target_device) && |
| + auto_target_device == device) { |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +void MediaStreamDevicesPrefs::AddOrigin(const char* pref_id, |
| + const GURL& origin, |
| + const std::string& device) { |
| + DictionaryPrefUpdate update(profile_->GetPrefs(), pref_id); |
| + DictionaryValue* whitelist = update.Get(); |
| + if (!whitelist) { |
|
Bernhard Bauer
2012/06/11 18:16:00
This will also never be NULL if the preference is
|
| + NOTREACHED() << "Unregistered media stream device whitelist pref " |
| + << pref_id; |
| + return; |
| + } |
| + |
| + whitelist->SetWithoutPathExpansion(origin.spec(), |
| + Value::CreateStringValue(device)); |
| +} |
| + |
| +void MediaStreamDevicesPrefs::RemoveOrigin( |
| + const char* pref_id, |
| + const GURL& origin) { |
| + DictionaryPrefUpdate update(profile_->GetPrefs(), pref_id); |
| + DictionaryValue* whitelist = update.Get(); |
| + if (!whitelist) { |
| + NOTREACHED() << "Unregistered media stream device whitelist pref" |
| + << pref_id; |
| + return; |
| + } |
| + |
| + std::string val; |
| + // Remove the origin if it is in the whitelist. |
| + if (whitelist->GetStringWithoutPathExpansion(origin.spec(), &val)) { |
| + whitelist->RemoveWithoutPathExpansion(origin.spec(), NULL); |
| + } |
| +} |