Chromium Code Reviews| Index: chrome/browser/content_settings/policy_content_settings_provider.cc |
| diff --git a/chrome/browser/content_settings/policy_content_settings_provider.cc b/chrome/browser/content_settings/policy_content_settings_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..091fc7242a3097d9daf5544fd5484ee5d1375fed |
| --- /dev/null |
| +++ b/chrome/browser/content_settings/policy_content_settings_provider.cc |
| @@ -0,0 +1,212 @@ |
| +// Copyright (c) 2010 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/content_settings/policy_content_settings_provider.h" |
| + |
| +#include "base/command_line.h" |
| +#include "chrome/browser/browser_thread.h" |
| +#include "chrome/browser/content_settings/content_settings_details.h" |
| +#include "chrome/browser/content_settings/content_settings_pattern.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/prefs/scoped_pref_update.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/notification_details.h" |
| +#include "chrome/common/notification_service.h" |
| +#include "chrome/common/notification_source.h" |
| +#include "chrome/common/pref_names.h" |
| + |
| +namespace { |
| + |
| +// Base pref path of the prefs that contain the managed default content |
| +// settings values. |
| +const std::string kManagedSettings = |
| + "profile.managed_default_content_settings"; |
| + |
| +// The preferences used to manage ContentSettingsTypes. |
| +const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = { |
| + prefs::kManagedDefaultCookiesSetting, |
| + prefs::kManagedDefaultImagesSetting, |
| + prefs::kManagedDefaultJavaScriptSetting, |
| + prefs::kManagedDefaultPluginsSetting, |
| + prefs::kManagedDefaultPopupsSetting, |
| + NULL, // Not used for Geolocation |
| + NULL, // Not used for Notifications |
| +}; |
| + |
| +} // namespace |
| + |
| +PolicyContentSettingsProvider::PolicyContentSettingsProvider(Profile* profile) |
| + : profile_(profile), |
| + is_off_the_record_(profile_->IsOffTheRecord()) { |
| + PrefService* prefs = profile->GetPrefs(); |
| + |
| + // Read global defaults. |
| + DCHECK_EQ(arraysize(kPrefToManageType), |
| + static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); |
| + ReadDefaultSettingsFromPrefs(prefs, &managed_default_content_settings_); |
| + |
| + pref_change_registrar_.Init(prefs); |
| + // The following preferences are only used to indicate if a |
| + // default-content-setting is managed and to hold the managed default-setting |
| + // value. If the value for any of the following perferences is set then the |
| + // corresponding default-content-setting is managed. These preferences exist |
| + // in parallel to the preference default-content-settings. If a |
| + // default-content-settings-type is managed any user defined excpetions |
| + // (patterns) for this type are ignored. |
| + pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this); |
| + pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this); |
| + pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this); |
| + pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); |
| + pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); |
| + notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| + Source<Profile>(profile_)); |
| +} |
| + |
| +PolicyContentSettingsProvider::~PolicyContentSettingsProvider() { |
| + UnregisterObservers(); |
| +} |
| + |
| +bool PolicyContentSettingsProvider::CanProvideDefaultSetting( |
| + ContentSettingsType content_type) const { |
| + if (managed_default_content_settings_.settings[content_type] != |
| + CONTENT_SETTING_DEFAULT) { |
| + return true; |
| + } else { |
| + return false; |
| + } |
| +} |
| + |
| +ContentSetting PolicyContentSettingsProvider::ProvideDefaultSetting( |
| + ContentSettingsType content_type) const { |
| + AutoLock auto_lock(lock_); |
| + return managed_default_content_settings_.settings[content_type]; |
| +} |
| + |
| +void PolicyContentSettingsProvider::UpdateDefaultSetting( |
| + ContentSettingsType content_type, |
| + ContentSetting setting) { |
| +} |
| + |
| +bool PolicyContentSettingsProvider::DefaultSettingIsManaged( |
| + ContentSettingsType content_type) const { |
| + if (managed_default_content_settings_.settings[content_type] != |
| + CONTENT_SETTING_DEFAULT) { |
| + return true; |
| + } else { |
| + return false; |
| + } |
| +} |
| + |
| +void PolicyContentSettingsProvider::ResetToDefaults() { |
| +} |
| + |
| +void PolicyContentSettingsProvider::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + if (NotificationType::PREF_CHANGED == type) { |
| + std::string* name = Details<std::string>(details).ptr(); |
|
gfeher
2010/12/08 13:59:17
Here and everywhere else: please test that the sou
jochen (gone - plz use gerrit)
2010/12/08 14:54:01
If the provider is used for the OTR profile, it'll
gfeher
2010/12/09 10:29:53
This raises an interesting question. When you regi
jochen (gone - plz use gerrit)
2010/12/09 13:18:53
Ok, I've played around with this.
For the PROFILE
|
| + if (prefs::kManagedDefaultCookiesSetting == *name) { |
| + UpdateManagedDefaultSetting(profile_->GetPrefs(), |
| + CONTENT_SETTINGS_TYPE_COOKIES, |
| + &managed_default_content_settings_); |
| + } else if (prefs::kManagedDefaultImagesSetting == *name) { |
| + UpdateManagedDefaultSetting(profile_->GetPrefs(), |
| + CONTENT_SETTINGS_TYPE_IMAGES, |
| + &managed_default_content_settings_); |
| + } else if (prefs::kManagedDefaultJavaScriptSetting == *name) { |
| + UpdateManagedDefaultSetting(profile_->GetPrefs(), |
| + CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| + &managed_default_content_settings_); |
| + } else if (prefs::kManagedDefaultPluginsSetting == *name) { |
| + UpdateManagedDefaultSetting(profile_->GetPrefs(), |
| + CONTENT_SETTINGS_TYPE_PLUGINS, |
| + &managed_default_content_settings_); |
| + } else if (prefs::kManagedDefaultPopupsSetting == *name) { |
| + UpdateManagedDefaultSetting(profile_->GetPrefs(), |
| + CONTENT_SETTINGS_TYPE_POPUPS, |
| + &managed_default_content_settings_); |
| + } else { |
| + NOTREACHED() << "Unexpected preference observed"; |
| + return; |
| + } |
| + |
| + if (!is_off_the_record_) { |
| + NotifyObservers(ContentSettingsDetails( |
| + ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); |
| + } |
| + } else if (NotificationType::PROFILE_DESTROYED == type) { |
| + UnregisterObservers(); |
| + } else { |
| + NOTREACHED() << "Unexpected notification"; |
| + } |
| +} |
| + |
| +void PolicyContentSettingsProvider::UnregisterObservers() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (!profile_) |
| + return; |
| + pref_change_registrar_.RemoveAll(); |
| + notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, |
| + Source<Profile>(profile_)); |
| + profile_ = NULL; |
| +} |
| + |
| + |
| +void PolicyContentSettingsProvider::NotifyObservers( |
| + const ContentSettingsDetails& details) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + if (profile_ == NULL) |
| + return; |
| + NotificationService::current()->Notify( |
| + NotificationType::CONTENT_SETTINGS_CHANGED, |
| + Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), |
| + Details<const ContentSettingsDetails>(&details)); |
| +} |
| + |
| +void PolicyContentSettingsProvider::ReadDefaultSettingsFromPrefs( |
| + const PrefService* prefs, ContentSettings* settings) { |
| + for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) { |
| + if (kPrefToManageType[type] == NULL) { |
| + // TODO(markusheintz): Handle Geolocation and notification separately. |
| + continue; |
| + } |
| + UpdateManagedDefaultSetting(prefs, ContentSettingsType(type), settings); |
| + } |
| +} |
| + |
| +void PolicyContentSettingsProvider::UpdateManagedDefaultSetting( |
| + const PrefService* prefs, |
| + ContentSettingsType type, |
| + ContentSettings* settings) { |
| + // If a pref to manage a default-content-setting was not set (NOTICE: |
| + // "HasPrefPath" returns false if no value was set for a registered pref) then |
| + // the default value of the preference is used. The default value of a |
| + // preference to manage a default-content-settings is |
| + // CONTENT_SETTING_DEFAULT. This indicates that no managed value is set. If a |
| + // pref was set, than it MUST be managed. |
| + DCHECK(!prefs->HasPrefPath(kPrefToManageType[type]) || |
| + prefs->IsManagedPreference(kPrefToManageType[type])); |
| + AutoLock auto_lock(lock_); |
| + settings->settings[type] = IntToContentSetting( |
| + prefs->GetInteger(kPrefToManageType[type])); |
| +} |
| + |
| +// static |
| +void PolicyContentSettingsProvider::RegisterUserPrefs(PrefService* prefs) { |
| + // Preferences for default content setting policies. A policy is not set of |
| + // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. |
| + prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, |
| + CONTENT_SETTING_DEFAULT); |
| + prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, |
| + CONTENT_SETTING_DEFAULT); |
| + prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting, |
| + CONTENT_SETTING_DEFAULT); |
| + prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting, |
| + CONTENT_SETTING_DEFAULT); |
| + prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting, |
| + CONTENT_SETTING_DEFAULT); |
| +} |