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_rule.h" | |
14 #include "chrome/browser/content_settings/content_settings_utils.h" | |
15 #include "chrome/common/content_settings.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "components/content_settings/core/common/content_settings_pattern.h" | |
18 #include "components/pref_registry/pref_registry_syncable.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 using content::BrowserThread; | |
22 | |
23 namespace content_settings { | |
24 | |
25 namespace { | |
26 | |
27 class OverrideRuleIterator : public RuleIterator { | |
28 public: | |
29 explicit OverrideRuleIterator(bool is_allowed) : is_done_(is_allowed) {} | |
30 | |
31 virtual bool HasNext() const OVERRIDE { return !is_done_; } | |
32 | |
33 virtual Rule Next() OVERRIDE { | |
34 DCHECK(!is_done_); | |
35 is_done_ = false; | |
Bernhard Bauer
2014/09/12 09:37:38
You want true here?
Daniel Nishi
2014/09/12 16:40:35
Done.
| |
36 return Rule(ContentSettingsPattern::Wildcard(), | |
37 ContentSettingsPattern::Wildcard(), | |
38 new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
39 } | |
40 | |
41 private: | |
42 bool is_done_; | |
43 }; | |
44 | |
45 } // namespace | |
46 | |
47 // static | |
48 void OverrideProvider::RegisterProfilePrefs( | |
49 user_prefs::PrefRegistrySyncable* registry) { | |
50 registry->RegisterDictionaryPref( | |
51 prefs::kOverrideContentSettings, | |
52 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
53 } | |
54 | |
55 OverrideProvider::OverrideProvider(PrefService* prefs, bool incognito) | |
56 : prefs_(prefs), is_incognito_(incognito) { | |
57 DCHECK(prefs_); | |
58 | |
59 // Read global overrides. | |
60 ReadOverrideSettings(); | |
61 } | |
62 | |
63 OverrideProvider::~OverrideProvider() { | |
64 } | |
65 | |
66 RuleIterator* OverrideProvider::GetRuleIterator( | |
67 ContentSettingsType content_type, | |
68 const ResourceIdentifier& resource_identifier, | |
69 bool incognito) const { | |
70 base::AutoLock lock(lock_); | |
71 if (resource_identifier.empty()) { | |
72 return new OverrideRuleIterator(allowed_settings_[content_type]); | |
73 } | |
74 return new EmptyRuleIterator(); | |
75 } | |
76 | |
77 void OverrideProvider::ClearAllContentSettingsRules( | |
78 ContentSettingsType content_type) { | |
79 } | |
80 | |
81 bool OverrideProvider::SetWebsiteSetting( | |
82 const ContentSettingsPattern& primary_pattern, | |
83 const ContentSettingsPattern& secondary_pattern, | |
84 ContentSettingsType content_type, | |
85 const ResourceIdentifier& resource_identifier, | |
86 base::Value* in_value) { | |
87 return false; | |
88 } | |
89 | |
90 void OverrideProvider::ShutdownOnUIThread() { | |
91 DCHECK(prefs_); | |
92 prefs_ = NULL; | |
93 } | |
94 | |
95 void OverrideProvider::SetOverrideSetting(ContentSettingsType content_type, | |
96 bool enabled) { | |
97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
98 DCHECK(prefs_); | |
99 | |
100 // Disallow incognito to change the state. | |
101 DCHECK(!is_incognito_); | |
102 | |
103 base::AutoLock lock(lock_); | |
104 DictionaryPrefUpdate update(prefs_, prefs::kOverrideContentSettings); | |
105 base::DictionaryValue* default_settings_dictionary = update.Get(); | |
106 if (enabled) { | |
107 allowed_settings_[content_type] = true; | |
108 default_settings_dictionary->RemoveWithoutPathExpansion( | |
109 GetTypeName(content_type), NULL); | |
110 } else { | |
111 allowed_settings_[content_type] = false; | |
112 default_settings_dictionary->SetWithoutPathExpansion( | |
113 GetTypeName(content_type), new base::FundamentalValue(false)); | |
114 } | |
115 } | |
116 | |
117 bool OverrideProvider::IsEnabled(ContentSettingsType content_type) const { | |
118 base::AutoLock lock(lock_); | |
119 return allowed_settings_[content_type]; | |
120 } | |
121 | |
122 void OverrideProvider::ReadOverrideSettings() { | |
123 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
124 const base::DictionaryValue* allowed_settings_dictionary = | |
125 prefs_->GetDictionary(prefs::kOverrideContentSettings); | |
126 | |
127 ClearBlockedSettings(); | |
128 if (allowed_settings_dictionary) { | |
Bernhard Bauer
2014/09/12 09:37:38
I don't think this check is necessary. If you regi
Daniel Nishi
2014/09/12 16:40:35
Done.
| |
129 for (base::DictionaryValue::Iterator i(*allowed_settings_dictionary); | |
130 !i.IsAtEnd(); | |
131 i.Advance()) { | |
132 const std::string& content_type(i.key()); | |
133 for (size_t type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) { | |
Bernhard Bauer
2014/09/12 09:37:38
You could do this the other way around: Iterate ov
Daniel Nishi
2014/09/12 16:40:35
I went the other direction where it sets the flag
Bernhard Bauer
2014/09/12 17:16:36
OK. Either one is fine with me, but we should docu
| |
134 if (content_type == GetTypeName(ContentSettingsType(type))) { | |
135 allowed_settings_[ContentSettingsType(type)] = false; | |
136 break; | |
137 } | |
138 } | |
139 } | |
140 } | |
141 } | |
142 | |
143 void OverrideProvider::ClearBlockedSettings() { | |
144 for (size_t i = 0; i < arraysize(allowed_settings_); ++i) | |
145 allowed_settings_[i] = true; | |
146 } | |
147 | |
148 } // namespace content_settings | |
OLD | NEW |