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" | 8 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 26 matching lines...) Expand all Loading... |
37 // RulesRegistry::Rule objects. It contains all the methods that need to run on | 37 // RulesRegistry::Rule objects. It contains all the methods that need to run on |
38 // the registry thread; methods that need to run on the UI thread are separated | 38 // the registry thread; methods that need to run on the UI thread are separated |
39 // in the RulesCacheDelegate object. | 39 // in the RulesCacheDelegate object. |
40 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { | 40 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { |
41 public: | 41 public: |
42 typedef extensions::api::events::Rule Rule; | 42 typedef extensions::api::events::Rule Rule; |
43 | 43 |
44 enum Defaults { DEFAULT_PRIORITY = 100 }; | 44 enum Defaults { DEFAULT_PRIORITY = 100 }; |
45 // After the RulesCacheDelegate object (the part of the registry which runs on | 45 // After the RulesCacheDelegate object (the part of the registry which runs on |
46 // the UI thread) is created, a pointer to it is passed to |*ui_part|. | 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 | 47 // In tests, |profile| and |ui_part| can be NULL (at the same time). In that |
50 // case the storage functionality disabled (no RulesCacheDelegate object | 48 // case the storage functionality disabled (no RulesCacheDelegate object |
51 // created) and the |log_storage_init_delay| flag is ignored. | 49 // created). |
52 RulesRegistry(Profile* profile, | 50 RulesRegistry(Profile* profile, |
53 const std::string& event_name, | 51 const std::string& event_name, |
54 content::BrowserThread::ID owner_thread, | 52 content::BrowserThread::ID owner_thread, |
55 bool log_storage_init_delay, | 53 RulesCacheDelegate* cache_delegate); |
56 scoped_ptr<RulesCacheDelegate>* ui_part); | |
57 | 54 |
58 const OneShotEvent& ready() const { | 55 const OneShotEvent& ready() const { |
59 return ready_; | 56 return ready_; |
60 } | 57 } |
61 | 58 |
62 // RulesRegistry implementation: | 59 // RulesRegistry implementation: |
63 | 60 |
64 // Registers |rules|, owned by |extension_id| to this RulesRegistry. | 61 // Registers |rules|, owned by |extension_id| to this RulesRegistry. |
65 // If a concrete RuleRegistry does not support some of the rules, | 62 // If a concrete RuleRegistry does not support some of the rules, |
66 // it may ignore them. | 63 // it may ignore them. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | 113 std::vector<linked_ptr<RulesRegistry::Rule> >* out); |
117 | 114 |
118 // Called to notify the RulesRegistry that an extension has been unloaded | 115 // Called to notify the RulesRegistry that an extension has been unloaded |
119 // and all rules of this extension need to be removed. | 116 // and all rules of this extension need to be removed. |
120 void OnExtensionUnloaded(const std::string& extension_id); | 117 void OnExtensionUnloaded(const std::string& extension_id); |
121 | 118 |
122 // Returns the number of entries in used_rule_identifiers_ for leak detection. | 119 // 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. | 120 // Every ExtensionId counts as one entry, even if it contains no rules. |
124 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | 121 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; |
125 | 122 |
| 123 // Returns the profile where the rules registry lives. |
| 124 Profile* profile() const { return profile_; } |
| 125 |
126 // 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. |
127 // It is safe to call this function from any thread. | 127 // It is safe to call this function from any thread. |
128 content::BrowserThread::ID owner_thread() const { return owner_thread_; } | 128 content::BrowserThread::ID owner_thread() const { return owner_thread_; } |
129 | 129 |
130 // The name of the event with which rules are registered. | 130 // The name of the event with which rules are registered. |
131 const std::string& event_name() const { return event_name_; } | 131 const std::string& event_name() const { return event_name_; } |
132 | 132 |
133 protected: | 133 protected: |
134 virtual ~RulesRegistry(); | 134 virtual ~RulesRegistry(); |
135 | 135 |
(...skipping 21 matching lines...) Expand all Loading... |
157 enum ProcessChangedRulesState { | 157 enum ProcessChangedRulesState { |
158 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. | 158 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. |
159 NEVER_PROCESS, | 159 NEVER_PROCESS, |
160 // A task to call ProcessChangedRules is scheduled for future execution. | 160 // A task to call ProcessChangedRules is scheduled for future execution. |
161 SCHEDULED_FOR_PROCESSING, | 161 SCHEDULED_FOR_PROCESSING, |
162 // No task to call ProcessChangedRules is scheduled yet, but it is possible | 162 // No task to call ProcessChangedRules is scheduled yet, but it is possible |
163 // to schedule one. | 163 // to schedule one. |
164 NOT_SCHEDULED_FOR_PROCESSING | 164 NOT_SCHEDULED_FOR_PROCESSING |
165 }; | 165 }; |
166 | 166 |
| 167 base::WeakPtr<RulesRegistry> GetWeakPtr() { |
| 168 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 169 return weak_ptr_factory_.GetWeakPtr(); |
| 170 } |
| 171 |
167 // Common processing after extension's rules have changed. | 172 // Common processing after extension's rules have changed. |
168 void ProcessChangedRules(const std::string& extension_id); | 173 void ProcessChangedRules(const std::string& extension_id); |
169 | 174 |
170 // Calls ProcessChangedRules if |process_changed_rules_requested_| == | 175 // Calls ProcessChangedRules if |process_changed_rules_requested_| == |
171 // NOT_SCHEDULED_FOR_PROCESSING. | 176 // NOT_SCHEDULED_FOR_PROCESSING. |
172 void MaybeProcessChangedRules(const std::string& extension_id); | 177 void MaybeProcessChangedRules(const std::string& extension_id); |
173 | 178 |
174 // Process the callbacks once the registry gets ready. | |
175 void MarkReady(base::Time storage_init_time); | 179 void MarkReady(base::Time storage_init_time); |
176 | 180 |
177 // Deserialize the rules from the given Value object and add them to the | 181 // Deserialize the rules from the given Value object and add them to the |
178 // RulesRegistry. | 182 // RulesRegistry. |
179 void DeserializeAndAddRules(const std::string& extension_id, | 183 void DeserializeAndAddRules(const std::string& extension_id, |
180 scoped_ptr<base::Value> rules); | 184 scoped_ptr<base::Value> rules); |
181 | 185 |
| 186 // The profile to which this rules registry belongs. |
| 187 Profile* profile_; |
182 | 188 |
183 // The ID of the thread on which the rules registry lives. | 189 // The ID of the thread on which the rules registry lives. |
184 const content::BrowserThread::ID owner_thread_; | 190 const content::BrowserThread::ID owner_thread_; |
185 | 191 |
186 // The name of the event with which rules are registered. | 192 // The name of the event with which rules are registered. |
187 const std::string event_name_; | 193 const std::string event_name_; |
188 | 194 |
189 RulesDictionary rules_; | 195 RulesDictionary rules_; |
190 | 196 |
191 // Signaled when we have finished reading from storage for all extensions that | 197 // Signaled when we have finished reading from storage for all extensions that |
192 // are loaded on startup. | 198 // are loaded on startup. |
193 OneShotEvent ready_; | 199 OneShotEvent ready_; |
194 | 200 |
195 // The factory needs to be declared before |cache_delegate_|, so that it can | 201 // The factory needs to be declared before |cache_delegate_|, so that it can |
196 // produce a pointer as a construction argument for |cache_delegate_|. | 202 // produce a pointer as a construction argument for |cache_delegate_|. |
197 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | 203 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; |
198 | 204 |
199 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is | 205 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is |
200 // NULL, then the storage functionality is disabled (this is used in tests). | 206 // NULL, then the storage functionality is disabled (this is used in tests). |
201 // This registry cannot own |cache_delegate_| because during the time after | 207 // This registry cannot own |cache_delegate_| because during the time after |
202 // rules registry service shuts down on UI thread, and the registry is | 208 // rules registry service shuts down on UI thread, and the registry is |
203 // destroyed on its thread, the use of the |cache_delegate_| would not be | 209 // destroyed on its thread, the use of the |cache_delegate_| would not be |
204 // safe. The registry only ever associates with one RulesCacheDelegate | 210 // safe. The registry only ever associates with one RulesCacheDelegate |
205 // instance. | 211 // instance. |
206 const base::WeakPtr<RulesCacheDelegate> cache_delegate_; | 212 base::WeakPtr<RulesCacheDelegate> cache_delegate_; |
207 | 213 |
208 ProcessChangedRulesState process_changed_rules_requested_; | 214 ProcessChangedRulesState process_changed_rules_requested_; |
209 | 215 |
210 // Returns whether any existing rule is registered with identifier |rule_id| | 216 // Returns whether any existing rule is registered with identifier |rule_id| |
211 // for extension |extension_id|. | 217 // for extension |extension_id|. |
212 bool IsUniqueId(const std::string& extension_id, | 218 bool IsUniqueId(const std::string& extension_id, |
213 const std::string& rule_id) const; | 219 const std::string& rule_id) const; |
214 | 220 |
215 // Creates an ID that is unique within the scope of|extension_id|. | 221 // Creates an ID that is unique within the scope of|extension_id|. |
216 std::string GenerateUniqueId(const std::string& extension_id); | 222 std::string GenerateUniqueId(const std::string& extension_id); |
(...skipping 21 matching lines...) Expand all Loading... |
238 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; | 244 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; |
239 RuleIdentifiersMap used_rule_identifiers_; | 245 RuleIdentifiersMap used_rule_identifiers_; |
240 int last_generated_rule_identifier_id_; | 246 int last_generated_rule_identifier_id_; |
241 | 247 |
242 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 248 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
243 }; | 249 }; |
244 | 250 |
245 } // namespace extensions | 251 } // namespace extensions |
246 | 252 |
247 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 253 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
OLD | NEW |