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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
7 | 7 |
8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | |
9 | |
10 #include <map> | |
11 #include <set> | |
8 #include <string> | 12 #include <string> |
9 #include <vector> | 13 #include <vector> |
10 | 14 |
11 #include "base/memory/linked_ptr.h" | 15 #include "base/callback_forward.h" |
12 #include "base/memory/ref_counted.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/gtest_prod_util.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/memory/weak_ptr.h" | |
13 #include "chrome/common/extensions/api/events.h" | 20 #include "chrome/common/extensions/api/events.h" |
14 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/browser/notification_observer.h" | |
23 #include "content/public/browser/notification_registrar.h" | |
24 #include "extensions/common/one_shot_event.h" | |
25 | |
26 class Profile; | |
15 | 27 |
16 namespace base { | 28 namespace base { |
17 class DictionaryValue; | 29 class Value; |
18 } | 30 } // namespace base |
19 | 31 |
20 namespace extensions { | 32 namespace extensions { |
21 | 33 |
22 class RulesRegistry; | 34 class RulesCacheDelegate; |
23 | 35 |
24 // Interface for rule registries. | 36 // A base class for RulesRegistries that takes care of storing the |
25 // | 37 // RulesRegistry::Rule objects. It contains all the methods that need to run on |
26 // All functions except GetOwnerThread() and the destructor are only called on | 38 // the registry thread; methods that need to run on the UI thread are separated |
27 // the thread indicated by GetOwnerThread(). | 39 // in the RulesCacheDelegate object. |
28 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { | 40 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { |
29 public: | 41 public: |
30 typedef extensions::api::events::Rule Rule; | 42 typedef extensions::api::events::Rule Rule; |
31 | 43 |
32 RulesRegistry(content::BrowserThread::ID owner_thread, | 44 enum Defaults { DEFAULT_PRIORITY = 100 }; |
33 const std::string& event_name) | 45 // After the RulesCacheDelegate object (the part of the registry which runs on |
34 : owner_thread_(owner_thread), event_name_(event_name) {} | 46 // the UI thread) is created, a pointer to it is passed to |*ui_part|. |
47 // If |log_storage_init_delay| is set, the delay caused by loading and | |
48 // registering rules on initialization will be logged with UMA. | |
49 // In tests, |profile| and |ui_part| can be NULL (at the same time). In that | |
50 // case the storage functionality disabled (no RulesCacheDelegate object | |
51 // created) and the |log_storage_init_delay| flag is ignored. | |
52 RulesRegistry(Profile* profile, | |
53 const std::string& event_name, | |
54 content::BrowserThread::ID owner_thread, | |
55 bool log_storage_init_delay, | |
56 scoped_ptr<RulesCacheDelegate>* ui_part); | |
57 | |
58 const OneShotEvent& ready() const { | |
59 return ready_; | |
60 } | |
61 | |
62 // RulesRegistry implementation: | |
vabr (Chromium)
2013/10/29 13:08:24
Is this comment just a relict from RulesRegistryWi
| |
35 | 63 |
36 // Registers |rules|, owned by |extension_id| to this RulesRegistry. | 64 // Registers |rules|, owned by |extension_id| to this RulesRegistry. |
37 // If a concrete RuleRegistry does not support some of the rules, | 65 // If a concrete RuleRegistry does not support some of the rules, |
38 // it may ignore them. | 66 // it may ignore them. |
39 // | 67 // |
40 // |rules| is a list of Rule instances following the definition of the | 68 // |rules| is a list of Rule instances following the definition of the |
41 // declarative extension APIs. It is guaranteed that each rule in |rules| has | 69 // declarative extension APIs. It is guaranteed that each rule in |rules| has |
42 // a unique name within the scope of |extension_id| that has not been | 70 // a unique name within the scope of |extension_id| that has not been |
43 // registered before, unless it has been removed again. | 71 // registered before, unless it has been removed again. |
44 // The ownership of rules remains with the caller. | 72 // The ownership of rules remains with the caller. |
45 // | 73 // |
46 // Returns an empty string if the function is successful or an error | 74 // Returns an empty string if the function is successful or an error |
47 // message otherwise. | 75 // message otherwise. |
48 // | 76 // |
49 // IMPORTANT: This function is atomic. Either all rules that are deemed | 77 // IMPORTANT: This function is atomic. Either all rules that are deemed |
50 // relevant are added or none. | 78 // relevant are added or none. |
51 virtual std::string AddRules( | 79 std::string AddRules( |
52 const std::string& extension_id, | 80 const std::string& extension_id, |
53 const std::vector<linked_ptr<Rule> >& rules) = 0; | 81 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); |
54 | 82 |
55 // Unregisters all rules listed in |rule_identifiers| and owned by | 83 // Unregisters all rules listed in |rule_identifiers| and owned by |
56 // |extension_id| from this RulesRegistry. | 84 // |extension_id| from this RulesRegistry. |
57 // Some or all IDs in |rule_identifiers| may not be stored in this | 85 // Some or all IDs in |rule_identifiers| may not be stored in this |
58 // RulesRegistry and are ignored. | 86 // RulesRegistry and are ignored. |
59 // | 87 // |
60 // Returns an empty string if the function is successful or an error | 88 // Returns an empty string if the function is successful or an error |
61 // message otherwise. | 89 // message otherwise. |
62 // | 90 // |
63 // IMPORTANT: This function is atomic. Either all rules that are deemed | 91 // IMPORTANT: This function is atomic. Either all rules that are deemed |
64 // relevant are removed or none. | 92 // relevant are removed or none. |
65 virtual std::string RemoveRules( | 93 std::string RemoveRules( |
66 const std::string& extension_id, | 94 const std::string& extension_id, |
67 const std::vector<std::string>& rule_identifiers) = 0; | 95 const std::vector<std::string>& rule_identifiers); |
68 | 96 |
69 // Same as RemoveAllRules but acts on all rules owned by |extension_id|. | 97 // Same as RemoveAllRules but acts on all rules owned by |extension_id|. |
70 virtual std::string RemoveAllRules(const std::string& extension_id) = 0; | 98 std::string RemoveAllRules(const std::string& extension_id); |
71 | 99 |
72 // Returns all rules listed in |rule_identifiers| and owned by |extension_id| | 100 // Returns all rules listed in |rule_identifiers| and owned by |extension_id| |
73 // registered in this RuleRegistry. Entries in |rule_identifiers| that | 101 // registered in this RuleRegistry. Entries in |rule_identifiers| that |
74 // are unknown are ignored. | 102 // are unknown are ignored. |
75 // | 103 // |
76 // The returned rules are stored in |out|. Ownership is passed to the caller. | 104 // The returned rules are stored in |out|. Ownership is passed to the caller. |
77 // | 105 // |
78 // Returns an empty string if the function is successful or an error | 106 // Returns an empty string if the function is successful or an error |
79 // message otherwise. | 107 // message otherwise. |
80 virtual std::string GetRules(const std::string& extension_id, | 108 std::string GetRules( |
81 const std::vector<std::string>& rule_identifiers, | 109 const std::string& extension_id, |
82 std::vector<linked_ptr<Rule> >* out) = 0; | 110 const std::vector<std::string>& rule_identifiers, |
111 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | |
83 | 112 |
84 // Same as GetRules but returns all rules owned by |extension_id|. | 113 // Same as GetRules but returns all rules owned by |extension_id|. |
85 virtual std::string GetAllRules(const std::string& extension_id, | 114 std::string GetAllRules( |
86 std::vector<linked_ptr<Rule> >* out) = 0; | 115 const std::string& extension_id, |
116 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | |
87 | 117 |
88 // Called to notify the RulesRegistry that an extension has been unloaded | 118 // Called to notify the RulesRegistry that an extension has been unloaded |
89 // and all rules of this extension need to be removed. | 119 // and all rules of this extension need to be removed. |
90 virtual void OnExtensionUnloaded(const std::string& extension_id) = 0; | 120 void OnExtensionUnloaded(const std::string& extension_id); |
121 | |
122 // Returns the number of entries in used_rule_identifiers_ for leak detection. | |
123 // Every ExtensionId counts as one entry, even if it contains no rules. | |
124 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | |
91 | 125 |
92 // Returns the ID of the thread on which the rules registry lives. | 126 // Returns the ID of the thread on which the rules registry lives. |
93 // It is safe to call this function from any thread. | 127 // It is safe to call this function from any thread. |
94 content::BrowserThread::ID owner_thread() const { return owner_thread_; } | 128 content::BrowserThread::ID owner_thread() const { return owner_thread_; } |
95 | 129 |
96 // The name of the event with which rules are registered. | 130 // The name of the event with which rules are registered. |
97 const std::string& event_name() const { return event_name_; } | 131 const std::string& event_name() const { return event_name_; } |
98 | 132 |
99 protected: | 133 protected: |
100 virtual ~RulesRegistry() {} | 134 virtual ~RulesRegistry(); |
135 | |
136 // These functions need to provide the same functionality as their | |
137 // RulesRegistry counterparts. They need to be atomic. | |
138 virtual std::string AddRulesImpl( | |
139 const std::string& extension_id, | |
140 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; | |
141 virtual std::string RemoveRulesImpl( | |
142 const std::string& extension_id, | |
143 const std::vector<std::string>& rule_identifiers) = 0; | |
144 virtual std::string RemoveAllRulesImpl( | |
145 const std::string& extension_id) = 0; | |
101 | 146 |
102 private: | 147 private: |
103 friend class base::RefCountedThreadSafe<RulesRegistry>; | 148 friend class base::RefCountedThreadSafe<RulesRegistry>; |
149 friend class RulesCacheDelegate; | |
vabr (Chromium)
2013/10/29 13:08:24
I'm a bit uncertain about friending the delegate.
| |
150 | |
151 typedef std::string ExtensionId; | |
152 typedef std::string RuleId; | |
153 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; | |
154 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> > | |
155 RulesDictionary; | |
156 enum ProcessChangedRulesState { | |
157 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. | |
158 NEVER_PROCESS, | |
159 // A task to call ProcessChangedRules is scheduled for future execution. | |
160 SCHEDULED_FOR_PROCESSING, | |
161 // No task to call ProcessChangedRules is scheduled yet, but it is possible | |
162 // to schedule one. | |
163 NOT_SCHEDULED_FOR_PROCESSING | |
164 }; | |
165 | |
166 // Common processing after extension's rules have changed. | |
167 void ProcessChangedRules(const std::string& extension_id); | |
168 | |
169 // Calls ProcessChangedRules if |process_changed_rules_requested_| == | |
170 // NOT_SCHEDULED_FOR_PROCESSING. | |
171 void MaybeProcessChangedRules(const std::string& extension_id); | |
172 | |
173 // Process the callbacks once the registry gets ready. | |
174 void MarkReady(base::Time storage_init_time); | |
175 | |
176 // Deserialize the rules from the given Value object and add them to the | |
177 // RulesRegistry. | |
178 void DeserializeAndAddRules(const std::string& extension_id, | |
179 scoped_ptr<base::Value> rules); | |
180 | |
104 | 181 |
105 // The ID of the thread on which the rules registry lives. | 182 // The ID of the thread on which the rules registry lives. |
106 const content::BrowserThread::ID owner_thread_; | 183 const content::BrowserThread::ID owner_thread_; |
107 | 184 |
108 // The name of the event with which rules are registered. | 185 // The name of the event with which rules are registered. |
109 const std::string event_name_; | 186 const std::string event_name_; |
110 | 187 |
188 RulesDictionary rules_; | |
189 | |
190 // Signaled when we have finished reading from storage for all extensions that | |
191 // are loaded on startup. | |
192 OneShotEvent ready_; | |
193 | |
194 // The factory needs to be declared before |cache_delegate_|, so that it can | |
195 // produce a pointer as a construction argument for |cache_delegate_|. | |
196 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | |
197 | |
198 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is | |
199 // NULL, then the storage functionality is disabled (this is used in tests). | |
200 // This registry cannot own |cache_delegate_| because during the time after | |
201 // rules registry service shuts down on UI thread, and the registry is | |
202 // destroyed on its thread, the use of the |cache_delegate_| would not be | |
203 // safe. The registry only ever associates with one RulesCacheDelegate | |
204 // instance. | |
205 const base::WeakPtr<RulesCacheDelegate> cache_delegate_; | |
206 | |
207 ProcessChangedRulesState process_changed_rules_requested_; | |
208 | |
209 // Returns whether any existing rule is registered with identifier |rule_id| | |
210 // for extension |extension_id|. | |
211 bool IsUniqueId(const std::string& extension_id, | |
212 const std::string& rule_id) const; | |
213 | |
214 // Creates an ID that is unique within the scope of|extension_id|. | |
vabr (Chromium)
2013/10/29 16:05:54
typo from the original code: space between "of" an
| |
215 std::string GenerateUniqueId(const std::string& extension_id); | |
216 | |
217 // Verifies that all |rules| have unique IDs or initializes them with | |
218 // unique IDs if they don't have one. In case of duplicate IDs, this function | |
219 // returns a non-empty error message. | |
220 std::string CheckAndFillInOptionalRules( | |
221 const std::string& extension_id, | |
222 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
223 | |
224 // Initializes the priority fields in case they have not been set. | |
225 void FillInOptionalPriorities( | |
226 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
227 | |
228 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. | |
229 void RemoveUsedRuleIdentifiers(const std::string& extension_id, | |
230 const std::vector<std::string>& identifiers); | |
231 | |
232 // Same as RemoveUsedRuleIdentifiers but operates on all rules of | |
233 // |extension_id|. | |
234 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); | |
235 | |
236 typedef std::string RuleIdentifier; | |
237 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; | |
238 RuleIdentifiersMap used_rule_identifiers_; | |
239 int last_generated_rule_identifier_id_; | |
240 | |
111 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 241 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
112 }; | 242 }; |
113 | 243 |
114 } // namespace extensions | 244 } // namespace extensions |
115 | 245 |
116 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 246 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
OLD | NEW |