| 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 DefaultProviderIterator; |
| 95 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator | 47 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator |
| 96 const_provider_iterator; | 48 ConstDefaultProviderIterator; |
| 49 |
| 50 typedef linked_ptr<content_settings::ProviderInterface> ProviderPtr; |
| 51 typedef std::vector<ProviderPtr>::iterator ProviderIterator; |
| 52 typedef std::vector<ProviderPtr>::const_iterator ConstProviderIterator; |
| 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 default content settings providers are created is |
| 113 // as providers that are further down in the list (i.e. added later) override | 69 // critical, as providers that are further down in the list (i.e. added later) |
| 114 // providers further up. | 70 // override 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. |
| 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 provided by the PrefProvider. |
| 139 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 93 // The order in which the content settings providers are created is critical, |
| 140 prefs->SetInteger(prefs::kContentSettingsVersion, | 94 // as providers that are further up in the list (i.e. added earlier) override |
| 141 ContentSettingsPattern::kContentSettingsPatternVersion); | 95 // providers further down. |
| 142 } | 96 content_settings_providers_.push_back(ProviderPtr( |
| 143 if (prefs->GetInteger(prefs::kContentSettingsVersion) > | 97 new content_settings::PrefProvider(profile))); |
| 144 ContentSettingsPattern::kContentSettingsPatternVersion) { | |
| 145 LOG(ERROR) << "Unknown content settings version in preferences."; | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 // Read exceptions. | |
| 150 ReadExceptions(false); | |
| 151 | 98 |
| 152 pref_change_registrar_.Init(prefs); | 99 pref_change_registrar_.Init(prefs); |
| 153 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | |
| 154 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); | 100 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); |
| 155 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); | 101 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); |
| 156 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 102 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 157 Source<Profile>(profile_)); | 103 Source<Profile>(profile_)); |
| 158 } | 104 } |
| 159 | 105 |
| 160 // static | 106 // static |
| 161 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 107 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 162 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, | |
| 163 ContentSettingsPattern::kContentSettingsPatternVersion); | |
| 164 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); | |
| 165 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); | 108 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); |
| 166 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); | 109 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); |
| 167 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); | 110 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); |
| 168 | 111 |
| 169 // Obsolete prefs, for migration: | 112 // Obsolete prefs, for migration: |
| 170 prefs->RegisterIntegerPref(prefs::kCookieBehavior, | 113 prefs->RegisterIntegerPref(prefs::kCookieBehavior, |
| 171 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); | 114 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); |
| 172 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); | |
| 173 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); | |
| 174 | 115 |
| 175 // Register the prefs for the content settings providers. | 116 // Register the prefs for the content settings providers. |
| 176 content_settings::PrefDefaultProvider::RegisterUserPrefs(prefs); | 117 content_settings::PrefDefaultProvider::RegisterUserPrefs(prefs); |
| 177 content_settings::PolicyDefaultProvider::RegisterUserPrefs(prefs); | 118 content_settings::PolicyDefaultProvider::RegisterUserPrefs(prefs); |
| 119 content_settings::PrefProvider::RegisterUserPrefs(prefs); |
| 178 } | 120 } |
| 179 | 121 |
| 180 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( | 122 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( |
| 181 ContentSettingsType content_type) const { | 123 ContentSettingsType content_type) const { |
| 182 ContentSetting setting = CONTENT_SETTING_DEFAULT; | 124 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 183 for (const_provider_iterator provider = | 125 for (ConstDefaultProviderIterator provider = |
| 184 default_content_settings_providers_.begin(); | 126 default_content_settings_providers_.begin(); |
| 185 provider != default_content_settings_providers_.end(); ++provider) { | 127 provider != default_content_settings_providers_.end(); ++provider) { |
| 186 ContentSetting provided_setting = | 128 ContentSetting provided_setting = |
| 187 (*provider)->ProvideDefaultSetting(content_type); | 129 (*provider)->ProvideDefaultSetting(content_type); |
| 188 if (provided_setting != CONTENT_SETTING_DEFAULT) | 130 if (provided_setting != CONTENT_SETTING_DEFAULT) |
| 189 setting = provided_setting; | 131 setting = provided_setting; |
| 190 } | 132 } |
| 191 // The method GetDefaultContentSetting always has to return an explicit | 133 // The method GetDefaultContentSetting always has to return an explicit |
| 192 // value that is to be used as default. We here rely on the | 134 // value that is to be used as default. We here rely on the |
| 193 // PrefContentSettingProvider to always provide a value. | 135 // PrefContentSettingProvider to always provide a value. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 209 return setting; | 151 return setting; |
| 210 } | 152 } |
| 211 | 153 |
| 212 ContentSetting HostContentSettingsMap::GetNonDefaultContentSetting( | 154 ContentSetting HostContentSettingsMap::GetNonDefaultContentSetting( |
| 213 const GURL& url, | 155 const GURL& url, |
| 214 ContentSettingsType content_type, | 156 ContentSettingsType content_type, |
| 215 const std::string& resource_identifier) const { | 157 const std::string& resource_identifier) const { |
| 216 if (ShouldAllowAllContent(url)) | 158 if (ShouldAllowAllContent(url)) |
| 217 return CONTENT_SETTING_ALLOW; | 159 return CONTENT_SETTING_ALLOW; |
| 218 | 160 |
| 219 if (!RequiresResourceIdentifier(content_type)) | 161 // Iterate through the list of providers and break when the first non default |
| 220 return GetNonDefaultContentSettings(url).settings[content_type]; | 162 // setting is found. |
| 221 | 163 ContentSetting provided_setting(CONTENT_SETTING_DEFAULT); |
| 222 if (CommandLine::ForCurrentProcess()->HasSwitch( | 164 for (ConstProviderIterator provider = content_settings_providers_.begin(); |
| 223 switches::kEnableResourceContentSettings)) { | 165 provider != content_settings_providers_.end(); |
| 224 DCHECK(!resource_identifier.empty()); | 166 ++provider) { |
| 167 provided_setting = (*provider)->GetContentSetting( |
| 168 url, url, content_type, resource_identifier); |
| 169 if (provided_setting != CONTENT_SETTING_DEFAULT) |
| 170 break; |
| 225 } | 171 } |
| 226 | 172 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 } | 173 } |
| 288 | 174 |
| 289 ContentSettings HostContentSettingsMap::GetContentSettings( | 175 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 290 const GURL& url) const { | 176 const GURL& url) const { |
| 291 ContentSettings output = GetNonDefaultContentSettings(url); | 177 ContentSettings output = GetNonDefaultContentSettings(url); |
| 292 | 178 |
| 293 // If we require a resource identifier, set the content settings to default, | 179 // If we require a resource identifier, set the content settings to default, |
| 294 // otherwise make the defaults explicit. | 180 // otherwise make the defaults explicit. |
| 295 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 181 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 296 if (RequiresResourceIdentifier(ContentSettingsType(j))) { | 182 // A managed default content setting has the highest priority and hence |
| 297 output.settings[j] = CONTENT_SETTING_DEFAULT; | 183 // will overwrite any previously set value. |
| 298 } else { | 184 if ((output.settings[j] == CONTENT_SETTING_DEFAULT && |
| 299 // A managed default content setting has the highest priority and hence | 185 j != CONTENT_SETTINGS_TYPE_PLUGINS) || |
| 300 // will overwrite any previously set value. | 186 IsDefaultContentSettingManaged(ContentSettingsType(j))) { |
| 301 if ((output.settings[j] == CONTENT_SETTING_DEFAULT && | 187 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j)); |
| 302 j != CONTENT_SETTINGS_TYPE_PLUGINS) || | |
| 303 IsDefaultContentSettingManaged(ContentSettingsType(j))) { | |
| 304 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j)); | |
| 305 } | |
| 306 } | 188 } |
| 307 } | 189 } |
| 308 return output; | 190 return output; |
| 309 } | 191 } |
| 310 | 192 |
| 311 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( | 193 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( |
| 312 const GURL& url) const { | 194 const GURL& url) const { |
| 313 if (ShouldAllowAllContent(url)) | 195 if (ShouldAllowAllContent(url)) |
| 314 return ContentSettings(CONTENT_SETTING_ALLOW); | 196 return ContentSettings(CONTENT_SETTING_ALLOW); |
| 315 | 197 |
| 316 base::AutoLock auto_lock(lock_); | 198 ContentSettings output(CONTENT_SETTING_DEFAULT); |
| 317 | 199 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 318 const std::string host(net::GetHostOrSpecFromURL(url)); | 200 output.settings[j] = GetNonDefaultContentSetting( |
| 319 ContentSettings output; | 201 url, ContentSettingsType(j) , ""); |
| 320 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | |
| 321 output.settings[j] = CONTENT_SETTING_DEFAULT; | |
| 322 | |
| 323 // Check for exact matches first. | |
| 324 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | |
| 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 } | 202 } |
| 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; | 203 return output; |
| 364 } | 204 } |
| 365 | 205 |
| 366 void HostContentSettingsMap::GetSettingsForOneType( | 206 void HostContentSettingsMap::GetSettingsForOneType( |
| 367 ContentSettingsType content_type, | 207 ContentSettingsType content_type, |
| 368 const std::string& resource_identifier, | 208 const std::string& resource_identifier, |
| 369 SettingsForOneType* settings) const { | 209 SettingsForOneType* settings) const { |
| 370 DCHECK(RequiresResourceIdentifier(content_type) != | |
| 371 resource_identifier.empty()); | |
| 372 DCHECK(settings); | 210 DCHECK(settings); |
| 373 settings->clear(); | 211 settings->clear(); |
| 374 | 212 |
| 375 const HostContentSettings* map_to_return = | 213 // Collect content_settings::Rules for the given content_type and |
| 376 is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; | 214 // resource_identifier from the content settings providers. |
| 377 ContentSettingsTypeResourceIdentifierPair | 215 Rules content_settings_rules; |
| 378 requested_setting(content_type, resource_identifier); | 216 for (ConstProviderIterator provider = content_settings_providers_.begin(); |
| 217 provider != content_settings_providers_.end(); |
| 218 ++provider) { |
| 219 // TODO(markusheintz): Only the rules that are applied should be collected. |
| 220 // Merge rules. |
| 221 (*provider)->GetAllContentSettingsRules( |
| 222 content_type, resource_identifier, &content_settings_rules); |
| 223 } |
| 379 | 224 |
| 380 base::AutoLock auto_lock(lock_); | 225 // convert Rules to SettingsForOneType |
| 381 for (HostContentSettings::const_iterator i(map_to_return->begin()); | 226 for (const_rules_iterator rule_iterator = |
| 382 i != map_to_return->end(); ++i) { | 227 content_settings_rules.begin(); |
| 383 ContentSetting setting; | 228 rule_iterator != content_settings_rules.end(); |
| 384 if (RequiresResourceIdentifier(content_type)) { | 229 ++rule_iterator) { |
| 385 if (i->second.content_settings_for_resources.find(requested_setting) != | 230 settings->push_back(std::make_pair(ContentSettingsPattern( |
| 386 i->second.content_settings_for_resources.end()) | 231 rule_iterator->requesting_url_pattern), |
| 387 setting = i->second.content_settings_for_resources.find( | 232 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 } | 233 } |
| 401 } | 234 } |
| 402 | 235 |
| 403 void HostContentSettingsMap::SetDefaultContentSetting( | 236 void HostContentSettingsMap::SetDefaultContentSetting( |
| 404 ContentSettingsType content_type, | 237 ContentSettingsType content_type, |
| 405 ContentSetting setting) { | 238 ContentSetting setting) { |
| 406 for (provider_iterator provider = | 239 for (DefaultProviderIterator provider = |
| 407 default_content_settings_providers_.begin(); | 240 default_content_settings_providers_.begin(); |
| 408 provider != default_content_settings_providers_.end(); ++provider) { | 241 provider != default_content_settings_providers_.end(); ++provider) { |
| 409 (*provider)->UpdateDefaultSetting(content_type, setting); | 242 (*provider)->UpdateDefaultSetting(content_type, setting); |
| 410 } | 243 } |
| 411 } | 244 } |
| 412 | 245 |
| 413 void HostContentSettingsMap::SetContentSetting( | 246 void HostContentSettingsMap::SetContentSetting( |
| 414 const ContentSettingsPattern& original_pattern, | 247 const ContentSettingsPattern& pattern, |
| 415 ContentSettingsType content_type, | 248 ContentSettingsType content_type, |
| 416 const std::string& resource_identifier, | 249 const std::string& resource_identifier, |
| 417 ContentSetting setting) { | 250 ContentSetting setting) { |
| 418 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 251 for (ProviderIterator provider = content_settings_providers_.begin(); |
| 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 provider != content_settings_providers_.end(); |
| 420 DCHECK_NE(RequiresResourceIdentifier(content_type), | 253 ++provider) { |
| 421 resource_identifier.empty()); | 254 (*provider)->SetContentSetting( |
| 422 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 255 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 } | 256 } |
| 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 } | 257 } |
| 518 | 258 |
| 519 void HostContentSettingsMap::AddExceptionForURL( | 259 void HostContentSettingsMap::AddExceptionForURL( |
| 520 const GURL& url, | 260 const GURL& url, |
| 521 ContentSettingsType content_type, | 261 ContentSettingsType content_type, |
| 522 const std::string& resource_identifier, | 262 const std::string& resource_identifier, |
| 523 ContentSetting setting) { | 263 ContentSetting setting) { |
| 524 // Make sure there is no entry that would override the pattern we are about | 264 // Make sure there is no entry that would override the pattern we are about |
| 525 // to insert for exactly this URL. | 265 // to insert for exactly this URL. |
| 526 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(url), | 266 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(url), |
| 527 content_type, | 267 content_type, |
| 528 resource_identifier, | 268 resource_identifier, |
| 529 CONTENT_SETTING_DEFAULT); | 269 CONTENT_SETTING_DEFAULT); |
| 530 SetContentSetting(ContentSettingsPattern::FromURL(url), | 270 SetContentSetting(ContentSettingsPattern::FromURL(url), |
| 531 content_type, | 271 content_type, |
| 532 resource_identifier, | 272 resource_identifier, |
| 533 setting); | 273 setting); |
| 534 } | 274 } |
| 535 | 275 |
| 536 void HostContentSettingsMap::ClearSettingsForOneType( | 276 void HostContentSettingsMap::ClearSettingsForOneType( |
| 537 ContentSettingsType content_type) { | 277 ContentSettingsType content_type) { |
| 538 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 278 for (ProviderIterator provider = content_settings_providers_.begin(); |
| 539 | 279 provider != content_settings_providers_.end(); |
| 540 PrefService* prefs = NULL; | 280 ++provider) { |
| 541 DictionaryValue* all_settings_dictionary = NULL; | 281 (*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 } | 282 } |
| 601 } | 283 } |
| 602 | 284 |
| 603 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { | 285 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { |
| 604 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 605 | 287 |
| 606 // This setting may not be directly modified for OTR sessions. Instead, it | 288 // This setting may not be directly modified for OTR sessions. Instead, it |
| 607 // is synced to the main profile's setting. | 289 // is synced to the main profile's setting. |
| 608 if (is_off_the_record_) { | 290 if (is_off_the_record_) { |
| 609 NOTREACHED(); | 291 NOTREACHED(); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 637 if (is_off_the_record_) { | 319 if (is_off_the_record_) { |
| 638 NOTREACHED(); | 320 NOTREACHED(); |
| 639 return; | 321 return; |
| 640 } | 322 } |
| 641 | 323 |
| 642 { | 324 { |
| 643 base::AutoLock auto_lock(lock_); | 325 base::AutoLock auto_lock(lock_); |
| 644 block_nonsandboxed_plugins_ = block; | 326 block_nonsandboxed_plugins_ = block; |
| 645 } | 327 } |
| 646 | 328 |
| 647 | |
| 648 PrefService* prefs = profile_->GetPrefs(); | 329 PrefService* prefs = profile_->GetPrefs(); |
| 649 if (block) { | 330 if (block) { |
| 650 UserMetrics::RecordAction( | 331 UserMetrics::RecordAction( |
| 651 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); | 332 UserMetricsAction("BlockNonsandboxedPlugins_Enable")); |
| 652 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); | 333 prefs->SetBoolean(prefs::kBlockNonsandboxedPlugins, true); |
| 653 } else { | 334 } else { |
| 654 UserMetrics::RecordAction( | 335 UserMetrics::RecordAction( |
| 655 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); | 336 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); |
| 656 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 337 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 657 } | 338 } |
| 658 } | 339 } |
| 659 | 340 |
| 660 void HostContentSettingsMap::ResetToDefaults() { | 341 void HostContentSettingsMap::ResetToDefaults() { |
| 661 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 662 | 343 |
| 663 { | 344 { |
| 664 base::AutoLock auto_lock(lock_); | 345 base::AutoLock auto_lock(lock_); |
| 665 for (provider_iterator provider = | 346 for (DefaultProviderIterator provider = |
| 666 default_content_settings_providers_.begin(); | 347 default_content_settings_providers_.begin(); |
| 667 provider != default_content_settings_providers_.end(); ++provider) { | 348 provider != default_content_settings_providers_.end(); ++provider) { |
| 668 (*provider)->ResetToDefaults(); | 349 (*provider)->ResetToDefaults(); |
| 669 } | 350 } |
| 670 host_content_settings_.clear(); | 351 |
| 671 off_the_record_settings_.clear(); | 352 for (ProviderIterator provider = content_settings_providers_.begin(); |
| 353 provider != content_settings_providers_.end(); |
| 354 ++provider) { |
| 355 (*provider)->ResetToDefaults(); |
| 356 } |
| 357 |
| 672 // Don't reset block third party cookies if they are managed. | 358 // Don't reset block third party cookies if they are managed. |
| 673 if (!IsBlockThirdPartyCookiesManaged()) | 359 if (!IsBlockThirdPartyCookiesManaged()) |
| 674 block_third_party_cookies_ = false; | 360 block_third_party_cookies_ = false; |
| 675 block_nonsandboxed_plugins_ = false; | 361 block_nonsandboxed_plugins_ = false; |
| 676 } | 362 } |
| 677 | 363 |
| 678 if (!is_off_the_record_) { | 364 if (!is_off_the_record_) { |
| 679 PrefService* prefs = profile_->GetPrefs(); | 365 PrefService* prefs = profile_->GetPrefs(); |
| 680 updating_preferences_ = true; | 366 updating_preferences_ = true; |
| 681 prefs->ClearPref(prefs::kContentSettingsPatterns); | |
| 682 // If the block third party cookies preference is managed we still must | 367 // 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 | 368 // clear it in order to restore the default value for later when the |
| 684 // preference is not managed anymore. | 369 // preference is not managed anymore. |
| 685 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 370 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 686 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 371 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 687 updating_preferences_ = false; | 372 updating_preferences_ = false; |
| 688 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), | 373 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 689 CONTENT_SETTINGS_TYPE_DEFAULT, | 374 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 690 "")); | 375 "")); |
| 691 } | 376 } |
| 692 } | 377 } |
| 693 | 378 |
| 694 void HostContentSettingsMap::Observe(NotificationType type, | 379 void HostContentSettingsMap::Observe(NotificationType type, |
| 695 const NotificationSource& source, | 380 const NotificationSource& source, |
| 696 const NotificationDetails& details) { | 381 const NotificationDetails& details) { |
| 697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 698 | 383 |
| 699 if (type == NotificationType::PREF_CHANGED) { | 384 if (type == NotificationType::PREF_CHANGED) { |
| 700 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 385 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); |
| 701 if (updating_preferences_) | 386 if (updating_preferences_) |
| 702 return; | 387 return; |
| 703 | 388 |
| 704 std::string* name = Details<std::string>(details).ptr(); | 389 std::string* name = Details<std::string>(details).ptr(); |
| 705 if (*name == prefs::kContentSettingsPatterns) { | 390 if (*name == prefs::kBlockThirdPartyCookies) { |
| 706 ReadExceptions(true); | |
| 707 } else if (*name == prefs::kBlockThirdPartyCookies) { | |
| 708 base::AutoLock auto_lock(lock_); | 391 base::AutoLock auto_lock(lock_); |
| 709 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 392 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( |
| 710 prefs::kBlockThirdPartyCookies); | 393 prefs::kBlockThirdPartyCookies); |
| 711 is_block_third_party_cookies_managed_ = | 394 is_block_third_party_cookies_managed_ = |
| 712 profile_->GetPrefs()->IsManagedPreference( | 395 profile_->GetPrefs()->IsManagedPreference( |
| 713 prefs::kBlockThirdPartyCookies); | 396 prefs::kBlockThirdPartyCookies); |
| 714 } else if (*name == prefs::kBlockNonsandboxedPlugins) { | 397 } else if (*name == prefs::kBlockNonsandboxedPlugins) { |
| 715 base::AutoLock auto_lock(lock_); | 398 base::AutoLock auto_lock(lock_); |
| 716 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( | 399 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( |
| 717 prefs::kBlockNonsandboxedPlugins); | 400 prefs::kBlockNonsandboxedPlugins); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 730 UnregisterObservers(); | 413 UnregisterObservers(); |
| 731 } else { | 414 } else { |
| 732 NOTREACHED() << "Unexpected notification"; | 415 NOTREACHED() << "Unexpected notification"; |
| 733 } | 416 } |
| 734 } | 417 } |
| 735 | 418 |
| 736 HostContentSettingsMap::~HostContentSettingsMap() { | 419 HostContentSettingsMap::~HostContentSettingsMap() { |
| 737 UnregisterObservers(); | 420 UnregisterObservers(); |
| 738 } | 421 } |
| 739 | 422 |
| 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( | 423 bool HostContentSettingsMap::IsDefaultContentSettingManaged( |
| 809 ContentSettingsType content_type) const { | 424 ContentSettingsType content_type) const { |
| 810 for (const_provider_iterator provider = | 425 for (ConstDefaultProviderIterator provider = |
| 811 default_content_settings_providers_.begin(); | 426 default_content_settings_providers_.begin(); |
| 812 provider != default_content_settings_providers_.end(); ++provider) { | 427 provider != default_content_settings_providers_.end(); ++provider) { |
| 813 if ((*provider)->DefaultSettingIsManaged(content_type)) | 428 if ((*provider)->DefaultSettingIsManaged(content_type)) |
| 814 return true; | 429 return true; |
| 815 } | 430 } |
| 816 return false; | 431 return false; |
| 817 } | 432 } |
| 818 | 433 |
| 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( | 434 void HostContentSettingsMap::NotifyObservers( |
| 854 const ContentSettingsDetails& details) { | 435 const ContentSettingsDetails& details) { |
| 855 NotificationService::current()->Notify( | 436 NotificationService::current()->Notify( |
| 856 NotificationType::CONTENT_SETTINGS_CHANGED, | 437 NotificationType::CONTENT_SETTINGS_CHANGED, |
| 857 Source<HostContentSettingsMap>(this), | 438 Source<HostContentSettingsMap>(this), |
| 858 Details<const ContentSettingsDetails>(&details)); | 439 Details<const ContentSettingsDetails>(&details)); |
| 859 } | 440 } |
| 860 | 441 |
| 861 void HostContentSettingsMap::UnregisterObservers() { | 442 void HostContentSettingsMap::UnregisterObservers() { |
| 862 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 876 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 457 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 877 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? | 458 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? |
| 878 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); | 459 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); |
| 879 } | 460 } |
| 880 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { | 461 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { |
| 881 SetBlockThirdPartyCookies(cookie_behavior == | 462 SetBlockThirdPartyCookies(cookie_behavior == |
| 882 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | 463 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); |
| 883 } | 464 } |
| 884 } | 465 } |
| 885 } | 466 } |
| 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 |