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