OLD | NEW |
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/debug/alias.h" | 7 #include "base/debug/alias.h" |
8 #include "base/debug/dump_without_crashing.h" | 8 #include "base/debug/dump_without_crashing.h" |
| 9 #include "base/stl_util.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 12 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 13 #include "content/public/browser/browser_context.h" |
12 #include "content/public/browser/notification_service.h" | 14 #include "content/public/browser/notification_service.h" |
13 #include "content/public/browser/notification_types.h" | 15 #include "content/public/browser/notification_types.h" |
14 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
15 #include "extensions/browser/extension_function_dispatcher.h" | 17 #include "extensions/browser/extension_function_dispatcher.h" |
16 #include "extensions/browser/extension_registry.h" | 18 #include "extensions/browser/extension_registry.h" |
| 19 #include "extensions/browser/extension_util.h" |
17 #include "extensions/browser/extensions_browser_client.h" | 20 #include "extensions/browser/extensions_browser_client.h" |
18 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 21 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
19 #include "extensions/common/extension_messages.h" | 22 #include "extensions/common/extension_messages.h" |
20 #include "extensions/common/extension_set.h" | 23 #include "extensions/common/extension_set.h" |
21 #include "extensions/common/extensions_client.h" | 24 #include "extensions/common/extensions_client.h" |
22 #include "extensions/common/features/feature_channel.h" | 25 #include "extensions/common/features/feature_channel.h" |
23 #include "extensions/common/features/feature_session_type.h" | 26 #include "extensions/common/features/feature_session_type.h" |
24 #include "ui/base/webui/web_ui_util.h" | 27 #include "ui/base/webui/web_ui_util.h" |
25 | 28 |
26 using content::BrowserContext; | 29 using content::BrowserContext; |
27 | 30 |
28 namespace extensions { | 31 namespace extensions { |
29 | 32 |
| 33 namespace { |
| 34 |
| 35 // Returns whether the |extension| should be loaded in the given |
| 36 // |browser_context|. |
| 37 bool IsExtensionVisibleToContext(const Extension& extension, |
| 38 content::BrowserContext* browser_context) { |
| 39 // Renderers don't need to know about themes. |
| 40 if (extension.is_theme()) |
| 41 return false; |
| 42 |
| 43 // Only extensions enabled in incognito mode should be loaded in an incognito |
| 44 // renderer. However extensions which can't be enabled in the incognito mode |
| 45 // (e.g. platform apps) should also be loaded in an incognito renderer to |
| 46 // ensure connections from incognito tabs to such extensions work. |
| 47 return !browser_context->IsOffTheRecord() || |
| 48 !util::CanBeIncognitoEnabled(&extension) || |
| 49 util::IsIncognitoEnabled(extension.id(), browser_context); |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
30 RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context) | 54 RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context) |
31 : browser_context_(browser_context) { | 55 : browser_context_(browser_context) { |
32 DCHECK(browser_context); | 56 DCHECK(browser_context); |
33 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 57 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
34 content::NotificationService::AllBrowserContextsAndSources()); | 58 content::NotificationService::AllBrowserContextsAndSources()); |
35 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 59 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
36 content::NotificationService::AllBrowserContextsAndSources()); | 60 content::NotificationService::AllBrowserContextsAndSources()); |
37 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 61 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
38 content::NotificationService::AllBrowserContextsAndSources()); | 62 content::NotificationService::AllBrowserContextsAndSources()); |
39 } | 63 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 // If the new render process is a WebView guest process, propagate the WebView | 117 // If the new render process is a WebView guest process, propagate the WebView |
94 // partition ID to it. | 118 // partition ID to it. |
95 std::string webview_partition_id = WebViewGuest::GetPartitionID(process); | 119 std::string webview_partition_id = WebViewGuest::GetPartitionID(process); |
96 if (!webview_partition_id.empty()) { | 120 if (!webview_partition_id.empty()) { |
97 process->Send(new ExtensionMsg_SetWebViewPartitionID( | 121 process->Send(new ExtensionMsg_SetWebViewPartitionID( |
98 WebViewGuest::GetPartitionID(process))); | 122 WebViewGuest::GetPartitionID(process))); |
99 } | 123 } |
100 | 124 |
101 // Loaded extensions. | 125 // Loaded extensions. |
102 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions; | 126 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions; |
| 127 BrowserContext* renderer_context = process->GetBrowserContext(); |
103 const ExtensionSet& extensions = | 128 const ExtensionSet& extensions = |
104 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); | 129 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); |
105 for (const auto& ext : extensions) { | 130 for (const auto& ext : extensions) { |
106 // OnLoadedExtension should have already been called for the extension. | 131 // OnLoadedExtension should have already been called for the extension. |
107 DCHECK(base::ContainsKey(extension_process_map_, ext->id())); | 132 DCHECK(base::ContainsKey(extension_process_map_, ext->id())); |
108 DCHECK(!base::ContainsKey(extension_process_map_[ext->id()], process)); | 133 DCHECK(!base::ContainsKey(extension_process_map_[ext->id()], process)); |
109 | 134 |
110 // Renderers don't need to know about themes. | 135 if (!IsExtensionVisibleToContext(*ext, renderer_context)) |
111 if (!ext->is_theme()) { | 136 continue; |
112 // TODO(kalman): Only include tab specific permissions for extension | 137 |
113 // processes, no other process needs it, so it's mildly wasteful. | 138 // TODO(kalman): Only include tab specific permissions for extension |
114 // I am not sure this is possible to know this here, at such a low | 139 // processes, no other process needs it, so it's mildly wasteful. |
115 // level of the stack. Perhaps site isolation can help. | 140 // I am not sure this is possible to know this here, at such a low |
116 bool include_tab_permissions = true; | 141 // level of the stack. Perhaps site isolation can help. |
117 loaded_extensions.push_back( | 142 bool include_tab_permissions = true; |
118 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); | 143 loaded_extensions.push_back( |
119 extension_process_map_[ext->id()].insert(process); | 144 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); |
120 } | 145 extension_process_map_[ext->id()].insert(process); |
121 } | 146 } |
| 147 |
| 148 // Activate pending extensions. |
122 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); | 149 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); |
123 auto iter = pending_active_extensions_.find(process); | 150 auto iter = pending_active_extensions_.find(process); |
124 if (iter != pending_active_extensions_.end()) { | 151 if (iter != pending_active_extensions_.end()) { |
125 for (const ExtensionId& id : iter->second) { | 152 for (const ExtensionId& id : iter->second) { |
126 // The extension should be loaded in the process. | 153 // The extension should be loaded in the process. |
127 DCHECK(extensions.Contains(id)); | 154 DCHECK(extensions.Contains(id)); |
128 DCHECK(base::ContainsKey(extension_process_map_, id)); | 155 DCHECK(base::ContainsKey(extension_process_map_, id)); |
129 DCHECK(base::ContainsKey(extension_process_map_[id], process)); | 156 DCHECK(base::ContainsKey(extension_process_map_[id], process)); |
130 process->Send(new ExtensionMsg_ActivateExtension(id)); | 157 process->Send(new ExtensionMsg_ActivateExtension(id)); |
131 } | 158 } |
(...skipping 28 matching lines...) Expand all Loading... |
160 #else | 187 #else |
161 char extension_id_copy[33]; | 188 char extension_id_copy[33]; |
162 base::strlcpy(extension_id_copy, extension.id().c_str(), | 189 base::strlcpy(extension_id_copy, extension.id().c_str(), |
163 arraysize(extension_id_copy)); | 190 arraysize(extension_id_copy)); |
164 base::debug::Alias(extension_id_copy); | 191 base::debug::Alias(extension_id_copy); |
165 base::debug::DumpWithoutCrashing(); | 192 base::debug::DumpWithoutCrashing(); |
166 return; | 193 return; |
167 #endif | 194 #endif |
168 } | 195 } |
169 | 196 |
170 // Renderers don't need to know about themes. We also don't normally | 197 if (!IsExtensionVisibleToContext(extension, process->GetBrowserContext())) |
171 // "activate" themes, but this could happen if someone tries to open a tab | |
172 // to the e.g. theme's manifest. | |
173 if (extension.is_theme()) | |
174 return; | 198 return; |
175 | 199 |
176 if (base::ContainsKey(initialized_processes_, process)) { | 200 if (base::ContainsKey(initialized_processes_, process)) { |
177 DCHECK(base::ContainsKey(extension_process_map_[extension.id()], process)); | 201 DCHECK(base::ContainsKey(extension_process_map_[extension.id()], process)); |
178 process->Send(new ExtensionMsg_ActivateExtension(extension.id())); | 202 process->Send(new ExtensionMsg_ActivateExtension(extension.id())); |
179 } else { | 203 } else { |
180 pending_active_extensions_[process].insert(extension.id()); | 204 pending_active_extensions_[process].insert(extension.id()); |
181 } | 205 } |
182 } | 206 } |
183 | 207 |
184 void RendererStartupHelper::OnExtensionLoaded(const Extension& extension) { | 208 void RendererStartupHelper::OnExtensionLoaded(const Extension& extension) { |
185 // Extension was already loaded. | 209 // Extension was already loaded. |
186 if (base::ContainsKey(extension_process_map_, extension.id())) | 210 if (base::ContainsKey(extension_process_map_, extension.id())) |
187 return; | 211 return; |
188 | 212 |
189 // Mark the extension as loaded. | 213 // Mark the extension as loaded. |
190 std::set<content::RenderProcessHost*>& loaded_process_set = | 214 std::set<content::RenderProcessHost*>& loaded_process_set = |
191 extension_process_map_[extension.id()]; | 215 extension_process_map_[extension.id()]; |
192 | 216 |
193 // Renderers don't need to know about themes. | |
194 if (extension.is_theme()) | |
195 return; | |
196 | |
197 // We don't need to include tab permisisons here, since the extension | 217 // We don't need to include tab permisisons here, since the extension |
198 // was just loaded. | 218 // was just loaded. |
199 // Uninitialized renderers will be informed of the extension load during the | 219 // Uninitialized renderers will be informed of the extension load during the |
200 // first batch of messages. | 220 // first batch of messages. |
201 std::vector<ExtensionMsg_Loaded_Params> params( | 221 std::vector<ExtensionMsg_Loaded_Params> params( |
202 1, | 222 1, |
203 ExtensionMsg_Loaded_Params(&extension, false /* no tab permissions */)); | 223 ExtensionMsg_Loaded_Params(&extension, false /* no tab permissions */)); |
204 for (content::RenderProcessHost* process : initialized_processes_) { | 224 for (content::RenderProcessHost* process : initialized_processes_) { |
| 225 if (!IsExtensionVisibleToContext(extension, process->GetBrowserContext())) |
| 226 continue; |
205 process->Send(new ExtensionMsg_Loaded(params)); | 227 process->Send(new ExtensionMsg_Loaded(params)); |
206 loaded_process_set.insert(process); | 228 loaded_process_set.insert(process); |
207 } | 229 } |
208 } | 230 } |
209 | 231 |
210 void RendererStartupHelper::OnExtensionUnloaded(const Extension& extension) { | 232 void RendererStartupHelper::OnExtensionUnloaded(const Extension& extension) { |
211 // Extension is not loaded. | 233 // Extension is not loaded. |
212 if (!base::ContainsKey(extension_process_map_, extension.id())) | 234 if (!base::ContainsKey(extension_process_map_, extension.id())) |
213 return; | 235 return; |
214 | 236 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 BrowserContext* context) const { | 280 BrowserContext* context) const { |
259 // Redirected in incognito. | 281 // Redirected in incognito. |
260 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 282 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
261 } | 283 } |
262 | 284 |
263 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { | 285 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { |
264 return true; | 286 return true; |
265 } | 287 } |
266 | 288 |
267 } // namespace extensions | 289 } // namespace extensions |
OLD | NEW |