electron_content_client.cc 9.1 KB

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