Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 const_rules_iterator; | 61 const_rules_iterator; |
| 62 | 62 |
| 63 const char* kProviderNames[] = { | 63 const char* kProviderNames[] = { |
| 64 "policy", | 64 "policy", |
| 65 "extension", | 65 "extension", |
| 66 "preference" | 66 "preference" |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 } // namespace | 69 } // namespace |
| 70 | 70 |
| 71 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) | 71 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) |
|
markusheintz_
2011/07/07 15:23:18
Since we do not store the profile anywhere. Should
Bernhard Bauer
2011/07/08 17:05:27
Done.
| |
| 72 : profile_(profile), | 72 : prefs_(profile->GetPrefs()), |
| 73 is_off_the_record_(profile_->IsOffTheRecord()), | 73 is_off_the_record_(profile->IsOffTheRecord()), |
| 74 updating_preferences_(false), | 74 updating_preferences_(false), |
| 75 block_third_party_cookies_(false), | 75 block_third_party_cookies_(false), |
| 76 is_block_third_party_cookies_managed_(false) { | 76 is_block_third_party_cookies_managed_(false) { |
| 77 // The order in which the default content settings providers are created is | 77 // The order in which the default content settings providers are created is |
| 78 // critical, as providers that are further down in the list (i.e. added later) | 78 // critical, as providers that are further down in the list (i.e. added later) |
| 79 // override providers further up. | 79 // override providers further up. |
| 80 default_content_settings_providers_.push_back( | 80 default_content_settings_providers_.push_back( |
| 81 make_linked_ptr(new content_settings::PrefDefaultProvider(profile))); | 81 make_linked_ptr(new content_settings::PrefDefaultProvider( |
| 82 this, prefs_, is_off_the_record_))); | |
| 82 content_settings::DefaultProviderInterface* policy_default_provider = | 83 content_settings::DefaultProviderInterface* policy_default_provider = |
| 83 new content_settings::PolicyDefaultProvider(profile); | 84 new content_settings::PolicyDefaultProvider(this, prefs_); |
| 84 default_content_settings_providers_.push_back( | 85 default_content_settings_providers_.push_back( |
| 85 make_linked_ptr(policy_default_provider)); | 86 make_linked_ptr(policy_default_provider)); |
| 86 | 87 |
| 87 PrefService* prefs = profile_->GetPrefs(); | |
| 88 | |
| 89 // TODO(markusheintz): Discuss whether it is sensible to move migration code | 88 // TODO(markusheintz): Discuss whether it is sensible to move migration code |
| 90 // to PrefContentSettingsProvider. | 89 // to PrefContentSettingsProvider. |
| 91 MigrateObsoleteCookiePref(prefs); | 90 MigrateObsoleteCookiePref(); |
| 92 | 91 |
| 93 // Read misc. global settings. | 92 // Read misc. global settings. |
| 94 block_third_party_cookies_ = | 93 block_third_party_cookies_ = |
| 95 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); | 94 prefs_->GetBoolean(prefs::kBlockThirdPartyCookies); |
| 96 if (block_third_party_cookies_) { | 95 if (block_third_party_cookies_) { |
| 97 UserMetrics::RecordAction( | 96 UserMetrics::RecordAction( |
| 98 UserMetricsAction("ThirdPartyCookieBlockingEnabled")); | 97 UserMetricsAction("ThirdPartyCookieBlockingEnabled")); |
| 99 } else { | 98 } else { |
| 100 UserMetrics::RecordAction( | 99 UserMetrics::RecordAction( |
| 101 UserMetricsAction("ThirdPartyCookieBlockingDisabled")); | 100 UserMetricsAction("ThirdPartyCookieBlockingDisabled")); |
| 102 } | 101 } |
| 103 is_block_third_party_cookies_managed_ = | 102 is_block_third_party_cookies_managed_ = |
| 104 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); | 103 prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies); |
| 105 | 104 |
| 106 // User defined non default content settings are provided by the PrefProvider. | 105 // User defined non default content settings are provided by the PrefProvider. |
| 107 // The order in which the content settings providers are created is critical, | 106 // The order in which the content settings providers are created is critical, |
| 108 // as providers that are further up in the list (i.e. added earlier) override | 107 // as providers that are further up in the list (i.e. added earlier) override |
| 109 // providers further down. | 108 // providers further down. |
| 110 content_settings_providers_.push_back(make_linked_ptr( | 109 content_settings_providers_.push_back(make_linked_ptr( |
| 111 new content_settings::PolicyProvider(profile, policy_default_provider))); | 110 new content_settings::PolicyProvider(this, |
| 111 prefs_, | |
| 112 policy_default_provider))); | |
| 112 ExtensionService* extension_service = profile->GetExtensionService(); | 113 ExtensionService* extension_service = profile->GetExtensionService(); |
| 113 if (extension_service) { | 114 if (extension_service) { |
| 114 // |extension_service| can be NULL in unit tests. | 115 // |extension_service| can be NULL in unit tests. |
| 115 content_settings_providers_.push_back(make_linked_ptr( | 116 content_settings_providers_.push_back(make_linked_ptr( |
| 116 new content_settings::ExtensionProvider( | 117 new content_settings::ExtensionProvider( |
| 117 this, | 118 this, |
| 118 extension_service->GetExtensionContentSettingsStore(), | 119 extension_service->GetExtensionContentSettingsStore(), |
| 119 is_off_the_record_))); | 120 is_off_the_record_))); |
| 120 } | 121 } |
| 121 content_settings_providers_.push_back( | 122 content_settings_providers_.push_back(make_linked_ptr( |
| 122 make_linked_ptr(new content_settings::PrefProvider(profile))); | 123 new content_settings::PrefProvider(this, prefs_, is_off_the_record_))); |
| 123 | 124 |
| 124 pref_change_registrar_.Init(prefs); | 125 pref_change_registrar_.Init(prefs_); |
| 125 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); | 126 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); |
| 126 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | |
| 127 Source<Profile>(profile_)); | |
| 128 } | 127 } |
| 129 | 128 |
| 130 // static | 129 // static |
| 131 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { | 130 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { |
| 132 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, | 131 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, |
| 133 false, | 132 false, |
| 134 PrefService::SYNCABLE_PREF); | 133 PrefService::SYNCABLE_PREF); |
| 135 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, | 134 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, |
| 136 0, | 135 0, |
| 137 PrefService::UNSYNCABLE_PREF); | 136 PrefService::UNSYNCABLE_PREF); |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 case CONTENT_SETTINGS_TYPE_GEOLOCATION: | 414 case CONTENT_SETTINGS_TYPE_GEOLOCATION: |
| 416 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: | 415 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: |
| 417 return (setting == CONTENT_SETTING_ASK); | 416 return (setting == CONTENT_SETTING_ASK); |
| 418 default: | 417 default: |
| 419 return false; | 418 return false; |
| 420 } | 419 } |
| 421 } | 420 } |
| 422 | 421 |
| 423 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { | 422 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 424 DCHECK(prefs_); | |
| 425 | 425 |
| 426 // This setting may not be directly modified for OTR sessions. Instead, it | 426 // This setting may not be directly modified for OTR sessions. Instead, it |
| 427 // is synced to the main profile's setting. | 427 // is synced to the main profile's setting. |
| 428 if (is_off_the_record_) { | 428 if (is_off_the_record_) { |
| 429 NOTREACHED(); | 429 NOTREACHED(); |
| 430 return; | 430 return; |
| 431 } | 431 } |
| 432 | 432 |
| 433 PrefService* prefs = profile_->GetPrefs(); | |
| 434 // If the preference block-third-party-cookies is managed then do not allow to | 433 // If the preference block-third-party-cookies is managed then do not allow to |
| 435 // change it. | 434 // change it. |
| 436 if (prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { | 435 if (prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { |
| 437 NOTREACHED(); | 436 NOTREACHED(); |
| 438 return; | 437 return; |
| 439 } | 438 } |
| 440 | 439 |
| 441 { | 440 { |
| 442 base::AutoLock auto_lock(lock_); | 441 base::AutoLock auto_lock(lock_); |
| 443 block_third_party_cookies_ = block; | 442 block_third_party_cookies_ = block; |
| 444 } | 443 } |
| 445 | 444 |
| 446 profile_->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, block); | 445 prefs_->SetBoolean(prefs::kBlockThirdPartyCookies, block); |
| 447 } | 446 } |
| 448 | 447 |
| 449 void HostContentSettingsMap::Observe(NotificationType type, | 448 void HostContentSettingsMap::Observe(NotificationType type, |
| 450 const NotificationSource& source, | 449 const NotificationSource& source, |
| 451 const NotificationDetails& details) { | 450 const NotificationDetails& details) { |
| 452 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 453 | 452 |
| 454 if (type == NotificationType::PREF_CHANGED) { | 453 if (type == NotificationType::PREF_CHANGED) { |
| 455 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 454 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); |
| 456 if (updating_preferences_) | 455 if (updating_preferences_) |
| 457 return; | 456 return; |
| 458 | 457 |
| 459 std::string* name = Details<std::string>(details).ptr(); | 458 std::string* name = Details<std::string>(details).ptr(); |
| 460 if (*name == prefs::kBlockThirdPartyCookies) { | 459 if (*name == prefs::kBlockThirdPartyCookies) { |
| 461 base::AutoLock auto_lock(lock_); | 460 base::AutoLock auto_lock(lock_); |
| 462 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 461 block_third_party_cookies_ = prefs_->GetBoolean( |
| 463 prefs::kBlockThirdPartyCookies); | 462 prefs::kBlockThirdPartyCookies); |
| 464 is_block_third_party_cookies_managed_ = | 463 is_block_third_party_cookies_managed_ = |
| 465 profile_->GetPrefs()->IsManagedPreference( | 464 prefs_->IsManagedPreference( |
| 466 prefs::kBlockThirdPartyCookies); | 465 prefs::kBlockThirdPartyCookies); |
| 467 } else { | 466 } else { |
| 468 NOTREACHED() << "Unexpected preference observed"; | 467 NOTREACHED() << "Unexpected preference observed"; |
| 469 return; | 468 return; |
| 470 } | 469 } |
| 471 } else if (type == NotificationType::PROFILE_DESTROYED) { | |
| 472 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | |
| 473 UnregisterObservers(); | |
| 474 } else { | 470 } else { |
| 475 NOTREACHED() << "Unexpected notification"; | 471 NOTREACHED() << "Unexpected notification"; |
| 476 } | 472 } |
| 477 } | 473 } |
| 478 | 474 |
| 479 HostContentSettingsMap::~HostContentSettingsMap() { | 475 HostContentSettingsMap::~HostContentSettingsMap() { |
| 480 UnregisterObservers(); | 476 DCHECK(!prefs_); |
| 481 } | 477 } |
| 482 | 478 |
| 483 bool HostContentSettingsMap::IsDefaultContentSettingManaged( | 479 bool HostContentSettingsMap::IsDefaultContentSettingManaged( |
| 484 ContentSettingsType content_type) const { | 480 ContentSettingsType content_type) const { |
| 485 for (ConstDefaultProviderIterator provider = | 481 for (ConstDefaultProviderIterator provider = |
| 486 default_content_settings_providers_.begin(); | 482 default_content_settings_providers_.begin(); |
| 487 provider != default_content_settings_providers_.end(); ++provider) { | 483 provider != default_content_settings_providers_.end(); ++provider) { |
| 488 if ((*provider)->DefaultSettingIsManaged(content_type)) | 484 if ((*provider)->DefaultSettingIsManaged(content_type)) |
| 489 return true; | 485 return true; |
| 490 } | 486 } |
| 491 return false; | 487 return false; |
| 492 } | 488 } |
| 493 | 489 |
| 494 void HostContentSettingsMap::ShutdownOnUIThread() { | 490 void HostContentSettingsMap::ShutdownOnUIThread() { |
| 491 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 492 DCHECK(prefs_); | |
| 493 pref_change_registrar_.RemoveAll(); | |
| 494 prefs_ = NULL; | |
| 495 for (ProviderIterator it = content_settings_providers_.begin(); | 495 for (ProviderIterator it = content_settings_providers_.begin(); |
| 496 it != content_settings_providers_.end(); | 496 it != content_settings_providers_.end(); |
| 497 ++it) { | 497 ++it) { |
| 498 (*it)->ShutdownOnUIThread(); | 498 (*it)->ShutdownOnUIThread(); |
| 499 } | 499 } |
| 500 for (DefaultProviderIterator it = default_content_settings_providers_.begin(); | |
| 501 it != default_content_settings_providers_.end(); | |
| 502 ++it) { | |
| 503 (*it)->ShutdownOnUIThread(); | |
| 504 } | |
| 500 } | 505 } |
| 501 | 506 |
| 502 void HostContentSettingsMap::UnregisterObservers() { | 507 void HostContentSettingsMap::MigrateObsoleteCookiePref() { |
| 503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 508 if (prefs_->HasPrefPath(prefs::kCookieBehavior)) { |
| 504 if (!profile_) | 509 int cookie_behavior = prefs_->GetInteger(prefs::kCookieBehavior); |
| 505 return; | 510 prefs_->ClearPref(prefs::kCookieBehavior); |
| 506 pref_change_registrar_.RemoveAll(); | 511 if (!prefs_->HasPrefPath(prefs::kDefaultContentSettings)) { |
| 507 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
| 508 Source<Profile>(profile_)); | |
| 509 profile_ = NULL; | |
| 510 } | |
| 511 | |
| 512 void HostContentSettingsMap::MigrateObsoleteCookiePref(PrefService* prefs) { | |
| 513 if (prefs->HasPrefPath(prefs::kCookieBehavior)) { | |
| 514 int cookie_behavior = prefs->GetInteger(prefs::kCookieBehavior); | |
| 515 prefs->ClearPref(prefs::kCookieBehavior); | |
| 516 if (!prefs->HasPrefPath(prefs::kDefaultContentSettings)) { | |
| 517 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 512 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
| 518 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? | 513 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? |
| 519 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); | 514 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); |
| 520 } | 515 } |
| 521 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { | 516 if (!prefs_->HasPrefPath(prefs::kBlockThirdPartyCookies)) { |
| 522 SetBlockThirdPartyCookies(cookie_behavior == | 517 SetBlockThirdPartyCookies(cookie_behavior == |
| 523 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | 518 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); |
| 524 } | 519 } |
| 525 } | 520 } |
| 526 } | 521 } |
| OLD | NEW |