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 "chrome/browser/extensions/api/declarative/rules_registry_service.h" | 5 #include "chrome/browser/extensions/api/declarative/rules_registry_service.h" |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" | 14 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" |
| 15 #include "chrome/browser/extensions/api/declarative_content/content_constants.h" | |
| 13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" | 16 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" |
| 14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | 18 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" |
| 16 #include "chrome/browser/extensions/api/web_request/web_request_api.h" | 19 #include "chrome/browser/extensions/api/web_request/web_request_api.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 18 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
| 23 #include "extensions/browser/extension_registry.h" | 26 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
| 28 #include "extensions/common/watched_pages_recipient.h" | |
| 25 | 29 |
| 26 namespace extensions { | 30 namespace extensions { |
| 27 | 31 |
| 28 namespace { | 32 namespace { |
| 29 | 33 |
| 30 // Registers |web_request_rules_registry| on the IO thread. | 34 // Registers |web_request_rules_registry| on the IO thread. |
| 31 void RegisterToExtensionWebRequestEventRouterOnIO( | 35 void RegisterToExtensionWebRequestEventRouterOnIO( |
| 32 void* profile, | 36 void* profile, |
| 33 const RulesRegistryService::WebViewKey& webview_key, | 37 const RulesRegistryService::WebViewKey& webview_key, |
| 34 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { | 38 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { |
| 35 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( | 39 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( |
| 36 profile, webview_key, web_request_rules_registry); | 40 profile, webview_key, web_request_rules_registry); |
| 37 } | 41 } |
| 38 | 42 |
| 39 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) { | 43 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) { |
| 40 return webview_key.embedder_process_id && webview_key.webview_instance_id; | 44 return webview_key.embedder_process_id && webview_key.webview_instance_id; |
| 41 } | 45 } |
| 42 | 46 |
| 43 } // namespace | 47 } // namespace |
| 44 | 48 |
| 45 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) | 49 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) |
| 46 : content_rules_registry_(NULL), | 50 : extension_registry_observer_(this), |
| 47 extension_registry_observer_(this), | |
| 48 profile_(Profile::FromBrowserContext(context)) { | 51 profile_(Profile::FromBrowserContext(context)) { |
| 49 if (profile_) { | 52 if (profile_) { |
| 50 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | 53 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 51 registrar_.Add( | 54 registrar_.Add( |
| 52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 55 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 53 content::NotificationService::AllBrowserContextsAndSources()); | 56 content::NotificationService::AllBrowserContextsAndSources()); |
| 54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0)); | 57 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0)); |
| 55 } | 58 } |
| 56 } | 59 } |
| 57 | 60 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 82 webview_key)); | 85 webview_key)); |
| 83 | 86 |
| 84 RegisterRulesRegistry(web_request_rules_registry); | 87 RegisterRulesRegistry(web_request_rules_registry); |
| 85 content::BrowserThread::PostTask( | 88 content::BrowserThread::PostTask( |
| 86 content::BrowserThread::IO, FROM_HERE, | 89 content::BrowserThread::IO, FROM_HERE, |
| 87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, | 90 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, |
| 88 profile_, webview_key, web_request_rules_registry)); | 91 profile_, webview_key, web_request_rules_registry)); |
| 89 | 92 |
| 90 // Only create a ContentRulesRegistry for regular pages and not webviews. | 93 // Only create a ContentRulesRegistry for regular pages and not webviews. |
| 91 if (!IsWebView(webview_key)) { | 94 if (!IsWebView(webview_key)) { |
| 92 RulesCacheDelegate* content_rules_cache_delegate = | 95 namespace dcc = declarative_content_constants; |
| 93 new RulesCacheDelegate(false /*log_storage_init_delay*/); | 96 typedef std::vector< std::pair<std::string, WatchedPagesRecipient> > |
|
Jeffrey Yasskin
2014/06/27 22:03:41
For something with a constant set of elements, I'd
Mark Dittmer
2014/06/30 12:34:02
Done.
| |
| 94 cache_delegates_.push_back(content_rules_cache_delegate); | 97 ContentRegistryIDList; |
| 95 scoped_refptr<ContentRulesRegistry> content_rules_registry( | 98 ContentRegistryIDList content_registry_ids; |
| 96 new ContentRulesRegistry(profile_, content_rules_cache_delegate)); | 99 |
| 97 RegisterRulesRegistry(content_rules_registry); | 100 // onPageChanged supports browser-side ShowPageAction. Browser-side rules |
| 98 content_rules_registry_ = content_rules_registry.get(); | 101 // registry must be notified. |
| 102 content_registry_ids.push_back( | |
| 103 std::make_pair(dcc::kOnPageChanged, CONTENT_RULES_REGISTRY)); | |
| 104 | |
| 105 // onDocument- Start/End/Idle supports renderer-side RequestContentScript. | |
| 106 // Renderer-side user script manager must be notified. | |
| 107 content_registry_ids.push_back( | |
| 108 std::make_pair(dcc::kOnDocumentStart, USER_SCRIPT_MANAGER)); | |
| 109 content_registry_ids.push_back( | |
| 110 std::make_pair(dcc::kOnDocumentEnd, USER_SCRIPT_MANAGER)); | |
| 111 content_registry_ids.push_back( | |
| 112 std::make_pair(dcc::kOnDocumentIdle, USER_SCRIPT_MANAGER)); | |
| 113 | |
| 114 for (ContentRegistryIDList::const_iterator it = | |
| 115 content_registry_ids.begin(); | |
| 116 it != content_registry_ids.end(); ++it) { | |
| 117 RulesCacheDelegate* content_rules_cache_delegate = | |
| 118 new RulesCacheDelegate(false /*log_storage_init_delay*/); | |
| 119 cache_delegates_.push_back(content_rules_cache_delegate); | |
| 120 scoped_refptr<ContentRulesRegistry> content_rules_registry( | |
| 121 new ContentRulesRegistry(profile_, | |
| 122 it->first, // event_name. | |
| 123 it->second, // watched_pages_recipient. | |
| 124 content_rules_cache_delegate)); | |
| 125 RegisterRulesRegistry(content_rules_registry); | |
| 126 content_rules_registry_map_.insert( | |
| 127 std::make_pair(it->first, // event_name. | |
| 128 content_rules_registry.get())); | |
|
Jeffrey Yasskin
2014/06/27 22:03:41
This is going to use the registry after it's been
Mark Dittmer
2014/06/30 11:50:35
How is this different from how the code was before
Jeffrey Yasskin
2014/07/15 17:12:58
Whoops, you're right. Never mind.
| |
| 129 } | |
| 99 } | 130 } |
| 100 } | 131 } |
| 101 | 132 |
| 102 void RulesRegistryService::Shutdown() { | 133 void RulesRegistryService::Shutdown() { |
| 103 // Release the references to all registries. This would happen soon during | 134 // Release the references to all registries. This would happen soon during |
| 104 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to | 135 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to |
| 105 // be the last to reference the WebRequestRulesRegistry objects, so that | 136 // 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 | 137 // the posted task below causes their destruction on the IO thread, not on UI |
| 107 // where the destruction of |*this| takes place. | 138 // where the destruction of |*this| takes place. |
| 108 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. | 139 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 const std::string& event_name) { | 173 const std::string& event_name) { |
| 143 EnsureDefaultRulesRegistriesRegistered(webview_key); | 174 EnsureDefaultRulesRegistriesRegistered(webview_key); |
| 144 | 175 |
| 145 RulesRegistryKey key(event_name, webview_key); | 176 RulesRegistryKey key(event_name, webview_key); |
| 146 RulesRegistryMap::const_iterator i = rule_registries_.find(key); | 177 RulesRegistryMap::const_iterator i = rule_registries_.find(key); |
| 147 if (i == rule_registries_.end()) | 178 if (i == rule_registries_.end()) |
| 148 return scoped_refptr<RulesRegistry>(); | 179 return scoped_refptr<RulesRegistry>(); |
| 149 return i->second; | 180 return i->second; |
| 150 } | 181 } |
| 151 | 182 |
| 183 ContentRulesRegistry* RulesRegistryService::GetContentRulesRegistry( | |
| 184 const std::string& event_name) { | |
| 185 ContentRulesRegistryMap::iterator it = | |
| 186 content_rules_registry_map_.find(event_name); | |
| 187 if (it == content_rules_registry_map_.end()) | |
| 188 return NULL; | |
| 189 | |
| 190 return content_rules_registry_map_[event_name]; | |
| 191 } | |
| 192 | |
| 193 | |
| 152 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { | 194 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { |
| 153 DCHECK_NE(0, process_id); | 195 DCHECK_NE(0, process_id); |
| 154 | 196 |
| 155 std::set<RulesRegistryKey> registries_to_delete; | 197 std::set<RulesRegistryKey> registries_to_delete; |
| 156 for (RulesRegistryMap::iterator it = rule_registries_.begin(); | 198 for (RulesRegistryMap::iterator it = rule_registries_.begin(); |
| 157 it != rule_registries_.end(); ++it) { | 199 it != rule_registries_.end(); ++it) { |
| 158 const RulesRegistryKey& key = it->first; | 200 const RulesRegistryKey& key = it->first; |
| 159 const WebViewKey& webview_key = key.webview_key; | 201 const WebViewKey& webview_key = key.webview_key; |
| 160 int embedder_process_id = webview_key.embedder_process_id; | 202 int embedder_process_id = webview_key.embedder_process_id; |
| 161 // |process_id| will always be non-zero. | 203 // |process_id| will always be non-zero. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 const content::NotificationSource& source, | 265 const content::NotificationSource& source, |
| 224 const content::NotificationDetails& details) { | 266 const content::NotificationDetails& details) { |
| 225 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); | 267 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type); |
| 226 | 268 |
| 227 content::RenderProcessHost* process = | 269 content::RenderProcessHost* process = |
| 228 content::Source<content::RenderProcessHost>(source).ptr(); | 270 content::Source<content::RenderProcessHost>(source).ptr(); |
| 229 RemoveWebViewRulesRegistries(process->GetID()); | 271 RemoveWebViewRulesRegistries(process->GetID()); |
| 230 } | 272 } |
| 231 | 273 |
| 232 } // namespace extensions | 274 } // namespace extensions |
| OLD | NEW |