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