OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Maps hostnames to custom content settings. Written on the UI thread and read | |
6 // on any thread. One instance per profile. | |
7 | |
8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | |
9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | |
10 | |
11 #include <map> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/basictypes.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/observer_list.h" | |
18 #include "base/prefs/pref_change_registrar.h" | |
19 #include "base/threading/platform_thread.h" | |
20 #include "base/threading/thread_checker.h" | |
21 #include "base/tuple.h" | |
22 #include "chrome/browser/content_settings/content_settings_override_provider.h" | |
23 #include "components/content_settings/core/browser/content_settings_observer.h" | |
24 #include "components/content_settings/core/common/content_settings.h" | |
25 #include "components/content_settings/core/common/content_settings_pattern.h" | |
26 #include "components/content_settings/core/common/content_settings_types.h" | |
27 | |
28 class ExtensionService; | |
29 class GURL; | |
30 class PrefService; | |
31 | |
32 namespace base { | |
33 class Clock; | |
34 class Value; | |
35 } | |
36 | |
37 namespace content_settings { | |
38 class OverrideProvider; | |
39 class ObservableProvider; | |
40 class ProviderInterface; | |
41 class PrefProvider; | |
42 } | |
43 | |
44 namespace user_prefs { | |
45 class PrefRegistrySyncable; | |
46 } | |
47 | |
48 class HostContentSettingsMap | |
49 : public content_settings::Observer, | |
50 public base::RefCountedThreadSafe<HostContentSettingsMap> { | |
51 public: | |
52 enum ProviderType { | |
53 // EXTENSION names is a layering violation when this class will move to | |
54 // components. | |
55 // TODO(mukai): find the solution. | |
56 INTERNAL_EXTENSION_PROVIDER = 0, | |
57 POLICY_PROVIDER, | |
58 CUSTOM_EXTENSION_PROVIDER, | |
59 OVERRIDE_PROVIDER, | |
60 PREF_PROVIDER, | |
61 DEFAULT_PROVIDER, | |
62 NUM_PROVIDER_TYPES, | |
63 }; | |
64 | |
65 // This should be called on the UI thread, otherwise |thread_checker_| handles | |
66 // CalledOnValidThread() wrongly. | |
67 HostContentSettingsMap(PrefService* prefs, bool incognito); | |
68 | |
69 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
70 | |
71 // Adds a new provider for |type|. | |
72 void RegisterProvider( | |
73 ProviderType type, | |
74 scoped_ptr<content_settings::ObservableProvider> provider); | |
75 | |
76 // Returns the default setting for a particular content type. If |provider_id| | |
77 // is not NULL, the id of the provider which provided the default setting is | |
78 // assigned to it. | |
79 // | |
80 // This may be called on any thread. | |
81 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, | |
82 std::string* provider_id) const; | |
83 | |
84 // Returns a single |ContentSetting| which applies to the given URLs. Note | |
85 // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|, | |
86 // |CookieSettings| should be used instead. For content types that can't be | |
87 // converted to a |ContentSetting|, |GetContentSettingValue| should be called. | |
88 // If there is no content setting, returns CONTENT_SETTING_DEFAULT. | |
89 // | |
90 // May be called on any thread. | |
91 ContentSetting GetContentSetting( | |
92 const GURL& primary_url, | |
93 const GURL& secondary_url, | |
94 ContentSettingsType content_type, | |
95 const std::string& resource_identifier) const; | |
96 | |
97 // Returns a single content setting |Value| which applies to the given URLs. | |
98 // If |info| is not NULL, then the |source| field of |info| is set to the | |
99 // source of the returned |Value| (POLICY, EXTENSION, USER, ...) and the | |
100 // |primary_pattern| and the |secondary_pattern| fields of |info| are set to | |
101 // the patterns of the applying rule. Note that certain internal schemes are | |
102 // whitelisted. For whitelisted schemes the |source| field of |info| is set | |
103 // the |SETTING_SOURCE_WHITELIST| and the |primary_pattern| and | |
104 // |secondary_pattern| are set to a wildcard pattern. If there is no content | |
105 // setting, NULL is returned and the |source| field of |info| is set to | |
106 // |SETTING_SOURCE_NONE|. The pattern fiels of |info| are set to empty | |
107 // patterns. | |
108 // May be called on any thread. | |
109 scoped_ptr<base::Value> GetWebsiteSetting( | |
110 const GURL& primary_url, | |
111 const GURL& secondary_url, | |
112 ContentSettingsType content_type, | |
113 const std::string& resource_identifier, | |
114 content_settings::SettingInfo* info) const; | |
115 | |
116 // For a given content type, returns all patterns with a non-default setting, | |
117 // mapped to their actual settings, in the precedence order of the rules. | |
118 // |settings| must be a non-NULL outparam. | |
119 // | |
120 // This may be called on any thread. | |
121 void GetSettingsForOneType(ContentSettingsType content_type, | |
122 const std::string& resource_identifier, | |
123 ContentSettingsForOneType* settings) const; | |
124 | |
125 // Sets the default setting for a particular content type. This method must | |
126 // not be invoked on an incognito map. | |
127 // | |
128 // This should only be called on the UI thread. | |
129 void SetDefaultContentSetting(ContentSettingsType content_type, | |
130 ContentSetting setting); | |
131 | |
132 // Sets the content |setting| for the given patterns, |content_type| and | |
133 // |resource_identifier|. Setting the value to CONTENT_SETTING_DEFAULT causes | |
134 // the default setting for that type to be used when loading pages matching | |
135 // this pattern. | |
136 // NOTICE: This is just a convenience method for content types that use | |
137 // |CONTENT_SETTING| as their data type. For content types that use other | |
138 // data types please use the method SetWebsiteSetting. | |
139 // | |
140 // This should only be called on the UI thread. | |
141 void SetContentSetting(const ContentSettingsPattern& primary_pattern, | |
142 const ContentSettingsPattern& secondary_pattern, | |
143 ContentSettingsType content_type, | |
144 const std::string& resource_identifier, | |
145 ContentSetting setting); | |
146 | |
147 // Sets the |value| for the given patterns, |content_type| and | |
148 // |resource_identifier|. Setting the value to NULL causes the default value | |
149 // for that type to be used when loading pages matching this pattern. | |
150 // | |
151 // Takes ownership of the passed value. | |
152 void SetWebsiteSetting(const ContentSettingsPattern& primary_pattern, | |
153 const ContentSettingsPattern& secondary_pattern, | |
154 ContentSettingsType content_type, | |
155 const std::string& resource_identifier, | |
156 base::Value* value); | |
157 | |
158 // Sets the most specific rule that currently defines the permission for the | |
159 // given permission type. | |
160 void SetNarrowestWebsiteSetting( | |
161 const ContentSettingsPattern& primary_pattern, | |
162 const ContentSettingsPattern& secondary_pattern, | |
163 ContentSettingsType content_type, | |
164 const std::string& resource_identifier, | |
165 ContentSetting setting, | |
166 content_settings::SettingInfo existing_info); | |
167 | |
168 // Convenience method to add a content setting for the given URLs, making sure | |
169 // that there is no setting overriding it. | |
170 // | |
171 // This should only be called on the UI thread. | |
172 void AddExceptionForURL(const GURL& primary_url, | |
173 const GURL& secondary_url, | |
174 ContentSettingsType content_type, | |
175 ContentSetting setting); | |
176 | |
177 // Clears all host-specific settings for one content type. | |
178 // | |
179 // This should only be called on the UI thread. | |
180 void ClearSettingsForOneType(ContentSettingsType content_type); | |
181 | |
182 static bool IsValueAllowedForType(PrefService* prefs, | |
183 const base::Value* value, | |
184 ContentSettingsType content_type); | |
185 static bool IsSettingAllowedForType(PrefService* prefs, | |
186 ContentSetting setting, | |
187 ContentSettingsType content_type); | |
188 | |
189 // Returns true if the values for content type are of type dictionary/map. | |
190 static bool ContentTypeHasCompoundValue(ContentSettingsType type); | |
191 | |
192 // Detaches the HostContentSettingsMap from all Profile-related objects like | |
193 // PrefService. This methods needs to be called before destroying the Profile. | |
194 // Afterwards, none of the methods above that should only be called on the UI | |
195 // thread should be called anymore. | |
196 void ShutdownOnUIThread(); | |
197 | |
198 // content_settings::Observer implementation. | |
199 void OnContentSettingChanged(const ContentSettingsPattern& primary_pattern, | |
200 const ContentSettingsPattern& secondary_pattern, | |
201 ContentSettingsType content_type, | |
202 std::string resource_identifier) override; | |
203 | |
204 // Returns true if we should allow all content types for this URL. This is | |
205 // true for various internal objects like chrome:// URLs, so UI and other | |
206 // things users think of as "not webpages" don't break. | |
207 static bool ShouldAllowAllContent(const GURL& primary_url, | |
208 const GURL& secondary_url, | |
209 ContentSettingsType content_type); | |
210 | |
211 // Returns the ProviderType associated with the given source string. | |
212 // TODO(estade): I regret adding this. At the moment there are no legitimate | |
213 // uses. We should stick to ProviderType rather than string so we don't have | |
214 // to convert backwards. | |
215 static ProviderType GetProviderTypeFromSource(const std::string& source); | |
216 | |
217 bool is_off_the_record() const { | |
218 return is_off_the_record_; | |
219 } | |
220 | |
221 // Returns a single |ContentSetting| which applies to the given URLs, just as | |
222 // |GetContentSetting| does. If the setting is allowed, it also records the | |
223 // last usage to preferences. | |
224 // | |
225 // This should only be called on the UI thread, unlike |GetContentSetting|. | |
226 ContentSetting GetContentSettingAndMaybeUpdateLastUsage( | |
227 const GURL& primary_url, | |
228 const GURL& secondary_url, | |
229 ContentSettingsType content_type, | |
230 const std::string& resource_identifier); | |
231 | |
232 // Sets the last time that a given content type has been used for the pattern | |
233 // which matches the URLs to the current time. | |
234 void UpdateLastUsage(const GURL& primary_url, | |
235 const GURL& secondary_url, | |
236 ContentSettingsType content_type); | |
237 | |
238 // Sets the last time that a given content type has been used for a pattern | |
239 // pair to the current time. | |
240 void UpdateLastUsageByPattern(const ContentSettingsPattern& primary_pattern, | |
241 const ContentSettingsPattern& secondary_pattern, | |
242 ContentSettingsType content_type); | |
243 | |
244 // Returns the last time the pattern that matches the URL has requested | |
245 // permission for the |content_type| setting. | |
246 base::Time GetLastUsage(const GURL& primary_url, | |
247 const GURL& secondary_url, | |
248 ContentSettingsType content_type); | |
249 | |
250 // Returns the last time the pattern has requested permission for the | |
251 // |content_type| setting. | |
252 base::Time GetLastUsageByPattern( | |
253 const ContentSettingsPattern& primary_pattern, | |
254 const ContentSettingsPattern& secondary_pattern, | |
255 ContentSettingsType content_type); | |
256 | |
257 // Returns the content setting without considering the global on/off toggle | |
258 // for the content setting that matches the URLs. | |
259 ContentSetting GetContentSettingWithoutOverride( | |
260 const GURL& primary_url, | |
261 const GURL& secondary_url, | |
262 ContentSettingsType content_type, | |
263 const std::string& resource_identifier); | |
264 | |
265 // Returns the single content setting |value| without considering the | |
266 // global on/off toggle for the content setting that matches the given | |
267 // patterns. | |
268 scoped_ptr<base::Value> GetWebsiteSettingWithoutOverride( | |
269 const GURL& primary_url, | |
270 const GURL& secondary_url, | |
271 ContentSettingsType content_type, | |
272 const std::string& resource_identifier, | |
273 content_settings::SettingInfo* info) const; | |
274 | |
275 // Sets globally if a given |content_type| |is_enabled|. | |
276 void SetContentSettingOverride(ContentSettingsType content_type, | |
277 bool is_enabled); | |
278 | |
279 // Returns if a given |content_type| is enabled. | |
280 bool GetContentSettingOverride(ContentSettingsType content_type); | |
281 | |
282 // Adds/removes an observer for content settings changes. | |
283 void AddObserver(content_settings::Observer* observer); | |
284 void RemoveObserver(content_settings::Observer* observer); | |
285 | |
286 // Passes ownership of |clock|. | |
287 void SetPrefClockForTesting(scoped_ptr<base::Clock> clock); | |
288 | |
289 private: | |
290 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; | |
291 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; | |
292 | |
293 typedef std::map<ProviderType, content_settings::ProviderInterface*> | |
294 ProviderMap; | |
295 typedef ProviderMap::iterator ProviderIterator; | |
296 typedef ProviderMap::const_iterator ConstProviderIterator; | |
297 | |
298 ~HostContentSettingsMap() override; | |
299 | |
300 ContentSetting GetDefaultContentSettingFromProvider( | |
301 ContentSettingsType content_type, | |
302 content_settings::ProviderInterface* provider) const; | |
303 | |
304 // Adds content settings for |content_type| and |resource_identifier|, | |
305 // provided by |provider|, into |settings|. If |incognito| is true, adds only | |
306 // the content settings which are applicable to the incognito mode and differ | |
307 // from the normal mode. Otherwise, adds the content settings for the normal | |
308 // mode. | |
309 void AddSettingsForOneType( | |
310 const content_settings::ProviderInterface* provider, | |
311 ProviderType provider_type, | |
312 ContentSettingsType content_type, | |
313 const std::string& resource_identifier, | |
314 ContentSettingsForOneType* settings, | |
315 bool incognito) const; | |
316 | |
317 // Call UsedContentSettingsProviders() whenever you access | |
318 // content_settings_providers_ (apart from initialization and | |
319 // teardown), so that we can DCHECK in RegisterExtensionService that | |
320 // it is not being called too late. | |
321 void UsedContentSettingsProviders() const; | |
322 | |
323 // Returns the single content setting |value| with a toggle for if it | |
324 // takes the global on/off switch into account. | |
325 scoped_ptr<base::Value> GetWebsiteSettingInternal( | |
326 const GURL& primary_url, | |
327 const GURL& secondary_url, | |
328 ContentSettingsType content_type, | |
329 const std::string& resource_identifier, | |
330 content_settings::SettingInfo* info, | |
331 bool get_override) const; | |
332 | |
333 content_settings::PrefProvider* GetPrefProvider(); | |
334 | |
335 #ifndef NDEBUG | |
336 // This starts as the thread ID of the thread that constructs this | |
337 // object, and remains until used by a different thread, at which | |
338 // point it is set to base::kInvalidThreadId. This allows us to | |
339 // DCHECK on unsafe usage of content_settings_providers_ (they | |
340 // should be set up on a single thread, after which they are | |
341 // immutable). | |
342 mutable base::PlatformThreadId used_from_thread_id_; | |
343 #endif | |
344 | |
345 // Weak; owned by the Profile. | |
346 PrefService* prefs_; | |
347 | |
348 // Whether this settings map is for an OTR session. | |
349 bool is_off_the_record_; | |
350 | |
351 // Content setting providers. This is only modified at construction | |
352 // time and by RegisterExtensionService, both of which should happen | |
353 // before any other uses of it. | |
354 ProviderMap content_settings_providers_; | |
355 | |
356 base::ThreadChecker thread_checker_; | |
357 | |
358 ObserverList<content_settings::Observer> observers_; | |
359 | |
360 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); | |
361 }; | |
362 | |
363 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | |
OLD | NEW |