OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" | 15 #include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h" |
| 16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
19 | 20 |
20 class Profile; | 21 class Profile; |
21 | 22 |
22 namespace content { | 23 namespace content { |
23 class NotificationSource; | 24 class NotificationSource; |
24 class NotificationSource; | 25 class NotificationSource; |
25 } | 26 } |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 class ContentRulesRegistry; | 29 class ContentRulesRegistry; |
29 class RulesRegistry; | 30 class RulesRegistry; |
30 class RulesRegistryStorageDelegate; | 31 class RulesRegistryStorageDelegate; |
31 } | 32 } |
32 | 33 |
33 namespace extensions { | 34 namespace extensions { |
34 | 35 |
35 // This class owns all RulesRegistries implementations of an ExtensionService. | 36 // This class owns all RulesRegistries implementations of an ExtensionService. |
36 // This class lives on the UI thread. | 37 // This class lives on the UI thread. |
37 class RulesRegistryService : public content::NotificationObserver { | 38 class RulesRegistryService : public ProfileKeyedAPI, |
| 39 public content::NotificationObserver { |
38 public: | 40 public: |
39 explicit RulesRegistryService(Profile* profile); | 41 explicit RulesRegistryService(Profile* profile); |
40 virtual ~RulesRegistryService(); | 42 virtual ~RulesRegistryService(); |
41 | 43 |
42 // Unregisters refptrs to concrete RulesRegistries at other objects that were | 44 // Unregisters refptrs to concrete RulesRegistries at other objects that were |
43 // created by us so that the RulesRegistries can be released. | 45 // created by us so that the RulesRegistries can be released. |
44 void Shutdown(); | 46 virtual void Shutdown() OVERRIDE; |
| 47 |
| 48 // ProfileKeyedAPI implementation. |
| 49 static ProfileKeyedAPIFactory<RulesRegistryService>* GetFactoryInstance(); |
| 50 |
| 51 // Convenience method to get the RulesRegistryService for a profile. |
| 52 static RulesRegistryService* Get(Profile* profile); |
45 | 53 |
46 // Registers the default RulesRegistries used in Chromium. | 54 // Registers the default RulesRegistries used in Chromium. |
47 void RegisterDefaultRulesRegistries(); | 55 void RegisterDefaultRulesRegistries(); |
48 | 56 |
49 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry. | 57 // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry. |
50 void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry); | 58 void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry); |
51 | 59 |
52 // Returns the RulesRegistry for |event_name| or NULL if no such registry | 60 // Returns the RulesRegistry for |event_name| or NULL if no such registry |
53 // has been registered. | 61 // has been registered. |
54 scoped_refptr<RulesRegistry> GetRulesRegistry( | 62 scoped_refptr<RulesRegistry> GetRulesRegistry( |
55 const std::string& event_name) const; | 63 const std::string& event_name) const; |
56 | 64 |
57 // Accessors for each type of rules registry. | 65 // Accessors for each type of rules registry. |
58 ContentRulesRegistry* content_rules_registry() const { | 66 ContentRulesRegistry* content_rules_registry() const { |
59 return content_rules_registry_; | 67 return content_rules_registry_; |
60 } | 68 } |
61 | 69 |
62 // For testing. | 70 // For testing. |
63 void SimulateExtensionUnloaded(const std::string& extension_id); | 71 void SimulateExtensionUnloaded(const std::string& extension_id); |
64 private: | 72 private: |
| 73 friend class ProfileKeyedAPIFactory<RulesRegistryService>; |
| 74 |
65 // Maps event names to RuleRegistries that handle these events. | 75 // Maps event names to RuleRegistries that handle these events. |
66 typedef std::map<std::string, scoped_refptr<RulesRegistry> > RulesRegistryMap; | 76 typedef std::map<std::string, scoped_refptr<RulesRegistry> > RulesRegistryMap; |
67 | 77 |
68 // Notifies all RulesRegistries that |extension_id| was unloaded. | 78 // Notifies all RulesRegistries that |extension_id| was unloaded. |
69 // It is not guaranteed that this notification is processed synchronously. | 79 // It is not guaranteed that this notification is processed synchronously. |
70 // If extensions live on another thread, the notification is posted. | 80 // If extensions live on another thread, the notification is posted. |
71 void OnExtensionUnloaded(const std::string& extension_id); | 81 void OnExtensionUnloaded(const std::string& extension_id); |
72 | 82 |
73 // Implementation of content::NotificationObserver. | 83 // Implementation of content::NotificationObserver. |
74 virtual void Observe(int type, | 84 virtual void Observe(int type, |
75 const content::NotificationSource& source, | 85 const content::NotificationSource& source, |
76 const content::NotificationDetails& details) OVERRIDE; | 86 const content::NotificationDetails& details) OVERRIDE; |
77 | 87 |
| 88 // ProfileKeyedAPI implementation. |
| 89 static const char* service_name() { |
| 90 return "RulesRegistryService"; |
| 91 } |
| 92 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 93 static const bool kServiceIsNULLWhileTesting = true; |
| 94 |
78 RulesRegistryMap rule_registries_; | 95 RulesRegistryMap rule_registries_; |
79 | 96 |
80 // We own the parts of the registries which need to run on the UI thread. | 97 // We own the parts of the registries which need to run on the UI thread. |
81 ScopedVector<RulesRegistryWithCache::RuleStorageOnUI> ui_parts_of_registries_; | 98 ScopedVector<RulesRegistryWithCache::RuleStorageOnUI> ui_parts_of_registries_; |
82 | 99 |
83 // Weak pointer into rule_registries_ to make it easier to handle content rule | 100 // Weak pointer into rule_registries_ to make it easier to handle content rule |
84 // conditions. | 101 // conditions. |
85 ContentRulesRegistry* content_rules_registry_; | 102 ContentRulesRegistry* content_rules_registry_; |
86 | 103 |
87 content::NotificationRegistrar registrar_; | 104 content::NotificationRegistrar registrar_; |
88 | 105 |
89 Profile* profile_; | 106 Profile* profile_; |
90 | 107 |
91 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); | 108 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); |
92 }; | 109 }; |
93 | 110 |
94 } // namespace extensions | 111 } // namespace extensions |
95 | 112 |
96 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 113 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |
OLD | NEW |