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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
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"
27 class NotificationSource;
30 namespace extensions {
31 class ContentRulesRegistry;
32 class ExtensionRegistry;
34 class RulesRegistryStorageDelegate;
37 namespace extensions {
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 {
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));
60 explicit RulesRegistryService(content::BrowserContext* context);
61 virtual ~RulesRegistryService();
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;
67 // BrowserContextKeyedAPI implementation.
68 static BrowserContextKeyedAPIFactory<RulesRegistryService>*
71 // Convenience method to get the RulesRegistryService for a profile.
72 static RulesRegistryService* Get(content::BrowserContext* context);
74 // Registers the default RulesRegistries used in Chromium.
75 void EnsureDefaultRulesRegistriesRegistered(const WebViewKey& webview_key);
77 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
78 void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);
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);
87 // Accessors for each type of rules registry.
88 ContentRulesRegistry* content_rules_registry() const {
89 CHECK(content_rules_registry_);
90 return content_rules_registry_;
93 // Removes all rules registries of a given webview embedder process ID.
94 void RemoveWebViewRulesRegistries(int process_id);
97 void SimulateExtensionUninstalled(const std::string& extension_id);
100 friend class BrowserContextKeyedAPIFactory<RulesRegistryService>;
102 // Maps <event name, webview key> to RuleRegistries that handle these
104 typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> >
107 // Implementation of content::NotificationObserver.
108 virtual void Observe(int type,
109 const content::NotificationSource& source,
110 const content::NotificationDetails& details) OVERRIDE;
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(
120 content::BrowserContext* browser_context,
121 const Extension* extension,
122 extensions::UninstallReason reason) OVERRIDE;
124 // Iterates over all registries, and calls |notification_callback| on them
125 // with |extension_id| as the argument. If a registry lives on a different
126 // thread, the call is posted to that thread, so no guarantee of synchronous
128 void NotifyRegistriesHelper(
129 void (RulesRegistry::*notification_callback)(const std::string&),
130 const std::string& extension_id);
132 // BrowserContextKeyedAPI implementation.
133 static const char* service_name() {
134 return "RulesRegistryService";
136 static const bool kServiceHasOwnInstanceInIncognito = true;
137 static const bool kServiceIsNULLWhileTesting = true;
139 RulesRegistryMap rule_registries_;
141 // We own the parts of the registries which need to run on the UI thread.
142 ScopedVector<RulesCacheDelegate> cache_delegates_;
144 // Weak pointer into rule_registries_ to make it easier to handle content rule
146 ContentRulesRegistry* content_rules_registry_;
148 content::NotificationRegistrar registrar_;
150 // Listen to extension load, unloaded notification.
151 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
152 extension_registry_observer_;
156 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService);
159 } // namespace extensions
161 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__