OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Maps hostnames to custom content settings. Written on the UI thread and read | 5 // Maps hostnames to custom content settings. Written on the UI thread and read |
6 // on any thread. One instance per profile. | 6 // on any thread. One instance per profile. |
7 | 7 |
8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 8 #ifndef CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 9 #define CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/tuple.h" | 19 #include "base/tuple.h" |
20 #include "chrome/browser/content_settings/content_settings_observer.h" | 20 #include "chrome/browser/content_settings/content_settings_observer.h" |
21 #include "chrome/browser/prefs/pref_change_registrar.h" | 21 #include "chrome/browser/prefs/pref_change_registrar.h" |
22 #include "chrome/common/content_settings.h" | 22 #include "chrome/common/content_settings.h" |
23 #include "chrome/common/content_settings_pattern.h" | 23 #include "chrome/common/content_settings_pattern.h" |
24 #include "content/public/browser/notification_observer.h" | |
25 #include "content/public/browser/notification_registrar.h" | |
26 | 24 |
27 namespace base { | 25 namespace base { |
28 class Value; | 26 class Value; |
29 } // namespace base | 27 } // namespace base |
30 | 28 |
31 namespace content_settings { | 29 namespace content_settings { |
32 class ProviderInterface; | 30 class ProviderInterface; |
33 } // namespace content_settings | 31 } // namespace content_settings |
34 | 32 |
35 class ExtensionService; | 33 class ExtensionService; |
36 class GURL; | 34 class GURL; |
37 class PrefService; | 35 class PrefService; |
38 | 36 |
39 class HostContentSettingsMap | 37 class HostContentSettingsMap |
40 : public content_settings::Observer, | 38 : public content_settings::Observer, |
41 public content::NotificationObserver, | |
42 public base::RefCountedThreadSafe<HostContentSettingsMap> { | 39 public base::RefCountedThreadSafe<HostContentSettingsMap> { |
43 public: | 40 public: |
44 enum ProviderType { | 41 enum ProviderType { |
45 POLICY_PROVIDER = 0, | 42 POLICY_PROVIDER = 0, |
46 EXTENSION_PROVIDER = 1, | 43 EXTENSION_PROVIDER = 1, |
47 PREF_PROVIDER, | 44 PREF_PROVIDER, |
48 DEFAULT_PROVIDER, | 45 DEFAULT_PROVIDER, |
49 NUM_PROVIDER_TYPES, | 46 NUM_PROVIDER_TYPES, |
50 }; | 47 }; |
51 | 48 |
52 HostContentSettingsMap(PrefService* prefs, | 49 HostContentSettingsMap(PrefService* prefs, |
53 ExtensionService* extension_service, | 50 ExtensionService* extension_service, |
54 bool incognito); | 51 bool incognito); |
55 | 52 |
56 static void RegisterUserPrefs(PrefService* prefs); | 53 static void RegisterUserPrefs(PrefService* prefs); |
57 | 54 |
58 // Returns the default setting for a particular content type. If |provider_id| | 55 // Returns the default setting for a particular content type. If |provider_id| |
59 // is not NULL, the id of the provider which provided the default setting is | 56 // is not NULL, the id of the provider which provided the default setting is |
60 // assigned to it. | 57 // assigned to it. |
61 // | 58 // |
62 // This may be called on any thread. | 59 // This may be called on any thread. |
63 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, | 60 ContentSetting GetDefaultContentSetting(ContentSettingsType content_type, |
64 std::string* provider_id) const; | 61 std::string* provider_id) const; |
65 | 62 |
66 // Returns the default settings for all content types. | 63 // Returns the default settings for all content types. |
67 // | 64 // |
68 // This may be called on any thread. | 65 // This may be called on any thread. |
69 ContentSettings GetDefaultContentSettings() const; | 66 ContentSettings GetDefaultContentSettings() const; |
70 | 67 |
71 // Returns a single |ContentSetting| which applies to the given URLs. | 68 // Returns a single |ContentSetting| which applies to the given URLs. Note |
72 // Note that certain internal schemes are whitelisted. | 69 // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|, |
73 // For |CONTENT_TYPE_COOKIES|, |GetCookieContentSetting| should be called, | 70 // |CookieSettings| should be used instead. For content types that can't be |
74 // and for content types that can't be converted to a ContentSetting, | 71 // converted to a |ContentSetting|, |GetContentSettingValue| should be called. |
75 // |GetContentSettingValue| should be called. | |
76 // If there is no content setting, returns CONTENT_SETTING_DEFAULT. | 72 // If there is no content setting, returns CONTENT_SETTING_DEFAULT. |
| 73 // |
77 // May be called on any thread. | 74 // May be called on any thread. |
78 ContentSetting GetContentSetting( | 75 ContentSetting GetContentSetting( |
79 const GURL& primary_url, | 76 const GURL& primary_url, |
80 const GURL& secondary_url, | 77 const GURL& secondary_url, |
81 ContentSettingsType content_type, | 78 ContentSettingsType content_type, |
82 const std::string& resource_identifier) const; | 79 const std::string& resource_identifier) const; |
83 | 80 |
84 // Returns a single content setting |Value| which applies to the given URLs. | 81 // Returns a single content setting |Value| which applies to the given URLs. |
85 // If |primary_pattern| and |secondary_pattern| are not NULL, they are set to | 82 // If |primary_pattern| and |secondary_pattern| are not NULL, they are set to |
86 // the patterns of the applying rule. | 83 // the patterns of the applying rule. |
87 // Note that certain internal schemes are whitelisted. | 84 // Note that certain internal schemes are whitelisted. |
88 // If there is no content setting, returns NULL and leaves |primary_pattern| | 85 // If there is no content setting, returns NULL and leaves |primary_pattern| |
89 // and |secondary_pattern| unchanged. | 86 // and |secondary_pattern| unchanged. |
90 // Otherwise transfers ownership of the resulting |Value| to the caller. | 87 // Otherwise transfers ownership of the resulting |Value| to the caller. |
91 // May be called on any thread. | 88 // May be called on any thread. |
92 base::Value* GetContentSettingValue( | 89 base::Value* GetContentSettingValue( |
93 const GURL& primary_url, | 90 const GURL& primary_url, |
94 const GURL& secondary_url, | 91 const GURL& secondary_url, |
95 ContentSettingsType content_type, | 92 ContentSettingsType content_type, |
96 const std::string& resource_identifier, | 93 const std::string& resource_identifier, |
97 ContentSettingsPattern* primary_pattern, | 94 ContentSettingsPattern* primary_pattern, |
98 ContentSettingsPattern* secondary_pattern) const; | 95 ContentSettingsPattern* secondary_pattern) const; |
99 | 96 |
100 // Gets the content setting for cookies. This takes the third party cookie | 97 // Returns all ContentSettings which apply to the given |primary_url|. For |
101 // flag into account, and therefore needs to know whether we read or write a | 98 // content setting types that require an additional resource identifier, the |
102 // cookie. | 99 // default content setting is returned. |
103 // | |
104 // This may be called on any thread. | |
105 ContentSetting GetCookieContentSetting( | |
106 const GURL& url, | |
107 const GURL& first_party_url, | |
108 bool setting_cookie) const; | |
109 | |
110 // Returns all ContentSettings which apply to the given URLs. For content | |
111 // setting types that require an additional resource identifier, the default | |
112 // content setting is returned. | |
113 // | 100 // |
114 // This may be called on any thread. | 101 // This may be called on any thread. |
115 ContentSettings GetContentSettings( | 102 ContentSettings GetContentSettings( |
116 const GURL& primary_url, | 103 const GURL& primary_url) const; |
117 const GURL& secondary_url) const; | |
118 | 104 |
119 // For a given content type, returns all patterns with a non-default setting, | 105 // For a given content type, returns all patterns with a non-default setting, |
120 // mapped to their actual settings, in the precedence order of the rules. | 106 // mapped to their actual settings, in the precedence order of the rules. |
121 // |settings| must be a non-NULL outparam. | 107 // |settings| must be a non-NULL outparam. |
122 // | 108 // |
123 // This may be called on any thread. | 109 // This may be called on any thread. |
124 void GetSettingsForOneType(ContentSettingsType content_type, | 110 void GetSettingsForOneType(ContentSettingsType content_type, |
125 const std::string& resource_identifier, | 111 const std::string& resource_identifier, |
126 ContentSettingsForOneType* settings) const; | 112 ContentSettingsForOneType* settings) const; |
127 | 113 |
(...skipping 30 matching lines...) Expand all Loading... |
158 ContentSetting setting); | 144 ContentSetting setting); |
159 | 145 |
160 // Clears all host-specific settings for one content type. | 146 // Clears all host-specific settings for one content type. |
161 // | 147 // |
162 // This should only be called on the UI thread. | 148 // This should only be called on the UI thread. |
163 void ClearSettingsForOneType(ContentSettingsType content_type); | 149 void ClearSettingsForOneType(ContentSettingsType content_type); |
164 | 150 |
165 static bool IsSettingAllowedForType(ContentSetting setting, | 151 static bool IsSettingAllowedForType(ContentSetting setting, |
166 ContentSettingsType content_type); | 152 ContentSettingsType content_type); |
167 | 153 |
168 // This setting trumps any host-specific settings. | |
169 bool BlockThirdPartyCookies() const { return block_third_party_cookies_; } | |
170 bool IsBlockThirdPartyCookiesManaged() const { | |
171 return is_block_third_party_cookies_managed_; | |
172 } | |
173 | |
174 // Sets whether we block all third-party cookies. This method must not be | |
175 // invoked on an incognito map. | |
176 // | |
177 // This should only be called on the UI thread. | |
178 void SetBlockThirdPartyCookies(bool block); | |
179 | |
180 // Detaches the HostContentSettingsMap from all Profile-related objects like | 154 // Detaches the HostContentSettingsMap from all Profile-related objects like |
181 // PrefService. This methods needs to be called before destroying the Profile. | 155 // PrefService. This methods needs to be called before destroying the Profile. |
182 // Afterwards, none of the methods above that should only be called on the UI | 156 // Afterwards, none of the methods above that should only be called on the UI |
183 // thread should be called anymore. | 157 // thread should be called anymore. |
184 void ShutdownOnUIThread(); | 158 void ShutdownOnUIThread(); |
185 | 159 |
186 // content_settings::Observer implementation. | 160 // content_settings::Observer implementation. |
187 virtual void OnContentSettingChanged( | 161 virtual void OnContentSettingChanged( |
188 ContentSettingsPattern primary_pattern, | 162 ContentSettingsPattern primary_pattern, |
189 ContentSettingsPattern secondary_pattern, | 163 ContentSettingsPattern secondary_pattern, |
190 ContentSettingsType content_type, | 164 ContentSettingsType content_type, |
191 std::string resource_identifier); | 165 std::string resource_identifier); |
192 | 166 |
193 // content::NotificationObserver implementation. | 167 // Returns true if we should allow all content types for this URL. This is |
194 virtual void Observe(int type, | 168 // true for various internal objects like chrome:// URLs, so UI and other |
195 const content::NotificationSource& source, | 169 // things users think of as "not webpages" don't break. |
196 const content::NotificationDetails& details); | 170 static bool ShouldAllowAllContent(const GURL& url, |
| 171 ContentSettingsType content_type); |
197 | 172 |
198 private: | 173 private: |
199 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; | 174 friend class base::RefCountedThreadSafe<HostContentSettingsMap>; |
200 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; | 175 friend class HostContentSettingsMapTest_NonDefaultSettings_Test; |
201 | 176 |
202 typedef std::map<ProviderType, content_settings::ProviderInterface*> | 177 typedef std::map<ProviderType, content_settings::ProviderInterface*> |
203 ProviderMap; | 178 ProviderMap; |
204 typedef ProviderMap::iterator ProviderIterator; | 179 typedef ProviderMap::iterator ProviderIterator; |
205 typedef ProviderMap::const_iterator ConstProviderIterator; | 180 typedef ProviderMap::const_iterator ConstProviderIterator; |
206 | 181 |
(...skipping 16 matching lines...) Expand all Loading... |
223 const content_settings::ProviderInterface* provider, | 198 const content_settings::ProviderInterface* provider, |
224 ProviderType provider_type, | 199 ProviderType provider_type, |
225 ContentSettingsType content_type, | 200 ContentSettingsType content_type, |
226 const std::string& resource_identifier, | 201 const std::string& resource_identifier, |
227 ContentSettingsForOneType* settings, | 202 ContentSettingsForOneType* settings, |
228 bool incognito) const; | 203 bool incognito) const; |
229 | 204 |
230 // Weak; owned by the Profile. | 205 // Weak; owned by the Profile. |
231 PrefService* prefs_; | 206 PrefService* prefs_; |
232 | 207 |
233 PrefChangeRegistrar pref_change_registrar_; | |
234 | |
235 // Whether this settings map is for an OTR session. | 208 // Whether this settings map is for an OTR session. |
236 bool is_off_the_record_; | 209 bool is_off_the_record_; |
237 | 210 |
238 // Whether we are currently updating preferences, this is used to ignore | 211 // Whether we are currently updating preferences, this is used to ignore |
239 // notifications from the preferences service that we triggered ourself. | 212 // notifications from the preferences service that we triggered ourself. |
240 bool updating_preferences_; | 213 bool updating_preferences_; |
241 | 214 |
242 // Content setting providers. | 215 // Content setting providers. |
243 ProviderMap content_settings_providers_; | 216 ProviderMap content_settings_providers_; |
244 | 217 |
245 // Used around accesses to the following objects to guarantee thread safety. | 218 // Used around accesses to the following objects to guarantee thread safety. |
246 mutable base::Lock lock_; | 219 mutable base::Lock lock_; |
247 | 220 |
248 // Misc global settings. | |
249 bool block_third_party_cookies_; | |
250 bool is_block_third_party_cookies_managed_; | |
251 | |
252 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); | 221 DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); |
253 }; | 222 }; |
254 | 223 |
255 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ | 224 #endif // CHROME_BROWSER_CONTENT_SETTINGS_HOST_CONTENT_SETTINGS_MAP_H_ |
OLD | NEW |