electron_content_client.cc 10 KB

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