electron_content_client.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "third_party/widevine/cdm/buildflags.h"
  23. #include "ui/base/l10n/l10n_util.h"
  24. #include "ui/base/resource/resource_bundle.h"
  25. #include "url/url_constants.h"
  26. // In SHARED_INTERMEDIATE_DIR.
  27. #include "widevine_cdm_version.h" // NOLINT(build/include_directory)
  28. #if BUILDFLAG(ENABLE_WIDEVINE)
  29. #include "base/native_library.h"
  30. #include "content/public/common/cdm_info.h"
  31. #include "media/base/video_codecs.h"
  32. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  33. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  34. #include "chrome/common/pdf_util.h"
  35. #include "components/pdf/common/internal_plugin_helpers.h"
  36. #include "pdf/pdf.h" // nogncheck
  37. #include "shell/common/electron_constants.h"
  38. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  39. #if BUILDFLAG(ENABLE_PLUGINS)
  40. #include "content/public/browser/plugin_service.h"
  41. #include "content/public/common/pepper_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. #if BUILDFLAG(ENABLE_PLUGINS)
  92. void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
  93. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  94. // TODO(upstream/thestig): Figure out how to make the PDF Viewer work without
  95. // this PPAPI plugin registration.
  96. content::PepperPluginInfo pdf_info;
  97. pdf_info.is_internal = true;
  98. pdf_info.is_out_of_process = true;
  99. pdf_info.name = kPDFInternalPluginName;
  100. pdf_info.description = "Portable Document Format";
  101. // This isn't a real file path; it's just used as a unique identifier.
  102. pdf_info.path = base::FilePath(kPdfPluginPath);
  103. content::WebPluginMimeType pdf_mime_type(pdf::kInternalPluginMimeType, "pdf",
  104. "Portable Document Format");
  105. pdf_info.mime_types.push_back(pdf_mime_type);
  106. plugins->push_back(pdf_info);
  107. // NB. in Chrome, this plugin isn't registered until the PDF extension is
  108. // loaded. However, in Electron, we load the PDF extension unconditionally
  109. // when it is enabled in the build, so we're OK to load the plugin eagerly
  110. // here.
  111. content::WebPluginInfo info;
  112. info.type = content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
  113. info.name = base::ASCIIToUTF16(kPDFExtensionPluginName);
  114. // This isn't a real file path; it's just used as a unique identifier.
  115. info.path = base::FilePath::FromUTF8Unsafe(extension_misc::kPdfExtensionId);
  116. info.background_color = content::WebPluginInfo::kDefaultBackgroundColor;
  117. info.mime_types.emplace_back(kPDFMimeType, "pdf", "Portable Document Format");
  118. content::PluginService::GetInstance()->RefreshPlugins();
  119. content::PluginService::GetInstance()->RegisterInternalPlugin(info, true);
  120. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  121. }
  122. #endif // BUILDFLAG(ENABLE_PLUGINS)
  123. void AppendDelimitedSwitchToVector(const base::StringPiece cmd_switch,
  124. std::vector<std::string>* append_me) {
  125. auto* command_line = base::CommandLine::ForCurrentProcess();
  126. auto switch_value = command_line->GetSwitchValueASCII(cmd_switch);
  127. if (!switch_value.empty()) {
  128. constexpr base::StringPiece delimiter(",", 1);
  129. auto tokens =
  130. base::SplitString(switch_value, delimiter, base::TRIM_WHITESPACE,
  131. base::SPLIT_WANT_NONEMPTY);
  132. append_me->reserve(append_me->size() + tokens.size());
  133. std::move(std::begin(tokens), std::end(tokens),
  134. std::back_inserter(*append_me));
  135. }
  136. }
  137. } // namespace
  138. ElectronContentClient::ElectronContentClient() = default;
  139. ElectronContentClient::~ElectronContentClient() = default;
  140. std::u16string ElectronContentClient::GetLocalizedString(int message_id) {
  141. return l10n_util::GetStringUTF16(message_id);
  142. }
  143. base::StringPiece ElectronContentClient::GetDataResource(
  144. int resource_id,
  145. ui::ResourceScaleFactor scale_factor) {
  146. return ui::ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
  147. resource_id, scale_factor);
  148. }
  149. gfx::Image& ElectronContentClient::GetNativeImageNamed(int resource_id) {
  150. return ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
  151. resource_id);
  152. }
  153. base::RefCountedMemory* ElectronContentClient::GetDataResourceBytes(
  154. int resource_id) {
  155. return ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
  156. resource_id);
  157. }
  158. void ElectronContentClient::AddAdditionalSchemes(Schemes* schemes) {
  159. auto* command_line = base::CommandLine::ForCurrentProcess();
  160. std::string process_type =
  161. command_line->GetSwitchValueASCII(::switches::kProcessType);
  162. // Browser Process registration happens in
  163. // `api::Protocol::RegisterSchemesAsPrivileged`
  164. //
  165. // Renderer Process registration happens in `RendererClientBase`
  166. //
  167. // We use this for registration to network utility process
  168. if (process_type == ::switches::kUtilityProcess) {
  169. AppendDelimitedSwitchToVector(switches::kServiceWorkerSchemes,
  170. &schemes->service_worker_schemes);
  171. AppendDelimitedSwitchToVector(switches::kStandardSchemes,
  172. &schemes->standard_schemes);
  173. AppendDelimitedSwitchToVector(switches::kSecureSchemes,
  174. &schemes->secure_schemes);
  175. AppendDelimitedSwitchToVector(switches::kBypassCSPSchemes,
  176. &schemes->csp_bypassing_schemes);
  177. AppendDelimitedSwitchToVector(switches::kCORSSchemes,
  178. &schemes->cors_enabled_schemes);
  179. }
  180. schemes->service_worker_schemes.emplace_back(url::kFileScheme);
  181. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  182. schemes->standard_schemes.push_back(extensions::kExtensionScheme);
  183. schemes->savable_schemes.push_back(extensions::kExtensionScheme);
  184. schemes->secure_schemes.push_back(extensions::kExtensionScheme);
  185. schemes->service_worker_schemes.push_back(extensions::kExtensionScheme);
  186. schemes->cors_enabled_schemes.push_back(extensions::kExtensionScheme);
  187. schemes->csp_bypassing_schemes.push_back(extensions::kExtensionScheme);
  188. #endif
  189. }
  190. void ElectronContentClient::AddPepperPlugins(
  191. std::vector<content::PepperPluginInfo>* plugins) {
  192. #if BUILDFLAG(ENABLE_PLUGINS)
  193. ComputeBuiltInPlugins(plugins);
  194. #endif // BUILDFLAG(ENABLE_PLUGINS)
  195. }
  196. void ElectronContentClient::AddContentDecryptionModules(
  197. std::vector<content::CdmInfo>* cdms,
  198. std::vector<media::CdmHostFilePath>* cdm_host_file_paths) {
  199. if (cdms) {
  200. #if BUILDFLAG(ENABLE_WIDEVINE)
  201. base::FilePath cdm_path;
  202. std::vector<media::VideoCodec> video_codecs_supported;
  203. base::flat_set<media::CdmSessionType> session_types_supported;
  204. base::flat_set<media::EncryptionMode> encryption_modes_supported;
  205. if (IsWidevineAvailable(&cdm_path, &video_codecs_supported,
  206. &session_types_supported,
  207. &encryption_modes_supported)) {
  208. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  209. auto cdm_version_string =
  210. command_line->GetSwitchValueASCII(switches::kWidevineCdmVersion);
  211. // CdmInfo needs |path| to be the actual Widevine library,
  212. // not the adapter, so adjust as necessary. It will be in the
  213. // same directory as the installed adapter.
  214. const base::Version version(cdm_version_string);
  215. DCHECK(version.IsValid());
  216. content::CdmCapability capability(
  217. video_codecs_supported, encryption_modes_supported,
  218. session_types_supported, base::flat_set<media::CdmProxy::Protocol>());
  219. cdms->push_back(content::CdmInfo(
  220. kWidevineCdmDisplayName, kWidevineCdmGuid, version, cdm_path,
  221. kWidevineCdmFileSystemId, capability, kWidevineKeySystem, false));
  222. }
  223. #endif // BUILDFLAG(ENABLE_WIDEVINE)
  224. }
  225. }
  226. } // namespace electron