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: |
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 apply the rules to the browser, while the base |
| 137 // class will handle defaulting empty fields before calling *Impl, and will |
| 138 // automatically cache the rules and re-call *Impl on browser startup. |
| 139 virtual std::string AddRulesImpl( |
| 140 const std::string& extension_id, |
| 141 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; |
| 142 virtual std::string RemoveRulesImpl( |
| 143 const std::string& extension_id, |
| 144 const std::vector<std::string>& rule_identifiers) = 0; |
| 145 virtual std::string RemoveAllRulesImpl( |
| 146 const std::string& extension_id) = 0; |
101 | 147 |
102 private: | 148 private: |
103 friend class base::RefCountedThreadSafe<RulesRegistry>; | 149 friend class base::RefCountedThreadSafe<RulesRegistry>; |
| 150 friend class RulesCacheDelegate; |
| 151 |
| 152 typedef std::string ExtensionId; |
| 153 typedef std::string RuleId; |
| 154 typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey; |
| 155 typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> > |
| 156 RulesDictionary; |
| 157 enum ProcessChangedRulesState { |
| 158 // ProcessChangedRules can never be called, |cache_delegate_| is NULL. |
| 159 NEVER_PROCESS, |
| 160 // A task to call ProcessChangedRules is scheduled for future execution. |
| 161 SCHEDULED_FOR_PROCESSING, |
| 162 // No task to call ProcessChangedRules is scheduled yet, but it is possible |
| 163 // to schedule one. |
| 164 NOT_SCHEDULED_FOR_PROCESSING |
| 165 }; |
| 166 |
| 167 // Common processing after extension's rules have changed. |
| 168 void ProcessChangedRules(const std::string& extension_id); |
| 169 |
| 170 // Calls ProcessChangedRules if |process_changed_rules_requested_| == |
| 171 // NOT_SCHEDULED_FOR_PROCESSING. |
| 172 void MaybeProcessChangedRules(const std::string& extension_id); |
| 173 |
| 174 // Process the callbacks once the registry gets ready. |
| 175 void MarkReady(base::Time storage_init_time); |
| 176 |
| 177 // Deserialize the rules from the given Value object and add them to the |
| 178 // RulesRegistry. |
| 179 void DeserializeAndAddRules(const std::string& extension_id, |
| 180 scoped_ptr<base::Value> rules); |
| 181 |
104 | 182 |
105 // The ID of the thread on which the rules registry lives. | 183 // The ID of the thread on which the rules registry lives. |
106 const content::BrowserThread::ID owner_thread_; | 184 const content::BrowserThread::ID owner_thread_; |
107 | 185 |
108 // The name of the event with which rules are registered. | 186 // The name of the event with which rules are registered. |
109 const std::string event_name_; | 187 const std::string event_name_; |
110 | 188 |
| 189 RulesDictionary rules_; |
| 190 |
| 191 // Signaled when we have finished reading from storage for all extensions that |
| 192 // are loaded on startup. |
| 193 OneShotEvent ready_; |
| 194 |
| 195 // The factory needs to be declared before |cache_delegate_|, so that it can |
| 196 // produce a pointer as a construction argument for |cache_delegate_|. |
| 197 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; |
| 198 |
| 199 // |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). |
| 201 // This registry cannot own |cache_delegate_| because during the time after |
| 202 // 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 |
| 204 // safe. The registry only ever associates with one RulesCacheDelegate |
| 205 // instance. |
| 206 const base::WeakPtr<RulesCacheDelegate> cache_delegate_; |
| 207 |
| 208 ProcessChangedRulesState process_changed_rules_requested_; |
| 209 |
| 210 // Returns whether any existing rule is registered with identifier |rule_id| |
| 211 // for extension |extension_id|. |
| 212 bool IsUniqueId(const std::string& extension_id, |
| 213 const std::string& rule_id) const; |
| 214 |
| 215 // Creates an ID that is unique within the scope of|extension_id|. |
| 216 std::string GenerateUniqueId(const std::string& extension_id); |
| 217 |
| 218 // Verifies that all |rules| have unique IDs or initializes them with |
| 219 // unique IDs if they don't have one. In case of duplicate IDs, this function |
| 220 // returns a non-empty error message. |
| 221 std::string CheckAndFillInOptionalRules( |
| 222 const std::string& extension_id, |
| 223 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); |
| 224 |
| 225 // Initializes the priority fields in case they have not been set. |
| 226 void FillInOptionalPriorities( |
| 227 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); |
| 228 |
| 229 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. |
| 230 void RemoveUsedRuleIdentifiers(const std::string& extension_id, |
| 231 const std::vector<std::string>& identifiers); |
| 232 |
| 233 // Same as RemoveUsedRuleIdentifiers but operates on all rules of |
| 234 // |extension_id|. |
| 235 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); |
| 236 |
| 237 typedef std::string RuleIdentifier; |
| 238 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; |
| 239 RuleIdentifiersMap used_rule_identifiers_; |
| 240 int last_generated_rule_identifier_id_; |
| 241 |
111 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 242 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
112 }; | 243 }; |
113 | 244 |
114 } // namespace extensions | 245 } // namespace extensions |
115 | 246 |
116 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 247 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
OLD | NEW |