electron_content_client.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "shell/app/electron_content_client.h"
  5. #include <string>
  6. #include <string_view>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/command_line.h"
  10. #include "base/files/file_util.h"
  11. #include "base/strings/string_split.h"
  12. #include "content/public/common/content_constants.h"
  13. #include "electron/buildflags/buildflags.h"
  14. #include "electron/fuses.h"
  15. #include "extensions/common/constants.h"
  16. #include "pdf/buildflags.h"
  17. #include "ppapi/buildflags/buildflags.h"
  18. #include "shell/common/options_switches.h"
  19. #include "shell/common/process_util.h"
  20. #include "third_party/widevine/cdm/buildflags.h"
  21. #include "ui/base/l10n/l10n_util.h"
  22. #include "ui/base/resource/resource_bundle.h"
  23. #include "url/url_constants.h"
  24. // In SHARED_INTERMEDIATE_DIR.
  25. #include "widevine_cdm_version.h" // NOLINT(build/include_directory)
  26. #if BUILDFLAG(ENABLE_WIDEVINE)
  27. #include "base/native_library.h"
  28. #include "content/public/common/cdm_info.h"
  29. #include "media/base/video_codecs.h"
  30. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  31. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  32. #include "components/pdf/common/constants.h" // nogncheck
  33. #include "pdf/pdf.h" // nogncheck
  34. #include "shell/common/electron_constants.h"
  35. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  36. #if BUILDFLAG(ENABLE_PLUGINS)
  37. #include "content/public/common/content_plugin_info.h"
  38. #endif // BUILDFLAG(ENABLE_PLUGINS)
  39. namespace electron {
  40. namespace {
  41. enum class WidevineCdmFileCheck {
  42. kNotChecked,
  43. kFound,
  44. kNotFound,
  45. };
  46. #if BUILDFLAG(ENABLE_WIDEVINE)
  47. bool IsWidevineAvailable(
  48. base::FilePath* cdm_path,
  49. std::vector<media::VideoCodec>* codecs_supported,
  50. base::flat_set<media::CdmSessionType>* session_types_supported,
  51. base::flat_set<media::EncryptionMode>* modes_supported) {
  52. static WidevineCdmFileCheck widevine_cdm_file_check =
  53. WidevineCdmFileCheck::kNotChecked;
  54. if (widevine_cdm_file_check == WidevineCdmFileCheck::kNotChecked) {
  55. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  56. *cdm_path = command_line->GetSwitchValuePath(switches::kWidevineCdmPath);
  57. if (!cdm_path->empty()) {
  58. *cdm_path = cdm_path->AppendASCII(
  59. base::GetNativeLibraryName(kWidevineCdmLibraryName));
  60. widevine_cdm_file_check = base::PathExists(*cdm_path)
  61. ? WidevineCdmFileCheck::kFound
  62. : WidevineCdmFileCheck::kNotFound;
  63. }
  64. }
  65. if (widevine_cdm_file_check == WidevineCdmFileCheck::kFound) {
  66. // Add the supported codecs as if they came from the component manifest.
  67. // This list must match the CDM that is being bundled with Chrome.
  68. codecs_supported->push_back(media::VideoCodec::kCodecVP8);
  69. codecs_supported->push_back(media::VideoCodec::kCodecVP9);
  70. #if BUILDFLAG(USE_PROPRIETARY_CODECS)
  71. codecs_supported->push_back(media::VideoCodec::kCodecH264);
  72. #endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
  73. // TODO(crbug.com/767941): Push persistent-license support info here once
  74. // we check in a new CDM that supports it on Linux.
  75. session_types_supported->insert(media::CdmSessionType::kTemporary);
  76. #if BUILDFLAG(IS_CHROMEOS)
  77. session_types_supported->insert(media::CdmSessionType::kPersistentLicense);
  78. #endif // BUILDFLAG(IS_CHROMEOS)
  79. modes_supported->insert(media::EncryptionMode::kCenc);
  80. return true;
  81. }
  82. return false;
  83. }
  84. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  85. void AppendDelimitedSwitchToVector(const std::string_view cmd_switch,
  86. std::vector<std::string>* append_me) {
  87. auto* command_line = base::CommandLine::ForCurrentProcess();
  88. auto switch_value = command_line->GetSwitchValueASCII(cmd_switch);
  89. if (!switch_value.empty()) {
  90. constexpr std::string_view delimiter{",", 1};
  91. auto tokens =
  92. base::SplitString(switch_value, delimiter, base::TRIM_WHITESPACE,
  93. base::SPLIT_WANT_NONEMPTY);
  94. append_me->reserve(append_me->size() + tokens.size());
  95. std::move(std::begin(tokens), std::end(tokens),
  96. std::back_inserter(*append_me));
  97. }
  98. }
  99. } // namespace
  100. ElectronContentClient::ElectronContentClient() = default;
  101. ElectronContentClient::~ElectronContentClient() = default;
  102. std::u16string ElectronContentClient::GetLocalizedString(int message_id) {
  103. return l10n_util::GetStringUTF16(message_id);
  104. }
  105. std::string_view ElectronContentClient::GetDataResource(
  106. int resource_id,
  107. ui::ResourceScaleFactor scale_factor) {
  108. return ui::ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
  109. resource_id, scale_factor);
  110. }
  111. gfx::Image& ElectronContentClient::GetNativeImageNamed(int resource_id) {
  112. return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
  113. resource_id);
  114. }
  115. base::RefCountedMemory* ElectronContentClient::GetDataResourceBytes(
  116. int resource_id) {
  117. return ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
  118. resource_id);
  119. }
  120. void ElectronContentClient::AddAdditionalSchemes(Schemes* schemes) {
  121. // Browser Process registration happens in
  122. // `api::Protocol::RegisterSchemesAsPrivileged`
  123. //
  124. // Renderer Process registration happens in `RendererClientBase`
  125. //
  126. // We use this for registration to network utility process
  127. if (IsUtilityProcess()) {
  128. AppendDelimitedSwitchToVector(switches::kServiceWorkerSchemes,
  129. &schemes->service_worker_schemes);
  130. AppendDelimitedSwitchToVector(switches::kStandardSchemes,
  131. &schemes->standard_schemes);
  132. AppendDelimitedSwitchToVector(switches::kSecureSchemes,
  133. &schemes->secure_schemes);
  134. AppendDelimitedSwitchToVector(switches::kBypassCSPSchemes,
  135. &schemes->csp_bypassing_schemes);
  136. AppendDelimitedSwitchToVector(switches::kCORSSchemes,
  137. &schemes->cors_enabled_schemes);
  138. }
  139. if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
  140. schemes->service_worker_schemes.emplace_back(url::kFileScheme);
  141. }
  142. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  143. schemes->standard_schemes.push_back(extensions::kExtensionScheme);
  144. schemes->savable_schemes.push_back(extensions::kExtensionScheme);
  145. schemes->secure_schemes.push_back(extensions::kExtensionScheme);
  146. schemes->service_worker_schemes.push_back(extensions::kExtensionScheme);
  147. schemes->cors_enabled_schemes.push_back(extensions::kExtensionScheme);
  148. schemes->csp_bypassing_schemes.push_back(extensions::kExtensionScheme);
  149. #endif
  150. }
  151. void ElectronContentClient::AddPlugins(
  152. std::vector<content::ContentPluginInfo>* plugins) {
  153. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  154. static constexpr char kPDFPluginExtension[] = "pdf";
  155. static constexpr char kPDFPluginDescription[] = "Portable Document Format";
  156. content::ContentPluginInfo pdf_info;
  157. pdf_info.is_internal = true;
  158. pdf_info.is_out_of_process = true;
  159. pdf_info.name = kPDFInternalPluginName;
  160. pdf_info.description = kPDFPluginDescription;
  161. // This isn't a real file path; it's just used as a unique identifier.
  162. pdf_info.path = base::FilePath(kPdfPluginPath);
  163. content::WebPluginMimeType pdf_mime_type(
  164. pdf::kInternalPluginMimeType, kPDFPluginExtension, kPDFPluginDescription);
  165. pdf_info.mime_types.push_back(pdf_mime_type);
  166. plugins->push_back(pdf_info);
  167. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  168. }
  169. void ElectronContentClient::AddContentDecryptionModules(
  170. std::vector<content::CdmInfo>* cdms,
  171. std::vector<media::CdmHostFilePath>* cdm_host_file_paths) {
  172. if (cdms) {
  173. #if BUILDFLAG(ENABLE_WIDEVINE)
  174. base::FilePath cdm_path;
  175. std::vector<media::VideoCodec> video_codecs_supported;
  176. base::flat_set<media::CdmSessionType> session_types_supported;
  177. base::flat_set<media::EncryptionMode> encryption_modes_supported;
  178. if (IsWidevineAvailable(&cdm_path, &video_codecs_supported,
  179. &session_types_supported,
  180. &encryption_modes_supported)) {
  181. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  182. auto cdm_version_string =
  183. command_line->GetSwitchValueASCII(switches::kWidevineCdmVersion);
  184. // CdmInfo needs |path| to be the actual Widevine library,
  185. // not the adapter, so adjust as necessary. It will be in the
  186. // same directory as the installed adapter.
  187. const base::Version version(cdm_version_string);
  188. DCHECK(version.IsValid());
  189. content::CdmCapability capability(
  190. video_codecs_supported, encryption_modes_supported,
  191. session_types_supported, base::flat_set<media::CdmProxy::Protocol>());
  192. cdms->push_back(content::CdmInfo(
  193. kWidevineCdmDisplayName, kWidevineCdmGuid, version, cdm_path,
  194. kWidevineCdmFileSystemId, capability, kWidevineKeySystem, false));
  195. }
  196. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  197. }
  198. }
  199. } // namespace electron