OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ | 5 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ |
6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ | 6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 17 matching lines...) Expand all Loading... |
28 namespace base { | 28 namespace base { |
29 class Value; | 29 class Value; |
30 } // namespace base | 30 } // namespace base |
31 | 31 |
32 namespace extensions { | 32 namespace extensions { |
33 | 33 |
34 class Extension; | 34 class Extension; |
35 class RulesCacheDelegate; | 35 class RulesCacheDelegate; |
36 | 36 |
37 // A base class for RulesRegistries that takes care of storing the | 37 // A base class for RulesRegistries that takes care of storing the |
38 // core_api::events::Rule objects. It contains all the methods that need to run | 38 // api::events::Rule objects. It contains all the methods that need to run |
39 // on the registry thread; methods that need to run on the UI thread are | 39 // on the registry thread; methods that need to run on the UI thread are |
40 // separated in the RulesCacheDelegate object. | 40 // separated in the RulesCacheDelegate object. |
41 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { | 41 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { |
42 public: | 42 public: |
43 enum Defaults { DEFAULT_PRIORITY = 100 }; | 43 enum Defaults { DEFAULT_PRIORITY = 100 }; |
44 // After the RulesCacheDelegate object (the part of the registry which runs on | 44 // After the RulesCacheDelegate object (the part of the registry which runs on |
45 // the UI thread) is created, a pointer to it is passed to |*ui_part|. | 45 // the UI thread) is created, a pointer to it is passed to |*ui_part|. |
46 // In tests, |browser_context| and |ui_part| can be NULL (at the same time). | 46 // In tests, |browser_context| and |ui_part| can be NULL (at the same time). |
47 // In that case the storage functionality disabled (no RulesCacheDelegate | 47 // In that case the storage functionality disabled (no RulesCacheDelegate |
48 // object created). | 48 // object created). |
(...skipping 17 matching lines...) Expand all Loading... |
66 // declarative extension APIs. It is guaranteed that each rule in |rules| has | 66 // declarative extension APIs. It is guaranteed that each rule in |rules| has |
67 // a unique name within the scope of |extension_id| that has not been | 67 // a unique name within the scope of |extension_id| that has not been |
68 // registered before, unless it has been removed again. | 68 // registered before, unless it has been removed again. |
69 // The ownership of rules remains with the caller. | 69 // The ownership of rules remains with the caller. |
70 // | 70 // |
71 // Returns an empty string if the function is successful or an error | 71 // Returns an empty string if the function is successful or an error |
72 // message otherwise. | 72 // message otherwise. |
73 // | 73 // |
74 // IMPORTANT: This function is atomic. Either all rules that are deemed | 74 // IMPORTANT: This function is atomic. Either all rules that are deemed |
75 // relevant are added or none. | 75 // relevant are added or none. |
76 std::string AddRules( | 76 std::string AddRules(const std::string& extension_id, |
77 const std::string& extension_id, | 77 const std::vector<linked_ptr<api::events::Rule>>& rules); |
78 const std::vector<linked_ptr<core_api::events::Rule>>& rules); | |
79 | 78 |
80 // Unregisters all rules listed in |rule_identifiers| and owned by | 79 // Unregisters all rules listed in |rule_identifiers| and owned by |
81 // |extension_id| from this RulesRegistry. | 80 // |extension_id| from this RulesRegistry. |
82 // Some or all IDs in |rule_identifiers| may not be stored in this | 81 // Some or all IDs in |rule_identifiers| may not be stored in this |
83 // RulesRegistry and are ignored. | 82 // RulesRegistry and are ignored. |
84 // | 83 // |
85 // Returns an empty string if the function is successful or an error | 84 // Returns an empty string if the function is successful or an error |
86 // message otherwise. | 85 // message otherwise. |
87 // | 86 // |
88 // IMPORTANT: This function is atomic. Either all rules that are deemed | 87 // IMPORTANT: This function is atomic. Either all rules that are deemed |
89 // relevant are removed or none. | 88 // relevant are removed or none. |
90 std::string RemoveRules( | 89 std::string RemoveRules( |
91 const std::string& extension_id, | 90 const std::string& extension_id, |
92 const std::vector<std::string>& rule_identifiers); | 91 const std::vector<std::string>& rule_identifiers); |
93 | 92 |
94 // Same as RemoveAllRules but acts on all rules owned by |extension_id|. | 93 // Same as RemoveAllRules but acts on all rules owned by |extension_id|. |
95 std::string RemoveAllRules(const std::string& extension_id); | 94 std::string RemoveAllRules(const std::string& extension_id); |
96 | 95 |
97 // Returns all rules listed in |rule_identifiers| and owned by |extension_id| | 96 // Returns all rules listed in |rule_identifiers| and owned by |extension_id| |
98 // registered in this RuleRegistry. Entries in |rule_identifiers| that | 97 // registered in this RuleRegistry. Entries in |rule_identifiers| that |
99 // are unknown are ignored. | 98 // are unknown are ignored. |
100 // | 99 // |
101 // The returned rules are stored in |out|. Ownership is passed to the caller. | 100 // The returned rules are stored in |out|. Ownership is passed to the caller. |
102 void GetRules(const std::string& extension_id, | 101 void GetRules(const std::string& extension_id, |
103 const std::vector<std::string>& rule_identifiers, | 102 const std::vector<std::string>& rule_identifiers, |
104 std::vector<linked_ptr<core_api::events::Rule>>* out); | 103 std::vector<linked_ptr<api::events::Rule>>* out); |
105 | 104 |
106 // Same as GetRules but returns all rules owned by |extension_id|. | 105 // Same as GetRules but returns all rules owned by |extension_id|. |
107 void GetAllRules(const std::string& extension_id, | 106 void GetAllRules(const std::string& extension_id, |
108 std::vector<linked_ptr<core_api::events::Rule>>* out); | 107 std::vector<linked_ptr<api::events::Rule>>* out); |
109 | 108 |
110 // Called to notify the RulesRegistry that the extension availability has | 109 // Called to notify the RulesRegistry that the extension availability has |
111 // changed, so that the registry can update which rules are active. | 110 // changed, so that the registry can update which rules are active. |
112 void OnExtensionUnloaded(const Extension* extension); | 111 void OnExtensionUnloaded(const Extension* extension); |
113 void OnExtensionUninstalled(const Extension* extension); | 112 void OnExtensionUninstalled(const Extension* extension); |
114 void OnExtensionLoaded(const Extension* extension); | 113 void OnExtensionLoaded(const Extension* extension); |
115 | 114 |
116 // Returns the number of entries in used_rule_identifiers_ for leak detection. | 115 // Returns the number of entries in used_rule_identifiers_ for leak detection. |
117 // Every ExtensionId counts as one entry, even if it contains no rules. | 116 // Every ExtensionId counts as one entry, even if it contains no rules. |
118 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | 117 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; |
(...skipping 17 matching lines...) Expand all Loading... |
136 int id() const { return id_; } | 135 int id() const { return id_; } |
137 | 136 |
138 protected: | 137 protected: |
139 virtual ~RulesRegistry(); | 138 virtual ~RulesRegistry(); |
140 | 139 |
141 // These functions need to apply the rules to the browser, while the base | 140 // These functions need to apply the rules to the browser, while the base |
142 // class will handle defaulting empty fields before calling *Impl, and will | 141 // class will handle defaulting empty fields before calling *Impl, and will |
143 // automatically cache the rules and re-call *Impl on browser startup. | 142 // automatically cache the rules and re-call *Impl on browser startup. |
144 virtual std::string AddRulesImpl( | 143 virtual std::string AddRulesImpl( |
145 const std::string& extension_id, | 144 const std::string& extension_id, |
146 const std::vector<linked_ptr<core_api::events::Rule>>& rules) = 0; | 145 const std::vector<linked_ptr<api::events::Rule>>& rules) = 0; |
147 virtual std::string RemoveRulesImpl( | 146 virtual std::string RemoveRulesImpl( |
148 const std::string& extension_id, | 147 const std::string& extension_id, |
149 const std::vector<std::string>& rule_identifiers) = 0; | 148 const std::vector<std::string>& rule_identifiers) = 0; |
150 virtual std::string RemoveAllRulesImpl( | 149 virtual std::string RemoveAllRulesImpl( |
151 const std::string& extension_id) = 0; | 150 const std::string& extension_id) = 0; |
152 | 151 |
153 private: | 152 private: |
154 friend class base::RefCountedThreadSafe<RulesRegistry>; | 153 friend class base::RefCountedThreadSafe<RulesRegistry>; |
155 friend class RulesCacheDelegate; | 154 friend class RulesCacheDelegate; |
156 | 155 |
157 typedef std::string ExtensionId; | 156 typedef std::string ExtensionId; |
158 typedef std::string RuleId; | 157 typedef std::string RuleId; |
159 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; | 158 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; |
160 typedef std::map<RulesDictionaryKey, linked_ptr<core_api::events::Rule>> | 159 typedef std::map<RulesDictionaryKey, linked_ptr<api::events::Rule>> |
161 RulesDictionary; | 160 RulesDictionary; |
162 enum ProcessChangedRulesState { | 161 enum ProcessChangedRulesState { |
163 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. | 162 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. |
164 NEVER_PROCESS, | 163 NEVER_PROCESS, |
165 // A task to call ProcessChangedRules is scheduled for future execution. | 164 // A task to call ProcessChangedRules is scheduled for future execution. |
166 SCHEDULED_FOR_PROCESSING, | 165 SCHEDULED_FOR_PROCESSING, |
167 // No task to call ProcessChangedRules is scheduled yet, but it is possible | 166 // No task to call ProcessChangedRules is scheduled yet, but it is possible |
168 // to schedule one. | 167 // to schedule one. |
169 NOT_SCHEDULED_FOR_PROCESSING | 168 NOT_SCHEDULED_FOR_PROCESSING |
170 }; | 169 }; |
171 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap; | 170 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap; |
172 | 171 |
173 base::WeakPtr<RulesRegistry> GetWeakPtr() { | 172 base::WeakPtr<RulesRegistry> GetWeakPtr() { |
174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
175 return weak_ptr_factory_.GetWeakPtr(); | 174 return weak_ptr_factory_.GetWeakPtr(); |
176 } | 175 } |
177 | 176 |
178 // Internal implementation of the AddRules interface which adds | 177 // Internal implementation of the AddRules interface which adds |
179 // |from_manifest| which is true when the source is the manifest. | 178 // |from_manifest| which is true when the source is the manifest. |
180 std::string AddRulesInternal( | 179 std::string AddRulesInternal( |
181 const std::string& extension_id, | 180 const std::string& extension_id, |
182 const std::vector<linked_ptr<core_api::events::Rule>>& rules, | 181 const std::vector<linked_ptr<api::events::Rule>>& rules, |
183 RulesDictionary* out); | 182 RulesDictionary* out); |
184 | 183 |
185 // The precondition for calling this method is that all rules have unique IDs. | 184 // The precondition for calling this method is that all rules have unique IDs. |
186 // AddRules establishes this precondition and calls into this method. | 185 // AddRules establishes this precondition and calls into this method. |
187 // Stored rules already meet this precondition and so they avoid calling | 186 // Stored rules already meet this precondition and so they avoid calling |
188 // CheckAndFillInOptionalRules for improved performance. | 187 // CheckAndFillInOptionalRules for improved performance. |
189 // | 188 // |
190 // Returns an empty string if the function is successful or an error | 189 // Returns an empty string if the function is successful or an error |
191 // message otherwise. | 190 // message otherwise. |
192 std::string AddRulesNoFill( | 191 std::string AddRulesNoFill( |
193 const std::string& extension_id, | 192 const std::string& extension_id, |
194 const std::vector<linked_ptr<core_api::events::Rule>>& rules, | 193 const std::vector<linked_ptr<api::events::Rule>>& rules, |
195 RulesDictionary* out); | 194 RulesDictionary* out); |
196 | 195 |
197 // Same as GetRules but returns all rules owned by |extension_id| for a given | 196 // Same as GetRules but returns all rules owned by |extension_id| for a given |
198 // |rules| dictionary. | 197 // |rules| dictionary. |
199 void GetRules(const std::string& extension_id, | 198 void GetRules(const std::string& extension_id, |
200 RulesDictionary& rules, | 199 RulesDictionary& rules, |
201 std::vector<linked_ptr<core_api::events::Rule>>* out); | 200 std::vector<linked_ptr<api::events::Rule>>* out); |
202 | 201 |
203 // Common processing after extension's rules have changed. | 202 // Common processing after extension's rules have changed. |
204 void ProcessChangedRules(const std::string& extension_id); | 203 void ProcessChangedRules(const std::string& extension_id); |
205 | 204 |
206 // Calls ProcessChangedRules if | 205 // Calls ProcessChangedRules if |
207 // |process_changed_rules_requested_(extension_id)| == | 206 // |process_changed_rules_requested_(extension_id)| == |
208 // NOT_SCHEDULED_FOR_PROCESSING. | 207 // NOT_SCHEDULED_FOR_PROCESSING. |
209 void MaybeProcessChangedRules(const std::string& extension_id); | 208 void MaybeProcessChangedRules(const std::string& extension_id); |
210 | 209 |
211 // This method implements the functionality of RemoveAllRules, except for not | 210 // This method implements the functionality of RemoveAllRules, except for not |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 const std::string& rule_id) const; | 256 const std::string& rule_id) const; |
258 | 257 |
259 // Creates an ID that is unique within the scope of|extension_id|. | 258 // Creates an ID that is unique within the scope of|extension_id|. |
260 std::string GenerateUniqueId(const std::string& extension_id); | 259 std::string GenerateUniqueId(const std::string& extension_id); |
261 | 260 |
262 // Verifies that all |rules| have unique IDs or initializes them with | 261 // Verifies that all |rules| have unique IDs or initializes them with |
263 // unique IDs if they don't have one. In case of duplicate IDs, this function | 262 // unique IDs if they don't have one. In case of duplicate IDs, this function |
264 // returns a non-empty error message. | 263 // returns a non-empty error message. |
265 std::string CheckAndFillInOptionalRules( | 264 std::string CheckAndFillInOptionalRules( |
266 const std::string& extension_id, | 265 const std::string& extension_id, |
267 const std::vector<linked_ptr<core_api::events::Rule>>& rules); | 266 const std::vector<linked_ptr<api::events::Rule>>& rules); |
268 | 267 |
269 // Initializes the priority fields in case they have not been set. | 268 // Initializes the priority fields in case they have not been set. |
270 void FillInOptionalPriorities( | 269 void FillInOptionalPriorities( |
271 const std::vector<linked_ptr<core_api::events::Rule>>& rules); | 270 const std::vector<linked_ptr<api::events::Rule>>& rules); |
272 | 271 |
273 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. | 272 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. |
274 void RemoveUsedRuleIdentifiers(const std::string& extension_id, | 273 void RemoveUsedRuleIdentifiers(const std::string& extension_id, |
275 const std::vector<std::string>& identifiers); | 274 const std::vector<std::string>& identifiers); |
276 | 275 |
277 // Same as RemoveUsedRuleIdentifiers but operates on all rules of | 276 // Same as RemoveUsedRuleIdentifiers but operates on all rules of |
278 // |extension_id|. | 277 // |extension_id|. |
279 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); | 278 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); |
280 | 279 |
281 typedef std::string RuleIdentifier; | 280 typedef std::string RuleIdentifier; |
(...skipping 11 matching lines...) Expand all Loading... |
293 base::WeakPtr<RulesCacheDelegate> cache_delegate_; | 292 base::WeakPtr<RulesCacheDelegate> cache_delegate_; |
294 | 293 |
295 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | 294 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; |
296 | 295 |
297 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 296 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
298 }; | 297 }; |
299 | 298 |
300 } // namespace extensions | 299 } // namespace extensions |
301 | 300 |
302 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ | 301 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ |
OLD | NEW |