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 "chrome/browser/extensions/api/declarative/rules_registry.h"
16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
24 class NotificationSource;
25 class NotificationSource;
28 namespace extensions {
29 class ContentRulesRegistry;
31 class RulesRegistryStorageDelegate;
34 namespace extensions {
36 // This class owns all RulesRegistries implementations of an ExtensionService.
37 // This class lives on the UI thread.
38 class RulesRegistryService : public ProfileKeyedAPI,
39 public content::NotificationObserver {
41 explicit RulesRegistryService(Profile* profile);
42 virtual ~RulesRegistryService();
44 // Unregisters refptrs to concrete RulesRegistries at other objects that were
45 // created by us so that the RulesRegistries can be released.
46 virtual void Shutdown() OVERRIDE;
48 // ProfileKeyedAPI implementation.
49 static ProfileKeyedAPIFactory<RulesRegistryService>* GetFactoryInstance();
51 // Convenience method to get the RulesRegistryService for a profile.
52 static RulesRegistryService* Get(Profile* profile);
54 // Registers the default RulesRegistries used in Chromium.
55 void RegisterDefaultRulesRegistries();
57 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
58 void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);
60 // Returns the RulesRegistry for |event_name| or NULL if no such registry
61 // has been registered.
62 scoped_refptr<RulesRegistry> GetRulesRegistry(
63 const std::string& event_name) const;
65 // Accessors for each type of rules registry.
66 ContentRulesRegistry* content_rules_registry() const {
67 return content_rules_registry_;
71 void SimulateExtensionUnloaded(const std::string& extension_id);
73 friend class ProfileKeyedAPIFactory<RulesRegistryService>;
75 // Maps event names to RuleRegistries that handle these events.
76 typedef std::map<std::string, scoped_refptr<RulesRegistry> >
79 // Notifies all RulesRegistries that |extension_id| was unloaded.
80 // It is not guaranteed that this notification is processed synchronously.
81 // If extensions live on another thread, the notification is posted.
82 void OnExtensionUnloaded(const std::string& extension_id);
84 // Implementation of content::NotificationObserver.
85 virtual void Observe(int type,
86 const content::NotificationSource& source,
87 const content::NotificationDetails& details) OVERRIDE;
89 // ProfileKeyedAPI implementation.
90 static const char* service_name() {
91 return "RulesRegistryService";
93 static const bool kServiceHasOwnInstanceInIncognito = true;
94 static const bool kServiceIsNULLWhileTesting = true;
96 RulesRegistryMap rule_registries_;
98 // We own the parts of the registries which need to run on the UI thread.
99 ScopedVector<RulesCacheDelegate> cache_delegates_;
101 // Weak pointer into rule_registries_ to make it easier to handle content rule
103 ContentRulesRegistry* content_rules_registry_;
105 content::NotificationRegistrar registrar_;
109 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService);
112 } // namespace extensions
114 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__