| 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_INITIALIZING_RULES_REGISTRY_H_
_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_INITIALIZING_RULES_REGISTRY_H_
_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_INITIALIZING_RULES_REGISTRY_H_
_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_INITIALIZING_RULES_REGISTRY_H_
_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | 9 #include "chrome/browser/extensions/api/declarative/rules_registry.h" |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <set> | 12 #include <set> |
| 13 | 13 |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/ref_counted.h" |
| 16 | 16 |
| 17 namespace extensions { | 17 namespace extensions { |
| 18 | 18 |
| 19 // Wrapper class for RulesRegistry objects that takes care that all optional | 19 // Wrapper class for RulesRegistry objects that takes care that all optional |
| 20 // fields of rules are filled with valid values. | 20 // fields of rules are filled with valid values. |
| 21 class InitializingRulesRegistry : public RulesRegistry { | 21 class InitializingRulesRegistry : public RulesRegistry { |
| 22 public: | 22 public: |
| 23 explicit InitializingRulesRegistry(scoped_ptr<RulesRegistry> delegate); | 23 enum Defaults { DEFAULT_PRIORITY = 100 }; |
| 24 |
| 25 explicit InitializingRulesRegistry(scoped_refptr<RulesRegistry> delegate); |
| 24 virtual ~InitializingRulesRegistry(); | 26 virtual ~InitializingRulesRegistry(); |
| 25 | 27 |
| 26 // Implementation for RulesRegistry: | 28 // Implementation for RulesRegistry: |
| 27 virtual std::string AddRules( | 29 virtual std::string AddRules( |
| 28 const std::string& extension_id, | 30 const std::string& extension_id, |
| 29 const std::vector<base::DictionaryValue*>& rules) OVERRIDE; | 31 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules) OVERRIDE; |
| 30 virtual std::string RemoveRules( | 32 virtual std::string RemoveRules( |
| 31 const std::string& extension_id, | 33 const std::string& extension_id, |
| 32 const std::vector<std::string>& rule_identifiers) OVERRIDE; | 34 const std::vector<std::string>& rule_identifiers) OVERRIDE; |
| 33 virtual std::string RemoveAllRules( | 35 virtual std::string RemoveAllRules( |
| 34 const std::string& extension_id) OVERRIDE; | 36 const std::string& extension_id) OVERRIDE; |
| 35 virtual std::string GetRules( | 37 virtual std::string GetRules( |
| 36 const std::string& extension_id, | 38 const std::string& extension_id, |
| 37 const std::vector<std::string>& rule_identifiers, | 39 const std::vector<std::string>& rule_identifiers, |
| 38 std::vector<base::DictionaryValue*>* out) OVERRIDE; | 40 std::vector<linked_ptr<RulesRegistry::Rule> >* out) OVERRIDE; |
| 39 virtual std::string GetAllRules( | 41 virtual std::string GetAllRules( |
| 40 const std::string& extension_id, | 42 const std::string& extension_id, |
| 41 std::vector<base::DictionaryValue*>* out) OVERRIDE; | 43 std::vector<linked_ptr<RulesRegistry::Rule> >* out) OVERRIDE; |
| 42 virtual void OnExtensionUnloaded(const std::string& extension_id) OVERRIDE; | 44 virtual void OnExtensionUnloaded(const std::string& extension_id) OVERRIDE; |
| 45 virtual content::BrowserThread::ID GetOwnerThread() const OVERRIDE; |
| 43 | 46 |
| 44 private: | 47 private: |
| 45 // Returns whether any existing rule is registered with identifier |rule_id| | 48 // Returns whether any existing rule is registered with identifier |rule_id| |
| 46 // for extension |extension_id|. | 49 // for extension |extension_id|. |
| 47 bool IsUniqueId(const std::string& extension_id, | 50 bool IsUniqueId(const std::string& extension_id, |
| 48 const std::string& rule_id) const; | 51 const std::string& rule_id) const; |
| 49 | 52 |
| 50 // Creates an ID that is unique within the scope of|extension_id|. | 53 // Creates an ID that is unique within the scope of|extension_id|. |
| 51 std::string GenerateUniqueId(std::string extension_id); | 54 std::string GenerateUniqueId(std::string extension_id); |
| 52 | 55 |
| 53 // Verifies that all |rules| have unique IDs or initializes them with | 56 // Verifies that all |rules| have unique IDs or initializes them with |
| 54 // unique IDs if they don't have one. In case of duplicate IDs, this function | 57 // unique IDs if they don't have one. In case of duplicate IDs, this function |
| 55 // returns a non-empty error message. | 58 // returns a non-empty error message. |
| 56 std::string CheckAndFillInOptionalRules( | 59 std::string CheckAndFillInOptionalRules( |
| 57 const std::string& extension_id, | 60 const std::string& extension_id, |
| 58 const std::vector<base::DictionaryValue*>& rules); | 61 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); |
| 59 | 62 |
| 60 // Initializes the priority fields in case they have not been set. | 63 // Initializes the priority fields in case they have not been set. |
| 61 void FillInOptionalPriorities( | 64 void FillInOptionalPriorities( |
| 62 const std::vector<base::DictionaryValue*>& rules); | 65 const std::vector<linked_ptr<RulesRegistry::Rule> >& rules); |
| 63 | 66 |
| 64 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. | 67 // Removes all |identifiers| of |extension_id| from |used_rule_identifiers_|. |
| 65 void RemoveUsedRuleIdentifiers(const std::string& extension_id, | 68 void RemoveUsedRuleIdentifiers(const std::string& extension_id, |
| 66 const std::vector<std::string>& identifiers); | 69 const std::vector<std::string>& identifiers); |
| 67 | 70 |
| 68 // Same as RemoveUsedRuleIdentifiers but operates on all rules of | 71 // Same as RemoveUsedRuleIdentifiers but operates on all rules of |
| 69 // |extension_id|. | 72 // |extension_id|. |
| 70 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); | 73 void RemoveAllUsedRuleIdentifiers(const std::string& extension_id); |
| 71 | 74 |
| 72 scoped_ptr<RulesRegistry> delegate_; | 75 scoped_refptr<RulesRegistry> delegate_; |
| 73 | 76 |
| 74 typedef std::map<std::string, std::set<std::string> > RuleIdentifiersMap; | 77 typedef std::map<std::string, std::set<std::string> > RuleIdentifiersMap; |
| 75 RuleIdentifiersMap used_rule_identifiers_; | 78 RuleIdentifiersMap used_rule_identifiers_; |
| 76 int last_generated_rule_identifier_id_; | 79 int last_generated_rule_identifier_id_; |
| 77 }; | 80 }; |
| 78 | 81 |
| 79 } // namespace extensions | 82 } // namespace extensions |
| 80 | 83 |
| 81 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_INITIALIZING_RULES_REGISTRY
_H__ | 84 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_INITIALIZING_RULES_REGISTRY
_H__ |
| OLD | NEW |