Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: chrome/browser/content_settings/host_content_settings_map.cc

Issue 7713034: HostContentSettingsMap refactoring. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Keeping up to date with trunk. Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/content_settings/content_settings_details.h" 10 #include "chrome/browser/content_settings/content_settings_details.h"
13 #include "chrome/browser/content_settings/content_settings_extension_provider.h" 11 #include "chrome/browser/content_settings/content_settings_extension_provider.h"
14 #include "chrome/browser/content_settings/content_settings_observable_provider.h " 12 #include "chrome/browser/content_settings/content_settings_observable_provider.h "
15 #include "chrome/browser/content_settings/content_settings_policy_provider.h" 13 #include "chrome/browser/content_settings/content_settings_policy_provider.h"
16 #include "chrome/browser/content_settings/content_settings_pref_provider.h" 14 #include "chrome/browser/content_settings/content_settings_pref_provider.h"
17 #include "chrome/browser/content_settings/content_settings_provider.h" 15 #include "chrome/browser/content_settings/content_settings_provider.h"
18 #include "chrome/browser/content_settings/content_settings_utils.h" 16 #include "chrome/browser/content_settings/content_settings_utils.h"
19 #include "chrome/browser/extensions/extension_service.h" 17 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/prefs/scoped_user_pref_update.h" 19 #include "chrome/browser/prefs/scoped_user_pref_update.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
24 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
27 #include "content/browser/browser_thread.h" 24 #include "content/browser/browser_thread.h"
28 #include "content/browser/user_metrics.h" 25 #include "content/browser/user_metrics.h"
29 #include "content/common/content_switches.h" 26 #include "content/common/content_switches.h"
30 #include "content/common/notification_service.h" 27 #include "content/common/notification_service.h"
31 #include "content/common/notification_source.h" 28 #include "content/common/notification_source.h"
32 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
33 #include "net/base/net_errors.h" 30 #include "net/base/net_errors.h"
34 #include "net/base/net_util.h" 31 #include "net/base/net_util.h"
35 #include "net/base/static_cookie_policy.h" 32 #include "net/base/static_cookie_policy.h"
36 33
37 namespace { 34 namespace {
38 35
39 // Returns true if we should allow all content types for this URL. This is
40 // true for various internal objects like chrome:// URLs, so UI and other
41 // things users think of as "not webpages" don't break.
42 static bool ShouldAllowAllContent(const GURL& url,
43 ContentSettingsType content_type) {
44 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
45 return false;
46 return url.SchemeIs(chrome::kChromeDevToolsScheme) ||
47 url.SchemeIs(chrome::kChromeInternalScheme) ||
48 url.SchemeIs(chrome::kChromeUIScheme) ||
49 url.SchemeIs(chrome::kExtensionScheme);
50 }
51
52 typedef linked_ptr<content_settings::DefaultProviderInterface> 36 typedef linked_ptr<content_settings::DefaultProviderInterface>
53 DefaultContentSettingsProviderPtr; 37 DefaultContentSettingsProviderPtr;
54 typedef std::vector<DefaultContentSettingsProviderPtr>::iterator 38 typedef std::vector<DefaultContentSettingsProviderPtr>::iterator
55 DefaultProviderIterator; 39 DefaultProviderIterator;
56 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator 40 typedef std::vector<DefaultContentSettingsProviderPtr>::const_iterator
57 ConstDefaultProviderIterator; 41 ConstDefaultProviderIterator;
58 42
59 typedef linked_ptr<content_settings::ProviderInterface> ProviderPtr; 43 typedef linked_ptr<content_settings::ProviderInterface> ProviderPtr;
60 typedef std::vector<ProviderPtr>::iterator ProviderIterator; 44 typedef std::vector<ProviderPtr>::iterator ProviderIterator;
61 typedef std::vector<ProviderPtr>::const_iterator ConstProviderIterator; 45 typedef std::vector<ProviderPtr>::const_iterator ConstProviderIterator;
(...skipping 15 matching lines...) Expand all
77 return type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE; 61 return type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE;
78 } 62 }
79 63
80 } // namespace 64 } // namespace
81 65
82 HostContentSettingsMap::HostContentSettingsMap( 66 HostContentSettingsMap::HostContentSettingsMap(
83 PrefService* prefs, 67 PrefService* prefs,
84 ExtensionService* extension_service, 68 ExtensionService* extension_service,
85 bool incognito) 69 bool incognito)
86 : prefs_(prefs), 70 : prefs_(prefs),
87 is_off_the_record_(incognito), 71 is_off_the_record_(incognito) {
88 updating_preferences_(false),
89 block_third_party_cookies_(false),
90 is_block_third_party_cookies_managed_(false) {
91 // The order in which the default content settings providers are created is 72 // The order in which the default content settings providers are created is
92 // critical, as providers that are further down in the list (i.e. added later) 73 // critical, as providers that are further down in the list (i.e. added later)
93 // override providers further up. 74 // override providers further up.
94 content_settings::PrefDefaultProvider* default_provider = 75 content_settings::PrefDefaultProvider* default_provider =
95 new content_settings::PrefDefaultProvider(prefs_, is_off_the_record_); 76 new content_settings::PrefDefaultProvider(prefs_, is_off_the_record_);
96 default_provider->AddObserver(this); 77 default_provider->AddObserver(this);
97 default_content_settings_providers_.push_back( 78 default_content_settings_providers_.push_back(
98 make_linked_ptr(default_provider)); 79 make_linked_ptr(default_provider));
99 80
100 content_settings::PolicyDefaultProvider* policy_default_provider = 81 content_settings::PolicyDefaultProvider* policy_default_provider =
101 new content_settings::PolicyDefaultProvider(prefs_); 82 new content_settings::PolicyDefaultProvider(prefs_);
102 policy_default_provider->AddObserver(this); 83 policy_default_provider->AddObserver(this);
103 default_content_settings_providers_.push_back( 84 default_content_settings_providers_.push_back(
104 make_linked_ptr(policy_default_provider)); 85 make_linked_ptr(policy_default_provider));
105 86
106 // TODO(markusheintz): Discuss whether it is sensible to move migration code
107 // to PrefContentSettingsProvider.
108 MigrateObsoleteCookiePref();
109
110 // Read misc. global settings. 87 // Read misc. global settings.
111 block_third_party_cookies_ =
112 prefs_->GetBoolean(prefs::kBlockThirdPartyCookies);
113 if (block_third_party_cookies_) {
114 UserMetrics::RecordAction(
115 UserMetricsAction("ThirdPartyCookieBlockingEnabled"));
116 } else {
117 UserMetrics::RecordAction(
118 UserMetricsAction("ThirdPartyCookieBlockingDisabled"));
119 }
120 is_block_third_party_cookies_managed_ =
121 prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies);
122 88
123 // User defined non default content settings are provided by the PrefProvider. 89 // User defined non default content settings are provided by the PrefProvider.
124 // The order in which the content settings providers are created is critical, 90 // The order in which the content settings providers are created is critical,
125 // as providers that are further up in the list (i.e. added earlier) override 91 // as providers that are further up in the list (i.e. added earlier) override
126 // providers further down. 92 // providers further down.
127 content_settings::ObservableProvider* provider = 93 content_settings::ObservableProvider* provider =
128 new content_settings::PolicyProvider(prefs_, policy_default_provider); 94 new content_settings::PolicyProvider(prefs_, policy_default_provider);
129 provider->AddObserver(this); 95 provider->AddObserver(this);
130 content_settings_providers_.push_back(make_linked_ptr(provider)); 96 content_settings_providers_.push_back(make_linked_ptr(provider));
131 97
132 if (extension_service) { 98 if (extension_service) {
133 // |extension_service| can be NULL in unit tests. 99 // |extension_service| can be NULL in unit tests.
134 provider = new content_settings::ExtensionProvider( 100 provider = new content_settings::ExtensionProvider(
135 extension_service->GetExtensionContentSettingsStore(), 101 extension_service->GetExtensionContentSettingsStore(),
136 is_off_the_record_); 102 is_off_the_record_);
137 provider->AddObserver(this); 103 provider->AddObserver(this);
138 content_settings_providers_.push_back(make_linked_ptr(provider)); 104 content_settings_providers_.push_back(make_linked_ptr(provider));
139 } 105 }
140 provider = new content_settings::PrefProvider(prefs_, is_off_the_record_); 106 provider = new content_settings::PrefProvider(prefs_, is_off_the_record_);
141 provider->AddObserver(this); 107 provider->AddObserver(this);
142 content_settings_providers_.push_back(make_linked_ptr(provider)); 108 content_settings_providers_.push_back(make_linked_ptr(provider));
143
144 pref_change_registrar_.Init(prefs_);
145 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
146 } 109 }
147 110
148 // static 111 // static
149 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { 112 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
150 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, 113 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies,
151 false, 114 false,
152 PrefService::SYNCABLE_PREF); 115 PrefService::SYNCABLE_PREF);
153 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 116 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex,
154 0, 117 0,
155 PrefService::UNSYNCABLE_PREF); 118 PrefService::UNSYNCABLE_PREF);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 150
188 ContentSettings HostContentSettingsMap::GetDefaultContentSettings() const { 151 ContentSettings HostContentSettingsMap::GetDefaultContentSettings() const {
189 ContentSettings output(CONTENT_SETTING_DEFAULT); 152 ContentSettings output(CONTENT_SETTING_DEFAULT);
190 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 153 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
191 if (!ContentTypeHasCompoundValue(ContentSettingsType(i))) 154 if (!ContentTypeHasCompoundValue(ContentSettingsType(i)))
192 output.settings[i] = GetDefaultContentSetting(ContentSettingsType(i)); 155 output.settings[i] = GetDefaultContentSetting(ContentSettingsType(i));
193 } 156 }
194 return output; 157 return output;
195 } 158 }
196 159
197 ContentSetting HostContentSettingsMap::GetCookieContentSetting(
198 const GURL& url,
199 const GURL& first_party_url,
200 bool setting_cookie) const {
201 if (ShouldAllowAllContent(first_party_url, CONTENT_SETTINGS_TYPE_COOKIES))
202 return CONTENT_SETTING_ALLOW;
203
204 // First get any host-specific settings.
205 ContentSetting setting = GetNonDefaultContentSetting(url,
206 first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
207
208 // If no explicit exception has been made and third-party cookies are blocked
209 // by default, apply that rule.
210 if (setting == CONTENT_SETTING_DEFAULT && BlockThirdPartyCookies()) {
211 bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kBlockReadingThirdPartyCookies);
213 net::StaticCookiePolicy policy(strict ?
214 net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
215 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
216 int rv;
217 if (setting_cookie)
218 rv = policy.CanSetCookie(url, first_party_url);
219 else
220 rv = policy.CanGetCookies(url, first_party_url);
221 DCHECK_NE(net::ERR_IO_PENDING, rv);
222 if (rv != net::OK)
223 setting = CONTENT_SETTING_BLOCK;
224 }
225
226 // If no other policy has changed the setting, use the default.
227 if (setting == CONTENT_SETTING_DEFAULT)
228 setting = GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES);
229
230 return setting;
231 }
232
233 ContentSetting HostContentSettingsMap::GetContentSetting( 160 ContentSetting HostContentSettingsMap::GetContentSetting(
234 const GURL& primary_url, 161 const GURL& primary_url,
235 const GURL& secondary_url, 162 const GURL& secondary_url,
236 ContentSettingsType content_type, 163 ContentSettingsType content_type,
237 const std::string& resource_identifier) const { 164 const std::string& resource_identifier) const {
238 DCHECK_NE(CONTENT_SETTINGS_TYPE_COOKIES, content_type); 165 DCHECK_NE(CONTENT_SETTINGS_TYPE_COOKIES, content_type);
239 DCHECK_NE(content_settings::RequiresResourceIdentifier(content_type), 166 DCHECK_NE(content_settings::RequiresResourceIdentifier(content_type),
240 resource_identifier.empty()); 167 resource_identifier.empty());
241 return GetContentSettingInternal( 168 return GetContentSettingInternal(
242 primary_url, secondary_url, content_type, resource_identifier); 169 primary_url, secondary_url, content_type, resource_identifier);
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 switches::kEnableWebIntents)) 388 switches::kEnableWebIntents))
462 return false; 389 return false;
463 390
464 // DEFAULT, ALLOW and BLOCK are always allowed. 391 // DEFAULT, ALLOW and BLOCK are always allowed.
465 if (setting == CONTENT_SETTING_DEFAULT || 392 if (setting == CONTENT_SETTING_DEFAULT ||
466 setting == CONTENT_SETTING_ALLOW || 393 setting == CONTENT_SETTING_ALLOW ||
467 setting == CONTENT_SETTING_BLOCK) { 394 setting == CONTENT_SETTING_BLOCK) {
468 return true; 395 return true;
469 } 396 }
470 switch (content_type) { 397 switch (content_type) {
471 case CONTENT_SETTINGS_TYPE_COOKIES:
472 return (setting == CONTENT_SETTING_SESSION_ONLY);
473 case CONTENT_SETTINGS_TYPE_PLUGINS: 398 case CONTENT_SETTINGS_TYPE_PLUGINS:
474 return (setting == CONTENT_SETTING_ASK && 399 return (setting == CONTENT_SETTING_ASK &&
475 CommandLine::ForCurrentProcess()->HasSwitch( 400 CommandLine::ForCurrentProcess()->HasSwitch(
476 switches::kEnableClickToPlay)); 401 switches::kEnableClickToPlay));
477 case CONTENT_SETTINGS_TYPE_GEOLOCATION: 402 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
478 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: 403 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
479 case CONTENT_SETTINGS_TYPE_INTENTS: 404 case CONTENT_SETTINGS_TYPE_INTENTS:
480 return (setting == CONTENT_SETTING_ASK); 405 return (setting == CONTENT_SETTING_ASK);
481 default: 406 default:
482 return false; 407 return false;
483 } 408 }
484 } 409 }
485 410
486 void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) {
487 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
488 DCHECK(prefs_);
489
490 // This setting may not be directly modified for OTR sessions. Instead, it
491 // is synced to the main profile's setting.
492 if (is_off_the_record_) {
493 NOTREACHED();
494 return;
495 }
496
497 // If the preference block-third-party-cookies is managed then do not allow to
498 // change it.
499 if (prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies)) {
500 NOTREACHED();
501 return;
502 }
503
504 {
505 base::AutoLock auto_lock(lock_);
506 block_third_party_cookies_ = block;
507 }
508
509 prefs_->SetBoolean(prefs::kBlockThirdPartyCookies, block);
510 }
511
512 void HostContentSettingsMap::OnContentSettingChanged( 411 void HostContentSettingsMap::OnContentSettingChanged(
513 ContentSettingsPattern primary_pattern, 412 ContentSettingsPattern primary_pattern,
514 ContentSettingsPattern secondary_pattern, 413 ContentSettingsPattern secondary_pattern,
515 ContentSettingsType content_type, 414 ContentSettingsType content_type,
516 std::string resource_identifier) { 415 std::string resource_identifier) {
517 const ContentSettingsDetails details(primary_pattern, 416 const ContentSettingsDetails details(primary_pattern,
518 secondary_pattern, 417 secondary_pattern,
519 content_type, 418 content_type,
520 resource_identifier); 419 resource_identifier);
521 NotificationService::current()->Notify( 420 NotificationService::current()->Notify(
522 chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED, 421 chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED,
523 Source<HostContentSettingsMap>(this), 422 Source<HostContentSettingsMap>(this),
524 Details<const ContentSettingsDetails>(&details)); 423 Details<const ContentSettingsDetails>(&details));
525 } 424 }
526 425
527 void HostContentSettingsMap::Observe(int type, 426 // static
528 const NotificationSource& source, 427 // Returns true if we should allow all content types for this URL. This is
529 const NotificationDetails& details) { 428 // true for various internal objects like chrome:// URLs, so UI and other
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 429 // things users think of as "not webpages" don't break.
531 430 bool HostContentSettingsMap::ShouldAllowAllContent(
532 if (type == chrome::NOTIFICATION_PREF_CHANGED) { 431 const GURL& url,
533 DCHECK_EQ(prefs_, Source<PrefService>(source).ptr()); 432 ContentSettingsType content_type) {
534 if (updating_preferences_) 433 if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
535 return; 434 return false;
536 435 return url.SchemeIs(chrome::kChromeDevToolsScheme) ||
537 std::string* name = Details<std::string>(details).ptr(); 436 url.SchemeIs(chrome::kChromeInternalScheme) ||
538 if (*name == prefs::kBlockThirdPartyCookies) { 437 url.SchemeIs(chrome::kChromeUIScheme) ||
539 base::AutoLock auto_lock(lock_); 438 url.SchemeIs(chrome::kExtensionScheme);
540 block_third_party_cookies_ = prefs_->GetBoolean(
541 prefs::kBlockThirdPartyCookies);
542 is_block_third_party_cookies_managed_ =
543 prefs_->IsManagedPreference(
544 prefs::kBlockThirdPartyCookies);
545 } else {
546 NOTREACHED() << "Unexpected preference observed";
547 return;
548 }
549 } else {
550 NOTREACHED() << "Unexpected notification";
551 }
552 } 439 }
553 440
554 HostContentSettingsMap::~HostContentSettingsMap() { 441 HostContentSettingsMap::~HostContentSettingsMap() {
555 DCHECK(!prefs_); 442 DCHECK(!prefs_);
556 } 443 }
557 444
558 bool HostContentSettingsMap::IsDefaultContentSettingManaged( 445 bool HostContentSettingsMap::IsDefaultContentSettingManaged(
559 ContentSettingsType content_type) const { 446 ContentSettingsType content_type) const {
560 for (ConstDefaultProviderIterator provider = 447 for (ConstDefaultProviderIterator provider =
561 default_content_settings_providers_.begin(); 448 default_content_settings_providers_.begin();
562 provider != default_content_settings_providers_.end(); ++provider) { 449 provider != default_content_settings_providers_.end(); ++provider) {
563 if ((*provider)->DefaultSettingIsManaged(content_type)) 450 if ((*provider)->DefaultSettingIsManaged(content_type))
564 return true; 451 return true;
565 } 452 }
566 return false; 453 return false;
567 } 454 }
568 455
569 void HostContentSettingsMap::ShutdownOnUIThread() { 456 void HostContentSettingsMap::ShutdownOnUIThread() {
570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
571 DCHECK(prefs_); 458 DCHECK(prefs_);
572 pref_change_registrar_.RemoveAll(); 459
573 prefs_ = NULL; 460 prefs_ = NULL;
574 for (ProviderIterator it = content_settings_providers_.begin(); 461 for (ProviderIterator it = content_settings_providers_.begin();
575 it != content_settings_providers_.end(); 462 it != content_settings_providers_.end();
576 ++it) { 463 ++it) {
577 (*it)->ShutdownOnUIThread(); 464 (*it)->ShutdownOnUIThread();
578 } 465 }
579 for (DefaultProviderIterator it = default_content_settings_providers_.begin(); 466 for (DefaultProviderIterator it = default_content_settings_providers_.begin();
580 it != default_content_settings_providers_.end(); 467 it != default_content_settings_providers_.end();
581 ++it) { 468 ++it) {
582 (*it)->ShutdownOnUIThread(); 469 (*it)->ShutdownOnUIThread();
583 } 470 }
584 } 471 }
585
586 void HostContentSettingsMap::MigrateObsoleteCookiePref() {
587 if (prefs_->HasPrefPath(prefs::kCookieBehavior)) {
588 int cookie_behavior = prefs_->GetInteger(prefs::kCookieBehavior);
589 prefs_->ClearPref(prefs::kCookieBehavior);
590 if (!prefs_->HasPrefPath(prefs::kDefaultContentSettings)) {
591 SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
592 (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ?
593 CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW);
594 }
595 if (!prefs_->HasPrefPath(prefs::kBlockThirdPartyCookies)) {
596 SetBlockThirdPartyCookies(cookie_behavior ==
597 net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
598 }
599 }
600 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698