Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative / rules_registry_service.cc
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.
4
5 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h"
6
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
13 #include "chrome/browser/extensions/api/declarative_content/content_rules_registry.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h"
15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h"
16 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "extensions/common/extension.h"
24
25 namespace extensions {
26
27 namespace {
28
29 // Registers |web_request_rules_registry| on the IO thread.
30 void RegisterToExtensionWebRequestEventRouterOnIO(
31     void* profile,
32     const RulesRegistryService::WebViewKey& webview_key,
33     scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
34   ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
35       profile, webview_key, web_request_rules_registry);
36 }
37
38 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
39   return webview_key.embedder_process_id && webview_key.webview_instance_id;
40 }
41
42 }  // namespace
43
44 RulesRegistryService::RulesRegistryService(content::BrowserContext* context)
45     : content_rules_registry_(NULL),
46       profile_(Profile::FromBrowserContext(context)) {
47   if (profile_) {
48     registrar_.Add(this,
49                    chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
50                    content::Source<Profile>(profile_->GetOriginalProfile()));
51     registrar_.Add(this,
52                    chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
53                    content::Source<Profile>(profile_->GetOriginalProfile()));
54     registrar_.Add(this,
55                    chrome::NOTIFICATION_EXTENSION_LOADED,
56                    content::Source<Profile>(profile_->GetOriginalProfile()));
57     registrar_.Add(
58         this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
59         content::NotificationService::AllBrowserContextsAndSources());
60     EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
61   }
62 }
63
64 RulesRegistryService::~RulesRegistryService() {}
65
66 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
67     const WebViewKey& webview_key) {
68   if (!profile_)
69     return;
70
71   RulesRegistryKey key(declarative_webrequest_constants::kOnRequest,
72                        webview_key);
73   // If we can find the key in the |rule_registries_| then we have already
74   // installed the default registries.
75   if (ContainsKey(rule_registries_, key))
76     return;
77
78
79   RulesCacheDelegate* web_request_cache_delegate = NULL;
80   if (!IsWebView(webview_key)) {
81     web_request_cache_delegate =
82         new RulesCacheDelegate(true /*log_storage_init_delay*/);
83     cache_delegates_.push_back(web_request_cache_delegate);
84   }
85   scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry(
86       new WebRequestRulesRegistry(profile_,
87                                   web_request_cache_delegate,
88                                   webview_key));
89
90   RegisterRulesRegistry(web_request_rules_registry);
91   content::BrowserThread::PostTask(
92       content::BrowserThread::IO, FROM_HERE,
93       base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
94           profile_, webview_key, web_request_rules_registry));
95
96 #if defined(ENABLE_EXTENSIONS)
97   // Only create a ContentRulesRegistry for regular pages and not webviews.
98   if (!IsWebView(webview_key)) {
99     RulesCacheDelegate* content_rules_cache_delegate =
100         new RulesCacheDelegate(false /*log_storage_init_delay*/);
101     cache_delegates_.push_back(content_rules_cache_delegate);
102     scoped_refptr<ContentRulesRegistry> content_rules_registry(
103         new ContentRulesRegistry(profile_, content_rules_cache_delegate));
104     RegisterRulesRegistry(content_rules_registry);
105     content_rules_registry_ = content_rules_registry.get();
106   }
107 #endif  // defined(ENABLE_EXTENSIONS)
108 }
109
110 void RulesRegistryService::Shutdown() {
111   // Release the references to all registries. This would happen soon during
112   // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
113   // be the last to reference the WebRequestRulesRegistry objects, so that
114   // the posted task below causes their destruction on the IO thread, not on UI
115   // where the destruction of |*this| takes place.
116   // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
117   rule_registries_.clear();
118   content::BrowserThread::PostTask(
119       content::BrowserThread::IO, FROM_HERE,
120       base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
121           profile_, WebViewKey(0, 0),
122           scoped_refptr<WebRequestRulesRegistry>(NULL)));
123 }
124
125 static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> >
126     g_factory = LAZY_INSTANCE_INITIALIZER;
127
128 // static
129 BrowserContextKeyedAPIFactory<RulesRegistryService>*
130 RulesRegistryService::GetFactoryInstance() {
131   return g_factory.Pointer();
132 }
133
134 // static
135 RulesRegistryService* RulesRegistryService::Get(
136     content::BrowserContext* context) {
137   return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context);
138 }
139
140 void RulesRegistryService::RegisterRulesRegistry(
141     scoped_refptr<RulesRegistry> rule_registry) {
142   const std::string event_name(rule_registry->event_name());
143   RulesRegistryKey key(event_name, rule_registry->webview_key());
144   DCHECK(rule_registries_.find(key) == rule_registries_.end());
145   rule_registries_[key] = rule_registry;
146 }
147
148 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry(
149     const WebViewKey& webview_key,
150     const std::string& event_name) {
151   EnsureDefaultRulesRegistriesRegistered(webview_key);
152
153   RulesRegistryKey key(event_name, webview_key);
154   RulesRegistryMap::const_iterator i = rule_registries_.find(key);
155   if (i == rule_registries_.end())
156     return scoped_refptr<RulesRegistry>();
157   return i->second;
158 }
159
160 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
161   DCHECK_NE(0, process_id);
162
163   std::set<RulesRegistryKey> registries_to_delete;
164   for (RulesRegistryMap::iterator it = rule_registries_.begin();
165        it != rule_registries_.end(); ++it) {
166     const RulesRegistryKey& key = it->first;
167     const WebViewKey& webview_key = key.webview_key;
168     int embedder_process_id = webview_key.embedder_process_id;
169     // |process_id| will always be non-zero.
170     // |embedder_process_id| will only be non-zero if the key corresponds to a
171     // webview registry.
172     // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
173     // webview embedder.
174     if (embedder_process_id != process_id)
175       continue;
176
177     // Modifying the container while iterating is bad so we'll save the keys we
178     // wish to delete in another container, and delete them in another loop.
179     registries_to_delete.insert(key);
180   }
181   for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin();
182        it != registries_to_delete.end(); ++it) {
183     rule_registries_.erase(*it);
184   }
185 }
186
187 void RulesRegistryService::SimulateExtensionUninstalled(
188     const std::string& extension_id) {
189   NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id);
190 }
191
192 void RulesRegistryService::NotifyRegistriesHelper(
193     void (RulesRegistry::*notification_callback)(const std::string&),
194     const std::string& extension_id) {
195   RulesRegistryMap::iterator i;
196   for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) {
197     scoped_refptr<RulesRegistry> registry = i->second;
198     if (content::BrowserThread::CurrentlyOn(registry->owner_thread())) {
199       (registry->*notification_callback)(extension_id);
200     } else {
201       content::BrowserThread::PostTask(
202           registry->owner_thread(),
203           FROM_HERE,
204           base::Bind(notification_callback, registry, extension_id));
205     }
206   }
207 }
208
209 void RulesRegistryService::Observe(
210     int type,
211     const content::NotificationSource& source,
212     const content::NotificationDetails& details) {
213   switch (type) {
214     case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
215       const Extension* extension =
216           content::Details<UnloadedExtensionInfo>(details)->extension;
217       NotifyRegistriesHelper(&RulesRegistry::OnExtensionUnloaded,
218                              extension->id());
219       break;
220     }
221     case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
222       const Extension* extension =
223           content::Details<const Extension>(details).ptr();
224       NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled,
225                              extension->id());
226       break;
227     }
228     case chrome::NOTIFICATION_EXTENSION_LOADED: {
229       const Extension* extension =
230           content::Details<const Extension>(details).ptr();
231       NotifyRegistriesHelper(&RulesRegistry::OnExtensionLoaded,
232                              extension->id());
233       break;
234     }
235     case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
236       content::RenderProcessHost* process =
237           content::Source<content::RenderProcessHost>(source).ptr();
238       RemoveWebViewRulesRegistries(process->GetID());
239       break;
240     }
241     default:
242       NOTREACHED();
243       break;
244   }
245 }
246
247 }  // namespace extensions