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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "base/tuple.h" | 16 #include "base/tuple.h" |
17 #include "chrome/browser/content_settings/content_settings_pattern.h" | 17 #include "chrome/browser/content_settings/content_settings_pattern.h" |
18 #include "chrome/browser/content_settings/content_settings_provider.h" | 18 #include "chrome/browser/content_settings/content_settings_provider.h" |
19 #include "chrome/browser/extensions/extension_prefs_scope.h" | 19 #include "chrome/browser/extensions/extension_prefs_scope.h" |
20 #include "chrome/common/content_settings.h" | 20 #include "chrome/common/content_settings.h" |
21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 | 22 |
23 class DictionaryValue; | 23 class DictionaryValue; |
24 class ListValue; | 24 class ListValue; |
25 | 25 |
26 // This class is the backend for extension-defined content settings. It is used | 26 // This class is the backend for extension-defined content settings. It is used |
27 // by the content_settings::ExtensionProvider to integrate its settings into the | 27 // by the content_settings::ExtensionProvider to integrate its settings into the |
28 // HostContentSettingsMap and by the content settings extension API to provide | 28 // HostContentSettingsMap and by the content settings extension API to provide |
29 // extensions with access to content settings. | 29 // extensions with access to content settings. |
30 class ExtensionContentSettingsStore { | 30 class ExtensionContentSettingsStore |
| 31 : public base::RefCountedThreadSafe<ExtensionContentSettingsStore> { |
31 public: | 32 public: |
32 class Observer { | 33 class Observer { |
33 public: | 34 public: |
34 virtual ~Observer() {} | 35 virtual ~Observer() {} |
35 | 36 |
36 // Called when a content setting changes in the | 37 // Called when a content setting changes in the |
37 // ExtensionContentSettingsStore. | 38 // ExtensionContentSettingsStore. |
38 virtual void OnContentSettingChanged( | 39 virtual void OnContentSettingChanged( |
39 const std::string& extension_id, | 40 const std::string& extension_id, |
40 bool incognito) = 0; | 41 bool incognito) = 0; |
41 | |
42 // Called when the ExtensionContentSettingsStore is being destroyed, so | |
43 // observers can invalidate their weak references. | |
44 virtual void OnDestruction() = 0; | |
45 }; | 42 }; |
46 | 43 |
47 ExtensionContentSettingsStore(); | 44 ExtensionContentSettingsStore(); |
48 virtual ~ExtensionContentSettingsStore(); | |
49 | 45 |
50 // ////////////////////////////////////////////////////////////////////////// | 46 // ////////////////////////////////////////////////////////////////////////// |
51 | 47 |
52 // Sets the content |setting| for |pattern| of extension |ext_id|. The | 48 // Sets the content |setting| for |pattern| of extension |ext_id|. The |
53 // |incognito| flag allow to set whether the provided setting is for | 49 // |incognito| flag allow to set whether the provided setting is for |
54 // incognito mode only. | 50 // incognito mode only. |
55 // Precondition: the extension must be registered. | 51 // Precondition: the extension must be registered. |
56 // This method should only be called on the UI thread. | 52 // This method should only be called on the UI thread. |
57 void SetExtensionContentSetting( | 53 void SetExtensionContentSetting( |
58 const std::string& ext_id, | 54 const std::string& ext_id, |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // visible. | 105 // visible. |
110 void SetExtensionState(const std::string& ext_id, bool is_enabled); | 106 void SetExtensionState(const std::string& ext_id, bool is_enabled); |
111 | 107 |
112 // Adds |observer|. This method should only be called on the UI thread. | 108 // Adds |observer|. This method should only be called on the UI thread. |
113 void AddObserver(Observer* observer); | 109 void AddObserver(Observer* observer); |
114 | 110 |
115 // Remove |observer|. This method should only be called on the UI thread. | 111 // Remove |observer|. This method should only be called on the UI thread. |
116 void RemoveObserver(Observer* observer); | 112 void RemoveObserver(Observer* observer); |
117 | 113 |
118 private: | 114 private: |
| 115 friend class base::RefCountedThreadSafe<ExtensionContentSettingsStore>; |
| 116 |
119 struct ExtensionEntry; | 117 struct ExtensionEntry; |
120 struct ContentSettingSpec { | 118 struct ContentSettingSpec { |
121 ContentSettingSpec(const ContentSettingsPattern& primary_pattern, | 119 ContentSettingSpec(const ContentSettingsPattern& primary_pattern, |
122 const ContentSettingsPattern& secondary_pattern, | 120 const ContentSettingsPattern& secondary_pattern, |
123 ContentSettingsType type, | 121 ContentSettingsType type, |
124 const content_settings::ResourceIdentifier& identifier, | 122 const content_settings::ResourceIdentifier& identifier, |
125 ContentSetting setting); | 123 ContentSetting setting); |
126 | 124 |
127 ContentSettingsPattern primary_pattern; | 125 ContentSettingsPattern primary_pattern; |
128 ContentSettingsPattern secondary_pattern; | 126 ContentSettingsPattern secondary_pattern; |
129 ContentSettingsType content_type; | 127 ContentSettingsType content_type; |
130 content_settings::ResourceIdentifier resource_identifier; | 128 content_settings::ResourceIdentifier resource_identifier; |
131 ContentSetting setting; | 129 ContentSetting setting; |
132 }; | 130 }; |
133 | 131 |
134 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; | 132 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; |
135 | 133 |
136 typedef std::list<ContentSettingSpec> ContentSettingSpecList; | 134 typedef std::list<ContentSettingSpec> ContentSettingSpecList; |
137 | 135 |
| 136 virtual ~ExtensionContentSettingsStore(); |
| 137 |
138 ContentSetting GetContentSettingFromSpecList( | 138 ContentSetting GetContentSettingFromSpecList( |
139 const GURL& embedded_url, | 139 const GURL& embedded_url, |
140 const GURL& top_level_url, | 140 const GURL& top_level_url, |
141 ContentSettingsType type, | 141 ContentSettingsType type, |
142 const content_settings::ResourceIdentifier& identifier, | 142 const content_settings::ResourceIdentifier& identifier, |
143 const ContentSettingSpecList& setting_spec_list) const; | 143 const ContentSettingSpecList& setting_spec_list) const; |
144 | 144 |
145 ContentSettingSpecList* GetContentSettingSpecList( | 145 ContentSettingSpecList* GetContentSettingSpecList( |
146 const std::string& ext_id, | 146 const std::string& ext_id, |
147 ExtensionPrefsScope scope); | 147 ExtensionPrefsScope scope); |
148 | 148 |
149 const ContentSettingSpecList* GetContentSettingSpecList( | 149 const ContentSettingSpecList* GetContentSettingSpecList( |
150 const std::string& ext_id, | 150 const std::string& ext_id, |
151 ExtensionPrefsScope scope) const; | 151 ExtensionPrefsScope scope) const; |
152 | 152 |
153 // Adds all content setting rules for |type| and |identifier| found in | 153 // Adds all content setting rules for |type| and |identifier| found in |
154 // |setting_spec_list| to |rules|. | 154 // |setting_spec_list| to |rules|. |
155 static void AddRules(ContentSettingsType type, | 155 static void AddRules(ContentSettingsType type, |
156 const content_settings::ResourceIdentifier& identifier, | 156 const content_settings::ResourceIdentifier& identifier, |
157 const ContentSettingSpecList* setting_spec_list, | 157 const ContentSettingSpecList* setting_spec_list, |
158 content_settings::ProviderInterface::Rules* rules); | 158 content_settings::ProviderInterface::Rules* rules); |
159 | 159 |
160 void NotifyOfContentSettingChanged(const std::string& extension_id, | 160 void NotifyOfContentSettingChanged(const std::string& extension_id, |
161 bool incognito); | 161 bool incognito); |
162 | 162 |
163 void NotifyOfDestruction(); | |
164 | |
165 bool OnCorrectThread(); | 163 bool OnCorrectThread(); |
166 | 164 |
167 ExtensionEntryMap entries_; | 165 ExtensionEntryMap entries_; |
168 | 166 |
169 ObserverList<Observer, false> observers_; | 167 ObserverList<Observer, false> observers_; |
170 | 168 |
171 mutable base::Lock lock_; | 169 mutable base::Lock lock_; |
172 | 170 |
173 DISALLOW_COPY_AND_ASSIGN(ExtensionContentSettingsStore); | 171 DISALLOW_COPY_AND_ASSIGN(ExtensionContentSettingsStore); |
174 }; | 172 }; |
175 | 173 |
176 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ | 174 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTENT_SETTINGS_STORE_H_ |
OLD | NEW |