electron_content_client.cc 10 KB

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