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

Side by Side Diff: extensions/browser/renderer_startup_helper.cc

Issue 2766063003: Extensions: Keep track of loaded extensions in RendererStartupHelper. (Closed)
Patch Set: -- Created 3 years, 8 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/renderer_startup_helper.h" 5 #include "extensions/browser/renderer_startup_helper.h"
6 6
7 #include "base/stl_util.h"
7 #include "base/values.h" 8 #include "base/values.h"
8 #include "components/keyed_service/content/browser_context_dependency_manager.h" 9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
9 #include "content/public/browser/notification_service.h" 10 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_types.h" 11 #include "content/public/browser/notification_types.h"
11 #include "content/public/browser/render_process_host.h" 12 #include "content/public/browser/render_process_host.h"
12 #include "extensions/browser/extension_function_dispatcher.h" 13 #include "extensions/browser/extension_function_dispatcher.h"
13 #include "extensions/browser/extension_registry.h" 14 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extensions_browser_client.h" 15 #include "extensions/browser/extensions_browser_client.h"
15 #include "extensions/browser/guest_view/web_view/web_view_guest.h" 16 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
16 #include "extensions/common/extension_messages.h" 17 #include "extensions/common/extension_messages.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 extensions::ExtensionsClient::Get()->GetScriptingWhitelist())); 85 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
85 86
86 // If the new render process is a WebView guest process, propagate the WebView 87 // If the new render process is a WebView guest process, propagate the WebView
87 // partition ID to it. 88 // partition ID to it.
88 std::string webview_partition_id = WebViewGuest::GetPartitionID(process); 89 std::string webview_partition_id = WebViewGuest::GetPartitionID(process);
89 if (!webview_partition_id.empty()) { 90 if (!webview_partition_id.empty()) {
90 process->Send(new ExtensionMsg_SetWebViewPartitionID( 91 process->Send(new ExtensionMsg_SetWebViewPartitionID(
91 WebViewGuest::GetPartitionID(process))); 92 WebViewGuest::GetPartitionID(process)));
92 } 93 }
93 94
94 // Loaded extensions. 95 std::set<ExtensionId>* loaded_extensions = &loaded_extensions_[process];
Devlin 2017/03/27 14:47:57 optional nit: maybe just prefer a reference here?
Devlin 2017/03/27 14:47:58 Can we DCHECK(loaded_extensions.empty())?
karandeepb 2017/04/04 00:49:41 Not relevant anymore.
karandeepb 2017/04/04 00:49:42 Done.
95 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions; 96 // Load extensions.
97 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions_params;
96 const ExtensionSet& extensions = 98 const ExtensionSet& extensions =
97 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); 99 ExtensionRegistry::Get(browser_context_)->enabled_extensions();
98 for (const auto& ext : extensions) { 100 for (const auto& ext : extensions) {
99 // Renderers don't need to know about themes. 101 // Renderers don't need to know about themes.
100 if (!ext->is_theme()) { 102 if (!ext->is_theme()) {
101 // TODO(kalman): Only include tab specific permissions for extension 103 // TODO(kalman): Only include tab specific permissions for extension
102 // processes, no other process needs it, so it's mildly wasteful. 104 // processes, no other process needs it, so it's mildly wasteful.
103 // I am not sure this is possible to know this here, at such a low 105 // I am not sure this is possible to know this here, at such a low
104 // level of the stack. Perhaps site isolation can help. 106 // level of the stack. Perhaps site isolation can help.
105 bool include_tab_permissions = true; 107 bool include_tab_permissions = true;
106 loaded_extensions.push_back( 108 loaded_extensions_params.push_back(
107 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); 109 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions));
110 loaded_extensions->insert(ext->id());
108 } 111 }
109 } 112 }
110 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); 113
114 // Activate pending extensions.
115 process->Send(new ExtensionMsg_Loaded(loaded_extensions_params));
111 auto iter = pending_active_extensions_.find(process); 116 auto iter = pending_active_extensions_.find(process);
112 if (iter != pending_active_extensions_.end()) { 117 if (iter != pending_active_extensions_.end()) {
113 for (const ExtensionId& id : iter->second) { 118 for (const ExtensionId& id : iter->second) {
114 DCHECK(extensions.Contains(id)); 119 DCHECK(extensions.Contains(id));
115 process->Send(new ExtensionMsg_ActivateExtension(id)); 120 process->Send(new ExtensionMsg_ActivateExtension(id));
116 } 121 }
117 } 122 }
118 123
119 initialized_processes_.insert(process); 124 pending_active_extensions_.erase(process);
120 } 125 }
121 126
122 void RendererStartupHelper::UntrackProcess( 127 void RendererStartupHelper::UntrackProcess(
123 content::RenderProcessHost* process) { 128 content::RenderProcessHost* process) {
124 if (!ExtensionsBrowserClient::Get()->IsSameContext( 129 if (!ExtensionsBrowserClient::Get()->IsSameContext(
125 browser_context_, process->GetBrowserContext())) { 130 browser_context_, process->GetBrowserContext())) {
126 return; 131 return;
127 } 132 }
128 133
129 initialized_processes_.erase(process); 134 loaded_extensions_.erase(process);
130 pending_active_extensions_.erase(process); 135 pending_active_extensions_.erase(process);
131 } 136 }
132 137
133 void RendererStartupHelper::ActivateExtensionInProcess( 138 void RendererStartupHelper::ActivateExtensionInProcess(
134 const Extension& extension, 139 const Extension& extension,
135 content::RenderProcessHost* process) { 140 content::RenderProcessHost* process) {
136 // Renderers don't need to know about themes. We also don't normally 141 // Renderers don't need to know about themes. We also don't normally
137 // "activate" themes, but this could happen if someone tries to open a tab 142 // "activate" themes, but this could happen if someone tries to open a tab
138 // to the e.g. theme's manifest. 143 // to the e.g. theme's manifest.
139 if (extension.is_theme()) 144 if (extension.is_theme())
140 return; 145 return;
141 146
142 if (initialized_processes_.count(process)) 147 const auto& process_extensions_pair = loaded_extensions_.find(process);
Devlin 2017/03/27 14:47:57 This is an iterator, so having const auto& isn't r
karandeepb 2017/04/04 00:49:42 Oh yeah, this takes reference to a temporary.
148 if (process_extensions_pair != loaded_extensions_.end()) {
149 // The extension should have already been loaded in the process.
150 if (!base::ContainsKey(process_extensions_pair->second, extension.id())) {
151 LOG(ERROR) << "Extension " << extension.id()
152 << " was not loaded for activation";
153 return;
154 }
143 process->Send(new ExtensionMsg_ActivateExtension(extension.id())); 155 process->Send(new ExtensionMsg_ActivateExtension(extension.id()));
Devlin 2017/03/27 14:47:57 Don't we need to add the extension to the set? if
karandeepb 2017/04/04 00:49:41 Not sure I understand. It is being activated. The
144 else 156 } else {
145 pending_active_extensions_[process].insert(extension.id()); 157 pending_active_extensions_[process].insert(extension.id());
158 }
146 } 159 }
147 160
148 void RendererStartupHelper::OnExtensionLoaded(const Extension& extension) { 161 void RendererStartupHelper::OnExtensionLoaded(const Extension& extension) {
149 // Renderers don't need to know about themes. 162 // Renderers don't need to know about themes.
150 if (extension.is_theme()) 163 if (extension.is_theme())
151 return; 164 return;
152 165
153 // We don't need to include tab permisisons here, since the extension 166 // We don't need to include tab permisisons here, since the extension
154 // was just loaded. 167 // was just loaded.
155 // Uninitialized renderers will be informed of the extension load during the 168 // Uninitialized renderers will be informed of the extension load during the
156 // first batch of messages. 169 // first batch of messages.
157 std::vector<ExtensionMsg_Loaded_Params> params( 170 std::vector<ExtensionMsg_Loaded_Params> params(
158 1, 171 1,
159 ExtensionMsg_Loaded_Params(&extension, false /* no tab permissions */)); 172 ExtensionMsg_Loaded_Params(&extension, false /* no tab permissions */));
160 for (content::RenderProcessHost* process : initialized_processes_) 173
174 for (auto& process_extensions_pair : loaded_extensions_) {
175 // If extension is already loaded in process, do nothing.
Devlin 2017/03/27 14:47:57 Hmm... can this happen, assuming OnExtensionLoaded
karandeepb 2017/04/04 00:49:42 For the incognito case I mentioned, yes.
176 if (base::ContainsKey(process_extensions_pair.second, extension.id()))
177 continue;
178
179 content::RenderProcessHost* process = process_extensions_pair.first;
161 process->Send(new ExtensionMsg_Loaded(params)); 180 process->Send(new ExtensionMsg_Loaded(params));
181 process_extensions_pair.second.insert(extension.id());
182 }
162 } 183 }
163 184
164 void RendererStartupHelper::OnExtensionUnloaded(const Extension& extension) { 185 void RendererStartupHelper::OnExtensionUnloaded(const Extension& extension) {
165 // Renderers don't need to know about themes. 186 for (auto& process_extensions_pair : loaded_extensions_) {
166 if (extension.is_theme()) 187 // If extension is already unloaded, do nothing.
167 return; 188 if (!base::ContainsKey(process_extensions_pair.second, extension.id()))
189 continue;
168 190
169 for (content::RenderProcessHost* process : initialized_processes_) 191 content::RenderProcessHost* process = process_extensions_pair.first;
170 process->Send(new ExtensionMsg_Unloaded(extension.id())); 192 process->Send(new ExtensionMsg_Unloaded(extension.id()));
193 process_extensions_pair.second.erase(extension.id());
194 }
195
171 for (auto& process_extensions_pair : pending_active_extensions_) 196 for (auto& process_extensions_pair : pending_active_extensions_)
172 process_extensions_pair.second.erase(extension.id()); 197 process_extensions_pair.second.erase(extension.id());
173 } 198 }
174 199
175 ////////////////////////////////////////////////////////////////////////////// 200 //////////////////////////////////////////////////////////////////////////////
176 201
177 // static 202 // static
178 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( 203 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext(
179 BrowserContext* context) { 204 BrowserContext* context) {
180 return static_cast<RendererStartupHelper*>( 205 return static_cast<RendererStartupHelper*>(
(...skipping 23 matching lines...) Expand all
204 BrowserContext* context) const { 229 BrowserContext* context) const {
205 // Redirected in incognito. 230 // Redirected in incognito.
206 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); 231 return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
207 } 232 }
208 233
209 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { 234 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const {
210 return true; 235 return true;
211 } 236 }
212 237
213 } // namespace extensions 238 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698