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
3 // found in the LICENSE file.
5 #include "extensions/shell/browser/shell_content_browser_client.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/common/url_constants.h"
13 #include "content/shell/browser/shell_browser_context.h"
14 #include "content/shell/browser/shell_devtools_manager_delegate.h"
15 #include "extensions/browser/extension_message_filter.h"
16 #include "extensions/browser/extension_protocols.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/info_map.h"
19 #include "extensions/browser/process_map.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/switches.h"
23 #include "extensions/shell/browser/shell_browser_context.h"
24 #include "extensions/shell/browser/shell_browser_main_parts.h"
25 #include "extensions/shell/browser/shell_extension_system.h"
28 #if !defined(DISABLE_NACL)
29 #include "components/nacl/browser/nacl_browser.h"
30 #include "components/nacl/browser/nacl_host_message_filter.h"
31 #include "components/nacl/browser/nacl_process_host.h"
32 #include "components/nacl/common/nacl_process_type.h"
33 #include "components/nacl/common/nacl_switches.h"
34 #include "content/public/browser/browser_child_process_host.h"
35 #include "content/public/browser/child_process_data.h"
38 using base::CommandLine;
39 using content::BrowserContext;
40 using content::BrowserThread;
42 namespace extensions {
45 ShellContentBrowserClient* g_instance = NULL;
49 ShellContentBrowserClient::ShellContentBrowserClient(
50 ShellBrowserMainDelegate* browser_main_delegate)
51 : browser_main_parts_(NULL), browser_main_delegate_(browser_main_delegate) {
56 ShellContentBrowserClient::~ShellContentBrowserClient() {
61 ShellContentBrowserClient* ShellContentBrowserClient::Get() {
65 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() {
66 return browser_main_parts_->browser_context();
69 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
70 const content::MainFunctionParams& parameters) {
72 new ShellBrowserMainParts(parameters, browser_main_delegate_);
73 return browser_main_parts_;
76 void ShellContentBrowserClient::RenderProcessWillLaunch(
77 content::RenderProcessHost* host) {
78 int render_process_id = host->GetID();
79 BrowserContext* browser_context = browser_main_parts_->browser_context();
81 new ExtensionMessageFilter(render_process_id, browser_context));
82 // PluginInfoMessageFilter is not required because app_shell does not have
83 // the concept of disabled plugins.
84 #if !defined(DISABLE_NACL)
85 host->AddFilter(new nacl::NaClHostMessageFilter(
87 browser_context->IsOffTheRecord(),
88 browser_context->GetPath(),
89 browser_context->GetRequestContextForRenderProcess(render_process_id)));
93 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
94 content::BrowserContext* browser_context,
95 const GURL& effective_url) {
96 // This ensures that all render views created for a single app will use the
97 // same render process (see content::SiteInstance::GetProcess). Otherwise the
98 // default behavior of ContentBrowserClient will lead to separate render
99 // processes for the background page and each app window view.
103 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
104 content::BrowserContext* content_browser_context,
105 content::ProtocolHandlerMap* protocol_handlers,
106 content::URLRequestInterceptorScopedVector request_interceptors) {
107 // Handle only chrome-extension:// requests. app_shell does not support
108 // chrome-extension-resource:// requests (it does not store shared extension
109 // data in its installation directory).
110 InfoMap* extension_info_map =
111 browser_main_parts_->extension_system()->info_map();
112 (*protocol_handlers)[kExtensionScheme] =
113 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
114 CreateExtensionProtocolHandler(false /* is_incognito */,
115 extension_info_map));
116 return browser_main_parts_->browser_context()->CreateRequestContext(
117 protocol_handlers, request_interceptors.Pass(), extension_info_map);
120 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
123 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
124 // content::ShellURLRequestContextGetter::GetURLRequestContext().
125 static const char* const kProtocolList[] = {
127 content::kChromeDevToolsScheme,
128 content::kChromeUIScheme,
131 url::kFileSystemScheme,
133 kExtensionResourceScheme,
135 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
136 if (url.scheme() == kProtocolList[i])
142 void ShellContentBrowserClient::SiteInstanceGotProcess(
143 content::SiteInstance* site_instance) {
144 // If this isn't an extension renderer there's nothing to do.
145 const Extension* extension = GetExtension(site_instance);
149 ProcessMap::Get(browser_main_parts_->browser_context())
150 ->Insert(extension->id(),
151 site_instance->GetProcess()->GetID(),
152 site_instance->GetId());
154 BrowserThread::PostTask(
157 base::Bind(&InfoMap::RegisterExtensionProcess,
158 browser_main_parts_->extension_system()->info_map(),
160 site_instance->GetProcess()->GetID(),
161 site_instance->GetId()));
164 void ShellContentBrowserClient::SiteInstanceDeleting(
165 content::SiteInstance* site_instance) {
166 // If this isn't an extension renderer there's nothing to do.
167 const Extension* extension = GetExtension(site_instance);
171 ProcessMap::Get(browser_main_parts_->browser_context())
172 ->Remove(extension->id(),
173 site_instance->GetProcess()->GetID(),
174 site_instance->GetId());
176 BrowserThread::PostTask(
179 base::Bind(&InfoMap::UnregisterExtensionProcess,
180 browser_main_parts_->extension_system()->info_map(),
182 site_instance->GetProcess()->GetID(),
183 site_instance->GetId()));
186 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
187 CommandLine* command_line,
188 int child_process_id) {
189 std::string process_type =
190 command_line->GetSwitchValueASCII(::switches::kProcessType);
191 if (process_type == ::switches::kRendererProcess)
192 AppendRendererSwitches(command_line);
195 content::BrowserPpapiHost*
196 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
197 #if !defined(DISABLE_NACL)
198 content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
199 while (!iter.Done()) {
200 nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
202 if (host->process() &&
203 host->process()->GetData().id == plugin_process_id) {
205 return host->browser_ppapi_host();
213 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
214 std::vector<std::string>* additional_allowed_schemes) {
215 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
216 additional_allowed_schemes);
217 additional_allowed_schemes->push_back(kExtensionScheme);
220 void ShellContentBrowserClient::AppendRendererSwitches(
221 CommandLine* command_line) {
222 // TODO(jamescook): Should we check here if the process is in the extension
223 // service process map, or can we assume all renderers are extension
225 command_line->AppendSwitch(switches::kExtensionProcess);
227 #if !defined(DISABLE_NACL)
228 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
229 // SFI switches either here or for the zygote process.
230 static const char* const kSwitchNames[] = {
231 ::switches::kEnableNaClDebug,
233 command_line->CopySwitchesFrom(*CommandLine::ForCurrentProcess(),
235 arraysize(kSwitchNames));
236 #endif // !defined(DISABLE_NACL)
239 const Extension* ShellContentBrowserClient::GetExtension(
240 content::SiteInstance* site_instance) {
241 ExtensionRegistry* registry =
242 ExtensionRegistry::Get(site_instance->GetBrowserContext());
243 return registry->enabled_extensions().GetExtensionOrAppByURL(
244 site_instance->GetSiteURL());
247 content::DevToolsManagerDelegate*
248 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
249 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
252 } // namespace extensions