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.
65 static RulesRegistryService
* Get(content::BrowserContext
* context
);
67 int GetNextRulesRegistryID();
69 // Registers the default RulesRegistries used in Chromium.
70 void EnsureDefaultRulesRegistriesRegistered(int rules_registry_id
);
72 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
73 void RegisterRulesRegistry(scoped_refptr
<RulesRegistry
> rule_registry
);
75 // Returns the RulesRegistry for |event_name| and |rules_registry_id| or
76 // NULL if no such registry has been registered. Default rules registries
77 // (such as the WebRequest rules registry) will be created on first access.
78 scoped_refptr
<RulesRegistry
> GetRulesRegistry(int rules_registry_id
,
79 const std::string
& event_name
);
81 // Remove all rules registries of the given rules_registry_id.
82 void RemoveRulesRegistriesByID(int rules_registry_id
);
84 // Accessors for each type of rules registry.
85 ContentRulesRegistry
* content_rules_registry() const {
86 CHECK(content_rules_registry_
);
87 return content_rules_registry_
;
91 void SimulateExtensionUninstalled(const Extension
* extension
);
94 friend class BrowserContextKeyedAPIFactory
<RulesRegistryService
>;
96 // Maps <event name, rules registry ID> to RuleRegistries that handle these
98 typedef std::map
<RulesRegistryKey
, scoped_refptr
<RulesRegistry
> >
101 // ExtensionRegistryObserver implementation.
102 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
103 const Extension
* extension
) override
;
104 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
105 const Extension
* extension
,
106 UnloadedExtensionInfo::Reason reason
) override
;
107 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
108 const Extension
* extension
,
109 extensions::UninstallReason reason
) override
;
111 // Iterates over all registries, and calls |notification_callback| on them
112 // with |extension| as the argument. If a registry lives on a different
113 // thread, the call is posted to that thread, so no guarantee of synchronous
115 void NotifyRegistriesHelper(
116 void (RulesRegistry::*notification_callback
)(const Extension
*),
117 const Extension
* extension
);
119 // BrowserContextKeyedAPI implementation.
120 static const char* service_name() {
121 return "RulesRegistryService";
123 static const bool kServiceHasOwnInstanceInIncognito
= true;
124 static const bool kServiceIsNULLWhileTesting
= true;
126 int current_rules_registry_id_
;
128 RulesRegistryMap rule_registries_
;
130 // We own the parts of the registries which need to run on the UI thread.
131 ScopedVector
<RulesCacheDelegate
> cache_delegates_
;
133 // Weak pointer into rule_registries_ to make it easier to handle content rule
135 ContentRulesRegistry
* content_rules_registry_
;
137 // Listen to extension load, unloaded notification.
138 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
139 extension_registry_observer_
;
141 content::BrowserContext
* browser_context_
;
143 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService
);
146 } // namespace extensions
148 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__