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

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

Issue 5528010: Implement preference and policy based content settings providers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/chrome/browser/content_settings
Patch Set: updates Created 10 years 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) 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/content_settings/content_settings_details.h"
12 #include "chrome/browser/content_settings/policy_content_settings_provider.h"
13 #include "chrome/browser/content_settings/pref_content_settings_provider.h"
12 #include "chrome/browser/metrics/user_metrics.h" 14 #include "chrome/browser/metrics/user_metrics.h"
13 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
14 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/prefs/scoped_pref_update.h" 17 #include "chrome/browser/prefs/scoped_pref_update.h"
16 #include "chrome/common/notification_service.h" 18 #include "chrome/common/notification_service.h"
17 #include "chrome/common/notification_source.h" 19 #include "chrome/common/notification_source.h"
18 #include "chrome/common/notification_type.h" 20 #include "chrome/common/notification_type.h"
19 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
22 #include "googleurl/src/gurl.h" 24 #include "googleurl/src/gurl.h"
23 #include "googleurl/src/url_canon.h"
24 #include "googleurl/src/url_parse.h"
25 #include "net/base/dns_util.h"
26 #include "net/base/net_util.h" 25 #include "net/base/net_util.h"
27 #include "net/base/static_cookie_policy.h" 26 #include "net/base/static_cookie_policy.h"
28 27
29 namespace { 28 namespace {
30 29
31 // Base pref path of the prefs that contain the managed default content
32 // settings values.
33 const std::string kManagedSettings =
34 "profile.managed_default_content_settings";
35
36 // The preference keys where resource identifiers are stored for 30 // The preference keys where resource identifiers are stored for
37 // ContentSettingsType values that support resource identifiers. 31 // ContentSettingsType values that support resource identifiers.
38 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { 32 const char* kResourceTypeNames[CONTENT_SETTINGS_NUM_TYPES] = {
39 NULL, 33 NULL,
40 NULL, 34 NULL,
41 NULL, 35 NULL,
42 "per_plugin", 36 "per_plugin",
43 NULL, 37 NULL,
44 NULL, // Not used for Geolocation 38 NULL, // Not used for Geolocation
45 NULL, // Not used for Notifications 39 NULL, // Not used for Notifications
46 }; 40 };
47 41
48 // The names of the ContentSettingsType values, for use with dictionary prefs. 42 // The names of the ContentSettingsType values, for use with dictionary prefs.
49 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = { 43 const char* kTypeNames[CONTENT_SETTINGS_NUM_TYPES] = {
50 "cookies", 44 "cookies",
51 "images", 45 "images",
52 "javascript", 46 "javascript",
53 "plugins", 47 "plugins",
54 "popups", 48 "popups",
55 NULL, // Not used for Geolocation 49 NULL, // Not used for Geolocation
56 NULL, // Not used for Notifications 50 NULL, // Not used for Notifications
57 }; 51 };
58 52
59 // The preferences used to manage ContentSettingsTypes.
60 const char* kPrefToManageType[CONTENT_SETTINGS_NUM_TYPES] = {
61 prefs::kManagedDefaultCookiesSetting,
62 prefs::kManagedDefaultImagesSetting,
63 prefs::kManagedDefaultJavaScriptSetting,
64 prefs::kManagedDefaultPluginsSetting,
65 prefs::kManagedDefaultPopupsSetting,
66 NULL, // Not used for Geolocation
67 NULL, // Not used for Notifications
68 };
69
70 // The default setting for each content type.
71 const ContentSetting kDefaultSettings[CONTENT_SETTINGS_NUM_TYPES] = {
72 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
73 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
74 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
75 CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
76 CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
77 CONTENT_SETTING_ASK, // Not used for Geolocation
78 CONTENT_SETTING_ASK, // Not used for Notifications
79 };
80
81 // True if a given content settings type requires additional resource 53 // True if a given content settings type requires additional resource
82 // identifiers. 54 // identifiers.
83 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { 55 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = {
84 false, // CONTENT_SETTINGS_TYPE_COOKIES 56 false, // CONTENT_SETTINGS_TYPE_COOKIES
85 false, // CONTENT_SETTINGS_TYPE_IMAGES 57 false, // CONTENT_SETTINGS_TYPE_IMAGES
86 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT 58 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
87 true, // CONTENT_SETTINGS_TYPE_PLUGINS 59 true, // CONTENT_SETTINGS_TYPE_PLUGINS
88 false, // CONTENT_SETTINGS_TYPE_POPUPS 60 false, // CONTENT_SETTINGS_TYPE_POPUPS
89 false, // Not used for Geolocation 61 false, // Not used for Geolocation
90 false, // Not used for Notifications 62 false, // Not used for Notifications
(...skipping 27 matching lines...) Expand all
118 } // namespace 90 } // namespace
119 91
120 92
121 struct HostContentSettingsMap::ExtendedContentSettings { 93 struct HostContentSettingsMap::ExtendedContentSettings {
122 ContentSettings content_settings; 94 ContentSettings content_settings;
123 ResourceContentSettings content_settings_for_resources; 95 ResourceContentSettings content_settings_for_resources;
124 }; 96 };
125 97
126 HostContentSettingsMap::HostContentSettingsMap(Profile* profile) 98 HostContentSettingsMap::HostContentSettingsMap(Profile* profile)
127 : profile_(profile), 99 : profile_(profile),
100 is_off_the_record_(profile_->IsOffTheRecord()),
101 updating_preferences_(false),
128 block_third_party_cookies_(false), 102 block_third_party_cookies_(false),
129 is_block_third_party_cookies_managed_(false), 103 is_block_third_party_cookies_managed_(false) {
130 is_off_the_record_(profile_->IsOffTheRecord()), 104 // The order in which the content settings providers are created is critical,
131 updating_preferences_(false) { 105 // as providers that are further down in the list (i.e. added later) override
106 // providers further up.
107 content_settings_providers_.push_back(
108 linked_ptr<ContentSettingsProviderInterface>(
109 new PrefContentSettingsProvider(profile)));
110 content_settings_providers_.push_back(
111 linked_ptr<ContentSettingsProviderInterface>(
112 new PolicyContentSettingsProvider(profile)));
113
132 PrefService* prefs = profile_->GetPrefs(); 114 PrefService* prefs = profile_->GetPrefs();
133 115
134 MigrateObsoleteCookiePref(prefs); 116 MigrateObsoleteCookiePref(prefs);
135 117
136 MigrateObsoletePopupsPref(prefs); 118 MigrateObsoletePopupsPref(prefs);
137 119
138 MigrateObsoletePerhostPref(prefs); 120 MigrateObsoletePerhostPref(prefs);
139 121
140 // Read global defaults.
141 DCHECK_EQ(arraysize(kTypeNames),
142 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
143 ReadDefaultSettings(false);
144
145 // Read misc. global settings. 122 // Read misc. global settings.
146 block_third_party_cookies_ = 123 block_third_party_cookies_ =
147 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); 124 prefs->GetBoolean(prefs::kBlockThirdPartyCookies);
148 is_block_third_party_cookies_managed_ = 125 is_block_third_party_cookies_managed_ =
149 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); 126 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies);
150 block_nonsandboxed_plugins_ = 127 block_nonsandboxed_plugins_ =
151 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); 128 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins);
152 129
153 // Verify preferences version. 130 // Verify preferences version.
154 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { 131 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) {
155 prefs->SetInteger(prefs::kContentSettingsVersion, 132 prefs->SetInteger(prefs::kContentSettingsVersion,
156 ContentSettingsPattern::kContentSettingsPatternVersion); 133 ContentSettingsPattern::kContentSettingsPatternVersion);
157 } 134 }
158 if (prefs->GetInteger(prefs::kContentSettingsVersion) > 135 if (prefs->GetInteger(prefs::kContentSettingsVersion) >
159 ContentSettingsPattern::kContentSettingsPatternVersion) { 136 ContentSettingsPattern::kContentSettingsPatternVersion) {
160 LOG(ERROR) << "Unknown content settings version in preferences."; 137 LOG(ERROR) << "Unknown content settings version in preferences.";
161 return; 138 return;
162 } 139 }
163 140
164 // Read exceptions. 141 // Read exceptions.
165 ReadExceptions(false); 142 ReadExceptions(false);
166 143
167 pref_change_registrar_.Init(prefs); 144 pref_change_registrar_.Init(prefs);
168 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this);
169 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); 145 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
170 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); 146 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
171 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); 147 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this);
172 // The following preferences are only used to indicate if a
173 // default-content-setting is managed and to hold the managed default-setting
174 // value. If the value for any of the following perferences is set then the
175 // corresponding default-content-setting is managed. These preferences exist
176 // in parallel to the preference default-content-settings. If a
177 // default-content-settings-type is managed any user defined excpetions
178 // (patterns) for this type are ignored.
179 pref_change_registrar_.Add(prefs::kManagedDefaultCookiesSetting, this);
180 pref_change_registrar_.Add(prefs::kManagedDefaultImagesSetting, this);
181 pref_change_registrar_.Add(prefs::kManagedDefaultJavaScriptSetting, this);
182 pref_change_registrar_.Add(prefs::kManagedDefaultPluginsSetting, this);
183 pref_change_registrar_.Add(prefs::kManagedDefaultPopupsSetting, this);
184 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, 148 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
185 Source<Profile>(profile_)); 149 Source<Profile>(profile_));
186 } 150 }
187 151
188 // static 152 // static
189 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { 153 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
190 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings);
191 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, 154 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion,
192 ContentSettingsPattern::kContentSettingsPatternVersion); 155 ContentSettingsPattern::kContentSettingsPatternVersion);
193 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); 156 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns);
194 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); 157 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false);
195 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); 158 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false);
196 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); 159 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0);
197 160
198 // Preferences for default content setting policies. A policy is not set of
199 // the corresponding preferences below is set to CONTENT_SETTING_DEFAULT.
200 prefs->RegisterIntegerPref(prefs::kManagedDefaultCookiesSetting,
201 CONTENT_SETTING_DEFAULT);
202 prefs->RegisterIntegerPref(prefs::kManagedDefaultImagesSetting,
203 CONTENT_SETTING_DEFAULT);
204 prefs->RegisterIntegerPref(prefs::kManagedDefaultJavaScriptSetting,
205 CONTENT_SETTING_DEFAULT);
206 prefs->RegisterIntegerPref(prefs::kManagedDefaultPluginsSetting,
207 CONTENT_SETTING_DEFAULT);
208 prefs->RegisterIntegerPref(prefs::kManagedDefaultPopupsSetting,
209 CONTENT_SETTING_DEFAULT);
210
211 // Obsolete prefs, for migration: 161 // Obsolete prefs, for migration:
212 prefs->RegisterIntegerPref(prefs::kCookieBehavior, 162 prefs->RegisterIntegerPref(prefs::kCookieBehavior,
213 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); 163 net::StaticCookiePolicy::ALLOW_ALL_COOKIES);
214 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); 164 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts);
215 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); 165 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings);
216 } 166 }
217 167
218 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( 168 ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
219 ContentSettingsType content_type) const { 169 ContentSettingsType content_type) const {
220 AutoLock auto_lock(lock_); 170 typedef
221 if (IsDefaultContentSettingManaged(content_type)) 171 std::vector<linked_ptr<ContentSettingsProviderInterface> >::const_iterator
222 return managed_default_content_settings_.settings[content_type]; 172 provider_iterator;
223 return default_content_settings_.settings[content_type]; 173 ContentSetting setting = CONTENT_SETTING_DEFAULT;
174 for (provider_iterator provider = content_settings_providers_.begin();
175 provider != content_settings_providers_.end(); ++provider) {
176 if (!(*provider)->CanProvideDefaultSetting(content_type))
177 continue;
178 ContentSetting provided_setting =
179 (*provider)->ProvideDefaultSetting(content_type);
180 if (provided_setting != CONTENT_SETTING_DEFAULT)
181 setting = provided_setting;
182 }
183 CHECK_NE(CONTENT_SETTING_DEFAULT, setting);
gfeher 2010/12/09 10:29:53 What does it mean, when all the providers provide
jochen (gone - plz use gerrit) 2010/12/09 13:18:53 Added a comment
184 return setting;
224 } 185 }
225 186
226 ContentSetting HostContentSettingsMap::GetContentSetting( 187 ContentSetting HostContentSettingsMap::GetContentSetting(
227 const GURL& url, 188 const GURL& url,
228 ContentSettingsType content_type, 189 ContentSettingsType content_type,
229 const std::string& resource_identifier) const { 190 const std::string& resource_identifier) const {
230 ContentSetting setting = GetNonDefaultContentSetting(url, 191 ContentSetting setting = GetNonDefaultContentSetting(url,
231 content_type, 192 content_type,
232 resource_identifier); 193 resource_identifier);
233 if (setting == CONTENT_SETTING_DEFAULT || 194 if (setting == CONTENT_SETTING_DEFAULT ||
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); 272 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1);
312 } 273 }
313 274
314 return CONTENT_SETTING_DEFAULT; 275 return CONTENT_SETTING_DEFAULT;
315 } 276 }
316 277
317 ContentSettings HostContentSettingsMap::GetContentSettings( 278 ContentSettings HostContentSettingsMap::GetContentSettings(
318 const GURL& url) const { 279 const GURL& url) const {
319 ContentSettings output = GetNonDefaultContentSettings(url); 280 ContentSettings output = GetNonDefaultContentSettings(url);
320 281
321 AutoLock auto_lock(lock_);
322
323 // If we require a resource identifier, set the content settings to default, 282 // If we require a resource identifier, set the content settings to default,
324 // otherwise make the defaults explicit. 283 // otherwise make the defaults explicit.
325 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { 284 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
326 if (RequiresResourceIdentifier(ContentSettingsType(j))) { 285 if (RequiresResourceIdentifier(ContentSettingsType(j))) {
327 output.settings[j] = CONTENT_SETTING_DEFAULT; 286 output.settings[j] = CONTENT_SETTING_DEFAULT;
328 } else { 287 } else {
329 if (output.settings[j] == CONTENT_SETTING_DEFAULT) {
330 output.settings[j] = default_content_settings_.settings[j];
331 }
332 // A managed default content setting has the highest priority and hence 288 // A managed default content setting has the highest priority and hence
333 // will overwrite any previously set value. 289 // will overwrite any previously set value.
334 if (IsDefaultContentSettingManaged(ContentSettingsType(j))) { 290 if ((output.settings[j] == CONTENT_SETTING_DEFAULT) ||
335 output.settings[j] = 291 IsDefaultContentSettingManaged(ContentSettingsType(j))) {
336 managed_default_content_settings_.settings[j]; 292 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j));
337 } 293 }
338 } 294 }
339 } 295 }
340 return output; 296 return output;
341 } 297 }
342 298
343 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( 299 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings(
344 const GURL& url) const { 300 const GURL& url) const {
345 if (ShouldAllowAllContent(url)) 301 if (ShouldAllowAllContent(url))
346 return ContentSettings(CONTENT_SETTING_ALLOW); 302 return ContentSettings(CONTENT_SETTING_ALLOW);
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 390
435 void HostContentSettingsMap::SetDefaultContentSetting( 391 void HostContentSettingsMap::SetDefaultContentSetting(
436 ContentSettingsType content_type, 392 ContentSettingsType content_type,
437 ContentSetting setting) { 393 ContentSetting setting) {
438 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 394 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 395 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
440 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || 396 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS ||
441 setting != CONTENT_SETTING_ASK || 397 setting != CONTENT_SETTING_ASK ||
442 CommandLine::ForCurrentProcess()->HasSwitch( 398 CommandLine::ForCurrentProcess()->HasSwitch(
443 switches::kEnableClickToPlay)); 399 switches::kEnableClickToPlay));
444 PrefService* prefs = profile_->GetPrefs();
445 400
446 // The default settings may not be directly modified for OTR sessions. 401 // The default settings may not be directly modified for OTR sessions.
447 // Instead, they are synced to the main profile's setting. 402 // Instead, they are synced to the main profile's setting.
448 if (is_off_the_record_) { 403 if (is_off_the_record_) {
449 NOTREACHED(); 404 NOTREACHED();
450 return; 405 return;
451 } 406 }
452 407
453 DictionaryValue* default_settings_dictionary = 408 typedef std::vector<linked_ptr<ContentSettingsProviderInterface> >::iterator
454 prefs->GetMutableDictionary(prefs::kDefaultContentSettings); 409 provider_iterator;
455 std::string dictionary_path(kTypeNames[content_type]); 410 for (provider_iterator provider = content_settings_providers_.begin();
456 updating_preferences_ = true; 411 provider != content_settings_providers_.end(); ++provider) {
457 { 412 (*provider)->UpdateDefaultSetting(content_type, setting);
458 AutoLock auto_lock(lock_);
459 ScopedPrefUpdate update(prefs, prefs::kDefaultContentSettings);
460 if ((setting == CONTENT_SETTING_DEFAULT) ||
461 (setting == kDefaultSettings[content_type])) {
462 default_content_settings_.settings[content_type] =
463 kDefaultSettings[content_type];
464 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
465 NULL);
466 } else {
467 default_content_settings_.settings[content_type] = setting;
468 default_settings_dictionary->SetWithoutPathExpansion(
469 dictionary_path, Value::CreateIntegerValue(setting));
470 }
471 } 413 }
472 updating_preferences_ = false;
473
474 NotifyObservers(
475 ContentSettingsDetails(ContentSettingsPattern(), content_type, ""));
476 } 414 }
477 415
478 void HostContentSettingsMap::SetContentSetting( 416 void HostContentSettingsMap::SetContentSetting(
479 const ContentSettingsPattern& original_pattern, 417 const ContentSettingsPattern& original_pattern,
480 ContentSettingsType content_type, 418 ContentSettingsType content_type,
481 const std::string& resource_identifier, 419 const std::string& resource_identifier,
482 ContentSetting setting) { 420 ContentSetting setting) {
483 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 421 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
485 DCHECK_NE(RequiresResourceIdentifier(content_type), 423 DCHECK_NE(RequiresResourceIdentifier(content_type),
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); 658 UserMetricsAction("BlockNonsandboxedPlugins_Disable"));
721 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); 659 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins);
722 } 660 }
723 } 661 }
724 662
725 void HostContentSettingsMap::ResetToDefaults() { 663 void HostContentSettingsMap::ResetToDefaults() {
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
727 665
728 { 666 {
729 AutoLock auto_lock(lock_); 667 AutoLock auto_lock(lock_);
730 default_content_settings_ = ContentSettings(); 668 typedef std::vector<linked_ptr<ContentSettingsProviderInterface> >::iterator
731 ForceDefaultsToBeExplicit(); 669 provider_iterator;
732 // Clear all content settings map except the 670 for (provider_iterator provider = content_settings_providers_.begin();
733 // managed_default_content_settings. 671 provider != content_settings_providers_.end(); ++provider) {
672 (*provider)->ResetToDefaults();
673 }
734 host_content_settings_.clear(); 674 host_content_settings_.clear();
735 off_the_record_settings_.clear(); 675 off_the_record_settings_.clear();
736 // Don't reset block third party cookies if they are managed. 676 // Don't reset block third party cookies if they are managed.
737 if (!IsBlockThirdPartyCookiesManaged()) 677 if (!IsBlockThirdPartyCookiesManaged())
738 block_third_party_cookies_ = false; 678 block_third_party_cookies_ = false;
739 block_nonsandboxed_plugins_ = false; 679 block_nonsandboxed_plugins_ = false;
740 } 680 }
741 681
742 if (!is_off_the_record_) { 682 if (!is_off_the_record_) {
743 PrefService* prefs = profile_->GetPrefs(); 683 PrefService* prefs = profile_->GetPrefs();
744 updating_preferences_ = true; 684 updating_preferences_ = true;
745 prefs->ClearPref(prefs::kDefaultContentSettings);
746 prefs->ClearPref(prefs::kContentSettingsPatterns); 685 prefs->ClearPref(prefs::kContentSettingsPatterns);
747 // If the block third party cookies preference is managed we still must 686 // If the block third party cookies preference is managed we still must
748 // clear it in order to restore the default value for later when the 687 // clear it in order to restore the default value for later when the
749 // preference is not managed anymore. 688 // preference is not managed anymore.
750 prefs->ClearPref(prefs::kBlockThirdPartyCookies); 689 prefs->ClearPref(prefs::kBlockThirdPartyCookies);
751 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); 690 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins);
752 updating_preferences_ = false; 691 updating_preferences_ = false;
753 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), 692 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(),
754 CONTENT_SETTINGS_TYPE_DEFAULT, 693 CONTENT_SETTINGS_TYPE_DEFAULT,
755 "")); 694 ""));
756 } 695 }
757 } 696 }
758 697
759 void HostContentSettingsMap::Observe(NotificationType type, 698 void HostContentSettingsMap::Observe(NotificationType type,
760 const NotificationSource& source, 699 const NotificationSource& source,
761 const NotificationDetails& details) { 700 const NotificationDetails& details) {
762 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
763 702
764 if (NotificationType::PREF_CHANGED == type) { 703 if (NotificationType::PREF_CHANGED == type) {
765 if (updating_preferences_) 704 if (updating_preferences_)
766 return; 705 return;
767 706
768 std::string* name = Details<std::string>(details).ptr(); 707 std::string* name = Details<std::string>(details).ptr();
769 if (prefs::kDefaultContentSettings == *name) { 708 if (prefs::kContentSettingsPatterns == *name) {
770 ReadDefaultSettings(true);
771 } else if (prefs::kContentSettingsPatterns == *name) {
772 ReadExceptions(true); 709 ReadExceptions(true);
773 } else if (prefs::kBlockThirdPartyCookies == *name) { 710 } else if (prefs::kBlockThirdPartyCookies == *name) {
774 AutoLock auto_lock(lock_); 711 AutoLock auto_lock(lock_);
775 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( 712 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean(
776 prefs::kBlockThirdPartyCookies); 713 prefs::kBlockThirdPartyCookies);
777 is_block_third_party_cookies_managed_ = 714 is_block_third_party_cookies_managed_ =
778 profile_->GetPrefs()->IsManagedPreference( 715 profile_->GetPrefs()->IsManagedPreference(
779 prefs::kBlockThirdPartyCookies); 716 prefs::kBlockThirdPartyCookies);
780 } else if (prefs::kBlockNonsandboxedPlugins == *name) { 717 } else if (prefs::kBlockNonsandboxedPlugins == *name) {
781 AutoLock auto_lock(lock_); 718 AutoLock auto_lock(lock_);
782 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( 719 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean(
783 prefs::kBlockNonsandboxedPlugins); 720 prefs::kBlockNonsandboxedPlugins);
784 } else if (prefs::kManagedDefaultCookiesSetting == *name) {
785 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_COOKIES,
786 profile_->GetPrefs(),
787 &managed_default_content_settings_);
788 } else if (prefs::kManagedDefaultImagesSetting == *name) {
789 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_IMAGES,
790 profile_->GetPrefs(),
791 &managed_default_content_settings_);
792 } else if (prefs::kManagedDefaultJavaScriptSetting == *name) {
793 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT,
794 profile_->GetPrefs(),
795 &managed_default_content_settings_);
796 } else if (prefs::kManagedDefaultPluginsSetting == *name) {
797 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_PLUGINS,
798 profile_->GetPrefs(),
799 &managed_default_content_settings_);
800 } else if (prefs::kManagedDefaultPopupsSetting == *name) {
801 UpdateManagedDefaultSetting(CONTENT_SETTINGS_TYPE_POPUPS,
802 profile_->GetPrefs(),
803 &managed_default_content_settings_);
804 } else { 721 } else {
805 NOTREACHED() << "Unexpected preference observed"; 722 NOTREACHED() << "Unexpected preference observed";
806 return; 723 return;
807 } 724 }
808 725
809 if (!is_off_the_record_) { 726 if (!is_off_the_record_) {
810 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), 727 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(),
811 CONTENT_SETTINGS_TYPE_DEFAULT, 728 CONTENT_SETTINGS_TYPE_DEFAULT,
812 "")); 729 ""));
813 } 730 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 ClickToPlayFixup(ContentSettingsType(type), 791 ClickToPlayFixup(ContentSettingsType(type),
875 ContentSetting(setting)); 792 ContentSetting(setting));
876 } 793 }
877 794
878 break; 795 break;
879 } 796 }
880 } 797 }
881 } 798 }
882 } 799 }
883 800
884 void HostContentSettingsMap::ForceDefaultsToBeExplicit() {
885 DCHECK_EQ(arraysize(kDefaultSettings),
886 static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
887
888 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
889 if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT)
890 default_content_settings_.settings[i] = kDefaultSettings[i];
891 }
892 }
893
894 bool HostContentSettingsMap::AllDefault( 801 bool HostContentSettingsMap::AllDefault(
895 const ExtendedContentSettings& settings) const { 802 const ExtendedContentSettings& settings) const {
896 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { 803 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) {
897 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) 804 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT)
898 return false; 805 return false;
899 } 806 }
900 return settings.content_settings_for_resources.empty(); 807 return settings.content_settings_for_resources.empty();
901 } 808 }
902 809
903 void HostContentSettingsMap::ReadDefaultSettings(bool overwrite) {
904 PrefService* prefs = profile_->GetPrefs();
905 const DictionaryValue* default_settings_dictionary =
906 prefs->GetDictionary(prefs::kDefaultContentSettings);
907
908 if (overwrite)
909 default_content_settings_ = ContentSettings();
910
911 // Careful: The returned value could be NULL if the pref has never been set.
912 if (default_settings_dictionary != NULL) {
913 GetSettingsFromDictionary(default_settings_dictionary,
914 &default_content_settings_);
915 }
916 ForceDefaultsToBeExplicit();
917
918 // Read managed default content settings.
919 ReadManagedDefaultSettings(prefs, &managed_default_content_settings_);
920 }
921
922 void HostContentSettingsMap::ReadManagedDefaultSettings(
923 const PrefService* prefs, ContentSettings* settings) {
924 for (size_t type = 0; type < arraysize(kPrefToManageType); ++type) {
925 if (kPrefToManageType[type] == NULL) {
926 // TODO(markusheintz): Handle Geolocation and notification separately.
927 continue;
928 }
929 UpdateManagedDefaultSetting(ContentSettingsType(type), prefs, settings);
930 }
931 }
932
933 void HostContentSettingsMap::UpdateManagedDefaultSetting(
934 ContentSettingsType type,
935 const PrefService* prefs,
936 ContentSettings* settings) {
937 // If a pref to manage a default-content-setting was not set (NOTICE:
938 // "HasPrefPath" returns false if no value was set for a registered pref) then
939 // the default value of the preference is used. The default value of a
940 // preference to manage a default-content-settings is
941 // CONTENT_SETTING_DEFAULT. This indicates that no managed value is set. If a
942 // pref was set, than it MUST be managed.
943 DCHECK(!prefs->HasPrefPath(kPrefToManageType[type]) ||
944 prefs->IsManagedPreference(kPrefToManageType[type]));
945 AutoLock auto_lock(lock_);
946 settings->settings[type] = IntToContentSetting(
947 prefs->GetInteger(kPrefToManageType[type]));
948 }
949
950 bool HostContentSettingsMap::IsDefaultContentSettingManaged( 810 bool HostContentSettingsMap::IsDefaultContentSettingManaged(
951 ContentSettingsType content_type) const { 811 ContentSettingsType content_type) const {
952 // All managed_default_content_settings_ are always set explicitly or 812 typedef
953 // initialized to CONTENT_SETTINGS_DEFAULT. Hence each content settings type 813 std::vector<linked_ptr<ContentSettingsProviderInterface> >::const_iterator
954 // that is set to CONTENT_SETTINGS_DEFAULT is not managed since it was not set 814 provider_iterator;
955 // explicitly. 815 for (provider_iterator provider = content_settings_providers_.begin();
956 return managed_default_content_settings_.settings[content_type] != 816 provider != content_settings_providers_.end(); ++provider) {
957 CONTENT_SETTING_DEFAULT; 817 if ((*provider)->DefaultSettingIsManaged(content_type))
818 return true;
819 }
820 return false;
958 } 821 }
959 822
960 void HostContentSettingsMap::ReadExceptions(bool overwrite) { 823 void HostContentSettingsMap::ReadExceptions(bool overwrite) {
961 PrefService* prefs = profile_->GetPrefs(); 824 PrefService* prefs = profile_->GetPrefs();
962 DictionaryValue* all_settings_dictionary = 825 DictionaryValue* all_settings_dictionary =
963 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); 826 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns);
964 827
965 if (overwrite) 828 if (overwrite)
966 host_content_settings_.clear(); 829 host_content_settings_.clear();
967 830
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 } 965 }
1103 966
1104 for (size_t i = 0; i < move_items.size(); ++i) { 967 for (size_t i = 0; i < move_items.size(); ++i) {
1105 Value* pattern_settings_dictionary = NULL; 968 Value* pattern_settings_dictionary = NULL;
1106 all_settings_dictionary->RemoveWithoutPathExpansion( 969 all_settings_dictionary->RemoveWithoutPathExpansion(
1107 move_items[i].first, &pattern_settings_dictionary); 970 move_items[i].first, &pattern_settings_dictionary);
1108 all_settings_dictionary->SetWithoutPathExpansion( 971 all_settings_dictionary->SetWithoutPathExpansion(
1109 move_items[i].second, pattern_settings_dictionary); 972 move_items[i].second, pattern_settings_dictionary);
1110 } 973 }
1111 } 974 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698