chrome_content_client.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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_switches.h" 21#include "chrome/common/chrome_version_info.h" 22#include "chrome/common/pepper_flash.h" 23#include "chrome/common/render_messages.h" 24#include "chrome/common/url_constants.h" 25#include "components/nacl/common/nacl_process_type.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/plugin_constants.h" 38#include "webkit/plugins/plugin_switches.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(switches::kEnablePnacl)) { 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