OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/common/media/cdm_host_files.h" | 5 #include "content/common/media/cdm_host_files.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file.h" | 12 #include "base/files/file.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
17 #include "base/native_library.h" | 17 #include "base/native_library.h" |
18 #include "base/path_service.h" | 18 #include "base/path_service.h" |
19 #include "base/scoped_native_library.h" | 19 #include "base/scoped_native_library.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "content/common/media/cdm_host_file.h" | 21 #include "content/common/media/cdm_host_file.h" |
22 #include "content/public/common/cdm_info.h" | 22 #include "content/public/common/cdm_info.h" |
23 #include "content/public/common/content_client.h" | 23 #include "content/public/common/content_client.h" |
| 24 #include "media/base/media_switches.h" |
24 #include "media/cdm/api/content_decryption_module_ext.h" | 25 #include "media/cdm/api/content_decryption_module_ext.h" |
25 #include "media/cdm/cdm_paths.h" | 26 #include "media/cdm/cdm_paths.h" |
26 | 27 |
27 #if defined(POSIX_WITH_ZYGOTE) | 28 #if defined(POSIX_WITH_ZYGOTE) |
28 #include "content/common/pepper_plugin_list.h" | 29 #include "content/common/pepper_plugin_list.h" |
29 #include "content/public/common/pepper_plugin_info.h" | 30 #include "content/public/common/pepper_plugin_info.h" |
30 #endif | 31 #endif |
31 | 32 |
32 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. | 33 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. |
33 | 34 |
34 namespace content { | 35 namespace content { |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
| 39 bool IgnoreMissingCdmHostFile() { |
| 40 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 41 switches::kIgnoreMissingCdmHostFile); |
| 42 } |
| 43 |
38 // TODO(xhwang): Move this to a common place if needed. | 44 // TODO(xhwang): Move this to a common place if needed. |
39 const base::FilePath::CharType kSignatureFileExtension[] = | 45 const base::FilePath::CharType kSignatureFileExtension[] = |
40 FILE_PATH_LITERAL(".sig"); | 46 FILE_PATH_LITERAL(".sig"); |
41 | 47 |
42 // Returns the signature file path given the |file_path|. This function should | 48 // Returns the signature file path given the |file_path|. This function should |
43 // only be used when the signature file and the file are located in the same | 49 // only be used when the signature file and the file are located in the same |
44 // directory, which is the case for the CDM and CDM adapter. | 50 // directory, which is the case for the CDM and CDM adapter. |
45 base::FilePath GetSigFilePath(const base::FilePath& file_path) { | 51 base::FilePath GetSigFilePath(const base::FilePath& file_path) { |
46 return file_path.AddExtension(kSignatureFileExtension); | 52 return file_path.AddExtension(kSignatureFileExtension); |
47 } | 53 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 } | 113 } |
108 | 114 |
109 #if defined(POSIX_WITH_ZYGOTE) | 115 #if defined(POSIX_WITH_ZYGOTE) |
110 // static | 116 // static |
111 void CdmHostFiles::CreateGlobalInstance() { | 117 void CdmHostFiles::CreateGlobalInstance() { |
112 DVLOG(1) << __func__; | 118 DVLOG(1) << __func__; |
113 DCHECK(!g_cdm_host_files.Get().get()); | 119 DCHECK(!g_cdm_host_files.Get().get()); |
114 | 120 |
115 std::unique_ptr<CdmHostFiles> cdm_host_files = | 121 std::unique_ptr<CdmHostFiles> cdm_host_files = |
116 base::MakeUnique<CdmHostFiles>(); | 122 base::MakeUnique<CdmHostFiles>(); |
117 cdm_host_files->OpenFilesForAllRegisteredCdms(); | 123 if (!cdm_host_files->OpenFilesForAllRegisteredCdms()) { |
| 124 DVLOG(1) << __func__ << " failed."; |
| 125 cdm_host_files.reset(); |
| 126 return; |
| 127 } |
| 128 |
118 g_cdm_host_files.Get().reset(cdm_host_files.release()); | 129 g_cdm_host_files.Get().reset(cdm_host_files.release()); |
119 } | 130 } |
120 | 131 |
121 // static | 132 // static |
122 std::unique_ptr<CdmHostFiles> CdmHostFiles::TakeGlobalInstance() { | 133 std::unique_ptr<CdmHostFiles> CdmHostFiles::TakeGlobalInstance() { |
123 return std::move(g_cdm_host_files.Get()); | 134 return std::move(g_cdm_host_files.Get()); |
124 } | 135 } |
125 #endif | 136 #endif |
126 | 137 |
127 // static | 138 // static |
128 std::unique_ptr<CdmHostFiles> CdmHostFiles::Create( | 139 std::unique_ptr<CdmHostFiles> CdmHostFiles::Create( |
129 const base::FilePath& cdm_adapter_path) { | 140 const base::FilePath& cdm_adapter_path) { |
130 DVLOG(1) << __func__; | 141 DVLOG(1) << __func__; |
131 std::unique_ptr<CdmHostFiles> cdm_host_files = | 142 std::unique_ptr<CdmHostFiles> cdm_host_files = |
132 base::MakeUnique<CdmHostFiles>(); | 143 base::MakeUnique<CdmHostFiles>(); |
133 cdm_host_files->OpenFiles(cdm_adapter_path); | 144 if (!cdm_host_files->OpenFiles(cdm_adapter_path)) { |
| 145 cdm_host_files.reset(); |
| 146 return nullptr; |
| 147 } |
| 148 |
134 return cdm_host_files; | 149 return cdm_host_files; |
135 } | 150 } |
136 | 151 |
137 bool CdmHostFiles::VerifyFiles(base::NativeLibrary cdm_adapter_library, | 152 bool CdmHostFiles::VerifyFiles(base::NativeLibrary cdm_adapter_library, |
138 const base::FilePath& cdm_adapter_path) { | 153 const base::FilePath& cdm_adapter_path) { |
139 DVLOG(1) << __func__; | 154 DVLOG(1) << __func__; |
140 DCHECK(cdm_adapter_library); | 155 DCHECK(cdm_adapter_library); |
141 | 156 |
142 // Get function pointer exported by the CDM. | 157 // Get function pointer exported by the CDM. |
143 // See media/cdm/api/content_decryption_module_ext.h. | 158 // See media/cdm/api/content_decryption_module_ext.h. |
(...skipping 27 matching lines...) Expand all Loading... |
171 kVerifyCdmHostFuncName)); | 186 kVerifyCdmHostFuncName)); |
172 if (!verify_cdm_host_func) { | 187 if (!verify_cdm_host_func) { |
173 LOG(ERROR) << "Function " << kVerifyCdmHostFuncName << " not found."; | 188 LOG(ERROR) << "Function " << kVerifyCdmHostFuncName << " not found."; |
174 CloseAllFiles(); | 189 CloseAllFiles(); |
175 return true; | 190 return true; |
176 } | 191 } |
177 | 192 |
178 // Fills |cdm_host_files| with common and CDM specific files for | 193 // Fills |cdm_host_files| with common and CDM specific files for |
179 // |cdm_adapter_path|. | 194 // |cdm_adapter_path|. |
180 std::vector<cdm::HostFile> cdm_host_files; | 195 std::vector<cdm::HostFile> cdm_host_files; |
181 TakePlatformFiles(cdm_adapter_path, &cdm_host_files); | 196 if (!TakePlatformFiles(cdm_adapter_path, &cdm_host_files)) { |
182 | 197 DVLOG(1) << "Failed to take platform files."; |
183 // All remaining files will be closed (e.g. files for other CDMs). | 198 CloseAllFiles(); |
184 CloseAllFiles(); | 199 return true; |
185 | 200 } |
186 // std::vector::data() is not guaranteed to be nullptr when empty(). | |
187 const cdm::HostFile* cdm_host_files_ptr = | |
188 cdm_host_files.empty() ? nullptr : cdm_host_files.data(); | |
189 | 201 |
190 // Call |verify_cdm_host_func| on the CDM with |cdm_host_files|. Note that | 202 // Call |verify_cdm_host_func| on the CDM with |cdm_host_files|. Note that |
191 // the ownership of these files are transferred to the CDM, which will close | 203 // the ownership of these files are transferred to the CDM, which will close |
192 // the files immediately after use. | 204 // the files immediately after use. |
193 DVLOG(1) << __func__ << ": Calling " << kVerifyCdmHostFuncName << "()."; | 205 DVLOG(1) << __func__ << ": Calling " << kVerifyCdmHostFuncName << "()."; |
194 if (!verify_cdm_host_func(cdm_host_files_ptr, cdm_host_files.size())) { | 206 if (!verify_cdm_host_func(cdm_host_files.data(), cdm_host_files.size())) { |
195 DVLOG(1) << "Failed to verify CDM host."; | 207 DVLOG(1) << "Failed to verify CDM host."; |
| 208 CloseAllFiles(); |
196 return false; | 209 return false; |
197 } | 210 } |
198 | 211 |
199 // Close all files not passed to the CDM. | 212 // Close all files not passed to the CDM. |
| 213 CloseAllFiles(); |
200 return true; | 214 return true; |
201 } | 215 } |
202 | 216 |
203 #if defined(POSIX_WITH_ZYGOTE) | 217 #if defined(POSIX_WITH_ZYGOTE) |
204 void CdmHostFiles::OpenFilesForAllRegisteredCdms() { | 218 bool CdmHostFiles::OpenFilesForAllRegisteredCdms() { |
205 std::vector<base::FilePath> cdm_adapter_paths; | 219 std::vector<base::FilePath> cdm_adapter_paths; |
206 GetRegisteredCdms(&cdm_adapter_paths); | 220 GetRegisteredCdms(&cdm_adapter_paths); |
207 if (cdm_adapter_paths.empty()) { | 221 if (cdm_adapter_paths.empty()) { |
208 DVLOG(1) << "No CDM registered."; | 222 DVLOG(1) << "No CDM registered."; |
209 return; | 223 return false; |
210 } | 224 } |
211 | 225 |
212 for (auto& cdm_adapter_path : cdm_adapter_paths) | 226 // Ignore |
213 OpenCdmFiles(cdm_adapter_path); | 227 for (auto& cdm_adapter_path : cdm_adapter_paths) { |
| 228 bool result = OpenCdmFiles(cdm_adapter_path); |
| 229 if (!result) |
| 230 DVLOG(1) << "CDM files cannot be opened for " << cdm_adapter_path.value(); |
| 231 // Ignore the failure and try other registered CDM. |
| 232 } |
214 | 233 |
215 OpenCommonFiles(); | 234 if (cdm_specific_files_map_.empty()) { |
| 235 DVLOG(1) << "CDM specific files cannot be opened for any registered CDM."; |
| 236 return false; |
| 237 } |
| 238 |
| 239 return OpenCommonFiles(); |
216 } | 240 } |
217 #endif | 241 #endif |
218 | 242 |
219 void CdmHostFiles::OpenFiles(const base::FilePath& cdm_adapter_path) { | 243 bool CdmHostFiles::OpenFiles(const base::FilePath& cdm_adapter_path) { |
220 OpenCdmFiles(cdm_adapter_path); | 244 if (!OpenCdmFiles(cdm_adapter_path)) |
221 OpenCommonFiles(); | 245 return false; |
| 246 |
| 247 return OpenCommonFiles(); |
222 } | 248 } |
223 | 249 |
224 void CdmHostFiles::OpenCommonFiles() { | 250 bool CdmHostFiles::OpenCommonFiles() { |
225 DCHECK(common_files_.empty()); | 251 DCHECK(common_files_.empty()); |
226 | 252 |
227 std::vector<CdmHostFilePath> cdm_host_file_paths; | 253 std::vector<CdmHostFilePath> cdm_host_file_paths; |
228 GetContentClient()->AddContentDecryptionModules(nullptr, | 254 GetContentClient()->AddContentDecryptionModules(nullptr, |
229 &cdm_host_file_paths); | 255 &cdm_host_file_paths); |
230 | 256 |
231 for (const CdmHostFilePath& value : cdm_host_file_paths) { | 257 for (const CdmHostFilePath& value : cdm_host_file_paths) { |
232 common_files_.push_back( | 258 std::unique_ptr<CdmHostFile> cdm_host_file = |
233 CdmHostFile::Create(value.file_path, value.sig_file_path)); | 259 CdmHostFile::Create(value.file_path, value.sig_file_path); |
| 260 if (cdm_host_file) { |
| 261 common_files_.push_back(std::move(cdm_host_file)); |
| 262 continue; |
| 263 } |
| 264 |
| 265 if (!IgnoreMissingCdmHostFile()) |
| 266 return false; |
234 } | 267 } |
| 268 |
| 269 return true; |
235 } | 270 } |
236 | 271 |
237 void CdmHostFiles::OpenCdmFiles(const base::FilePath& cdm_adapter_path) { | 272 bool CdmHostFiles::OpenCdmFiles(const base::FilePath& cdm_adapter_path) { |
238 DCHECK(!cdm_adapter_path.empty()); | 273 DCHECK(!cdm_adapter_path.empty()); |
239 DCHECK(!cdm_specific_files_map_.count(cdm_adapter_path)); | 274 DCHECK(!cdm_specific_files_map_.count(cdm_adapter_path)); |
240 | 275 |
241 std::unique_ptr<CdmHostFile> cdm_adapter_file = | 276 std::unique_ptr<CdmHostFile> cdm_adapter_file = |
242 CdmHostFile::Create(cdm_adapter_path, GetSigFilePath(cdm_adapter_path)); | 277 CdmHostFile::Create(cdm_adapter_path, GetSigFilePath(cdm_adapter_path)); |
| 278 if (!cdm_adapter_file) |
| 279 return false; |
243 | 280 |
244 base::FilePath cdm_path = GetCdmPath(cdm_adapter_path); | 281 base::FilePath cdm_path = GetCdmPath(cdm_adapter_path); |
245 std::unique_ptr<CdmHostFile> cdm_file = | 282 std::unique_ptr<CdmHostFile> cdm_file = |
246 CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path)); | 283 CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path)); |
| 284 if (!cdm_file) |
| 285 return false; |
247 | 286 |
248 ScopedFileVector cdm_specific_files; | 287 ScopedFileVector cdm_specific_files; |
249 cdm_specific_files.reserve(2); | 288 cdm_specific_files.reserve(2); |
250 cdm_specific_files.push_back(std::move(cdm_adapter_file)); | 289 cdm_specific_files.push_back(std::move(cdm_adapter_file)); |
251 cdm_specific_files.push_back(std::move(cdm_file)); | 290 cdm_specific_files.push_back(std::move(cdm_file)); |
252 | 291 |
253 cdm_specific_files_map_[cdm_adapter_path] = std::move(cdm_specific_files); | 292 cdm_specific_files_map_[cdm_adapter_path] = std::move(cdm_specific_files); |
| 293 return true; |
254 } | 294 } |
255 | 295 |
256 void CdmHostFiles::TakePlatformFiles( | 296 bool CdmHostFiles::TakePlatformFiles( |
257 const base::FilePath& cdm_adapter_path, | 297 const base::FilePath& cdm_adapter_path, |
258 std::vector<cdm::HostFile>* cdm_host_files) { | 298 std::vector<cdm::HostFile>* cdm_host_files) { |
259 DCHECK(cdm_host_files->empty()); | 299 DCHECK(cdm_host_files->empty()); |
260 | 300 |
| 301 if (!IgnoreMissingCdmHostFile()) |
| 302 DCHECK(!common_files_.empty()); |
| 303 |
| 304 // Check whether CDM specific files exist. |
| 305 const auto& iter = cdm_specific_files_map_.find(cdm_adapter_path); |
| 306 if (iter == cdm_specific_files_map_.end()) { |
| 307 // This could happen on Linux where CDM files fail to open for Foo CDM, but |
| 308 // now we hit Bar CDM. |
| 309 DVLOG(1) << "No CDM specific files for " << cdm_adapter_path.value(); |
| 310 return false; |
| 311 } |
| 312 |
| 313 const ScopedFileVector& cdm_specific_files = iter->second; |
| 314 |
| 315 cdm_host_files->reserve(common_files_.size() + cdm_specific_files.size()); |
| 316 |
261 // Populate an array of cdm::HostFile. | 317 // Populate an array of cdm::HostFile. |
262 for (const auto& file : common_files_) | 318 for (const auto& file : common_files_) |
263 cdm_host_files->push_back(file->TakePlatformFile()); | 319 cdm_host_files->push_back(file->TakePlatformFile()); |
264 | 320 |
265 // Check whether CDM specific files exist. | 321 for (const auto& file : cdm_specific_files) |
266 const auto& iter = cdm_specific_files_map_.find(cdm_adapter_path); | 322 cdm_host_files->push_back(file->TakePlatformFile()); |
267 if (iter == cdm_specific_files_map_.end()) { | 323 |
268 NOTREACHED() << "No CDM specific files for " << cdm_adapter_path.value(); | 324 return true; |
269 } else { | |
270 const ScopedFileVector& cdm_specific_files = iter->second; | |
271 for (const auto& file : cdm_specific_files) | |
272 cdm_host_files->push_back(file->TakePlatformFile()); | |
273 } | |
274 } | 325 } |
275 | 326 |
276 void CdmHostFiles::CloseAllFiles() { | 327 void CdmHostFiles::CloseAllFiles() { |
277 common_files_.clear(); | 328 common_files_.clear(); |
278 cdm_specific_files_map_.clear(); | 329 cdm_specific_files_map_.clear(); |
279 } | 330 } |
280 | 331 |
281 // Question(xhwang): Any better way to check whether a plugin is a CDM? Maybe | 332 // Question(xhwang): Any better way to check whether a plugin is a CDM? Maybe |
282 // when we register the plugin we can set some flag explicitly? | 333 // when we register the plugin we can set some flag explicitly? |
283 bool IsCdm(const base::FilePath& cdm_adapter_path) { | 334 bool IsCdm(const base::FilePath& cdm_adapter_path) { |
284 return !GetCdmPath(cdm_adapter_path).empty(); | 335 return !GetCdmPath(cdm_adapter_path).empty(); |
285 } | 336 } |
286 | 337 |
287 } // namespace content | 338 } // namespace content |
OLD | NEW |