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