1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/content_settings/content_settings_pref_provider.h"
#include <list>
#include <string>
#include <utility>
#include <vector>
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/content_settings/content_settings_pattern.h"
#include "chrome/browser/content_settings/content_settings_utils.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/content_settings.h"
#include "chrome/common/pref_names.h"
#include "content/browser/browser_thread.h"
#include "content/browser/user_metrics.h"
#include "content/common/notification_service.h"
#include "content/common/notification_source.h"
#include "googleurl/src/gurl.h"
#include "net/base/net_util.h"
namespace {
// The preference keys where resource identifiers are stored for
// ContentSettingsType values that support resource identifiers.
const char* kResourceTypeNames[] = {
NULL,
NULL,
NULL,
"per_plugin",
NULL,
NULL, // Not used for Geolocation
NULL, // Not used for Notifications
};
COMPILE_ASSERT(arraysize(kResourceTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
resource_type_names_incorrect_size);
// The default setting for each content type.
const ContentSetting kDefaultSettings[] = {
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_COOKIES
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_IMAGES
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_JAVASCRIPT
CONTENT_SETTING_ALLOW, // CONTENT_SETTINGS_TYPE_PLUGINS
CONTENT_SETTING_BLOCK, // CONTENT_SETTINGS_TYPE_POPUPS
CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_GEOLOCATION
CONTENT_SETTING_ASK, // CONTENT_SETTINGS_TYPE_NOTIFICATIONS
};
COMPILE_ASSERT(arraysize(kDefaultSettings) == CONTENT_SETTINGS_NUM_TYPES,
default_settings_incorrect_size);
// The names of the ContentSettingsType values, for use with dictionary prefs.
const char* kTypeNames[] = {
"cookies",
"images",
"javascript",
"plugins",
"popups",
// TODO(markusheintz): Refactoring in progress. Content settings exceptions
// for notifications and geolocation will be added next.
"geolocation", // Only used for default Geolocation settings
"notifications", // Only used for default Notifications settings.
};
COMPILE_ASSERT(arraysize(kTypeNames) == CONTENT_SETTINGS_NUM_TYPES,
type_names_incorrect_size);
void SetDefaultContentSettings(DictionaryValue* default_settings) {
default_settings->Clear();
for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
if (kTypeNames[i] != NULL) {
default_settings->SetInteger(kTypeNames[i],
kDefaultSettings[i]);
}
}
}
const char* kPatternSeparator = ",";
std::string CreatePatternString(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern) {
return primary_pattern.ToString() +
std::string(kPatternSeparator) +
secondary_pattern.ToString();
}
std::pair<ContentSettingsPattern, ContentSettingsPattern>
ParsePatternString(const std::string& pattern_str) {
DCHECK(!pattern_str.empty());
size_t pos = pattern_str.find(kPatternSeparator);
std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair;
if (pos == std::string::npos) {
pattern_pair.first = ContentSettingsPattern::FromString(pattern_str);
DCHECK(pattern_pair.first.IsValid());
pattern_pair.second = ContentSettingsPattern();
} else {
pattern_pair.first = ContentSettingsPattern::FromString(
pattern_str.substr(0, pos));
DCHECK(pattern_pair.first.IsValid());
pattern_pair.second = ContentSettingsPattern::FromString(
pattern_str.substr(pos+1, pattern_str.size() - pos - 1));
DCHECK(pattern_pair.second.IsValid());
}
return pattern_pair;
}
ContentSetting ValueToContentSetting(Value* value) {
int int_value;
value->GetAsInteger(&int_value);
return IntToContentSetting(int_value);
}
ContentSettingsType StringToContentSettingsType(
const std::string& content_type_str) {
for (size_t type = 0; type < arraysize(kTypeNames); ++type) {
if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type_str))
return ContentSettingsType(type);
}
for (size_t type = 0; type < arraysize(kResourceTypeNames); ++type) {
if ((kResourceTypeNames[type] != NULL) &&
(kResourceTypeNames[type] == content_type_str)) {
return ContentSettingsType(type);
}
}
return CONTENT_SETTINGS_TYPE_DEFAULT;
}
ContentSetting FixObsoleteCookiePromptMode(ContentSettingsType content_type,
ContentSetting setting) {
if (content_type == CONTENT_SETTINGS_TYPE_COOKIES &&
setting == CONTENT_SETTING_ASK) {
return CONTENT_SETTING_BLOCK;
}
return setting;
}
} // namespace
namespace content_settings {
PrefDefaultProvider::PrefDefaultProvider(PrefService* prefs,
bool incognito)
: prefs_(prefs),
is_incognito_(incognito),
updating_preferences_(false) {
DCHECK(prefs_);
MigrateObsoleteNotificationPref();
MigrateObsoleteGeolocationPref();
// Read global defaults.
DCHECK_EQ(arraysize(kTypeNames),
static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
ReadDefaultSettings(true);
if (default_content_settings_.settings[CONTENT_SETTINGS_TYPE_COOKIES] ==
CONTENT_SETTING_BLOCK) {
UserMetrics::RecordAction(
UserMetricsAction("CookieBlockingEnabledPerDefault"));
} else {
UserMetrics::RecordAction(
UserMetricsAction("CookieBlockingDisabledPerDefault"));
}
pref_change_registrar_.Init(prefs_);
pref_change_registrar_.Add(prefs::kDefaultContentSettings, this);
pref_change_registrar_.Add(prefs::kGeolocationDefaultContentSetting, this);
}
PrefDefaultProvider::~PrefDefaultProvider() {
DCHECK(!prefs_);
}
ContentSetting PrefDefaultProvider::ProvideDefaultSetting(
ContentSettingsType content_type) const {
base::AutoLock lock(lock_);
return default_content_settings_.settings[content_type];
}
void PrefDefaultProvider::UpdateDefaultSetting(
ContentSettingsType content_type,
ContentSetting setting) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prefs_);
DCHECK(kTypeNames[content_type] != NULL);
// The default settings may not be directly modified for OTR sessions.
// Instead, they are synced to the main profile's setting.
if (is_incognito_)
return;
std::string dictionary_path(kTypeNames[content_type]);
{
AutoReset<bool> auto_reset(&updating_preferences_, true);
base::AutoLock lock(lock_);
DictionaryPrefUpdate update(prefs_, prefs::kDefaultContentSettings);
DictionaryValue* default_settings_dictionary = update.Get();
if ((setting == CONTENT_SETTING_DEFAULT) ||
(setting == kDefaultSettings[content_type])) {
default_content_settings_.settings[content_type] =
kDefaultSettings[content_type];
default_settings_dictionary->RemoveWithoutPathExpansion(dictionary_path,
NULL);
} else {
default_content_settings_.settings[content_type] = setting;
default_settings_dictionary->SetWithoutPathExpansion(
dictionary_path, Value::CreateIntegerValue(setting));
}
// Keep the obsolete pref in sync as long as backwards compatibility is
// required. This is required to keep sync working correctly.
if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
prefs_->SetInteger(prefs::kGeolocationDefaultContentSetting,
setting == CONTENT_SETTING_DEFAULT ?
kDefaultSettings[content_type] : setting);
}
}
NotifyObservers(ContentSettingsPattern(),
ContentSettingsPattern(),
content_type,
std::string());
}
bool PrefDefaultProvider::DefaultSettingIsManaged(
ContentSettingsType content_type) const {
return false;
}
void PrefDefaultProvider::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (type == chrome::NOTIFICATION_PREF_CHANGED) {
DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
if (updating_preferences_)
return;
std::string* name = Details<std::string>(details).ptr();
if (*name == prefs::kDefaultContentSettings) {
ReadDefaultSettings(true);
} else if (*name == prefs::kGeolocationDefaultContentSetting) {
MigrateObsoleteGeolocationPref();
// Return and don't send a notifications. Migrating the obsolete
// geolocation pref will change the prefs::kDefaultContentSettings and
// cause the notification to be fired.
return;
} else {
NOTREACHED() << "Unexpected preference observed";
return;
}
NotifyObservers(ContentSettingsPattern(),
ContentSettingsPattern(),
CONTENT_SETTINGS_TYPE_DEFAULT,
std::string());
} else {
NOTREACHED() << "Unexpected notification";
}
}
void PrefDefaultProvider::ShutdownOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prefs_);
RemoveAllObservers();
pref_change_registrar_.RemoveAll();
prefs_ = NULL;
}
void PrefDefaultProvider::ReadDefaultSettings(bool overwrite) {
const DictionaryValue* default_settings_dictionary =
prefs_->GetDictionary(prefs::kDefaultContentSettings);
base::AutoLock lock(lock_);
if (overwrite)
default_content_settings_ = ContentSettings();
// Careful: The returned value could be NULL if the pref has never been set.
if (default_settings_dictionary != NULL) {
GetSettingsFromDictionary(default_settings_dictionary,
&default_content_settings_);
}
ForceDefaultsToBeExplicit();
}
void PrefDefaultProvider::ForceDefaultsToBeExplicit() {
DCHECK_EQ(arraysize(kDefaultSettings),
static_cast<size_t>(CONTENT_SETTINGS_NUM_TYPES));
for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
if (default_content_settings_.settings[i] == CONTENT_SETTING_DEFAULT)
default_content_settings_.settings[i] = kDefaultSettings[i];
}
}
void PrefDefaultProvider::GetSettingsFromDictionary(
const DictionaryValue* dictionary,
ContentSettings* settings) {
for (DictionaryValue::key_iterator i(dictionary->begin_keys());
i != dictionary->end_keys(); ++i) {
const std::string& content_type(*i);
for (size_t type = 0; type < arraysize(kTypeNames); ++type) {
if ((kTypeNames[type] != NULL) && (kTypeNames[type] == content_type)) {
int setting = CONTENT_SETTING_DEFAULT;
bool found = dictionary->GetIntegerWithoutPathExpansion(content_type,
&setting);
DCHECK(found);
settings->settings[type] = IntToContentSetting(setting);
break;
}
}
}
// Migrate obsolete cookie prompt mode.
if (settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] ==
CONTENT_SETTING_ASK)
settings->settings[CONTENT_SETTINGS_TYPE_COOKIES] = CONTENT_SETTING_BLOCK;
settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS] =
ClickToPlayFixup(CONTENT_SETTINGS_TYPE_PLUGINS,
settings->settings[CONTENT_SETTINGS_TYPE_PLUGINS]);
}
void PrefDefaultProvider::MigrateObsoleteNotificationPref() {
if (prefs_->HasPrefPath(prefs::kDesktopNotificationDefaultContentSetting)) {
ContentSetting setting = IntToContentSetting(
prefs_->GetInteger(prefs::kDesktopNotificationDefaultContentSetting));
UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting);
prefs_->ClearPref(prefs::kDesktopNotificationDefaultContentSetting);
}
}
void PrefDefaultProvider::MigrateObsoleteGeolocationPref() {
if (prefs_->HasPrefPath(prefs::kGeolocationDefaultContentSetting)) {
ContentSetting setting = IntToContentSetting(
prefs_->GetInteger(prefs::kGeolocationDefaultContentSetting));
UpdateDefaultSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION, setting);
// Do not clear the old preference yet as long as we need to maintain
// backward compatibility.
}
}
// static
void PrefDefaultProvider::RegisterUserPrefs(PrefService* prefs) {
// The registration of the preference prefs::kDefaultContentSettings should
// also include the default values for default content settings. This allows
// functional tests to get default content settings by reading the preference
// prefs::kDefaultContentSettings via pyauto.
// TODO(markusheintz): Write pyauto hooks for the content settings map as
// content settings should be read from the host content settings map.
DictionaryValue* default_content_settings = new DictionaryValue();
SetDefaultContentSettings(default_content_settings);
prefs->RegisterDictionaryPref(prefs::kDefaultContentSettings,
default_content_settings,
PrefService::SYNCABLE_PREF);
// Obsolete prefs, for migrations:
prefs->RegisterIntegerPref(
prefs::kDesktopNotificationDefaultContentSetting,
kDefaultSettings[CONTENT_SETTINGS_TYPE_NOTIFICATIONS],
PrefService::SYNCABLE_PREF);
prefs->RegisterIntegerPref(
prefs::kGeolocationDefaultContentSetting,
kDefaultSettings[CONTENT_SETTINGS_TYPE_GEOLOCATION],
PrefService::UNSYNCABLE_PREF);
}
// ////////////////////////////////////////////////////////////////////////////
// PrefProvider:
//
// static
void PrefProvider::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterIntegerPref(
prefs::kContentSettingsVersion,
ContentSettingsPattern::kContentSettingsPatternVersion,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterDictionaryPref(prefs::kContentSettingsPatternPairs,
PrefService::SYNCABLE_PREF);
// Obsolete prefs, for migration:
prefs->RegisterDictionaryPref(prefs::kContentSettingsPatterns,
PrefService::SYNCABLE_PREF);
prefs->RegisterListPref(prefs::kPopupWhitelistedHosts,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterDictionaryPref(prefs::kPerHostContentSettings,
PrefService::UNSYNCABLE_PREF);
}
PrefProvider::PrefProvider(PrefService* prefs,
bool incognito)
: prefs_(prefs),
is_incognito_(incognito),
updating_preferences_(false) {
DCHECK(prefs_);
if (!is_incognito_) {
// Migrate obsolete preferences.
MigrateObsoletePerhostPref();
MigrateObsoletePopupsPref();
MigrateObsoleteContentSettingsPatternPref();
}
// Verify preferences version.
if (!prefs_->HasPrefPath(prefs::kContentSettingsVersion)) {
prefs_->SetInteger(prefs::kContentSettingsVersion,
ContentSettingsPattern::kContentSettingsPatternVersion);
}
if (prefs_->GetInteger(prefs::kContentSettingsVersion) >
ContentSettingsPattern::kContentSettingsPatternVersion) {
return;
}
// Read content settings exceptions.
ReadContentSettingsFromPref(false);
if (!is_incognito_) {
UMA_HISTOGRAM_COUNTS("ContentSettings.NumberOfExceptions",
value_map_.size());
}
pref_change_registrar_.Init(prefs_);
pref_change_registrar_.Add(prefs::kContentSettingsPatterns, this);
pref_change_registrar_.Add(prefs::kContentSettingsPatternPairs, this);
}
ContentSetting PrefProvider::GetContentSetting(
const GURL& primary_url,
const GURL& secondary_url,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier) const {
// For a |PrefProvider| used in a |HostContentSettingsMap| of a non incognito
// profile, this will always return NULL.
// TODO(markusheintz): I don't like this. I'd like to have an
// IncognitoProviderWrapper that wrapps the pref provider for a host content
// settings map of an incognito profile.
base::AutoLock auto_lock(lock_);
Value* incognito_value = incognito_value_map_.GetValue(
primary_url,
secondary_url,
content_type,
resource_identifier);
if (incognito_value)
return ValueToContentSetting(incognito_value);
Value* value = value_map_.GetValue(
primary_url,
secondary_url,
content_type,
resource_identifier);
if (value)
return ValueToContentSetting(value);
return CONTENT_SETTING_DEFAULT;
}
void PrefProvider::GetAllContentSettingsRules(
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
Rules* content_setting_rules) const {
DCHECK(content_setting_rules);
content_setting_rules->clear();
const OriginIdentifierValueMap* map_to_return =
is_incognito_ ? &incognito_value_map_ : &value_map_;
base::AutoLock auto_lock(lock_);
for (OriginIdentifierValueMap::const_iterator entry = map_to_return->begin();
entry != map_to_return->end();
++entry) {
if (entry->content_type == content_type &&
entry->identifier == resource_identifier) {
ContentSetting setting = ValueToContentSetting(entry->value.get());
DCHECK(setting != CONTENT_SETTING_DEFAULT);
Rule new_rule(entry->primary_pattern,
entry->secondary_pattern,
setting);
content_setting_rules->push_back(new_rule);
}
}
}
void PrefProvider::SetContentSetting(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
ContentSetting setting) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prefs_);
DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
// Update in memory value map.
OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
if (!is_incognito_)
map_to_modify = &value_map_;
{
base::AutoLock auto_lock(lock_);
if (setting == CONTENT_SETTING_DEFAULT) {
map_to_modify->DeleteValue(
primary_pattern,
secondary_pattern,
content_type,
resource_identifier);
} else {
map_to_modify->SetValue(
primary_pattern,
secondary_pattern,
content_type,
resource_identifier,
Value::CreateIntegerValue(setting));
}
}
// Update the content settings preference.
if (!is_incognito_) {
UpdatePref(primary_pattern,
secondary_pattern,
content_type,
resource_identifier,
setting);
}
NotifyObservers(
primary_pattern, secondary_pattern, content_type, resource_identifier);
}
void PrefProvider::ClearAllContentSettingsRules(
ContentSettingsType content_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(kTypeNames[content_type] != NULL); // Don't call this for Geolocation.
DCHECK(prefs_);
OriginIdentifierValueMap* map_to_modify = &incognito_value_map_;
if (!is_incognito_)
map_to_modify = &value_map_;
{
base::AutoLock auto_lock(lock_);
OriginIdentifierValueMap::iterator entry(map_to_modify->begin());
while (entry != map_to_modify->end()) {
if (entry->content_type == content_type) {
UpdatePref(
entry->primary_pattern,
entry->secondary_pattern,
entry->content_type,
entry->identifier,
CONTENT_SETTING_DEFAULT);
// Delete current |entry| and set |entry| to the next value map entry.
entry = map_to_modify->erase(entry);
} else {
++entry;
}
}
}
NotifyObservers(ContentSettingsPattern(),
ContentSettingsPattern(),
content_type,
std::string());
}
void PrefProvider::Observe(
int type,
const NotificationSource& source,
const NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (type == chrome::NOTIFICATION_PREF_CHANGED) {
DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
if (updating_preferences_)
return;
std::string* name = Details<std::string>(details).ptr();
if (*name == prefs::kContentSettingsPatternPairs) {
SyncObsoletePref();
ReadContentSettingsFromPref(true);
} else if (*name == prefs::kContentSettingsPatterns) {
AutoReset<bool> auto_reset(&updating_preferences_, true);
MigrateObsoleteContentSettingsPatternPref();
ReadContentSettingsFromPref(true);
} else {
NOTREACHED() << "Unexpected preference observed";
return;
}
NotifyObservers(ContentSettingsPattern(),
ContentSettingsPattern(),
CONTENT_SETTINGS_TYPE_DEFAULT,
std::string());
} else {
NOTREACHED() << "Unexpected notification";
}
}
PrefProvider::~PrefProvider() {
DCHECK(!prefs_);
}
// ////////////////////////////////////////////////////////////////////////////
// Private
void PrefProvider::UpdatePref(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
ContentSetting setting) {
AutoReset<bool> auto_reset(&updating_preferences_, true);
UpdatePatternPairsPref(primary_pattern,
secondary_pattern,
content_type,
resource_identifier,
setting);
UpdatePatternsPref(primary_pattern,
secondary_pattern,
content_type,
resource_identifier,
setting);
}
void PrefProvider::ReadContentSettingsFromPref(bool overwrite) {
base::AutoLock auto_lock(lock_);
const DictionaryValue* all_settings_dictionary =
prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
if (overwrite)
value_map_.clear();
AutoReset<bool> auto_reset(&updating_preferences_, true);
// Careful: The returned value could be NULL if the pref has never been set.
if (all_settings_dictionary != NULL) {
DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
DictionaryValue* mutable_settings;
scoped_ptr<DictionaryValue> mutable_settings_scope;
if (!is_incognito_) {
mutable_settings = update.Get();
} else {
// Create copy as we do not want to persist anything in OTR prefs.
mutable_settings = all_settings_dictionary->DeepCopy();
mutable_settings_scope.reset(mutable_settings);
}
// Convert all Unicode patterns into punycode form, then read.
CanonicalizeContentSettingsExceptions(mutable_settings);
for (DictionaryValue::key_iterator i(mutable_settings->begin_keys());
i != mutable_settings->end_keys(); ++i) {
const std::string& pattern_str(*i);
std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
ParsePatternString(pattern_str);
if (!pattern_pair.first.IsValid() ||
!pattern_pair.second.IsValid()) {
LOG(DFATAL) << "Invalid pattern strings: " << pattern_str;
continue;
}
// Get settings dictionary for the current pattern string, and read
// settings from the dictionary.
DictionaryValue* settings_dictionary = NULL;
bool found = mutable_settings->GetDictionaryWithoutPathExpansion(
pattern_str, &settings_dictionary);
DCHECK(found);
for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
ContentSettingsType content_type = static_cast<ContentSettingsType>(i);
if (RequiresResourceIdentifier(content_type)) {
const std::string content_type_str = kResourceTypeNames[i];
DictionaryValue* resource_dictionary = NULL;
if (settings_dictionary->GetDictionary(
content_type_str, &resource_dictionary)) {
for (DictionaryValue::key_iterator j(
resource_dictionary->begin_keys());
j != resource_dictionary->end_keys();
++j) {
const std::string& resource_identifier(*j);
int setting = CONTENT_SETTING_DEFAULT;
found = resource_dictionary->GetIntegerWithoutPathExpansion(
resource_identifier, &setting);
DCHECK_NE(CONTENT_SETTING_DEFAULT, setting);
setting = ClickToPlayFixup(content_type,
ContentSetting(setting));
value_map_.SetValue(pattern_pair.first,
pattern_pair.second,
content_type,
resource_identifier,
Value::CreateIntegerValue(setting));
}
}
} else if (kTypeNames[i]) {
const std::string content_type_str(kTypeNames[i]);
int setting = CONTENT_SETTING_DEFAULT;
if (settings_dictionary->GetIntegerWithoutPathExpansion(
content_type_str, &setting)) {
DCHECK_NE(CONTENT_SETTING_DEFAULT, setting);
setting = FixObsoleteCookiePromptMode(content_type,
ContentSetting(setting));
setting = ClickToPlayFixup(content_type,
ContentSetting(setting));
value_map_.SetValue(pattern_pair.first,
pattern_pair.second,
content_type,
ResourceIdentifier(""),
Value::CreateIntegerValue(setting));
}
}
}
}
}
}
void PrefProvider::UpdatePatternsPref(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
ContentSetting setting) {
DictionaryPrefUpdate update(prefs_,
prefs::kContentSettingsPatterns);
DictionaryValue* all_settings_dictionary = update.Get();
// Get settings dictionary for |primary_pattern|.
std::string pattern_str(primary_pattern.ToString());
DictionaryValue* settings_dictionary = NULL;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
pattern_str, &settings_dictionary);
if (!found && (setting != CONTENT_SETTING_DEFAULT)) {
settings_dictionary = new DictionaryValue;
all_settings_dictionary->SetWithoutPathExpansion(
pattern_str, settings_dictionary);
}
if (settings_dictionary) {
if (RequiresResourceIdentifier(content_type)) {
// Get resource dictionary.
std::string res_dictionary_path(kResourceTypeNames[content_type]);
DictionaryValue* resource_dictionary = NULL;
found = settings_dictionary->GetDictionary(
res_dictionary_path, &resource_dictionary);
if (!found) {
if (setting == CONTENT_SETTING_DEFAULT)
return; // Nothing to remove. Exit early.
resource_dictionary = new DictionaryValue;
settings_dictionary->Set(res_dictionary_path, resource_dictionary);
}
// Update resource dictionary.
if (setting == CONTENT_SETTING_DEFAULT) {
resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
NULL);
if (resource_dictionary->empty()) {
settings_dictionary->RemoveWithoutPathExpansion(
res_dictionary_path, NULL);
}
} else {
resource_dictionary->SetWithoutPathExpansion(
resource_identifier, Value::CreateIntegerValue(setting));
}
} else {
// Update settings dictionary.
std::string setting_path(kTypeNames[content_type]);
if (setting == CONTENT_SETTING_DEFAULT) {
settings_dictionary->RemoveWithoutPathExpansion(setting_path,
NULL);
} else {
settings_dictionary->SetWithoutPathExpansion(
setting_path, Value::CreateIntegerValue(setting));
}
}
// Remove the settings dictionary if it is empty.
if (settings_dictionary->empty()) {
all_settings_dictionary->RemoveWithoutPathExpansion(
pattern_str, NULL);
}
}
}
void PrefProvider::UpdatePatternPairsPref(
const ContentSettingsPattern& primary_pattern,
const ContentSettingsPattern& secondary_pattern,
ContentSettingsType content_type,
const ResourceIdentifier& resource_identifier,
ContentSetting setting) {
DictionaryPrefUpdate update(prefs_,
prefs::kContentSettingsPatternPairs);
DictionaryValue* all_settings_dictionary = update.Get();
// Get settings dictionary for the given patterns.
std::string pattern_str(CreatePatternString(primary_pattern,
secondary_pattern));
DictionaryValue* settings_dictionary = NULL;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
pattern_str, &settings_dictionary);
if (!found && (setting != CONTENT_SETTING_DEFAULT)) {
settings_dictionary = new DictionaryValue;
all_settings_dictionary->SetWithoutPathExpansion(
pattern_str, settings_dictionary);
}
if (settings_dictionary) {
if (RequiresResourceIdentifier(content_type)) {
// Get resource dictionary.
std::string res_dictionary_path(kResourceTypeNames[content_type]);
DictionaryValue* resource_dictionary = NULL;
found = settings_dictionary->GetDictionary(
res_dictionary_path, &resource_dictionary);
if (!found) {
if (setting == CONTENT_SETTING_DEFAULT)
return; // Nothing to remove. Exit early.
resource_dictionary = new DictionaryValue;
settings_dictionary->Set(res_dictionary_path, resource_dictionary);
}
// Update resource dictionary.
if (setting == CONTENT_SETTING_DEFAULT) {
resource_dictionary->RemoveWithoutPathExpansion(resource_identifier,
NULL);
if (resource_dictionary->empty()) {
settings_dictionary->RemoveWithoutPathExpansion(
res_dictionary_path, NULL);
}
} else {
resource_dictionary->SetWithoutPathExpansion(
resource_identifier, Value::CreateIntegerValue(setting));
}
} else {
// Update settings dictionary.
std::string setting_path(kTypeNames[content_type]);
if (setting == CONTENT_SETTING_DEFAULT) {
settings_dictionary->RemoveWithoutPathExpansion(setting_path,
NULL);
} else {
settings_dictionary->SetWithoutPathExpansion(
setting_path, Value::CreateIntegerValue(setting));
}
}
// Remove the settings dictionary if it is empty.
if (settings_dictionary->empty()) {
all_settings_dictionary->RemoveWithoutPathExpansion(
pattern_str, NULL);
}
}
}
// static
void PrefProvider::CanonicalizeContentSettingsExceptions(
DictionaryValue* all_settings_dictionary) {
DCHECK(all_settings_dictionary);
std::vector<std::string> remove_items;
std::vector<std::pair<std::string, std::string> > move_items;
for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
i != all_settings_dictionary->end_keys(); ++i) {
const std::string& pattern_str(*i);
std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
ParsePatternString(pattern_str);
const std::string canonicalized_pattern_str = CreatePatternString(
pattern_pair.first, pattern_pair.second);
if (canonicalized_pattern_str.empty() ||
canonicalized_pattern_str == pattern_str)
continue;
// Clear old pattern if prefs already have canonicalized pattern.
DictionaryValue* new_pattern_settings_dictionary = NULL;
if (all_settings_dictionary->GetDictionaryWithoutPathExpansion(
canonicalized_pattern_str, &new_pattern_settings_dictionary)) {
remove_items.push_back(pattern_str);
continue;
}
// Move old pattern to canonicalized pattern.
DictionaryValue* old_pattern_settings_dictionary = NULL;
if (all_settings_dictionary->GetDictionaryWithoutPathExpansion(
pattern_str, &old_pattern_settings_dictionary)) {
move_items.push_back(
std::make_pair(pattern_str, canonicalized_pattern_str));
}
}
for (size_t i = 0; i < remove_items.size(); ++i) {
all_settings_dictionary->RemoveWithoutPathExpansion(remove_items[i], NULL);
}
for (size_t i = 0; i < move_items.size(); ++i) {
Value* pattern_settings_dictionary = NULL;
all_settings_dictionary->RemoveWithoutPathExpansion(
move_items[i].first, &pattern_settings_dictionary);
all_settings_dictionary->SetWithoutPathExpansion(
move_items[i].second, pattern_settings_dictionary);
}
}
void PrefProvider::ShutdownOnUIThread() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prefs_);
RemoveAllObservers();
pref_change_registrar_.RemoveAll();
prefs_ = NULL;
}
void PrefProvider::MigrateObsoletePerhostPref() {
if (prefs_->HasPrefPath(prefs::kPerHostContentSettings)) {
const DictionaryValue* all_settings_dictionary =
prefs_->GetDictionary(prefs::kPerHostContentSettings);
DCHECK(all_settings_dictionary);
for (DictionaryValue::key_iterator
i(all_settings_dictionary->begin_keys());
i != all_settings_dictionary->end_keys(); ++i) {
const std::string& host(*i);
ContentSettingsPattern pattern =
ContentSettingsPattern::FromString(
std::string(ContentSettingsPattern::kDomainWildcard) + host);
DictionaryValue* host_settings_dictionary = NULL;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
host, &host_settings_dictionary);
DCHECK(found);
for (DictionaryValue::key_iterator i(
host_settings_dictionary->begin_keys());
i != host_settings_dictionary->end_keys();
++i) {
const std::string& content_type_str(*i);
ContentSettingsType content_type =
StringToContentSettingsType(content_type_str);
if (content_type == CONTENT_SETTINGS_TYPE_DEFAULT) {
NOTREACHED();
LOG(DFATAL) << "Skip settings for invalid content settings type '"
<< content_type_str << "'";
continue;
}
if (!RequiresResourceIdentifier(content_type)) {
int setting_int_value = CONTENT_SETTING_DEFAULT;
bool found =
host_settings_dictionary->GetIntegerWithoutPathExpansion(
content_type_str, &setting_int_value);
DCHECK(found);
ContentSetting setting = IntToContentSetting(setting_int_value);
setting = FixObsoleteCookiePromptMode(content_type, setting);
setting = ClickToPlayFixup(content_type, setting);
// TODO(markusheintz): Maybe this check can be removed.
if (setting != CONTENT_SETTING_DEFAULT) {
SetContentSetting(
pattern,
pattern,
content_type,
"",
setting);
}
}
}
}
prefs_->ClearPref(prefs::kPerHostContentSettings);
}
}
void PrefProvider::MigrateObsoletePopupsPref() {
if (prefs_->HasPrefPath(prefs::kPopupWhitelistedHosts)) {
const ListValue* whitelist_pref =
prefs_->GetList(prefs::kPopupWhitelistedHosts);
for (ListValue::const_iterator i(whitelist_pref->begin());
i != whitelist_pref->end(); ++i) {
std::string host;
(*i)->GetAsString(&host);
SetContentSetting(ContentSettingsPattern::FromString(host),
ContentSettingsPattern::FromString(host),
CONTENT_SETTINGS_TYPE_POPUPS,
"",
CONTENT_SETTING_ALLOW);
}
prefs_->ClearPref(prefs::kPopupWhitelistedHosts);
}
}
void PrefProvider::MigrateObsoleteContentSettingsPatternPref() {
if (prefs_->HasPrefPath(prefs::kContentSettingsPatterns) && !is_incognito_) {
const DictionaryValue* all_settings_dictionary =
prefs_->GetDictionary(prefs::kContentSettingsPatterns);
DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatternPairs);
DictionaryValue* exceptions_dictionary;
exceptions_dictionary = update.Get();
for (DictionaryValue::key_iterator i(all_settings_dictionary->begin_keys());
i != all_settings_dictionary->end_keys();
++i) {
const std::string& key(*i);
if (key.empty())
continue;
// Validate pattern string and skip it if it is invalid.
std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
ParsePatternString(key);
ContentSettingsPattern primary_pattern = pattern_pair.first;
if (!primary_pattern.IsValid())
continue;
// Copy dictionary value.
// Get old settings.
DictionaryValue* dictionary = NULL;
bool found = all_settings_dictionary->GetDictionaryWithoutPathExpansion(
key, &dictionary);
DCHECK(found);
// Create new dictionary key.
std::string new_pattern_str = CreatePatternString(
primary_pattern, ContentSettingsPattern::Wildcard());
// Existing values are overwritten.
exceptions_dictionary->SetWithoutPathExpansion(
new_pattern_str, dictionary->DeepCopy());
}
}
}
void PrefProvider::SyncObsoletePref() {
AutoReset<bool> auto_reset(&updating_preferences_, true);
if (prefs_->HasPrefPath(prefs::kContentSettingsPatternPairs) &&
!is_incognito_) {
const DictionaryValue* pattern_pairs_dictionary =
prefs_->GetDictionary(prefs::kContentSettingsPatternPairs);
DictionaryPrefUpdate update(prefs_, prefs::kContentSettingsPatterns);
DictionaryValue* obsolete_settings_dictionary = update.Get();
for (DictionaryValue::key_iterator i =
pattern_pairs_dictionary->begin_keys();
i != pattern_pairs_dictionary->end_keys();
++i) {
const std::string& key(*i);
// Validate pattern string and skip it if it is invalid.
std::pair<ContentSettingsPattern, ContentSettingsPattern> pattern_pair =
ParsePatternString(key);
if (!pattern_pair.first.IsValid() || !pattern_pair.second.IsValid())
continue;
// Copy dictionary
DictionaryValue* dictionary = NULL;
bool found = pattern_pairs_dictionary->GetDictionaryWithoutPathExpansion(
key, &dictionary);
DCHECK(found);
std::string new_key = pattern_pair.first.ToString();
// Existing values are overwritten.
obsolete_settings_dictionary->SetWithoutPathExpansion(
new_key, dictionary->DeepCopy());
}
}
}
} // namespace content_settings
|