| 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 13 matching lines...) Expand all Loading... |
| 24 namespace content { | 24 namespace content { |
| 25 class BrowserContext; | 25 class BrowserContext; |
| 26 } | 26 } |
| 27 | 27 |
| 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 RulesCacheDelegate; | 35 class RulesCacheDelegate; |
| 35 | 36 |
| 36 // A base class for RulesRegistries that takes care of storing the | 37 // A base class for RulesRegistries that takes care of storing the |
| 37 // RulesRegistry::Rule objects. It contains all the methods that need to run on | 38 // 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 | 39 // the registry thread; methods that need to run on the UI thread are separated |
| 39 // in the RulesCacheDelegate object. | 40 // in the RulesCacheDelegate object. |
| 40 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { | 41 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> { |
| 41 public: | 42 public: |
| 42 typedef extensions::core_api::events::Rule Rule; | 43 typedef extensions::core_api::events::Rule Rule; |
| 43 | 44 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 void GetRules(const std::string& extension_id, | 104 void GetRules(const std::string& extension_id, |
| 104 const std::vector<std::string>& rule_identifiers, | 105 const std::vector<std::string>& rule_identifiers, |
| 105 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | 106 std::vector<linked_ptr<RulesRegistry::Rule> >* out); |
| 106 | 107 |
| 107 // Same as GetRules but returns all rules owned by |extension_id|. | 108 // Same as GetRules but returns all rules owned by |extension_id|. |
| 108 void GetAllRules(const std::string& extension_id, | 109 void GetAllRules(const std::string& extension_id, |
| 109 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | 110 std::vector<linked_ptr<RulesRegistry::Rule> >* out); |
| 110 | 111 |
| 111 // Called to notify the RulesRegistry that the extension availability has | 112 // Called to notify the RulesRegistry that the extension availability has |
| 112 // changed, so that the registry can update which rules are active. | 113 // changed, so that the registry can update which rules are active. |
| 113 void OnExtensionUnloaded(const std::string& extension_id); | 114 void OnExtensionUnloaded(const Extension* extension); |
| 114 void OnExtensionUninstalled(const std::string& extension_id); | 115 void OnExtensionUninstalled(const Extension* extension); |
| 115 void OnExtensionLoaded(const std::string& extension_id); | 116 void OnExtensionLoaded(const Extension* extension); |
| 116 | 117 |
| 117 // Returns the number of entries in used_rule_identifiers_ for leak detection. | 118 // Returns the number of entries in used_rule_identifiers_ for leak detection. |
| 118 // Every ExtensionId counts as one entry, even if it contains no rules. | 119 // Every ExtensionId counts as one entry, even if it contains no rules. |
| 119 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | 120 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; |
| 120 | 121 |
| 121 // Returns the RulesCacheDelegate. This is used for testing. | 122 // Returns the RulesCacheDelegate. This is used for testing. |
| 122 RulesCacheDelegate* rules_cache_delegate_for_testing() const { | 123 RulesCacheDelegate* rules_cache_delegate_for_testing() const { |
| 123 return cache_delegate_.get(); | 124 return cache_delegate_.get(); |
| 124 } | 125 } |
| 125 | 126 |
| 126 // Returns the context where the rules registry lives. | 127 // Returns the context where the rules registry lives. |
| 127 content::BrowserContext* browser_context() const { return browser_context_; } | 128 content::BrowserContext* browser_context() const { return browser_context_; } |
| 128 | 129 |
| 129 // Returns the ID of the thread on which the rules registry lives. | 130 // Returns the ID of the thread on which the rules registry lives. |
| 130 // It is safe to call this function from any thread. | 131 // It is safe to call this function from any thread. |
| 131 content::BrowserThread::ID owner_thread() const { return owner_thread_; } | 132 content::BrowserThread::ID owner_thread() const { return owner_thread_; } |
| 132 | 133 |
| 133 // The name of the event with which rules are registered. | 134 // The name of the event with which rules are registered. |
| 134 const std::string& event_name() const { return event_name_; } | 135 const std::string& event_name() const { return event_name_; } |
| 135 | 136 |
| 136 // The unique identifier for this RulesRegistry object. | 137 // The unique identifier for this RulesRegistry object. |
| 137 int id() const { return id_; } | 138 int id() const { return id_; } |
| 138 | 139 |
| 139 protected: | 140 protected: |
| 140 virtual ~RulesRegistry(); | 141 virtual ~RulesRegistry(); |
| 141 | 142 |
| 142 // The precondition for calling this method is that all rules have unique IDs. | |
| 143 // AddRules establishes this precondition and calls into this method. | |
| 144 // Stored rules already meet this precondition and so they avoid calling | |
| 145 // CheckAndFillInOptionalRules for improved performance. | |
| 146 // | |
| 147 // Returns an empty string if the function is successful or an error | |
| 148 // message otherwise. | |
| 149 std::string AddRulesNoFill( | |
| 150 const std::string& extension_id, | |
| 151 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); | |
| 152 | |
| 153 // These functions need to apply the rules to the browser, while the base | 143 // These functions need to apply the rules to the browser, while the base |
| 154 // class will handle defaulting empty fields before calling *Impl, and will | 144 // class will handle defaulting empty fields before calling *Impl, and will |
| 155 // automatically cache the rules and re-call *Impl on browser startup. | 145 // automatically cache the rules and re-call *Impl on browser startup. |
| 156 virtual std::string AddRulesImpl( | 146 virtual std::string AddRulesImpl( |
| 157 const std::string& extension_id, | 147 const std::string& extension_id, |
| 158 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; | 148 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; |
| 159 virtual std::string RemoveRulesImpl( | 149 virtual std::string RemoveRulesImpl( |
| 160 const std::string& extension_id, | 150 const std::string& extension_id, |
| 161 const std::vector<std::string>& rule_identifiers) = 0; | 151 const std::vector<std::string>& rule_identifiers) = 0; |
| 162 virtual std::string RemoveAllRulesImpl( | 152 virtual std::string RemoveAllRulesImpl( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 180 // to schedule one. | 170 // to schedule one. |
| 181 NOT_SCHEDULED_FOR_PROCESSING | 171 NOT_SCHEDULED_FOR_PROCESSING |
| 182 }; | 172 }; |
| 183 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap; | 173 typedef std::map<ExtensionId, ProcessChangedRulesState> ProcessStateMap; |
| 184 | 174 |
| 185 base::WeakPtr<RulesRegistry> GetWeakPtr() { | 175 base::WeakPtr<RulesRegistry> GetWeakPtr() { |
| 186 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 176 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 187 return weak_ptr_factory_.GetWeakPtr(); | 177 return weak_ptr_factory_.GetWeakPtr(); |
| 188 } | 178 } |
| 189 | 179 |
| 180 // Internal implementation of the AddRules interface which adds |
| 181 // |from_manifest| which is true when the source is the manifest. |
| 182 std::string AddRulesInternal( |
| 183 const std::string& extension_id, |
| 184 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules, |
| 185 RulesDictionary* out); |
| 186 |
| 187 // The precondition for calling this method is that all rules have unique IDs. |
| 188 // AddRules establishes this precondition and calls into this method. |
| 189 // Stored rules already meet this precondition and so they avoid calling |
| 190 // CheckAndFillInOptionalRules for improved performance. |
| 191 // |
| 192 // Returns an empty string if the function is successful or an error |
| 193 // message otherwise. |
| 194 std::string AddRulesNoFill( |
| 195 const std::string& extension_id, |
| 196 const std::vector<linked_ptr<RulesRegistry::Rule>>& rules, |
| 197 RulesDictionary* out); |
| 198 |
| 199 // Same as GetRules but returns all rules owned by |extension_id| for a given |
| 200 // |rules| dictionary. |
| 201 void GetRules(const std::string& extension_id, |
| 202 RulesDictionary& rules, |
| 203 std::vector<linked_ptr<RulesRegistry::Rule>>* out); |
| 204 |
| 190 // Common processing after extension's rules have changed. | 205 // Common processing after extension's rules have changed. |
| 191 void ProcessChangedRules(const std::string& extension_id); | 206 void ProcessChangedRules(const std::string& extension_id); |
| 192 | 207 |
| 193 // Calls ProcessChangedRules if | 208 // Calls ProcessChangedRules if |
| 194 // |process_changed_rules_requested_(extension_id)| == | 209 // |process_changed_rules_requested_(extension_id)| == |
| 195 // NOT_SCHEDULED_FOR_PROCESSING. | 210 // NOT_SCHEDULED_FOR_PROCESSING. |
| 196 void MaybeProcessChangedRules(const std::string& extension_id); | 211 void MaybeProcessChangedRules(const std::string& extension_id); |
| 197 | 212 |
| 198 // This method implements the functionality of RemoveAllRules, except for not | 213 // This method implements the functionality of RemoveAllRules, except for not |
| 199 // calling MaybeProcessChangedRules. That way updating the rules store and | 214 // calling MaybeProcessChangedRules. That way updating the rules store and |
| 200 // extension prefs is avoided. This method is called when an extension is | 215 // extension prefs is avoided. This method is called when an extension is |
| 201 // uninstalled, that way there is no clash with the preferences being wiped. | 216 // uninstalled, that way there is no clash with the preferences being wiped. |
| 202 std::string RemoveAllRulesNoStoreUpdate(const std::string& extension_id); | 217 // Set |remove_manifest_rules| to true if |manifest_rules_| should be cleared |
| 218 // along with |rules_|. |
| 219 std::string RemoveAllRulesNoStoreUpdate(const std::string& extension_id, |
| 220 bool remove_manifest_rules); |
| 203 | 221 |
| 204 void MarkReady(base::Time storage_init_time); | 222 void MarkReady(base::Time storage_init_time); |
| 205 | 223 |
| 206 // Deserialize the rules from the given Value object and add them to the | 224 // Deserialize the rules from the given Value object and add them to the |
| 207 // RulesRegistry. | 225 // RulesRegistry. |
| 208 void DeserializeAndAddRules(const std::string& extension_id, | 226 void DeserializeAndAddRules(const std::string& extension_id, |
| 209 scoped_ptr<base::Value> rules); | 227 scoped_ptr<base::Value> rules); |
| 210 | 228 |
| 211 // The context to which this rules registry belongs. | 229 // The context to which this rules registry belongs. |
| 212 content::BrowserContext* browser_context_; | 230 content::BrowserContext* browser_context_; |
| 213 | 231 |
| 214 // The ID of the thread on which the rules registry lives. | 232 // The ID of the thread on which the rules registry lives. |
| 215 const content::BrowserThread::ID owner_thread_; | 233 const content::BrowserThread::ID owner_thread_; |
| 216 | 234 |
| 217 // The name of the event with which rules are registered. | 235 // The name of the event with which rules are registered. |
| 218 const std::string event_name_; | 236 const std::string event_name_; |
| 219 | 237 |
| 220 // The key that identifies the context in which these rules apply. | 238 // The key that identifies the context in which these rules apply. |
| 221 int id_; | 239 int id_; |
| 222 | 240 |
| 223 RulesDictionary rules_; | 241 RulesDictionary rules_; |
| 224 | 242 |
| 243 RulesDictionary manifest_rules_; |
| 244 |
| 225 // Signaled when we have finished reading from storage for all extensions that | 245 // Signaled when we have finished reading from storage for all extensions that |
| 226 // are loaded on startup. | 246 // are loaded on startup. |
| 227 OneShotEvent ready_; | 247 OneShotEvent ready_; |
| 228 | 248 |
| 229 ProcessStateMap process_changed_rules_requested_; | 249 ProcessStateMap process_changed_rules_requested_; |
| 230 | 250 |
| 231 // Returns whether any existing rule is registered with identifier |rule_id| | 251 // Returns whether any existing rule is registered with identifier |rule_id| |
| 232 // for extension |extension_id|. | 252 // for extension |extension_id|. |
| 233 bool IsUniqueId(const std::string& extension_id, | 253 bool IsUniqueId(const std::string& extension_id, |
| 234 const std::string& rule_id) const; | 254 const std::string& rule_id) const; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 base::WeakPtr<RulesCacheDelegate> cache_delegate_; | 290 base::WeakPtr<RulesCacheDelegate> cache_delegate_; |
| 271 | 291 |
| 272 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | 292 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; |
| 273 | 293 |
| 274 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 294 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
| 275 }; | 295 }; |
| 276 | 296 |
| 277 } // namespace extensions | 297 } // namespace extensions |
| 278 | 298 |
| 279 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ | 299 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_H__ |
| OLD | NEW |