1 // Copyright (c) 2012 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 "chrome/common/chrome_content_client.h"
7 #include "base/command_line.h"
9 #include "base/debug/crash_logging.h"
10 #include "base/file_util.h"
11 #include "base/path_service.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h"
19 #include "chrome/common/child_process_logging.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/chrome_version_info.h"
23 #include "chrome/common/crash_keys.h"
24 #include "chrome/common/pepper_flash.h"
25 #include "chrome/common/render_messages.h"
26 #include "chrome/common/url_constants.h"
27 #include "components/nacl/common/nacl_process_type.h"
28 #include "content/public/common/content_constants.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/common/pepper_plugin_info.h"
31 #include "content/public/common/url_constants.h"
32 #include "extensions/common/constants.h"
33 #include "gpu/config/gpu_info.h"
34 #include "grit/common_resources.h"
35 #include "ppapi/shared_impl/ppapi_permissions.h"
36 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/base/layout.h"
38 #include "ui/base/resource/resource_bundle.h"
39 #include "webkit/common/user_agent/user_agent_util.h"
41 #include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR.
42 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
45 #include "base/win/registry.h"
46 #include "base/win/windows_version.h"
47 #elif defined(OS_MACOSX)
48 #include "components/nacl/common/nacl_sandbox_type_mac.h"
51 #if defined(ENABLE_REMOTING)
52 #include "remoting/client/plugin/pepper_entrypoints.h"
55 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) && \
56 !defined(WIDEVINE_CDM_IS_COMPONENT)
57 #include "chrome/common/widevine_cdm_constants.h"
62 const char kPDFPluginMimeType[] = "application/pdf";
63 const char kPDFPluginExtension[] = "pdf";
64 const char kPDFPluginDescription[] = "Portable Document Format";
65 const char kPDFPluginPrintPreviewMimeType
66 [] = "application/x-google-chrome-print-preview-pdf";
67 const uint32 kPDFPluginPermissions = ppapi::PERMISSION_PRIVATE |
68 ppapi::PERMISSION_DEV;
70 const char kNaClPluginMimeType[] = "application/x-nacl";
71 const char kNaClPluginExtension[] = "";
72 const char kNaClPluginDescription[] = "Native Client Executable";
73 const uint32 kNaClPluginPermissions = ppapi::PERMISSION_PRIVATE |
74 ppapi::PERMISSION_DEV;
76 const char kPnaclPluginMimeType[] = "application/x-pnacl";
77 const char kPnaclPluginExtension[] = "";
78 const char kPnaclPluginDescription[] = "Portable Native Client Executable";
80 const char kO3DPluginName[] = "Google Talk Plugin Video Accelerator";
81 const char kO3DPluginMimeType[] ="application/vnd.o3d.auto";
82 const char kO3DPluginExtension[] = "";
83 const char kO3DPluginDescription[] = "O3D MIME";
84 const uint32 kO3DPluginPermissions = ppapi::PERMISSION_PRIVATE |
85 ppapi::PERMISSION_DEV;
87 const char kO1DPluginName[] = "Google Talk Plugin Video Renderer";
88 const char kO1DPluginMimeType[] ="application/o1d";
89 const char kO1DPluginExtension[] = "";
90 const char kO1DPluginDescription[] = "Google Talk Plugin Video Renderer";
91 const uint32 kO1DPluginPermissions = ppapi::PERMISSION_PRIVATE |
92 ppapi::PERMISSION_DEV;
94 const char kEffectsPluginName[] = "Google Talk Effects Plugin";
95 const char kEffectsPluginMimeType[] ="application/x-ppapi-hangouts-effects";
96 const char kEffectsPluginExtension[] = "";
97 const char kEffectsPluginDescription[] = "Google Talk Effects Plugin";
98 const uint32 kEffectsPluginPermissions = ppapi::PERMISSION_PRIVATE |
99 ppapi::PERMISSION_DEV;
101 const char kGTalkPluginName[] = "Google Talk Plugin";
102 const char kGTalkPluginMimeType[] ="application/googletalk";
103 const char kGTalkPluginExtension[] = ".googletalk";
104 const char kGTalkPluginDescription[] = "Google Talk Plugin";
105 const uint32 kGTalkPluginPermissions = ppapi::PERMISSION_PRIVATE |
106 ppapi::PERMISSION_DEV;
108 #if defined(ENABLE_REMOTING)
109 #if defined(GOOGLE_CHROME_BUILD)
110 const char kRemotingViewerPluginName[] = "Chrome Remote Desktop Viewer";
112 const char kRemotingViewerPluginName[] = "Chromoting Viewer";
113 #endif // defined(GOOGLE_CHROME_BUILD)
114 const char kRemotingViewerPluginDescription[] =
115 "This plugin allows you to securely access other computers that have been "
116 "shared with you. To use this plugin you must first install the "
117 "<a href=\"https://chrome.google.com/remotedesktop\">"
118 "Chrome Remote Desktop</a> webapp.";
119 // Use a consistent MIME-type regardless of branding.
120 const char kRemotingViewerPluginMimeType[] =
121 "application/vnd.chromium.remoting-viewer";
122 const char kRemotingViewerPluginMimeExtension[] = "";
123 const char kRemotingViewerPluginMimeDescription[] = "";
124 const uint32 kRemotingViewerPluginPermissions = ppapi::PERMISSION_PRIVATE |
125 ppapi::PERMISSION_DEV;
126 #endif // defined(ENABLE_REMOTING)
128 #if defined(OS_MACOSX) && !defined(OS_IOS)
129 const char kInterposeLibraryPath[] =
130 "@executable_path/../../../libplugin_carbon_interpose.dylib";
131 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
133 // Appends the known built-in plugins to the given vector. Some built-in
134 // plugins are "internal" which means they are compiled into the Chrome binary,
135 // and some are extra shared libraries distributed with the browser (these are
136 // not marked internal, aside from being automatically registered, they're just
138 void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
141 // Once we're sandboxed, we can't know if the PDF plugin is available or not;
142 // but (on Linux) this function is always called once before we're sandboxed.
143 // So the first time through test if the file is available and then skip the
144 // check on subsequent calls if yes.
145 static bool skip_pdf_file_check = false;
147 if (PathService::Get(chrome::FILE_PDF_PLUGIN, &path)) {
148 if (skip_pdf_file_check || base::PathExists(path)) {
149 content::PepperPluginInfo pdf;
151 pdf.name = chrome::ChromeContentClient::kPDFPluginName;
152 content::WebPluginMimeType pdf_mime_type(kPDFPluginMimeType,
154 kPDFPluginDescription);
155 content::WebPluginMimeType print_preview_pdf_mime_type(
156 kPDFPluginPrintPreviewMimeType,
158 kPDFPluginDescription);
159 pdf.mime_types.push_back(pdf_mime_type);
160 pdf.mime_types.push_back(print_preview_pdf_mime_type);
161 pdf.permissions = kPDFPluginPermissions;
162 plugins->push_back(pdf);
164 skip_pdf_file_check = true;
168 // Handle Native Client just like the PDF plugin. This means that it is
169 // enabled by default for the non-portable case. This allows apps installed
170 // from the Chrome Web Store to use NaCl even if the command line switch
171 // isn't set. For other uses of NaCl we check for the command line switch.
172 // Specifically, Portable Native Client is only enabled by the command line
174 static bool skip_nacl_file_check = false;
175 if (PathService::Get(chrome::FILE_NACL_PLUGIN, &path)) {
176 if (skip_nacl_file_check || base::PathExists(path)) {
177 content::PepperPluginInfo nacl;
179 nacl.name = chrome::ChromeContentClient::kNaClPluginName;
180 content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
181 kNaClPluginExtension,
182 kNaClPluginDescription);
183 nacl.mime_types.push_back(nacl_mime_type);
184 if (!CommandLine::ForCurrentProcess()->HasSwitch(
185 switches::kDisablePnacl)) {
186 content::WebPluginMimeType pnacl_mime_type(kPnaclPluginMimeType,
187 kPnaclPluginExtension,
188 kPnaclPluginDescription);
189 nacl.mime_types.push_back(pnacl_mime_type);
191 nacl.permissions = kNaClPluginPermissions;
192 plugins->push_back(nacl);
194 skip_nacl_file_check = true;
198 // TODO(jhorwich|noahric): Remove o3d ppapi code once o3d is replaced
199 // entirely with o1d.
200 static bool skip_o3d_file_check = false;
201 if (PathService::Get(chrome::FILE_O3D_PLUGIN, &path)) {
202 if (skip_o3d_file_check || base::PathExists(path)) {
203 content::PepperPluginInfo o3d;
205 o3d.name = kO3DPluginName;
206 o3d.is_out_of_process = true;
207 o3d.is_sandboxed = false;
208 o3d.permissions = kO3DPluginPermissions;
209 content::WebPluginMimeType o3d_mime_type(kO3DPluginMimeType,
211 kO3DPluginDescription);
212 o3d.mime_types.push_back(o3d_mime_type);
213 plugins->push_back(o3d);
215 skip_o3d_file_check = true;
219 static bool skip_o1d_file_check = false;
220 if (PathService::Get(chrome::FILE_O1D_PLUGIN, &path)) {
221 if (skip_o1d_file_check || base::PathExists(path)) {
222 content::PepperPluginInfo o1d;
224 o1d.name = kO1DPluginName;
225 o1d.is_out_of_process = true;
226 o1d.is_sandboxed = false;
227 o1d.permissions = kO1DPluginPermissions;
228 content::WebPluginMimeType o1d_mime_type(kO1DPluginMimeType,
230 kO1DPluginDescription);
231 o1d.mime_types.push_back(o1d_mime_type);
232 plugins->push_back(o1d);
234 skip_o1d_file_check = true;
238 // TODO(vrk): Remove this when NaCl effects plugin replaces the ppapi effects
240 static bool skip_effects_file_check = false;
241 if (PathService::Get(chrome::FILE_EFFECTS_PLUGIN, &path)) {
242 if (skip_effects_file_check || base::PathExists(path)) {
243 content::PepperPluginInfo effects;
245 effects.name = kEffectsPluginName;
246 effects.is_out_of_process = true;
247 effects.is_sandboxed = true;
248 effects.permissions = kEffectsPluginPermissions;
249 content::WebPluginMimeType effects_mime_type(kEffectsPluginMimeType,
250 kEffectsPluginExtension,
251 kEffectsPluginDescription);
252 effects.mime_types.push_back(effects_mime_type);
253 plugins->push_back(effects);
255 skip_effects_file_check = true;
259 static bool skip_gtalk_file_check = false;
260 if (PathService::Get(chrome::FILE_GTALK_PLUGIN, &path)) {
261 if (skip_gtalk_file_check || base::PathExists(path)) {
262 content::PepperPluginInfo gtalk;
264 gtalk.name = kGTalkPluginName;
265 gtalk.is_out_of_process = true;
266 gtalk.is_sandboxed = false;
267 gtalk.permissions = kGTalkPluginPermissions;
268 content::WebPluginMimeType gtalk_mime_type(kGTalkPluginMimeType,
269 kGTalkPluginExtension,
270 kGTalkPluginDescription);
271 gtalk.mime_types.push_back(gtalk_mime_type);
272 plugins->push_back(gtalk);
274 skip_gtalk_file_check = true;
278 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) && \
279 !defined(WIDEVINE_CDM_IS_COMPONENT)
280 static bool skip_widevine_cdm_file_check = false;
281 if (PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &path)) {
282 if (skip_widevine_cdm_file_check || base::PathExists(path)) {
283 content::PepperPluginInfo widevine_cdm;
284 widevine_cdm.is_out_of_process = true;
285 widevine_cdm.path = path;
286 widevine_cdm.name = kWidevineCdmDisplayName;
287 widevine_cdm.description = kWidevineCdmDescription;
288 widevine_cdm.version = WIDEVINE_CDM_VERSION_STRING;
289 content::WebPluginMimeType widevine_cdm_mime_type(
290 kWidevineCdmPluginMimeType,
291 kWidevineCdmPluginExtension,
292 kWidevineCdmPluginMimeTypeDescription);
294 // Add the supported codecs as if they came from the component manifest.
295 std::vector<std::string> codecs;
296 codecs.push_back(kCdmSupportedCodecVorbis);
297 codecs.push_back(kCdmSupportedCodecVp8);
298 #if defined(USE_PROPRIETARY_CODECS)
299 // TODO(ddorwin): Rename these macros to reflect their real meaning: whether the
300 // CDM Chrome was built [and shipped] with support these types.
301 #if defined(WIDEVINE_CDM_AAC_SUPPORT_AVAILABLE)
302 codecs.push_back(kCdmSupportedCodecAac);
304 #if defined(WIDEVINE_CDM_AVC1_SUPPORT_AVAILABLE)
305 codecs.push_back(kCdmSupportedCodecAvc1);
307 #endif // defined(USE_PROPRIETARY_CODECS)
308 std::string codec_string =
309 JoinString(codecs, kCdmSupportedCodecsValueDelimiter);
310 widevine_cdm_mime_type.additional_param_names.push_back(
311 base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
312 widevine_cdm_mime_type.additional_param_values.push_back(
313 base::ASCIIToUTF16(codec_string));
315 widevine_cdm.mime_types.push_back(widevine_cdm_mime_type);
316 widevine_cdm.permissions = kWidevineCdmPluginPermissions;
317 plugins->push_back(widevine_cdm);
319 skip_widevine_cdm_file_check = true;
322 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) &&
323 // !defined(WIDEVINE_CDM_IS_COMPONENT)
325 // The Remoting Viewer plugin is built-in.
326 #if defined(ENABLE_REMOTING)
327 content::PepperPluginInfo info;
328 info.is_internal = true;
329 info.is_out_of_process = true;
330 info.name = kRemotingViewerPluginName;
331 info.description = kRemotingViewerPluginDescription;
332 info.path = base::FilePath::FromUTF8Unsafe(
333 chrome::ChromeContentClient::kRemotingViewerPluginPath);
334 content::WebPluginMimeType remoting_mime_type(
335 kRemotingViewerPluginMimeType,
336 kRemotingViewerPluginMimeExtension,
337 kRemotingViewerPluginMimeDescription);
338 info.mime_types.push_back(remoting_mime_type);
339 info.internal_entry_points.get_interface = remoting::PPP_GetInterface;
340 info.internal_entry_points.initialize_module =
341 remoting::PPP_InitializeModule;
342 info.internal_entry_points.shutdown_module = remoting::PPP_ShutdownModule;
343 info.permissions = kRemotingViewerPluginPermissions;
345 plugins->push_back(info);
349 content::PepperPluginInfo CreatePepperFlashInfo(const base::FilePath& path,
350 const std::string& version) {
351 content::PepperPluginInfo plugin;
353 // Flash being out of process is handled separately than general plugins
354 // for testing purposes.
355 plugin.is_out_of_process = !CommandLine::ForCurrentProcess()->HasSwitch(
356 switches::kPpapiFlashInProcess);
357 plugin.name = content::kFlashPluginName;
359 plugin.permissions = kPepperFlashPermissions;
361 std::vector<std::string> flash_version_numbers;
362 base::SplitString(version, '.', &flash_version_numbers);
363 if (flash_version_numbers.size() < 1)
364 flash_version_numbers.push_back("11");
365 // |SplitString()| puts in an empty string given an empty string. :(
366 else if (flash_version_numbers[0].empty())
367 flash_version_numbers[0] = "11";
368 if (flash_version_numbers.size() < 2)
369 flash_version_numbers.push_back("2");
370 if (flash_version_numbers.size() < 3)
371 flash_version_numbers.push_back("999");
372 if (flash_version_numbers.size() < 4)
373 flash_version_numbers.push_back("999");
374 // E.g., "Shockwave Flash 10.2 r154":
375 plugin.description = plugin.name + " " + flash_version_numbers[0] + "." +
376 flash_version_numbers[1] + " r" + flash_version_numbers[2];
377 plugin.version = JoinString(flash_version_numbers, '.');
378 content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
379 content::kFlashPluginSwfExtension,
380 content::kFlashPluginSwfDescription);
381 plugin.mime_types.push_back(swf_mime_type);
382 content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
383 content::kFlashPluginSplExtension,
384 content::kFlashPluginSplDescription);
385 plugin.mime_types.push_back(spl_mime_type);
390 void AddPepperFlashFromCommandLine(
391 std::vector<content::PepperPluginInfo>* plugins) {
392 const CommandLine::StringType flash_path =
393 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
394 switches::kPpapiFlashPath);
395 if (flash_path.empty())
398 // Also get the version from the command-line. Should be something like 11.2
400 std::string flash_version =
401 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
402 switches::kPpapiFlashVersion);
405 CreatePepperFlashInfo(base::FilePath(flash_path), flash_version));
408 bool GetBundledPepperFlash(content::PepperPluginInfo* plugin) {
409 #if defined(FLAPPER_AVAILABLE)
410 CommandLine* command_line = CommandLine::ForCurrentProcess();
412 // Ignore bundled Pepper Flash if there is Pepper Flash specified from the
414 if (command_line->HasSwitch(switches::kPpapiFlashPath))
418 command_line->HasSwitch(switches::kDisableBundledPpapiFlash);
422 // For Linux ia32, Flapper requires SSE2.
423 #if defined(OS_LINUX) && defined(ARCH_CPU_X86)
424 if (!base::CPU().has_sse2())
426 #endif // ARCH_CPU_X86
428 base::FilePath flash_path;
429 if (!PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &flash_path))
432 *plugin = CreatePepperFlashInfo(flash_path, FLAPPER_VERSION_STRING);
436 #endif // FLAPPER_AVAILABLE
443 void ChromeContentClient::SetActiveURL(const GURL& url) {
444 base::debug::SetCrashKeyValue(crash_keys::kActiveURL,
445 url.possibly_invalid_spec());
448 void ChromeContentClient::SetGpuInfo(const gpu::GPUInfo& gpu_info) {
449 #if !defined(OS_ANDROID)
450 base::debug::SetCrashKeyValue(crash_keys::kGPUVendorID,
451 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
452 base::debug::SetCrashKeyValue(crash_keys::kGPUDeviceID,
453 base::StringPrintf("0x%04x", gpu_info.gpu.device_id));
455 base::debug::SetCrashKeyValue(crash_keys::kGPUDriverVersion,
456 gpu_info.driver_version);
457 base::debug::SetCrashKeyValue(crash_keys::kGPUPixelShaderVersion,
458 gpu_info.pixel_shader_version);
459 base::debug::SetCrashKeyValue(crash_keys::kGPUVertexShaderVersion,
460 gpu_info.vertex_shader_version);
461 #if defined(OS_MACOSX)
462 base::debug::SetCrashKeyValue(crash_keys::kGPUGLVersion, gpu_info.gl_version);
463 #elif defined(OS_POSIX)
464 base::debug::SetCrashKeyValue(crash_keys::kGPUVendor, gpu_info.gl_vendor);
465 base::debug::SetCrashKeyValue(crash_keys::kGPURenderer, gpu_info.gl_renderer);
469 void ChromeContentClient::AddPepperPlugins(
470 std::vector<content::PepperPluginInfo>* plugins) {
471 ComputeBuiltInPlugins(plugins);
472 AddPepperFlashFromCommandLine(plugins);
474 content::PepperPluginInfo plugin;
475 if (GetBundledPepperFlash(&plugin))
476 plugins->push_back(plugin);
479 void ChromeContentClient::AddAdditionalSchemes(
480 std::vector<std::string>* standard_schemes,
481 std::vector<std::string>* savable_schemes) {
482 standard_schemes->push_back(extensions::kExtensionScheme);
483 savable_schemes->push_back(extensions::kExtensionScheme);
484 standard_schemes->push_back(kExtensionResourceScheme);
485 savable_schemes->push_back(kExtensionResourceScheme);
486 standard_schemes->push_back(chrome::kChromeSearchScheme);
487 savable_schemes->push_back(chrome::kChromeSearchScheme);
488 #if defined(OS_CHROMEOS)
489 standard_schemes->push_back(kCrosScheme);
493 bool ChromeContentClient::CanHandleWhileSwappedOut(
494 const IPC::Message& msg) {
495 // Any Chrome-specific messages (apart from those listed in
496 // CanSendWhileSwappedOut) that must be handled by the browser when sent from
497 // swapped out renderers.
501 std::string ChromeContentClient::GetProduct() const {
502 chrome::VersionInfo version_info;
503 return version_info.is_valid() ?
504 version_info.ProductNameAndVersionForUserAgent() : std::string();
507 std::string ChromeContentClient::GetUserAgent() const {
508 std::string product = GetProduct();
509 #if defined(OS_ANDROID)
510 CommandLine* command_line = CommandLine::ForCurrentProcess();
511 if (command_line->HasSwitch(switches::kUseMobileUserAgent))
512 product += " Mobile";
514 return webkit_glue::BuildUserAgentFromProduct(product);
517 string16 ChromeContentClient::GetLocalizedString(int message_id) const {
518 return l10n_util::GetStringUTF16(message_id);
521 base::StringPiece ChromeContentClient::GetDataResource(
523 ui::ScaleFactor scale_factor) const {
524 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
525 resource_id, scale_factor);
528 base::RefCountedStaticMemory* ChromeContentClient::GetDataResourceBytes(
529 int resource_id) const {
530 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
533 gfx::Image& ChromeContentClient::GetNativeImageNamed(int resource_id) const {
534 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
537 std::string ChromeContentClient::GetProcessTypeNameInEnglish(int type) {
539 case PROCESS_TYPE_NACL_LOADER:
540 return "Native Client module";
541 case PROCESS_TYPE_NACL_BROKER:
542 return "Native Client broker";
545 DCHECK(false) << "Unknown child process type!";
549 #if defined(OS_MACOSX) && !defined(OS_IOS)
550 bool ChromeContentClient::GetSandboxProfileForSandboxType(
552 int* sandbox_profile_resource_id) const {
553 DCHECK(sandbox_profile_resource_id);
554 if (sandbox_type == NACL_SANDBOX_TYPE_NACL_LOADER) {
555 *sandbox_profile_resource_id = IDR_NACL_SANDBOX_PROFILE;
561 std::string ChromeContentClient::GetCarbonInterposePath() const {
562 return std::string(kInterposeLibraryPath);
566 } // namespace chrome