atom_content_client.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/app/atom_content_client.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/common/atom_version.h"
  8. #include "atom/common/chrome_version.h"
  9. #include "atom/common/options_switches.h"
  10. #include "base/command_line.h"
  11. #include "base/files/file_util.h"
  12. #include "base/strings/string_split.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "content/public/common/content_constants.h"
  16. #include "content/public/common/pepper_plugin_info.h"
  17. #include "content/public/common/user_agent.h"
  18. #include "electron/buildflags/buildflags.h"
  19. #include "ppapi/shared_impl/ppapi_permissions.h"
  20. #include "ui/base/l10n/l10n_util.h"
  21. #include "url/url_constants.h"
  22. // In SHARED_INTERMEDIATE_DIR.
  23. #include "widevine_cdm_version.h" // NOLINT(build/include)
  24. #if defined(WIDEVINE_CDM_AVAILABLE)
  25. #include "base/native_library.h"
  26. #include "content/public/common/cdm_info.h"
  27. #include "media/base/video_codecs.h"
  28. #endif // defined(WIDEVINE_CDM_AVAILABLE)
  29. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  30. #include "atom/common/atom_constants.h"
  31. #include "pdf/pdf.h"
  32. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  33. namespace atom {
  34. namespace {
  35. #if defined(WIDEVINE_CDM_AVAILABLE)
  36. bool IsWidevineAvailable(
  37. base::FilePath* cdm_path,
  38. std::vector<media::VideoCodec>* codecs_supported,
  39. base::flat_set<media::CdmSessionType>* session_types_supported,
  40. base::flat_set<media::EncryptionMode>* modes_supported) {
  41. static enum {
  42. NOT_CHECKED,
  43. FOUND,
  44. NOT_FOUND,
  45. } widevine_cdm_file_check = NOT_CHECKED;
  46. if (widevine_cdm_file_check == NOT_CHECKED) {
  47. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  48. *cdm_path = command_line->GetSwitchValuePath(switches::kWidevineCdmPath);
  49. if (!cdm_path->empty()) {
  50. *cdm_path = cdm_path->AppendASCII(
  51. base::GetNativeLibraryName(kWidevineCdmLibraryName));
  52. widevine_cdm_file_check = base::PathExists(*cdm_path) ? FOUND : NOT_FOUND;
  53. }
  54. }
  55. if (widevine_cdm_file_check == FOUND) {
  56. // Add the supported codecs as if they came from the component manifest.
  57. // This list must match the CDM that is being bundled with Chrome.
  58. codecs_supported->push_back(media::VideoCodec::kCodecVP8);
  59. codecs_supported->push_back(media::VideoCodec::kCodecVP9);
  60. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  61. codecs_supported->push_back(media::VideoCodec::kCodecH264);
  62. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  63. // TODO(crbug.com/767941): Push persistent-license support info here once
  64. // we check in a new CDM that supports it on Linux.
  65. session_types_supported->insert(media::CdmSessionType::kTemporary);
  66. #if defined(OS_CHROMEOS)
  67. session_types_supported->insert(media::CdmSessionType::kPersistentLicense);
  68. #endif // defined(OS_CHROMEOS)
  69. modes_supported->insert(media::EncryptionMode::kCenc);
  70. return true;
  71. }
  72. return false;
  73. }
  74. #endif // defined(WIDEVINE_CDM_AVAILABLE)
  75. #if BUILDFLAG(ENABLE_PEPPER_FLASH)
  76. content::PepperPluginInfo CreatePepperFlashInfo(const base::FilePath& path,
  77. const std::string& version) {
  78. content::PepperPluginInfo plugin;
  79. plugin.is_out_of_process = true;
  80. plugin.name = content::kFlashPluginName;
  81. plugin.path = path;
  82. plugin.permissions = ppapi::PERMISSION_ALL_BITS;
  83. std::vector<std::string> flash_version_numbers = base::SplitString(
  84. version, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  85. if (flash_version_numbers.empty())
  86. flash_version_numbers.push_back("11");
  87. // |SplitString()| puts in an empty string given an empty string. :(
  88. else if (flash_version_numbers[0].empty())
  89. flash_version_numbers[0] = "11";
  90. if (flash_version_numbers.size() < 2)
  91. flash_version_numbers.push_back("2");
  92. if (flash_version_numbers.size() < 3)
  93. flash_version_numbers.push_back("999");
  94. if (flash_version_numbers.size() < 4)
  95. flash_version_numbers.push_back("999");
  96. // E.g., "Shockwave Flash 10.2 r154":
  97. plugin.description = plugin.name + " " + flash_version_numbers[0] + "." +
  98. flash_version_numbers[1] + " r" +
  99. flash_version_numbers[2];
  100. plugin.version = base::JoinString(flash_version_numbers, ".");
  101. content::WebPluginMimeType swf_mime_type(content::kFlashPluginSwfMimeType,
  102. content::kFlashPluginSwfExtension,
  103. content::kFlashPluginSwfDescription);
  104. plugin.mime_types.push_back(swf_mime_type);
  105. content::WebPluginMimeType spl_mime_type(content::kFlashPluginSplMimeType,
  106. content::kFlashPluginSplExtension,
  107. content::kFlashPluginSplDescription);
  108. plugin.mime_types.push_back(spl_mime_type);
  109. return plugin;
  110. }
  111. void AddPepperFlashFromCommandLine(
  112. base::CommandLine* command_line,
  113. std::vector<content::PepperPluginInfo>* plugins) {
  114. base::FilePath flash_path =
  115. command_line->GetSwitchValuePath(switches::kPpapiFlashPath);
  116. if (flash_path.empty())
  117. return;
  118. auto flash_version =
  119. command_line->GetSwitchValueASCII(switches::kPpapiFlashVersion);
  120. plugins->push_back(CreatePepperFlashInfo(flash_path, flash_version));
  121. }
  122. #endif // BUILDFLAG(ENABLE_PEPPER_FLASH)
  123. void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
  124. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  125. content::PepperPluginInfo pdf_info;
  126. pdf_info.is_internal = true;
  127. pdf_info.is_out_of_process = true;
  128. pdf_info.name = "Chromium PDF Viewer";
  129. pdf_info.description = "Portable Document Format";
  130. pdf_info.path = base::FilePath::FromUTF8Unsafe(kPdfPluginPath);
  131. content::WebPluginMimeType pdf_mime_type(kPdfPluginMimeType, "pdf",
  132. "Portable Document Format");
  133. pdf_info.mime_types.push_back(pdf_mime_type);
  134. pdf_info.internal_entry_points.get_interface = chrome_pdf::PPP_GetInterface;
  135. pdf_info.internal_entry_points.initialize_module =
  136. chrome_pdf::PPP_InitializeModule;
  137. pdf_info.internal_entry_points.shutdown_module =
  138. chrome_pdf::PPP_ShutdownModule;
  139. pdf_info.permissions = ppapi::PERMISSION_PRIVATE | ppapi::PERMISSION_DEV;
  140. plugins->push_back(pdf_info);
  141. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  142. }
  143. void ConvertStringWithSeparatorToVector(std::vector<std::string>* vec,
  144. const char* separator,
  145. const char* cmd_switch) {
  146. auto* command_line = base::CommandLine::ForCurrentProcess();
  147. auto string_with_separator = command_line->GetSwitchValueASCII(cmd_switch);
  148. if (!string_with_separator.empty())
  149. *vec = base::SplitString(string_with_separator, separator,
  150. base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  151. }
  152. } // namespace
  153. AtomContentClient::AtomContentClient() {}
  154. AtomContentClient::~AtomContentClient() {}
  155. std::string AtomContentClient::GetProduct() const {
  156. return "Chrome/" CHROME_VERSION_STRING;
  157. }
  158. std::string AtomContentClient::GetUserAgent() const {
  159. return content::BuildUserAgentFromProduct("Chrome/" CHROME_VERSION_STRING
  160. " " ATOM_PRODUCT_NAME
  161. "/" ATOM_VERSION_STRING);
  162. }
  163. base::string16 AtomContentClient::GetLocalizedString(int message_id) const {
  164. return l10n_util::GetStringUTF16(message_id);
  165. }
  166. void AtomContentClient::AddAdditionalSchemes(Schemes* schemes) {
  167. schemes->standard_schemes.push_back("chrome-extension");
  168. std::vector<std::string> splited;
  169. ConvertStringWithSeparatorToVector(&splited, ",",
  170. switches::kRegisterServiceWorkerSchemes);
  171. for (const std::string& scheme : splited)
  172. schemes->service_worker_schemes.push_back(scheme);
  173. schemes->service_worker_schemes.push_back(url::kFileScheme);
  174. ConvertStringWithSeparatorToVector(&splited, ",", switches::kSecureSchemes);
  175. for (const std::string& scheme : splited)
  176. schemes->secure_schemes.push_back(scheme);
  177. }
  178. void AtomContentClient::AddPepperPlugins(
  179. std::vector<content::PepperPluginInfo>* plugins) {
  180. #if BUILDFLAG(ENABLE_PEPPER_FLASH)
  181. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  182. AddPepperFlashFromCommandLine(command_line, plugins);
  183. #endif // BUILDFLAG(ENABLE_PEPPER_FLASH)
  184. ComputeBuiltInPlugins(plugins);
  185. }
  186. void AtomContentClient::AddContentDecryptionModules(
  187. std::vector<content::CdmInfo>* cdms,
  188. std::vector<media::CdmHostFilePath>* cdm_host_file_paths) {
  189. if (cdms) {
  190. #if defined(WIDEVINE_CDM_AVAILABLE)
  191. base::FilePath cdm_path;
  192. std::vector<media::VideoCodec> video_codecs_supported;
  193. base::flat_set<media::CdmSessionType> session_types_supported;
  194. base::flat_set<media::EncryptionMode> encryption_modes_supported;
  195. if (IsWidevineAvailable(&cdm_path, &video_codecs_supported,
  196. &session_types_supported,
  197. &encryption_modes_supported)) {
  198. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  199. auto cdm_version_string =
  200. command_line->GetSwitchValueASCII(switches::kWidevineCdmVersion);
  201. // CdmInfo needs |path| to be the actual Widevine library,
  202. // not the adapter, so adjust as necessary. It will be in the
  203. // same directory as the installed adapter.
  204. const base::Version version(cdm_version_string);
  205. DCHECK(version.IsValid());
  206. content::CdmCapability capability(
  207. video_codecs_supported, encryption_modes_supported,
  208. session_types_supported, base::flat_set<media::CdmProxy::Protocol>());
  209. cdms->push_back(content::CdmInfo(
  210. kWidevineCdmDisplayName, kWidevineCdmGuid, version, cdm_path,
  211. kWidevineCdmFileSystemId, capability, kWidevineKeySystem, false));
  212. }
  213. #endif // defined(WIDEVINE_CDM_AVAILABLE)
  214. }
  215. }
  216. } // namespace atom