Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: chrome/browser/extensions/api/declarative/rules_registry_service.cc

Issue 28273006: <webview>: Implement declarativeWebRequest API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed nits Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "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 "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h" 12 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h" 13 #include "chrome/browser/extensions/api/declarative_content/content_rules_regist ry.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
15 #include "chrome/browser/extensions/api/web_request/web_request_api.h" 16 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
16 #include "chrome/common/extensions/extension.h" 17 #include "chrome/common/extensions/extension.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h" 19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_source.h" 21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/render_process_host.h"
20 23
21 namespace extensions { 24 namespace extensions {
22 25
23 namespace { 26 namespace {
24 27
25 // Registers |web_request_rules_registry| on the IO thread. 28 // Registers |web_request_rules_registry| on the IO thread.
26 void RegisterToExtensionWebRequestEventRouterOnIO( 29 void RegisterToExtensionWebRequestEventRouterOnIO(
27 void* profile, 30 void* profile,
31 const RulesRegistryService::WebViewKey& webview_key,
28 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { 32 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
29 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( 33 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
30 profile, web_request_rules_registry); 34 profile, webview_key, web_request_rules_registry);
35 }
36
37 bool IsWebView(const RulesRegistryService::WebViewKey& webview_key) {
38 return webview_key.embedder_process_id && webview_key.webview_instance_id;
31 } 39 }
32 40
33 } // namespace 41 } // namespace
34 42
35 RulesRegistryService::RulesRegistryService(Profile* profile) 43 RulesRegistryService::RulesRegistryService(Profile* profile)
36 : content_rules_registry_(NULL), 44 : content_rules_registry_(NULL),
37 profile_(profile) { 45 profile_(profile) {
38 if (profile) { 46 if (profile) {
39 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 47 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
40 content::Source<Profile>(profile->GetOriginalProfile())); 48 content::Source<Profile>(profile->GetOriginalProfile()));
41 RegisterDefaultRulesRegistries(); 49 registrar_.Add(
50 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
51 content::NotificationService::AllBrowserContextsAndSources());
52 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
42 } 53 }
43 } 54 }
44 55
45 RulesRegistryService::~RulesRegistryService() {} 56 RulesRegistryService::~RulesRegistryService() {}
46 57
47 void RulesRegistryService::RegisterDefaultRulesRegistries() { 58 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
48 scoped_ptr<RulesCacheDelegate> web_request_cache_delegate( 59 const WebViewKey& webview_key) {
49 new RulesCacheDelegate(true /*log_storage_init_delay*/)); 60 if (!profile_)
61 return;
62
63 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest,
64 webview_key);
65 // If we can find the key in the |rule_registries_| then we have already
66 // installed the default registries.
67 if (ContainsKey(rule_registries_, key))
68 return;
69
70
71 RulesCacheDelegate* web_request_cache_delegate = NULL;
72 if (!IsWebView(webview_key)) {
73 web_request_cache_delegate =
74 new RulesCacheDelegate(true /*log_storage_init_delay*/);
75 cache_delegates_.push_back(web_request_cache_delegate);
76 }
50 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( 77 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry(
51 new WebRequestRulesRegistry(profile_, web_request_cache_delegate.get())); 78 new WebRequestRulesRegistry(profile_,
52 cache_delegates_.push_back(web_request_cache_delegate.release()); 79 web_request_cache_delegate,
80 webview_key));
53 81
54 RegisterRulesRegistry(web_request_rules_registry); 82 RegisterRulesRegistry(web_request_rules_registry);
55 content::BrowserThread::PostTask( 83 content::BrowserThread::PostTask(
56 content::BrowserThread::IO, FROM_HERE, 84 content::BrowserThread::IO, FROM_HERE,
57 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 85 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
58 profile_, web_request_rules_registry)); 86 profile_, webview_key, web_request_rules_registry));
59 87
60 #if defined(ENABLE_EXTENSIONS) 88 #if defined(ENABLE_EXTENSIONS)
61 scoped_ptr<RulesCacheDelegate> content_rules_cache_delegate( 89 // Only create a ContentRulesRegistry for regular pages and not webviews.
62 new RulesCacheDelegate(false /*log_storage_init_delay*/)); 90 if (!IsWebView(webview_key)) {
63 scoped_refptr<ContentRulesRegistry> content_rules_registry( 91 RulesCacheDelegate* content_rules_cache_delegate =
64 new ContentRulesRegistry(profile_, content_rules_cache_delegate.get())); 92 new RulesCacheDelegate(false /*log_storage_init_delay*/);
65 cache_delegates_.push_back(content_rules_cache_delegate.release()); 93 cache_delegates_.push_back(content_rules_cache_delegate);
66 94 scoped_refptr<ContentRulesRegistry> content_rules_registry(
67 RegisterRulesRegistry(content_rules_registry); 95 new ContentRulesRegistry(profile_, content_rules_cache_delegate));
68 content_rules_registry_ = content_rules_registry.get(); 96 RegisterRulesRegistry(content_rules_registry);
97 content_rules_registry_ = content_rules_registry.get();
98 }
69 #endif // defined(ENABLE_EXTENSIONS) 99 #endif // defined(ENABLE_EXTENSIONS)
70 } 100 }
71 101
72 void RulesRegistryService::Shutdown() { 102 void RulesRegistryService::Shutdown() {
73 // Release the references to all registries. This would happen soon during 103 // Release the references to all registries. This would happen soon during
74 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to 104 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
75 // be the last to reference the WebRequestRulesRegistry objects, so that 105 // be the last to reference the WebRequestRulesRegistry objects, so that
76 // the posted task below causes their destruction on the IO thread, not on UI 106 // the posted task below causes their destruction on the IO thread, not on UI
77 // where the destruction of |*this| takes place. 107 // where the destruction of |*this| takes place.
78 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. 108 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
79 rule_registries_.clear(); 109 rule_registries_.clear();
80 content::BrowserThread::PostTask( 110 content::BrowserThread::PostTask(
81 content::BrowserThread::IO, FROM_HERE, 111 content::BrowserThread::IO, FROM_HERE,
82 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 112 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
83 profile_, scoped_refptr<WebRequestRulesRegistry>(NULL))); 113 profile_, WebViewKey(0, 0),
114 scoped_refptr<WebRequestRulesRegistry>(NULL)));
84 } 115 }
85 116
86 static base::LazyInstance<ProfileKeyedAPIFactory<RulesRegistryService> > 117 static base::LazyInstance<ProfileKeyedAPIFactory<RulesRegistryService> >
87 g_factory = LAZY_INSTANCE_INITIALIZER; 118 g_factory = LAZY_INSTANCE_INITIALIZER;
88 119
89 // static 120 // static
90 ProfileKeyedAPIFactory<RulesRegistryService>* 121 ProfileKeyedAPIFactory<RulesRegistryService>*
91 RulesRegistryService::GetFactoryInstance() { 122 RulesRegistryService::GetFactoryInstance() {
92 return &g_factory.Get(); 123 return &g_factory.Get();
93 } 124 }
94 125
95 // static 126 // static
96 RulesRegistryService* RulesRegistryService::Get(Profile* profile) { 127 RulesRegistryService* RulesRegistryService::Get(Profile* profile) {
97 return ProfileKeyedAPIFactory<RulesRegistryService>::GetForProfile(profile); 128 return ProfileKeyedAPIFactory<RulesRegistryService>::GetForProfile(profile);
98 } 129 }
99 130
100 void RulesRegistryService::RegisterRulesRegistry( 131 void RulesRegistryService::RegisterRulesRegistry(
101 scoped_refptr<RulesRegistry> rule_registry) { 132 scoped_refptr<RulesRegistry> rule_registry) {
102 const std::string event_name(rule_registry->event_name()); 133 const std::string event_name(rule_registry->event_name());
103 DCHECK(rule_registries_.find(event_name) == rule_registries_.end()); 134 RulesRegistryKey key(event_name, rule_registry->webview_key());
104 rule_registries_[event_name] = rule_registry; 135 DCHECK(rule_registries_.find(key) == rule_registries_.end());
136 rule_registries_[key] = rule_registry;
105 } 137 }
106 138
107 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry( 139 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry(
108 const std::string& event_name) const { 140 const WebViewKey& webview_key,
109 RulesRegistryMap::const_iterator i = rule_registries_.find(event_name); 141 const std::string& event_name) {
142 EnsureDefaultRulesRegistriesRegistered(webview_key);
143
144 RulesRegistryKey key(event_name, webview_key);
145 RulesRegistryMap::const_iterator i = rule_registries_.find(key);
110 if (i == rule_registries_.end()) 146 if (i == rule_registries_.end())
111 return scoped_refptr<RulesRegistry>(); 147 return scoped_refptr<RulesRegistry>();
112 return i->second; 148 return i->second;
113 } 149 }
114 150
151 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) {
152 DCHECK_NE(0, process_id);
153
154 std::set<RulesRegistryKey> registries_to_delete;
155 for (RulesRegistryMap::iterator it = rule_registries_.begin();
156 it != rule_registries_.end(); ++it) {
157 const RulesRegistryKey& key = it->first;
158 const WebViewKey& webview_key = key.webview_key;
159 int embedder_process_id = webview_key.embedder_process_id;
160 // |process_id| will always be non-zero.
161 // |embedder_process_id| will only be non-zero if the key corresponds to a
162 // webview registry.
163 // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
164 // webview embedder.
165 if (embedder_process_id != process_id)
166 continue;
167
168 // Modifying the container while iterating is bad so we'll save the keys we
169 // wish to delete in another container, and delete them in another loop.
170 registries_to_delete.insert(key);
171 }
172 for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin();
173 it != registries_to_delete.end(); ++it) {
174 rule_registries_.erase(*it);
175 }
176 }
177
115 void RulesRegistryService::SimulateExtensionUnloaded( 178 void RulesRegistryService::SimulateExtensionUnloaded(
116 const std::string& extension_id) { 179 const std::string& extension_id) {
117 OnExtensionUnloaded(extension_id); 180 OnExtensionUnloaded(extension_id);
118 } 181 }
119 182
120 void RulesRegistryService::OnExtensionUnloaded( 183 void RulesRegistryService::OnExtensionUnloaded(
121 const std::string& extension_id) { 184 const std::string& extension_id) {
122 RulesRegistryMap::iterator i; 185 RulesRegistryMap::iterator i;
123 for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) { 186 for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) {
124 scoped_refptr<RulesRegistry> registry = i->second; 187 scoped_refptr<RulesRegistry> registry = i->second;
(...skipping 13 matching lines...) Expand all
138 int type, 201 int type,
139 const content::NotificationSource& source, 202 const content::NotificationSource& source,
140 const content::NotificationDetails& details) { 203 const content::NotificationDetails& details) {
141 switch (type) { 204 switch (type) {
142 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 205 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
143 const Extension* extension = 206 const Extension* extension =
144 content::Details<UnloadedExtensionInfo>(details)->extension; 207 content::Details<UnloadedExtensionInfo>(details)->extension;
145 OnExtensionUnloaded(extension->id()); 208 OnExtensionUnloaded(extension->id());
146 break; 209 break;
147 } 210 }
211 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: {
212 content::RenderProcessHost* process =
213 content::Source<content::RenderProcessHost>(source).ptr();
214 RemoveWebViewRulesRegistries(process->GetID());
215 break;
216 }
148 default: 217 default:
149 NOTREACHED(); 218 NOTREACHED();
150 break; 219 break;
151 } 220 }
152 } 221 }
153 222
154 } // namespace extensions 223 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698