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.
5 // Maps hostnames to custom content settings. Written on the UI thread and read
6 // on any thread. One instance per profile.
8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_
9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_
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/tuple.h"
21 #include "chrome/browser/content_settings/content_settings_observer.h"
22 #include "chrome/common/content_settings.h"
23 #include "chrome/common/content_settings_pattern.h"
24 #include "components/content_settings/core/common/content_settings_types.h"
26 class ExtensionService
;
35 namespace content_settings
{
36 class ProviderInterface
;
40 namespace user_prefs
{
41 class PrefRegistrySyncable
;
44 class HostContentSettingsMap
45 : public content_settings::Observer
,
46 public base::RefCountedThreadSafe
<HostContentSettingsMap
> {
49 INTERNAL_EXTENSION_PROVIDER
= 0,
51 CUSTOM_EXTENSION_PROVIDER
,
57 HostContentSettingsMap(PrefService
* prefs
, bool incognito
);
59 #if defined(ENABLE_EXTENSIONS)
60 // In some cases, the ExtensionService is not available at the time the
61 // HostContentSettingsMap is constructed. In these cases, we register the
62 // service once it's available.
63 void RegisterExtensionService(ExtensionService
* extension_service
);
66 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
68 // Returns the default setting for a particular content type. If |provider_id|
69 // is not NULL, the id of the provider which provided the default setting is
72 // This may be called on any thread.
73 ContentSetting
GetDefaultContentSetting(ContentSettingsType content_type
,
74 std::string
* provider_id
) const;
76 // Returns a single |ContentSetting| which applies to the given URLs. Note
77 // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|,
78 // |CookieSettings| should be used instead. For content types that can't be
79 // converted to a |ContentSetting|, |GetContentSettingValue| should be called.
80 // If there is no content setting, returns CONTENT_SETTING_DEFAULT.
82 // May be called on any thread.
83 ContentSetting
GetContentSetting(
84 const GURL
& primary_url
,
85 const GURL
& secondary_url
,
86 ContentSettingsType content_type
,
87 const std::string
& resource_identifier
) const;
89 // Returns a single content setting |Value| which applies to the given URLs.
90 // If |info| is not NULL, then the |source| field of |info| is set to the
91 // source of the returned |Value| (POLICY, EXTENSION, USER, ...) and the
92 // |primary_pattern| and the |secondary_pattern| fields of |info| are set to
93 // the patterns of the applying rule. Note that certain internal schemes are
94 // whitelisted. For whitelisted schemes the |source| field of |info| is set
95 // the |SETTING_SOURCE_WHITELIST| and the |primary_pattern| and
96 // |secondary_pattern| are set to a wildcard pattern. If there is no content
97 // setting, NULL is returned and the |source| field of |info| is set to
98 // |SETTING_SOURCE_NONE|. The pattern fiels of |info| are set to empty
100 // The ownership of the resulting |Value| is transfered to the caller.
101 // May be called on any thread.
102 base::Value
* GetWebsiteSetting(
103 const GURL
& primary_url
,
104 const GURL
& secondary_url
,
105 ContentSettingsType content_type
,
106 const std::string
& resource_identifier
,
107 content_settings::SettingInfo
* info
) const;
109 // For a given content type, returns all patterns with a non-default setting,
110 // mapped to their actual settings, in the precedence order of the rules.
111 // |settings| must be a non-NULL outparam.
113 // This may be called on any thread.
114 void GetSettingsForOneType(ContentSettingsType content_type
,
115 const std::string
& resource_identifier
,
116 ContentSettingsForOneType
* settings
) const;
118 // Sets the default setting for a particular content type. This method must
119 // not be invoked on an incognito map.
121 // This should only be called on the UI thread.
122 void SetDefaultContentSetting(ContentSettingsType content_type
,
123 ContentSetting setting
);
125 // Sets the content |setting| for the given patterns, |content_type| and
126 // |resource_identifier|. Setting the value to CONTENT_SETTING_DEFAULT causes
127 // the default setting for that type to be used when loading pages matching
129 // NOTICE: This is just a convenience method for content types that use
130 // |CONTENT_SETTING| as their data type. For content types that use other
131 // data types please use the method SetWebsiteSetting.
133 // This should only be called on the UI thread.
134 void SetContentSetting(const ContentSettingsPattern
& primary_pattern
,
135 const ContentSettingsPattern
& secondary_pattern
,
136 ContentSettingsType content_type
,
137 const std::string
& resource_identifier
,
138 ContentSetting setting
);
140 // Sets the |value| for the given patterns, |content_type| and
141 // |resource_identifier|. Setting the value to NULL causes the default value
142 // for that type to be used when loading pages matching this pattern.
144 // Takes ownership of the passed value.
145 void SetWebsiteSetting(const ContentSettingsPattern
& primary_pattern
,
146 const ContentSettingsPattern
& secondary_pattern
,
147 ContentSettingsType content_type
,
148 const std::string
& resource_identifier
,
151 // Sets the most specific rule that currently defines the permission for the
152 // given permission type.
153 void SetNarrowestWebsiteSetting(
154 const ContentSettingsPattern
& primary_pattern
,
155 const ContentSettingsPattern
& secondary_pattern
,
156 ContentSettingsType content_type
,
157 const std::string
& resource_identifier
,
158 ContentSetting setting
,
159 content_settings::SettingInfo existing_info
);
161 // Convenience method to add a content setting for the given URLs, making sure
162 // that there is no setting overriding it.
164 // This should only be called on the UI thread.
165 void AddExceptionForURL(const GURL
& primary_url
,
166 const GURL
& secondary_url
,
167 ContentSettingsType content_type
,
168 ContentSetting setting
);
170 // Clears all host-specific settings for one content type.
172 // This should only be called on the UI thread.
173 void ClearSettingsForOneType(ContentSettingsType content_type
);
175 static bool IsValueAllowedForType(PrefService
* prefs
,
176 const base::Value
* value
,
177 ContentSettingsType content_type
);
178 static bool IsSettingAllowedForType(PrefService
* prefs
,
179 ContentSetting setting
,
180 ContentSettingsType content_type
);
182 // Returns true if the values for content type are of type dictionary/map.
183 static bool ContentTypeHasCompoundValue(ContentSettingsType type
);
185 // Detaches the HostContentSettingsMap from all Profile-related objects like
186 // PrefService. This methods needs to be called before destroying the Profile.
187 // Afterwards, none of the methods above that should only be called on the UI
188 // thread should be called anymore.
189 void ShutdownOnUIThread();
191 // content_settings::Observer implementation.
192 virtual void OnContentSettingChanged(
193 const ContentSettingsPattern
& primary_pattern
,
194 const ContentSettingsPattern
& secondary_pattern
,
195 ContentSettingsType content_type
,
196 std::string resource_identifier
) OVERRIDE
;
198 // Returns true if we should allow all content types for this URL. This is
199 // true for various internal objects like chrome:// URLs, so UI and other
200 // things users think of as "not webpages" don't break.
201 static bool ShouldAllowAllContent(const GURL
& primary_url
,
202 const GURL
& secondary_url
,
203 ContentSettingsType content_type
);
205 // Returns the ProviderType associated with the given source string.
206 // TODO(estade): I regret adding this. At the moment there are no legitimate
207 // uses. We should stick to ProviderType rather than string so we don't have
208 // to convert backwards.
209 static ProviderType
GetProviderTypeFromSource(const std::string
& source
);
211 bool is_off_the_record() const {
212 return is_off_the_record_
;
215 // Returns a single |ContentSetting| which applies to the given URLs, just as
216 // |GetContentSetting| does. If the setting is allowed, it also records the
217 // last usage to preferences.
219 // This should only be called on the UI thread, unlike |GetContentSetting|.
220 ContentSetting
GetContentSettingAndMaybeUpdateLastUsage(
221 const GURL
& primary_url
,
222 const GURL
& secondary_url
,
223 ContentSettingsType content_type
,
224 const std::string
& resource_identifier
);
226 // Sets the last time that a given content type has been used for the pattern
227 // which matches the URLs to the current time.
228 void UpdateLastUsage(const GURL
& primary_url
,
229 const GURL
& secondary_url
,
230 ContentSettingsType content_type
);
232 // Sets the last time that a given content type has been used for a pattern
233 // pair to the current time.
234 void UpdateLastUsageByPattern(const ContentSettingsPattern
& primary_pattern
,
235 const ContentSettingsPattern
& secondary_pattern
,
236 ContentSettingsType content_type
);
238 // Returns the last time the pattern that matches the URL has requested
239 // permission for the |content_type| setting.
240 base::Time
GetLastUsage(const GURL
& primary_url
,
241 const GURL
& secondary_url
,
242 ContentSettingsType content_type
);
244 // Returns the last time the pattern has requested permission for the
245 // |content_type| setting.
246 base::Time
GetLastUsageByPattern(
247 const ContentSettingsPattern
& primary_pattern
,
248 const ContentSettingsPattern
& secondary_pattern
,
249 ContentSettingsType content_type
);
251 // Adds/removes an observer for content settings changes.
252 void AddObserver(content_settings::Observer
* observer
);
253 void RemoveObserver(content_settings::Observer
* observer
);
255 // Passes ownership of |clock|.
256 void SetPrefClockForTesting(scoped_ptr
<base::Clock
> clock
);
259 friend class base::RefCountedThreadSafe
<HostContentSettingsMap
>;
260 friend class HostContentSettingsMapTest_NonDefaultSettings_Test
;
262 typedef std::map
<ProviderType
, content_settings::ProviderInterface
*>
264 typedef ProviderMap::iterator ProviderIterator
;
265 typedef ProviderMap::const_iterator ConstProviderIterator
;
267 virtual ~HostContentSettingsMap();
269 ContentSetting
GetDefaultContentSettingFromProvider(
270 ContentSettingsType content_type
,
271 content_settings::ProviderInterface
* provider
) const;
273 // Migrate the Clear on exit pref into equivalent content settings.
274 void MigrateObsoleteClearOnExitPref();
276 // Adds content settings for |content_type| and |resource_identifier|,
277 // provided by |provider|, into |settings|. If |incognito| is true, adds only
278 // the content settings which are applicable to the incognito mode and differ
279 // from the normal mode. Otherwise, adds the content settings for the normal
281 void AddSettingsForOneType(
282 const content_settings::ProviderInterface
* provider
,
283 ProviderType provider_type
,
284 ContentSettingsType content_type
,
285 const std::string
& resource_identifier
,
286 ContentSettingsForOneType
* settings
,
287 bool incognito
) const;
289 // Call UsedContentSettingsProviders() whenever you access
290 // content_settings_providers_ (apart from initialization and
291 // teardown), so that we can DCHECK in RegisterExtensionService that
292 // it is not being called too late.
293 void UsedContentSettingsProviders() const;
295 content_settings::PrefProvider
* GetPrefProvider();
298 // This starts as the thread ID of the thread that constructs this
299 // object, and remains until used by a different thread, at which
300 // point it is set to base::kInvalidThreadId. This allows us to
301 // DCHECK on unsafe usage of content_settings_providers_ (they
302 // should be set up on a single thread, after which they are
304 mutable base::PlatformThreadId used_from_thread_id_
;
307 // Weak; owned by the Profile.
310 // Whether this settings map is for an OTR session.
311 bool is_off_the_record_
;
313 // Content setting providers. This is only modified at construction
314 // time and by RegisterExtensionService, both of which should happen
315 // before any other uses of it.
316 ProviderMap content_settings_providers_
;
318 ObserverList
<content_settings::Observer
> observers_
;
320 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap
);
323 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_