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

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: 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 content_settings_providers_deleter_(&content_settings_providers_),
128 block_third_party_cookies_(false), 101 block_third_party_cookies_(false),
129 is_block_third_party_cookies_managed_(false), 102 is_block_third_party_cookies_managed_(false),
130 is_off_the_record_(profile_->IsOffTheRecord()), 103 is_off_the_record_(profile_->IsOffTheRecord()),
131 updating_preferences_(false) { 104 updating_preferences_(false) {
105 // The order in which the content settings providers are created is critical,
106 // as providers that are further down in the list (i.e. added later) override
107 // providers further up.
108 content_settings_providers_.push_back(
109 new PrefContentSettingsProvider(profile));
110 content_settings_providers_.push_back(
111 new PolicyContentSettingsProvider(profile));
112
132 PrefService* prefs = profile_->GetPrefs(); 113 PrefService* prefs = profile_->GetPrefs();
133 114
134 MigrateObsoleteCookiePref(prefs); 115 MigrateObsoleteCookiePref(prefs);
135 116
136 MigrateObsoletePopupsPref(prefs); 117 MigrateObsoletePopupsPref(prefs);
137 118
138 MigrateObsoletePerhostPref(prefs); 119 MigrateObsoletePerhostPref(prefs);
139 120
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. 121 // Read misc. global settings.
146 block_third_party_cookies_ = 122 block_third_party_cookies_ =
147 prefs->GetBoolean(prefs::kBlockThirdPartyCookies); 123 prefs->GetBoolean(prefs::kBlockThirdPartyCookies);
148 is_block_third_party_cookies_managed_ = 124 is_block_third_party_cookies_managed_ =
149 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies); 125 prefs->IsManagedPreference(prefs::kBlockThirdPartyCookies);
150 block_nonsandboxed_plugins_ = 126 block_nonsandboxed_plugins_ =
151 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins); 127 prefs->GetBoolean(prefs::kBlockNonsandboxedPlugins);
152 128
153 // Verify preferences version. 129 // Verify preferences version.
154 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) { 130 if (!prefs->HasPrefPath(prefs::kContentSettingsVersion)) {
155 prefs->SetInteger(prefs::kContentSettingsVersion, 131 prefs->SetInteger(prefs::kContentSettingsVersion,
156 ContentSettingsPattern::kContentSettingsPatternVersion); 132 ContentSettingsPattern::kContentSettingsPatternVersion);
157 } 133 }
158 if (prefs->GetInteger(prefs::kContentSettingsVersion) > 134 if (prefs->GetInteger(prefs::kContentSettingsVersion) >
159 ContentSettingsPattern::kContentSettingsPatternVersion) { 135 ContentSettingsPattern::kContentSettingsPatternVersion) {
160 LOG(ERROR) << "Unknown content settings version in preferences."; 136 LOG(ERROR) << "Unknown content settings version in preferences.";
161 return; 137 return;
162 } 138 }
163 139
164 // Read exceptions. 140 // Read exceptions.
165 ReadExceptions(false); 141 ReadExceptions(false);
166 142
167 pref_change_registrar_.Init(prefs); 143 pref_change_registrar_.Init(prefs);
168 pref_change_registrar_.Add(prefs::kDefaultContentSettings, this);
169 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this); 144 pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
170 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); 145 pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
171 pref_change_registrar_.Add(prefs::kBlockNonsandboxedPlugins, this); 146 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, 147 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED,
185 Source<Profile>(profile_)); 148 Source<Profile>(profile_));
186 } 149 }
187 150
188 // static 151 // static
189 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { 152 void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) {
190 prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings);
191 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion, 153 prefs->RegisterIntegerPref(prefs::kContentSettingsVersion,
192 ContentSettingsPattern::kContentSettingsPatternVersion); 154 ContentSettingsPattern::kContentSettingsPatternVersion);
193 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns); 155 prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns);
194 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false); 156 prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, false);
195 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false); 157 prefs->RegisterBooleanPref(prefs::kBlockNonsandboxedPlugins, false);
196 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); 158 prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0);
197 159
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: 160 // Obsolete prefs, for migration:
212 prefs->RegisterIntegerPref(prefs::kCookieBehavior, 161 prefs->RegisterIntegerPref(prefs::kCookieBehavior,
213 net::StaticCookiePolicy::ALLOW_ALL_COOKIES); 162 net::StaticCookiePolicy::ALLOW_ALL_COOKIES);
214 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts); 163 prefs->RegisterListPref(prefs::kPopupWhitelistedHosts);
215 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings); 164 prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings);
216 } 165 }
217 166
218 ContentSetting HostContentSettingsMap::GetDefaultContentSetting( 167 ContentSetting HostContentSettingsMap::GetDefaultContentSetting(
219 ContentSettingsType content_type) const { 168 ContentSettingsType content_type) const {
220 AutoLock auto_lock(lock_); 169 AutoLock auto_lock(lock_);
221 if (IsDefaultContentSettingManaged(content_type)) 170 typedef std::vector<ContentSettingsProviderInterface*>::const_iterator
222 return managed_default_content_settings_.settings[content_type]; 171 provider_iterator;
223 return default_content_settings_.settings[content_type]; 172 ContentSetting setting = CONTENT_SETTING_DEFAULT;
173 for (provider_iterator provider = content_settings_providers_.begin();
174 provider != content_settings_providers_.end(); ++provider) {
175 if (!(*provider)->CanProvideDefaultSetting(content_type))
176 continue;
177 ContentSetting provided_setting =
178 (*provider)->ProvideDefaultSetting(content_type);
179 if (provided_setting != CONTENT_SETTING_DEFAULT)
180 setting = provided_setting;
181 }
182 CHECK_NE(CONTENT_SETTING_DEFAULT, setting);
183 return setting;
224 } 184 }
225 185
226 ContentSetting HostContentSettingsMap::GetContentSetting( 186 ContentSetting HostContentSettingsMap::GetContentSetting(
227 const GURL& url, 187 const GURL& url,
228 ContentSettingsType content_type, 188 ContentSettingsType content_type,
229 const std::string& resource_identifier) const { 189 const std::string& resource_identifier) const {
230 ContentSetting setting = GetNonDefaultContentSetting(url, 190 ContentSetting setting = GetNonDefaultContentSetting(url,
231 content_type, 191 content_type,
232 resource_identifier); 192 resource_identifier);
233 if (setting == CONTENT_SETTING_DEFAULT || 193 if (setting == CONTENT_SETTING_DEFAULT ||
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); 271 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1);
312 } 272 }
313 273
314 return CONTENT_SETTING_DEFAULT; 274 return CONTENT_SETTING_DEFAULT;
315 } 275 }
316 276
317 ContentSettings HostContentSettingsMap::GetContentSettings( 277 ContentSettings HostContentSettingsMap::GetContentSettings(
318 const GURL& url) const { 278 const GURL& url) const {
319 ContentSettings output = GetNonDefaultContentSettings(url); 279 ContentSettings output = GetNonDefaultContentSettings(url);
320 280
321 AutoLock auto_lock(lock_);
322
323 // If we require a resource identifier, set the content settings to default, 281 // If we require a resource identifier, set the content settings to default,
324 // otherwise make the defaults explicit. 282 // otherwise make the defaults explicit.
325 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { 283 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) {
326 if (RequiresResourceIdentifier(ContentSettingsType(j))) { 284 if (RequiresResourceIdentifier(ContentSettingsType(j))) {
327 output.settings[j] = CONTENT_SETTING_DEFAULT; 285 output.settings[j] = CONTENT_SETTING_DEFAULT;
328 } else { 286 } else {
329 if (output.settings[j] == CONTENT_SETTING_DEFAULT) { 287 if (output.settings[j] == CONTENT_SETTING_DEFAULT) {
330 output.settings[j] = default_content_settings_.settings[j]; 288 output.settings[j] = GetDefaultContentSetting(ContentSettingsType(j));
331 } 289 }
332 // A managed default content setting has the highest priority and hence 290 // A managed default content setting has the highest priority and hence
333 // will overwrite any previously set value. 291 // will overwrite any previously set value.
334 if (IsDefaultContentSettingManaged(ContentSettingsType(j))) { 292 if (IsDefaultContentSettingManaged(ContentSettingsType(j))) {
293 AutoLock auto_lock(lock_);
335 output.settings[j] = 294 output.settings[j] =
336 managed_default_content_settings_.settings[j]; 295 managed_default_content_settings_.settings[j];
337 } 296 }
338 } 297 }
339 } 298 }
340 return output; 299 return output;
341 } 300 }
342 301
343 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings( 302 ContentSettings HostContentSettingsMap::GetNonDefaultContentSettings(
344 const GURL& url) const { 303 const GURL& url) const {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 393
435 void HostContentSettingsMap::SetDefaultContentSetting( 394 void HostContentSettingsMap::SetDefaultContentSetting(
436 ContentSettingsType content_type, 395 ContentSettingsType content_type,
437 ContentSetting setting) { 396 ContentSetting setting) {
438 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 397 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
440 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS || 399 DCHECK(content_type != CONTENT_SETTINGS_TYPE_PLUGINS ||
441 setting != CONTENT_SETTING_ASK || 400 setting != CONTENT_SETTING_ASK ||
442 CommandLine::ForCurrentProcess()->HasSwitch( 401 CommandLine::ForCurrentProcess()->HasSwitch(
443 switches::kEnableClickToPlay)); 402 switches::kEnableClickToPlay));
444 PrefService* prefs = profile_->GetPrefs();
445 403
446 // The default settings may not be directly modified for OTR sessions. 404 // The default settings may not be directly modified for OTR sessions.
447 // Instead, they are synced to the main profile's setting. 405 // Instead, they are synced to the main profile's setting.
448 if (is_off_the_record_) { 406 if (is_off_the_record_) {
449 NOTREACHED(); 407 NOTREACHED();
450 return; 408 return;
451 } 409 }
452 410
453 DictionaryValue* default_settings_dictionary =
454 prefs->GetMutableDictionary(prefs::kDefaultContentSettings);
455 std::string dictionary_path(kTypeNames[content_type]);
456 updating_preferences_ = true;
457 { 411 {
gfeher 2010/12/08 13:59:17 Why is this in a separate block?
jochen (gone - plz use gerrit) 2010/12/08 14:54:01 Done.
458 AutoLock auto_lock(lock_); 412 typedef std::vector<ContentSettingsProviderInterface*>::iterator
459 ScopedPrefUpdate update(prefs, prefs::kDefaultContentSettings); 413 provider_iterator;
460 if ((setting == CONTENT_SETTING_DEFAULT) || 414 for (provider_iterator provider = content_settings_providers_.begin();
461 (setting == kDefaultSettings[content_type])) { 415 provider != content_settings_providers_.end(); ++provider) {
462 default_content_settings_.settings[content_type] = 416 if (!(*provider)->CanProvideDefaultSetting(content_type))
463 kDefaultSettings[content_type]; 417 continue;
464 default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path, 418 (*provider)->UpdateDefaultSetting(content_type, setting);
gfeher 2010/12/08 13:59:17 Adding CanUpdateDefaultSetting to the interface an
jochen (gone - plz use gerrit) 2010/12/08 14:54:01 I think it's better to broadcast the update to all
gfeher 2010/12/09 10:29:53 Ok.
465 NULL);
466 } else {
467 default_content_settings_.settings[content_type] = setting;
468 default_settings_dictionary->SetWithoutPathExpansion(
469 dictionary_path, Value::CreateIntegerValue(setting));
470 } 419 }
471 } 420 }
472 updating_preferences_ = false;
473
474 NotifyObservers(
475 ContentSettingsDetails(ContentSettingsPattern(), content_type, ""));
476 } 421 }
477 422
478 void HostContentSettingsMap::SetContentSetting( 423 void HostContentSettingsMap::SetContentSetting(
479 const ContentSettingsPattern& original_pattern, 424 const ContentSettingsPattern& original_pattern,
480 ContentSettingsType content_type, 425 ContentSettingsType content_type,
481 const std::string& resource_identifier, 426 const std::string& resource_identifier,
482 ContentSetting setting) { 427 ContentSetting setting) {
483 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation. 428 DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
485 DCHECK_NE(RequiresResourceIdentifier(content_type), 430 DCHECK_NE(RequiresResourceIdentifier(content_type),
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 UserMetricsAction("BlockNonsandboxedPlugins_Disable")); 665 UserMetricsAction("BlockNonsandboxedPlugins_Disable"));
721 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); 666 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins);
722 } 667 }
723 } 668 }
724 669
725 void HostContentSettingsMap::ResetToDefaults() { 670 void HostContentSettingsMap::ResetToDefaults() {
726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 671 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
727 672
728 { 673 {
729 AutoLock auto_lock(lock_); 674 AutoLock auto_lock(lock_);
730 default_content_settings_ = ContentSettings(); 675 typedef std::vector<ContentSettingsProviderInterface*>::iterator
731 ForceDefaultsToBeExplicit(); 676 provider_iterator;
677 for (provider_iterator provider = content_settings_providers_.begin();
678 provider != content_settings_providers_.end(); ++provider) {
679 (*provider)->ResetToDefaults();
680 }
732 // Clear all content settings map except the 681 // Clear all content settings map except the
733 // managed_default_content_settings. 682 // managed_default_content_settings.
734 host_content_settings_.clear(); 683 host_content_settings_.clear();
735 off_the_record_settings_.clear(); 684 off_the_record_settings_.clear();
736 // Don't reset block third party cookies if they are managed. 685 // Don't reset block third party cookies if they are managed.
737 if (!IsBlockThirdPartyCookiesManaged()) 686 if (!IsBlockThirdPartyCookiesManaged())
738 block_third_party_cookies_ = false; 687 block_third_party_cookies_ = false;
739 block_nonsandboxed_plugins_ = false; 688 block_nonsandboxed_plugins_ = false;
740 } 689 }
741 690
742 if (!is_off_the_record_) { 691 if (!is_off_the_record_) {
743 PrefService* prefs = profile_->GetPrefs(); 692 PrefService* prefs = profile_->GetPrefs();
744 updating_preferences_ = true; 693 updating_preferences_ = true;
745 prefs->ClearPref(prefs::kDefaultContentSettings);
746 prefs->ClearPref(prefs::kContentSettingsPatterns); 694 prefs->ClearPref(prefs::kContentSettingsPatterns);
747 // If the block third party cookies preference is managed we still must 695 // 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 696 // clear it in order to restore the default value for later when the
749 // preference is not managed anymore. 697 // preference is not managed anymore.
750 prefs->ClearPref(prefs::kBlockThirdPartyCookies); 698 prefs->ClearPref(prefs::kBlockThirdPartyCookies);
751 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins); 699 prefs->ClearPref(prefs::kBlockNonsandboxedPlugins);
752 updating_preferences_ = false; 700 updating_preferences_ = false;
753 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), 701 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(),
754 CONTENT_SETTINGS_TYPE_DEFAULT, 702 CONTENT_SETTINGS_TYPE_DEFAULT,
755 "")); 703 ""));
756 } 704 }
757 } 705 }
758 706
759 void HostContentSettingsMap::Observe(NotificationType type, 707 void HostContentSettingsMap::Observe(NotificationType type,
760 const NotificationSource& source, 708 const NotificationSource& source,
761 const NotificationDetails& details) { 709 const NotificationDetails& details) {
762 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
763 711
764 if (NotificationType::PREF_CHANGED == type) { 712 if (NotificationType::PREF_CHANGED == type) {
765 if (updating_preferences_) 713 if (updating_preferences_)
766 return; 714 return;
767 715
768 std::string* name = Details<std::string>(details).ptr(); 716 std::string* name = Details<std::string>(details).ptr();
769 if (prefs::kDefaultContentSettings == *name) { 717 if (prefs::kContentSettingsPatterns == *name) {
770 ReadDefaultSettings(true);
771 } else if (prefs::kContentSettingsPatterns == *name) {
772 ReadExceptions(true); 718 ReadExceptions(true);
773 } else if (prefs::kBlockThirdPartyCookies == *name) { 719 } else if (prefs::kBlockThirdPartyCookies == *name) {
774 AutoLock auto_lock(lock_); 720 AutoLock auto_lock(lock_);
775 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean( 721 block_third_party_cookies_ = profile_->GetPrefs()->GetBoolean(
776 prefs::kBlockThirdPartyCookies); 722 prefs::kBlockThirdPartyCookies);
777 is_block_third_party_cookies_managed_ = 723 is_block_third_party_cookies_managed_ =
778 profile_->GetPrefs()->IsManagedPreference( 724 profile_->GetPrefs()->IsManagedPreference(
779 prefs::kBlockThirdPartyCookies); 725 prefs::kBlockThirdPartyCookies);
780 } else if (prefs::kBlockNonsandboxedPlugins == *name) { 726 } else if (prefs::kBlockNonsandboxedPlugins == *name) {
781 AutoLock auto_lock(lock_); 727 AutoLock auto_lock(lock_);
782 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean( 728 block_nonsandboxed_plugins_ = profile_->GetPrefs()->GetBoolean(
783 prefs::kBlockNonsandboxedPlugins); 729 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 { 730 } else {
805 NOTREACHED() << "Unexpected preference observed"; 731 NOTREACHED() << "Unexpected preference observed";
806 return; 732 return;
807 } 733 }
808 734
809 if (!is_off_the_record_) { 735 if (!is_off_the_record_) {
810 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(), 736 NotifyObservers(ContentSettingsDetails(ContentSettingsPattern(),
811 CONTENT_SETTINGS_TYPE_DEFAULT, 737 CONTENT_SETTINGS_TYPE_DEFAULT,
812 "")); 738 ""));
813 } 739 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 ClickToPlayFixup(ContentSettingsType(type), 800 ClickToPlayFixup(ContentSettingsType(type),
875 ContentSetting(setting)); 801 ContentSetting(setting));
876 } 802 }
877 803
878 break; 804 break;
879 } 805 }
880 } 806 }
881 } 807 }
882 } 808 }
883 809
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( 810 bool HostContentSettingsMap::AllDefault(
895 const ExtendedContentSettings& settings) const { 811 const ExtendedContentSettings& settings) const {
896 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { 812 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) {
897 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) 813 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT)
898 return false; 814 return false;
899 } 815 }
900 return settings.content_settings_for_resources.empty(); 816 return settings.content_settings_for_resources.empty();
901 } 817 }
902 818
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( 819 bool HostContentSettingsMap::IsDefaultContentSettingManaged(
951 ContentSettingsType content_type) const { 820 ContentSettingsType content_type) const {
952 // All managed_default_content_settings_ are always set explicitly or 821 typedef std::vector<ContentSettingsProviderInterface*>::const_iterator
953 // initialized to CONTENT_SETTINGS_DEFAULT. Hence each content settings type 822 provider_iterator;
954 // that is set to CONTENT_SETTINGS_DEFAULT is not managed since it was not set 823 for (provider_iterator provider = content_settings_providers_.begin();
markusheintz_ 2010/12/08 14:03:06 Theoretically It is possible that a content settin
jochen (gone - plz use gerrit) 2010/12/08 14:54:01 That's true. However, in that case, already the st
955 // explicitly. 824 provider != content_settings_providers_.end(); ++provider) {
956 return managed_default_content_settings_.settings[content_type] != 825 if ((*provider)->DefaultSettingIsManaged(content_type))
957 CONTENT_SETTING_DEFAULT; 826 return true;
827 }
828 return false;
958 } 829 }
959 830
960 void HostContentSettingsMap::ReadExceptions(bool overwrite) { 831 void HostContentSettingsMap::ReadExceptions(bool overwrite) {
961 PrefService* prefs = profile_->GetPrefs(); 832 PrefService* prefs = profile_->GetPrefs();
962 DictionaryValue* all_settings_dictionary = 833 DictionaryValue* all_settings_dictionary =
963 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns); 834 prefs->GetMutableDictionary(prefs::kContentSettingsPatterns);
964 835
965 if (overwrite) 836 if (overwrite)
966 host_content_settings_.clear(); 837 host_content_settings_.clear();
967 838
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 } 973 }
1103 974
1104 for (size_t i = 0; i < move_items.size(); ++i) { 975 for (size_t i = 0; i < move_items.size(); ++i) {
1105 Value* pattern_settings_dictionary = NULL; 976 Value* pattern_settings_dictionary = NULL;
1106 all_settings_dictionary->RemoveWithoutPathExpansion( 977 all_settings_dictionary->RemoveWithoutPathExpansion(
1107 move_items[i].first, &pattern_settings_dictionary); 978 move_items[i].first, &pattern_settings_dictionary);
1108 all_settings_dictionary->SetWithoutPathExpansion( 979 all_settings_dictionary->SetWithoutPathExpansion(
1109 move_items[i].second, pattern_settings_dictionary); 980 move_items[i].second, pattern_settings_dictionary);
1110 } 981 }
1111 } 982 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698