Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "chrome/common/notification_type.h" | 21 #include "chrome/common/notification_type.h" |
| 22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 24 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 25 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
| 26 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
| 27 #include "net/base/static_cookie_policy.h" | 27 #include "net/base/static_cookie_policy.h" |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 // The preference keys where resource identifiers are stored for | |
| 32 // ContentSettingsType values that support resource identifiers. | |
| 33 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 34 NULL, | |
| 35 NULL, | |
| 36 NULL, | |
| 37 "per_plugin", | |
| 38 NULL, | |
| 39 NULL, // Not used for Geolocation | |
| 40 NULL, // Not used for Notifications | |
| 41 }; | |
| 42 | |
| 43 // The names of the ContentSettingsType values, for use with dictionary prefs. | |
| 44 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 45 "cookies", | |
| 46 "images", | |
| 47 "javascript", | |
| 48 "plugins", | |
| 49 "popups", | |
| 50 NULL, // Not used for Geolocation | |
| 51 NULL, // Not used for Notifications | |
| 52 }; | |
| 53 | |
| 54 // True if a given content settings type requires additional resource | |
| 55 // identifiers. | |
| 56 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 57 false, // CONTENT_SETTINGS_TYPE_COOKIES | |
| 58 false, // CONTENT_SETTINGS_TYPE_IMAGES | |
| 59 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT | |
| 60 true, // CONTENT_SETTINGS_TYPE_PLUGINS | |
| 61 false, // CONTENT_SETTINGS_TYPE_POPUPS | |
| 62 false, // Not used for Geolocation | |
| 63 false, // Not used for Notifications | |
| 64 }; | |
| 65 | |
| 66 // Returns true if we should allow all content types for this URL. This is | 31 // Returns true if we should allow all content types for this URL. This is |
| 67 // true for various internal objects like chrome:// URLs, so UI and other | 32 // true for various internal objects like chrome:// URLs, so UI and other |
| 68 // things users think of as "not webpages" don't break. | 33 // things users think of as "not webpages" don't break. |
| 69 static bool ShouldAllowAllContent(const GURL& url) { | 34 static bool ShouldAllowAllContent(const GURL& url) { |
| 70 return url.SchemeIs(chrome::kChromeDevToolsScheme) || | 35 return url.SchemeIs(chrome::kChromeDevToolsScheme) || |
| 71 url.SchemeIs(chrome::kChromeInternalScheme) || | 36 url.SchemeIs(chrome::kChromeInternalScheme) || |
| 72 url.SchemeIs(chrome::kChromeUIScheme) || | 37 url.SchemeIs(chrome::kChromeUIScheme) || |
| 73 url.SchemeIs(chrome::kExtensionScheme) || | 38 url.SchemeIs(chrome::kExtensionScheme) || |
| 74 url.SchemeIs(chrome::kGearsScheme) || | 39 url.SchemeIs(chrome::kGearsScheme) || |
| 75 url.SchemeIs(chrome::kUserScriptScheme); | 40 url.SchemeIs(chrome::kUserScriptScheme); |
| 76 } | 41 } |
| 77 | 42 |
| 78 // Map ASK for the plugins content type to BLOCK if click-to-play is | |
| 79 // not enabled. | |
| 80 ContentSetting ClickToPlayFixup(ContentSettingsType content_type, | |
| 81 ContentSetting setting) { | |
| 82 if (setting == CONTENT_SETTING_ASK && | |
| 83 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && | |
| 84 !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 85 switches::kEnableClickToPlay)) { | |
| 86 return CONTENT_SETTING_BLOCK; | |
| 87 } | |
| 88 return setting; | |
| 89 } | |
| 90 | |
| 91 typedef linked_ptr<content_settings::DefaultProviderInterface> | 43 typedef linked_ptr<content_settings::DefaultProviderInterface> |
| 92 DefaultContentSettingsProviderPtr; | 44 DefaultContentSettingsProviderPtr; |
| 93 typedef std::vector<DefaultContentSettingsProviderPtr>::iterator | 45 typedef std::vector<DefaultContentSettingsProviderPtr>::iterator |
| 94 provider_iterator; | 46 default_provider_iterator; |
| 95 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator | 47 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator |
| 96 const_provider_iterator; | 48 const_default_provider_iterator; |
| 49 | |
| 50 typedef linked_ptr<content_settings::ProviderInterface> ProviderPtr; | |
| 51 typedef std::vector<ProviderPtr>::iterator provider_iterator; | |
| 52 typedef std::vector<ProviderPtr>::const_iterator const_provider_iterator; | |
| 53 | |
| 54 typedef content_settings::ProviderInterface::Rules Rules; | |
| 55 typedef content_settings::ProviderInterface::Rules::iterator | |
| 56 rules_iterator; | |
| 57 typedef content_settings::ProviderInterface::Rules::const_iterator | |
| 58 const_rules_iterator; | |
| 97 | 59 |
| 98 } // namespace | 60 } // namespace |
| 99 | 61 |
| 100 | |
| 101 struct HostContentSettingsMap::ExtendedContentSettings { | |
| 102 ContentSettings content_settings; | |
| 103 ResourceContentSettings content_settings_for_resources; | |
| 104 }; | |
| 105 | |
| 106 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) | 62 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) |
| 107 : profile_(profile), | 63 : profile_(profile), |
| 108 is_off_the_record_(profile_->IsOffTheRecord()), | 64 is_off_the_record_(profile_->IsOffTheRecord()), |
| 109 updating_preferences_(false), | 65 updating_preferences_(false), |
| 110 block_third_party_cookies_(false), | 66 block_third_party_cookies_(false), |
| 111 is_block_third_party_cookies_managed_(false) { | 67 is_block_third_party_cookies_managed_(false) { |
| 112 // The order in which the content settings providers are created is critical, | 68 // The order in which the content settings providers are created is critical, |
| 113 // as providers that are further down in the list (i.e. added later) override | 69 // as providers that are further down in the list (i.e. added later) override |
| 114 // providers further up. | 70 // providers further up. |
| 115 default_content_settings_providers_.push_back( | 71 default_content_settings_providers_.push_back( |
| 116 DefaultContentSettingsProviderPtr( | 72 DefaultContentSettingsProviderPtr( |
| 117 new content_settings::PrefDefaultProvider(profile))); | 73 new content_settings::PrefDefaultProvider(profile))); |
| 118 default_content_settings_providers_.push_back( | 74 default_content_settings_providers_.push_back( |
| 119 DefaultContentSettingsProviderPtr( | 75 DefaultContentSettingsProviderPtr( |
| 120 new content_settings::PolicyDefaultProvider(profile))); | 76 new content_settings::PolicyDefaultProvider(profile))); |
| 121 | 77 |
| 122 PrefService* prefs = profile_->GetPrefs(); | 78 PrefService* prefs = profile_->GetPrefs(); |
| 123 | 79 |
| 80 // TODO(markusheintz): Discuss whether it is sensible to move migration code | |
| 81 // to PrefContentSettingsProvider. | |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
sure.
markusheintz_
2011/02/04 10:08:03
Cool:-) When I added that comment I was afraid tha
| |
| 124 MigrateObsoleteCookiePref(prefs); | 82 MigrateObsoleteCookiePref(prefs); |
| 125 | 83 |
| 126 MigrateObsoletePopupsPref(prefs); | |
| 127 | |
| 128 MigrateObsoletePerhostPref(prefs); | |
| 129 | |
| 130 // Read misc. global settings. | 84 // Read misc. global settings. |
| 131 block_third_party_cookies_ = | 85 block_third_party_cookies_ = |
| 132 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); | 86 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); |
| 133 is_block_third_party_cookies_managed_ = | 87 is_block_third_party_cookies_managed_ = |
| 134 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); | 88 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); |
| 135 block_nonsandboxed_plugins_ = | 89 block_nonsandboxed_plugins_ = |
| 136 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); | 90 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); |
| 137 | 91 |
| 138 // Verify preferences version. | 92 // User defined non default content settings are managed provided by the |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
s/managed //
markusheintz_
2011/02/04 10:08:03
Done.
| |
| 139 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 93 // PrefProvider. |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
please add a comment that the order is critical. W
markusheintz_
2011/02/04 10:08:03
Done.
| |
| 140 prefs->SetInteger(prefs::kContentSettingsVersion, | 94 content_settings_providers_.push_back(ProviderPtr( |
| 141 ContentSettingsPattern::kContentSettingsPatternVersion); | 95 new content_settings::PrefProvider(profile))); |
| 142 } | |
| 143 if (prefs->GetInteger(prefs::kContentSettingsVersion) > | |
| 144 ContentSettingsPattern::kContentSettingsPatternVersion) { | |
| 145 LOG(ERROR) << "Unknown content settings version in preferences."; | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 // Read exceptions. | |
| 150 ReadExceptions(false); | |
| 151 | 96 |
| 152 pref_change_registrar_.Init(prefs); | 97 pref_change_registrar_.Init(prefs); |
| 153 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | |
| 154 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); | 98 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); |
| 155 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); | 99 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); |
| 156 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 100 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 157 Source<Profile>(profile_)); | 101 Source<Profile>(profile_)); |
| 158 } | 102 } |
| 159 | 103 |
| 160 // static | 104 // static |
| 161 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 105 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 162 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, | |
| 163 ContentSettingsPattern::kContentSettingsPatternVersion); | |
| 164 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); | |
| 165 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); | 106 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); |
| 166 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); | 107 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); |
| 167 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); | 108 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); |
| 168 | 109 |
| 169 // Obsolete prefs, for migration: | 110 // Obsolete prefs, for migration: |
| 170 prefs->RegisterIntegerPref(prefs::kCookieBehavior, | 111 prefs->RegisterIntegerPref(prefs::kCookieBehavior, |
| 171 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); | 112 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); |
| 172 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); | |
| 173 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); | |
| 174 | 113 |
| 175 // Register the prefs for the content settings providers. | 114 // Register the prefs for the content settings providers. |
| 176 content_settings::PrefDefaultProvider::RegisterUserPrefs(prefs); | 115 content_settings::PrefDefaultProvider::RegisterUserPrefs(prefs); |
| 177 content_settings::PolicyDefaultProvider::RegisterUserPrefs(prefs); | 116 content_settings::PolicyDefaultProvider::RegisterUserPrefs(prefs); |
| 117 content_settings::PrefProvider::RegisterUserPrefs(prefs); | |
| 178 } | 118 } |
| 179 | 119 |
| 180 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( | 120 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( |
| 181 ContentSettingsType content_type) const { | 121 ContentSettingsType content_type) const { |
| 182 ContentSetting setting = CONTENT_SETTING_DEFAULT; | 122 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 183 for (const_provider_iterator provider = | 123 for (const_default_provider_iterator provider = |
| 184 default_content_settings_providers_.begin(); | 124 default_content_settings_providers_.begin(); |
| 185 provider != default_content_settings_providers_.end(); ++provider) { | 125 provider != default_content_settings_providers_.end(); ++provider) { |
| 186 ContentSetting provided_setting = | 126 ContentSetting provided_setting = |
| 187 (*provider)->ProvideDefaultSetting(content_type); | 127 (*provider)->ProvideDefaultSetting(content_type); |
| 188 if (provided_setting != CONTENT_SETTING_DEFAULT) | 128 if (provided_setting != CONTENT_SETTING_DEFAULT) |
| 189 setting = provided_setting; | 129 setting = provided_setting; |
| 190 } | 130 } |
| 191 // The method GetDefaultContentSetting always has to return an explicit | 131 // The method GetDefaultContentSetting always has to return an explicit |
| 192 // value that is to be used as default. We here rely on the | 132 // value that is to be used as default. We here rely on the |
| 193 // PrefContentSettingProvider to always provide a value. | 133 // PrefContentSettingProvider to always provide a value. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 206 IsDefaultContentSettingManaged(content_type)) { | 146 IsDefaultContentSettingManaged(content_type)) { |
| 207 return GetDefaultContentSetting(content_type); | 147 return GetDefaultContentSetting(content_type); |
| 208 } | 148 } |
| 209 return setting; | 149 return setting; |
| 210 } | 150 } |
| 211 | 151 |
| 212 ContentSetting HostContentSettingsMap::GetNonDefaultContentSetting( | 152 ContentSetting HostContentSettingsMap::GetNonDefaultContentSetting( |
| 213 const GURL& url, | 153 const GURL& url, |
| 214 ContentSettingsType content_type, | 154 ContentSettingsType content_type, |
| 215 const std::string& resource_identifier) const { | 155 const std::string& resource_identifier) const { |
| 156 // Check if the URL is matched be a whitlisted scheme. | |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
I don't think we need this comment, as the name of
markusheintz_
2011/02/04 10:08:03
Removed this line.
| |
| 216 if (ShouldAllowAllContent(url)) | 157 if (ShouldAllowAllContent(url)) |
| 217 return CONTENT_SETTING_ALLOW; | 158 return CONTENT_SETTING_ALLOW; |
| 218 | 159 |
| 219 if (!RequiresResourceIdentifier(content_type)) | 160 // Iterate through the list of providers and break when the first non default |
| 220 return GetNonDefaultContentSettings(url).settings[content_type]; | 161 // setting is found. |
| 221 | 162 ContentSetting provided_setting(CONTENT_SETTING_DEFAULT); |
| 222 if (CommandLine::ForCurrentProcess()->HasSwitch( | 163 for (const_provider_iterator provider = content_settings_providers_.begin(); |
| 223 switches::kEnableResourceContentSettings)) { | 164 provider != content_settings_providers_.end(); |
| 224 DCHECK(!resource_identifier.empty()); | 165 ++provider) { |
| 166 provided_setting = (*provider)->GetContentSetting( | |
| 167 url, url, content_type, resource_identifier); | |
| 168 if (provided_setting != CONTENT_SETTING_DEFAULT) | |
| 169 break; | |
| 225 } | 170 } |
| 226 | 171 return provided_setting; |
| 227 // Host content settings are ignored if the default_content_setting is | |
| 228 // managed. | |
| 229 if (IsDefaultContentSettingManaged(content_type)) { | |
| 230 return GetDefaultContentSetting(content_type); | |
| 231 } | |
| 232 | |
| 233 base::AutoLock auto_lock(lock_); | |
| 234 | |
| 235 const std::string host(net::GetHostOrSpecFromURL(url)); | |
| 236 ContentSettingsTypeResourceIdentifierPair | |
| 237 requested_setting(content_type, resource_identifier); | |
| 238 | |
| 239 // Check for exact matches first. | |
| 240 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | |
| 241 if (i != host_content_settings_.end() && | |
| 242 i->second.content_settings_for_resources.find(requested_setting) != | |
| 243 i->second.content_settings_for_resources.end()) { | |
| 244 return i->second.content_settings_for_resources.find( | |
| 245 requested_setting)->second; | |
| 246 } | |
| 247 | |
| 248 // If this map is not for an off-the-record profile, these searches will never | |
| 249 // match. The additional off-the-record exceptions always overwrite the | |
| 250 // regular ones. | |
| 251 i = off_the_record_settings_.find(host); | |
| 252 if (i != off_the_record_settings_.end() && | |
| 253 i->second.content_settings_for_resources.find(requested_setting) != | |
| 254 i->second.content_settings_for_resources.end()) { | |
| 255 return i->second.content_settings_for_resources.find( | |
| 256 requested_setting)->second; | |
| 257 } | |
| 258 | |
| 259 // Match patterns starting with the most concrete pattern match. | |
| 260 for (std::string key = | |
| 261 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { | |
| 262 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | |
| 263 if (i != off_the_record_settings_.end() && | |
| 264 i->second.content_settings_for_resources.find(requested_setting) != | |
| 265 i->second.content_settings_for_resources.end()) { | |
| 266 return i->second.content_settings_for_resources.find( | |
| 267 requested_setting)->second; | |
| 268 } | |
| 269 | |
| 270 i = host_content_settings_.find(key); | |
| 271 if (i != host_content_settings_.end() && | |
| 272 i->second.content_settings_for_resources.find(requested_setting) != | |
| 273 i->second.content_settings_for_resources.end()) { | |
| 274 return i->second.content_settings_for_resources.find( | |
| 275 requested_setting)->second; | |
| 276 } | |
| 277 | |
| 278 const size_t next_dot = | |
| 279 key.find('.', ContentSettingsPattern::kDomainWildcardLength); | |
| 280 if (next_dot == std::string::npos) | |
| 281 break; | |
| 282 key.erase(ContentSettingsPattern::kDomainWildcardLength, | |
| 283 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); | |
| 284 } | |
| 285 | |
| 286 return CONTENT_SETTING_DEFAULT; | |
| 287 } | 172 } |
| 288 | 173 |
| 289 ContentSettings HostContentSettingsMap::GetContentSettings( | 174 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 290 const GURL& url) const { | 175 const GURL& url) const { |
| 291 ContentSettings output = GetNonDefaultContentSettings(url); | 176 ContentSettings output = GetNonDefaultContentSettings(url); |
| 292 | 177 |
| 293 // If we require a resource identifier, set the content settings to default, | 178 // If we require a resource identifier, set the content settings to default, |
| 294 // otherwise make the defaults explicit. | 179 // otherwise make the defaults explicit. |
| 295 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 180 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 296 if (RequiresResourceIdentifier(ContentSettingsType(j))) { | 181 if (content_settings::PrefProvider::RequiresResourceIdentifier( |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
can't all providers just return DEFAULT in this ca
markusheintz_
2011/02/04 10:08:03
Yeah that would be much nicer. I will need to chec
| |
| 182 ContentSettingsType(j))) { | |
| 297 output.settings[j] = CONTENT_SETTING_DEFAULT; | 183 output.settings[j] = CONTENT_SETTING_DEFAULT; |
| 298 } else { | 184 } else { |
| 299 // A managed default content setting has the highest priority and hence | 185 // A managed default content setting has the highest priority and hence |
| 300 // will overwrite any previously set value. | 186 // will overwrite any previously set value. |
| 301 if ((output.settings[j] == CONTENT_SETTING_DEFAULT && | 187 if ((output.settings[j] == CONTENT_SETTING_DEFAULT && |
| 302 j != CONTENT_SETTINGS_TYPE_PLUGINS) || | 188 j != CONTENT_SETTINGS_TYPE_PLUGINS) || |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
I think we could also remove this test, and just o
markusheintz_
2011/02/04 10:08:03
This would also set the content settings CONTENT_S
| |
| 303 IsDefaultContentSettingManaged(ContentSettingsType(j))) { | 189 IsDefaultContentSettingManaged(ContentSettingsType(j))) { |
| 304 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j)); | 190 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j)); |
| 305 } | 191 } |
| 306 } | 192 } |
| 307 } | 193 } |
| 308 return output; | 194 return output; |
| 309 } | 195 } |
| 310 | 196 |
| 311 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( | 197 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( |
| 312 const GURL& url) const { | 198 const GURL& url) const { |
| 313 if (ShouldAllowAllContent(url)) | 199 if (ShouldAllowAllContent(url)) |
| 314 return ContentSettings(CONTENT_SETTING_ALLOW); | 200 return ContentSettings(CONTENT_SETTING_ALLOW); |
| 315 | 201 |
| 316 base::AutoLock auto_lock(lock_); | 202 ContentSettings output(CONTENT_SETTING_DEFAULT); |
| 317 | 203 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 318 const std::string host(net::GetHostOrSpecFromURL(url)); | 204 if (content_settings::PrefProvider::RequiresResourceIdentifier( |
| 319 ContentSettings output; | 205 ContentSettingsType(j))) { |
| 320 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | 206 output.settings[j] = CONTENT_SETTING_DEFAULT; |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
again, just assume the provider returns _DEFAULT
markusheintz_
2011/02/04 10:08:03
Done.
| |
| 321 output.settings[j] = CONTENT_SETTING_DEFAULT; | 207 } else { |
| 322 | 208 output.settings[j] = GetNonDefaultContentSetting( |
| 323 // Check for exact matches first. | 209 url, ContentSettingsType(j) , ""); |
| 324 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | 210 } |
| 325 if (i != host_content_settings_.end()) | |
| 326 output = i->second.content_settings; | |
| 327 | |
| 328 // If this map is not for an off-the-record profile, these searches will never | |
| 329 // match. The additional off-the-record exceptions always overwrite the | |
| 330 // regular ones. | |
| 331 i = off_the_record_settings_.find(host); | |
| 332 if (i != off_the_record_settings_.end()) { | |
| 333 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | |
| 334 if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) | |
| 335 output.settings[j] = i->second.content_settings.settings[j]; | |
| 336 } | 211 } |
| 337 | |
| 338 // Match patterns starting with the most concrete pattern match. | |
| 339 for (std::string key = | |
| 340 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { | |
| 341 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | |
| 342 if (i != off_the_record_settings_.end()) { | |
| 343 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | |
| 344 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | |
| 345 output.settings[j] = i->second.content_settings.settings[j]; | |
| 346 } | |
| 347 } | |
| 348 i = host_content_settings_.find(key); | |
| 349 if (i != host_content_settings_.end()) { | |
| 350 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | |
| 351 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | |
| 352 output.settings[j] = i->second.content_settings.settings[j]; | |
| 353 } | |
| 354 } | |
| 355 const size_t next_dot = | |
| 356 key.find('.', ContentSettingsPattern::kDomainWildcardLength); | |
| 357 if (next_dot == std::string::npos) | |
| 358 break; | |
| 359 key.erase(ContentSettingsPattern::kDomainWildcardLength, | |
| 360 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); | |
| 361 } | |
| 362 | |
| 363 return output; | 212 return output; |
| 364 } | 213 } |
| 365 | 214 |
| 366 void HostContentSettingsMap::GetSettingsForOneType( | 215 void HostContentSettingsMap::GetSettingsForOneType( |
| 367 ContentSettingsType content_type, | 216 ContentSettingsType content_type, |
| 368 const std::string& resource_identifier, | 217 const std::string& resource_identifier, |
| 369 SettingsForOneType* settings) const { | 218 SettingsForOneType* settings) const { |
| 370 DCHECK(RequiresResourceIdentifier(content_type) != | |
| 371 resource_identifier.empty()); | |
| 372 DCHECK(settings); | 219 DCHECK(settings); |
| 373 settings->clear(); | 220 settings->clear(); |
| 374 | 221 |
| 375 const HostContentSettings* map_to_return = | 222 // Collect content_settings::Rules for the given content_type and |
| 376 is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; | 223 // resource_identifier from the content settings providers. |
| 377 ContentSettingsTypeResourceIdentifierPair | 224 Rules content_settings_rules; |
| 378 requested_setting(content_type, resource_identifier); | 225 for (const_provider_iterator provider = content_settings_providers_.begin(); |
| 226 provider != content_settings_providers_.end(); | |
| 227 ++provider) { | |
| 228 // TODO(markusheintz): Only the rules that are applied should be collected. | |
| 229 // AI: Think about and discuss what is sensible here. | |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
i think all rules should be returned, the UI can t
markusheintz_
2011/02/04 10:08:03
Cool with me.
Could we leave it this way for this
| |
| 230 (*provider)->GetAllContentSettingsRules( | |
| 231 content_type, resource_identifier, &content_settings_rules); | |
| 232 } | |
| 379 | 233 |
| 380 base::AutoLock auto_lock(lock_); | 234 // convert Rules to SettingsForOneType |
| 381 for (HostContentSettings::const_iterator i(map_to_return->begin()); | 235 for (const_rules_iterator rule_iterator = |
| 382 i != map_to_return->end(); ++i) { | 236 content_settings_rules.begin(); |
| 383 ContentSetting setting; | 237 rule_iterator != content_settings_rules.end(); |
| 384 if (RequiresResourceIdentifier(content_type)) { | 238 ++rule_iterator) { |
| 385 if (i->second.content_settings_for_resources.find(requested_setting) != | 239 settings->push_back(std::make_pair(ContentSettingsPattern( |
| 386 i->second.content_settings_for_resources.end()) | 240 rule_iterator->requesting_url_pattern), |
| 387 setting = i->second.content_settings_for_resources.find( | 241 rule_iterator->content_setting)); |
| 388 requested_setting)->second; | |
| 389 else | |
| 390 setting = CONTENT_SETTING_DEFAULT; | |
| 391 } else { | |
| 392 setting = i->second.content_settings.settings[content_type]; | |
| 393 } | |
| 394 if (setting != CONTENT_SETTING_DEFAULT) { | |
| 395 // Use of push_back() relies on the map iterator traversing in order of | |
| 396 // ascending keys. | |
| 397 settings->push_back( | |
| 398 std::make_pair(ContentSettingsPattern(i->first), setting)); | |
| 399 } | |
| 400 } | 242 } |
| 401 } | 243 } |
| 402 | 244 |
| 403 void HostContentSettingsMap::SetDefaultContentSetting( | 245 void HostContentSettingsMap::SetDefaultContentSetting( |
| 404 ContentSettingsType content_type, | 246 ContentSettingsType content_type, |
| 405 ContentSetting setting) { | 247 ContentSetting setting) { |
| 406 for (provider_iterator provider = | 248 for (default_provider_iterator provider = |
| 407 default_content_settings_providers_.begin(); | 249 default_content_settings_providers_.begin(); |
| 408 provider != default_content_settings_providers_.end(); ++provider) { | 250 provider != default_content_settings_providers_.end(); ++provider) { |
| 409 (*provider)->UpdateDefaultSetting(content_type, setting); | 251 (*provider)->UpdateDefaultSetting(content_type, setting); |
| 410 } | 252 } |
| 411 } | 253 } |
| 412 | 254 |
| 413 void HostContentSettingsMap::SetContentSetting( | 255 void HostContentSettingsMap::SetContentSetting( |
| 414 const ContentSettingsPattern& original_pattern, | 256 const ContentSettingsPattern& pattern, |
| 415 ContentSettingsType content_type, | 257 ContentSettingsType content_type, |
| 416 const std::string& resource_identifier, | 258 const std::string& resource_identifier, |
| 417 ContentSetting setting) { | 259 ContentSetting setting) { |
| 418 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 260 for (provider_iterator provider = content_settings_providers_.begin(); |
| 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 261 provider != content_settings_providers_.end(); |
| 420 DCHECK_NE(RequiresResourceIdentifier(content_type), | 262 ++provider) { |
| 421 resource_identifier.empty()); | 263 (*provider)->SetContentSetting( |
| 422 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 264 pattern, pattern, content_type, resource_identifier, setting); |
| 423 setting != CONTENT_SETTING_ASK || | |
| 424 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 425 switches::kEnableClickToPlay)); | |
| 426 | |
| 427 const ContentSettingsPattern pattern(original_pattern.CanonicalizePattern()); | |
| 428 | |
| 429 bool early_exit = false; | |
| 430 std::string pattern_str(pattern.AsString()); | |
| 431 PrefService* prefs = NULL; | |
| 432 DictionaryValue* all_settings_dictionary = NULL; | |
| 433 HostContentSettings* map_to_modify = &off_the_record_settings_; | |
| 434 if (!is_off_the_record_) { | |
| 435 prefs = profile_->GetPrefs(); | |
| 436 all_settings_dictionary = | |
| 437 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | |
| 438 map_to_modify = &host_content_settings_; | |
| 439 } | 265 } |
| 440 | |
| 441 { | |
| 442 base::AutoLock auto_lock(lock_); | |
| 443 if (!map_to_modify->count(pattern_str)) | |
| 444 (*map_to_modify)[pattern_str].content_settings = ContentSettings(); | |
| 445 HostContentSettings::iterator | |
| 446 i(map_to_modify->find(pattern_str)); | |
| 447 ContentSettings& settings = i->second.content_settings; | |
| 448 if (RequiresResourceIdentifier(content_type)) { | |
| 449 settings.settings[content_type] = CONTENT_SETTING_DEFAULT; | |
| 450 if (setting != CONTENT_SETTING_DEFAULT) { | |
| 451 i->second.content_settings_for_resources[ | |
| 452 ContentSettingsTypeResourceIdentifierPair(content_type, | |
| 453 resource_identifier)] = setting; | |
| 454 } else { | |
| 455 i->second.content_settings_for_resources.erase( | |
| 456 ContentSettingsTypeResourceIdentifierPair(content_type, | |
| 457 resource_identifier)); | |
| 458 } | |
| 459 } else { | |
| 460 settings.settings[content_type] = setting; | |
| 461 } | |
| 462 if (AllDefault(i->second)) { | |
| 463 map_to_modify->erase(i); | |
| 464 if (all_settings_dictionary) | |
| 465 all_settings_dictionary->RemoveWithoutPathExpansion(pattern_str, NULL); | |
| 466 | |
| 467 // We can't just return because |NotifyObservers()| needs to be called, | |
| 468 // without |lock_| being held. | |
| 469 early_exit = true; | |
| 470 } | |
| 471 } | |
| 472 | |
| 473 if (!early_exit && all_settings_dictionary) { | |
| 474 DictionaryValue* host_settings_dictionary = NULL; | |
| 475 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 476 pattern_str, &host_settings_dictionary); | |
| 477 if (!found) { | |
| 478 host_settings_dictionary = new DictionaryValue; | |
| 479 all_settings_dictionary->SetWithoutPathExpansion( | |
| 480 pattern_str, host_settings_dictionary); | |
| 481 DCHECK_NE(setting, CONTENT_SETTING_DEFAULT); | |
| 482 } | |
| 483 if (RequiresResourceIdentifier(content_type)) { | |
| 484 std::string dictionary_path(kResourceTypeNames[content_type]); | |
| 485 DictionaryValue* resource_dictionary = NULL; | |
| 486 found = host_settings_dictionary->GetDictionary( | |
| 487 dictionary_path, &resource_dictionary); | |
| 488 if (!found) { | |
| 489 resource_dictionary = new DictionaryValue; | |
| 490 host_settings_dictionary->Set(dictionary_path, resource_dictionary); | |
| 491 } | |
| 492 if (setting == CONTENT_SETTING_DEFAULT) { | |
| 493 resource_dictionary->RemoveWithoutPathExpansion(resource_identifier, | |
| 494 NULL); | |
| 495 } else { | |
| 496 resource_dictionary->SetWithoutPathExpansion( | |
| 497 resource_identifier, Value::CreateIntegerValue(setting)); | |
| 498 } | |
| 499 } else { | |
| 500 std::string dictionary_path(kTypeNames[content_type]); | |
| 501 if (setting == CONTENT_SETTING_DEFAULT) { | |
| 502 host_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | |
| 503 NULL); | |
| 504 } else { | |
| 505 host_settings_dictionary->SetWithoutPathExpansion( | |
| 506 dictionary_path, Value::CreateIntegerValue(setting)); | |
| 507 } | |
| 508 } | |
| 509 } | |
| 510 | |
| 511 updating_preferences_ = true; | |
| 512 if (!is_off_the_record_) | |
| 513 ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | |
| 514 updating_preferences_ = false; | |
| 515 | |
| 516 NotifyObservers(ContentSettingsDetails(pattern, content_type, "")); | |
| 517 } | 266 } |
| 518 | 267 |
| 519 void HostContentSettingsMap::AddExceptionForURL( | 268 void HostContentSettingsMap::AddExceptionForURL( |
| 520 const GURL& url, | 269 const GURL& url, |
| 521 ContentSettingsType content_type, | 270 ContentSettingsType content_type, |
| 522 const std::string& resource_identifier, | 271 const std::string& resource_identifier, |
| 523 ContentSetting setting) { | 272 ContentSetting setting) { |
| 524 // Make sure there is no entry that would override the pattern we are about | 273 // Make sure there is no entry that would override the pattern we are about |
| 525 // to insert for exactly this URL. | 274 // to insert for exactly this URL. |
| 526 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(url), | 275 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(url), |
| 527 content_type, | 276 content_type, |
| 528 resource_identifier, | 277 resource_identifier, |
| 529 CONTENT_SETTING_DEFAULT); | 278 CONTENT_SETTING_DEFAULT); |
| 530 SetContentSetting(ContentSettingsPattern::FromURL(url), | 279 SetContentSetting(ContentSettingsPattern::FromURL(url), |
| 531 content_type, | 280 content_type, |
| 532 resource_identifier, | 281 resource_identifier, |
| 533 setting); | 282 setting); |
| 534 } | 283 } |
| 535 | 284 |
| 536 void HostContentSettingsMap::ClearSettingsForOneType( | 285 void HostContentSettingsMap::ClearSettingsForOneType( |
| 537 ContentSettingsType content_type) { | 286 ContentSettingsType content_type) { |
| 538 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 287 for (provider_iterator provider = content_settings_providers_.begin(); |
| 539 | 288 provider != content_settings_providers_.end(); |
| 540 PrefService* prefs = NULL; | 289 ++provider) { |
| 541 DictionaryValue* all_settings_dictionary = NULL; | 290 (*provider)->ClearAllContentSettingsRules(content_type); |
| 542 HostContentSettings* map_to_modify = &off_the_record_settings_; | |
| 543 | |
| 544 if (!is_off_the_record_) { | |
| 545 prefs = profile_->GetPrefs(); | |
| 546 all_settings_dictionary = | |
| 547 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | |
| 548 map_to_modify = &host_content_settings_; | |
| 549 } | |
| 550 | |
| 551 { | |
| 552 base::AutoLock auto_lock(lock_); | |
| 553 for (HostContentSettings::iterator i(map_to_modify->begin()); | |
| 554 i != map_to_modify->end(); ) { | |
| 555 if (RequiresResourceIdentifier(content_type) || | |
| 556 i->second.content_settings.settings[content_type] != | |
| 557 CONTENT_SETTING_DEFAULT) { | |
| 558 if (RequiresResourceIdentifier(content_type)) | |
| 559 i->second.content_settings_for_resources.clear(); | |
| 560 i->second.content_settings.settings[content_type] = | |
| 561 CONTENT_SETTING_DEFAULT; | |
| 562 std::string host(i->first); | |
| 563 if (AllDefault(i->second)) { | |
| 564 if (all_settings_dictionary) | |
| 565 all_settings_dictionary-> | |
| 566 RemoveWithoutPathExpansion(host, NULL); | |
| 567 map_to_modify->erase(i++); | |
| 568 } else if (all_settings_dictionary) { | |
| 569 DictionaryValue* host_settings_dictionary; | |
| 570 bool found = | |
| 571 all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 572 host, &host_settings_dictionary); | |
| 573 DCHECK(found); | |
| 574 host_settings_dictionary->RemoveWithoutPathExpansion( | |
| 575 kTypeNames[content_type], NULL); | |
| 576 ++i; | |
| 577 } | |
| 578 } else { | |
| 579 ++i; | |
| 580 } | |
| 581 } | |
| 582 } | |
| 583 | |
| 584 updating_preferences_ = true; | |
| 585 if (!is_off_the_record_) | |
| 586 ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | |
| 587 updating_preferences_ = false; | |
| 588 | |
| 589 NotifyObservers( | |
| 590 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); | |
| 591 } | |
| 592 | |
| 593 bool HostContentSettingsMap::RequiresResourceIdentifier( | |
| 594 ContentSettingsType content_type) const { | |
| 595 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 596 switches::kEnableResourceContentSettings)) { | |
| 597 return kRequiresResourceIdentifier[content_type]; | |
| 598 } else { | |
| 599 return false; | |
| 600 } | 291 } |
| 601 } | 292 } |
| 602 | 293 |
| 603 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { | 294 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { |
| 604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 605 | 296 |
| 606 // This setting may not be directly modified for OTR sessions. Instead, it | 297 // This setting may not be directly modified for OTR sessions. Instead, it |
| 607 // is synced to the main profile's setting. | 298 // is synced to the main profile's setting. |
| 608 if (is_off_the_record_) { | 299 if (is_off_the_record_) { |
| 609 NOTREACHED(); | 300 NOTREACHED(); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 637 if (is_off_the_record_) { | 328 if (is_off_the_record_) { |
| 638 NOTREACHED(); | 329 NOTREACHED(); |
| 639 return; | 330 return; |
| 640 } | 331 } |
| 641 | 332 |
| 642 { | 333 { |
| 643 base::AutoLock auto_lock(lock_); | 334 base::AutoLock auto_lock(lock_); |
| 644 block_nonsandboxed_plugins_ = block; | 335 block_nonsandboxed_plugins_ = block; |
| 645 } | 336 } |
| 646 | 337 |
| 647 | |
| 648 PrefService* prefs = profile_->GetPrefs(); | 338 PrefService* prefs = profile_->GetPrefs(); |
| 649 if (block) { | 339 if (block) { |
| 650 UserMetrics::RecordAction( | 340 UserMetrics::RecordAction( |
| 651 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); | 341 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); |
| 652 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); | 342 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); |
| 653 } else { | 343 } else { |
| 654 UserMetrics::RecordAction( | 344 UserMetrics::RecordAction( |
| 655 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); | 345 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); |
| 656 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 346 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 657 } | 347 } |
| 658 } | 348 } |
| 659 | 349 |
| 660 void HostContentSettingsMap::ResetToDefaults() { | 350 void HostContentSettingsMap::ResetToDefaults() { |
| 661 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 351 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 662 | 352 |
| 663 { | 353 { |
| 664 base::AutoLock auto_lock(lock_); | 354 base::AutoLock auto_lock(lock_); |
| 665 for (provider_iterator provider = | 355 for (default_provider_iterator provider = |
| 666 default_content_settings_providers_.begin(); | 356 default_content_settings_providers_.begin(); |
| 667 provider != default_content_settings_providers_.end(); ++provider) { | 357 provider != default_content_settings_providers_.end(); ++provider) { |
| 668 (*provider)->ResetToDefaults(); | 358 (*provider)->ResetToDefaults(); |
| 669 } | 359 } |
| 670 host_content_settings_.clear(); | 360 |
| 671 off_the_record_settings_.clear(); | 361 for (provider_iterator provider = content_settings_providers_.begin(); |
| 362 provider != content_settings_providers_.end(); | |
| 363 ++provider) { | |
| 364 (*provider)->ResetToDefaults(); | |
| 365 } | |
| 366 | |
| 672 // Don't reset block third party cookies if they are managed. | 367 // Don't reset block third party cookies if they are managed. |
| 673 if (!IsBlockThirdPartyCookiesManaged()) | 368 if (!IsBlockThirdPartyCookiesManaged()) |
| 674 block_third_party_cookies_ = false; | 369 block_third_party_cookies_ = false; |
| 675 block_nonsandboxed_plugins_ = false; | 370 block_nonsandboxed_plugins_ = false; |
| 676 } | 371 } |
| 677 | 372 |
| 678 if (!is_off_the_record_) { | 373 if (!is_off_the_record_) { |
| 679 PrefService* prefs = profile_->GetPrefs(); | 374 PrefService* prefs = profile_->GetPrefs(); |
| 680 updating_preferences_ = true; | 375 updating_preferences_ = true; |
| 681 prefs->ClearPref(prefs::kContentSettingsPatterns); | |
| 682 // If the block third party cookies preference is managed we still must | 376 // If the block third party cookies preference is managed we still must |
| 683 // clear it in order to restore the default value for later when the | 377 // clear it in order to restore the default value for later when the |
| 684 // preference is not managed anymore. | 378 // preference is not managed anymore. |
| 685 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 379 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 686 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 380 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 687 updating_preferences_ = false; | 381 updating_preferences_ = false; |
| 688 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 382 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 689 CONTENT_SETTINGS_TYPE_DEFAULT, | 383 CONTENT_SETTINGS_TYPE_DEFAULT, |
|
jochen (gone - plz use gerrit)
2011/02/04 08:38:43
indenting?
markusheintz_
2011/02/04 10:08:03
Done.
| |
| 690 "")); | 384 "")); |
| 691 } | 385 } |
| 692 } | 386 } |
| 693 | 387 |
| 694 void HostContentSettingsMap::Observe(NotificationType type, | 388 void HostContentSettingsMap::Observe(NotificationType type, |
| 695 const NotificationSource& source, | 389 const NotificationSource& source, |
| 696 const NotificationDetails& details) { | 390 const NotificationDetails& details) { |
| 697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 391 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 698 | 392 |
| 699 if (type == NotificationType::PREF_CHANGED) { | 393 if (type == NotificationType::PREF_CHANGED) { |
| 700 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 394 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 701 if (updating_preferences_) | 395 if (updating_preferences_) |
| 702 return; | 396 return; |
| 703 | 397 |
| 704 std::string* name = Details<std::string>(details).ptr(); | 398 std::string* name = Details<std::string>(details).ptr(); |
| 705 if (*name == prefs::kContentSettingsPatterns) { | 399 if (*name == prefs::kBlockThirdPartyCookies) { |
| 706 ReadExceptions(true); | |
| 707 } else if (*name == prefs::kBlockThirdPartyCookies) { | |
| 708 base::AutoLock auto_lock(lock_); | 400 base::AutoLock auto_lock(lock_); |
| 709 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 401 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( |
| 710 prefs::kBlockThirdPartyCookies); | 402 prefs::kBlockThirdPartyCookies); |
| 711 is_block_third_party_cookies_managed_ = | 403 is_block_third_party_cookies_managed_ = |
| 712 profile_->GetPrefs()->IsManagedPreference( | 404 profile_->GetPrefs()->IsManagedPreference( |
| 713 prefs::kBlockThirdPartyCookies); | 405 prefs::kBlockThirdPartyCookies); |
| 714 } else if (*name == prefs::kBlockNonsandboxedPlugins) { | 406 } else if (*name == prefs::kBlockNonsandboxedPlugins) { |
| 715 base::AutoLock auto_lock(lock_); | 407 base::AutoLock auto_lock(lock_); |
| 716 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( | 408 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( |
| 717 prefs::kBlockNonsandboxedPlugins); | 409 prefs::kBlockNonsandboxedPlugins); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 730 UnregisterObservers(); | 422 UnregisterObservers(); |
| 731 } else { | 423 } else { |
| 732 NOTREACHED() << "Unexpected notification"; | 424 NOTREACHED() << "Unexpected notification"; |
| 733 } | 425 } |
| 734 } | 426 } |
| 735 | 427 |
| 736 HostContentSettingsMap::~HostContentSettingsMap() { | 428 HostContentSettingsMap::~HostContentSettingsMap() { |
| 737 UnregisterObservers(); | 429 UnregisterObservers(); |
| 738 } | 430 } |
| 739 | 431 |
| 740 void HostContentSettingsMap::GetSettingsFromDictionary( | |
| 741 const DictionaryValue* dictionary, | |
| 742 ContentSettings* settings) { | |
| 743 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | |
| 744 i != dictionary->end_keys(); ++i) { | |
| 745 const std::string& content_type(*i); | |
| 746 for (size_t type = 0; type < arraysize(kTypeNames); ++type) { | |
| 747 if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type)) { | |
| 748 int setting = CONTENT_SETTING_DEFAULT; | |
| 749 bool found = dictionary->GetIntegerWithoutPathExpansion(content_type, | |
| 750 &setting); | |
| 751 DCHECK(found); | |
| 752 settings->settings[type] = IntToContentSetting(setting); | |
| 753 break; | |
| 754 } | |
| 755 } | |
| 756 } | |
| 757 // Migrate obsolete cookie prompt mode. | |
| 758 if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] == | |
| 759 CONTENT_SETTING_ASK) | |
| 760 settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK; | |
| 761 | |
| 762 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] = | |
| 763 ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS, | |
| 764 settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]); | |
| 765 } | |
| 766 | |
| 767 void HostContentSettingsMap::GetResourceSettingsFromDictionary( | |
| 768 const DictionaryValue* dictionary, | |
| 769 ResourceContentSettings* settings) { | |
| 770 for (DictionaryValue::key_iterator i(dictionary->begin_keys()); | |
| 771 i != dictionary->end_keys(); ++i) { | |
| 772 const std::string& content_type(*i); | |
| 773 for (size_t type = 0; type < arraysize(kResourceTypeNames); ++type) { | |
| 774 if ((kResourceTypeNames[type] != NULL) && | |
| 775 (kResourceTypeNames[type] == content_type)) { | |
| 776 DictionaryValue* resource_dictionary = NULL; | |
| 777 bool found = dictionary->GetDictionary(content_type, | |
| 778 &resource_dictionary); | |
| 779 DCHECK(found); | |
| 780 for (DictionaryValue::key_iterator j(resource_dictionary->begin_keys()); | |
| 781 j != resource_dictionary->end_keys(); ++j) { | |
| 782 const std::string& resource_identifier(*j); | |
| 783 int setting = CONTENT_SETTING_DEFAULT; | |
| 784 bool found = resource_dictionary->GetIntegerWithoutPathExpansion( | |
| 785 resource_identifier, &setting); | |
| 786 DCHECK(found); | |
| 787 (*settings)[ContentSettingsTypeResourceIdentifierPair( | |
| 788 ContentSettingsType(type), resource_identifier)] = | |
| 789 ClickToPlayFixup(ContentSettingsType(type), | |
| 790 ContentSetting(setting)); | |
| 791 } | |
| 792 | |
| 793 break; | |
| 794 } | |
| 795 } | |
| 796 } | |
| 797 } | |
| 798 | |
| 799 bool HostContentSettingsMap::AllDefault( | |
| 800 const ExtendedContentSettings& settings) const { | |
| 801 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { | |
| 802 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) | |
| 803 return false; | |
| 804 } | |
| 805 return settings.content_settings_for_resources.empty(); | |
| 806 } | |
| 807 | |
| 808 bool HostContentSettingsMap::IsDefaultContentSettingManaged( | 432 bool HostContentSettingsMap::IsDefaultContentSettingManaged( |
| 809 ContentSettingsType content_type) const { | 433 ContentSettingsType content_type) const { |
| 810 for (const_provider_iterator provider = | 434 for (const_default_provider_iterator provider = |
| 811 default_content_settings_providers_.begin(); | 435 default_content_settings_providers_.begin(); |
| 812 provider != default_content_settings_providers_.end(); ++provider) { | 436 provider != default_content_settings_providers_.end(); ++provider) { |
| 813 if ((*provider)->DefaultSettingIsManaged(content_type)) | 437 if ((*provider)->DefaultSettingIsManaged(content_type)) |
| 814 return true; | 438 return true; |
| 815 } | 439 } |
| 816 return false; | 440 return false; |
| 817 } | 441 } |
| 818 | 442 |
| 819 void HostContentSettingsMap::ReadExceptions(bool overwrite) { | |
| 820 base::AutoLock lock(lock_); | |
| 821 | |
| 822 PrefService* prefs = profile_->GetPrefs(); | |
| 823 DictionaryValue* all_settings_dictionary = | |
| 824 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | |
| 825 | |
| 826 if (overwrite) | |
| 827 host_content_settings_.clear(); | |
| 828 | |
| 829 // Careful: The returned value could be NULL if the pref has never been set. | |
| 830 if (all_settings_dictionary != NULL) { | |
| 831 // Convert all Unicode patterns into punycode form, then read. | |
| 832 CanonicalizeContentSettingsExceptions(all_settings_dictionary); | |
| 833 | |
| 834 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | |
| 835 i != all_settings_dictionary->end_keys(); ++i) { | |
| 836 const std::string& pattern(*i); | |
| 837 if (!ContentSettingsPattern(pattern).IsValid()) | |
| 838 LOG(WARNING) << "Invalid pattern stored in content settings"; | |
| 839 DictionaryValue* pattern_settings_dictionary = NULL; | |
| 840 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 841 pattern, &pattern_settings_dictionary); | |
| 842 DCHECK(found); | |
| 843 ContentSettings settings; | |
| 844 GetSettingsFromDictionary(pattern_settings_dictionary, &settings); | |
| 845 host_content_settings_[pattern].content_settings = settings; | |
| 846 GetResourceSettingsFromDictionary( | |
| 847 pattern_settings_dictionary, | |
| 848 &host_content_settings_[pattern].content_settings_for_resources); | |
| 849 } | |
| 850 } | |
| 851 } | |
| 852 | |
| 853 void HostContentSettingsMap::NotifyObservers( | 443 void HostContentSettingsMap::NotifyObservers( |
| 854 const ContentSettingsDetails& details) { | 444 const ContentSettingsDetails& details) { |
| 855 NotificationService::current()->Notify( | 445 NotificationService::current()->Notify( |
| 856 NotificationType::CONTENT_SETTINGS_CHANGED, | 446 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 857 Source<HostContentSettingsMap>(this), | 447 Source<HostContentSettingsMap>(this), |
| 858 Details<const ContentSettingsDetails>(&details)); | 448 Details<const ContentSettingsDetails>(&details)); |
| 859 } | 449 } |
| 860 | 450 |
| 861 void HostContentSettingsMap::UnregisterObservers() { | 451 void HostContentSettingsMap::UnregisterObservers() { |
| 862 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 876 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 466 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 877 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? | 467 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? |
| 878 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); | 468 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); |
| 879 } | 469 } |
| 880 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { | 470 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { |
| 881 SetBlockThirdPartyCookies(cookie_behavior == | 471 SetBlockThirdPartyCookies(cookie_behavior == |
| 882 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | 472 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); |
| 883 } | 473 } |
| 884 } | 474 } |
| 885 } | 475 } |
| 886 | |
| 887 void HostContentSettingsMap::MigrateObsoletePopupsPref(PrefService* prefs) { | |
| 888 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { | |
| 889 const ListValue* whitelist_pref = | |
| 890 prefs->GetList(prefs::kPopupWhitelistedHosts); | |
| 891 for (ListValue::const_iterator i(whitelist_pref->begin()); | |
| 892 i != whitelist_pref->end(); ++i) { | |
| 893 std::string host; | |
| 894 (*i)->GetAsString(&host); | |
| 895 SetContentSetting(ContentSettingsPattern(host), | |
| 896 CONTENT_SETTINGS_TYPE_POPUPS, | |
| 897 "", | |
| 898 CONTENT_SETTING_ALLOW); | |
| 899 } | |
| 900 prefs->ClearPref(prefs::kPopupWhitelistedHosts); | |
| 901 } | |
| 902 } | |
| 903 | |
| 904 void HostContentSettingsMap::MigrateObsoletePerhostPref(PrefService* prefs) { | |
| 905 if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) { | |
| 906 const DictionaryValue* all_settings_dictionary = | |
| 907 prefs->GetDictionary(prefs::kPerHostContentSettings); | |
| 908 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | |
| 909 i != all_settings_dictionary->end_keys(); ++i) { | |
| 910 const std::string& host(*i); | |
| 911 ContentSettingsPattern pattern( | |
| 912 std::string(ContentSettingsPattern::kDomainWildcard) + host); | |
| 913 DictionaryValue* host_settings_dictionary = NULL; | |
| 914 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 915 host, &host_settings_dictionary); | |
| 916 DCHECK(found); | |
| 917 ContentSettings settings; | |
| 918 GetSettingsFromDictionary(host_settings_dictionary, &settings); | |
| 919 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | |
| 920 if (settings.settings[j] != CONTENT_SETTING_DEFAULT && | |
| 921 !RequiresResourceIdentifier(ContentSettingsType(j))) | |
| 922 SetContentSetting( | |
| 923 pattern, ContentSettingsType(j), "", settings.settings[j]); | |
| 924 } | |
| 925 } | |
| 926 prefs->ClearPref(prefs::kPerHostContentSettings); | |
| 927 } | |
| 928 } | |
| 929 | |
| 930 void HostContentSettingsMap::CanonicalizeContentSettingsExceptions( | |
| 931 DictionaryValue* all_settings_dictionary) { | |
| 932 DCHECK(all_settings_dictionary); | |
| 933 | |
| 934 std::vector<std::string> remove_items; | |
| 935 std::vector<std::pair<std::string, std::string> > move_items; | |
| 936 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | |
| 937 i != all_settings_dictionary->end_keys(); ++i) { | |
| 938 const std::string& pattern(*i); | |
| 939 const std::string canonicalized_pattern = | |
| 940 ContentSettingsPattern(pattern).CanonicalizePattern(); | |
| 941 | |
| 942 if (canonicalized_pattern.empty() || canonicalized_pattern == pattern) | |
| 943 continue; | |
| 944 | |
| 945 // Clear old pattern if prefs already have canonicalized pattern. | |
| 946 DictionaryValue* new_pattern_settings_dictionary = NULL; | |
| 947 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 948 canonicalized_pattern, &new_pattern_settings_dictionary)) { | |
| 949 remove_items.push_back(pattern); | |
| 950 continue; | |
| 951 } | |
| 952 | |
| 953 // Move old pattern to canonicalized pattern. | |
| 954 DictionaryValue* old_pattern_settings_dictionary = NULL; | |
| 955 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( | |
| 956 pattern, &old_pattern_settings_dictionary)) { | |
| 957 move_items.push_back(std::make_pair(pattern, canonicalized_pattern)); | |
| 958 } | |
| 959 } | |
| 960 | |
| 961 for (size_t i = 0; i < remove_items.size(); ++i) { | |
| 962 all_settings_dictionary->RemoveWithoutPathExpansion(remove_items[i], NULL); | |
| 963 } | |
| 964 | |
| 965 for (size_t i = 0; i < move_items.size(); ++i) { | |
| 966 Value* pattern_settings_dictionary = NULL; | |
| 967 all_settings_dictionary->RemoveWithoutPathExpansion( | |
| 968 move_items[i].first, &pattern_settings_dictionary); | |
| 969 all_settings_dictionary->SetWithoutPathExpansion( | |
| 970 move_items[i].second, pattern_settings_dictionary); | |
| 971 } | |
| 972 } | |
| OLD | NEW |