OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/content_settings/content_settings_override_provider.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/auto_reset.h" | |
10 #include "base/prefs/pref_service.h" | |
11 #include "base/prefs/scoped_user_pref_update.h" | |
12 #include "base/values.h" | |
13 #include "chrome/browser/content_settings/content_settings_utils.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "components/content_settings/core/browser/content_settings_rule.h" | |
16 #include "components/content_settings/core/common/content_settings.h" | |
17 #include "components/content_settings/core/common/content_settings_pattern.h" | |
18 #include "components/pref_registry/pref_registry_syncable.h" | |
19 | |
20 namespace content_settings { | |
21 | |
22 namespace { | |
23 | |
24 class OverrideRuleIterator : public RuleIterator { | |
25 public: | |
26 explicit OverrideRuleIterator(bool is_allowed) : is_done_(is_allowed) {} | |
27 | |
28 bool HasNext() const override { return !is_done_; } | |
29 | |
30 Rule Next() override { | |
31 DCHECK(!is_done_); | |
32 is_done_ = true; | |
33 return Rule(ContentSettingsPattern::Wildcard(), | |
34 ContentSettingsPattern::Wildcard(), | |
35 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
36 } | |
37 | |
38 private: | |
39 bool is_done_; | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 // static | |
45 void OverrideProvider::RegisterProfilePrefs( | |
46 user_prefs::PrefRegistrySyncable* registry) { | |
47 registry->RegisterDictionaryPref( | |
48 prefs::kOverrideContentSettings, | |
49 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
50 } | |
51 | |
52 OverrideProvider::OverrideProvider(PrefService* prefs, bool incognito) | |
53 : prefs_(prefs), is_incognito_(incognito) { | |
54 DCHECK(prefs_); | |
55 | |
56 // Read global overrides. | |
57 ReadOverrideSettings(); | |
58 } | |
59 | |
60 OverrideProvider::~OverrideProvider() { | |
61 } | |
62 | |
63 RuleIterator* OverrideProvider::GetRuleIterator( | |
64 ContentSettingsType content_type, | |
65 const ResourceIdentifier& resource_identifier, | |
66 bool incognito) const { | |
67 base::AutoLock lock(lock_); | |
68 if (resource_identifier.empty()) { | |
69 return new OverrideRuleIterator(allowed_settings_[content_type]); | |
70 } | |
71 return new EmptyRuleIterator(); | |
72 } | |
73 | |
74 void OverrideProvider::ClearAllContentSettingsRules( | |
75 ContentSettingsType content_type) { | |
76 } | |
77 | |
78 bool OverrideProvider::SetWebsiteSetting( | |
79 const ContentSettingsPattern& primary_pattern, | |
80 const ContentSettingsPattern& secondary_pattern, | |
81 ContentSettingsType content_type, | |
82 const ResourceIdentifier& resource_identifier, | |
83 base::Value* in_value) { | |
84 return false; | |
85 } | |
86 | |
87 void OverrideProvider::ShutdownOnUIThread() { | |
88 DCHECK(prefs_); | |
89 prefs_ = NULL; | |
90 } | |
91 | |
92 void OverrideProvider::SetOverrideSetting(ContentSettingsType content_type, | |
93 bool enabled) { | |
94 DCHECK(thread_checker_.CalledOnValidThread()); | |
95 DCHECK(prefs_); | |
96 | |
97 // Disallow incognito to change the state. | |
98 DCHECK(!is_incognito_); | |
99 | |
100 base::AutoLock lock(lock_); | |
101 DictionaryPrefUpdate update(prefs_, prefs::kOverrideContentSettings); | |
102 base::DictionaryValue* default_settings_dictionary = update.Get(); | |
103 if (enabled) { | |
104 allowed_settings_[content_type] = true; | |
105 default_settings_dictionary->RemoveWithoutPathExpansion( | |
106 GetTypeName(content_type), NULL); | |
107 } else { | |
108 allowed_settings_[content_type] = false; | |
109 default_settings_dictionary->SetWithoutPathExpansion( | |
110 GetTypeName(content_type), new base::FundamentalValue(true)); | |
111 } | |
112 } | |
113 | |
114 bool OverrideProvider::IsEnabled(ContentSettingsType content_type) const { | |
115 base::AutoLock lock(lock_); | |
116 return allowed_settings_[content_type]; | |
117 } | |
118 | |
119 void OverrideProvider::ReadOverrideSettings() { | |
120 const base::DictionaryValue* blocked_settings_dictionary = | |
121 prefs_->GetDictionary(prefs::kOverrideContentSettings); | |
122 | |
123 for (int type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { | |
124 ContentSettingsType content_setting = ContentSettingsType(type); | |
125 allowed_settings_[content_setting] = | |
126 !blocked_settings_dictionary->HasKey(GetTypeName(content_setting)); | |
127 } | |
128 } | |
129 | |
130 } // namespace content_settings | |
OLD | NEW |