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