Chromium Code Reviews| 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 #include "extensions/browser/api/declarative/rules_registry_service.h" | 5 #include "extensions/browser/api/declarative/rules_registry_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/notification_details.h" | 12 #include "content/public/browser/notification_details.h" |
| 13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
| 14 #include "content/public/browser/notification_source.h" | 14 #include "content/public/browser/notification_source.h" |
| 15 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "extensions/browser/api/declarative/rules_cache_delegate.h" | 17 #include "extensions/browser/api/declarative/rules_cache_delegate.h" |
| 18 #include "extensions/browser/api/declarative_content/content_rules_registry.h" | 18 #include "extensions/browser/api/declarative_content/content_rules_registry.h" |
| 19 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 19 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 20 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h" | 20 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h" |
| 21 #include "extensions/browser/api/extensions_api_client.h" | 21 #include "extensions/browser/api/extensions_api_client.h" |
| 22 #include "extensions/browser/api/web_request/web_request_api.h" | 22 #include "extensions/browser/api/web_request/web_request_api.h" |
| 23 #include "extensions/browser/extension_registry.h" | 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | |
|
Fady Samuel
2014/11/26 23:39:40
Avoid this include.
Xi Han
2014/11/27 23:25:25
Done.
| |
| 24 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 // Registers |web_request_rules_registry| on the IO thread. | 31 // Registers |web_request_rules_registry| on the IO thread. |
| 31 void RegisterToExtensionWebRequestEventRouterOnIO( | 32 void RegisterToExtensionWebRequestEventRouterOnIO( |
| 32 content::BrowserContext* browser_context, | 33 content::BrowserContext* browser_context, |
| 33 const RulesRegistryService::WebViewKey& webview_key, | 34 int rules_registry_id, |
| 34 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { | 35 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { |
| 35 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( | 36 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( |
| 36 browser_context, webview_key, web_request_rules_registry); | 37 browser_context, rules_registry_id, web_request_rules_registry); |
| 37 } | |
| 38 | |
| 39 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) { | |
| 40 return webview_key.embedder_process_id && webview_key.webview_instance_id; | |
| 41 } | 38 } |
| 42 | 39 |
| 43 } // namespace | 40 } // namespace |
| 44 | 41 |
| 45 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) | 42 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) |
| 46 : content_rules_registry_(NULL), | 43 : current_rules_registry_id_(0), |
| 44 content_rules_registry_(NULL), | |
| 47 extension_registry_observer_(this), | 45 extension_registry_observer_(this), |
| 48 browser_context_(context) { | 46 browser_context_(context) { |
| 49 if (browser_context_) { | 47 if (browser_context_) { |
| 50 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 48 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 51 registrar_.Add( | 49 registrar_.Add( |
| 52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 50 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 53 content::NotificationService::AllBrowserContextsAndSources()); | 51 content::NotificationService::AllBrowserContextsAndSources()); |
| 54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0)); | 52 EnsureDefaultRulesRegistriesRegistered(kDefultRulesRegistryID, false); |
| 55 } | 53 } |
| 56 } | 54 } |
| 57 | 55 |
| 58 RulesRegistryService::~RulesRegistryService() {} | 56 RulesRegistryService::~RulesRegistryService() {} |
| 59 | 57 |
| 58 int RulesRegistryService::GetNextRulesRegistryId() { | |
|
Fady Samuel
2014/11/26 23:39:40
GetNextRulesRegistryID
Xi Han
2014/11/27 23:25:25
Done.
| |
| 59 return ++current_rules_registry_id_; | |
| 60 } | |
| 61 | |
| 60 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered( | 62 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered( |
| 61 const WebViewKey& webview_key) { | 63 int rules_registry_id, |
| 64 bool is_web_view) { | |
|
Fady Samuel
2014/11/26 23:39:40
bool cache_rules
Xi Han
2014/11/27 23:25:25
Done.
| |
| 62 if (!browser_context_) | 65 if (!browser_context_) |
| 63 return; | 66 return; |
| 64 | |
| 65 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest, | 67 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest, |
| 66 webview_key); | 68 rules_registry_id); |
| 67 // If we can find the key in the |rule_registries_| then we have already | 69 // If we can find the key in the |rule_registries_| then we have already |
| 68 // installed the default registries. | 70 // installed the default registries. |
| 69 if (ContainsKey(rule_registries_, key)) | 71 if (ContainsKey(rule_registries_, key)) |
| 70 return; | 72 return; |
| 71 | 73 |
| 72 | 74 |
| 73 RulesCacheDelegate* web_request_cache_delegate = NULL; | 75 RulesCacheDelegate* web_request_cache_delegate = NULL; |
| 74 if (!IsWebView(webview_key)) { | 76 if (!is_web_view) { |
|
Fady Samuel
2014/11/26 23:39:40
if (!cache_rules)
Xi Han
2014/11/27 23:25:25
Done.
| |
| 75 web_request_cache_delegate = | 77 web_request_cache_delegate = |
| 76 new RulesCacheDelegate(true /*log_storage_init_delay*/); | 78 new RulesCacheDelegate(true /*log_storage_init_delay*/); |
| 77 cache_delegates_.push_back(web_request_cache_delegate); | 79 cache_delegates_.push_back(web_request_cache_delegate); |
| 78 } | 80 } |
| 79 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( | 81 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( |
| 80 new WebRequestRulesRegistry(browser_context_, | 82 new WebRequestRulesRegistry(browser_context_, web_request_cache_delegate, |
| 81 web_request_cache_delegate, | 83 rules_registry_id)); |
| 82 webview_key)); | |
| 83 | 84 |
| 84 RegisterRulesRegistry(web_request_rules_registry); | 85 RegisterRulesRegistry(web_request_rules_registry); |
| 85 content::BrowserThread::PostTask( | 86 content::BrowserThread::PostTask( |
| 86 content::BrowserThread::IO, FROM_HERE, | 87 content::BrowserThread::IO, FROM_HERE, |
| 87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, | 88 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, |
| 88 browser_context_, webview_key, web_request_rules_registry)); | 89 browser_context_, rules_registry_id, |
| 90 web_request_rules_registry)); | |
| 89 | 91 |
| 90 // Only create a ContentRulesRegistry for regular pages and not webviews. | 92 // Only create a ContentRulesRegistry for regular pages and not webviews. |
| 91 if (!IsWebView(webview_key)) { | 93 if (!is_web_view) { |
|
Fady Samuel
2014/11/26 23:39:41
!cache_rules
Xi Han
2014/11/27 23:25:25
Done.
| |
| 92 RulesCacheDelegate* content_rules_cache_delegate = | 94 RulesCacheDelegate* content_rules_cache_delegate = |
| 93 new RulesCacheDelegate(false /*log_storage_init_delay*/); | 95 new RulesCacheDelegate(false /*log_storage_init_delay*/); |
| 94 cache_delegates_.push_back(content_rules_cache_delegate); | 96 cache_delegates_.push_back(content_rules_cache_delegate); |
| 95 scoped_refptr<ContentRulesRegistry> content_rules_registry = | 97 scoped_refptr<ContentRulesRegistry> content_rules_registry = |
| 96 ExtensionsAPIClient::Get()->CreateContentRulesRegistry( | 98 ExtensionsAPIClient::Get()->CreateContentRulesRegistry( |
| 97 browser_context_, content_rules_cache_delegate); | 99 browser_context_, content_rules_cache_delegate); |
| 98 if (content_rules_registry.get() != nullptr) { | 100 if (content_rules_registry.get() != nullptr) { |
| 99 RegisterRulesRegistry(content_rules_registry); | 101 RegisterRulesRegistry(content_rules_registry); |
| 100 content_rules_registry_ = content_rules_registry.get(); | 102 content_rules_registry_ = content_rules_registry.get(); |
| 101 } | 103 } |
| 102 } | 104 } |
| 103 } | 105 } |
| 104 | 106 |
| 105 void RulesRegistryService::Shutdown() { | 107 void RulesRegistryService::Shutdown() { |
| 106 // Release the references to all registries. This would happen soon during | 108 // Release the references to all registries. This would happen soon during |
| 107 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to | 109 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to |
| 108 // be the last to reference the WebRequestRulesRegistry objects, so that | 110 // 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 | 111 // the posted task below causes their destruction on the IO thread, not on UI |
| 110 // where the destruction of |*this| takes place. | 112 // where the destruction of |*this| takes place. |
| 111 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. | 113 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. |
| 112 rule_registries_.clear(); | 114 rule_registries_.clear(); |
| 113 content::BrowserThread::PostTask( | 115 content::BrowserThread::PostTask( |
| 114 content::BrowserThread::IO, FROM_HERE, | 116 content::BrowserThread::IO, FROM_HERE, |
| 115 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, | 117 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, |
| 116 browser_context_, WebViewKey(0, 0), | 118 browser_context_, 0 /* not for web_view*/, |
|
Fady Samuel
2014/11/26 23:39:41
Use kDefaultRulesRegistryID
Xi Han
2014/11/27 23:25:25
Done.
| |
| 117 scoped_refptr<WebRequestRulesRegistry>(NULL))); | 119 scoped_refptr<WebRequestRulesRegistry>(NULL))); |
| 118 } | 120 } |
| 119 | 121 |
| 120 static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> > | 122 static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> > |
| 121 g_factory = LAZY_INSTANCE_INITIALIZER; | 123 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 122 | 124 |
| 123 // static | 125 // static |
| 124 BrowserContextKeyedAPIFactory<RulesRegistryService>* | 126 BrowserContextKeyedAPIFactory<RulesRegistryService>* |
| 125 RulesRegistryService::GetFactoryInstance() { | 127 RulesRegistryService::GetFactoryInstance() { |
| 126 return g_factory.Pointer(); | 128 return g_factory.Pointer(); |
| 127 } | 129 } |
| 128 | 130 |
| 129 // static | 131 // static |
| 130 RulesRegistryService* RulesRegistryService::Get( | 132 RulesRegistryService* RulesRegistryService::Get( |
| 131 content::BrowserContext* context) { | 133 content::BrowserContext* context) { |
| 132 return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context); | 134 return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context); |
| 133 } | 135 } |
| 134 | 136 |
| 135 void RulesRegistryService::RegisterRulesRegistry( | 137 void RulesRegistryService::RegisterRulesRegistry( |
| 136 scoped_refptr<RulesRegistry> rule_registry) { | 138 scoped_refptr<RulesRegistry> rule_registry) { |
| 137 const std::string event_name(rule_registry->event_name()); | 139 const std::string event_name(rule_registry->event_name()); |
| 138 RulesRegistryKey key(event_name, rule_registry->webview_key()); | 140 RulesRegistryKey key(event_name, rule_registry->rules_registry_id()); |
|
Fady Samuel
2014/11/26 23:39:40
This seems really redundant. How about just callin
Xi Han
2014/11/27 23:25:25
Done.
| |
| 139 DCHECK(rule_registries_.find(key) == rule_registries_.end()); | 141 DCHECK(rule_registries_.find(key) == rule_registries_.end()); |
| 140 rule_registries_[key] = rule_registry; | 142 rule_registries_[key] = rule_registry; |
| 141 } | 143 } |
| 142 | 144 |
| 143 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry( | 145 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry( |
| 144 const WebViewKey& webview_key, | 146 int rules_registry_id, |
| 147 bool is_web_view, | |
|
Fady Samuel
2014/11/26 23:39:41
bool cache_rules
Xi Han
2014/11/27 23:25:25
Done.
| |
| 145 const std::string& event_name) { | 148 const std::string& event_name) { |
| 146 EnsureDefaultRulesRegistriesRegistered(webview_key); | 149 EnsureDefaultRulesRegistriesRegistered(rules_registry_id, is_web_view); |
| 147 | 150 |
| 148 RulesRegistryKey key(event_name, webview_key); | 151 RulesRegistryKey key(event_name, rules_registry_id); |
| 149 RulesRegistryMap::const_iterator i = rule_registries_.find(key); | 152 RulesRegistryMap::const_iterator i = rule_registries_.find(key); |
| 150 if (i == rule_registries_.end()) | 153 if (i == rule_registries_.end()) |
| 151 return scoped_refptr<RulesRegistry>(); | 154 return scoped_refptr<RulesRegistry>(); |
| 152 return i->second; | 155 return i->second; |
| 153 } | 156 } |
| 154 | 157 |
| 155 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { | 158 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { |
|
Fady Samuel
2014/11/26 23:39:41
Can we move this to WebViewGuest?
Xi Han
2014/11/27 23:25:24
Moved.
| |
| 156 DCHECK_NE(0, process_id); | 159 DCHECK_NE(0, process_id); |
| 157 | 160 |
| 158 std::set<RulesRegistryKey> registries_to_delete; | 161 std::set<RulesRegistryKey> registries_to_delete; |
| 159 for (RulesRegistryMap::iterator it = rule_registries_.begin(); | 162 for (RulesRegistryMap::iterator it = rule_registries_.begin(); |
| 160 it != rule_registries_.end(); ++it) { | 163 it != rule_registries_.end(); ++it) { |
| 161 const RulesRegistryKey& key = it->first; | 164 const RulesRegistryKey& key = it->first; |
| 162 const WebViewKey& webview_key = key.webview_key; | 165 int rules_registry_id = key.rules_registry_id; |
| 163 int embedder_process_id = webview_key.embedder_process_id; | 166 int embedder_process_id = |
| 167 WebViewGuest::GetEmbedderProcessID(rules_registry_id); | |
| 164 // |process_id| will always be non-zero. | 168 // |process_id| will always be non-zero. |
| 165 // |embedder_process_id| will only be non-zero if the key corresponds to a | 169 // |embedder_process_id| will only be non-zero if the key corresponds to a |
| 166 // webview registry. | 170 // webview registry. |
| 167 // Thus, |embedder_process_id| == |process_id| ==> the process ID is a | 171 // Thus, |embedder_process_id| == |process_id| ==> the process ID is a |
| 168 // webview embedder. | 172 // webview embedder. |
| 169 if (embedder_process_id != process_id) | 173 if (embedder_process_id != process_id) |
| 170 continue; | 174 continue; |
| 171 | 175 |
| 172 // Modifying the container while iterating is bad so we'll save the keys we | 176 // 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. | 177 // wish to delete in another container, and delete them in another loop. |
| 174 registries_to_delete.insert(key); | 178 registries_to_delete.insert(key); |
| 175 } | 179 } |
| 176 for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin(); | 180 for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin(); |
| 177 it != registries_to_delete.end(); ++it) { | 181 it != registries_to_delete.end(); ++it) { |
| 182 WebViewGuest::RemoveRulesRegistryID(it->rules_registry_id); | |
| 178 rule_registries_.erase(*it); | 183 rule_registries_.erase(*it); |
| 179 } | 184 } |
| 180 } | 185 } |
| 181 | 186 |
| 182 void RulesRegistryService::SimulateExtensionUninstalled( | 187 void RulesRegistryService::SimulateExtensionUninstalled( |
| 183 const std::string& extension_id) { | 188 const std::string& extension_id) { |
| 184 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id); | 189 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id); |
| 185 } | 190 } |
| 186 | 191 |
| 187 void RulesRegistryService::NotifyRegistriesHelper( | 192 void RulesRegistryService::NotifyRegistriesHelper( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 const content::NotificationSource& source, | 232 const content::NotificationSource& source, |
| 228 const content::NotificationDetails& details) { | 233 const content::NotificationDetails& details) { |
| 229 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); | 234 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); |
| 230 | 235 |
| 231 content::RenderProcessHost* process = | 236 content::RenderProcessHost* process = |
| 232 content::Source<content::RenderProcessHost>(source).ptr(); | 237 content::Source<content::RenderProcessHost>(source).ptr(); |
| 233 RemoveWebViewRulesRegistries(process->GetID()); | 238 RemoveWebViewRulesRegistries(process->GetID()); |
| 234 } | 239 } |
| 235 | 240 |
| 236 } // namespace extensions | 241 } // namespace extensions |
| OLD | NEW |