| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| 11 #include "chrome/browser/content_settings/content_settings_details.h" |
| 11 #include "chrome/browser/metrics/user_metrics.h" | 12 #include "chrome/browser/metrics/user_metrics.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profile.h" | 14 #include "chrome/browser/profile.h" |
| 14 #include "chrome/browser/prefs/scoped_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_pref_update.h" |
| 15 #include "chrome/common/notification_service.h" | 16 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/notification_source.h" | 17 #include "chrome/common/notification_source.h" |
| 17 #include "chrome/common/notification_type.h" | 18 #include "chrome/common/notification_type.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 #include "googleurl/src/url_canon.h" | 23 #include "googleurl/src/url_canon.h" |
| 23 #include "googleurl/src/url_parse.h" | 24 #include "googleurl/src/url_parse.h" |
| 24 #include "net/base/dns_util.h" | 25 #include "net/base/dns_util.h" |
| 25 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
| 26 #include "net/base/static_cookie_policy.h" | 27 #include "net/base/static_cookie_policy.h" |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 // The version of the pattern format implemented. Version 1 includes the | |
| 30 // following patterns: | |
| 31 // - [*.]domain.tld (matches domain.tld and all sub-domains) | |
| 32 // - host (matches an exact hostname) | |
| 33 // - a.b.c.d (matches an exact IPv4 ip) | |
| 34 // - [a:b:c:d:e:f:g:h] (matches an exact IPv6 ip) | |
| 35 // - file:///tmp/test.html (a complete URL without a host) | |
| 36 // Version 2 adds a resource identifier for plugins. | |
| 37 // TODO(jochen): update once this feature is no longer behind a flag. | |
| 38 const int kContentSettingsPatternVersion = 1; | |
| 39 | |
| 40 // The format of a domain wildcard. | |
| 41 const char kDomainWildcard[] = "[*.]"; | |
| 42 | |
| 43 // The length of kDomainWildcard (without the trailing '\0') | |
| 44 const size_t kDomainWildcardLength = arraysize(kDomainWildcard) - 1; | |
| 45 | 30 |
| 46 // Base pref path of the prefs that contain the managed default content | 31 // Base pref path of the prefs that contain the managed default content |
| 47 // settings values. | 32 // settings values. |
| 48 const std::string kManagedSettings = | 33 const std::string kManagedSettings = |
| 49 "profile.managed_default_content_settings"; | 34 "profile.managed_default_content_settings"; |
| 50 | 35 |
| 51 // The preference keys where resource identifiers are stored for | 36 // The preference keys where resource identifiers are stored for |
| 52 // ContentSettingsType values that support resource identifiers. | 37 // ContentSettingsType values that support resource identifiers. |
| 53 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { | 38 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { |
| 54 NULL, | 39 NULL, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 116 } |
| 132 | 117 |
| 133 } // namespace | 118 } // namespace |
| 134 | 119 |
| 135 | 120 |
| 136 struct HostContentSettingsMap::ExtendedContentSettings { | 121 struct HostContentSettingsMap::ExtendedContentSettings { |
| 137 ContentSettings content_settings; | 122 ContentSettings content_settings; |
| 138 ResourceContentSettings content_settings_for_resources; | 123 ResourceContentSettings content_settings_for_resources; |
| 139 }; | 124 }; |
| 140 | 125 |
| 141 // static | |
| 142 HostContentSettingsMap::Pattern HostContentSettingsMap::Pattern::FromURL( | |
| 143 const GURL& url) { | |
| 144 return Pattern(!url.has_host() || url.HostIsIPAddress() ? | |
| 145 net::GetHostOrSpecFromURL(url) : | |
| 146 std::string(kDomainWildcard) + url.host()); | |
| 147 } | |
| 148 | |
| 149 // static | |
| 150 HostContentSettingsMap::Pattern | |
| 151 HostContentSettingsMap::Pattern::FromURLNoWildcard(const GURL& url) { | |
| 152 return Pattern(net::GetHostOrSpecFromURL(url)); | |
| 153 } | |
| 154 | |
| 155 bool HostContentSettingsMap::Pattern::IsValid() const { | |
| 156 if (pattern_.empty()) | |
| 157 return false; | |
| 158 | |
| 159 const std::string host(pattern_.length() > kDomainWildcardLength && | |
| 160 StartsWithASCII(pattern_, kDomainWildcard, false) ? | |
| 161 pattern_.substr(kDomainWildcardLength) : | |
| 162 pattern_); | |
| 163 url_canon::CanonHostInfo host_info; | |
| 164 return host.find('*') == std::string::npos && | |
| 165 !net::CanonicalizeHost(host, &host_info).empty(); | |
| 166 } | |
| 167 | |
| 168 bool HostContentSettingsMap::Pattern::Matches(const GURL& url) const { | |
| 169 if (!IsValid()) | |
| 170 return false; | |
| 171 | |
| 172 const std::string host(net::GetHostOrSpecFromURL(url)); | |
| 173 if (pattern_.length() < kDomainWildcardLength || | |
| 174 !StartsWithASCII(pattern_, kDomainWildcard, false)) | |
| 175 return pattern_ == host; | |
| 176 | |
| 177 const size_t match = | |
| 178 host.rfind(pattern_.substr(kDomainWildcardLength)); | |
| 179 | |
| 180 return (match != std::string::npos) && | |
| 181 (match == 0 || host[match - 1] == '.') && | |
| 182 (match + pattern_.length() - kDomainWildcardLength == host.length()); | |
| 183 } | |
| 184 | |
| 185 std::string HostContentSettingsMap::Pattern::CanonicalizePattern() const { | |
| 186 if (!IsValid()) { | |
| 187 return ""; | |
| 188 } | |
| 189 | |
| 190 bool starts_with_wildcard = pattern_.length() > kDomainWildcardLength && | |
| 191 StartsWithASCII(pattern_, kDomainWildcard, false); | |
| 192 | |
| 193 const std::string host(starts_with_wildcard ? | |
| 194 pattern_.substr(kDomainWildcardLength) : pattern_); | |
| 195 | |
| 196 std::string canonicalized_pattern = | |
| 197 starts_with_wildcard ? kDomainWildcard : ""; | |
| 198 | |
| 199 url_canon::CanonHostInfo host_info; | |
| 200 canonicalized_pattern += net::CanonicalizeHost(host, &host_info); | |
| 201 | |
| 202 return canonicalized_pattern; | |
| 203 } | |
| 204 | |
| 205 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) | 126 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) |
| 206 : profile_(profile), | 127 : profile_(profile), |
| 207 block_third_party_cookies_(false), | 128 block_third_party_cookies_(false), |
| 208 is_block_third_party_cookies_managed_(false), | 129 is_block_third_party_cookies_managed_(false), |
| 209 is_off_the_record_(profile_->IsOffTheRecord()), | 130 is_off_the_record_(profile_->IsOffTheRecord()), |
| 210 updating_preferences_(false) { | 131 updating_preferences_(false) { |
| 211 PrefService* prefs = profile_->GetPrefs(); | 132 PrefService* prefs = profile_->GetPrefs(); |
| 212 | 133 |
| 213 MigrateObsoleteCookiePref(prefs); | 134 MigrateObsoleteCookiePref(prefs); |
| 214 | 135 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 225 block_third_party_cookies_ = | 146 block_third_party_cookies_ = |
| 226 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); | 147 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); |
| 227 is_block_third_party_cookies_managed_ = | 148 is_block_third_party_cookies_managed_ = |
| 228 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); | 149 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); |
| 229 block_nonsandboxed_plugins_ = | 150 block_nonsandboxed_plugins_ = |
| 230 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); | 151 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); |
| 231 | 152 |
| 232 // Verify preferences version. | 153 // Verify preferences version. |
| 233 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { | 154 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { |
| 234 prefs->SetInteger(prefs::kContentSettingsVersion, | 155 prefs->SetInteger(prefs::kContentSettingsVersion, |
| 235 kContentSettingsPatternVersion); | 156 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 236 } | 157 } |
| 237 if (prefs->GetInteger(prefs::kContentSettingsVersion) > | 158 if (prefs->GetInteger(prefs::kContentSettingsVersion) > |
| 238 kContentSettingsPatternVersion) { | 159 ContentSettingsPattern::kContentSettingsPatternVersion) { |
| 239 LOG(ERROR) << "Unknown content settings version in preferences."; | 160 LOG(ERROR) << "Unknown content settings version in preferences."; |
| 240 return; | 161 return; |
| 241 } | 162 } |
| 242 | 163 |
| 243 // Read exceptions. | 164 // Read exceptions. |
| 244 ReadExceptions(false); | 165 ReadExceptions(false); |
| 245 | 166 |
| 246 pref_change_registrar_.Init(prefs); | 167 pref_change_registrar_.Init(prefs); |
| 247 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); | 168 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this); |
| 248 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); | 169 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 261 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); | 182 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this); |
| 262 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); | 183 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this); |
| 263 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 184 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 264 Source<Profile>(profile_)); | 185 Source<Profile>(profile_)); |
| 265 } | 186 } |
| 266 | 187 |
| 267 // static | 188 // static |
| 268 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 189 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 269 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); | 190 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings); |
| 270 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, | 191 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, |
| 271 kContentSettingsPatternVersion); | 192 ContentSettingsPattern::kContentSettingsPatternVersion); |
| 272 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); | 193 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); |
| 273 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); | 194 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); |
| 274 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); | 195 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); |
| 275 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); | 196 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); |
| 276 | 197 |
| 277 // Preferences for default content setting policies. A policy is not set of | 198 // Preferences for default content setting policies. A policy is not set of |
| 278 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. | 199 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT. |
| 279 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, | 200 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting, |
| 280 CONTENT_SETTING_DEFAULT); | 201 CONTENT_SETTING_DEFAULT); |
| 281 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, | 202 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 // regular ones. | 278 // regular ones. |
| 358 i = off_the_record_settings_.find(host); | 279 i = off_the_record_settings_.find(host); |
| 359 if (i != off_the_record_settings_.end() && | 280 if (i != off_the_record_settings_.end() && |
| 360 i->second.content_settings_for_resources.find(requested_setting) != | 281 i->second.content_settings_for_resources.find(requested_setting) != |
| 361 i->second.content_settings_for_resources.end()) { | 282 i->second.content_settings_for_resources.end()) { |
| 362 return i->second.content_settings_for_resources.find( | 283 return i->second.content_settings_for_resources.find( |
| 363 requested_setting)->second; | 284 requested_setting)->second; |
| 364 } | 285 } |
| 365 | 286 |
| 366 // Match patterns starting with the most concrete pattern match. | 287 // Match patterns starting with the most concrete pattern match. |
| 367 for (std::string key = std::string(kDomainWildcard) + host; ; ) { | 288 for (std::string key = |
| 289 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { |
| 368 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | 290 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); |
| 369 if (i != off_the_record_settings_.end() && | 291 if (i != off_the_record_settings_.end() && |
| 370 i->second.content_settings_for_resources.find(requested_setting) != | 292 i->second.content_settings_for_resources.find(requested_setting) != |
| 371 i->second.content_settings_for_resources.end()) { | 293 i->second.content_settings_for_resources.end()) { |
| 372 return i->second.content_settings_for_resources.find( | 294 return i->second.content_settings_for_resources.find( |
| 373 requested_setting)->second; | 295 requested_setting)->second; |
| 374 } | 296 } |
| 375 | 297 |
| 376 i = host_content_settings_.find(key); | 298 i = host_content_settings_.find(key); |
| 377 if (i != host_content_settings_.end() && | 299 if (i != host_content_settings_.end() && |
| 378 i->second.content_settings_for_resources.find(requested_setting) != | 300 i->second.content_settings_for_resources.find(requested_setting) != |
| 379 i->second.content_settings_for_resources.end()) { | 301 i->second.content_settings_for_resources.end()) { |
| 380 return i->second.content_settings_for_resources.find( | 302 return i->second.content_settings_for_resources.find( |
| 381 requested_setting)->second; | 303 requested_setting)->second; |
| 382 } | 304 } |
| 383 | 305 |
| 384 const size_t next_dot = key.find('.', kDomainWildcardLength); | 306 const size_t next_dot = |
| 307 key.find('.', ContentSettingsPattern::kDomainWildcardLength); |
| 385 if (next_dot == std::string::npos) | 308 if (next_dot == std::string::npos) |
| 386 break; | 309 break; |
| 387 key.erase(kDomainWildcardLength, next_dot - kDomainWildcardLength + 1); | 310 key.erase(ContentSettingsPattern::kDomainWildcardLength, |
| 311 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |
| 388 } | 312 } |
| 389 | 313 |
| 390 return CONTENT_SETTING_DEFAULT; | 314 return CONTENT_SETTING_DEFAULT; |
| 391 } | 315 } |
| 392 | 316 |
| 393 ContentSettings HostContentSettingsMap::GetContentSettings( | 317 ContentSettings HostContentSettingsMap::GetContentSettings( |
| 394 const GURL& url) const { | 318 const GURL& url) const { |
| 395 ContentSettings output = GetNonDefaultContentSettings(url); | 319 ContentSettings output = GetNonDefaultContentSettings(url); |
| 396 | 320 |
| 397 AutoLock auto_lock(lock_); | 321 AutoLock auto_lock(lock_); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 // match. The additional off-the-record exceptions always overwrite the | 361 // match. The additional off-the-record exceptions always overwrite the |
| 438 // regular ones. | 362 // regular ones. |
| 439 i = off_the_record_settings_.find(host); | 363 i = off_the_record_settings_.find(host); |
| 440 if (i != off_the_record_settings_.end()) { | 364 if (i != off_the_record_settings_.end()) { |
| 441 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | 365 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |
| 442 if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) | 366 if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) |
| 443 output.settings[j] = i->second.content_settings.settings[j]; | 367 output.settings[j] = i->second.content_settings.settings[j]; |
| 444 } | 368 } |
| 445 | 369 |
| 446 // Match patterns starting with the most concrete pattern match. | 370 // Match patterns starting with the most concrete pattern match. |
| 447 for (std::string key = std::string(kDomainWildcard) + host; ; ) { | 371 for (std::string key = |
| 372 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { |
| 448 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | 373 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); |
| 449 if (i != off_the_record_settings_.end()) { | 374 if (i != off_the_record_settings_.end()) { |
| 450 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 375 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 451 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | 376 if (output.settings[j] == CONTENT_SETTING_DEFAULT) |
| 452 output.settings[j] = i->second.content_settings.settings[j]; | 377 output.settings[j] = i->second.content_settings.settings[j]; |
| 453 } | 378 } |
| 454 } | 379 } |
| 455 i = host_content_settings_.find(key); | 380 i = host_content_settings_.find(key); |
| 456 if (i != host_content_settings_.end()) { | 381 if (i != host_content_settings_.end()) { |
| 457 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 382 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 458 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | 383 if (output.settings[j] == CONTENT_SETTING_DEFAULT) |
| 459 output.settings[j] = i->second.content_settings.settings[j]; | 384 output.settings[j] = i->second.content_settings.settings[j]; |
| 460 } | 385 } |
| 461 } | 386 } |
| 462 const size_t next_dot = key.find('.', kDomainWildcardLength); | 387 const size_t next_dot = |
| 388 key.find('.', ContentSettingsPattern::kDomainWildcardLength); |
| 463 if (next_dot == std::string::npos) | 389 if (next_dot == std::string::npos) |
| 464 break; | 390 break; |
| 465 key.erase(kDomainWildcardLength, next_dot - kDomainWildcardLength + 1); | 391 key.erase(ContentSettingsPattern::kDomainWildcardLength, |
| 392 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |
| 466 } | 393 } |
| 467 | 394 |
| 468 return output; | 395 return output; |
| 469 } | 396 } |
| 470 | 397 |
| 471 void HostContentSettingsMap::GetSettingsForOneType( | 398 void HostContentSettingsMap::GetSettingsForOneType( |
| 472 ContentSettingsType content_type, | 399 ContentSettingsType content_type, |
| 473 const std::string& resource_identifier, | 400 const std::string& resource_identifier, |
| 474 SettingsForOneType* settings) const { | 401 SettingsForOneType* settings) const { |
| 475 DCHECK(RequiresResourceIdentifier(content_type) != | 402 DCHECK(RequiresResourceIdentifier(content_type) != |
| (...skipping 16 matching lines...) Expand all Loading... |
| 492 setting = i->second.content_settings_for_resources.find( | 419 setting = i->second.content_settings_for_resources.find( |
| 493 requested_setting)->second; | 420 requested_setting)->second; |
| 494 else | 421 else |
| 495 setting = CONTENT_SETTING_DEFAULT; | 422 setting = CONTENT_SETTING_DEFAULT; |
| 496 } else { | 423 } else { |
| 497 setting = i->second.content_settings.settings[content_type]; | 424 setting = i->second.content_settings.settings[content_type]; |
| 498 } | 425 } |
| 499 if (setting != CONTENT_SETTING_DEFAULT) { | 426 if (setting != CONTENT_SETTING_DEFAULT) { |
| 500 // Use of push_back() relies on the map iterator traversing in order of | 427 // Use of push_back() relies on the map iterator traversing in order of |
| 501 // ascending keys. | 428 // ascending keys. |
| 502 settings->push_back(std::make_pair(Pattern(i->first), setting)); | 429 settings->push_back( |
| 430 std::make_pair(ContentSettingsPattern(i->first), setting)); |
| 503 } | 431 } |
| 504 } | 432 } |
| 505 } | 433 } |
| 506 | 434 |
| 507 void HostContentSettingsMap::SetDefaultContentSetting( | 435 void HostContentSettingsMap::SetDefaultContentSetting( |
| 508 ContentSettingsType content_type, | 436 ContentSettingsType content_type, |
| 509 ContentSetting setting) { | 437 ContentSetting setting) { |
| 510 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 438 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 511 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 512 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 440 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || |
| (...skipping 23 matching lines...) Expand all Loading... |
| 536 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, | 464 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, |
| 537 NULL); | 465 NULL); |
| 538 } else { | 466 } else { |
| 539 default_content_settings_.settings[content_type] = setting; | 467 default_content_settings_.settings[content_type] = setting; |
| 540 default_settings_dictionary->SetWithoutPathExpansion( | 468 default_settings_dictionary->SetWithoutPathExpansion( |
| 541 dictionary_path, Value::CreateIntegerValue(setting)); | 469 dictionary_path, Value::CreateIntegerValue(setting)); |
| 542 } | 470 } |
| 543 } | 471 } |
| 544 updating_preferences_ = false; | 472 updating_preferences_ = false; |
| 545 | 473 |
| 546 NotifyObservers(ContentSettingsDetails(Pattern(), content_type, "")); | 474 NotifyObservers( |
| 475 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); |
| 547 } | 476 } |
| 548 | 477 |
| 549 void HostContentSettingsMap::SetContentSetting( | 478 void HostContentSettingsMap::SetContentSetting( |
| 550 const Pattern& original_pattern, | 479 const ContentSettingsPattern& original_pattern, |
| 551 ContentSettingsType content_type, | 480 ContentSettingsType content_type, |
| 552 const std::string& resource_identifier, | 481 const std::string& resource_identifier, |
| 553 ContentSetting setting) { | 482 ContentSetting setting) { |
| 554 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 483 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 556 DCHECK_NE(RequiresResourceIdentifier(content_type), | 485 DCHECK_NE(RequiresResourceIdentifier(content_type), |
| 557 resource_identifier.empty()); | 486 resource_identifier.empty()); |
| 558 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || | 487 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || |
| 559 setting != CONTENT_SETTING_ASK || | 488 setting != CONTENT_SETTING_ASK || |
| 560 CommandLine::ForCurrentProcess()->HasSwitch( | 489 CommandLine::ForCurrentProcess()->HasSwitch( |
| 561 switches::kEnableClickToPlay)); | 490 switches::kEnableClickToPlay)); |
| 562 | 491 |
| 563 const Pattern pattern(original_pattern.CanonicalizePattern()); | 492 const ContentSettingsPattern pattern(original_pattern.CanonicalizePattern()); |
| 564 | 493 |
| 565 bool early_exit = false; | 494 bool early_exit = false; |
| 566 std::string pattern_str(pattern.AsString()); | 495 std::string pattern_str(pattern.AsString()); |
| 567 PrefService* prefs = NULL; | 496 PrefService* prefs = NULL; |
| 568 DictionaryValue* all_settings_dictionary = NULL; | 497 DictionaryValue* all_settings_dictionary = NULL; |
| 569 HostContentSettings* map_to_modify = &off_the_record_settings_; | 498 HostContentSettings* map_to_modify = &off_the_record_settings_; |
| 570 if (!is_off_the_record_) { | 499 if (!is_off_the_record_) { |
| 571 prefs = profile_->GetPrefs(); | 500 prefs = profile_->GetPrefs(); |
| 572 all_settings_dictionary = | 501 all_settings_dictionary = |
| 573 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); | 502 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 NotifyObservers(ContentSettingsDetails(pattern, content_type, "")); | 581 NotifyObservers(ContentSettingsDetails(pattern, content_type, "")); |
| 653 } | 582 } |
| 654 | 583 |
| 655 void HostContentSettingsMap::AddExceptionForURL( | 584 void HostContentSettingsMap::AddExceptionForURL( |
| 656 const GURL& url, | 585 const GURL& url, |
| 657 ContentSettingsType content_type, | 586 ContentSettingsType content_type, |
| 658 const std::string& resource_identifier, | 587 const std::string& resource_identifier, |
| 659 ContentSetting setting) { | 588 ContentSetting setting) { |
| 660 // Make sure there is no entry that would override the pattern we are about | 589 // Make sure there is no entry that would override the pattern we are about |
| 661 // to insert for exactly this URL. | 590 // to insert for exactly this URL. |
| 662 SetContentSetting(Pattern::FromURLNoWildcard(url), | 591 SetContentSetting(ContentSettingsPattern::FromURLNoWildcard(url), |
| 663 content_type, | 592 content_type, |
| 664 resource_identifier, | 593 resource_identifier, |
| 665 CONTENT_SETTING_DEFAULT); | 594 CONTENT_SETTING_DEFAULT); |
| 666 SetContentSetting(Pattern::FromURL(url), | 595 SetContentSetting(ContentSettingsPattern::FromURL(url), |
| 667 content_type, | 596 content_type, |
| 668 resource_identifier, | 597 resource_identifier, |
| 669 setting); | 598 setting); |
| 670 } | 599 } |
| 671 | 600 |
| 672 void HostContentSettingsMap::ClearSettingsForOneType( | 601 void HostContentSettingsMap::ClearSettingsForOneType( |
| 673 ContentSettingsType content_type) { | 602 ContentSettingsType content_type) { |
| 674 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. | 603 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. |
| 675 | 604 |
| 676 PrefService* prefs = NULL; | 605 PrefService* prefs = NULL; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 ++i; | 644 ++i; |
| 716 } | 645 } |
| 717 } | 646 } |
| 718 } | 647 } |
| 719 | 648 |
| 720 updating_preferences_ = true; | 649 updating_preferences_ = true; |
| 721 if (!is_off_the_record_) | 650 if (!is_off_the_record_) |
| 722 ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); | 651 ScopedPrefUpdate update(prefs, prefs::kContentSettingsPatterns); |
| 723 updating_preferences_ = false; | 652 updating_preferences_ = false; |
| 724 | 653 |
| 725 NotifyObservers(ContentSettingsDetails(Pattern(), content_type, "")); | 654 NotifyObservers( |
| 655 ContentSettingsDetails(ContentSettingsPattern(), content_type, "")); |
| 726 } | 656 } |
| 727 | 657 |
| 728 bool HostContentSettingsMap::RequiresResourceIdentifier( | 658 bool HostContentSettingsMap::RequiresResourceIdentifier( |
| 729 ContentSettingsType content_type) const { | 659 ContentSettingsType content_type) const { |
| 730 if (CommandLine::ForCurrentProcess()->HasSwitch( | 660 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 731 switches::kEnableResourceContentSettings)) { | 661 switches::kEnableResourceContentSettings)) { |
| 732 return kRequiresResourceIdentifier[content_type]; | 662 return kRequiresResourceIdentifier[content_type]; |
| 733 } else { | 663 } else { |
| 734 return false; | 664 return false; |
| 735 } | 665 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 813 PrefService* prefs = profile_->GetPrefs(); | 743 PrefService* prefs = profile_->GetPrefs(); |
| 814 updating_preferences_ = true; | 744 updating_preferences_ = true; |
| 815 prefs->ClearPref(prefs::kDefaultContentSettings); | 745 prefs->ClearPref(prefs::kDefaultContentSettings); |
| 816 prefs->ClearPref(prefs::kContentSettingsPatterns); | 746 prefs->ClearPref(prefs::kContentSettingsPatterns); |
| 817 // If the block third party cookies preference is managed we still must | 747 // If the block third party cookies preference is managed we still must |
| 818 // clear it in order to restore the default value for later when the | 748 // clear it in order to restore the default value for later when the |
| 819 // preference is not managed anymore. | 749 // preference is not managed anymore. |
| 820 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 750 prefs->ClearPref(prefs::kBlockThirdPartyCookies); |
| 821 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); | 751 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); |
| 822 updating_preferences_ = false; | 752 updating_preferences_ = false; |
| 823 NotifyObservers( | 753 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 824 ContentSettingsDetails(Pattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); | 754 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 755 "")); |
| 825 } | 756 } |
| 826 } | 757 } |
| 827 | 758 |
| 828 void HostContentSettingsMap::Observe(NotificationType type, | 759 void HostContentSettingsMap::Observe(NotificationType type, |
| 829 const NotificationSource& source, | 760 const NotificationSource& source, |
| 830 const NotificationDetails& details) { | 761 const NotificationDetails& details) { |
| 831 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 762 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 832 | 763 |
| 833 if (NotificationType::PREF_CHANGED == type) { | 764 if (NotificationType::PREF_CHANGED == type) { |
| 834 if (updating_preferences_) | 765 if (updating_preferences_) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 869 } else if (prefs::kManagedDefaultPopupsSetting == *name) { | 800 } else if (prefs::kManagedDefaultPopupsSetting == *name) { |
| 870 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_POPUPS, | 801 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_POPUPS, |
| 871 profile_->GetPrefs(), | 802 profile_->GetPrefs(), |
| 872 &managed_default_content_settings_); | 803 &managed_default_content_settings_); |
| 873 } else { | 804 } else { |
| 874 NOTREACHED() << "Unexpected preference observed"; | 805 NOTREACHED() << "Unexpected preference observed"; |
| 875 return; | 806 return; |
| 876 } | 807 } |
| 877 | 808 |
| 878 if (!is_off_the_record_) { | 809 if (!is_off_the_record_) { |
| 879 NotifyObservers( | 810 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), |
| 880 ContentSettingsDetails(Pattern(), CONTENT_SETTINGS_TYPE_DEFAULT, "")); | 811 CONTENT_SETTINGS_TYPE_DEFAULT, |
| 812 "")); |
| 881 } | 813 } |
| 882 } else if (NotificationType::PROFILE_DESTROYED == type) { | 814 } else if (NotificationType::PROFILE_DESTROYED == type) { |
| 883 UnregisterObservers(); | 815 UnregisterObservers(); |
| 884 } else { | 816 } else { |
| 885 NOTREACHED() << "Unexpected notification"; | 817 NOTREACHED() << "Unexpected notification"; |
| 886 } | 818 } |
| 887 } | 819 } |
| 888 | 820 |
| 889 HostContentSettingsMap::~HostContentSettingsMap() { | 821 HostContentSettingsMap::~HostContentSettingsMap() { |
| 890 UnregisterObservers(); | 822 UnregisterObservers(); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1034 host_content_settings_.clear(); | 966 host_content_settings_.clear(); |
| 1035 | 967 |
| 1036 // Careful: The returned value could be NULL if the pref has never been set. | 968 // Careful: The returned value could be NULL if the pref has never been set. |
| 1037 if (all_settings_dictionary != NULL) { | 969 if (all_settings_dictionary != NULL) { |
| 1038 // Convert all Unicode patterns into punycode form, then read. | 970 // Convert all Unicode patterns into punycode form, then read. |
| 1039 CanonicalizeContentSettingsExceptions(all_settings_dictionary); | 971 CanonicalizeContentSettingsExceptions(all_settings_dictionary); |
| 1040 | 972 |
| 1041 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 973 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 1042 i != all_settings_dictionary->end_keys(); ++i) { | 974 i != all_settings_dictionary->end_keys(); ++i) { |
| 1043 const std::string& pattern(*i); | 975 const std::string& pattern(*i); |
| 1044 if (!Pattern(pattern).IsValid()) | 976 if (!ContentSettingsPattern(pattern).IsValid()) |
| 1045 LOG(WARNING) << "Invalid pattern stored in content settings"; | 977 LOG(WARNING) << "Invalid pattern stored in content settings"; |
| 1046 DictionaryValue* pattern_settings_dictionary = NULL; | 978 DictionaryValue* pattern_settings_dictionary = NULL; |
| 1047 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 979 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 1048 pattern, &pattern_settings_dictionary); | 980 pattern, &pattern_settings_dictionary); |
| 1049 DCHECK(found); | 981 DCHECK(found); |
| 1050 ContentSettings settings; | 982 ContentSettings settings; |
| 1051 GetSettingsFromDictionary(pattern_settings_dictionary, &settings); | 983 GetSettingsFromDictionary(pattern_settings_dictionary, &settings); |
| 1052 host_content_settings_[pattern].content_settings = settings; | 984 host_content_settings_[pattern].content_settings = settings; |
| 1053 GetResourceSettingsFromDictionary( | 985 GetResourceSettingsFromDictionary( |
| 1054 pattern_settings_dictionary, | 986 pattern_settings_dictionary, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1092 } | 1024 } |
| 1093 | 1025 |
| 1094 void HostContentSettingsMap::MigrateObsoletePopupsPref(PrefService* prefs) { | 1026 void HostContentSettingsMap::MigrateObsoletePopupsPref(PrefService* prefs) { |
| 1095 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { | 1027 if (prefs->HasPrefPath(prefs::kPopupWhitelistedHosts)) { |
| 1096 const ListValue* whitelist_pref = | 1028 const ListValue* whitelist_pref = |
| 1097 prefs->GetList(prefs::kPopupWhitelistedHosts); | 1029 prefs->GetList(prefs::kPopupWhitelistedHosts); |
| 1098 for (ListValue::const_iterator i(whitelist_pref->begin()); | 1030 for (ListValue::const_iterator i(whitelist_pref->begin()); |
| 1099 i != whitelist_pref->end(); ++i) { | 1031 i != whitelist_pref->end(); ++i) { |
| 1100 std::string host; | 1032 std::string host; |
| 1101 (*i)->GetAsString(&host); | 1033 (*i)->GetAsString(&host); |
| 1102 SetContentSetting(Pattern(host), CONTENT_SETTINGS_TYPE_POPUPS, "", | 1034 SetContentSetting(ContentSettingsPattern(host), |
| 1035 CONTENT_SETTINGS_TYPE_POPUPS, |
| 1036 "", |
| 1103 CONTENT_SETTING_ALLOW); | 1037 CONTENT_SETTING_ALLOW); |
| 1104 } | 1038 } |
| 1105 prefs->ClearPref(prefs::kPopupWhitelistedHosts); | 1039 prefs->ClearPref(prefs::kPopupWhitelistedHosts); |
| 1106 } | 1040 } |
| 1107 } | 1041 } |
| 1108 | 1042 |
| 1109 void HostContentSettingsMap::MigrateObsoletePerhostPref(PrefService* prefs) { | 1043 void HostContentSettingsMap::MigrateObsoletePerhostPref(PrefService* prefs) { |
| 1110 if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) { | 1044 if (prefs->HasPrefPath(prefs::kPerHostContentSettings)) { |
| 1111 const DictionaryValue* all_settings_dictionary = | 1045 const DictionaryValue* all_settings_dictionary = |
| 1112 prefs->GetDictionary(prefs::kPerHostContentSettings); | 1046 prefs->GetDictionary(prefs::kPerHostContentSettings); |
| 1113 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 1047 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 1114 i != all_settings_dictionary->end_keys(); ++i) { | 1048 i != all_settings_dictionary->end_keys(); ++i) { |
| 1115 const std::string& host(*i); | 1049 const std::string& host(*i); |
| 1116 Pattern pattern(std::string(kDomainWildcard) + host); | 1050 ContentSettingsPattern pattern( |
| 1051 std::string(ContentSettingsPattern::kDomainWildcard) + host); |
| 1117 DictionaryValue* host_settings_dictionary = NULL; | 1052 DictionaryValue* host_settings_dictionary = NULL; |
| 1118 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 1053 bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 1119 host, &host_settings_dictionary); | 1054 host, &host_settings_dictionary); |
| 1120 DCHECK(found); | 1055 DCHECK(found); |
| 1121 ContentSettings settings; | 1056 ContentSettings settings; |
| 1122 GetSettingsFromDictionary(host_settings_dictionary, &settings); | 1057 GetSettingsFromDictionary(host_settings_dictionary, &settings); |
| 1123 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | 1058 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 1124 if (settings.settings[j] != CONTENT_SETTING_DEFAULT && | 1059 if (settings.settings[j] != CONTENT_SETTING_DEFAULT && |
| 1125 !RequiresResourceIdentifier(ContentSettingsType(j))) | 1060 !RequiresResourceIdentifier(ContentSettingsType(j))) |
| 1126 SetContentSetting( | 1061 SetContentSetting( |
| 1127 pattern, ContentSettingsType(j), "", settings.settings[j]); | 1062 pattern, ContentSettingsType(j), "", settings.settings[j]); |
| 1128 } | 1063 } |
| 1129 } | 1064 } |
| 1130 prefs->ClearPref(prefs::kPerHostContentSettings); | 1065 prefs->ClearPref(prefs::kPerHostContentSettings); |
| 1131 } | 1066 } |
| 1132 } | 1067 } |
| 1133 | 1068 |
| 1134 void HostContentSettingsMap::CanonicalizeContentSettingsExceptions( | 1069 void HostContentSettingsMap::CanonicalizeContentSettingsExceptions( |
| 1135 DictionaryValue* all_settings_dictionary) { | 1070 DictionaryValue* all_settings_dictionary) { |
| 1136 DCHECK(all_settings_dictionary); | 1071 DCHECK(all_settings_dictionary); |
| 1137 | 1072 |
| 1138 std::vector<std::string> remove_items; | 1073 std::vector<std::string> remove_items; |
| 1139 std::vector<std::pair<std::string, std::string> > move_items; | 1074 std::vector<std::pair<std::string, std::string> > move_items; |
| 1140 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); | 1075 for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys()); |
| 1141 i != all_settings_dictionary->end_keys(); ++i) { | 1076 i != all_settings_dictionary->end_keys(); ++i) { |
| 1142 const std::string& pattern(*i); | 1077 const std::string& pattern(*i); |
| 1143 const std::string canonicalized_pattern = | 1078 const std::string canonicalized_pattern = |
| 1144 Pattern(pattern).CanonicalizePattern(); | 1079 ContentSettingsPattern(pattern).CanonicalizePattern(); |
| 1145 | 1080 |
| 1146 if (canonicalized_pattern.empty() || canonicalized_pattern == pattern) | 1081 if (canonicalized_pattern.empty() || canonicalized_pattern == pattern) |
| 1147 continue; | 1082 continue; |
| 1148 | 1083 |
| 1149 // Clear old pattern if prefs already have canonicalized pattern. | 1084 // Clear old pattern if prefs already have canonicalized pattern. |
| 1150 DictionaryValue* new_pattern_settings_dictionary = NULL; | 1085 DictionaryValue* new_pattern_settings_dictionary = NULL; |
| 1151 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( | 1086 if (all_settings_dictionary->GetDictionaryWithoutPathExpansion( |
| 1152 canonicalized_pattern, &new_pattern_settings_dictionary)) { | 1087 canonicalized_pattern, &new_pattern_settings_dictionary)) { |
| 1153 remove_items.push_back(pattern); | 1088 remove_items.push_back(pattern); |
| 1154 continue; | 1089 continue; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1167 } | 1102 } |
| 1168 | 1103 |
| 1169 for (size_t i = 0; i < move_items.size(); ++i) { | 1104 for (size_t i = 0; i < move_items.size(); ++i) { |
| 1170 Value* pattern_settings_dictionary = NULL; | 1105 Value* pattern_settings_dictionary = NULL; |
| 1171 all_settings_dictionary->RemoveWithoutPathExpansion( | 1106 all_settings_dictionary->RemoveWithoutPathExpansion( |
| 1172 move_items[i].first, &pattern_settings_dictionary); | 1107 move_items[i].first, &pattern_settings_dictionary); |
| 1173 all_settings_dictionary->SetWithoutPathExpansion( | 1108 all_settings_dictionary->SetWithoutPathExpansion( |
| 1174 move_items[i].second, pattern_settings_dictionary); | 1109 move_items[i].second, pattern_settings_dictionary); |
| 1175 } | 1110 } |
| 1176 } | 1111 } |
| OLD | NEW |