chrome_content_client.cc revision 3551c9c881056c480085172ff9840cab31610854
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 "grit/common_resources.h"
33#include "ppapi/shared_impl/ppapi_permissions.h"
34#include "remoting/client/plugin/pepper_entrypoints.h"
35#include "ui/base/l10n/l10n_util.h"
36#include "ui/base/layout.h"
37#include "ui/base/resource/resource_bundle.h"
38#include "webkit/common/user_agent/user_agent_util.h"
39
40#include "flapper_version.h"  // In SHARED_INTERMEDIATE_DIR.
41#include "widevine_cdm_version.h"  // In SHARED_INTERMEDIATE_DIR.
42
43#if defined(OS_WIN)
44#include "base/win/registry.h"
45#include "base/win/windows_version.h"
46#include "sandbox/win/src/sandbox.h"
47#elif defined(OS_MACOSX)
48#include "components/nacl/common/nacl_sandbox_type_mac.h"
49#endif
50
51#if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) && \
52    !defined(WIDEVINE_CDM_IS_COMPONENT)
53#include "chrome/common/widevine_cdm_constants.h"
54#endif
55
56namespace {
57
58const char kPDFPluginMimeType[] = "application/pdf";
59const char kPDFPluginExtension[] = "pdf";
60const char kPDFPluginDescription[] = "Portable Document Format";
61const char kPDFPluginPrintPreviewMimeType
62   [] = "application/x-google-chrome-print-preview-pdf";
63const uint32 kPDFPluginPermissions = ppapi::PERMISSION_PRIVATE |
64                                     ppapi::PERMISSION_DEV;
65
66const char kNaClPluginMimeType[] = "application/x-nacl";
67const char kNaClPluginExtension[] = "";
68const char kNaClPluginDescription[] = "Native Client Executable";
69const uint32 kNaClPluginPermissions = ppapi::PERMISSION_PRIVATE |
70                                      ppapi::PERMISSION_DEV;
71
72const char kPnaclPluginMimeType[] = "application/x-pnacl";
73const char kPnaclPluginExtension[] = "";
74const char kPnaclPluginDescription[] = "Portable Native Client Executable";
75const uint32 kPnaclPluginPermissions = ppapi::PERMISSION_PRIVATE |
76                                       ppapi::PERMISSION_DEV;
77
78const char kO3DPluginName[] = "Google Talk Plugin Video Accelerator";
79const char kO3DPluginMimeType[] ="application/vnd.o3d.auto";
80const char kO3DPluginExtension[] = "";
81const char kO3DPluginDescription[] = "O3D MIME";
82const uint32 kO3DPluginPermissions = ppapi::PERMISSION_PRIVATE |
83                                     ppapi::PERMISSION_DEV;
84
85const char kO1DPluginName[] = "Google Talk Plugin Video Renderer";
86const char kO1DPluginMimeType[] ="application/o1d";
87const char kO1DPluginExtension[] = "";
88const char kO1DPluginDescription[] = "Google Talk Plugin Video Renderer";
89const uint32 kO1DPluginPermissions = ppapi::PERMISSION_PRIVATE |
90                                     ppapi::PERMISSION_DEV;
91
92const char kGTalkPluginName[] = "Google Talk Plugin";
93const char kGTalkPluginMimeType[] ="application/googletalk";
94const char kGTalkPluginExtension[] = ".googletalk";
95const char kGTalkPluginDescription[] = "Google Talk Plugin";
96const uint32 kGTalkPluginPermissions = ppapi::PERMISSION_PRIVATE |
97                                       ppapi::PERMISSION_DEV;
98
99#if defined(ENABLE_REMOTING)
100#if defined(GOOGLE_CHROME_BUILD)
101const char kRemotingViewerPluginName[] = "Chrome Remote Desktop Viewer";
102#else
103const char kRemotingViewerPluginName[] = "Chromoting Viewer";
104#endif  // defined(GOOGLE_CHROME_BUILD)
105const char kRemotingViewerPluginDescription[] =
106    "This plugin allows you to securely access other computers that have been "
107    "shared with you. To use this plugin you must first install the "
108    "<a href=\"https://chrome.google.com/remotedesktop\">"
109    "Chrome Remote Desktop</a> webapp.";
110const base::FilePath::CharType kRemotingViewerPluginPath[] =
111    FILE_PATH_LITERAL("internal-remoting-viewer");
112// Use a consistent MIME-type regardless of branding.
113const char kRemotingViewerPluginMimeType[] =
114    "application/vnd.chromium.remoting-viewer";
115const char kRemotingViewerPluginMimeExtension[] = "";
116const char kRemotingViewerPluginMimeDescription[] = "";
117const uint32 kRemotingViewerPluginPermissions = ppapi::PERMISSION_PRIVATE |
118                                                ppapi::PERMISSION_DEV;
119#endif  // defined(ENABLE_REMOTING)
120
121const char kInterposeLibraryPath[] =
122    "@executable_path/../../../libplugin_carbon_interpose.dylib";
123
124// Appends the known built-in plugins to the given vector. Some built-in
125// plugins are "internal" which means they are compiled into the Chrome binary,
126// and some are extra shared libraries distributed with the browser (these are
127// not marked internal, aside from being automatically registered, they're just
128// regular plugins).
129void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
130  // PDF.
131  //
132  // Once we're sandboxed, we can't know if the PDF plugin is available or not;
133  // but (on Linux) this function is always called once before we're sandboxed.
134  // So the first time through test if the file is available and then skip the
135  // check on subsequent calls if yes.
136  static bool skip_pdf_file_check = false;
137  base::FilePath path;
138  if (PathService::Get(chrome::FILE_PDF_PLUGIN, &path)) {
139    if (skip_pdf_file_check || base::PathExists(path)) {
140      content::PepperPluginInfo pdf;
141      pdf.path = path;
142      pdf.name = chrome::ChromeContentClient::kPDFPluginName;
143      content::WebPluginMimeType pdf_mime_type(kPDFPluginMimeType,
144                                               kPDFPluginExtension,
145                                               kPDFPluginDescription);
146      content::WebPluginMimeType print_preview_pdf_mime_type(
147          kPDFPluginPrintPreviewMimeType,
148          kPDFPluginExtension,
149          kPDFPluginDescription);
150      pdf.mime_types.push_back(pdf_mime_type);
151      pdf.mime_types.push_back(print_preview_pdf_mime_type);
152      pdf.permissions = kPDFPluginPermissions;
153      plugins->push_back(pdf);
154
155      skip_pdf_file_check = true;
156    }
157  }
158
159  // Handle Native Client just like the PDF plugin. This means that it is
160  // enabled by default for the non-portable case.  This allows apps installed
161  // from the Chrome Web Store to use NaCl even if the command line switch
162  // isn't set.  For other uses of NaCl we check for the command line switch.
163  // Specifically, Portable Native Client is only enabled by the command line
164  // switch.
165  static bool skip_nacl_file_check = false;
166  if (PathService::Get(chrome::FILE_NACL_PLUGIN, &path)) {
167    if (skip_nacl_file_check || base::PathExists(path)) {
168      content::PepperPluginInfo nacl;
169      nacl.path = path;
170      nacl.name = chrome::ChromeContentClient::kNaClPluginName;
171      content::WebPluginMimeType nacl_mime_type(kNaClPluginMimeType,
172                                                kNaClPluginExtension,
173                                                kNaClPluginDescription);
174      nacl.mime_types.push_back(nacl_mime_type);
175      if (!CommandLine::ForCurrentProcess()->HasSwitch(
176              switches::kDisablePnacl)) {
177        content::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 || base::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      content::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 || base::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      content::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 || base::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      content::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 || base::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      content::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  content::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 = content::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  content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
326                                           content::kFlashPluginSwfExtension,
327                                           content::kFlashPluginSwfDescription);
328  plugin.mime_types.push_back(swf_mime_type);
329  content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
330                                           content::kFlashPluginSplExtension,
331                                           content::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  base::debug::SetCrashKeyValue(crash_keys::kActiveURL,
392                                url.possibly_invalid_spec());
393}
394
395void ChromeContentClient::SetGpuInfo(const gpu::GPUInfo& gpu_info) {
396  child_process_logging::SetGpuInfo(gpu_info);
397}
398
399void ChromeContentClient::AddPepperPlugins(
400    std::vector<content::PepperPluginInfo>* plugins) {
401  ComputeBuiltInPlugins(plugins);
402  AddPepperFlashFromCommandLine(plugins);
403
404  content::PepperPluginInfo plugin;
405  if (GetBundledPepperFlash(&plugin))
406    plugins->push_back(plugin);
407}
408
409void ChromeContentClient::AddAdditionalSchemes(
410    std::vector<std::string>* standard_schemes,
411    std::vector<std::string>* savable_schemes) {
412  standard_schemes->push_back(extensions::kExtensionScheme);
413  savable_schemes->push_back(extensions::kExtensionScheme);
414  standard_schemes->push_back(kExtensionResourceScheme);
415  savable_schemes->push_back(kExtensionResourceScheme);
416  standard_schemes->push_back(chrome::kChromeSearchScheme);
417  savable_schemes->push_back(chrome::kChromeSearchScheme);
418#if defined(OS_CHROMEOS)
419  standard_schemes->push_back(kCrosScheme);
420#endif
421}
422
423bool ChromeContentClient::CanHandleWhileSwappedOut(
424    const IPC::Message& msg) {
425  // Any Chrome-specific messages (apart from those listed in
426  // CanSendWhileSwappedOut) that must be handled by the browser when sent from
427  // swapped out renderers.
428  return false;
429}
430
431std::string ChromeContentClient::GetProduct() const {
432  chrome::VersionInfo version_info;
433  return version_info.is_valid() ?
434      version_info.ProductNameAndVersionForUserAgent() : std::string();
435}
436
437std::string ChromeContentClient::GetUserAgent() const {
438  std::string product = GetProduct();
439#if defined(OS_ANDROID)
440  CommandLine* command_line = CommandLine::ForCurrentProcess();
441  if (command_line->HasSwitch(switches::kUseMobileUserAgent))
442    product += " Mobile";
443#endif
444  return webkit_glue::BuildUserAgentFromProduct(product);
445}
446
447string16 ChromeContentClient::GetLocalizedString(int message_id) const {
448  return l10n_util::GetStringUTF16(message_id);
449}
450
451base::StringPiece ChromeContentClient::GetDataResource(
452    int resource_id,
453    ui::ScaleFactor scale_factor) const {
454  return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
455      resource_id, scale_factor);
456}
457
458base::RefCountedStaticMemory* ChromeContentClient::GetDataResourceBytes(
459    int resource_id) const {
460  return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
461}
462
463gfx::Image& ChromeContentClient::GetNativeImageNamed(int resource_id) const {
464  return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
465}
466
467std::string ChromeContentClient::GetProcessTypeNameInEnglish(int type) {
468  switch (type) {
469    case PROCESS_TYPE_NACL_LOADER:
470      return "Native Client module";
471    case PROCESS_TYPE_NACL_BROKER:
472      return "Native Client broker";
473  }
474
475  DCHECK(false) << "Unknown child process type!";
476  return "Unknown";
477}
478
479#if defined(OS_MACOSX) && !defined(OS_IOS)
480bool ChromeContentClient::GetSandboxProfileForSandboxType(
481    int sandbox_type,
482    int* sandbox_profile_resource_id) const {
483  DCHECK(sandbox_profile_resource_id);
484  if (sandbox_type == NACL_SANDBOX_TYPE_NACL_LOADER) {
485    *sandbox_profile_resource_id = IDR_NACL_SANDBOX_PROFILE;
486    return true;
487  }
488  return false;
489}
490
491std::string ChromeContentClient::GetCarbonInterposePath() const {
492  return std::string(kInterposeLibraryPath);
493}
494#endif
495
496}  // namespace chrome
497