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 "extensions/browser/api/declarative/rules_registry.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/browser/extension_registry_observer.h"
24 namespace extensions
{
25 class ContentRulesRegistry
;
26 class ExtensionRegistry
;
27 class RulesRegistryStorageDelegate
;
30 namespace extensions
{
32 // This class owns all RulesRegistries implementations of an ExtensionService.
33 // This class lives on the UI thread.
34 class RulesRegistryService
: public BrowserContextKeyedAPI
,
35 public ExtensionRegistryObserver
{
37 static const int kDefaultRulesRegistryID
;
38 static const int kInvalidRulesRegistryID
;
40 struct RulesRegistryKey
{
41 std::string event_name
;
42 int rules_registry_id
;
43 RulesRegistryKey(const std::string
& event_name
, int rules_registry_id
)
44 : event_name(event_name
), rules_registry_id(rules_registry_id
) {}
45 bool operator<(const RulesRegistryKey
& other
) const {
46 return (event_name
< other
.event_name
) ||
47 ((event_name
== other
.event_name
) &&
48 (rules_registry_id
< other
.rules_registry_id
));
52 explicit RulesRegistryService(content::BrowserContext
* context
);
53 ~RulesRegistryService() override
;
55 // Unregisters refptrs to concrete RulesRegistries at other objects that were
56 // created by us so that the RulesRegistries can be released.
57 void Shutdown() override
;
59 // BrowserContextKeyedAPI implementation.
60 static BrowserContextKeyedAPIFactory
<RulesRegistryService
>*
63 // Convenience method to get the RulesRegistryService for a context. If a
64 // RulesRegistryService does not already exist for |context|, one will be
65 // created and returned.
66 static RulesRegistryService
* Get(content::BrowserContext
* context
);
68 // The same as Get(), except that if a RulesRegistryService does not already
69 // exist for |context|, nullptr is returned.
70 static RulesRegistryService
* GetIfExists(content::BrowserContext
* context
);
72 int GetNextRulesRegistryID();
74 // Registers the default RulesRegistries used in Chromium.
75 void EnsureDefaultRulesRegistriesRegistered(int rules_registry_id
);
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 |rules_registry_id| or
81 // NULL if no such registry has been registered. Default rules registries
82 // (such as the WebRequest rules registry) will be created on first access.
83 scoped_refptr
<RulesRegistry
> GetRulesRegistry(int rules_registry_id
,
84 const std::string
& event_name
);
86 // Remove all rules registries of the given rules_registry_id.
87 void RemoveRulesRegistriesByID(int rules_registry_id
);
89 // Accessors for each type of rules registry.
90 ContentRulesRegistry
* content_rules_registry() const {
91 CHECK(content_rules_registry_
);
92 return content_rules_registry_
;
96 void SimulateExtensionUninstalled(const Extension
* extension
);
99 friend class BrowserContextKeyedAPIFactory
<RulesRegistryService
>;
101 // Maps <event name, rules registry ID> to RuleRegistries that handle these
103 typedef std::map
<RulesRegistryKey
, scoped_refptr
<RulesRegistry
> >
106 // ExtensionRegistryObserver implementation.
107 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
108 const Extension
* extension
) override
;
109 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
110 const Extension
* extension
,
111 UnloadedExtensionInfo::Reason reason
) override
;
112 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
113 const Extension
* extension
,
114 extensions::UninstallReason reason
) override
;
116 // Iterates over all registries, and calls |notification_callback| on them
117 // with |extension| as the argument. If a registry lives on a different
118 // thread, the call is posted to that thread, so no guarantee of synchronous
120 void NotifyRegistriesHelper(
121 void (RulesRegistry::*notification_callback
)(const Extension
*),
122 const Extension
* extension
);
124 // BrowserContextKeyedAPI implementation.
125 static const char* service_name() {
126 return "RulesRegistryService";
128 static const bool kServiceHasOwnInstanceInIncognito
= true;
129 static const bool kServiceIsNULLWhileTesting
= true;
131 int current_rules_registry_id_
;
133 RulesRegistryMap rule_registries_
;
135 // We own the parts of the registries which need to run on the UI thread.
136 ScopedVector
<RulesCacheDelegate
> cache_delegates_
;
138 // Weak pointer into rule_registries_ to make it easier to handle content rule
140 ContentRulesRegistry
* content_rules_registry_
;
142 // Listen to extension load, unloaded notification.
143 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
144 extension_registry_observer_
;
146 content::BrowserContext
* browser_context_
;
148 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService
);
151 } // namespace extensions
153 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__