atom_content_client.cc 10 KB

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