Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| 11 #include "chrome/browser/content_settings/content_settings_details.h" | 11 #include "chrome/browser/content_settings/content_settings_details.h" |
| 12 #include "chrome/browser/content_settings/policy_content_settings_provider.h" | |
| 13 #include "chrome/browser/content_settings/pref_content_settings_provider.h" | |
| 12 #include "chrome/browser/metrics/user_metrics.h" | 14 #include "chrome/browser/metrics/user_metrics.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/prefs/scoped_pref_update.h" | 17 #include "chrome/browser/prefs/scoped_pref_update.h" |
| 16 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
| 17 #include "chrome/common/notification_source.h" | 19 #include "chrome/common/notification_source.h" |
| 18 #include "chrome/common/notification_type.h" | 20 #include "chrome/common/notification_type.h" |
| 19 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 22 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
| 23 #include "googleurl/src/url_canon.h" | |
| 24 #include "googleurl/src/url_parse.h" | |
| 25 #include "net/base/dns_util.h" | |
| 26 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 27 #include "net/base/static_cookie_policy.h" | 26 #include "net/base/static_cookie_policy.h" |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 // Base pref path of the prefs that contain the managed default content | |
| 32 // settings values. | |
| 33 const std::string kManagedSettings = | |
| 34 "profile.managed_default_content_settings"; | |
| 35 | |
| 36 // The preference keys where resource identifiers are stored for | 30 // The preference keys where resource identifiers are stored for |
| 37 // ContentSettingsType values that support resource identifiers. | 31 // ContentSettingsType values that support resource identifiers. |
| 38 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | 32 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { |
| 39 NULL, | 33 NULL, |
| 40 NULL, | 34 NULL, |
| 41 NULL, | 35 NULL, |
| 42 "per_plugin", | 36 "per_plugin", |
| 43 NULL, | 37 NULL, |
| 44 NULL, // Not used for Geolocation | 38 NULL, // Not used for Geolocation |
| 45 NULL, // Not used for Notifications | 39 NULL, // Not used for Notifications |
| 46 }; | 40 }; |
| 47 | 41 |
| 48 // The names of the ContentSettingsType values, for use with dictionary prefs. | 42 // The names of the ContentSettingsType values, for use with dictionary prefs. |
| 49 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | 43 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { |
| 50 "cookies", | 44 "cookies", |
| 51 "images", | 45 "images", |
| 52 "javascript", | 46 "javascript", |
| 53 "plugins", | 47 "plugins", |
| 54 "popups", | 48 "popups", |
| 55 NULL, // Not used for Geolocation | 49 NULL, // Not used for Geolocation |
| 56 NULL, // Not used for Notifications | 50 NULL, // Not used for Notifications |
| 57 }; | 51 }; |
| 58 | 52 |
| 59 // The preferences used to manage ContentSettingsTypes. | |
| 60 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 61 prefs::kManagedDefaultCookiesSetting, | |
| 62 prefs::kManagedDefaultImagesSetting, | |
| 63 prefs::kManagedDefaultJavaScriptSetting, | |
| 64 prefs::kManagedDefaultPluginsSetting, | |
| 65 prefs::kManagedDefaultPopupsSetting, | |
| 66 NULL, // Not used for Geolocation | |
| 67 NULL, // Not used for Notifications | |
| 68 }; | |
| 69 | |
| 70 // The default setting for each content type. | |
| 71 const ContentSetting kDefaultSettings[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 72 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES | |
| 73 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES | |
| 74 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT | |
| 75 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS | |
| 76 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS | |
| 77 CONTENT_SETTING_ASK, // Not used for Geolocation | |
| 78 CONTENT_SETTING_ASK, // Not used for Notifications | |
| 79 }; | |
| 80 | |
| 81 // True if a given content settings type requires additional resource | 53 // True if a given content settings type requires additional resource |
| 82 // identifiers. | 54 // identifiers. |
| 83 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { | 55 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { |
| 84 false, // CONTENT_SETTINGS_TYPE_COOKIES | 56 false, // CONTENT_SETTINGS_TYPE_COOKIES |
| 85 false, // CONTENT_SETTINGS_TYPE_IMAGES | 57 false, // CONTENT_SETTINGS_TYPE_IMAGES |
| 86 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT | 58 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT |
| 87 true, // CONTENT_SETTINGS_TYPE_PLUGINS | 59 true, // CONTENT_SETTINGS_TYPE_PLUGINS |
| 88 false, // CONTENT_SETTINGS_TYPE_POPUPS | 60 false, // CONTENT_SETTINGS_TYPE_POPUPS |
| 89 false, // Not used for Geolocation | 61 false, // Not used for Geolocation |
| 90 false, // Not used for Notifications | 62 false, // Not used for Notifications |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 108 ContentSetting setting) { | 80 ContentSetting setting) { |
| 109 if (setting == CONTENT_SETTING_ASK && | 81 if (setting == CONTENT_SETTING_ASK && |
| 110 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && | 82 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && |
| 111 !CommandLine::ForCurrentProcess()->HasSwitch( | 83 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 112 switches::kEnableClickToPlay)) { | 84 switches::kEnableClickToPlay)) { |
| 113 return CONTENT_SETTING_BLOCK; | 85 return CONTENT_SETTING_BLOCK; |
| 114 } | 86 } |
| 115 return setting; | 87 return setting; |
| 116 } | 88 } |
| 117 | 89 |
| 90 typedef std::vector<linked_ptr<ContentSettingsProviderInterface> >::iterator | |
| 91 provider_iterator; | |
| 92 typedef | |
| 93 std::vector<linked_ptr<ContentSettingsProviderInterface> >::const_iterator | |
| 94 const_provider_iterator; | |
| 95 | |
| 118 } // namespace | 96 } // namespace |
| 119 | 97 |
| 120 | 98 |
| 121 struct HostContentSettingsMap::ExtendedContentSettings { | 99 struct HostContentSettingsMap::ExtendedContentSettings { |
| 122 ContentSettings content_settings; | 100 ContentSettings content_settings; |
| 123 ResourceContentSettings content_settings_for_resources; | 101 ResourceContentSettings content_settings_for_resources; |
| 124 }; | 102 }; |
| 125 | 103 |
| 126 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) | 104 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) |
| 127 : profile_(profile), | 105 : profile_(profile), |
| 106 is_off_the_record_(profile_->IsOffTheRecord()), | |
| 107 updating_preferences_(false), | |
| 128 block_third_party_cookies_(false), | 108 block_third_party_cookies_(false), |
| 129 is_block_third_party_cookies_managed_(false), | 109 is_block_third_party_cookies_managed_(false) { |
| 130 is_off_the_record_(profile_->IsOffTheRecord()), | 110 // The order in which the content settings providers are created is critical, |
| 131 updating_preferences_(false) { | 111 // as providers that are further down in the list (i.e. added later) override |
| 112 // providers further up. | |
| 113 content_settings_providers_.push_back( | |
| 114 linked_ptr<ContentSettingsProviderInterface>( | |
| 115 new PrefContentSettingsProvider(profile))); | |
| 116 content_settings_providers_.push_back( | |
| 117 linked_ptr<ContentSettingsProviderInterface>( | |
| 118 new PolicyContentSettingsProvider(profile))); | |
| 119 | |
| 132 PrefService* prefs = profile_->GetPrefs(); | 120 PrefService* prefs = profile_->GetPrefs(); |
| 133 | 121 |
| 134 MigrateObsoleteCookiePref(prefs); | 122 MigrateObsoleteCookiePref(prefs); |
| 135 | 123 |
| 136 MigrateObsoletePopupsPref(prefs); | 124 MigrateObsoletePopupsPref(prefs); |
| 137 | 125 |
| 138 MigrateObsoletePerhostPref(prefs); | 126 MigrateObsoletePerhostPref(prefs); |
| 139 | 127 |
| 140 // Read global defaults. | |
| 141 DCHECK_EQ(arraysize(kTypeNames), | |
| 142 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | |
| 143 ReadDefaultSettings(false); | |
| 144 | |
| 145 // Read misc. global settings. | 128 // Read misc. global settings. |
| 146 block_third_party_cookies_ = | 129 block_third_party_cookies_ = |
| 147 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); | 130 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); |
| 148 is_block_third_party_cookies_managed_ = | 131 is_block_third_party_cookies_managed_ = |
| 149 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); | 132 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); |
| 150 block_nonsandboxed_plugins_ = | 133 block_nonsandboxed_plugins_ = |
| 151 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); | 134 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); |
| 152 | 135 |
| 153 // Verify preferences version. | 136 // Verify preferences version. |
| 154 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 137 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { |
| 155 prefs->SetInteger(prefs::kContentSettingsVersion, | 138 prefs->SetInteger(prefs::kContentSettingsVersion, |
| 156 ContentSettingsPattern::kContentSettingsPatternVersion); | 139 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 157 } | 140 } |
| 158 if (prefs->GetInteger(prefs::kContentSettingsVersion) > | 141 if (prefs->GetInteger(prefs::kContentSettingsVersion) > |
| 159 ContentSettingsPattern::kContentSettingsPatternVersion) { | 142 ContentSettingsPattern::kContentSettingsPatternVersion) { |
| 160 LOG(ERROR) << "Unknown content settings version in preferences."; | 143 LOG(ERROR) << "Unknown content settings version in preferences."; |
| 161 return; | 144 return; |
| 162 } | 145 } |
| 163 | 146 |
| 164 // Read exceptions. | 147 // Read exceptions. |
| 165 ReadExceptions(false); | 148 ReadExceptions(false); |
| 166 | 149 |
| 167 pref_change_registrar_.Init(prefs); | 150 pref_change_registrar_.Init(prefs); |
| 168 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | |
| 169 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | 151 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); |
| 170 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); | 152 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); |
| 171 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); | 153 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); |
| 172 // The following preferences are only used to indicate if a | |
| 173 // default-content-setting is managed and to hold the managed default-setting | |
| 174 // value. If the value for any of the following perferences is set then the | |
| 175 // corresponding default-content-setting is managed. These preferences exist | |
| 176 // in parallel to the preference default-content-settings. If a | |
| 177 // default-content-settings-type is managed any user defined excpetions | |
| 178 // (patterns) for this type are ignored. | |
| 179 pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this); | |
| 180 pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this); | |
| 181 pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this); | |
| 182 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); | |
| 183 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); | |
| 184 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 154 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 185 Source<Profile>(profile_)); | 155 Source<Profile>(profile_)); |
| 186 } | 156 } |
| 187 | 157 |
| 188 // static | 158 // static |
| 189 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 159 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 190 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | |
| 191 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, | 160 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, |
| 192 ContentSettingsPattern::kContentSettingsPatternVersion); | 161 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 193 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); | 162 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); |
| 194 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); | 163 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); |
| 195 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); | 164 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); |
| 196 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); | 165 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); |
| 197 | 166 |
| 198 // Preferences for default content setting policies. A policy is not set of | |
| 199 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. | |
| 200 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, | |
| 201 CONTENT_SETTING_DEFAULT); | |
| 202 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, | |
| 203 CONTENT_SETTING_DEFAULT); | |
| 204 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting, | |
| 205 CONTENT_SETTING_DEFAULT); | |
| 206 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting, | |
| 207 CONTENT_SETTING_DEFAULT); | |
| 208 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting, | |
| 209 CONTENT_SETTING_DEFAULT); | |
| 210 | |
| 211 // Obsolete prefs, for migration: | 167 // Obsolete prefs, for migration: |
| 212 prefs->RegisterIntegerPref(prefs::kCookieBehavior, | 168 prefs->RegisterIntegerPref(prefs::kCookieBehavior, |
| 213 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); | 169 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); |
| 214 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); | 170 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); |
| 215 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); | 171 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); |
| 216 } | 172 } |
| 217 | 173 |
| 218 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( | 174 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( |
| 219 ContentSettingsType content_type) const { | 175 ContentSettingsType content_type) const { |
| 220 AutoLock auto_lock(lock_); | 176 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 221 if (IsDefaultContentSettingManaged(content_type)) | 177 for (const_provider_iterator provider = content_settings_providers_.begin(); |
| 222 return managed_default_content_settings_.settings[content_type]; | 178 provider != content_settings_providers_.end(); ++provider) { |
| 223 return default_content_settings_.settings[content_type]; | 179 if (!(*provider)->CanProvideDefaultSetting(content_type)) |
| 180 continue; | |
| 181 ContentSetting provided_setting = | |
| 182 (*provider)->ProvideDefaultSetting(content_type); | |
| 183 if (provided_setting != CONTENT_SETTING_DEFAULT) | |
| 184 setting = provided_setting; | |
| 185 } | |
| 186 // The method GetDefaultContentSetting always has to return an explicit | |
| 187 // value that is to be used as default. We here rely on the | |
| 188 // PrefContentSettingProvider to always provide a value. | |
| 189 CHECK_NE(CONTENT_SETTING_DEFAULT, setting); | |
| 190 return setting; | |
| 224 } | 191 } |
| 225 | 192 |
| 226 ContentSetting HostContentSettingsMap::GetContentSetting( | 193 ContentSetting HostContentSettingsMap::GetContentSetting( |
| 227 const GURL& url, | 194 const GURL& url, |
| 228 ContentSettingsType content_type, | 195 ContentSettingsType content_type, |
| 229 const std::string& resource_identifier) const { | 196 const std::string& resource_identifier) const { |
| 230 ContentSetting setting = GetNonDefaultContentSetting(url, | 197 ContentSetting setting = GetNonDefaultContentSetting(url, |
| 231 content_type, | 198 content_type, |
| 232 resource_identifier); | 199 resource_identifier); |
| 233 if (setting == CONTENT_SETTING_DEFAULT || | 200 if (setting == CONTENT_SETTING_DEFAULT || |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); | 278 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |
| 312 } | 279 } |
| 313 | 280 |
| 314 return CONTENT_SETTING_DEFAULT; | 281 return CONTENT_SETTING_DEFAULT; |
| 315 } | 282 } |
| 316 | 283 |
| 317 ContentSettings HostContentSettingsMap::GetContentSettings( | 284 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 318 const GURL& url) const { | 285 const GURL& url) const { |
| 319 ContentSettings output = GetNonDefaultContentSettings(url); | 286 ContentSettings output = GetNonDefaultContentSettings(url); |
| 320 | 287 |
| 321 AutoLock auto_lock(lock_); | |
| 322 | |
| 323 // If we require a resource identifier, set the content settings to default, | 288 // If we require a resource identifier, set the content settings to default, |
| 324 // otherwise make the defaults explicit. | 289 // otherwise make the defaults explicit. |
| 325 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 290 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 326 if (RequiresResourceIdentifier(ContentSettingsType(j))) { | 291 if (RequiresResourceIdentifier(ContentSettingsType(j))) { |
| 327 output.settings[j] = CONTENT_SETTING_DEFAULT; | 292 output.settings[j] = CONTENT_SETTING_DEFAULT; |
| 328 } else { | 293 } else { |
| 329 if (output.settings[j] == CONTENT_SETTING_DEFAULT) { | |
| 330 output.settings[j] = default_content_settings_.settings[j]; | |
| 331 } | |
| 332 // A managed default content setting has the highest priority and hence | 294 // A managed default content setting has the highest priority and hence |
| 333 // will overwrite any previously set value. | 295 // will overwrite any previously set value. |
| 334 if (IsDefaultContentSettingManaged(ContentSettingsType(j))) { | 296 if ((output.settings[j] == CONTENT_SETTING_DEFAULT) || |
| 335 output.settings[j] = | 297 IsDefaultContentSettingManaged(ContentSettingsType(j))) { |
| 336 managed_default_content_settings_.settings[j]; | 298 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j)); |
| 337 } | 299 } |
| 338 } | 300 } |
| 339 } | 301 } |
| 340 return output; | 302 return output; |
| 341 } | 303 } |
| 342 | 304 |
| 343 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( | 305 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( |
| 344 const GURL& url) const { | 306 const GURL& url) const { |
| 345 if (ShouldAllowAllContent(url)) | 307 if (ShouldAllowAllContent(url)) |
| 346 return ContentSettings(CONTENT_SETTING_ALLOW); | 308 return ContentSettings(CONTENT_SETTING_ALLOW); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 | 396 |
| 435 void HostContentSettingsMap::SetDefaultContentSetting( | 397 void HostContentSettingsMap::SetDefaultContentSetting( |
| 436 ContentSettingsType content_type, | 398 ContentSettingsType content_type, |
| 437 ContentSetting setting) { | 399 ContentSetting setting) { |
| 438 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 400 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 440 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 402 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || |
| 441 setting != CONTENT_SETTING_ASK || | 403 setting != CONTENT_SETTING_ASK || |
| 442 CommandLine::ForCurrentProcess()->HasSwitch( | 404 CommandLine::ForCurrentProcess()->HasSwitch( |
| 443 switches::kEnableClickToPlay)); | 405 switches::kEnableClickToPlay)); |
| 444 PrefService* prefs = profile_->GetPrefs(); | |
| 445 | 406 |
| 446 // The default settings may not be directly modified for OTR sessions. | 407 // The default settings may not be directly modified for OTR sessions. |
| 447 // Instead, they are synced to the main profile's setting. | 408 // Instead, they are synced to the main profile's setting. |
| 448 if (is_off_the_record_) { | 409 if (is_off_the_record_) { |
| 449 NOTREACHED(); | 410 NOTREACHED(); |
| 450 return; | 411 return; |
| 451 } | 412 } |
| 452 | 413 |
| 453 DictionaryValue* default_settings_dictionary = | 414 for (provider_iterator provider = content_settings_providers_.begin(); |
| 454 prefs->GetMutableDictionary(prefs::kDefaultContentSettings); | 415 provider != content_settings_providers_.end(); ++provider) { |
| 455 std::string dictionary_path(kTypeNames[content_type]); | 416 (*provider)->UpdateDefaultSetting(content_type, setting); |
| 456 updating_preferences_ = true; | |
| 457 { | |
| 458 AutoLock auto_lock(lock_); | |
| 459 ScopedPrefUpdate update(prefs, prefs::kDefaultContentSettings); | |
| 460 if ((setting == CONTENT_SETTING_DEFAULT) || | |
| 461 (setting == kDefaultSettings[content_type])) { | |
| 462 default_content_settings_.settings[content_type] = | |
| 463 kDefaultSettings[content_type]; | |
| 464 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | |
| 465 NULL); | |
| 466 } else { | |
| 467 default_content_settings_.settings[content_type] = setting; | |
| 468 default_settings_dictionary->SetWithoutPathExpansion( | |
| 469 dictionary_path, Value::CreateIntegerValue(setting)); | |
| 470 } | |
| 471 } | 417 } |
| 472 updating_preferences_ = false; | |
| 473 | |
| 474 NotifyObservers( | |
| 475 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | |
| 476 } | 418 } |
| 477 | 419 |
| 478 void HostContentSettingsMap::SetContentSetting( | 420 void HostContentSettingsMap::SetContentSetting( |
| 479 const ContentSettingsPattern& original_pattern, | 421 const ContentSettingsPattern& original_pattern, |
| 480 ContentSettingsType content_type, | 422 ContentSettingsType content_type, |
| 481 const std::string& resource_identifier, | 423 const std::string& resource_identifier, |
| 482 ContentSetting setting) { | 424 ContentSetting setting) { |
| 483 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 425 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 426 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 485 DCHECK_NE(RequiresResourceIdentifier(content_type), | 427 DCHECK_NE(RequiresResourceIdentifier(content_type), |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 720 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); | 662 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); |
| 721 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 663 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 722 } | 664 } |
| 723 } | 665 } |
| 724 | 666 |
| 725 void HostContentSettingsMap::ResetToDefaults() { | 667 void HostContentSettingsMap::ResetToDefaults() { |
| 726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 668 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 727 | 669 |
| 728 { | 670 { |
| 729 AutoLock auto_lock(lock_); | 671 AutoLock auto_lock(lock_); |
| 730 default_content_settings_ = ContentSettings(); | 672 for (provider_iterator provider = content_settings_providers_.begin(); |
| 731 ForceDefaultsToBeExplicit(); | 673 provider != content_settings_providers_.end(); ++provider) { |
| 732 // Clear all content settings map except the | 674 (*provider)->ResetToDefaults(); |
| 733 // managed_default_content_settings. | 675 } |
| 734 host_content_settings_.clear(); | 676 host_content_settings_.clear(); |
| 735 off_the_record_settings_.clear(); | 677 off_the_record_settings_.clear(); |
| 736 // Don't reset block third party cookies if they are managed. | 678 // Don't reset block third party cookies if they are managed. |
| 737 if (!IsBlockThirdPartyCookiesManaged()) | 679 if (!IsBlockThirdPartyCookiesManaged()) |
| 738 block_third_party_cookies_ = false; | 680 block_third_party_cookies_ = false; |
| 739 block_nonsandboxed_plugins_ = false; | 681 block_nonsandboxed_plugins_ = false; |
| 740 } | 682 } |
| 741 | 683 |
| 742 if (!is_off_the_record_) { | 684 if (!is_off_the_record_) { |
| 743 PrefService* prefs = profile_->GetPrefs(); | 685 PrefService* prefs = profile_->GetPrefs(); |
| 744 updating_preferences_ = true; | 686 updating_preferences_ = true; |
| 745 prefs->ClearPref(prefs::kDefaultContentSettings); | |
| 746 prefs->ClearPref(prefs::kContentSettingsPatterns); | 687 prefs->ClearPref(prefs::kContentSettingsPatterns); |
| 747 // If the block third party cookies preference is managed we still must | 688 // If the block third party cookies preference is managed we still must |
| 748 // clear it in order to restore the default value for later when the | 689 // clear it in order to restore the default value for later when the |
| 749 // preference is not managed anymore. | 690 // preference is not managed anymore. |
| 750 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 691 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 751 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 692 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 752 updating_preferences_ = false; | 693 updating_preferences_ = false; |
| 753 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 694 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 754 CONTENT_SETTINGS_TYPE_DEFAULT, | 695 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 755 "")); | 696 "")); |
| 756 } | 697 } |
| 757 } | 698 } |
| 758 | 699 |
| 759 void HostContentSettingsMap::Observe(NotificationType type, | 700 void HostContentSettingsMap::Observe(NotificationType type, |
| 760 const NotificationSource& source, | 701 const NotificationSource& source, |
| 761 const NotificationDetails& details) { | 702 const NotificationDetails& details) { |
| 762 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 763 | 704 |
| 764 if (NotificationType::PREF_CHANGED == type) { | 705 if (type == NotificationType::PREF_CHANGED) { |
| 706 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | |
| 765 if (updating_preferences_) | 707 if (updating_preferences_) |
| 766 return; | 708 return; |
| 767 | 709 |
| 768 std::string* name = Details<std::string>(details).ptr(); | 710 std::string* name = Details<std::string>(details).ptr(); |
| 769 if (prefs::kDefaultContentSettings == *name) { | 711 if (*name == prefs::kContentSettingsPatterns) { |
| 770 ReadDefaultSettings(true); | |
| 771 } else if (prefs::kContentSettingsPatterns == *name) { | |
| 772 ReadExceptions(true); | 712 ReadExceptions(true); |
| 773 } else if (prefs::kBlockThirdPartyCookies == *name) { | 713 } else if (*name == prefs::kBlockThirdPartyCookies) { |
| 774 AutoLock auto_lock(lock_); | 714 AutoLock auto_lock(lock_); |
| 775 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 715 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( |
| 776 prefs::kBlockThirdPartyCookies); | 716 prefs::kBlockThirdPartyCookies); |
| 777 is_block_third_party_cookies_managed_ = | 717 is_block_third_party_cookies_managed_ = |
| 778 profile_->GetPrefs()->IsManagedPreference( | 718 profile_->GetPrefs()->IsManagedPreference( |
| 779 prefs::kBlockThirdPartyCookies); | 719 prefs::kBlockThirdPartyCookies); |
| 780 } else if (prefs::kBlockNonsandboxedPlugins == *name) { | 720 } else if (*name == prefs::kBlockNonsandboxedPlugins) { |
| 781 AutoLock auto_lock(lock_); | 721 AutoLock auto_lock(lock_); |
| 782 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( | 722 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( |
| 783 prefs::kBlockNonsandboxedPlugins); | 723 prefs::kBlockNonsandboxedPlugins); |
| 784 } else if (prefs::kManagedDefaultCookiesSetting == *name) { | |
| 785 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES, | |
| 786 profile_->GetPrefs(), | |
| 787 &managed_default_content_settings_); | |
| 788 } else if (prefs::kManagedDefaultImagesSetting == *name) { | |
| 789 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_IMAGES, | |
| 790 profile_->GetPrefs(), | |
| 791 &managed_default_content_settings_); | |
| 792 } else if (prefs::kManagedDefaultJavaScriptSetting == *name) { | |
| 793 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
| 794 profile_->GetPrefs(), | |
| 795 &managed_default_content_settings_); | |
| 796 } else if (prefs::kManagedDefaultPluginsSetting == *name) { | |
| 797 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 798 profile_->GetPrefs(), | |
| 799 &managed_default_content_settings_); | |
| 800 } else if (prefs::kManagedDefaultPopupsSetting == *name) { | |
| 801 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_POPUPS, | |
| 802 profile_->GetPrefs(), | |
| 803 &managed_default_content_settings_); | |
| 804 } else { | 724 } else { |
| 805 NOTREACHED() << "Unexpected preference observed"; | 725 NOTREACHED() << "Unexpected preference observed"; |
| 806 return; | 726 return; |
| 807 } | 727 } |
| 808 | 728 |
| 809 if (!is_off_the_record_) { | 729 if (!is_off_the_record_) { |
| 810 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 730 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 811 CONTENT_SETTINGS_TYPE_DEFAULT, | 731 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 812 "")); | 732 "")); |
| 813 } | 733 } |
| 814 } else if (NotificationType::PROFILE_DESTROYED == type) { | 734 } else if (type == NotificationType::PROFILE_DESTROYED) { |
| 735 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | |
| 815 UnregisterObservers(); | 736 UnregisterObservers(); |
| 816 } else { | 737 } else { |
| 817 NOTREACHED() << "Unexpected notification"; | 738 NOTREACHED() << "Unexpected notification"; |
| 818 } | 739 } |
| 819 } | 740 } |
| 820 | 741 |
| 821 HostContentSettingsMap::~HostContentSettingsMap() { | 742 HostContentSettingsMap::~HostContentSettingsMap() { |
| 822 UnregisterObservers(); | 743 UnregisterObservers(); |
| 823 } | 744 } |
| 824 | 745 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 874 ClickToPlayFixup(ContentSettingsType(type), | 795 ClickToPlayFixup(ContentSettingsType(type), |
| 875 ContentSetting(setting)); | 796 ContentSetting(setting)); |
| 876 } | 797 } |
| 877 | 798 |
| 878 break; | 799 break; |
| 879 } | 800 } |
| 880 } | 801 } |
| 881 } | 802 } |
| 882 } | 803 } |
| 883 | 804 |
| 884 void HostContentSettingsMap::ForceDefaultsToBeExplicit() { | |
| 885 DCHECK_EQ(arraysize(kDefaultSettings), | |
| 886 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES)); | |
| 887 | |
| 888 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { | |
| 889 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT) | |
| 890 default_content_settings_.settings[i] = kDefaultSettings[i]; | |
| 891 } | |
| 892 } | |
| 893 | |
| 894 bool HostContentSettingsMap::AllDefault( | 805 bool HostContentSettingsMap::AllDefault( |
| 895 const ExtendedContentSettings& settings) const { | 806 const ExtendedContentSettings& settings) const { |
| 896 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { | 807 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { |
| 897 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) | 808 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) |
| 898 return false; | 809 return false; |
| 899 } | 810 } |
| 900 return settings.content_settings_for_resources.empty(); | 811 return settings.content_settings_for_resources.empty(); |
| 901 } | 812 } |
| 902 | 813 |
| 903 void HostContentSettingsMap::ReadDefaultSettings(bool overwrite) { | |
| 904 PrefService* prefs = profile_->GetPrefs(); | |
| 905 const DictionaryValue* default_settings_dictionary = | |
| 906 prefs->GetDictionary(prefs::kDefaultContentSettings); | |
| 907 | |
| 908 if (overwrite) | |
| 909 default_content_settings_ = ContentSettings(); | |
| 910 | |
| 911 // Careful: The returned value could be NULL if the pref has never been set. | |
| 912 if (default_settings_dictionary != NULL) { | |
| 913 GetSettingsFromDictionary(default_settings_dictionary, | |
| 914 &default_content_settings_); | |
| 915 } | |
| 916 ForceDefaultsToBeExplicit(); | |
| 917 | |
| 918 // Read managed default content settings. | |
| 919 ReadManagedDefaultSettings(prefs, &managed_default_content_settings_); | |
| 920 } | |
| 921 | |
| 922 void HostContentSettingsMap::ReadManagedDefaultSettings( | |
| 923 const PrefService* prefs, ContentSettings* settings) { | |
| 924 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) { | |
| 925 if (kPrefToManageType[type] == NULL) { | |
| 926 // TODO(markusheintz): Handle Geolocation and notification separately. | |
| 927 continue; | |
| 928 } | |
| 929 UpdateManagedDefaultSetting(ContentSettingsType(type), prefs, settings); | |
| 930 } | |
| 931 } | |
| 932 | |
| 933 void HostContentSettingsMap::UpdateManagedDefaultSetting( | |
| 934 ContentSettingsType type, | |
| 935 const PrefService* prefs, | |
| 936 ContentSettings* settings) { | |
| 937 // If a pref to manage a default-content-setting was not set (NOTICE: | |
| 938 // "HasPrefPath" returns false if no value was set for a registered pref) then | |
| 939 // the default value of the preference is used. The default value of a | |
| 940 // preference to manage a default-content-settings is | |
| 941 // CONTENT_SETTING_DEFAULT. This indicates that no managed value is set. If a | |
| 942 // pref was set, than it MUST be managed. | |
| 943 DCHECK(!prefs->HasPrefPath(kPrefToManageType[type]) || | |
| 944 prefs->IsManagedPreference(kPrefToManageType[type])); | |
| 945 AutoLock auto_lock(lock_); | |
| 946 settings->settings[type] = IntToContentSetting( | |
| 947 prefs->GetInteger(kPrefToManageType[type])); | |
| 948 } | |
| 949 | |
| 950 bool HostContentSettingsMap::IsDefaultContentSettingManaged( | 814 bool HostContentSettingsMap::IsDefaultContentSettingManaged( |
| 951 ContentSettingsType content_type) const { | 815 ContentSettingsType content_type) const { |
| 952 // All managed_default_content_settings_ are always set explicitly or | 816 for (const_provider_iterator provider = content_settings_providers_.begin(); |
| 953 // initialized to CONTENT_SETTINGS_DEFAULT. Hence each content settings type | 817 provider != content_settings_providers_.end(); ++provider) { |
| 954 // that is set to CONTENT_SETTINGS_DEFAULT is not managed since it was not set | 818 if ((*provider)->DefaultSettingIsManaged(content_type)) |
| 955 // explicitly. | 819 return true; |
| 956 return managed_default_content_settings_.settings[content_type] != | 820 } |
| 957 CONTENT_SETTING_DEFAULT; | 821 return false; |
| 958 } | 822 } |
| 959 | 823 |
| 960 void HostContentSettingsMap::ReadExceptions(bool overwrite) { | 824 void HostContentSettingsMap::ReadExceptions(bool overwrite) { |
| 961 PrefService* prefs = profile_->GetPrefs(); | 825 PrefService* prefs = profile_->GetPrefs(); |
| 962 DictionaryValue* all_settings_dictionary = | 826 DictionaryValue* all_settings_dictionary = |
| 963 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 827 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |
| 964 | 828 |
| 965 if (overwrite) | 829 if (overwrite) |
| 966 host_content_settings_.clear(); | 830 host_content_settings_.clear(); |
|
gfeher
2010/12/09 16:09:03
Please protect this with a lock while you are here
| |
| 967 | 831 |
| 968 // Careful: The returned value could be NULL if the pref has never been set. | 832 // Careful: The returned value could be NULL if the pref has never been set. |
| 969 if (all_settings_dictionary != NULL) { | 833 if (all_settings_dictionary != NULL) { |
| 970 // Convert all Unicode patterns into punycode form, then read. | 834 // Convert all Unicode patterns into punycode form, then read. |
| 971 CanonicalizeContentSettingsExceptions(all_settings_dictionary); | 835 CanonicalizeContentSettingsExceptions(all_settings_dictionary); |
| 972 | 836 |
| 973 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 837 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 974 i != all_settings_dictionary->end_keys(); ++i) { | 838 i != all_settings_dictionary->end_keys(); ++i) { |
| 975 const std::string& pattern(*i); | 839 const std::string& pattern(*i); |
| 976 if (!ContentSettingsPattern(pattern).IsValid()) | 840 if (!ContentSettingsPattern(pattern).IsValid()) |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1102 } | 966 } |
| 1103 | 967 |
| 1104 for (size_t i = 0; i < move_items.size(); ++i) { | 968 for (size_t i = 0; i < move_items.size(); ++i) { |
| 1105 Value* pattern_settings_dictionary = NULL; | 969 Value* pattern_settings_dictionary = NULL; |
| 1106 all_settings_dictionary->RemoveWithoutPathExpansion( | 970 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 1107 move_items[i].first, &pattern_settings_dictionary); | 971 move_items[i].first, &pattern_settings_dictionary); |
| 1108 all_settings_dictionary->SetWithoutPathExpansion( | 972 all_settings_dictionary->SetWithoutPathExpansion( |
| 1109 move_items[i].second, pattern_settings_dictionary); | 973 move_items[i].second, pattern_settings_dictionary); |
| 1110 } | 974 } |
| 1111 } | 975 } |
| OLD | NEW |