ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / shell / browser / shell_content_browser_client.cc
blob905a3438ff89265e5c77973c92c20df6d284dcb4
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/guest_view/guest_view_message_filter.h"
19 #include "extensions/browser/info_map.h"
20 #include "extensions/browser/io_thread_extension_message_filter.h"
21 #include "extensions/browser/process_map.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/switches.h"
25 #include "extensions/shell/browser/shell_browser_context.h"
26 #include "extensions/shell/browser/shell_browser_main_parts.h"
27 #include "extensions/shell/browser/shell_extension_system.h"
28 #include "extensions/shell/browser/shell_speech_recognition_manager_delegate.h"
29 #include "url/gurl.h"
31 #if !defined(DISABLE_NACL)
32 #include "components/nacl/browser/nacl_browser.h"
33 #include "components/nacl/browser/nacl_host_message_filter.h"
34 #include "components/nacl/browser/nacl_process_host.h"
35 #include "components/nacl/common/nacl_process_type.h"
36 #include "components/nacl/common/nacl_switches.h"
37 #include "content/public/browser/browser_child_process_host.h"
38 #include "content/public/browser/child_process_data.h"
39 #endif
41 using base::CommandLine;
42 using content::BrowserContext;
43 using content::BrowserThread;
45 namespace extensions {
46 namespace {
48 ShellContentBrowserClient* g_instance = NULL;
50 } // namespace
52 ShellContentBrowserClient::ShellContentBrowserClient(
53 ShellBrowserMainDelegate* browser_main_delegate)
54 : browser_main_parts_(NULL), browser_main_delegate_(browser_main_delegate) {
55 DCHECK(!g_instance);
56 g_instance = this;
59 ShellContentBrowserClient::~ShellContentBrowserClient() {
60 g_instance = NULL;
63 // static
64 ShellContentBrowserClient* ShellContentBrowserClient::Get() {
65 return g_instance;
68 content::BrowserContext* ShellContentBrowserClient::GetBrowserContext() {
69 return browser_main_parts_->browser_context();
72 content::BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
73 const content::MainFunctionParams& parameters) {
74 browser_main_parts_ =
75 CreateShellBrowserMainParts(parameters, browser_main_delegate_);
76 return browser_main_parts_;
79 void ShellContentBrowserClient::RenderProcessWillLaunch(
80 content::RenderProcessHost* host) {
81 int render_process_id = host->GetID();
82 BrowserContext* browser_context = browser_main_parts_->browser_context();
83 host->AddFilter(
84 new ExtensionMessageFilter(render_process_id, browser_context));
85 host->AddFilter(
86 new IOThreadExtensionMessageFilter(render_process_id, browser_context));
87 host->AddFilter(
88 new GuestViewMessageFilter(render_process_id, browser_context));
89 // PluginInfoMessageFilter is not required because app_shell does not have
90 // the concept of disabled plugins.
91 #if !defined(DISABLE_NACL)
92 host->AddFilter(new nacl::NaClHostMessageFilter(
93 render_process_id,
94 browser_context->IsOffTheRecord(),
95 browser_context->GetPath(),
96 browser_context->GetRequestContextForRenderProcess(render_process_id)));
97 #endif
100 bool ShellContentBrowserClient::ShouldUseProcessPerSite(
101 content::BrowserContext* browser_context,
102 const GURL& effective_url) {
103 // This ensures that all render views created for a single app will use the
104 // same render process (see content::SiteInstance::GetProcess). Otherwise the
105 // default behavior of ContentBrowserClient will lead to separate render
106 // processes for the background page and each app window view.
107 return true;
110 net::URLRequestContextGetter* ShellContentBrowserClient::CreateRequestContext(
111 content::BrowserContext* content_browser_context,
112 content::ProtocolHandlerMap* protocol_handlers,
113 content::URLRequestInterceptorScopedVector request_interceptors) {
114 // Handle only chrome-extension:// requests. app_shell does not support
115 // chrome-extension-resource:// requests (it does not store shared extension
116 // data in its installation directory).
117 InfoMap* extension_info_map =
118 browser_main_parts_->extension_system()->info_map();
119 (*protocol_handlers)[kExtensionScheme] =
120 linked_ptr<net::URLRequestJobFactory::ProtocolHandler>(
121 CreateExtensionProtocolHandler(false /* is_incognito */,
122 extension_info_map));
123 return browser_main_parts_->browser_context()->CreateRequestContext(
124 protocol_handlers, request_interceptors.Pass(), extension_info_map);
127 bool ShellContentBrowserClient::IsHandledURL(const GURL& url) {
128 if (!url.is_valid())
129 return false;
130 // Keep in sync with ProtocolHandlers added in CreateRequestContext() and in
131 // content::ShellURLRequestContextGetter::GetURLRequestContext().
132 static const char* const kProtocolList[] = {
133 url::kBlobScheme,
134 content::kChromeDevToolsScheme,
135 content::kChromeUIScheme,
136 url::kDataScheme,
137 url::kFileScheme,
138 url::kFileSystemScheme,
139 kExtensionScheme,
140 kExtensionResourceScheme,
142 for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
143 if (url.scheme() == kProtocolList[i])
144 return true;
146 return false;
149 void ShellContentBrowserClient::SiteInstanceGotProcess(
150 content::SiteInstance* site_instance) {
151 // If this isn't an extension renderer there's nothing to do.
152 const Extension* extension = GetExtension(site_instance);
153 if (!extension)
154 return;
156 ProcessMap::Get(browser_main_parts_->browser_context())
157 ->Insert(extension->id(),
158 site_instance->GetProcess()->GetID(),
159 site_instance->GetId());
161 BrowserThread::PostTask(
162 BrowserThread::IO,
163 FROM_HERE,
164 base::Bind(&InfoMap::RegisterExtensionProcess,
165 browser_main_parts_->extension_system()->info_map(),
166 extension->id(),
167 site_instance->GetProcess()->GetID(),
168 site_instance->GetId()));
171 void ShellContentBrowserClient::SiteInstanceDeleting(
172 content::SiteInstance* site_instance) {
173 // If this isn't an extension renderer there's nothing to do.
174 const Extension* extension = GetExtension(site_instance);
175 if (!extension)
176 return;
178 ProcessMap::Get(browser_main_parts_->browser_context())
179 ->Remove(extension->id(),
180 site_instance->GetProcess()->GetID(),
181 site_instance->GetId());
183 BrowserThread::PostTask(
184 BrowserThread::IO,
185 FROM_HERE,
186 base::Bind(&InfoMap::UnregisterExtensionProcess,
187 browser_main_parts_->extension_system()->info_map(),
188 extension->id(),
189 site_instance->GetProcess()->GetID(),
190 site_instance->GetId()));
193 void ShellContentBrowserClient::AppendExtraCommandLineSwitches(
194 base::CommandLine* command_line,
195 int child_process_id) {
196 std::string process_type =
197 command_line->GetSwitchValueASCII(::switches::kProcessType);
198 if (process_type == ::switches::kRendererProcess)
199 AppendRendererSwitches(command_line);
202 content::SpeechRecognitionManagerDelegate*
203 ShellContentBrowserClient::CreateSpeechRecognitionManagerDelegate() {
204 return new speech::ShellSpeechRecognitionManagerDelegate();
207 content::BrowserPpapiHost*
208 ShellContentBrowserClient::GetExternalBrowserPpapiHost(int plugin_process_id) {
209 #if !defined(DISABLE_NACL)
210 content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
211 while (!iter.Done()) {
212 nacl::NaClProcessHost* host = static_cast<nacl::NaClProcessHost*>(
213 iter.GetDelegate());
214 if (host->process() &&
215 host->process()->GetData().id == plugin_process_id) {
216 // Found the plugin.
217 return host->browser_ppapi_host();
219 ++iter;
221 #endif
222 return NULL;
225 void ShellContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
226 std::vector<std::string>* additional_allowed_schemes) {
227 ContentBrowserClient::GetAdditionalAllowedSchemesForFileSystem(
228 additional_allowed_schemes);
229 additional_allowed_schemes->push_back(kExtensionScheme);
232 content::DevToolsManagerDelegate*
233 ShellContentBrowserClient::GetDevToolsManagerDelegate() {
234 return new content::ShellDevToolsManagerDelegate(GetBrowserContext());
237 ShellBrowserMainParts* ShellContentBrowserClient::CreateShellBrowserMainParts(
238 const content::MainFunctionParams& parameters,
239 ShellBrowserMainDelegate* browser_main_delegate) {
240 return new ShellBrowserMainParts(parameters, browser_main_delegate);
243 void ShellContentBrowserClient::AppendRendererSwitches(
244 base::CommandLine* command_line) {
245 // TODO(jamescook): Should we check here if the process is in the extension
246 // service process map, or can we assume all renderers are extension
247 // renderers?
248 command_line->AppendSwitch(switches::kExtensionProcess);
250 #if !defined(DISABLE_NACL)
251 // NOTE: app_shell does not support non-SFI mode, so it does not pass through
252 // SFI switches either here or for the zygote process.
253 static const char* const kSwitchNames[] = {
254 ::switches::kEnableNaClDebug,
256 command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
257 kSwitchNames, arraysize(kSwitchNames));
258 #endif // !defined(DISABLE_NACL)
261 const Extension* ShellContentBrowserClient::GetExtension(
262 content::SiteInstance* site_instance) {
263 ExtensionRegistry* registry =
264 ExtensionRegistry::Get(site_instance->GetBrowserContext());
265 return registry->enabled_extensions().GetExtensionOrAppByURL(
266 site_instance->GetSiteURL());
269 } // namespace extensions