OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/content_settings/policy_content_settings_provider.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/browser_thread.h" |
| 9 #include "chrome/browser/content_settings/content_settings_details.h" |
| 10 #include "chrome/browser/content_settings/content_settings_pattern.h" |
| 11 #include "chrome/browser/prefs/pref_service.h" |
| 12 #include "chrome/browser/prefs/scoped_pref_update.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/notification_details.h" |
| 16 #include "chrome/common/notification_service.h" |
| 17 #include "chrome/common/notification_source.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Base pref path of the prefs that contain the managed default content |
| 23 // settings values. |
| 24 const std::string kManagedSettings = |
| 25 "profile.managed_default_content_settings"; |
| 26 |
| 27 // The preferences used to manage ContentSettingsTypes. |
| 28 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = { |
| 29 prefs::kManagedDefaultCookiesSetting, |
| 30 prefs::kManagedDefaultImagesSetting, |
| 31 prefs::kManagedDefaultJavaScriptSetting, |
| 32 prefs::kManagedDefaultPluginsSetting, |
| 33 prefs::kManagedDefaultPopupsSetting, |
| 34 NULL, // Not used for Geolocation |
| 35 NULL, // Not used for Notifications |
| 36 }; |
| 37 |
| 38 } // namespace |
| 39 |
| 40 PolicyContentSettingsProvider::PolicyContentSettingsProvider(Profile* profile) |
| 41 : profile_(profile), |
| 42 is_off_the_record_(profile_->IsOffTheRecord()) { |
| 43 PrefService* prefs = profile->GetPrefs(); |
| 44 |
| 45 // Read global defaults. |
| 46 DCHECK_EQ(arraysize(kPrefToManageType), |
| 47 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); |
| 48 ReadManagedDefaultSettings(); |
| 49 |
| 50 pref_change_registrar_.Init(prefs); |
| 51 // The following preferences are only used to indicate if a |
| 52 // default-content-setting is managed and to hold the managed default-setting |
| 53 // value. If the value for any of the following perferences is set then the |
| 54 // corresponding default-content-setting is managed. These preferences exist |
| 55 // in parallel to the preference default-content-settings. If a |
| 56 // default-content-settings-type is managed any user defined excpetions |
| 57 // (patterns) for this type are ignored. |
| 58 pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this); |
| 59 pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this); |
| 60 pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this); |
| 61 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); |
| 62 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); |
| 63 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 64 Source<Profile>(profile_)); |
| 65 } |
| 66 |
| 67 PolicyContentSettingsProvider::~PolicyContentSettingsProvider() { |
| 68 UnregisterObservers(); |
| 69 } |
| 70 |
| 71 bool PolicyContentSettingsProvider::CanProvideDefaultSetting( |
| 72 ContentSettingsType content_type) const { |
| 73 AutoLock lock(lock_); |
| 74 if (managed_default_content_settings_.settings[content_type] != |
| 75 CONTENT_SETTING_DEFAULT) { |
| 76 return true; |
| 77 } else { |
| 78 return false; |
| 79 } |
| 80 } |
| 81 |
| 82 ContentSetting PolicyContentSettingsProvider::ProvideDefaultSetting( |
| 83 ContentSettingsType content_type) const { |
| 84 AutoLock auto_lock(lock_); |
| 85 return managed_default_content_settings_.settings[content_type]; |
| 86 } |
| 87 |
| 88 void PolicyContentSettingsProvider::UpdateDefaultSetting( |
| 89 ContentSettingsType content_type, |
| 90 ContentSetting setting) { |
| 91 } |
| 92 |
| 93 bool PolicyContentSettingsProvider::DefaultSettingIsManaged( |
| 94 ContentSettingsType content_type) const { |
| 95 AutoLock lock(lock_); |
| 96 if (managed_default_content_settings_.settings[content_type] != |
| 97 CONTENT_SETTING_DEFAULT) { |
| 98 return true; |
| 99 } else { |
| 100 return false; |
| 101 } |
| 102 } |
| 103 |
| 104 void PolicyContentSettingsProvider::ResetToDefaults() { |
| 105 } |
| 106 |
| 107 void PolicyContentSettingsProvider::Observe(NotificationType type, |
| 108 const NotificationSource& source, |
| 109 const NotificationDetails& details) { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 |
| 112 if (type == NotificationType::PREF_CHANGED) { |
| 113 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 114 std::string* name = Details<std::string>(details).ptr(); |
| 115 if (*name == prefs::kManagedDefaultCookiesSetting) { |
| 116 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES); |
| 117 } else if (*name == prefs::kManagedDefaultImagesSetting) { |
| 118 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_IMAGES); |
| 119 } else if (*name == prefs::kManagedDefaultJavaScriptSetting) { |
| 120 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT); |
| 121 } else if (*name == prefs::kManagedDefaultPluginsSetting) { |
| 122 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_PLUGINS); |
| 123 } else if (*name == prefs::kManagedDefaultPopupsSetting) { |
| 124 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_POPUPS); |
| 125 } else { |
| 126 NOTREACHED() << "Unexpected preference observed"; |
| 127 return; |
| 128 } |
| 129 |
| 130 if (!is_off_the_record_) { |
| 131 NotifyObservers(ContentSettingsDetails( |
| 132 ContentSettingsPattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); |
| 133 } |
| 134 } else if (type == NotificationType::PROFILE_DESTROYED) { |
| 135 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); |
| 136 UnregisterObservers(); |
| 137 } else { |
| 138 NOTREACHED() << "Unexpected notification"; |
| 139 } |
| 140 } |
| 141 |
| 142 void PolicyContentSettingsProvider::UnregisterObservers() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 if (!profile_) |
| 145 return; |
| 146 pref_change_registrar_.RemoveAll(); |
| 147 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, |
| 148 Source<Profile>(profile_)); |
| 149 profile_ = NULL; |
| 150 } |
| 151 |
| 152 |
| 153 void PolicyContentSettingsProvider::NotifyObservers( |
| 154 const ContentSettingsDetails& details) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 156 if (profile_ == NULL) |
| 157 return; |
| 158 NotificationService::current()->Notify( |
| 159 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 160 Source<HostContentSettingsMap>(profile_->GetHostContentSettingsMap()), |
| 161 Details<const ContentSettingsDetails>(&details)); |
| 162 } |
| 163 |
| 164 void PolicyContentSettingsProvider::ReadManagedDefaultSettings() { |
| 165 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) { |
| 166 if (kPrefToManageType[type] == NULL) { |
| 167 // TODO(markusheintz): Handle Geolocation and notification separately. |
| 168 continue; |
| 169 } |
| 170 UpdateManagedDefaultSetting(ContentSettingsType(type)); |
| 171 } |
| 172 } |
| 173 |
| 174 void PolicyContentSettingsProvider::UpdateManagedDefaultSetting( |
| 175 ContentSettingsType type) { |
| 176 // If a pref to manage a default-content-setting was not set (NOTICE: |
| 177 // "HasPrefPath" returns false if no value was set for a registered pref) then |
| 178 // the default value of the preference is used. The default value of a |
| 179 // preference to manage a default-content-settings is |
| 180 // CONTENT_SETTING_DEFAULT. This indicates that no managed value is set. If a |
| 181 // pref was set, than it MUST be managed. |
| 182 PrefService* prefs = profile_->GetPrefs(); |
| 183 DCHECK(!prefs->HasPrefPath(kPrefToManageType[type]) || |
| 184 prefs->IsManagedPreference(kPrefToManageType[type])); |
| 185 AutoLock auto_lock(lock_); |
| 186 managed_default_content_settings_.settings[type] = IntToContentSetting( |
| 187 prefs->GetInteger(kPrefToManageType[type])); |
| 188 } |
| 189 |
| 190 // static |
| 191 void PolicyContentSettingsProvider::RegisterUserPrefs(PrefService* prefs) { |
| 192 // Preferences for default content setting policies. A policy is not set of |
| 193 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. |
| 194 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, |
| 195 CONTENT_SETTING_DEFAULT); |
| 196 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, |
| 197 CONTENT_SETTING_DEFAULT); |
| 198 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting, |
| 199 CONTENT_SETTING_DEFAULT); |
| 200 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting, |
| 201 CONTENT_SETTING_DEFAULT); |
| 202 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting, |
| 203 CONTENT_SETTING_DEFAULT); |
| 204 } |
OLD | NEW |