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
),
45 rules_registry_id(rules_registry_id
) {}
46 bool operator<(const RulesRegistryKey
& other
) const {
47 return (event_name
< other
.event_name
) ||
48 ((event_name
== other
.event_name
) &&
49 (rules_registry_id
< other
.rules_registry_id
));
53 explicit RulesRegistryService(content::BrowserContext
* context
);
54 ~RulesRegistryService() override
;
56 // Unregisters refptrs to concrete RulesRegistries at other objects that were
57 // created by us so that the RulesRegistries can be released.
58 void Shutdown() override
;
60 // BrowserContextKeyedAPI implementation.
61 static BrowserContextKeyedAPIFactory
<RulesRegistryService
>*
64 // Convenience method to get the RulesRegistryService for a context. If a
65 // RulesRegistryService does not already exist for |context|, one will be
66 // created and returned.
67 static RulesRegistryService
* Get(content::BrowserContext
* context
);
69 // The same as Get(), except that if a RulesRegistryService does not already
70 // exist for |context|, nullptr is returned.
71 static RulesRegistryService
* GetIfExists(content::BrowserContext
* context
);
73 int GetNextRulesRegistryID();
75 // Registers the default RulesRegistries used in Chromium.
76 void EnsureDefaultRulesRegistriesRegistered(int rules_registry_id
);
78 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
79 void RegisterRulesRegistry(scoped_refptr
<RulesRegistry
> rule_registry
);
81 // Returns the RulesRegistry for |event_name| and |rules_registry_id| or
82 // NULL if no such registry has been registered. Default rules registries
83 // (such as the WebRequest rules registry) will be created on first access.
84 scoped_refptr
<RulesRegistry
> GetRulesRegistry(int rules_registry_id
,
85 const std::string
& event_name
);
87 // Remove all rules registries of the given rules_registry_id.
88 void RemoveRulesRegistriesByID(int rules_registry_id
);
90 // Accessors for each type of rules registry.
91 ContentRulesRegistry
* content_rules_registry() const {
92 CHECK(content_rules_registry_
);
93 return content_rules_registry_
;
97 void SimulateExtensionUninstalled(const Extension
* extension
);
100 friend class BrowserContextKeyedAPIFactory
<RulesRegistryService
>;
102 // Maps <event name, rules registry ID> to RuleRegistries that handle these
104 typedef std::map
<RulesRegistryKey
, scoped_refptr
<RulesRegistry
> >
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| 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 Extension
*),
123 const Extension
* extension
);
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 int current_rules_registry_id_
;
134 RulesRegistryMap rule_registries_
;
136 // We own the parts of the registries which need to run on the UI thread.
137 ScopedVector
<RulesCacheDelegate
> cache_delegates_
;
139 // Weak pointer into rule_registries_ to make it easier to handle content rule
141 ContentRulesRegistry
* content_rules_registry_
;
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__