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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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) |
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::ResetToDefaults() { | 448 void HostContentSettingsMap::ResetToDefaults() { |
450 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 449 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
451 | 450 |
452 { | 451 { |
453 base::AutoLock auto_lock(lock_); | 452 base::AutoLock auto_lock(lock_); |
454 for (DefaultProviderIterator provider = | 453 for (DefaultProviderIterator provider = |
455 default_content_settings_providers_.begin(); | 454 default_content_settings_providers_.begin(); |
456 provider != default_content_settings_providers_.end(); ++provider) { | 455 provider != default_content_settings_providers_.end(); ++provider) { |
457 (*provider)->ResetToDefaults(); | 456 (*provider)->ResetToDefaults(); |
458 } | 457 } |
459 | 458 |
460 for (ProviderIterator provider = content_settings_providers_.begin(); | 459 for (ProviderIterator provider = content_settings_providers_.begin(); |
461 provider != content_settings_providers_.end(); | 460 provider != content_settings_providers_.end(); |
462 ++provider) { | 461 ++provider) { |
463 (*provider)->ResetToDefaults(); | 462 (*provider)->ResetToDefaults(); |
464 } | 463 } |
465 | 464 |
466 // Don't reset block third party cookies if they are managed. | 465 // Don't reset block third party cookies if they are managed. |
467 if (!IsBlockThirdPartyCookiesManaged()) | 466 if (!IsBlockThirdPartyCookiesManaged()) |
468 block_third_party_cookies_ = false; | 467 block_third_party_cookies_ = false; |
469 } | 468 } |
470 | 469 |
471 if (!is_off_the_record_) { | 470 if (!is_off_the_record_) { |
472 PrefService* prefs = profile_->GetPrefs(); | 471 DCHECK(prefs_); |
473 updating_preferences_ = true; | 472 updating_preferences_ = true; |
474 // If the block third party cookies preference is managed we still must | 473 // If the block third party cookies preference is managed we still must |
475 // clear it in order to restore the default value for later when the | 474 // clear it in order to restore the default value for later when the |
476 // preference is not managed anymore. | 475 // preference is not managed anymore. |
477 prefs->ClearPref(prefs::kBlockThirdPartyCookies); | 476 prefs_->ClearPref(prefs::kBlockThirdPartyCookies); |
478 updating_preferences_ = false; | 477 updating_preferences_ = false; |
479 } | 478 } |
480 } | 479 } |
481 | 480 |
482 void HostContentSettingsMap::Observe(NotificationType type, | 481 void HostContentSettingsMap::Observe(NotificationType type, |
483 const NotificationSource& source, | 482 const NotificationSource& source, |
484 const NotificationDetails& details) { | 483 const NotificationDetails& details) { |
485 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
486 | 485 |
487 if (type == NotificationType::PREF_CHANGED) { | 486 if (type == NotificationType::PREF_CHANGED) { |
488 DCHECK_EQ(profile_->GetPrefs(), Source<PrefService>(source).ptr()); | 487 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); |
489 if (updating_preferences_) | 488 if (updating_preferences_) |
490 return; | 489 return; |
491 | 490 |
492 std::string* name = Details<std::string>(details).ptr(); | 491 std::string* name = Details<std::string>(details).ptr(); |
493 if (*name == prefs::kBlockThirdPartyCookies) { | 492 if (*name == prefs::kBlockThirdPartyCookies) { |
494 base::AutoLock auto_lock(lock_); | 493 base::AutoLock auto_lock(lock_); |
495 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( | 494 block_third_party_cookies_ = prefs_->GetBoolean( |
496 prefs::kBlockThirdPartyCookies); | 495 prefs::kBlockThirdPartyCookies); |
497 is_block_third_party_cookies_managed_ = | 496 is_block_third_party_cookies_managed_ = |
498 profile_->GetPrefs()->IsManagedPreference( | 497 prefs_->IsManagedPreference( |
499 prefs::kBlockThirdPartyCookies); | 498 prefs::kBlockThirdPartyCookies); |
500 } else { | 499 } else { |
501 NOTREACHED() << "Unexpected preference observed"; | 500 NOTREACHED() << "Unexpected preference observed"; |
502 return; | 501 return; |
503 } | 502 } |
504 } else if (type == NotificationType::PROFILE_DESTROYED) { | |
505 DCHECK_EQ(profile_, Source<Profile>(source).ptr()); | |
506 UnregisterObservers(); | |
507 } else { | 503 } else { |
508 NOTREACHED() << "Unexpected notification"; | 504 NOTREACHED() << "Unexpected notification"; |
509 } | 505 } |
510 } | 506 } |
511 | 507 |
512 HostContentSettingsMap::~HostContentSettingsMap() { | 508 HostContentSettingsMap::~HostContentSettingsMap() { |
513 UnregisterObservers(); | 509 DCHECK(!prefs_); |
514 } | 510 } |
515 | 511 |
516 bool HostContentSettingsMap::IsDefaultContentSettingManaged( | 512 bool HostContentSettingsMap::IsDefaultContentSettingManaged( |
517 ContentSettingsType content_type) const { | 513 ContentSettingsType content_type) const { |
518 for (ConstDefaultProviderIterator provider = | 514 for (ConstDefaultProviderIterator provider = |
519 default_content_settings_providers_.begin(); | 515 default_content_settings_providers_.begin(); |
520 provider != default_content_settings_providers_.end(); ++provider) { | 516 provider != default_content_settings_providers_.end(); ++provider) { |
521 if ((*provider)->DefaultSettingIsManaged(content_type)) | 517 if ((*provider)->DefaultSettingIsManaged(content_type)) |
522 return true; | 518 return true; |
523 } | 519 } |
524 return false; | 520 return false; |
525 } | 521 } |
526 | 522 |
527 void HostContentSettingsMap::ShutdownOnUIThread() { | 523 void HostContentSettingsMap::ShutdownOnUIThread() { |
| 524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 525 if (!prefs_) |
| 526 return; |
| 527 pref_change_registrar_.RemoveAll(); |
| 528 prefs_ = NULL; |
528 for (ProviderIterator it = content_settings_providers_.begin(); | 529 for (ProviderIterator it = content_settings_providers_.begin(); |
529 it != content_settings_providers_.end(); | 530 it != content_settings_providers_.end(); |
530 ++it) { | 531 ++it) { |
531 (*it)->ShutdownOnUIThread(); | 532 (*it)->ShutdownOnUIThread(); |
532 } | 533 } |
| 534 for (DefaultProviderIterator it = default_content_settings_providers_.begin(); |
| 535 it != default_content_settings_providers_.end(); |
| 536 ++it) { |
| 537 (*it)->ShutdownOnUIThread(); |
| 538 } |
533 } | 539 } |
534 | 540 |
535 void HostContentSettingsMap::UnregisterObservers() { | 541 void HostContentSettingsMap::MigrateObsoleteCookiePref() { |
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 542 if (prefs_->HasPrefPath(prefs::kCookieBehavior)) { |
537 if (!profile_) | 543 int cookie_behavior = prefs_->GetInteger(prefs::kCookieBehavior); |
538 return; | 544 prefs_->ClearPref(prefs::kCookieBehavior); |
539 pref_change_registrar_.RemoveAll(); | 545 if (!prefs_->HasPrefPath(prefs::kDefaultContentSettings)) { |
540 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | |
541 Source<Profile>(profile_)); | |
542 profile_ = NULL; | |
543 } | |
544 | |
545 void HostContentSettingsMap::MigrateObsoleteCookiePref(PrefService* prefs) { | |
546 if (prefs->HasPrefPath(prefs::kCookieBehavior)) { | |
547 int cookie_behavior = prefs->GetInteger(prefs::kCookieBehavior); | |
548 prefs->ClearPref(prefs::kCookieBehavior); | |
549 if (!prefs->HasPrefPath(prefs::kDefaultContentSettings)) { | |
550 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | 546 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, |
551 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? | 547 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? |
552 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); | 548 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); |
553 } | 549 } |
554 if (!prefs->HasPrefPath(prefs::kBlockThirdPartyCookies)) { | 550 if (!prefs_->HasPrefPath(prefs::kBlockThirdPartyCookies)) { |
555 SetBlockThirdPartyCookies(cookie_behavior == | 551 SetBlockThirdPartyCookies(cookie_behavior == |
556 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); | 552 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); |
557 } | 553 } |
558 } | 554 } |
559 } | 555 } |
OLD | NEW |