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

Side by Side Diff: components/content_settings/core/browser/host_content_settings_map.cc

Issue 1686343002: Change HostContentSettingsMap::SetContentSetting to use GURLs instead of patterns (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add migrate method Created 4 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/content_settings/core/browser/host_content_settings_map.h" 5 #include "components/content_settings/core/browser/host_content_settings_map.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 16 matching lines...) Expand all
27 #include "components/content_settings/core/browser/content_settings_utils.h" 27 #include "components/content_settings/core/browser/content_settings_utils.h"
28 #include "components/content_settings/core/browser/website_settings_registry.h" 28 #include "components/content_settings/core/browser/website_settings_registry.h"
29 #include "components/content_settings/core/common/content_settings_pattern.h" 29 #include "components/content_settings/core/common/content_settings_pattern.h"
30 #include "components/content_settings/core/common/pref_names.h" 30 #include "components/content_settings/core/common/pref_names.h"
31 #include "components/pref_registry/pref_registry_syncable.h" 31 #include "components/pref_registry/pref_registry_syncable.h"
32 #include "components/prefs/pref_service.h" 32 #include "components/prefs/pref_service.h"
33 #include "net/base/net_errors.h" 33 #include "net/base/net_errors.h"
34 #include "net/base/static_cookie_policy.h" 34 #include "net/base/static_cookie_policy.h"
35 #include "url/gurl.h" 35 #include "url/gurl.h"
36 36
37 using content_settings::WebsiteSettingsInfo;
38
37 namespace { 39 namespace {
38 40
39 typedef std::vector<content_settings::Rule> Rules; 41 typedef std::vector<content_settings::Rule> Rules;
40 42
41 typedef std::pair<std::string, std::string> StringPair; 43 typedef std::pair<std::string, std::string> StringPair;
42 44
43 struct ProviderNamesSourceMapEntry { 45 struct ProviderNamesSourceMapEntry {
44 const char* provider_name; 46 const char* provider_name;
45 content_settings::SettingSource provider_source; 47 content_settings::SettingSource provider_source;
46 }; 48 };
47 49
48 const ProviderNamesSourceMapEntry kProviderNamesSourceMap[] = { 50 const ProviderNamesSourceMapEntry kProviderNamesSourceMap[] = {
49 {"platform_app", content_settings::SETTING_SOURCE_EXTENSION}, 51 {"platform_app", content_settings::SETTING_SOURCE_EXTENSION},
50 {"policy", content_settings::SETTING_SOURCE_POLICY}, 52 {"policy", content_settings::SETTING_SOURCE_POLICY},
51 {"supervised_user", content_settings::SETTING_SOURCE_SUPERVISED}, 53 {"supervised_user", content_settings::SETTING_SOURCE_SUPERVISED},
52 {"extension", content_settings::SETTING_SOURCE_EXTENSION}, 54 {"extension", content_settings::SETTING_SOURCE_EXTENSION},
53 {"preference", content_settings::SETTING_SOURCE_USER}, 55 {"preference", content_settings::SETTING_SOURCE_USER},
54 {"default", content_settings::SETTING_SOURCE_USER}, 56 {"default", content_settings::SETTING_SOURCE_USER},
55 }; 57 };
56 58
59 const ContentSettingsType kMigrateContentSettingTypes[] = {
60 // Only content types of scoping type: REQUESTING_DOMAIN_ONLY_SCOPE,
61 // REQUESTING_ORIGIN_ONLY_SCOPE and TOP_LEVEL_DOMAIN_ONLY_SCOPE need to be
62 // migrated.
63 CONTENT_SETTINGS_TYPE_COOKIES,
64 CONTENT_SETTINGS_TYPE_IMAGES,
65 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
66 CONTENT_SETTINGS_TYPE_PLUGINS,
67 CONTENT_SETTINGS_TYPE_POPUPS,
68 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
69 CONTENT_SETTINGS_TYPE_MOUSELOCK,
70 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
71 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
72 CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
73 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
74 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE,
75 CONTENT_SETTINGS_TYPE_KEYGEN,
76 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS,
77 CONTENT_SETTINGS_TYPE_MIXEDSCRIPT};
78
57 static_assert( 79 static_assert(
58 arraysize(kProviderNamesSourceMap) == 80 arraysize(kProviderNamesSourceMap) ==
59 HostContentSettingsMap::NUM_PROVIDER_TYPES, 81 HostContentSettingsMap::NUM_PROVIDER_TYPES,
60 "kProviderNamesSourceMap should have NUM_PROVIDER_TYPES elements"); 82 "kProviderNamesSourceMap should have NUM_PROVIDER_TYPES elements");
61 83
62 // Returns true if the |content_type| supports a resource identifier. 84 // Returns true if the |content_type| supports a resource identifier.
63 // Resource identifiers are supported (but not required) for plugins. 85 // Resource identifiers are supported (but not required) for plugins.
64 bool SupportsResourceIdentifier(ContentSettingsType content_type) { 86 bool SupportsResourceIdentifier(ContentSettingsType content_type) {
65 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS; 87 return content_type == CONTENT_SETTINGS_TYPE_PLUGINS;
66 } 88 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 content_settings::ValueToContentSetting(value.get()); 121 content_settings::ValueToContentSetting(value.get());
100 if (setting != CONTENT_SETTING_ALLOW) 122 if (setting != CONTENT_SETTING_ALLOW)
101 return value; 123 return value;
102 DCHECK(content_settings_info->IsSettingValid(CONTENT_SETTING_ASK)); 124 DCHECK(content_settings_info->IsSettingValid(CONTENT_SETTING_ASK));
103 return content_settings::ContentSettingToValue(CONTENT_SETTING_ASK); 125 return content_settings::ContentSettingToValue(CONTENT_SETTING_ASK);
104 } 126 }
105 127
106 return value; 128 return value;
107 } 129 }
108 130
131 struct PatternPair {
132 ContentSettingsPattern primary_pattern;
133 ContentSettingsPattern secondary_pattern;
134 };
135
136 PatternPair GetPatternsFromScopingType(
137 WebsiteSettingsInfo::ScopingType scoping_type,
138 const GURL& primary_url,
139 const GURL& secondary_url) {
140 PatternPair patterns;
141
142 switch (scoping_type) {
143 case WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE:
144 case WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE:
145 patterns.primary_pattern = ContentSettingsPattern::FromURL(primary_url);
146 patterns.secondary_pattern = ContentSettingsPattern::Wildcard();
147 break;
148 case WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE:
149 patterns.primary_pattern =
150 ContentSettingsPattern::FromURLNoWildcard(primary_url);
151 patterns.secondary_pattern = ContentSettingsPattern::Wildcard();
152 break;
153 case WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE:
154 patterns.primary_pattern =
155 ContentSettingsPattern::FromURLNoWildcard(primary_url);
156 patterns.secondary_pattern =
157 ContentSettingsPattern::FromURLNoWildcard(secondary_url);
158 break;
159 }
160 return patterns;
161 }
162
109 } // namespace 163 } // namespace
110 164
111 HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs, 165 HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs,
112 bool is_incognito_profile, 166 bool is_incognito_profile,
113 bool is_guest_profile) 167 bool is_guest_profile)
114 : 168 :
115 #ifndef NDEBUG 169 #ifndef NDEBUG
116 used_from_thread_id_(base::PlatformThread::CurrentId()), 170 used_from_thread_id_(base::PlatformThread::CurrentId()),
117 #endif 171 #endif
118 prefs_(prefs), 172 prefs_(prefs),
(...skipping 11 matching lines...) Expand all
130 // This ensures that content settings are cleared for the guest profile. This 184 // This ensures that content settings are cleared for the guest profile. This
131 // wouldn't be needed except that we used to allow settings to be stored for 185 // wouldn't be needed except that we used to allow settings to be stored for
132 // the guest profile and so we need to ensure those get cleared. 186 // the guest profile and so we need to ensure those get cleared.
133 if (is_guest_profile) 187 if (is_guest_profile)
134 pref_provider->ClearPrefs(); 188 pref_provider->ClearPrefs();
135 189
136 content_settings::ObservableProvider* default_provider = 190 content_settings::ObservableProvider* default_provider =
137 new content_settings::DefaultProvider(prefs_, is_off_the_record_); 191 new content_settings::DefaultProvider(prefs_, is_off_the_record_);
138 default_provider->AddObserver(this); 192 default_provider->AddObserver(this);
139 content_settings_providers_[DEFAULT_PROVIDER] = default_provider; 193 content_settings_providers_[DEFAULT_PROVIDER] = default_provider;
194
195 MigrateOldSettings();
140 } 196 }
141 197
142 // static 198 // static
143 void HostContentSettingsMap::RegisterProfilePrefs( 199 void HostContentSettingsMap::RegisterProfilePrefs(
144 user_prefs::PrefRegistrySyncable* registry) { 200 user_prefs::PrefRegistrySyncable* registry) {
145 // Ensure the content settings are all registered. 201 // Ensure the content settings are all registered.
146 content_settings::ContentSettingsRegistry::GetInstance(); 202 content_settings::ContentSettingsRegistry::GetInstance();
147 203
148 registry->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0); 204 registry->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0);
149 205
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 ContentSettingsPattern::Wildcard(), content_type, 332 ContentSettingsPattern::Wildcard(), content_type,
277 std::string(), std::move(value)); 333 std::string(), std::move(value));
278 } 334 }
279 335
280 void HostContentSettingsMap::SetWebsiteSettingDefaultScope( 336 void HostContentSettingsMap::SetWebsiteSettingDefaultScope(
281 const GURL& requesting_url, 337 const GURL& requesting_url,
282 const GURL& top_level_url, 338 const GURL& top_level_url,
283 ContentSettingsType content_type, 339 ContentSettingsType content_type,
284 const std::string& resource_identifier, 340 const std::string& resource_identifier,
285 base::Value* value) { 341 base::Value* value) {
286 using content_settings::WebsiteSettingsInfo;
287
288 const WebsiteSettingsInfo* info = 342 const WebsiteSettingsInfo* info =
289 content_settings::WebsiteSettingsRegistry::GetInstance()->Get( 343 content_settings::WebsiteSettingsRegistry::GetInstance()->Get(
290 content_type); 344 content_type);
291 ContentSettingsPattern primary_pattern; 345 PatternPair patterns = GetPatternsFromScopingType(
292 ContentSettingsPattern secondary_pattern; 346 info->scoping_type(), requesting_url, top_level_url);
293 switch (info->scoping_type()) { 347 ContentSettingsPattern primary_pattern = patterns.primary_pattern;
294 case WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE: 348 ContentSettingsPattern secondary_pattern = patterns.secondary_pattern;
295 primary_pattern = ContentSettingsPattern::FromURL(top_level_url);
296 secondary_pattern = ContentSettingsPattern::Wildcard();
297 DCHECK(requesting_url.is_empty());
298 break;
299 case WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE:
300 primary_pattern = ContentSettingsPattern::FromURL(requesting_url);
301 secondary_pattern = ContentSettingsPattern::Wildcard();
302 DCHECK(top_level_url.is_empty());
303 break;
304 case WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE:
305 primary_pattern =
306 ContentSettingsPattern::FromURLNoWildcard(requesting_url);
307 secondary_pattern = ContentSettingsPattern::Wildcard();
308 DCHECK(top_level_url.is_empty());
309 break;
310 case WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE:
311 primary_pattern =
312 ContentSettingsPattern::FromURLNoWildcard(requesting_url);
313 secondary_pattern =
314 ContentSettingsPattern::FromURLNoWildcard(top_level_url);
315 break;
316 }
317 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) 349 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid())
318 return; 350 return;
351
319 SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type, 352 SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type,
320 resource_identifier, make_scoped_ptr(value)); 353 resource_identifier, make_scoped_ptr(value));
321 } 354 }
322 355
323 void HostContentSettingsMap::SetWebsiteSettingCustomScope( 356 void HostContentSettingsMap::SetWebsiteSettingCustomScope(
324 const ContentSettingsPattern& primary_pattern, 357 const ContentSettingsPattern& primary_pattern,
325 const ContentSettingsPattern& secondary_pattern, 358 const ContentSettingsPattern& secondary_pattern,
326 ContentSettingsType content_type, 359 ContentSettingsType content_type,
327 const std::string& resource_identifier, 360 const std::string& resource_identifier,
328 scoped_ptr<base::Value> value) { 361 scoped_ptr<base::Value> value) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 if (setting != CONTENT_SETTING_DEFAULT) { 463 if (setting != CONTENT_SETTING_DEFAULT) {
431 DCHECK(content_settings::ContentSettingsRegistry::GetInstance() 464 DCHECK(content_settings::ContentSettingsRegistry::GetInstance()
432 ->Get(content_type) 465 ->Get(content_type)
433 ->IsSettingValid(setting)); 466 ->IsSettingValid(setting));
434 value.reset(new base::FundamentalValue(setting)); 467 value.reset(new base::FundamentalValue(setting));
435 } 468 }
436 SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type, 469 SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type,
437 resource_identifier, std::move(value)); 470 resource_identifier, std::move(value));
438 } 471 }
439 472
473 void HostContentSettingsMap::SetContentSettingDefaultScope(
474 const GURL& primary_url,
475 const GURL& secondary_url,
476 ContentSettingsType content_type,
477 const std::string& resource_identifier,
478 ContentSetting setting) {
479 using content_settings::ContentSettingsInfo;
480 const ContentSettingsInfo* info =
481 content_settings::ContentSettingsRegistry::GetInstance()->Get(
482 content_type);
483 DCHECK(info);
484
485 PatternPair patterns =
486 GetPatternsFromScopingType(info->website_settings_info()->scoping_type(),
487 primary_url, secondary_url);
488 ContentSettingsPattern primary_pattern = patterns.primary_pattern;
489 ContentSettingsPattern secondary_pattern = patterns.secondary_pattern;
490 if (!primary_pattern.IsValid() || !secondary_pattern.IsValid())
491 return;
492
493 SetContentSettingCustomScope(primary_pattern, secondary_pattern, content_type,
494 resource_identifier, setting);
495 }
496
497 void HostContentSettingsMap::SetContentSettingCustomScope(
raymes 2016/03/01 06:17:32 Maybe we shouldn't add this yet - instead we could
lshang 2016/03/02 04:02:28 Done.
498 const ContentSettingsPattern& primary_pattern,
499 const ContentSettingsPattern& secondary_pattern,
500 ContentSettingsType content_type,
501 const std::string& resource_identifier,
502 ContentSetting setting) {
503 DCHECK(content_settings::ContentSettingsRegistry::GetInstance()->Get(
504 content_type));
505 if (setting == CONTENT_SETTING_ALLOW &&
506 (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION ||
507 content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)) {
508 UpdateLastUsageByPattern(primary_pattern, secondary_pattern, content_type);
509 }
510
511 scoped_ptr<base::Value> value;
512 // A value of CONTENT_SETTING_DEFAULT implies deleting the content setting.
513 if (setting != CONTENT_SETTING_DEFAULT) {
514 DCHECK(content_settings::ContentSettingsRegistry::GetInstance()
515 ->Get(content_type)
516 ->IsSettingValid(setting));
517 value.reset(new base::FundamentalValue(setting));
518 }
519 SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type,
520 resource_identifier, std::move(value));
521 }
522
523 void HostContentSettingsMap::MigrateOldSettings() {
524 for (const ContentSettingsType& type : kMigrateContentSettingTypes) {
525 ContentSettingsForOneType settings;
526 GetSettingsForOneType(type, std::string(), &settings);
527 for (const ContentSettingPatternSource& setting_entry : settings) {
528 // Migrate old-format settings only.
529 if (setting_entry.secondary_pattern !=
530 ContentSettingsPattern::Wildcard()) {
531 GURL url(setting_entry.primary_pattern.ToString());
532 // Pull out the value of the old-format setting. Only do this if the
533 // patterns are as we expect them to be, otherwise the setting will just
534 // be removed for safety.
535 ContentSetting content_setting = CONTENT_SETTING_DEFAULT;
536 if (setting_entry.primary_pattern == setting_entry.secondary_pattern &&
537 url.is_valid()) {
538 content_setting = GetContentSetting(url, url, type, std::string());
539 }
540 // Remove the old pattern.
541 SetContentSettingCustomScope(setting_entry.primary_pattern,
542 setting_entry.secondary_pattern, type,
543 std::string(), CONTENT_SETTING_DEFAULT);
544 // Set the new pattern.
545 if (content_setting) {
546 SetContentSettingDefaultScope(url, GURL(), type, std::string(),
547 content_setting);
548 }
549 }
550 }
551 }
552 }
553
440 ContentSetting HostContentSettingsMap::GetContentSettingAndMaybeUpdateLastUsage( 554 ContentSetting HostContentSettingsMap::GetContentSettingAndMaybeUpdateLastUsage(
441 const GURL& primary_url, 555 const GURL& primary_url,
442 const GURL& secondary_url, 556 const GURL& secondary_url,
443 ContentSettingsType content_type, 557 ContentSettingsType content_type,
444 const std::string& resource_identifier) { 558 const std::string& resource_identifier) {
445 DCHECK(thread_checker_.CalledOnValidThread()); 559 DCHECK(thread_checker_.CalledOnValidThread());
446 560
447 ContentSetting setting = GetContentSetting( 561 ContentSetting setting = GetContentSetting(
448 primary_url, secondary_url, content_type, resource_identifier); 562 primary_url, secondary_url, content_type, resource_identifier);
449 if (setting == CONTENT_SETTING_ALLOW) { 563 if (setting == CONTENT_SETTING_ALLOW) {
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
769 rule.secondary_pattern.Matches(secondary_url)) { 883 rule.secondary_pattern.Matches(secondary_url)) {
770 if (primary_pattern) 884 if (primary_pattern)
771 *primary_pattern = rule.primary_pattern; 885 *primary_pattern = rule.primary_pattern;
772 if (secondary_pattern) 886 if (secondary_pattern)
773 *secondary_pattern = rule.secondary_pattern; 887 *secondary_pattern = rule.secondary_pattern;
774 return make_scoped_ptr(rule.value.get()->DeepCopy()); 888 return make_scoped_ptr(rule.value.get()->DeepCopy());
775 } 889 }
776 } 890 }
777 return scoped_ptr<base::Value>(); 891 return scoped_ptr<base::Value>();
778 } 892 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698