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 #include "extensions/browser/api/declarative/rules_registry_service.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_details.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/notification_types.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "extensions/browser/api/declarative/rules_cache_delegate.h"
18 #include "extensions/browser/api/declarative_content/content_rules_registry.h"
19 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
20 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registry.h"
21 #include "extensions/browser/api/extensions_api_client.h"
22 #include "extensions/browser/api/web_request/web_request_api.h"
23 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h"
26 namespace extensions
{
30 // Registers |web_request_rules_registry| on the IO thread.
31 void RegisterToExtensionWebRequestEventRouterOnIO(
32 content::BrowserContext
* browser_context
,
33 const RulesRegistryService::WebViewKey
& webview_key
,
34 scoped_refptr
<WebRequestRulesRegistry
> web_request_rules_registry
) {
35 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
36 browser_context
, webview_key
, web_request_rules_registry
);
39 bool IsWebView(const RulesRegistryService::WebViewKey
& webview_key
) {
40 return webview_key
.embedder_process_id
&& webview_key
.webview_instance_id
;
45 RulesRegistryService::RulesRegistryService(content::BrowserContext
* context
)
46 : content_rules_registry_(NULL
),
47 extension_registry_observer_(this),
48 browser_context_(context
) {
49 if (browser_context_
) {
50 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context_
));
52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
53 content::NotificationService::AllBrowserContextsAndSources());
54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
58 RulesRegistryService::~RulesRegistryService() {}
60 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
61 const WebViewKey
& webview_key
) {
62 if (!browser_context_
)
65 RulesRegistryKey
key(declarative_webrequest_constants::kOnRequest
,
67 // If we can find the key in the |rule_registries_| then we have already
68 // installed the default registries.
69 if (ContainsKey(rule_registries_
, key
))
73 RulesCacheDelegate
* web_request_cache_delegate
= NULL
;
74 if (!IsWebView(webview_key
)) {
75 web_request_cache_delegate
=
76 new RulesCacheDelegate(true /*log_storage_init_delay*/);
77 cache_delegates_
.push_back(web_request_cache_delegate
);
79 scoped_refptr
<WebRequestRulesRegistry
> web_request_rules_registry(
80 new WebRequestRulesRegistry(browser_context_
,
81 web_request_cache_delegate
,
84 RegisterRulesRegistry(web_request_rules_registry
);
85 content::BrowserThread::PostTask(
86 content::BrowserThread::IO
, FROM_HERE
,
87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO
,
88 browser_context_
, webview_key
, web_request_rules_registry
));
90 // Only create a ContentRulesRegistry for regular pages and not webviews.
91 if (!IsWebView(webview_key
)) {
92 RulesCacheDelegate
* content_rules_cache_delegate
=
93 new RulesCacheDelegate(false /*log_storage_init_delay*/);
94 cache_delegates_
.push_back(content_rules_cache_delegate
);
95 scoped_refptr
<ContentRulesRegistry
> content_rules_registry
=
96 ExtensionsAPIClient::Get()->CreateContentRulesRegistry(
97 browser_context_
, content_rules_cache_delegate
);
98 if (content_rules_registry
.get() != nullptr) {
99 RegisterRulesRegistry(content_rules_registry
);
100 content_rules_registry_
= content_rules_registry
.get();
105 void RulesRegistryService::Shutdown() {
106 // Release the references to all registries. This would happen soon during
107 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
108 // be the last to reference the WebRequestRulesRegistry objects, so that
109 // the posted task below causes their destruction on the IO thread, not on UI
110 // where the destruction of |*this| takes place.
111 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
112 rule_registries_
.clear();
113 content::BrowserThread::PostTask(
114 content::BrowserThread::IO
, FROM_HERE
,
115 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO
,
116 browser_context_
, WebViewKey(0, 0),
117 scoped_refptr
<WebRequestRulesRegistry
>(NULL
)));
120 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<RulesRegistryService
> >
121 g_factory
= LAZY_INSTANCE_INITIALIZER
;
124 BrowserContextKeyedAPIFactory
<RulesRegistryService
>*
125 RulesRegistryService::GetFactoryInstance() {
126 return g_factory
.Pointer();
130 RulesRegistryService
* RulesRegistryService::Get(
131 content::BrowserContext
* context
) {
132 return BrowserContextKeyedAPIFactory
<RulesRegistryService
>::Get(context
);
135 void RulesRegistryService::RegisterRulesRegistry(
136 scoped_refptr
<RulesRegistry
> rule_registry
) {
137 const std::string
event_name(rule_registry
->event_name());
138 RulesRegistryKey
key(event_name
, rule_registry
->webview_key());
139 DCHECK(rule_registries_
.find(key
) == rule_registries_
.end());
140 rule_registries_
[key
] = rule_registry
;
143 scoped_refptr
<RulesRegistry
> RulesRegistryService::GetRulesRegistry(
144 const WebViewKey
& webview_key
,
145 const std::string
& event_name
) {
146 EnsureDefaultRulesRegistriesRegistered(webview_key
);
148 RulesRegistryKey
key(event_name
, webview_key
);
149 RulesRegistryMap::const_iterator i
= rule_registries_
.find(key
);
150 if (i
== rule_registries_
.end())
151 return scoped_refptr
<RulesRegistry
>();
155 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id
) {
156 DCHECK_NE(0, process_id
);
158 std::set
<RulesRegistryKey
> registries_to_delete
;
159 for (RulesRegistryMap::iterator it
= rule_registries_
.begin();
160 it
!= rule_registries_
.end(); ++it
) {
161 const RulesRegistryKey
& key
= it
->first
;
162 const WebViewKey
& webview_key
= key
.webview_key
;
163 int embedder_process_id
= webview_key
.embedder_process_id
;
164 // |process_id| will always be non-zero.
165 // |embedder_process_id| will only be non-zero if the key corresponds to a
167 // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
169 if (embedder_process_id
!= process_id
)
172 // Modifying the container while iterating is bad so we'll save the keys we
173 // wish to delete in another container, and delete them in another loop.
174 registries_to_delete
.insert(key
);
176 for (std::set
<RulesRegistryKey
>::iterator it
= registries_to_delete
.begin();
177 it
!= registries_to_delete
.end(); ++it
) {
178 rule_registries_
.erase(*it
);
182 void RulesRegistryService::SimulateExtensionUninstalled(
183 const std::string
& extension_id
) {
184 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled
, extension_id
);
187 void RulesRegistryService::NotifyRegistriesHelper(
188 void (RulesRegistry::*notification_callback
)(const std::string
&),
189 const std::string
& extension_id
) {
190 RulesRegistryMap::iterator i
;
191 for (i
= rule_registries_
.begin(); i
!= rule_registries_
.end(); ++i
) {
192 scoped_refptr
<RulesRegistry
> registry
= i
->second
;
193 if (content::BrowserThread::CurrentlyOn(registry
->owner_thread())) {
194 (registry
.get()->*notification_callback
)(extension_id
);
196 content::BrowserThread::PostTask(
197 registry
->owner_thread(),
199 base::Bind(notification_callback
, registry
, extension_id
));
204 void RulesRegistryService::OnExtensionLoaded(
205 content::BrowserContext
* browser_context
,
206 const Extension
* extension
) {
207 NotifyRegistriesHelper(&RulesRegistry::OnExtensionLoaded
, extension
->id());
210 void RulesRegistryService::OnExtensionUnloaded(
211 content::BrowserContext
* browser_context
,
212 const Extension
* extension
,
213 UnloadedExtensionInfo::Reason reason
) {
214 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUnloaded
, extension
->id());
217 void RulesRegistryService::OnExtensionUninstalled(
218 content::BrowserContext
* browser_context
,
219 const Extension
* extension
,
220 extensions::UninstallReason reason
) {
221 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled
,
225 void RulesRegistryService::Observe(
227 const content::NotificationSource
& source
,
228 const content::NotificationDetails
& details
) {
229 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
, type
);
231 content::RenderProcessHost
* process
=
232 content::Source
<content::RenderProcessHost
>(source
).ptr();
233 RemoveWebViewRulesRegistries(process
->GetID());
236 } // namespace extensions