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