Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / content_settings / host_content_settings_map.h
blob602ff15f1f76535eef7e7a8f98915fae461d27b8
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_
11 #include <map>
12 #include <string>
13 #include <vector>
15 #include "base/basictypes.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/prefs/public/pref_change_registrar.h"
18 #include "base/synchronization/lock.h"
19 #include "base/tuple.h"
20 #include "chrome/browser/content_settings/content_settings_observer.h"
21 #include "chrome/common/content_settings.h"
22 #include "chrome/common/content_settings_pattern.h"
23 #include "chrome/common/content_settings_types.h"
25 namespace base {
26 class Value;
27 } // namespace base
29 namespace content_settings {
30 class ProviderInterface;
31 } // namespace content_settings
33 class ExtensionService;
34 class GURL;
35 class PrefService;
37 class HostContentSettingsMap
38 : public content_settings::Observer,
39 public base::RefCountedThreadSafe<HostContentSettingsMap> {
40 public:
41 enum ProviderType {
42 INTERNAL_EXTENSION_PROVIDER = 0,
43 POLICY_PROVIDER,
44 CUSTOM_EXTENSION_PROVIDER,
45 PREF_PROVIDER,
46 DEFAULT_PROVIDER,
47 NUM_PROVIDER_TYPES,
50 HostContentSettingsMap(PrefService* prefs,
51 bool incognito);
53 #if defined(ENABLE_EXTENSIONS)
54 // In some cases, the ExtensionService is not available at the time the
55 // HostContentSettingsMap is constructed. In these cases, we register the
56 // service once it's available.
57 void RegisterExtensionService(ExtensionService* extension_service);
58 #endif
60 static void RegisterUserPrefs(PrefService* prefs);
62 // Returns the default setting for a particular content type. If |provider_id|
63 // is not NULL, the id of the provider which provided the default setting is
64 // assigned to it.
66 // This may be called on any thread.
67 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type,
68 std::string* provider_id) const;
70 // Returns a single |ContentSetting| which applies to the given URLs. Note
71 // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|,
72 // |CookieSettings| should be used instead. For content types that can't be
73 // converted to a |ContentSetting|, |GetContentSettingValue| should be called.
74 // If there is no content setting, returns CONTENT_SETTING_DEFAULT.
76 // May be called on any thread.
77 ContentSetting GetContentSetting(
78 const GURL& primary_url,
79 const GURL& secondary_url,
80 ContentSettingsType content_type,
81 const std::string& resource_identifier) const;
83 // Returns a single content setting |Value| which applies to the given URLs.
84 // If |info| is not NULL, then the |source| field of |info| is set to the
85 // source of the returned |Value| (POLICY, EXTENSION, USER, ...) and the
86 // |primary_pattern| and the |secondary_pattern| fields of |info| are set to
87 // the patterns of the applying rule. Note that certain internal schemes are
88 // whitelisted. For whitelisted schemes the |source| field of |info| is set
89 // the |SETTING_SOURCE_WHITELIST| and the |primary_pattern| and
90 // |secondary_pattern| are set to a wildcard pattern. If there is no content
91 // setting, NULL is returned and the |source| field of |info| is set to
92 // |SETTING_SOURCE_NONE|. The pattern fiels of |info| are set to empty
93 // patterns.
94 // The ownership of the resulting |Value| is transfered to the caller.
95 // May be called on any thread.
96 base::Value* GetWebsiteSetting(
97 const GURL& primary_url,
98 const GURL& secondary_url,
99 ContentSettingsType content_type,
100 const std::string& resource_identifier,
101 content_settings::SettingInfo* info) const;
103 // For a given content type, returns all patterns with a non-default setting,
104 // mapped to their actual settings, in the precedence order of the rules.
105 // |settings| must be a non-NULL outparam.
107 // This may be called on any thread.
108 void GetSettingsForOneType(ContentSettingsType content_type,
109 const std::string& resource_identifier,
110 ContentSettingsForOneType* settings) const;
112 // Sets the default setting for a particular content type. This method must
113 // not be invoked on an incognito map.
115 // This should only be called on the UI thread.
116 void SetDefaultContentSetting(ContentSettingsType content_type,
117 ContentSetting setting);
119 // Sets the content |setting| for the given patterns, |content_type| and
120 // |resource_identifier|. Setting the value to CONTENT_SETTING_DEFAULT causes
121 // the default setting for that type to be used when loading pages matching
122 // this pattern.
123 // NOTICE: This is just a convenience method for content types that use
124 // |CONTENT_SETTING| as their data type. For content types that use other
125 // data types please use the method SetWebsiteSetting.
127 // This should only be called on the UI thread.
128 void SetContentSetting(const ContentSettingsPattern& primary_pattern,
129 const ContentSettingsPattern& secondary_pattern,
130 ContentSettingsType content_type,
131 const std::string& resource_identifier,
132 ContentSetting setting);
134 // Sets the |value| for the given patterns, |content_type| and
135 // |resource_identifier|. Setting the value to NULL causes the default value
136 // for that type to be used when loading pages matching this pattern.
138 // Takes ownership of the passed value.
139 void SetWebsiteSetting(const ContentSettingsPattern& primary_pattern,
140 const ContentSettingsPattern& secondary_pattern,
141 ContentSettingsType content_type,
142 const std::string& resource_identifier,
143 base::Value* value);
145 // Convenience method to add a content setting for the given URLs, making sure
146 // that there is no setting overriding it. For ContentSettingsTypes that
147 // require an resource identifier to be specified, the |resource_identifier|
148 // must be non-empty.
150 // This should only be called on the UI thread.
151 void AddExceptionForURL(const GURL& primary_url,
152 const GURL& secondary_url,
153 ContentSettingsType content_type,
154 const std::string& resource_identifier,
155 ContentSetting setting);
157 // Clears all host-specific settings for one content type.
159 // This should only be called on the UI thread.
160 void ClearSettingsForOneType(ContentSettingsType content_type);
162 static bool IsValueAllowedForType(PrefService* prefs,
163 const base::Value* value,
164 ContentSettingsType content_type);
165 static bool IsSettingAllowedForType(PrefService* prefs,
166 ContentSetting setting,
167 ContentSettingsType content_type);
169 // Returns true if the values for content type are of type dictionary/map.
170 static bool ContentTypeHasCompoundValue(ContentSettingsType type);
172 // Detaches the HostContentSettingsMap from all Profile-related objects like
173 // PrefService. This methods needs to be called before destroying the Profile.
174 // Afterwards, none of the methods above that should only be called on the UI
175 // thread should be called anymore.
176 void ShutdownOnUIThread();
178 // content_settings::Observer implementation.
179 virtual void OnContentSettingChanged(
180 const ContentSettingsPattern& primary_pattern,
181 const ContentSettingsPattern& secondary_pattern,
182 ContentSettingsType content_type,
183 std::string resource_identifier) OVERRIDE;
185 // Returns true if we should allow all content types for this URL. This is
186 // true for various internal objects like chrome:// URLs, so UI and other
187 // things users think of as "not webpages" don't break.
188 static bool ShouldAllowAllContent(const GURL& primary_url,
189 const GURL& secondary_url,
190 ContentSettingsType content_type);
192 // Returns the ProviderType associated with the given source string.
193 // TODO(estade): I regret adding this. At the moment there are no legitimate
194 // uses. We should stick to ProviderType rather than string so we don't have
195 // to convert backwards.
196 static ProviderType GetProviderTypeFromSource(const std::string& source);
198 bool is_off_the_record() const {
199 return is_off_the_record_;
202 private:
203 friend class base::RefCountedThreadSafe<HostContentSettingsMap>;
204 friend class HostContentSettingsMapTest_NonDefaultSettings_Test;
206 typedef std::map<ProviderType, content_settings::ProviderInterface*>
207 ProviderMap;
208 typedef ProviderMap::iterator ProviderIterator;
209 typedef ProviderMap::const_iterator ConstProviderIterator;
211 virtual ~HostContentSettingsMap();
213 ContentSetting GetDefaultContentSettingFromProvider(
214 ContentSettingsType content_type,
215 content_settings::ProviderInterface* provider) const;
217 // Migrate the Clear on exit pref into equivalent content settings.
218 void MigrateObsoleteClearOnExitPref();
220 // Adds content settings for |content_type| and |resource_identifier|,
221 // provided by |provider|, into |settings|. If |incognito| is true, adds only
222 // the content settings which are applicable to the incognito mode and differ
223 // from the normal mode. Otherwise, adds the content settings for the normal
224 // mode.
225 void AddSettingsForOneType(
226 const content_settings::ProviderInterface* provider,
227 ProviderType provider_type,
228 ContentSettingsType content_type,
229 const std::string& resource_identifier,
230 ContentSettingsForOneType* settings,
231 bool incognito) const;
233 // Weak; owned by the Profile.
234 PrefService* prefs_;
236 // Whether this settings map is for an OTR session.
237 bool is_off_the_record_;
239 // Content setting providers.
240 ProviderMap content_settings_providers_;
242 // Used around accesses to the following objects to guarantee thread safety.
243 mutable base::Lock lock_;
245 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap);
248 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_