| 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 class RulesCacheDelegate; | 34 class RulesCacheDelegate; |
| 35 | 35 |
| 36 // A base class for RulesRegistries that takes care of storing the | 36 // 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 | 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 struct WebViewKey { |
| 44 int embedder_process_id; |
| 45 int webview_instance_id; |
| 46 WebViewKey(int embedder_process_id, int webview_instance_id) |
| 47 : embedder_process_id(embedder_process_id), |
| 48 webview_instance_id(webview_instance_id) {} |
| 49 bool operator<(const WebViewKey& other) const { |
| 50 return embedder_process_id < other.embedder_process_id || |
| 51 ((embedder_process_id == other.embedder_process_id) && |
| 52 (webview_instance_id < other.webview_instance_id)); |
| 53 } |
| 54 }; |
| 43 | 55 |
| 44 enum Defaults { DEFAULT_PRIORITY = 100 }; | 56 enum Defaults { DEFAULT_PRIORITY = 100 }; |
| 45 // After the RulesCacheDelegate object (the part of the registry which runs on | 57 // 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|. | 58 // the UI thread) is created, a pointer to it is passed to |*ui_part|. |
| 47 // In tests, |profile| and |ui_part| can be NULL (at the same time). In that | 59 // In tests, |profile| and |ui_part| can be NULL (at the same time). In that |
| 48 // case the storage functionality disabled (no RulesCacheDelegate object | 60 // case the storage functionality disabled (no RulesCacheDelegate object |
| 49 // created). | 61 // created). |
| 50 RulesRegistry(Profile* profile, | 62 RulesRegistry(Profile* profile, |
| 51 const std::string& event_name, | 63 const std::string& event_name, |
| 52 content::BrowserThread::ID owner_thread, | 64 content::BrowserThread::ID owner_thread, |
| 53 RulesCacheDelegate* cache_delegate); | 65 RulesCacheDelegate* cache_delegate, |
| 66 const WebViewKey& webview_key); |
| 54 | 67 |
| 55 const OneShotEvent& ready() const { | 68 const OneShotEvent& ready() const { |
| 56 return ready_; | 69 return ready_; |
| 57 } | 70 } |
| 58 | 71 |
| 59 // RulesRegistry implementation: | 72 // RulesRegistry implementation: |
| 60 | 73 |
| 61 // Registers |rules|, owned by |extension_id| to this RulesRegistry. | 74 // Registers |rules|, owned by |extension_id| to this RulesRegistry. |
| 62 // If a concrete RuleRegistry does not support some of the rules, | 75 // If a concrete RuleRegistry does not support some of the rules, |
| 63 // it may ignore them. | 76 // it may ignore them. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 std::vector<linked_ptr<RulesRegistry::Rule> >* out); | 126 std::vector<linked_ptr<RulesRegistry::Rule> >* out); |
| 114 | 127 |
| 115 // Called to notify the RulesRegistry that an extension has been unloaded | 128 // Called to notify the RulesRegistry that an extension has been unloaded |
| 116 // and all rules of this extension need to be removed. | 129 // and all rules of this extension need to be removed. |
| 117 void OnExtensionUnloaded(const std::string& extension_id); | 130 void OnExtensionUnloaded(const std::string& extension_id); |
| 118 | 131 |
| 119 // Returns the number of entries in used_rule_identifiers_ for leak detection. | 132 // Returns the number of entries in used_rule_identifiers_ for leak detection. |
| 120 // Every ExtensionId counts as one entry, even if it contains no rules. | 133 // Every ExtensionId counts as one entry, even if it contains no rules. |
| 121 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; | 134 size_t GetNumberOfUsedRuleIdentifiersForTesting() const; |
| 122 | 135 |
| 136 // Returns the RulesCacheDelegate. This is used for testing. |
| 137 RulesCacheDelegate* rules_cache_delegate_for_testing() const { |
| 138 return cache_delegate_.get(); |
| 139 } |
| 140 |
| 123 // Returns the profile where the rules registry lives. | 141 // Returns the profile where the rules registry lives. |
| 124 Profile* profile() const { return profile_; } | 142 Profile* profile() const { return profile_; } |
| 125 | 143 |
| 126 // Returns the ID of the thread on which the rules registry lives. | 144 // Returns the ID of the thread on which the rules registry lives. |
| 127 // It is safe to call this function from any thread. | 145 // It is safe to call this function from any thread. |
| 128 content::BrowserThread::ID owner_thread() const { return owner_thread_; } | 146 content::BrowserThread::ID owner_thread() const { return owner_thread_; } |
| 129 | 147 |
| 130 // The name of the event with which rules are registered. | 148 // The name of the event with which rules are registered. |
| 131 const std::string& event_name() const { return event_name_; } | 149 const std::string& event_name() const { return event_name_; } |
| 132 | 150 |
| 151 // The key that identifies the webview (or tabs) in which these rules apply. |
| 152 // If the rules apply to the main browser, then this returns the tuple (0, 0). |
| 153 const WebViewKey& webview_key() const { |
| 154 return webview_key_; |
| 155 } |
| 156 |
| 133 protected: | 157 protected: |
| 134 virtual ~RulesRegistry(); | 158 virtual ~RulesRegistry(); |
| 135 | 159 |
| 136 // These functions need to apply the rules to the browser, while the base | 160 // 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 | 161 // class will handle defaulting empty fields before calling *Impl, and will |
| 138 // automatically cache the rules and re-call *Impl on browser startup. | 162 // automatically cache the rules and re-call *Impl on browser startup. |
| 139 virtual std::string AddRulesImpl( | 163 virtual std::string AddRulesImpl( |
| 140 const std::string& extension_id, | 164 const std::string& extension_id, |
| 141 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; | 165 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) = 0; |
| 142 virtual std::string RemoveRulesImpl( | 166 virtual std::string RemoveRulesImpl( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 209 |
| 186 // The profile to which this rules registry belongs. | 210 // The profile to which this rules registry belongs. |
| 187 Profile* profile_; | 211 Profile* profile_; |
| 188 | 212 |
| 189 // The ID of the thread on which the rules registry lives. | 213 // The ID of the thread on which the rules registry lives. |
| 190 const content::BrowserThread::ID owner_thread_; | 214 const content::BrowserThread::ID owner_thread_; |
| 191 | 215 |
| 192 // The name of the event with which rules are registered. | 216 // The name of the event with which rules are registered. |
| 193 const std::string event_name_; | 217 const std::string event_name_; |
| 194 | 218 |
| 219 // The key that identifies the context in which these rules apply. |
| 220 WebViewKey webview_key_; |
| 221 |
| 195 RulesDictionary rules_; | 222 RulesDictionary rules_; |
| 196 | 223 |
| 197 // Signaled when we have finished reading from storage for all extensions that | 224 // Signaled when we have finished reading from storage for all extensions that |
| 198 // are loaded on startup. | 225 // are loaded on startup. |
| 199 OneShotEvent ready_; | 226 OneShotEvent ready_; |
| 200 | 227 |
| 201 // The factory needs to be declared before |cache_delegate_|, so that it can | 228 // The factory needs to be declared before |cache_delegate_|, so that it can |
| 202 // produce a pointer as a construction argument for |cache_delegate_|. | 229 // produce a pointer as a construction argument for |cache_delegate_|. |
| 203 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; | 230 base::WeakPtrFactory<RulesRegistry> weak_ptr_factory_; |
| 204 | 231 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; | 271 typedef std::map<ExtensionId, std::set<RuleIdentifier> > RuleIdentifiersMap; |
| 245 RuleIdentifiersMap used_rule_identifiers_; | 272 RuleIdentifiersMap used_rule_identifiers_; |
| 246 int last_generated_rule_identifier_id_; | 273 int last_generated_rule_identifier_id_; |
| 247 | 274 |
| 248 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); | 275 DISALLOW_COPY_AND_ASSIGN(RulesRegistry); |
| 249 }; | 276 }; |
| 250 | 277 |
| 251 } // namespace extensions | 278 } // namespace extensions |
| 252 | 279 |
| 253 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ | 280 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__ |
| OLD | NEW |