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