Elim cr-checkbox
[chromium-blink-merge.git] / extensions / shell / browser / shell_content_browser_client.cc
blob06cf81b3a48f46b81540d9202e945f70507c7286
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 "components/guest_view/browser/guest_view_message_filter.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/site_instance.h"
12 #include "content/public/common/content_descriptors.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/shell/browser/shell_browser_context.h"
16 #include "content/shell/browser/shell_devtools_manager_delegate.h"
17 #include "extensions/browser/extension_message_filter.h"
18 #include "extensions/browser/extension_protocols.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/guest_view/extensions_guest_view_message_filter.h"
21 #include "extensions/browser/info_map.h"
22 #include "extensions/browser/io_thread_extension_message_filter.h"
23 #include "extensions/browser/process_map.h"
24 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/switches.h"
27 #include "extensions/shell/browser/shell_browser_context.h"
28 #include "extensions/shell/browser/shell_browser_main_parts.h"
29 #include "extensions/shell/browser/shell_extension_system.h"
30 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
31 #include "url/gurl.h"
33 #if !defined(DISABLE_NACL)
34 #include "components/nacl/browser/nacl_browser.h"
35 #include "components/nacl/browser/nacl_host_message_filter.h"
36 #include "components/nacl/browser/nacl_process_host.h"
37 #include "components/nacl/common/nacl_process_type.h"
38 #include "components/nacl/common/nacl_switches.h"
39 #include "content/public/browser/browser_child_process_host.h"
40 #include "content/public/browser/child_process_data.h"
41 #endif
43 using base::CommandLine;
44 using content::BrowserContext;
45 using content::BrowserThread;
47 namespace extensions {
48 namespace {
50 ShellContentBrowserClient* g_instance = nullptr;
52 } // namespace
54 ShellContentBrowserClient::ShellContentBrowserClient(
55 ShellBrowserMainDelegate* browser_main_delegate)
56 : browser_main_parts_(nullptr),
57 browser_main_delegate_(browser_main_delegate) {
58 DCHECK(!g_instance);
59 g_instance = this;
62 ShellContentBrowserClient::~ShellContentBrowserClient() {
63 g_instance = nullptr;
66 // static
67 ShellContentBrowserClient* ShellContentBrowserClient::Get() {
68 return g_instance;
71 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() {
72 return browser_main_parts_->browser_context();
75 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
76 const content::MainFunctionParams& parameters) {
77 browser_main_parts_ =
78 CreateShellBrowserMainParts(parameters, browser_main_delegate_);
79 return browser_main_parts_;
82 void ShellContentBrowserClient::RenderProcessWillLaunch(
83 content::RenderProcessHost* host) {
84 int render_process_id = host->GetID();
85 BrowserContext* browser_context = browser_main_parts_->browser_context();
86 host->AddFilter(
87 new ExtensionMessageFilter(render_process_id, browser_context));
88 host->AddFilter(
89 new IOThreadExtensionMessageFilter(render_process_id, browser_context));
90 host->AddFilter(
91 new ExtensionsGuestViewMessageFilter(
92 render_process_id, browser_context));
93 // PluginInfoMessageFilter is not required because app_shell does not have
94 // the concept of disabled plugins.
95 #if !defined(DISABLE_NACL)
96 host->AddFilter(new nacl::NaClHostMessageFilter(
97 render_process_id,
98 browser_context->IsOffTheRecord(),
99 browser_context->GetPath(),
100 browser_context->GetRequestContextForRenderProcess(render_process_id)));
101 #endif
104 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
105 content::BrowserContext* browser_context,
106 const GURL& effective_url) {
107 // This ensures that all render views created for a single app will use the
108 // same render process (see content::SiteInstance::GetProcess). Otherwise the
109 // default behavior of ContentBrowserClient will lead to separate render
110 // processes for the background page and each app window view.
111 return true;
114 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
115 content::BrowserContext* content_browser_context,
116 content::ProtocolHandlerMap* protocol_handlers,
117 content::URLRequestInterceptorScopedVector request_interceptors) {
118 // Handle only chrome-extension:// requests. app_shell does not support
119 // chrome-extension-resource:// requests (it does not store shared extension
120 // data in its installation directory).
121 InfoMap* extension_info_map =
122 browser_main_parts_->extension_system()->info_map();
123 (*protocol_handlers)[kExtensionScheme] =
124 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
125 CreateExtensionProtocolHandler(false /* is_incognito */,
126 extension_info_map)
127 .release());
128 return browser_main_parts_->browser_context()->CreateRequestContext(
129 protocol_handlers, request_interceptors.Pass(), extension_info_map);
132 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
133 if (!url.is_valid())
134 return false;
135 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
136 // content::ShellURLRequestContextGetter::GetURLRequestContext().
137 static const char* const kProtocolList[] = {
138 url::kBlobScheme,
139 content::kChromeDevToolsScheme,
140 content::kChromeUIScheme,
141 url::kDataScheme,
142 url::kFileScheme,
143 url::kFileSystemScheme,
144 kExtensionScheme,
145 kExtensionResourceScheme,
147 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
148 if (url.scheme() == kProtocolList[i])
149 return true;
151 return false;
154 void ShellContentBrowserClient::SiteInstanceGotProcess(
155 content::SiteInstance* site_instance) {
156 // If this isn't an extension renderer there's nothing to do.
157 const Extension* extension = GetExtension(site_instance);
158 if (!extension)
159 return;
161 ProcessMap::Get(browser_main_parts_->browser_context())
162 ->Insert(extension->id(),
163 site_instance->GetProcess()->GetID(),
164 site_instance->GetId());
166 BrowserThread::PostTask(
167 BrowserThread::IO,
168 FROM_HERE,
169 base::Bind(&InfoMap::RegisterExtensionProcess,
170 browser_main_parts_->extension_system()->info_map(),
171 extension->id(),
172 site_instance->GetProcess()->GetID(),
173 site_instance->GetId()));
176 void ShellContentBrowserClient::SiteInstanceDeleting(
177 content::SiteInstance* site_instance) {
178 // If this isn't an extension renderer there's nothing to do.
179 const Extension* extension = GetExtension(site_instance);
180 if (!extension)
181 return;
183 ProcessMap::Get(browser_main_parts_->browser_context())
184 ->Remove(extension->id(),
185 site_instance->GetProcess()->GetID(),
186 site_instance->GetId());
188 BrowserThread::PostTask(
189 BrowserThread::IO,
190 FROM_HERE,
191 base::Bind(&InfoMap::UnregisterExtensionProcess,
192 browser_main_parts_->extension_system()->info_map(),
193 extension->id(),
194 site_instance->GetProcess()->GetID(),
195 site_instance->GetId()));
198 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
199 base::CommandLine* command_line,
200 int child_process_id) {
201 std::string process_type =
202 command_line->GetSwitchValueASCII(::switches::kProcessType);
203 if (process_type == ::switches::kRendererProcess)
204 AppendRendererSwitches(command_line);
207 content::SpeechRecognitionManagerDelegate*
208 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
209 return new speech::ShellSpeechRecognitionManagerDelegate();
212 content::BrowserPpapiHost*
213 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
214 #if !defined(DISABLE_NACL)
215 content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
216 while (!iter.Done()) {
217 nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
218 iter.GetDelegate());
219 if (host->process() &&
220 host->process()->GetData().id == plugin_process_id) {
221 // Found the plugin.
222 return host->browser_ppapi_host();
224 ++iter;
226 #endif
227 return nullptr;
230 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
231 std::vector<std::string>* additional_allowed_schemes) {
232 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
233 additional_allowed_schemes);
234 additional_allowed_schemes->push_back(kExtensionScheme);
237 content::DevToolsManagerDelegate*
238 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
239 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
242 ShellBrowserMainParts* ShellContentBrowserClient::CreateShellBrowserMainParts(
243 const content::MainFunctionParams& parameters,
244 ShellBrowserMainDelegate* browser_main_delegate) {
245 return new ShellBrowserMainParts(parameters, browser_main_delegate);
248 void ShellContentBrowserClient::AppendRendererSwitches(
249 base::CommandLine* command_line) {
250 // TODO(jamescook): Should we check here if the process is in the extension
251 // service process map, or can we assume all renderers are extension
252 // renderers?
253 command_line->AppendSwitch(switches::kExtensionProcess);
255 #if !defined(DISABLE_NACL)
256 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
257 // SFI switches either here or for the zygote process.
258 static const char* const kSwitchNames[] = {
259 ::switches::kEnableNaClDebug,
261 command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
262 kSwitchNames, arraysize(kSwitchNames));
263 #endif // !defined(DISABLE_NACL)
266 const Extension* ShellContentBrowserClient::GetExtension(
267 content::SiteInstance* site_instance) {
268 ExtensionRegistry* registry =
269 ExtensionRegistry::Get(site_instance->GetBrowserContext());
270 return registry->enabled_extensions().GetExtensionOrAppByURL(
271 site_instance->GetSiteURL());
274 } // namespace extensions