Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_registry.h

Issue 53273002: Decouple RulesCacheDelegate from RulesRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor_rules_registry_with_cache
Patch Set: Merge with ToT Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
56 scoped_ptr<RulesCacheDelegate>* ui_part);
57 53
58 const OneShotEvent& ready() const { 54 const OneShotEvent& ready() const {
59 return ready_; 55 return ready_;
60 } 56 }
61 57
58 base::WeakPtr<RulesRegistry> GetWeakPtr() {
Jeffrey Yasskin 2013/11/01 00:15:09 It's good to avoid passing WeakPtrs out of the cla
Fady Samuel 2013/11/01 15:13:39 I'll take care of this in a subsequent CL. Thanks!
59 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
60 return weak_ptr_factory_.GetWeakPtr();
61 }
62
62 // RulesRegistry implementation: 63 // RulesRegistry implementation:
63 64
64 // Registers |rules|, owned by |extension_id| to this RulesRegistry. 65 // Registers |rules|, owned by |extension_id| to this RulesRegistry.
65 // If a concrete RuleRegistry does not support some of the rules, 66 // If a concrete RuleRegistry does not support some of the rules,
66 // it may ignore them. 67 // it may ignore them.
67 // 68 //
68 // |rules| is a list of Rule instances following the definition of the 69 // |rules| is a list of Rule instances following the definition of the
69 // declarative extension APIs. It is guaranteed that each rule in |rules| has 70 // declarative extension APIs. It is guaranteed that each rule in |rules| has
70 // a unique name within the scope of |extension_id| that has not been 71 // a unique name within the scope of |extension_id| that has not been
71 // registered before, unless it has been removed again. 72 // registered before, unless it has been removed again.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 113
113 // Same as GetRules but returns all rules owned by |extension_id|. 114 // Same as GetRules but returns all rules owned by |extension_id|.
114 std::string GetAllRules( 115 std::string GetAllRules(
115 const std::string& extension_id, 116 const std::string& extension_id,
116 std::vector<linked_ptr<RulesRegistry::Rule> >* out); 117 std::vector<linked_ptr<RulesRegistry::Rule> >* out);
117 118
118 // Called to notify the RulesRegistry that an extension has been unloaded 119 // Called to notify the RulesRegistry that an extension has been unloaded
119 // and all rules of this extension need to be removed. 120 // and all rules of this extension need to be removed.
120 void OnExtensionUnloaded(const std::string& extension_id); 121 void OnExtensionUnloaded(const std::string& extension_id);
121 122
123 // Assigns a RulesCacheDelegate to this RulesRegistry. This delegate is
124 // responsible for storing the rules and retreiving them.
125 bool SetCacheDelegate(base::WeakPtr<RulesCacheDelegate> cache_delegate);
Jeffrey Yasskin 2013/11/01 00:15:09 I think it's safe to pass WeakPtrs by reference, w
Jeffrey Yasskin 2013/11/01 00:15:09 For functions that return bool, you need to say wh
Fady Samuel 2013/11/01 15:13:39 Done.
Fady Samuel 2013/11/01 15:13:39 Done.
126
122 // Returns the number of entries in used_rule_identifiers_ for leak detection. 127 // 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. 128 // Every ExtensionId counts as one entry, even if it contains no rules.
124 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; 129 size_t GetNumberOfUsedRuleIdentifiersForTesting() const;
125 130
131 // Returns the profile where the rules registry lives.
132 Profile* profile() const { return profile_; }
133
126 // Returns the ID of the thread on which the rules registry lives. 134 // Returns the ID of the thread on which the rules registry lives.
127 // It is safe to call this function from any thread. 135 // It is safe to call this function from any thread.
128 content::BrowserThread::ID owner_thread() const { return owner_thread_; } 136 content::BrowserThread::ID owner_thread() const { return owner_thread_; }
129 137
130 // The name of the event with which rules are registered. 138 // The name of the event with which rules are registered.
131 const std::string& event_name() const { return event_name_; } 139 const std::string& event_name() const { return event_name_; }
132 140
133 protected: 141 protected:
134 virtual ~RulesRegistry(); 142 virtual ~RulesRegistry();
135 143
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // Common processing after extension's rules have changed. 175 // Common processing after extension's rules have changed.
168 void ProcessChangedRules(const std::string& extension_id); 176 void ProcessChangedRules(const std::string& extension_id);
169 177
170 // Calls ProcessChangedRules if |process_changed_rules_requested_| == 178 // Calls ProcessChangedRules if |process_changed_rules_requested_| ==
171 // NOT_SCHEDULED_FOR_PROCESSING. 179 // NOT_SCHEDULED_FOR_PROCESSING.
172 void MaybeProcessChangedRules(const std::string& extension_id); 180 void MaybeProcessChangedRules(const std::string& extension_id);
173 181
174 // Process the callbacks once the registry gets ready. 182 // Process the callbacks once the registry gets ready.
175 void MarkReady(base::Time storage_init_time); 183 void MarkReady(base::Time storage_init_time);
176 184
177 // Deserialize the rules from the given Value object and add them to the 185 // The profile to which this rules registry belongs.
178 // RulesRegistry. 186 Profile* profile_;
179 void DeserializeAndAddRules(const std::string& extension_id,
180 scoped_ptr<base::Value> rules);
181
182 187
183 // The ID of the thread on which the rules registry lives. 188 // The ID of the thread on which the rules registry lives.
184 const content::BrowserThread::ID owner_thread_; 189 const content::BrowserThread::ID owner_thread_;
185 190
186 // The name of the event with which rules are registered. 191 // The name of the event with which rules are registered.
187 const std::string event_name_; 192 const std::string event_name_;
188 193
189 RulesDictionary rules_; 194 RulesDictionary rules_;
190 195
191 // Signaled when we have finished reading from storage for all extensions that 196 // Signaled when we have finished reading from storage for all extensions that
192 // are loaded on startup. 197 // are loaded on startup.
193 OneShotEvent ready_; 198 OneShotEvent ready_;
194 199
195 // The factory needs to be declared before |cache_delegate_|, so that it can 200 // The factory needs to be declared before |cache_delegate_|, so that it can
196 // produce a pointer as a construction argument for |cache_delegate_|. 201 // produce a pointer as a construction argument for |cache_delegate_|.
197 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; 202 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_;
198 203
199 // |cache_delegate_| is owned by the registry service. If |cache_delegate_| is 204 // |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). 205 // NULL, then the storage functionality is disabled (this is used in tests).
201 // This registry cannot own |cache_delegate_| because during the time after 206 // This registry cannot own |cache_delegate_| because during the time after
202 // rules registry service shuts down on UI thread, and the registry is 207 // 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 208 // destroyed on its thread, the use of the |cache_delegate_| would not be
204 // safe. The registry only ever associates with one RulesCacheDelegate 209 // safe. The registry only ever associates with one RulesCacheDelegate
205 // instance. 210 // instance.
206 const base::WeakPtr<RulesCacheDelegate> cache_delegate_; 211 base::WeakPtr<RulesCacheDelegate> cache_delegate_;
207 212
208 ProcessChangedRulesState process_changed_rules_requested_; 213 ProcessChangedRulesState process_changed_rules_requested_;
209 214
210 // Returns whether any existing rule is registered with identifier |rule_id| 215 // Returns whether any existing rule is registered with identifier |rule_id|
211 // for extension |extension_id|. 216 // for extension |extension_id|.
212 bool IsUniqueId(const std::string& extension_id, 217 bool IsUniqueId(const std::string& extension_id,
213 const std::string& rule_id) const; 218 const std::string& rule_id) const;
214 219
215 // Creates an ID that is unique within the scope of|extension_id|. 220 // Creates an ID that is unique within the scope of|extension_id|.
216 std::string GenerateUniqueId(const std::string& extension_id); 221 std::string GenerateUniqueId(const std::string& extension_id);
(...skipping 21 matching lines...) Expand all
238 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; 243 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap;
239 RuleIdentifiersMap used_rule_identifiers_; 244 RuleIdentifiersMap used_rule_identifiers_;
240 int last_generated_rule_identifier_id_; 245 int last_generated_rule_identifier_id_;
241 246
242 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); 247 DISALLOW_COPY_AND_ASSIGN(RulesRegistry);
243 }; 248 };
244 249
245 } // namespace extensions 250 } // namespace extensions
246 251
247 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ 252 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698