OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/media/cdm_host_files.h" | |
6 | |
7 #include <map> | |
8 #include <memory> | |
9 #include <vector> | |
10 | |
11 #include "base/command_line.h" | |
12 #include "base/files/file.h" | |
13 #include "base/files/file_path.h" | |
14 #include "base/lazy_instance.h" | |
15 #include "base/logging.h" | |
16 #include "base/memory/ptr_util.h" | |
17 #include "base/native_library.h" | |
18 #include "base/path_service.h" | |
19 #include "base/scoped_native_library.h" | |
20 #include "build/build_config.h" | |
21 #include "content/common/media/cdm_host_file.h" | |
22 #include "content/public/common/cdm_info.h" | |
23 #include "content/public/common/content_client.h" | |
24 #include "media/base/media_switches.h" | |
25 #include "media/cdm/api/content_decryption_module_ext.h" | |
26 #include "media/cdm/cdm_paths.h" | |
27 | |
28 #if defined(POSIX_WITH_ZYGOTE) | |
29 #include "content/common/pepper_plugin_list.h" | |
30 #include "content/public/common/pepper_plugin_info.h" | |
31 #endif | |
32 | |
33 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. | |
34 | |
35 namespace content { | |
36 | |
37 namespace { | |
38 | |
39 bool IgnoreMissingCdmHostFile() { | |
40 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
41 switches::kIgnoreMissingCdmHostFileForTesting); | |
42 } | |
43 | |
44 // TODO(xhwang): Move this to a common place if needed. | |
45 const base::FilePath::CharType kSignatureFileExtension[] = | |
46 FILE_PATH_LITERAL(".sig"); | |
47 | |
48 // Returns the signature file path given the |file_path|. This function should | |
49 // only be used when the signature file and the file are located in the same | |
50 // directory, which is the case for the CDM and CDM adapter. | |
51 base::FilePath GetSigFilePath(const base::FilePath& file_path) { | |
52 return file_path.AddExtension(kSignatureFileExtension); | |
53 } | |
54 | |
55 // Returns the CDM library file name given the |cdm_adapter_file_name|. Returns | |
56 // nullptr if |cdm_adapter_file_name| does not correspond to a known CDM. | |
57 const char* GetCdmFileName(const base::FilePath& cdm_adapter_file_name) { | |
58 #if defined(WIDEVINE_CDM_AVAILABLE) | |
59 if (cdm_adapter_file_name == | |
60 base::FilePath::FromUTF8Unsafe(kWidevineCdmAdapterFileName)) | |
61 return kWidevineCdmLibraryName; | |
62 #endif | |
63 | |
64 // Clear Key CDM. For test only. | |
65 if (cdm_adapter_file_name == | |
66 base::FilePath::FromUTF8Unsafe(media::kClearKeyCdmAdapterFileName)) | |
67 return media::kClearKeyCdmLibraryName; | |
68 | |
69 return nullptr; | |
70 } | |
71 | |
72 // Returns the path to the CDM binary given the |cdm_adapter_path|. Returns an | |
73 // empty path if |cdm_adapter_path| does not correspond to a known CDM. | |
74 base::FilePath GetCdmPath(const base::FilePath& cdm_adapter_path) { | |
75 const char* cdm_file_name = GetCdmFileName(cdm_adapter_path.BaseName()); | |
76 if (!cdm_file_name) | |
77 return base::FilePath(); | |
78 | |
79 return cdm_adapter_path.DirName().AppendASCII( | |
80 base::GetNativeLibraryName(cdm_file_name)); | |
81 } | |
82 | |
83 #if defined(POSIX_WITH_ZYGOTE) | |
84 // From the list of registered plugins, finds all registered CDMs and fills | |
85 // |cdm_adapter_paths| with found CDM adapters paths. | |
86 void GetRegisteredCdms(std::vector<base::FilePath>* cdm_adapter_paths) { | |
87 std::vector<PepperPluginInfo> plugins; | |
88 ComputePepperPluginList(&plugins); | |
89 for (const auto& plugin : plugins) { | |
90 // CDM is not an internal plugin. | |
91 if (plugin.is_internal) | |
92 continue; | |
93 | |
94 if (IsCdm(plugin.path)) | |
95 cdm_adapter_paths->push_back(plugin.path); | |
96 } | |
97 } | |
98 | |
99 // A global instance used on platforms where we have to open the files in the | |
100 // Zygote process. | |
101 base::LazyInstance<std::unique_ptr<CdmHostFiles>> g_cdm_host_files = | |
102 LAZY_INSTANCE_INITIALIZER; | |
103 #endif | |
104 | |
105 } // namespace | |
106 | |
107 CdmHostFiles::CdmHostFiles() { | |
108 DVLOG(1) << __func__; | |
109 } | |
110 | |
111 CdmHostFiles::~CdmHostFiles() { | |
112 DVLOG(1) << __func__; | |
113 } | |
114 | |
115 #if defined(POSIX_WITH_ZYGOTE) | |
116 // static | |
117 void CdmHostFiles::CreateGlobalInstance() { | |
118 DVLOG(1) << __func__; | |
119 DCHECK(!g_cdm_host_files.Get().get()); | |
120 | |
121 std::unique_ptr<CdmHostFiles> cdm_host_files = | |
122 base::MakeUnique<CdmHostFiles>(); | |
123 if (!cdm_host_files->OpenFilesForAllRegisteredCdms()) { | |
124 DVLOG(1) << __func__ << " failed."; | |
125 cdm_host_files.reset(); | |
126 return; | |
127 } | |
128 | |
129 g_cdm_host_files.Get().reset(cdm_host_files.release()); | |
130 } | |
131 | |
132 // static | |
133 std::unique_ptr<CdmHostFiles> CdmHostFiles::TakeGlobalInstance() { | |
134 return std::move(g_cdm_host_files.Get()); | |
135 } | |
136 #endif | |
137 | |
138 // static | |
139 std::unique_ptr<CdmHostFiles> CdmHostFiles::Create( | |
140 const base::FilePath& cdm_adapter_path) { | |
141 DVLOG(1) << __func__; | |
142 std::unique_ptr<CdmHostFiles> cdm_host_files = | |
143 base::MakeUnique<CdmHostFiles>(); | |
144 if (!cdm_host_files->OpenFiles(cdm_adapter_path)) { | |
145 cdm_host_files.reset(); | |
146 return nullptr; | |
147 } | |
148 | |
149 return cdm_host_files; | |
150 } | |
151 | |
152 void CdmHostFiles::VerifyFiles(base::NativeLibrary cdm_adapter_library, | |
153 const base::FilePath& cdm_adapter_path) { | |
154 DVLOG(1) << __func__; | |
155 DCHECK(cdm_adapter_library); | |
156 | |
157 // Get VerifyHostFiles() function pointer exported by the CDM. | |
158 // See media/cdm/api/content_decryption_module_ext.h. | |
159 using VerifyHostFilesFunc = | |
160 bool (*)(const cdm::HostFile* cdm_host_files, uint32_t num_files); | |
161 static const char kVerifyHostFilesFuncName[] = "VerifyHostFiles"; | |
tinskip1
2017/01/19 03:42:47
This function name should probably be versioned ju
xhwang
2017/01/19 08:30:51
Agreed that if we change the API we need a differe
| |
162 | |
163 base::NativeLibrary cdm_library; | |
164 #if defined(OS_LINUX) || defined(OS_MACOSX) | |
165 // On POSIX, "the dlsym() function shall search for the named symbol in all | |
166 // objects loaded automatically as a result of loading the object referenced | |
167 // by handle". Since the CDM is loaded automatically as a result of loading | |
168 // the CDM adapter, we can just use the adapter to look for CDM symbols. | |
169 cdm_library = cdm_adapter_library; | |
170 #elif defined(OS_WIN) | |
171 // On Windows, we have manually load the CDM. | |
172 base::ScopedNativeLibrary scoped_cdm_library; | |
173 base::NativeLibraryLoadError error; | |
174 scoped_cdm_library.Reset( | |
175 base::LoadNativeLibrary(GetCdmPath(cdm_adapter_path), &error)); | |
176 if (!scoped_cdm_library.is_valid()) { | |
177 LOG(ERROR) << "Failed to load CDM (error: " << error.ToString() << ")"; | |
178 CloseAllFiles(); | |
179 return; | |
180 } | |
181 cdm_library = scoped_cdm_library.get(); | |
182 #endif | |
183 | |
184 VerifyHostFilesFunc verify_host_files_func = | |
185 reinterpret_cast<VerifyHostFilesFunc>( | |
186 base::GetFunctionPointerFromNativeLibrary(cdm_library, | |
187 kVerifyHostFilesFuncName)); | |
188 if (!verify_host_files_func) { | |
189 LOG(ERROR) << "Function " << kVerifyHostFilesFuncName << " not found."; | |
190 CloseAllFiles(); | |
191 return; | |
192 } | |
193 | |
194 // Fills |cdm_host_files| with common and CDM specific files for | |
195 // |cdm_adapter_path|. | |
196 std::vector<cdm::HostFile> cdm_host_files; | |
197 if (!TakePlatformFiles(cdm_adapter_path, &cdm_host_files)) { | |
198 DVLOG(1) << "Failed to take platform files."; | |
199 CloseAllFiles(); | |
200 return; | |
201 } | |
202 | |
203 // Call VerifyHostFiles() on the CDM with |cdm_host_files|. Note that the | |
204 // ownership of these files are transferred to the CDM, which will close the | |
205 // files immediately after use. | |
206 DVLOG(1) << __func__ << ": Calling " << kVerifyHostFilesFuncName << "()."; | |
207 verify_host_files_func(cdm_host_files.data(), cdm_host_files.size()); | |
tinskip1
2017/01/19 03:42:47
Check return value. Fail if false.
xhwang
2017/01/19 08:30:51
The current API doesn't return a boolean:
https:/
| |
208 | |
209 // Close all files not passed to the CDM. | |
210 CloseAllFiles(); | |
211 } | |
212 | |
213 #if defined(POSIX_WITH_ZYGOTE) | |
214 bool CdmHostFiles::OpenFilesForAllRegisteredCdms() { | |
215 std::vector<base::FilePath> cdm_adapter_paths; | |
216 GetRegisteredCdms(&cdm_adapter_paths); | |
217 if (cdm_adapter_paths.empty()) { | |
218 DVLOG(1) << "No CDM registered."; | |
219 return false; | |
220 } | |
221 | |
222 // Ignore | |
223 for (auto& cdm_adapter_path : cdm_adapter_paths) { | |
224 bool result = OpenCdmFiles(cdm_adapter_path); | |
225 if (!result) | |
226 DVLOG(1) << "CDM files cannot be opened for " << cdm_adapter_path.value(); | |
227 // Ignore the failure and try other registered CDM. | |
228 } | |
229 | |
230 if (cdm_specific_files_map_.empty()) { | |
231 DVLOG(1) << "CDM specific files cannot be opened for any registered CDM."; | |
232 return false; | |
233 } | |
234 | |
235 return OpenCommonFiles(); | |
236 } | |
237 #endif | |
238 | |
239 bool CdmHostFiles::OpenFiles(const base::FilePath& cdm_adapter_path) { | |
240 if (!OpenCdmFiles(cdm_adapter_path)) | |
241 return false; | |
242 | |
243 return OpenCommonFiles(); | |
244 } | |
245 | |
246 bool CdmHostFiles::OpenCommonFiles() { | |
247 DCHECK(common_files_.empty()); | |
248 | |
249 std::vector<CdmHostFilePath> cdm_host_file_paths; | |
250 GetContentClient()->AddContentDecryptionModuleHostFilePaths( | |
251 &cdm_host_file_paths); | |
252 | |
253 for (const CdmHostFilePath& value : cdm_host_file_paths) { | |
254 std::unique_ptr<CdmHostFile> cdm_host_file = | |
255 CdmHostFile::Create(value.file_path, value.sig_file_path); | |
256 if (cdm_host_file) { | |
257 common_files_.push_back(std::move(cdm_host_file)); | |
258 continue; | |
259 } | |
260 | |
261 if (!IgnoreMissingCdmHostFile()) | |
262 return false; | |
263 } | |
264 | |
265 return true; | |
266 } | |
267 | |
268 bool CdmHostFiles::OpenCdmFiles(const base::FilePath& cdm_adapter_path) { | |
269 DCHECK(!cdm_adapter_path.empty()); | |
270 DCHECK(!cdm_specific_files_map_.count(cdm_adapter_path)); | |
271 | |
272 std::unique_ptr<CdmHostFile> cdm_adapter_file = | |
273 CdmHostFile::Create(cdm_adapter_path, GetSigFilePath(cdm_adapter_path)); | |
274 if (!cdm_adapter_file) | |
275 return false; | |
276 | |
277 base::FilePath cdm_path = GetCdmPath(cdm_adapter_path); | |
278 std::unique_ptr<CdmHostFile> cdm_file = | |
279 CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path)); | |
280 if (!cdm_file) | |
281 return false; | |
282 | |
283 ScopedFileVector cdm_specific_files; | |
284 cdm_specific_files.reserve(2); | |
285 cdm_specific_files.push_back(std::move(cdm_adapter_file)); | |
286 cdm_specific_files.push_back(std::move(cdm_file)); | |
287 | |
288 cdm_specific_files_map_[cdm_adapter_path] = std::move(cdm_specific_files); | |
289 return true; | |
290 } | |
291 | |
292 bool CdmHostFiles::TakePlatformFiles( | |
293 const base::FilePath& cdm_adapter_path, | |
294 std::vector<cdm::HostFile>* cdm_host_files) { | |
295 DCHECK(cdm_host_files->empty()); | |
296 | |
297 if (!IgnoreMissingCdmHostFile()) | |
298 DCHECK(!common_files_.empty()); | |
299 | |
300 // Check whether CDM specific files exist. | |
301 const auto& iter = cdm_specific_files_map_.find(cdm_adapter_path); | |
302 if (iter == cdm_specific_files_map_.end()) { | |
303 // This could happen on Linux where CDM files fail to open for Foo CDM, but | |
304 // now we hit Bar CDM. | |
305 DVLOG(1) << "No CDM specific files for " << cdm_adapter_path.value(); | |
306 return false; | |
307 } | |
308 | |
309 const ScopedFileVector& cdm_specific_files = iter->second; | |
310 | |
311 cdm_host_files->reserve(common_files_.size() + cdm_specific_files.size()); | |
312 | |
313 // Populate an array of cdm::HostFile. | |
314 for (const auto& file : common_files_) | |
315 cdm_host_files->push_back(file->TakePlatformFile()); | |
316 | |
317 for (const auto& file : cdm_specific_files) | |
318 cdm_host_files->push_back(file->TakePlatformFile()); | |
319 | |
320 return true; | |
321 } | |
322 | |
323 void CdmHostFiles::CloseAllFiles() { | |
324 common_files_.clear(); | |
325 cdm_specific_files_map_.clear(); | |
326 } | |
327 | |
328 // Question(xhwang): Any better way to check whether a plugin is a CDM? Maybe | |
329 // when we register the plugin we can set some flag explicitly? | |
330 bool IsCdm(const base::FilePath& cdm_adapter_path) { | |
331 return !GetCdmPath(cdm_adapter_path).empty(); | |
332 } | |
333 | |
334 } // namespace content | |
OLD | NEW |