Chromium Code Reviews| Index: components/content_settings/core/browser/content_settings_supervised_provider.cc |
| diff --git a/components/content_settings/core/browser/content_settings_supervised_provider.cc b/components/content_settings/core/browser/content_settings_supervised_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0efba2cc9bc973e32fedcc79234acb14fe49ef07 |
| --- /dev/null |
| +++ b/components/content_settings/core/browser/content_settings_supervised_provider.cc |
| @@ -0,0 +1,145 @@ |
| +// Copyright (c) 2015 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 "components/content_settings/core/browser/content_settings_supervised_provider.h" |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/bind.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/values.h" |
| +#include "components/content_settings/core/browser/content_settings_binary_value_map.h" |
| +#include "components/content_settings/core/browser/content_settings_rule.h" |
| +#include "components/content_settings/core/browser/content_settings_utils.h" |
| +#include "components/content_settings/core/common/pref_names.h" |
| +#include "components/pref_registry/pref_registry_syncable.h" |
| + |
| +namespace { |
| + |
| +struct PrefsForSupervisedContentSettingsMapEntry { |
| + const char* pref_name; |
| + ContentSettingsType content_type; |
| +}; |
| + |
| +const PrefsForSupervisedContentSettingsMapEntry |
| + kPrefsForSupervisedContentSettingsMap[] = { |
| + { |
| + prefs::kSupervisedGeolocationDisabled, |
| + CONTENT_SETTINGS_TYPE_GEOLOCATION, |
| + }, { |
| + prefs::kSupervisedCameraMicDisabled, |
| + CONTENT_SETTINGS_TYPE_MEDIASTREAM, |
| + } |
| +}; |
| + |
| +void SetContentSettingFromPreference( |
| + PrefService* prefs, |
| + content_settings::BinaryValueMap& value_map, |
|
Bernhard Bauer
2015/02/05 12:07:51
Don't use non-const references. If you want to cha
knn
2015/02/26 12:07:30
Done. This isn't a method as I don't want to expos
|
| + const PrefsForSupervisedContentSettingsMapEntry& pref_map_entry) { |
| + const PrefService::Preference* pref = |
| + prefs->FindPreference(pref_map_entry.pref_name); |
| + DCHECK(pref); |
| + DCHECK(pref->IsManagedByCustodian()); |
| + bool is_disabled; |
| + if (!pref->GetValue()->GetAsBoolean(&is_disabled)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + value_map.SetContentSettingDisabled(pref_map_entry.content_type, is_disabled); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace content_settings { |
| + |
| +// static |
| +void SupervisedProvider::RegisterProfilePrefs( |
| + user_prefs::PrefRegistrySyncable* registry) { |
| + registry->RegisterBooleanPref( |
| + prefs::kSupervisedGeolocationDisabled, false, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| + registry->RegisterBooleanPref( |
| + prefs::kSupervisedCameraMicDisabled, false, |
| + user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| +} |
| + |
| +SupervisedProvider::SupervisedProvider(PrefService* prefs) : prefs_(prefs) { |
| + ReadContentSettingsFromPreferences(); |
| + |
| + pref_change_registrar_.Init(prefs_); |
| + PrefChangeRegistrar::NamedChangeCallback callback = base::Bind( |
| + &SupervisedProvider::OnPreferenceChanged, base::Unretained(this)); |
| + pref_change_registrar_.Add(prefs::kSupervisedGeolocationDisabled, callback); |
| + pref_change_registrar_.Add(prefs::kSupervisedCameraMicDisabled, callback); |
| +} |
| + |
| +SupervisedProvider::~SupervisedProvider() { |
| + DCHECK(!prefs_); |
| +} |
| + |
| +RuleIterator* SupervisedProvider::GetRuleIterator( |
| + ContentSettingsType content_type, |
| + const ResourceIdentifier& resource_identifier, |
| + bool incognito) const { |
| + scoped_ptr<base::AutoLock> auto_lock(new base::AutoLock(lock_)); |
| + return value_map_.GetRuleIterator(content_type, resource_identifier, |
| + auto_lock.Pass()); |
| +} |
| + |
| +void SupervisedProvider::ReadContentSettingsFromPreferences() { |
| + base::AutoLock auto_lock(lock_); |
| + for (const auto& pref_map_entry : kPrefsForSupervisedContentSettingsMap) { |
| + if (!prefs_->HasPrefPath(pref_map_entry.pref_name)) { |
| + VLOG(2) << "Skipping unset preference: " << pref_map_entry.pref_name; |
| + continue; |
| + } |
| + SetContentSettingFromPreference(prefs_, value_map_, pref_map_entry); |
| + } |
| +} |
| + |
| +// Since the SupervisedProvider is a read only content settings provider, all |
| +// methodes of the ProviderInterface that set or delete any settings do nothing. |
|
Bernhard Bauer
2015/02/05 12:07:51
Nit: "methods"
knn
2015/02/26 12:07:30
Done.
|
| +bool SupervisedProvider::SetWebsiteSetting( |
| + const ContentSettingsPattern& primary_pattern, |
| + const ContentSettingsPattern& secondary_pattern, |
| + ContentSettingsType content_type, |
| + const ResourceIdentifier& resource_identifier, |
| + base::Value* value) { |
| + return false; |
| +} |
| + |
| +void SupervisedProvider::ClearAllContentSettingsRules( |
| + ContentSettingsType content_type) { |
| +} |
| + |
| +void SupervisedProvider::ShutdownOnUIThread() { |
| + DCHECK(CalledOnValidThread()); |
| + RemoveAllObservers(); |
| + if (!prefs_) |
| + return; |
| + pref_change_registrar_.RemoveAll(); |
| + prefs_ = NULL; |
|
Bernhard Bauer
2015/02/05 12:07:51
Use nullptr instead of NULL.
knn
2015/02/26 12:07:30
Done.
|
| +} |
| + |
| +void SupervisedProvider::OnPreferenceChanged(const std::string& name) { |
| + DCHECK(CalledOnValidThread()); |
| + scoped_ptr<base::AutoLock> auto_lock(new base::AutoLock(lock_)); |
|
Bernhard Bauer
2015/02/05 12:07:51
You can just put this into a nested scope that end
knn
2015/02/26 12:07:30
Done.
|
| + bool is_valid_preference = false; |
| + for (const auto& pref_map_entry : kPrefsForSupervisedContentSettingsMap) { |
| + if (name == pref_map_entry.pref_name) { |
| + is_valid_preference = true; |
| + SetContentSettingFromPreference(prefs_, value_map_, pref_map_entry); |
| + break; |
| + } |
| + } |
| + DCHECK(is_valid_preference); |
| + auto_lock.reset(); |
| + |
| + NotifyObservers(ContentSettingsPattern(), ContentSettingsPattern(), |
| + CONTENT_SETTINGS_TYPE_DEFAULT, std::string()); |
| +} |
| + |
| +} // namespace content_settings |