| 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_SERVICE_H__ |    5 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 
|    6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |    6 #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 
|    7  |    7  | 
|    8 #include <map> |    8 #include <map> | 
|    9 #include <string> |    9 #include <string> | 
|   10 #include <vector> |   10 #include <vector> | 
|   11  |   11  | 
|   12 #include "base/callback_forward.h" |   12 #include "base/callback_forward.h" | 
|   13 #include "base/memory/ref_counted.h" |   13 #include "base/memory/ref_counted.h" | 
|   14 #include "base/memory/scoped_vector.h" |   14 #include "base/memory/scoped_vector.h" | 
|   15 #include "base/scoped_observer.h" |   15 #include "base/scoped_observer.h" | 
|   16 #include "content/public/browser/notification_observer.h" |  | 
|   17 #include "content/public/browser/notification_registrar.h" |  | 
|   18 #include "extensions/browser/api/declarative/rules_registry.h" |   16 #include "extensions/browser/api/declarative/rules_registry.h" | 
|   19 #include "extensions/browser/browser_context_keyed_api_factory.h" |   17 #include "extensions/browser/browser_context_keyed_api_factory.h" | 
|   20 #include "extensions/browser/extension_registry_observer.h" |   18 #include "extensions/browser/extension_registry_observer.h" | 
|   21  |   19  | 
|   22 namespace content { |   20 namespace content { | 
|   23 class BrowserContext; |   21 class BrowserContext; | 
|   24 class NotificationSource; |  | 
|   25 } |   22 } | 
|   26  |   23  | 
|   27 namespace extensions { |   24 namespace extensions { | 
|   28 class ContentRulesRegistry; |   25 class ContentRulesRegistry; | 
|   29 class ExtensionRegistry; |   26 class ExtensionRegistry; | 
|   30 class RulesRegistryStorageDelegate; |   27 class RulesRegistryStorageDelegate; | 
|   31 } |   28 } | 
|   32  |   29  | 
|   33 namespace extensions { |   30 namespace extensions { | 
|   34  |   31  | 
|   35 // This class owns all RulesRegistries implementations of an ExtensionService. |   32 // This class owns all RulesRegistries implementations of an ExtensionService. | 
|   36 // This class lives on the UI thread. |   33 // This class lives on the UI thread. | 
|   37 class RulesRegistryService : public BrowserContextKeyedAPI, |   34 class RulesRegistryService : public BrowserContextKeyedAPI, | 
|   38                              public content::NotificationObserver, |  | 
|   39                              public ExtensionRegistryObserver { |   35                              public ExtensionRegistryObserver { | 
|   40  public: |   36  public: | 
|   41   typedef RulesRegistry::WebViewKey WebViewKey; |   37   static const int kDefaultRulesRegistryID; | 
 |   38   static const int kInvalidRulesRegistryID; | 
 |   39  | 
|   42   struct RulesRegistryKey { |   40   struct RulesRegistryKey { | 
|   43     std::string event_name; |   41     std::string event_name; | 
|   44     WebViewKey webview_key; |   42     int rules_registry_id; | 
|   45     RulesRegistryKey(const std::string event_name, |   43     RulesRegistryKey(const std::string event_name, int rules_registry_id) | 
|   46                      const WebViewKey& webview_key) |  | 
|   47         : event_name(event_name), |   44         : event_name(event_name), | 
|   48           webview_key(webview_key) {} |   45           rules_registry_id(rules_registry_id) {} | 
|   49     bool operator<(const RulesRegistryKey& other) const { |   46     bool operator<(const RulesRegistryKey& other) const { | 
|   50       return (event_name < other.event_name) || |   47       return (event_name < other.event_name) || | 
|   51           ((event_name == other.event_name) && |   48              ((event_name == other.event_name) && | 
|   52           (webview_key < other.webview_key)); |   49               (rules_registry_id < other.rules_registry_id)); | 
|   53     } |   50     } | 
|   54   }; |   51   }; | 
|   55  |   52  | 
|   56   explicit RulesRegistryService(content::BrowserContext* context); |   53   explicit RulesRegistryService(content::BrowserContext* context); | 
|   57   ~RulesRegistryService() override; |   54   ~RulesRegistryService() override; | 
|   58  |   55  | 
|   59   // Unregisters refptrs to concrete RulesRegistries at other objects that were |   56   // Unregisters refptrs to concrete RulesRegistries at other objects that were | 
|   60   // created by us so that the RulesRegistries can be released. |   57   // created by us so that the RulesRegistries can be released. | 
|   61   void Shutdown() override; |   58   void Shutdown() override; | 
|   62  |   59  | 
|   63   // BrowserContextKeyedAPI implementation. |   60   // BrowserContextKeyedAPI implementation. | 
|   64   static BrowserContextKeyedAPIFactory<RulesRegistryService>* |   61   static BrowserContextKeyedAPIFactory<RulesRegistryService>* | 
|   65       GetFactoryInstance(); |   62       GetFactoryInstance(); | 
|   66  |   63  | 
|   67   // Convenience method to get the RulesRegistryService for a context. |   64   // Convenience method to get the RulesRegistryService for a context. | 
|   68   static RulesRegistryService* Get(content::BrowserContext* context); |   65   static RulesRegistryService* Get(content::BrowserContext* context); | 
|   69  |   66  | 
 |   67   int GetNextRulesRegistryID(); | 
 |   68  | 
|   70   // Registers the default RulesRegistries used in Chromium. |   69   // Registers the default RulesRegistries used in Chromium. | 
|   71   void EnsureDefaultRulesRegistriesRegistered(const WebViewKey& webview_key); |   70   void EnsureDefaultRulesRegistriesRegistered(int rules_registry_id); | 
|   72  |   71  | 
|   73   // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry. |   72   // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry. | 
|   74   void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry); |   73   void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry); | 
|   75  |   74  | 
|   76   // Returns the RulesRegistry for |event_name| and |webview_key| or NULL if no |   75   // Returns the RulesRegistry for |event_name| and |rules_registry_id| or | 
|   77   // such registry has been registered. Default rules registries (such as the |   76   // NULL if no such registry has been registered. Default rules registries | 
|   78   // WebRequest rules registry) will be created on first access. |   77   // (such as the WebRequest rules registry) will be created on first access. | 
|   79   scoped_refptr<RulesRegistry> GetRulesRegistry(const WebViewKey& webview_key, |   78   scoped_refptr<RulesRegistry> GetRulesRegistry(int rules_registry_id, | 
|   80                                                 const std::string& event_name); |   79                                                 const std::string& event_name); | 
|   81  |   80  | 
 |   81   // Remove all rules registries of the given rules_registry_id. | 
 |   82   void RemoveRulesRegistriesByID(int rules_registry_id); | 
 |   83  | 
|   82   // Accessors for each type of rules registry. |   84   // Accessors for each type of rules registry. | 
|   83   ContentRulesRegistry* content_rules_registry() const { |   85   ContentRulesRegistry* content_rules_registry() const { | 
|   84     CHECK(content_rules_registry_); |   86     CHECK(content_rules_registry_); | 
|   85     return content_rules_registry_; |   87     return content_rules_registry_; | 
|   86   } |   88   } | 
|   87  |   89  | 
|   88   // Removes all rules registries of a given webview embedder process ID. |  | 
|   89   void RemoveWebViewRulesRegistries(int process_id); |  | 
|   90  |  | 
|   91   // For testing. |   90   // For testing. | 
|   92   void SimulateExtensionUninstalled(const std::string& extension_id); |   91   void SimulateExtensionUninstalled(const std::string& extension_id); | 
|   93  |   92  | 
|   94  private: |   93  private: | 
|   95   friend class BrowserContextKeyedAPIFactory<RulesRegistryService>; |   94   friend class BrowserContextKeyedAPIFactory<RulesRegistryService>; | 
|   96  |   95  | 
|   97   // Maps <event name, webview key> to RuleRegistries that handle these |   96   // Maps <event name, rules registry ID> to RuleRegistries that handle these | 
|   98   // events. |   97   // events. | 
|   99   typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> > |   98   typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> > | 
|  100       RulesRegistryMap; |   99       RulesRegistryMap; | 
|  101  |  100  | 
|  102   // Implementation of content::NotificationObserver. |  | 
|  103   void Observe(int type, |  | 
|  104                const content::NotificationSource& source, |  | 
|  105                const content::NotificationDetails& details) override; |  | 
|  106  |  | 
|  107   // ExtensionRegistryObserver implementation. |  101   // ExtensionRegistryObserver implementation. | 
|  108   void OnExtensionLoaded(content::BrowserContext* browser_context, |  102   void OnExtensionLoaded(content::BrowserContext* browser_context, | 
|  109                          const Extension* extension) override; |  103                          const Extension* extension) override; | 
|  110   void OnExtensionUnloaded(content::BrowserContext* browser_context, |  104   void OnExtensionUnloaded(content::BrowserContext* browser_context, | 
|  111                            const Extension* extension, |  105                            const Extension* extension, | 
|  112                            UnloadedExtensionInfo::Reason reason) override; |  106                            UnloadedExtensionInfo::Reason reason) override; | 
|  113   void OnExtensionUninstalled(content::BrowserContext* browser_context, |  107   void OnExtensionUninstalled(content::BrowserContext* browser_context, | 
|  114                               const Extension* extension, |  108                               const Extension* extension, | 
|  115                               extensions::UninstallReason reason) override; |  109                               extensions::UninstallReason reason) override; | 
|  116  |  110  | 
|  117   // Iterates over all registries, and calls |notification_callback| on them |  111   // Iterates over all registries, and calls |notification_callback| on them | 
|  118   // with |extension_id| as the argument. If a registry lives on a different |  112   // with |extension_id| as the argument. If a registry lives on a different | 
|  119   // thread, the call is posted to that thread, so no guarantee of synchronous |  113   // thread, the call is posted to that thread, so no guarantee of synchronous | 
|  120   // processing. |  114   // processing. | 
|  121   void NotifyRegistriesHelper( |  115   void NotifyRegistriesHelper( | 
|  122       void (RulesRegistry::*notification_callback)(const std::string&), |  116       void (RulesRegistry::*notification_callback)(const std::string&), | 
|  123       const std::string& extension_id); |  117       const std::string& extension_id); | 
|  124  |  118  | 
|  125   // BrowserContextKeyedAPI implementation. |  119   // BrowserContextKeyedAPI implementation. | 
|  126   static const char* service_name() { |  120   static const char* service_name() { | 
|  127     return "RulesRegistryService"; |  121     return "RulesRegistryService"; | 
|  128   } |  122   } | 
|  129   static const bool kServiceHasOwnInstanceInIncognito = true; |  123   static const bool kServiceHasOwnInstanceInIncognito = true; | 
|  130   static const bool kServiceIsNULLWhileTesting = true; |  124   static const bool kServiceIsNULLWhileTesting = true; | 
|  131  |  125  | 
 |  126   int current_rules_registry_id_; | 
 |  127  | 
|  132   RulesRegistryMap rule_registries_; |  128   RulesRegistryMap rule_registries_; | 
|  133  |  129  | 
|  134   // We own the parts of the registries which need to run on the UI thread. |  130   // We own the parts of the registries which need to run on the UI thread. | 
|  135   ScopedVector<RulesCacheDelegate> cache_delegates_; |  131   ScopedVector<RulesCacheDelegate> cache_delegates_; | 
|  136  |  132  | 
|  137   // Weak pointer into rule_registries_ to make it easier to handle content rule |  133   // Weak pointer into rule_registries_ to make it easier to handle content rule | 
|  138   // conditions. |  134   // conditions. | 
|  139   ContentRulesRegistry* content_rules_registry_; |  135   ContentRulesRegistry* content_rules_registry_; | 
|  140  |  136  | 
|  141   content::NotificationRegistrar registrar_; |  | 
|  142  |  | 
|  143   // Listen to extension load, unloaded notification. |  137   // Listen to extension load, unloaded notification. | 
|  144   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |  138   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 
|  145       extension_registry_observer_; |  139       extension_registry_observer_; | 
|  146  |  140  | 
|  147   content::BrowserContext* browser_context_; |  141   content::BrowserContext* browser_context_; | 
|  148  |  142  | 
|  149   DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); |  143   DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); | 
|  150 }; |  144 }; | 
|  151  |  145  | 
|  152 }  // namespace extensions |  146 }  // namespace extensions | 
|  153  |  147  | 
|  154 #endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |  148 #endif  // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 
| OLD | NEW |