electron_content_client.cc 9.2 KB

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