Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / content_settings / host_content_settings_map.h
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/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"
25
26 class ExtensionService;
27 class GURL;
28 class PrefService;
29
30 namespace base {
31 class Clock;
32 class Value;
33 }
34
35 namespace content_settings {
36 class ProviderInterface;
37 class PrefProvider;
38 }
39
40 namespace user_prefs {
41 class PrefRegistrySyncable;
42 }
43
44 class HostContentSettingsMap
45     : public content_settings::Observer,
46       public base::RefCountedThreadSafe<HostContentSettingsMap> {
47  public:
48   enum ProviderType {
49     INTERNAL_EXTENSION_PROVIDER = 0,
50     POLICY_PROVIDER,
51     CUSTOM_EXTENSION_PROVIDER,
52     PREF_PROVIDER,
53     DEFAULT_PROVIDER,
54     NUM_PROVIDER_TYPES,
55   };
56
57   HostContentSettingsMap(PrefService* prefs, bool incognito);
58
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);
64 #endif
65
66   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
67
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
70   // assigned to it.
71   //
72   // This may be called on any thread.
73   ContentSetting GetDefaultContentSetting(ContentSettingsType content_type,
74                                           std::string* provider_id) const;
75
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.
81   //
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;
88
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
99   // patterns.
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;
108
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.
112   //
113   // This may be called on any thread.
114   void GetSettingsForOneType(ContentSettingsType content_type,
115                              const std::string& resource_identifier,
116                              ContentSettingsForOneType* settings) const;
117
118   // Sets the default setting for a particular content type. This method must
119   // not be invoked on an incognito map.
120   //
121   // This should only be called on the UI thread.
122   void SetDefaultContentSetting(ContentSettingsType content_type,
123                                 ContentSetting setting);
124
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
128   // this pattern.
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.
132   //
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);
139
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.
143   //
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,
149                          base::Value* value);
150
151   // Convenience method to add a content setting for the given URLs, making sure
152   // that there is no setting overriding it.
153   //
154   // This should only be called on the UI thread.
155   void AddExceptionForURL(const GURL& primary_url,
156                           const GURL& secondary_url,
157                           ContentSettingsType content_type,
158                           ContentSetting setting);
159
160   // Clears all host-specific settings for one content type.
161   //
162   // This should only be called on the UI thread.
163   void ClearSettingsForOneType(ContentSettingsType content_type);
164
165   static bool IsValueAllowedForType(PrefService* prefs,
166                                     const base::Value* value,
167                                     ContentSettingsType content_type);
168   static bool IsSettingAllowedForType(PrefService* prefs,
169                                       ContentSetting setting,
170                                       ContentSettingsType content_type);
171
172   // Returns true if the values for content type are of type dictionary/map.
173   static bool ContentTypeHasCompoundValue(ContentSettingsType type);
174
175   // Detaches the HostContentSettingsMap from all Profile-related objects like
176   // PrefService. This methods needs to be called before destroying the Profile.
177   // Afterwards, none of the methods above that should only be called on the UI
178   // thread should be called anymore.
179   void ShutdownOnUIThread();
180
181   // content_settings::Observer implementation.
182   virtual void OnContentSettingChanged(
183       const ContentSettingsPattern& primary_pattern,
184       const ContentSettingsPattern& secondary_pattern,
185       ContentSettingsType content_type,
186       std::string resource_identifier) OVERRIDE;
187
188   // Returns true if we should allow all content types for this URL.  This is
189   // true for various internal objects like chrome:// URLs, so UI and other
190   // things users think of as "not webpages" don't break.
191   static bool ShouldAllowAllContent(const GURL& primary_url,
192                                     const GURL& secondary_url,
193                                     ContentSettingsType content_type);
194
195   // Returns the ProviderType associated with the given source string.
196   // TODO(estade): I regret adding this. At the moment there are no legitimate
197   // uses. We should stick to ProviderType rather than string so we don't have
198   // to convert backwards.
199   static ProviderType GetProviderTypeFromSource(const std::string& source);
200
201   bool is_off_the_record() const {
202     return is_off_the_record_;
203   }
204
205   // Returns a single |ContentSetting| which applies to the given URLs, just as
206   // |GetContentSetting| does. If the setting is allowed, it also records the
207   // last usage to preferences.
208   //
209   // This should only be called on the UI thread, unlike |GetContentSetting|.
210   ContentSetting GetContentSettingAndMaybeUpdateLastUsage(
211       const GURL& primary_url,
212       const GURL& secondary_url,
213       ContentSettingsType content_type,
214       const std::string& resource_identifier);
215
216   // Sets the last time that a given content type has been used for the pattern
217   // which matches the URLs to the current time.
218   void UpdateLastUsage(const GURL& primary_url,
219                        const GURL& secondary_url,
220                        ContentSettingsType content_type);
221
222   // Sets the last time that a given content type has been used for a pattern
223   // pair to the current time.
224   void UpdateLastUsageByPattern(const ContentSettingsPattern& primary_pattern,
225                                 const ContentSettingsPattern& secondary_pattern,
226                                 ContentSettingsType content_type);
227
228   // Returns the last time the pattern that matches the URL has requested
229   // permission for the |content_type| setting.
230   base::Time GetLastUsage(const GURL& primary_url,
231                           const GURL& secondary_url,
232                           ContentSettingsType content_type);
233
234   // Returns the last time the pattern has requested permission for the
235   // |content_type| setting.
236   base::Time GetLastUsageByPattern(
237       const ContentSettingsPattern& primary_pattern,
238       const ContentSettingsPattern& secondary_pattern,
239       ContentSettingsType content_type);
240
241   // Adds/removes an observer for content settings changes.
242   void AddObserver(content_settings::Observer* observer);
243   void RemoveObserver(content_settings::Observer* observer);
244
245   // Passes ownership of |clock|.
246   void SetPrefClockForTesting(scoped_ptr<base::Clock> clock);
247
248  private:
249   friend class base::RefCountedThreadSafe<HostContentSettingsMap>;
250   friend class HostContentSettingsMapTest_NonDefaultSettings_Test;
251
252   typedef std::map<ProviderType, content_settings::ProviderInterface*>
253       ProviderMap;
254   typedef ProviderMap::iterator ProviderIterator;
255   typedef ProviderMap::const_iterator ConstProviderIterator;
256
257   virtual ~HostContentSettingsMap();
258
259   ContentSetting GetDefaultContentSettingFromProvider(
260       ContentSettingsType content_type,
261       content_settings::ProviderInterface* provider) const;
262
263   // Migrate the Clear on exit pref into equivalent content settings.
264   void MigrateObsoleteClearOnExitPref();
265
266   // Adds content settings for |content_type| and |resource_identifier|,
267   // provided by |provider|, into |settings|. If |incognito| is true, adds only
268   // the content settings which are applicable to the incognito mode and differ
269   // from the normal mode. Otherwise, adds the content settings for the normal
270   // mode.
271   void AddSettingsForOneType(
272       const content_settings::ProviderInterface* provider,
273       ProviderType provider_type,
274       ContentSettingsType content_type,
275       const std::string& resource_identifier,
276       ContentSettingsForOneType* settings,
277       bool incognito) const;
278
279   // Call UsedContentSettingsProviders() whenever you access
280   // content_settings_providers_ (apart from initialization and
281   // teardown), so that we can DCHECK in RegisterExtensionService that
282   // it is not being called too late.
283   void UsedContentSettingsProviders() const;
284
285   content_settings::PrefProvider* GetPrefProvider();
286
287 #ifndef NDEBUG
288   // This starts as the thread ID of the thread that constructs this
289   // object, and remains until used by a different thread, at which
290   // point it is set to base::kInvalidThreadId. This allows us to
291   // DCHECK on unsafe usage of content_settings_providers_ (they
292   // should be set up on a single thread, after which they are
293   // immutable).
294   mutable base::PlatformThreadId used_from_thread_id_;
295 #endif
296
297   // Weak; owned by the Profile.
298   PrefService* prefs_;
299
300   // Whether this settings map is for an OTR session.
301   bool is_off_the_record_;
302
303   // Content setting providers. This is only modified at construction
304   // time and by RegisterExtensionService, both of which should happen
305   // before any other uses of it.
306   ProviderMap content_settings_providers_;
307
308   ObserverList<content_settings::Observer> observers_;
309
310   DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap);
311 };
312
313 #endif  // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_