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_RULES_REGISTRY_H__
      6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/linked_ptr.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "chrome/common/extensions/api/events.h"
     14 #include "content/public/browser/browser_thread.h"
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 }
     19 
     20 namespace extensions {
     21 
     22 class RulesRegistry;
     23 
     24 // Interface for rule registries.
     25 //
     26 // All functions except GetOwnerThread() and the destructor are only called on
     27 // the thread indicated by GetOwnerThread().
     28 class RulesRegistry : public base::RefCountedThreadSafe<RulesRegistry> {
     29  public:
     30   typedef extensions::api::events::Rule Rule;
     31 
     32   RulesRegistry(content::BrowserThread::ID owner_thread,
     33                 const std::string& event_name)
     34       : owner_thread_(owner_thread), event_name_(event_name) {}
     35 
     36   // Registers |rules|, owned by |extension_id| to this RulesRegistry.
     37   // If a concrete RuleRegistry does not support some of the rules,
     38   // it may ignore them.
     39   //
     40   // |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
     42   // a unique name within the scope of |extension_id| that has not been
     43   // registered before, unless it has been removed again.
     44   // The ownership of rules remains with the caller.
     45   //
     46   // Returns an empty string if the function is successful or an error
     47   // message otherwise.
     48   //
     49   // IMPORTANT: This function is atomic. Either all rules that are deemed
     50   // relevant are added or none.
     51   virtual std::string AddRules(
     52       const std::string& extension_id,
     53       const std::vector<linked_ptr<Rule> >& rules) = 0;
     54 
     55   // Unregisters all rules listed in |rule_identifiers| and owned by
     56   // |extension_id| from this RulesRegistry.
     57   // Some or all IDs in |rule_identifiers| may not be stored in this
     58   // RulesRegistry and are ignored.
     59   //
     60   // Returns an empty string if the function is successful or an error
     61   // message otherwise.
     62   //
     63   // IMPORTANT: This function is atomic. Either all rules that are deemed
     64   // relevant are removed or none.
     65   virtual std::string RemoveRules(
     66       const std::string& extension_id,
     67       const std::vector<std::string>& rule_identifiers) = 0;
     68 
     69   // Same as RemoveAllRules but acts on all rules owned by |extension_id|.
     70   virtual std::string RemoveAllRules(const std::string& extension_id) = 0;
     71 
     72   // Returns all rules listed in |rule_identifiers| and owned by |extension_id|
     73   // registered in this RuleRegistry. Entries in |rule_identifiers| that
     74   // are unknown are ignored.
     75   //
     76   // The returned rules are stored in |out|. Ownership is passed to the caller.
     77   //
     78   // Returns an empty string if the function is successful or an error
     79   // message otherwise.
     80   virtual std::string GetRules(const std::string& extension_id,
     81                                const std::vector<std::string>& rule_identifiers,
     82                                std::vector<linked_ptr<Rule> >* out) = 0;
     83 
     84   // Same as GetRules but returns all rules owned by |extension_id|.
     85   virtual std::string GetAllRules(const std::string& extension_id,
     86                                   std::vector<linked_ptr<Rule> >* out) = 0;
     87 
     88   // Called to notify the RulesRegistry that an extension has been unloaded
     89   // and all rules of this extension need to be removed.
     90   virtual void OnExtensionUnloaded(const std::string& extension_id) = 0;
     91 
     92   // Returns the ID of the thread on which the rules registry lives.
     93   // It is safe to call this function from any thread.
     94   content::BrowserThread::ID owner_thread() const { return owner_thread_; }
     95 
     96   // The name of the event with which rules are registered.
     97   const std::string& event_name() const { return event_name_; }
     98 
     99  protected:
    100   virtual ~RulesRegistry() {}
    101 
    102  private:
    103   friend class base::RefCountedThreadSafe<RulesRegistry>;
    104 
    105   // The ID of the thread on which the rules registry lives.
    106   const content::BrowserThread::ID owner_thread_;
    107 
    108   // The name of the event with which rules are registered.
    109   const std::string event_name_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(RulesRegistry);
    112 };
    113 
    114 }  // namespace extensions
    115 
    116 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_H__
    117