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

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

Issue 764643002: Remove WebViewKey in rules registry. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renaming move "remove rules registry for webview" part to WebViewGuest. Created 6 years 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 "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"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/notification_types.h"
16 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
17 #include "extensions/browser/api/declarative/rules_cache_delegate.h" 13 #include "extensions/browser/api/declarative/rules_cache_delegate.h"
18 #include "extensions/browser/api/declarative_content/content_rules_registry.h" 14 #include "extensions/browser/api/declarative_content/content_rules_registry.h"
19 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" 15 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
20 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h" 16 #include "extensions/browser/api/declarative_webrequest/webrequest_rules_registr y.h"
21 #include "extensions/browser/api/extensions_api_client.h" 17 #include "extensions/browser/api/extensions_api_client.h"
22 #include "extensions/browser/api/web_request/web_request_api.h" 18 #include "extensions/browser/api/web_request/web_request_api.h"
23 #include "extensions/browser/extension_registry.h" 19 #include "extensions/browser/extension_registry.h"
24 #include "extensions/common/extension.h" 20 #include "extensions/common/extension.h"
25 21
26 namespace extensions { 22 namespace extensions {
27 23
28 namespace { 24 namespace {
29 25
30 // Registers |web_request_rules_registry| on the IO thread. 26 // Registers |web_request_rules_registry| on the IO thread.
31 void RegisterToExtensionWebRequestEventRouterOnIO( 27 void RegisterToExtensionWebRequestEventRouterOnIO(
32 content::BrowserContext* browser_context, 28 content::BrowserContext* browser_context,
33 const RulesRegistryService::WebViewKey& webview_key, 29 int rules_registry_id,
34 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) { 30 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry) {
35 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry( 31 ExtensionWebRequestEventRouter::GetInstance()->RegisterRulesRegistry(
36 browser_context, webview_key, web_request_rules_registry); 32 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 } 33 }
42 34
43 } // namespace 35 } // namespace
44 36
37 const int RulesRegistryService::kDefaultRulesRegistryID = 0;
38
45 RulesRegistryService::RulesRegistryService(content::BrowserContext* context) 39 RulesRegistryService::RulesRegistryService(content::BrowserContext* context)
46 : content_rules_registry_(NULL), 40 : current_rules_registry_id_(0),
Fady Samuel 2014/11/28 11:41:31 Use kDefaultRulesRegistryID
Xi Han 2014/11/28 16:12:56 Done.
41 content_rules_registry_(NULL),
47 extension_registry_observer_(this), 42 extension_registry_observer_(this),
48 browser_context_(context) { 43 browser_context_(context) {
49 if (browser_context_) { 44 if (browser_context_) {
50 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 45 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
51 registrar_.Add( 46 EnsureDefaultRulesRegistriesRegistered(kDefaultRulesRegistryID, false);
52 this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
53 content::NotificationService::AllBrowserContextsAndSources());
54 EnsureDefaultRulesRegistriesRegistered(WebViewKey(0, 0));
55 } 47 }
56 } 48 }
57 49
58 RulesRegistryService::~RulesRegistryService() {} 50 RulesRegistryService::~RulesRegistryService() {}
59 51
52 int RulesRegistryService::GetNextRulesRegistryID() {
53 return ++current_rules_registry_id_;
54 }
55
60 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered( 56 void RulesRegistryService::EnsureDefaultRulesRegistriesRegistered(
61 const WebViewKey& webview_key) { 57 int rules_registry_id,
58 bool cache_rules) {
Fady Samuel 2014/11/28 11:34:24 You know, now that I think about it, we can probab
Xi Han 2014/11/28 16:12:56 This is a great idea, thanks.
62 if (!browser_context_) 59 if (!browser_context_)
63 return; 60 return;
64
65 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest, 61 RulesRegistryKey key(declarative_webrequest_constants::kOnRequest,
66 webview_key); 62 rules_registry_id);
67 // If we can find the key in the |rule_registries_| then we have already 63 // If we can find the key in the |rule_registries_| then we have already
68 // installed the default registries. 64 // installed the default registries.
69 if (ContainsKey(rule_registries_, key)) 65 if (ContainsKey(rule_registries_, key))
70 return; 66 return;
71 67
72 68
73 RulesCacheDelegate* web_request_cache_delegate = NULL; 69 RulesCacheDelegate* web_request_cache_delegate = NULL;
74 if (!IsWebView(webview_key)) { 70 if (!cache_rules) {
Fady Samuel 2014/11/28 11:34:24 The logic here is reversed. if (cache_rules) then
Xi Han 2014/11/28 16:12:56 Thanks a lot for catching up this reversed logic.
75 web_request_cache_delegate = 71 web_request_cache_delegate =
76 new RulesCacheDelegate(true /*log_storage_init_delay*/); 72 new RulesCacheDelegate(true /*log_storage_init_delay*/);
77 cache_delegates_.push_back(web_request_cache_delegate); 73 cache_delegates_.push_back(web_request_cache_delegate);
78 } 74 }
79 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry( 75 scoped_refptr<WebRequestRulesRegistry> web_request_rules_registry(
80 new WebRequestRulesRegistry(browser_context_, 76 new WebRequestRulesRegistry(browser_context_, web_request_cache_delegate,
81 web_request_cache_delegate, 77 rules_registry_id));
82 webview_key));
83 78
84 RegisterRulesRegistry(web_request_rules_registry); 79 RegisterRulesRegistry(web_request_rules_registry);
85 content::BrowserThread::PostTask( 80 content::BrowserThread::PostTask(
86 content::BrowserThread::IO, FROM_HERE, 81 content::BrowserThread::IO, FROM_HERE,
87 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 82 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
88 browser_context_, webview_key, web_request_rules_registry)); 83 browser_context_, rules_registry_id,
84 web_request_rules_registry));
89 85
90 // Only create a ContentRulesRegistry for regular pages and not webviews. 86 // Only create a ContentRulesRegistry for regular pages and not webviews.
Fady Samuel 2014/11/28 11:34:24 Get rid of the reference to <webview>.
Xi Han 2014/11/28 16:12:56 Done.
91 if (!IsWebView(webview_key)) { 87 if (!cache_rules) {
Fady Samuel 2014/11/28 11:34:24 This is inverted. As above, simply do the followin
Xi Han 2014/11/28 16:12:56 Done.
92 RulesCacheDelegate* content_rules_cache_delegate = 88 RulesCacheDelegate* content_rules_cache_delegate =
93 new RulesCacheDelegate(false /*log_storage_init_delay*/); 89 new RulesCacheDelegate(false /*log_storage_init_delay*/);
94 cache_delegates_.push_back(content_rules_cache_delegate); 90 cache_delegates_.push_back(content_rules_cache_delegate);
95 scoped_refptr<ContentRulesRegistry> content_rules_registry = 91 scoped_refptr<ContentRulesRegistry> content_rules_registry =
96 ExtensionsAPIClient::Get()->CreateContentRulesRegistry( 92 ExtensionsAPIClient::Get()->CreateContentRulesRegistry(
97 browser_context_, content_rules_cache_delegate); 93 browser_context_, content_rules_cache_delegate);
98 if (content_rules_registry.get() != nullptr) { 94 if (content_rules_registry.get() != nullptr) {
99 RegisterRulesRegistry(content_rules_registry); 95 RegisterRulesRegistry(content_rules_registry);
100 content_rules_registry_ = content_rules_registry.get(); 96 content_rules_registry_ = content_rules_registry.get();
101 } 97 }
102 } 98 }
103 } 99 }
104 100
105 void RulesRegistryService::Shutdown() { 101 void RulesRegistryService::Shutdown() {
106 // Release the references to all registries. This would happen soon during 102 // Release the references to all registries. This would happen soon during
107 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to 103 // destruction of |*this|, but we need the ExtensionWebRequestEventRouter to
108 // be the last to reference the WebRequestRulesRegistry objects, so that 104 // 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 105 // the posted task below causes their destruction on the IO thread, not on UI
110 // where the destruction of |*this| takes place. 106 // where the destruction of |*this| takes place.
111 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed. 107 // TODO(vabr): Remove once http://crbug.com/218451#c6 gets addressed.
112 rule_registries_.clear(); 108 rule_registries_.clear();
113 content::BrowserThread::PostTask( 109 content::BrowserThread::PostTask(
114 content::BrowserThread::IO, FROM_HERE, 110 content::BrowserThread::IO, FROM_HERE,
115 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO, 111 base::Bind(&RegisterToExtensionWebRequestEventRouterOnIO,
116 browser_context_, WebViewKey(0, 0), 112 browser_context_,
117 scoped_refptr<WebRequestRulesRegistry>(NULL))); 113 RulesRegistryService::kDefaultRulesRegistryID,
114 scoped_refptr<WebRequestRulesRegistry>(NULL)));
118 } 115 }
119 116
120 static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> > 117 static base::LazyInstance<BrowserContextKeyedAPIFactory<RulesRegistryService> >
121 g_factory = LAZY_INSTANCE_INITIALIZER; 118 g_factory = LAZY_INSTANCE_INITIALIZER;
122 119
123 // static 120 // static
124 BrowserContextKeyedAPIFactory<RulesRegistryService>* 121 BrowserContextKeyedAPIFactory<RulesRegistryService>*
125 RulesRegistryService::GetFactoryInstance() { 122 RulesRegistryService::GetFactoryInstance() {
126 return g_factory.Pointer(); 123 return g_factory.Pointer();
127 } 124 }
128 125
129 // static 126 // static
130 RulesRegistryService* RulesRegistryService::Get( 127 RulesRegistryService* RulesRegistryService::Get(
131 content::BrowserContext* context) { 128 content::BrowserContext* context) {
132 return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context); 129 return BrowserContextKeyedAPIFactory<RulesRegistryService>::Get(context);
133 } 130 }
134 131
135 void RulesRegistryService::RegisterRulesRegistry( 132 void RulesRegistryService::RegisterRulesRegistry(
136 scoped_refptr<RulesRegistry> rule_registry) { 133 scoped_refptr<RulesRegistry> rule_registry) {
137 const std::string event_name(rule_registry->event_name()); 134 const std::string event_name(rule_registry->event_name());
138 RulesRegistryKey key(event_name, rule_registry->webview_key()); 135 RulesRegistryKey key(event_name, rule_registry->id());
139 DCHECK(rule_registries_.find(key) == rule_registries_.end()); 136 DCHECK(rule_registries_.find(key) == rule_registries_.end());
140 rule_registries_[key] = rule_registry; 137 rule_registries_[key] = rule_registry;
141 } 138 }
142 139
143 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry( 140 scoped_refptr<RulesRegistry> RulesRegistryService::GetRulesRegistry(
144 const WebViewKey& webview_key, 141 int rules_registry_id,
142 bool cache_rules,
Fady Samuel 2014/11/28 11:34:24 We can probably get rid of this.
Xi Han 2014/11/28 16:12:56 Done.
145 const std::string& event_name) { 143 const std::string& event_name) {
146 EnsureDefaultRulesRegistriesRegistered(webview_key); 144 EnsureDefaultRulesRegistriesRegistered(rules_registry_id, cache_rules);
147 145
148 RulesRegistryKey key(event_name, webview_key); 146 RulesRegistryKey key(event_name, rules_registry_id);
149 RulesRegistryMap::const_iterator i = rule_registries_.find(key); 147 RulesRegistryMap::const_iterator i = rule_registries_.find(key);
150 if (i == rule_registries_.end()) 148 if (i == rule_registries_.end())
151 return scoped_refptr<RulesRegistry>(); 149 return scoped_refptr<RulesRegistry>();
152 return i->second; 150 return i->second;
153 } 151 }
154 152
155 void RulesRegistryService::RemoveWebViewRulesRegistries(int process_id) { 153 void RulesRegistryService::RemoveRulesRegistryID(int rules_registry_id) {
156 DCHECK_NE(0, process_id);
157
158 std::set<RulesRegistryKey> registries_to_delete;
159 for (RulesRegistryMap::iterator it = rule_registries_.begin(); 154 for (RulesRegistryMap::iterator it = rule_registries_.begin();
160 it != rule_registries_.end(); ++it) { 155 it != rule_registries_.end(); ++it) {
161 const RulesRegistryKey& key = it->first; 156 if (it->first.rules_registry_id != rules_registry_id)
162 const WebViewKey& webview_key = key.webview_key;
163 int embedder_process_id = webview_key.embedder_process_id;
164 // |process_id| will always be non-zero.
165 // |embedder_process_id| will only be non-zero if the key corresponds to a
166 // webview registry.
167 // Thus, |embedder_process_id| == |process_id| ==> the process ID is a
168 // webview embedder.
169 if (embedder_process_id != process_id)
170 continue; 157 continue;
171 158 rule_registries_.erase(it);
Fady Samuel 2014/11/28 11:41:31 Modifying a container while iterating over it can
Xi Han 2014/11/28 16:12:56 I just realize that we might need to delete multip
172 // Modifying the container while iterating is bad so we'll save the keys we 159 return;
173 // wish to delete in another container, and delete them in another loop.
174 registries_to_delete.insert(key);
175 }
176 for (std::set<RulesRegistryKey>::iterator it = registries_to_delete.begin();
177 it != registries_to_delete.end(); ++it) {
178 rule_registries_.erase(*it);
179 } 160 }
180 } 161 }
181 162
182 void RulesRegistryService::SimulateExtensionUninstalled( 163 void RulesRegistryService::SimulateExtensionUninstalled(
183 const std::string& extension_id) { 164 const std::string& extension_id) {
184 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id); 165 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, extension_id);
185 } 166 }
186 167
187 void RulesRegistryService::NotifyRegistriesHelper( 168 void RulesRegistryService::NotifyRegistriesHelper(
188 void (RulesRegistry::*notification_callback)(const std::string&), 169 void (RulesRegistry::*notification_callback)(const std::string&),
(...skipping 26 matching lines...) Expand all
215 } 196 }
216 197
217 void RulesRegistryService::OnExtensionUninstalled( 198 void RulesRegistryService::OnExtensionUninstalled(
218 content::BrowserContext* browser_context, 199 content::BrowserContext* browser_context,
219 const Extension* extension, 200 const Extension* extension,
220 extensions::UninstallReason reason) { 201 extensions::UninstallReason reason) {
221 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled, 202 NotifyRegistriesHelper(&RulesRegistry::OnExtensionUninstalled,
222 extension->id()); 203 extension->id());
223 } 204 }
224 205
225 void RulesRegistryService::Observe(
226 int type,
227 const content::NotificationSource& source,
228 const content::NotificationDetails& details) {
229 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, type);
230
231 content::RenderProcessHost* process =
232 content::Source<content::RenderProcessHost>(source).ptr();
233 RemoveWebViewRulesRegistries(process->GetID());
234 }
235
236 } // namespace extensions 206 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698