Fix some interleaving issues in EmbeddedWorkerInstance.
[chromium-blink-merge.git] / chrome / common / chrome_content_client.cc
blob5fc72b7195198b132668b06406fae00ee1cc0c64
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.
5 #include "chrome/common/chrome_content_client.h"
7 #include "base/command_line.h"
8 #include "base/debug/crash_logging.h"
9 #include "base/files/file_util.h"
10 #include "base/json/json_reader.h"
11 #include "base/path_service.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "build/build_config.h"
19 #include "chrome/common/child_process_logging.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/crash_keys.h"
24 #include "chrome/common/pepper_flash.h"
25 #include "chrome/common/secure_origin_whitelist.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/grit/common_resources.h"
28 #include "components/dom_distiller/core/url_constants.h"
29 #include "components/version_info/version_info.h"
30 #include "content/public/common/content_constants.h"
31 #include "content/public/common/content_switches.h"
32 #include "content/public/common/url_constants.h"
33 #include "content/public/common/user_agent.h"
34 #include "extensions/common/constants.h"
35 #include "gpu/config/gpu_info.h"
36 #include "net/http/http_util.h"
37 #include "ui/base/l10n/l10n_util.h"
38 #include "ui/base/layout.h"
39 #include "ui/base/resource/resource_bundle.h"
41 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
43 #if defined(OS_WIN)
44 #include "base/win/registry.h"
45 #include "base/win/windows_version.h"
46 #endif
48 #if !defined(DISABLE_NACL)
49 #include "components/nacl/common/nacl_constants.h"
50 #include "components/nacl/common/nacl_process_type.h"
51 #include "components/nacl/common/nacl_sandbox_type.h"
52 #endif
54 #if defined(ENABLE_PLUGINS)
55 #include "content/public/common/pepper_plugin_info.h"
56 #include "flapper_version.h" // In SHARED_INTERMEDIATE_DIR.
57 #include "ppapi/shared_impl/ppapi_permissions.h"
58 #endif
60 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) && \
61 !defined(WIDEVINE_CDM_IS_COMPONENT)
62 #include "chrome/common/widevine_cdm_constants.h"
63 #endif
65 namespace {
67 #if defined(ENABLE_PLUGINS)
68 const char kPDFPluginExtension[] = "pdf";
69 const char kPDFPluginDescription[] = "Portable Document Format";
70 const char kPDFPluginOutOfProcessMimeType[] =
71 "application/x-google-chrome-pdf";
72 const uint32 kPDFPluginPermissions = ppapi::PERMISSION_PRIVATE |
73 ppapi::PERMISSION_DEV;
75 content::PepperPluginInfo::GetInterfaceFunc g_pdf_get_interface;
76 content::PepperPluginInfo::PPP_InitializeModuleFunc g_pdf_initialize_module;
77 content::PepperPluginInfo::PPP_ShutdownModuleFunc g_pdf_shutdown_module;
79 #if !defined(DISABLE_NACL)
80 content::PepperPluginInfo::GetInterfaceFunc g_nacl_get_interface;
81 content::PepperPluginInfo::PPP_InitializeModuleFunc g_nacl_initialize_module;
82 content::PepperPluginInfo::PPP_ShutdownModuleFunc g_nacl_shutdown_module;
83 #endif
85 // Appends the known built-in plugins to the given vector. Some built-in
86 // plugins are "internal" which means they are compiled into the Chrome binary,
87 // and some are extra shared libraries distributed with the browser (these are
88 // not marked internal, aside from being automatically registered, they're just
89 // regular plugins).
90 void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
91 content::PepperPluginInfo pdf_info;
92 pdf_info.is_internal = true;
93 pdf_info.is_out_of_process = true;
94 pdf_info.name = ChromeContentClient::kPDFPluginName;
95 pdf_info.description = kPDFPluginDescription;
96 pdf_info.path = base::FilePath::FromUTF8Unsafe(
97 ChromeContentClient::kPDFPluginPath);
98 content::WebPluginMimeType pdf_mime_type(
99 kPDFPluginOutOfProcessMimeType,
100 kPDFPluginExtension,
101 kPDFPluginDescription);
102 pdf_info.mime_types.push_back(pdf_mime_type);
103 pdf_info.internal_entry_points.get_interface = g_pdf_get_interface;
104 pdf_info.internal_entry_points.initialize_module = g_pdf_initialize_module;
105 pdf_info.internal_entry_points.shutdown_module = g_pdf_shutdown_module;
106 pdf_info.permissions = kPDFPluginPermissions;
107 plugins->push_back(pdf_info);
109 base::FilePath path;
111 #if !defined(DISABLE_NACL)
112 // Handle Native Client just like the PDF plugin. This means that it is
113 // enabled by default for the non-portable case. This allows apps installed
114 // from the Chrome Web Store to use NaCl even if the command line switch
115 // isn't set. For other uses of NaCl we check for the command line switch.
116 if (PathService::Get(chrome::FILE_NACL_PLUGIN, &path)) {
117 content::PepperPluginInfo nacl;
118 // The nacl plugin is now built into the Chromium binary.
119 nacl.is_internal = true;
120 nacl.path = path;
121 nacl.name = nacl::kNaClPluginName;
122 content::WebPluginMimeType nacl_mime_type(nacl::kNaClPluginMimeType,
123 nacl::kNaClPluginExtension,
124 nacl::kNaClPluginDescription);
125 nacl.mime_types.push_back(nacl_mime_type);
126 content::WebPluginMimeType pnacl_mime_type(nacl::kPnaclPluginMimeType,
127 nacl::kPnaclPluginExtension,
128 nacl::kPnaclPluginDescription);
129 nacl.mime_types.push_back(pnacl_mime_type);
130 nacl.internal_entry_points.get_interface = g_nacl_get_interface;
131 nacl.internal_entry_points.initialize_module = g_nacl_initialize_module;
132 nacl.internal_entry_points.shutdown_module = g_nacl_shutdown_module;
133 nacl.permissions = ppapi::PERMISSION_PRIVATE | ppapi::PERMISSION_DEV;
134 plugins->push_back(nacl);
136 #endif // !defined(DISABLE_NACL)
138 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) && \
139 !defined(WIDEVINE_CDM_IS_COMPONENT)
140 static bool skip_widevine_cdm_file_check = false;
141 if (PathService::Get(chrome::FILE_WIDEVINE_CDM_ADAPTER, &path)) {
142 if (skip_widevine_cdm_file_check || base::PathExists(path)) {
143 content::PepperPluginInfo widevine_cdm;
144 widevine_cdm.is_out_of_process = true;
145 widevine_cdm.path = path;
146 widevine_cdm.name = kWidevineCdmDisplayName;
147 widevine_cdm.description = kWidevineCdmDescription +
148 std::string(" (version: ") +
149 WIDEVINE_CDM_VERSION_STRING + ")";
150 widevine_cdm.version = WIDEVINE_CDM_VERSION_STRING;
151 content::WebPluginMimeType widevine_cdm_mime_type(
152 kWidevineCdmPluginMimeType,
153 kWidevineCdmPluginExtension,
154 kWidevineCdmPluginMimeTypeDescription);
156 // Add the supported codecs as if they came from the component manifest.
157 std::vector<std::string> codecs;
158 codecs.push_back(kCdmSupportedCodecVorbis);
159 codecs.push_back(kCdmSupportedCodecVp8);
160 codecs.push_back(kCdmSupportedCodecVp9);
161 #if defined(USE_PROPRIETARY_CODECS)
162 codecs.push_back(kCdmSupportedCodecAac);
163 codecs.push_back(kCdmSupportedCodecAvc1);
164 #endif // defined(USE_PROPRIETARY_CODECS)
165 std::string codec_string = base::JoinString(
166 codecs, std::string(1, kCdmSupportedCodecsValueDelimiter));
167 widevine_cdm_mime_type.additional_param_names.push_back(
168 base::ASCIIToUTF16(kCdmSupportedCodecsParamName));
169 widevine_cdm_mime_type.additional_param_values.push_back(
170 base::ASCIIToUTF16(codec_string));
172 widevine_cdm.mime_types.push_back(widevine_cdm_mime_type);
173 widevine_cdm.permissions = kWidevineCdmPluginPermissions;
174 plugins->push_back(widevine_cdm);
176 skip_widevine_cdm_file_check = true;
179 #endif // defined(WIDEVINE_CDM_AVAILABLE) && defined(ENABLE_PEPPER_CDMS) &&
180 // !defined(WIDEVINE_CDM_IS_COMPONENT)
183 content::PepperPluginInfo CreatePepperFlashInfo(const base::FilePath& path,
184 const std::string& version) {
185 content::PepperPluginInfo plugin;
187 plugin.is_out_of_process = true;
188 plugin.name = content::kFlashPluginName;
189 plugin.path = path;
190 plugin.permissions = chrome::kPepperFlashPermissions;
192 std::vector<std::string> flash_version_numbers = base::SplitString(
193 version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
194 if (flash_version_numbers.size() < 1)
195 flash_version_numbers.push_back("11");
196 if (flash_version_numbers.size() < 2)
197 flash_version_numbers.push_back("2");
198 if (flash_version_numbers.size() < 3)
199 flash_version_numbers.push_back("999");
200 if (flash_version_numbers.size() < 4)
201 flash_version_numbers.push_back("999");
202 // E.g., "Shockwave Flash 10.2 r154":
203 plugin.description = plugin.name + " " + flash_version_numbers[0] + "." +
204 flash_version_numbers[1] + " r" + flash_version_numbers[2];
205 plugin.version = base::JoinString(flash_version_numbers, ".");
206 content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
207 content::kFlashPluginSwfExtension,
208 content::kFlashPluginSwfDescription);
209 plugin.mime_types.push_back(swf_mime_type);
210 content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
211 content::kFlashPluginSplExtension,
212 content::kFlashPluginSplDescription);
213 plugin.mime_types.push_back(spl_mime_type);
215 return plugin;
218 void AddPepperFlashFromCommandLine(
219 std::vector<content::PepperPluginInfo>* plugins) {
220 const base::CommandLine::StringType flash_path =
221 base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
222 switches::kPpapiFlashPath);
223 if (flash_path.empty())
224 return;
226 // Also get the version from the command-line. Should be something like 11.2
227 // or 11.2.123.45.
228 std::string flash_version =
229 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
230 switches::kPpapiFlashVersion);
232 plugins->push_back(
233 CreatePepperFlashInfo(base::FilePath(flash_path), flash_version));
236 bool GetBundledPepperFlash(content::PepperPluginInfo* plugin) {
237 #if defined(FLAPPER_AVAILABLE)
238 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
240 // Ignore bundled Pepper Flash if there is Pepper Flash specified from the
241 // command-line.
242 if (command_line->HasSwitch(switches::kPpapiFlashPath))
243 return false;
245 bool force_disable =
246 command_line->HasSwitch(switches::kDisableBundledPpapiFlash);
247 if (force_disable)
248 return false;
250 base::FilePath flash_path;
251 if (!PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &flash_path))
252 return false;
254 *plugin = CreatePepperFlashInfo(flash_path, FLAPPER_VERSION_STRING);
255 return true;
256 #else
257 return false;
258 #endif // FLAPPER_AVAILABLE
261 #if defined(FLAPPER_AVAILABLE)
262 bool IsSystemFlashScriptDebuggerPresent() {
263 #if defined(OS_WIN)
264 const wchar_t kFlashRegistryRoot[] =
265 L"SOFTWARE\\Macromedia\\FlashPlayerPepper";
266 const wchar_t kIsDebuggerValueName[] = L"isScriptDebugger";
268 base::win::RegKey path_key(HKEY_LOCAL_MACHINE, kFlashRegistryRoot, KEY_READ);
269 DWORD debug_value;
270 if (FAILED(path_key.ReadValueDW(kIsDebuggerValueName, &debug_value)))
271 return false;
273 return (debug_value == 1);
274 #else
275 // TODO(wfh): implement this on OS X and Linux. crbug.com/497996.
276 return false;
277 #endif
279 #endif
281 bool GetSystemPepperFlash(content::PepperPluginInfo* plugin) {
282 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
284 #if defined(FLAPPER_AVAILABLE)
285 // If flapper is available, only try the system plugin if either:
286 // --disable-bundled-ppapi-flash is specified, or the system debugger is the
287 // Flash Script Debugger.
288 if (!command_line->HasSwitch(switches::kDisableBundledPpapiFlash) &&
289 !IsSystemFlashScriptDebuggerPresent())
290 return false;
291 #endif // defined(FLAPPER_AVAILABLE)
293 // Do not try and find System Pepper Flash if there is a specific path on
294 // the commmand-line.
295 if (command_line->HasSwitch(switches::kPpapiFlashPath))
296 return false;
298 base::FilePath flash_filename;
299 if (!PathService::Get(chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN,
300 &flash_filename))
301 return false;
303 if (!base::PathExists(flash_filename))
304 return false;
306 base::FilePath manifest_path(
307 flash_filename.DirName().AppendASCII("manifest.json"));
309 std::string manifest_data;
310 if (!base::ReadFileToString(manifest_path, &manifest_data))
311 return false;
312 scoped_ptr<base::Value> manifest_value(
313 base::JSONReader::Read(manifest_data, base::JSON_ALLOW_TRAILING_COMMAS));
314 if (!manifest_value.get())
315 return false;
316 base::DictionaryValue* manifest = NULL;
317 if (!manifest_value->GetAsDictionary(&manifest))
318 return false;
320 Version version;
321 if (!chrome::CheckPepperFlashManifest(*manifest, &version))
322 return false;
324 *plugin = CreatePepperFlashInfo(flash_filename, version.GetString());
325 return true;
327 #endif // defined(ENABLE_PLUGINS)
329 std::string GetProduct() {
330 return version_info::GetProductNameAndVersionForUserAgent();
333 } // namespace
335 std::string GetUserAgent() {
336 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
337 if (command_line->HasSwitch(switches::kUserAgent)) {
338 std::string ua = command_line->GetSwitchValueASCII(switches::kUserAgent);
339 if (net::HttpUtil::IsValidHeaderValue(ua))
340 return ua;
341 LOG(WARNING) << "Ignored invalid value for flag --" << switches::kUserAgent;
344 std::string product = GetProduct();
345 #if defined(OS_ANDROID)
346 if (command_line->HasSwitch(switches::kUseMobileUserAgent))
347 product += " Mobile";
348 #endif
349 return content::BuildUserAgentFromProduct(product);
352 #if !defined(DISABLE_NACL)
353 void ChromeContentClient::SetNaClEntryFunctions(
354 content::PepperPluginInfo::GetInterfaceFunc get_interface,
355 content::PepperPluginInfo::PPP_InitializeModuleFunc initialize_module,
356 content::PepperPluginInfo::PPP_ShutdownModuleFunc shutdown_module) {
357 g_nacl_get_interface = get_interface;
358 g_nacl_initialize_module = initialize_module;
359 g_nacl_shutdown_module = shutdown_module;
361 #endif
363 #if defined(ENABLE_PLUGINS)
364 void ChromeContentClient::SetPDFEntryFunctions(
365 content::PepperPluginInfo::GetInterfaceFunc get_interface,
366 content::PepperPluginInfo::PPP_InitializeModuleFunc initialize_module,
367 content::PepperPluginInfo::PPP_ShutdownModuleFunc shutdown_module) {
368 g_pdf_get_interface = get_interface;
369 g_pdf_initialize_module = initialize_module;
370 g_pdf_shutdown_module = shutdown_module;
372 #endif
374 void ChromeContentClient::SetActiveURL(const GURL& url) {
375 base::debug::SetCrashKeyValue(crash_keys::kActiveURL,
376 url.possibly_invalid_spec());
379 void ChromeContentClient::SetGpuInfo(const gpu::GPUInfo& gpu_info) {
380 #if !defined(OS_ANDROID)
381 base::debug::SetCrashKeyValue(crash_keys::kGPUVendorID,
382 base::StringPrintf("0x%04x", gpu_info.gpu.vendor_id));
383 base::debug::SetCrashKeyValue(crash_keys::kGPUDeviceID,
384 base::StringPrintf("0x%04x", gpu_info.gpu.device_id));
385 #endif
386 base::debug::SetCrashKeyValue(crash_keys::kGPUDriverVersion,
387 gpu_info.driver_version);
388 base::debug::SetCrashKeyValue(crash_keys::kGPUPixelShaderVersion,
389 gpu_info.pixel_shader_version);
390 base::debug::SetCrashKeyValue(crash_keys::kGPUVertexShaderVersion,
391 gpu_info.vertex_shader_version);
392 #if defined(OS_MACOSX)
393 base::debug::SetCrashKeyValue(crash_keys::kGPUGLVersion, gpu_info.gl_version);
394 #elif defined(OS_POSIX)
395 base::debug::SetCrashKeyValue(crash_keys::kGPUVendor, gpu_info.gl_vendor);
396 base::debug::SetCrashKeyValue(crash_keys::kGPURenderer, gpu_info.gl_renderer);
397 #endif
400 void ChromeContentClient::AddPepperPlugins(
401 std::vector<content::PepperPluginInfo>* plugins) {
402 #if defined(ENABLE_PLUGINS)
403 ComputeBuiltInPlugins(plugins);
404 AddPepperFlashFromCommandLine(plugins);
406 content::PepperPluginInfo plugin;
407 if (GetBundledPepperFlash(&plugin))
408 plugins->push_back(plugin);
409 if (GetSystemPepperFlash(&plugin))
410 plugins->push_back(plugin);
411 #endif
414 void ChromeContentClient::AddAdditionalSchemes(
415 std::vector<std::string>* standard_schemes,
416 std::vector<std::string>* savable_schemes) {
417 standard_schemes->push_back(extensions::kExtensionScheme);
418 savable_schemes->push_back(extensions::kExtensionScheme);
419 standard_schemes->push_back(chrome::kChromeNativeScheme);
420 standard_schemes->push_back(extensions::kExtensionResourceScheme);
421 savable_schemes->push_back(extensions::kExtensionResourceScheme);
422 standard_schemes->push_back(chrome::kChromeSearchScheme);
423 savable_schemes->push_back(chrome::kChromeSearchScheme);
424 standard_schemes->push_back(dom_distiller::kDomDistillerScheme);
425 savable_schemes->push_back(dom_distiller::kDomDistillerScheme);
426 #if defined(OS_CHROMEOS)
427 standard_schemes->push_back(chrome::kCrosScheme);
428 #endif
431 std::string ChromeContentClient::GetProduct() const {
432 return ::GetProduct();
435 std::string ChromeContentClient::GetUserAgent() const {
436 return ::GetUserAgent();
439 base::string16 ChromeContentClient::GetLocalizedString(int message_id) const {
440 return l10n_util::GetStringUTF16(message_id);
443 base::StringPiece ChromeContentClient::GetDataResource(
444 int resource_id,
445 ui::ScaleFactor scale_factor) const {
446 return ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
447 resource_id, scale_factor);
450 base::RefCountedStaticMemory* ChromeContentClient::GetDataResourceBytes(
451 int resource_id) const {
452 return ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id);
455 gfx::Image& ChromeContentClient::GetNativeImageNamed(int resource_id) const {
456 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
459 std::string ChromeContentClient::GetProcessTypeNameInEnglish(int type) {
460 #if !defined(DISABLE_NACL)
461 switch (type) {
462 case PROCESS_TYPE_NACL_LOADER:
463 return "Native Client module";
464 case PROCESS_TYPE_NACL_BROKER:
465 return "Native Client broker";
467 #endif
469 NOTREACHED() << "Unknown child process type!";
470 return "Unknown";
473 #if defined(OS_MACOSX) && !defined(OS_IOS)
474 bool ChromeContentClient::GetSandboxProfileForSandboxType(
475 int sandbox_type,
476 int* sandbox_profile_resource_id) const {
477 DCHECK(sandbox_profile_resource_id);
478 #if !defined(DISABLE_NACL)
479 if (sandbox_type == NACL_SANDBOX_TYPE_NACL_LOADER) {
480 *sandbox_profile_resource_id = IDR_NACL_SANDBOX_PROFILE;
481 return true;
483 #endif
484 return false;
486 #endif
488 void ChromeContentClient::AddSecureSchemesAndOrigins(
489 std::set<std::string>* schemes,
490 std::set<GURL>* origins) {
491 schemes->insert(chrome::kChromeSearchScheme);
492 schemes->insert(content::kChromeUIScheme);
493 schemes->insert(extensions::kExtensionScheme);
494 schemes->insert(extensions::kExtensionResourceScheme);
495 GetSecureOriginWhitelist(origins);
498 void ChromeContentClient::AddServiceWorkerSchemes(
499 std::set<std::string>* schemes) {
500 schemes->insert(extensions::kExtensionScheme);