Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / shell / browser / shell_content_browser_client.cc
blobc9314ad6b292e2cef7ffd9bc8e398db55b926c58
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 return browser_main_parts_->browser_context()->CreateRequestContext(
128 protocol_handlers, request_interceptors.Pass(), extension_info_map);
131 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
132 if (!url.is_valid())
133 return false;
134 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
135 // content::ShellURLRequestContextGetter::GetURLRequestContext().
136 static const char* const kProtocolList[] = {
137 url::kBlobScheme,
138 content::kChromeDevToolsScheme,
139 content::kChromeUIScheme,
140 url::kDataScheme,
141 url::kFileScheme,
142 url::kFileSystemScheme,
143 kExtensionScheme,
144 kExtensionResourceScheme,
146 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
147 if (url.scheme() == kProtocolList[i])
148 return true;
150 return false;
153 void ShellContentBrowserClient::SiteInstanceGotProcess(
154 content::SiteInstance* site_instance) {
155 // If this isn't an extension renderer there's nothing to do.
156 const Extension* extension = GetExtension(site_instance);
157 if (!extension)
158 return;
160 ProcessMap::Get(browser_main_parts_->browser_context())
161 ->Insert(extension->id(),
162 site_instance->GetProcess()->GetID(),
163 site_instance->GetId());
165 BrowserThread::PostTask(
166 BrowserThread::IO,
167 FROM_HERE,
168 base::Bind(&InfoMap::RegisterExtensionProcess,
169 browser_main_parts_->extension_system()->info_map(),
170 extension->id(),
171 site_instance->GetProcess()->GetID(),
172 site_instance->GetId()));
175 void ShellContentBrowserClient::SiteInstanceDeleting(
176 content::SiteInstance* site_instance) {
177 // If this isn't an extension renderer there's nothing to do.
178 const Extension* extension = GetExtension(site_instance);
179 if (!extension)
180 return;
182 ProcessMap::Get(browser_main_parts_->browser_context())
183 ->Remove(extension->id(),
184 site_instance->GetProcess()->GetID(),
185 site_instance->GetId());
187 BrowserThread::PostTask(
188 BrowserThread::IO,
189 FROM_HERE,
190 base::Bind(&InfoMap::UnregisterExtensionProcess,
191 browser_main_parts_->extension_system()->info_map(),
192 extension->id(),
193 site_instance->GetProcess()->GetID(),
194 site_instance->GetId()));
197 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
198 base::CommandLine* command_line,
199 int child_process_id) {
200 std::string process_type =
201 command_line->GetSwitchValueASCII(::switches::kProcessType);
202 if (process_type == ::switches::kRendererProcess)
203 AppendRendererSwitches(command_line);
206 content::SpeechRecognitionManagerDelegate*
207 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
208 return new speech::ShellSpeechRecognitionManagerDelegate();
211 content::BrowserPpapiHost*
212 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
213 #if !defined(DISABLE_NACL)
214 content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
215 while (!iter.Done()) {
216 nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
217 iter.GetDelegate());
218 if (host->process() &&
219 host->process()->GetData().id == plugin_process_id) {
220 // Found the plugin.
221 return host->browser_ppapi_host();
223 ++iter;
225 #endif
226 return nullptr;
229 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
230 std::vector<std::string>* additional_allowed_schemes) {
231 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
232 additional_allowed_schemes);
233 additional_allowed_schemes->push_back(kExtensionScheme);
236 content::DevToolsManagerDelegate*
237 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
238 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
241 ShellBrowserMainParts* ShellContentBrowserClient::CreateShellBrowserMainParts(
242 const content::MainFunctionParams& parameters,
243 ShellBrowserMainDelegate* browser_main_delegate) {
244 return new ShellBrowserMainParts(parameters, browser_main_delegate);
247 void ShellContentBrowserClient::AppendRendererSwitches(
248 base::CommandLine* command_line) {
249 // TODO(jamescook): Should we check here if the process is in the extension
250 // service process map, or can we assume all renderers are extension
251 // renderers?
252 command_line->AppendSwitch(switches::kExtensionProcess);
254 #if !defined(DISABLE_NACL)
255 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
256 // SFI switches either here or for the zygote process.
257 static const char* const kSwitchNames[] = {
258 ::switches::kEnableNaClDebug,
260 command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
261 kSwitchNames, arraysize(kSwitchNames));
262 #endif // !defined(DISABLE_NACL)
265 const Extension* ShellContentBrowserClient::GetExtension(
266 content::SiteInstance* site_instance) {
267 ExtensionRegistry* registry =
268 ExtensionRegistry::Get(site_instance->GetBrowserContext());
269 return registry->enabled_extensions().GetExtensionOrAppByURL(
270 site_instance->GetSiteURL());
273 } // namespace extensions