atom_content_client.cc 11 KB

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