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