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