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 "base/scoped_observer.h" |
15 #include "chrome/browser/extensions/api/declarative/rules_registry.h" | 16 #include "chrome/browser/extensions/api/declarative/rules_registry.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 #include "extensions/browser/browser_context_keyed_api_factory.h" | 20 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 21 #include "extensions/browser/extension_registry_observer.h" |
20 | 22 |
21 class Profile; | 23 class Profile; |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 class BrowserContext; | 26 class BrowserContext; |
25 class NotificationSource; | 27 class NotificationSource; |
26 } | 28 } |
27 | 29 |
28 namespace extensions { | 30 namespace extensions { |
29 class ContentRulesRegistry; | 31 class ContentRulesRegistry; |
| 32 class ExtensionRegistry; |
30 class RulesRegistry; | 33 class RulesRegistry; |
31 class RulesRegistryStorageDelegate; | 34 class RulesRegistryStorageDelegate; |
32 } | 35 } |
33 | 36 |
34 namespace extensions { | 37 namespace extensions { |
35 | 38 |
36 // This class owns all RulesRegistries implementations of an ExtensionService. | 39 // This class owns all RulesRegistries implementations of an ExtensionService. |
37 // This class lives on the UI thread. | 40 // This class lives on the UI thread. |
38 class RulesRegistryService : public BrowserContextKeyedAPI, | 41 class RulesRegistryService : public BrowserContextKeyedAPI, |
39 public content::NotificationObserver { | 42 public content::NotificationObserver, |
| 43 public ExtensionRegistryObserver { |
40 public: | 44 public: |
41 typedef RulesRegistry::WebViewKey WebViewKey; | 45 typedef RulesRegistry::WebViewKey WebViewKey; |
42 struct RulesRegistryKey { | 46 struct RulesRegistryKey { |
43 std::string event_name; | 47 std::string event_name; |
44 WebViewKey webview_key; | 48 WebViewKey webview_key; |
45 RulesRegistryKey(const std::string event_name, | 49 RulesRegistryKey(const std::string event_name, |
46 const WebViewKey& webview_key) | 50 const WebViewKey& webview_key) |
47 : event_name(event_name), | 51 : event_name(event_name), |
48 webview_key(webview_key) {} | 52 webview_key(webview_key) {} |
49 bool operator<(const RulesRegistryKey& other) const { | 53 bool operator<(const RulesRegistryKey& other) const { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // Maps <event name, webview key> to RuleRegistries that handle these | 102 // Maps <event name, webview key> to RuleRegistries that handle these |
99 // events. | 103 // events. |
100 typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> > | 104 typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> > |
101 RulesRegistryMap; | 105 RulesRegistryMap; |
102 | 106 |
103 // Implementation of content::NotificationObserver. | 107 // Implementation of content::NotificationObserver. |
104 virtual void Observe(int type, | 108 virtual void Observe(int type, |
105 const content::NotificationSource& source, | 109 const content::NotificationSource& source, |
106 const content::NotificationDetails& details) OVERRIDE; | 110 const content::NotificationDetails& details) OVERRIDE; |
107 | 111 |
| 112 // ExtensionRegistryObserver implementation. |
| 113 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 114 const Extension* extension) OVERRIDE; |
| 115 virtual void OnExtensionUnloaded( |
| 116 content::BrowserContext* browser_context, |
| 117 const Extension* extension, |
| 118 UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 119 |
108 // Iterates over all registries, and calls |notification_callback| on them | 120 // Iterates over all registries, and calls |notification_callback| on them |
109 // with |extension_id| as the argument. If a registry lives on a different | 121 // with |extension_id| as the argument. If a registry lives on a different |
110 // thread, the call is posted to that thread, so no guarantee of synchronous | 122 // thread, the call is posted to that thread, so no guarantee of synchronous |
111 // processing. | 123 // processing. |
112 void NotifyRegistriesHelper( | 124 void NotifyRegistriesHelper( |
113 void (RulesRegistry::*notification_callback)(const std::string&), | 125 void (RulesRegistry::*notification_callback)(const std::string&), |
114 const std::string& extension_id); | 126 const std::string& extension_id); |
115 | 127 |
116 // BrowserContextKeyedAPI implementation. | 128 // BrowserContextKeyedAPI implementation. |
117 static const char* service_name() { | 129 static const char* service_name() { |
118 return "RulesRegistryService"; | 130 return "RulesRegistryService"; |
119 } | 131 } |
120 static const bool kServiceHasOwnInstanceInIncognito = true; | 132 static const bool kServiceHasOwnInstanceInIncognito = true; |
121 static const bool kServiceIsNULLWhileTesting = true; | 133 static const bool kServiceIsNULLWhileTesting = true; |
122 | 134 |
123 RulesRegistryMap rule_registries_; | 135 RulesRegistryMap rule_registries_; |
124 | 136 |
125 // We own the parts of the registries which need to run on the UI thread. | 137 // We own the parts of the registries which need to run on the UI thread. |
126 ScopedVector<RulesCacheDelegate> cache_delegates_; | 138 ScopedVector<RulesCacheDelegate> cache_delegates_; |
127 | 139 |
128 // Weak pointer into rule_registries_ to make it easier to handle content rule | 140 // Weak pointer into rule_registries_ to make it easier to handle content rule |
129 // conditions. | 141 // conditions. |
130 ContentRulesRegistry* content_rules_registry_; | 142 ContentRulesRegistry* content_rules_registry_; |
131 | 143 |
132 content::NotificationRegistrar registrar_; | 144 content::NotificationRegistrar registrar_; |
133 | 145 |
| 146 // Listen to extension load, unloaded notification. |
| 147 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 148 extension_registry_observer_; |
| 149 |
134 Profile* profile_; | 150 Profile* profile_; |
135 | 151 |
136 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); | 152 DISALLOW_COPY_AND_ASSIGN(RulesRegistryService); |
137 }; | 153 }; |
138 | 154 |
139 } // namespace extensions | 155 } // namespace extensions |
140 | 156 |
141 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ | 157 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__ |
OLD | NEW |