1 // Copyright 2015 The Chromium Embedded Framework Authors.
2 // Portions copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "libcef/common/alloy/alloy_content_client.h"
7
8 #include <stdint.h>
9
10 #include "include/cef_stream.h"
11 #include "include/cef_version.h"
12 #include "libcef/common/app_manager.h"
13 #include "libcef/common/cef_switches.h"
14 #include "libcef/common/extensions/extensions_util.h"
15
16 #include "base/command_line.h"
17 #include "base/files/file_util.h"
18 #include "base/json/json_reader.h"
19 #include "base/logging.h"
20 #include "base/path_service.h"
21 #include "base/strings/string_piece.h"
22 #include "base/strings/string_split.h"
23 #include "base/strings/string_util.h"
24 #include "base/strings/stringprintf.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_content_client.h"
28 #include "chrome/common/chrome_paths.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chrome/common/media/cdm_registration.h"
31 #include "components/pdf/common/internal_plugin_helpers.h"
32 #include "content/public/common/cdm_info.h"
33 #include "content/public/common/content_constants.h"
34 #include "content/public/common/content_switches.h"
35 #include "content/public/common/pepper_plugin_info.h"
36 #include "ppapi/shared_impl/ppapi_permissions.h"
37 #include "third_party/widevine/cdm/buildflags.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/resource/resource_bundle.h"
40
41 #if BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
42 #include "chrome/common/media/cdm_host_file_path.h"
43 #endif
44
45 namespace {
46
47 // The following plugin-related methods are from
48 // chrome/common/chrome_content_client.cc
49
50 const char kPDFPluginExtension[] = "pdf";
51 const char kPDFPluginDescription[] = "Portable Document Format";
52 const uint32_t kPDFPluginPermissions =
53 ppapi::PERMISSION_PDF | ppapi::PERMISSION_DEV;
54
55 content::PepperPluginInfo::GetInterfaceFunc g_pdf_get_interface;
56 content::PepperPluginInfo::PPP_InitializeModuleFunc g_pdf_initialize_module;
57 content::PepperPluginInfo::PPP_ShutdownModuleFunc g_pdf_shutdown_module;
58
59 // Appends the known built-in plugins to the given vector. Some built-in
60 // plugins are "internal" which means they are compiled into the Chrome binary,
61 // and some are extra shared libraries distributed with the browser (these are
62 // not marked internal, aside from being automatically registered, they're just
63 // regular plugins).
ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo> * plugins)64 void ComputeBuiltInPlugins(std::vector<content::PepperPluginInfo>* plugins) {
65 if (extensions::PdfExtensionEnabled()) {
66 content::PepperPluginInfo pdf_info;
67 pdf_info.is_internal = true;
68 pdf_info.is_out_of_process = true;
69 pdf_info.name = ChromeContentClient::kPDFInternalPluginName;
70 pdf_info.description = kPDFPluginDescription;
71 pdf_info.path = base::FilePath(ChromeContentClient::kPDFPluginPath);
72 content::WebPluginMimeType pdf_mime_type(pdf::kInternalPluginMimeType,
73 kPDFPluginExtension,
74 kPDFPluginDescription);
75 pdf_info.mime_types.push_back(pdf_mime_type);
76 pdf_info.internal_entry_points.get_interface = g_pdf_get_interface;
77 pdf_info.internal_entry_points.initialize_module = g_pdf_initialize_module;
78 pdf_info.internal_entry_points.shutdown_module = g_pdf_shutdown_module;
79 pdf_info.permissions = kPDFPluginPermissions;
80 plugins->push_back(pdf_info);
81 }
82 }
83
84 } // namespace
85
86 AlloyContentClient::AlloyContentClient() = default;
87 AlloyContentClient::~AlloyContentClient() = default;
88
AddPepperPlugins(std::vector<content::PepperPluginInfo> * plugins)89 void AlloyContentClient::AddPepperPlugins(
90 std::vector<content::PepperPluginInfo>* plugins) {
91 ComputeBuiltInPlugins(plugins);
92 }
93
AddContentDecryptionModules(std::vector<content::CdmInfo> * cdms,std::vector<media::CdmHostFilePath> * cdm_host_file_paths)94 void AlloyContentClient::AddContentDecryptionModules(
95 std::vector<content::CdmInfo>* cdms,
96 std::vector<media::CdmHostFilePath>* cdm_host_file_paths) {
97 if (cdms)
98 RegisterCdmInfo(cdms);
99
100 #if BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
101 if (cdm_host_file_paths)
102 AddCdmHostFilePaths(cdm_host_file_paths);
103 #endif
104 }
105
AddAdditionalSchemes(Schemes * schemes)106 void AlloyContentClient::AddAdditionalSchemes(Schemes* schemes) {
107 CefAppManager::Get()->AddAdditionalSchemes(schemes);
108 }
109
GetLocalizedString(int message_id)110 std::u16string AlloyContentClient::GetLocalizedString(int message_id) {
111 std::u16string value =
112 ui::ResourceBundle::GetSharedInstance().GetLocalizedString(message_id);
113 if (value.empty())
114 LOG(ERROR) << "No localized string available for id " << message_id;
115
116 return value;
117 }
118
GetLocalizedString(int message_id,const std::u16string & replacement)119 std::u16string AlloyContentClient::GetLocalizedString(
120 int message_id,
121 const std::u16string& replacement) {
122 std::u16string value = l10n_util::GetStringFUTF16(message_id, replacement);
123 if (value.empty())
124 LOG(ERROR) << "No localized string available for id " << message_id;
125
126 return value;
127 }
128
GetDataResource(int resource_id,ui::ResourceScaleFactor scale_factor)129 base::StringPiece AlloyContentClient::GetDataResource(
130 int resource_id,
131 ui::ResourceScaleFactor scale_factor) {
132 base::StringPiece value =
133 ui::ResourceBundle::GetSharedInstance().GetRawDataResourceForScale(
134 resource_id, scale_factor);
135 if (value.empty())
136 LOG(ERROR) << "No data resource available for id " << resource_id;
137
138 return value;
139 }
140
GetDataResourceBytes(int resource_id)141 base::RefCountedMemory* AlloyContentClient::GetDataResourceBytes(
142 int resource_id) {
143 base::RefCountedMemory* value =
144 ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
145 resource_id);
146 if (!value)
147 LOG(ERROR) << "No data resource bytes available for id " << resource_id;
148
149 return value;
150 }
151
GetNativeImageNamed(int resource_id)152 gfx::Image& AlloyContentClient::GetNativeImageNamed(int resource_id) {
153 gfx::Image& value =
154 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(resource_id);
155 if (value.IsEmpty())
156 LOG(ERROR) << "No native image available for id " << resource_id;
157
158 return value;
159 }
160
161 // static
SetPDFEntryFunctions(content::PepperPluginInfo::GetInterfaceFunc get_interface,content::PepperPluginInfo::PPP_InitializeModuleFunc initialize_module,content::PepperPluginInfo::PPP_ShutdownModuleFunc shutdown_module)162 void AlloyContentClient::SetPDFEntryFunctions(
163 content::PepperPluginInfo::GetInterfaceFunc get_interface,
164 content::PepperPluginInfo::PPP_InitializeModuleFunc initialize_module,
165 content::PepperPluginInfo::PPP_ShutdownModuleFunc shutdown_module) {
166 g_pdf_get_interface = get_interface;
167 g_pdf_initialize_module = initialize_module;
168 g_pdf_shutdown_module = shutdown_module;
169 }
170