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 EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
6 #define EXTENSIONS_BROWSER_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 "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/api/declarative/rules_registry.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/extension_registry_observer.h"
24 class NotificationSource;
27 namespace extensions {
28 class ContentRulesRegistry;
29 class ExtensionRegistry;
30 class RulesRegistryStorageDelegate;
33 namespace extensions {
35 // This class owns all RulesRegistries implementations of an ExtensionService.
36 // This class lives on the UI thread.
37 class RulesRegistryService : public BrowserContextKeyedAPI,
38 public content::NotificationObserver,
39 public ExtensionRegistryObserver {
41 typedef RulesRegistry::WebViewKey WebViewKey;
42 struct RulesRegistryKey {
43 std::string event_name;
44 WebViewKey webview_key;
45 RulesRegistryKey(const std::string event_name,
46 const WebViewKey& webview_key)
47 : event_name(event_name),
48 webview_key(webview_key) {}
49 bool operator<(const RulesRegistryKey& other) const {
50 return (event_name < other.event_name) ||
51 ((event_name == other.event_name) &&
52 (webview_key < other.webview_key));
56 explicit RulesRegistryService(content::BrowserContext* context);
57 ~RulesRegistryService() override;
59 // Unregisters refptrs to concrete RulesRegistries at other objects that were
60 // created by us so that the RulesRegistries can be released.
61 void Shutdown() override;
63 // BrowserContextKeyedAPI implementation.
64 static BrowserContextKeyedAPIFactory<RulesRegistryService>*
67 // Convenience method to get the RulesRegistryService for a context.
68 static RulesRegistryService* Get(content::BrowserContext* context);
70 // Registers the default RulesRegistries used in Chromium.
71 void EnsureDefaultRulesRegistriesRegistered(const WebViewKey& webview_key);
73 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
74 void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);
76 // Returns the RulesRegistry for |event_name| and |webview_key| or NULL if no
77 // such registry has been registered. Default rules registries (such as the
78 // WebRequest rules registry) will be created on first access.
79 scoped_refptr<RulesRegistry> GetRulesRegistry(const WebViewKey& webview_key,
80 const std::string& event_name);
82 // Accessors for each type of rules registry.
83 ContentRulesRegistry* content_rules_registry() const {
84 CHECK(content_rules_registry_);
85 return content_rules_registry_;
88 // Removes all rules registries of a given webview embedder process ID.
89 void RemoveWebViewRulesRegistries(int process_id);
92 void SimulateExtensionUninstalled(const std::string& extension_id);
95 friend class BrowserContextKeyedAPIFactory<RulesRegistryService>;
97 // Maps <event name, webview key> to RuleRegistries that handle these
99 typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> >
102 // Implementation of content::NotificationObserver.
103 void Observe(int type,
104 const content::NotificationSource& source,
105 const content::NotificationDetails& details) override;
107 // ExtensionRegistryObserver implementation.
108 void OnExtensionLoaded(content::BrowserContext* browser_context,
109 const Extension* extension) override;
110 void OnExtensionUnloaded(content::BrowserContext* browser_context,
111 const Extension* extension,
112 UnloadedExtensionInfo::Reason reason) override;
113 void OnExtensionUninstalled(content::BrowserContext* browser_context,
114 const Extension* extension,
115 extensions::UninstallReason reason) override;
117 // Iterates over all registries, and calls |notification_callback| on them
118 // 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
121 void NotifyRegistriesHelper(
122 void (RulesRegistry::*notification_callback)(const std::string&),
123 const std::string& extension_id);
125 // BrowserContextKeyedAPI implementation.
126 static const char* service_name() {
127 return "RulesRegistryService";
129 static const bool kServiceHasOwnInstanceInIncognito = true;
130 static const bool kServiceIsNULLWhileTesting = true;
132 RulesRegistryMap rule_registries_;
134 // We own the parts of the registries which need to run on the UI thread.
135 ScopedVector<RulesCacheDelegate> cache_delegates_;
137 // Weak pointer into rule_registries_ to make it easier to handle content rule
139 ContentRulesRegistry* content_rules_registry_;
141 content::NotificationRegistrar registrar_;
143 // Listen to extension load, unloaded notification.
144 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
145 extension_registry_observer_;
147 content::BrowserContext* browser_context_;
149 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService);
152 } // namespace extensions
154 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__