Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(497)

Side by Side Diff: content/common/media/cdm_host_files.cc

Issue 2773283002: media: Simplify CdmHostFile(s) (Closed)
Patch Set: comments addressed Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/media/cdm_host_files.h ('k') | media/base/media_switches.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
25 #include "media/cdm/api/content_decryption_module_ext.h" 24 #include "media/cdm/api/content_decryption_module_ext.h"
26 #include "media/cdm/cdm_paths.h" 25 #include "media/cdm/cdm_paths.h"
27 26
28 #if defined(POSIX_WITH_ZYGOTE) 27 #if defined(POSIX_WITH_ZYGOTE)
29 #include "content/common/pepper_plugin_list.h" 28 #include "content/common/pepper_plugin_list.h"
30 #include "content/public/common/pepper_plugin_info.h" 29 #include "content/public/common/pepper_plugin_info.h"
31 #endif 30 #endif
32 31
33 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. 32 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
34 33
35 namespace content { 34 namespace content {
36 35
37 namespace { 36 namespace {
38 37
39 bool IgnoreMissingCdmHostFile() {
40 return base::CommandLine::ForCurrentProcess()->HasSwitch(
41 switches::kIgnoreMissingCdmHostFile);
42 }
43
44 // TODO(xhwang): Move this to a common place if needed. 38 // TODO(xhwang): Move this to a common place if needed.
45 const base::FilePath::CharType kSignatureFileExtension[] = 39 const base::FilePath::CharType kSignatureFileExtension[] =
46 FILE_PATH_LITERAL(".sig"); 40 FILE_PATH_LITERAL(".sig");
47 41
48 // Returns the signature file path given the |file_path|. This function should 42 // 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 43 // 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. 44 // directory, which is the case for the CDM and CDM adapter.
51 base::FilePath GetSigFilePath(const base::FilePath& file_path) { 45 base::FilePath GetSigFilePath(const base::FilePath& file_path) {
52 return file_path.AddExtension(kSignatureFileExtension); 46 return file_path.AddExtension(kSignatureFileExtension);
53 } 47 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 } 107 }
114 108
115 #if defined(POSIX_WITH_ZYGOTE) 109 #if defined(POSIX_WITH_ZYGOTE)
116 // static 110 // static
117 void CdmHostFiles::CreateGlobalInstance() { 111 void CdmHostFiles::CreateGlobalInstance() {
118 DVLOG(1) << __func__; 112 DVLOG(1) << __func__;
119 DCHECK(!g_cdm_host_files.Get().get()); 113 DCHECK(!g_cdm_host_files.Get().get());
120 114
121 std::unique_ptr<CdmHostFiles> cdm_host_files = 115 std::unique_ptr<CdmHostFiles> cdm_host_files =
122 base::MakeUnique<CdmHostFiles>(); 116 base::MakeUnique<CdmHostFiles>();
123 if (!cdm_host_files->OpenFilesForAllRegisteredCdms()) { 117 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()); 118 g_cdm_host_files.Get().reset(cdm_host_files.release());
130 } 119 }
131 120
132 // static 121 // static
133 std::unique_ptr<CdmHostFiles> CdmHostFiles::TakeGlobalInstance() { 122 std::unique_ptr<CdmHostFiles> CdmHostFiles::TakeGlobalInstance() {
134 return std::move(g_cdm_host_files.Get()); 123 return std::move(g_cdm_host_files.Get());
135 } 124 }
136 #endif 125 #endif
137 126
138 // static 127 // static
139 std::unique_ptr<CdmHostFiles> CdmHostFiles::Create( 128 std::unique_ptr<CdmHostFiles> CdmHostFiles::Create(
140 const base::FilePath& cdm_adapter_path) { 129 const base::FilePath& cdm_adapter_path) {
141 DVLOG(1) << __func__; 130 DVLOG(1) << __func__;
142 std::unique_ptr<CdmHostFiles> cdm_host_files = 131 std::unique_ptr<CdmHostFiles> cdm_host_files =
143 base::MakeUnique<CdmHostFiles>(); 132 base::MakeUnique<CdmHostFiles>();
144 if (!cdm_host_files->OpenFiles(cdm_adapter_path)) { 133 cdm_host_files->OpenFiles(cdm_adapter_path);
145 cdm_host_files.reset();
146 return nullptr;
147 }
148
149 return cdm_host_files; 134 return cdm_host_files;
150 } 135 }
151 136
152 bool CdmHostFiles::VerifyFiles(base::NativeLibrary cdm_adapter_library, 137 bool CdmHostFiles::VerifyFiles(base::NativeLibrary cdm_adapter_library,
153 const base::FilePath& cdm_adapter_path) { 138 const base::FilePath& cdm_adapter_path) {
154 DVLOG(1) << __func__; 139 DVLOG(1) << __func__;
155 DCHECK(cdm_adapter_library); 140 DCHECK(cdm_adapter_library);
156 141
157 // Get function pointer exported by the CDM. 142 // Get function pointer exported by the CDM.
158 // See media/cdm/api/content_decryption_module_ext.h. 143 // See media/cdm/api/content_decryption_module_ext.h.
(...skipping 27 matching lines...) Expand all
186 kVerifyCdmHostFuncName)); 171 kVerifyCdmHostFuncName));
187 if (!verify_cdm_host_func) { 172 if (!verify_cdm_host_func) {
188 LOG(ERROR) << "Function " << kVerifyCdmHostFuncName << " not found."; 173 LOG(ERROR) << "Function " << kVerifyCdmHostFuncName << " not found.";
189 CloseAllFiles(); 174 CloseAllFiles();
190 return true; 175 return true;
191 } 176 }
192 177
193 // Fills |cdm_host_files| with common and CDM specific files for 178 // Fills |cdm_host_files| with common and CDM specific files for
194 // |cdm_adapter_path|. 179 // |cdm_adapter_path|.
195 std::vector<cdm::HostFile> cdm_host_files; 180 std::vector<cdm::HostFile> cdm_host_files;
196 if (!TakePlatformFiles(cdm_adapter_path, &cdm_host_files)) { 181 TakePlatformFiles(cdm_adapter_path, &cdm_host_files);
197 DVLOG(1) << "Failed to take platform files."; 182
198 CloseAllFiles(); 183 // All remaining files will be closed (e.g. files for other CDMs).
199 return true; 184 CloseAllFiles();
200 } 185
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();
201 189
202 // Call |verify_cdm_host_func| on the CDM with |cdm_host_files|. Note that 190 // Call |verify_cdm_host_func| on the CDM with |cdm_host_files|. Note that
203 // the ownership of these files are transferred to the CDM, which will close 191 // the ownership of these files are transferred to the CDM, which will close
204 // the files immediately after use. 192 // the files immediately after use.
205 DVLOG(1) << __func__ << ": Calling " << kVerifyCdmHostFuncName << "()."; 193 DVLOG(1) << __func__ << ": Calling " << kVerifyCdmHostFuncName << "().";
206 if (!verify_cdm_host_func(cdm_host_files.data(), cdm_host_files.size())) { 194 if (!verify_cdm_host_func(cdm_host_files_ptr, cdm_host_files.size())) {
207 DVLOG(1) << "Failed to verify CDM host."; 195 DVLOG(1) << "Failed to verify CDM host.";
208 CloseAllFiles();
209 return false; 196 return false;
210 } 197 }
211 198
212 // Close all files not passed to the CDM. 199 // Close all files not passed to the CDM.
213 CloseAllFiles();
214 return true; 200 return true;
215 } 201 }
216 202
217 #if defined(POSIX_WITH_ZYGOTE) 203 #if defined(POSIX_WITH_ZYGOTE)
218 bool CdmHostFiles::OpenFilesForAllRegisteredCdms() { 204 void CdmHostFiles::OpenFilesForAllRegisteredCdms() {
219 std::vector<base::FilePath> cdm_adapter_paths; 205 std::vector<base::FilePath> cdm_adapter_paths;
220 GetRegisteredCdms(&cdm_adapter_paths); 206 GetRegisteredCdms(&cdm_adapter_paths);
221 if (cdm_adapter_paths.empty()) { 207 if (cdm_adapter_paths.empty()) {
222 DVLOG(1) << "No CDM registered."; 208 DVLOG(1) << "No CDM registered.";
223 return false; 209 return;
224 } 210 }
225 211
226 // Ignore 212 for (auto& cdm_adapter_path : cdm_adapter_paths)
227 for (auto& cdm_adapter_path : cdm_adapter_paths) { 213 OpenCdmFiles(cdm_adapter_path);
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 }
233 214
234 if (cdm_specific_files_map_.empty()) { 215 OpenCommonFiles();
235 DVLOG(1) << "CDM specific files cannot be opened for any registered CDM.";
236 return false;
237 }
238
239 return OpenCommonFiles();
240 } 216 }
241 #endif 217 #endif
242 218
243 bool CdmHostFiles::OpenFiles(const base::FilePath& cdm_adapter_path) { 219 void CdmHostFiles::OpenFiles(const base::FilePath& cdm_adapter_path) {
244 if (!OpenCdmFiles(cdm_adapter_path)) 220 OpenCdmFiles(cdm_adapter_path);
245 return false; 221 OpenCommonFiles();
246
247 return OpenCommonFiles();
248 } 222 }
249 223
250 bool CdmHostFiles::OpenCommonFiles() { 224 void CdmHostFiles::OpenCommonFiles() {
251 DCHECK(common_files_.empty()); 225 DCHECK(common_files_.empty());
252 226
253 std::vector<CdmHostFilePath> cdm_host_file_paths; 227 std::vector<CdmHostFilePath> cdm_host_file_paths;
254 GetContentClient()->AddContentDecryptionModules(nullptr, 228 GetContentClient()->AddContentDecryptionModules(nullptr,
255 &cdm_host_file_paths); 229 &cdm_host_file_paths);
256 230
257 for (const CdmHostFilePath& value : cdm_host_file_paths) { 231 for (const CdmHostFilePath& value : cdm_host_file_paths) {
258 std::unique_ptr<CdmHostFile> cdm_host_file = 232 common_files_.push_back(
259 CdmHostFile::Create(value.file_path, value.sig_file_path); 233 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;
267 } 234 }
268
269 return true;
270 } 235 }
271 236
272 bool CdmHostFiles::OpenCdmFiles(const base::FilePath& cdm_adapter_path) { 237 void CdmHostFiles::OpenCdmFiles(const base::FilePath& cdm_adapter_path) {
273 DCHECK(!cdm_adapter_path.empty()); 238 DCHECK(!cdm_adapter_path.empty());
274 DCHECK(!cdm_specific_files_map_.count(cdm_adapter_path)); 239 DCHECK(!cdm_specific_files_map_.count(cdm_adapter_path));
275 240
276 std::unique_ptr<CdmHostFile> cdm_adapter_file = 241 std::unique_ptr<CdmHostFile> cdm_adapter_file =
277 CdmHostFile::Create(cdm_adapter_path, GetSigFilePath(cdm_adapter_path)); 242 CdmHostFile::Create(cdm_adapter_path, GetSigFilePath(cdm_adapter_path));
278 if (!cdm_adapter_file)
279 return false;
280 243
281 base::FilePath cdm_path = GetCdmPath(cdm_adapter_path); 244 base::FilePath cdm_path = GetCdmPath(cdm_adapter_path);
282 std::unique_ptr<CdmHostFile> cdm_file = 245 std::unique_ptr<CdmHostFile> cdm_file =
283 CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path)); 246 CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path));
284 if (!cdm_file)
285 return false;
286 247
287 ScopedFileVector cdm_specific_files; 248 ScopedFileVector cdm_specific_files;
288 cdm_specific_files.reserve(2); 249 cdm_specific_files.reserve(2);
289 cdm_specific_files.push_back(std::move(cdm_adapter_file)); 250 cdm_specific_files.push_back(std::move(cdm_adapter_file));
290 cdm_specific_files.push_back(std::move(cdm_file)); 251 cdm_specific_files.push_back(std::move(cdm_file));
291 252
292 cdm_specific_files_map_[cdm_adapter_path] = std::move(cdm_specific_files); 253 cdm_specific_files_map_[cdm_adapter_path] = std::move(cdm_specific_files);
293 return true;
294 } 254 }
295 255
296 bool CdmHostFiles::TakePlatformFiles( 256 void CdmHostFiles::TakePlatformFiles(
297 const base::FilePath& cdm_adapter_path, 257 const base::FilePath& cdm_adapter_path,
298 std::vector<cdm::HostFile>* cdm_host_files) { 258 std::vector<cdm::HostFile>* cdm_host_files) {
299 DCHECK(cdm_host_files->empty()); 259 DCHECK(cdm_host_files->empty());
300 260
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
317 // Populate an array of cdm::HostFile. 261 // Populate an array of cdm::HostFile.
318 for (const auto& file : common_files_) 262 for (const auto& file : common_files_)
319 cdm_host_files->push_back(file->TakePlatformFile()); 263 cdm_host_files->push_back(file->TakePlatformFile());
320 264
321 for (const auto& file : cdm_specific_files) 265 // Check whether CDM specific files exist.
322 cdm_host_files->push_back(file->TakePlatformFile()); 266 const auto& iter = cdm_specific_files_map_.find(cdm_adapter_path);
323 267 if (iter == cdm_specific_files_map_.end()) {
324 return true; 268 NOTREACHED() << "No CDM specific files for " << cdm_adapter_path.value();
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 }
325 } 274 }
326 275
327 void CdmHostFiles::CloseAllFiles() { 276 void CdmHostFiles::CloseAllFiles() {
328 common_files_.clear(); 277 common_files_.clear();
329 cdm_specific_files_map_.clear(); 278 cdm_specific_files_map_.clear();
330 } 279 }
331 280
332 // Question(xhwang): Any better way to check whether a plugin is a CDM? Maybe 281 // Question(xhwang): Any better way to check whether a plugin is a CDM? Maybe
333 // when we register the plugin we can set some flag explicitly? 282 // when we register the plugin we can set some flag explicitly?
334 bool IsCdm(const base::FilePath& cdm_adapter_path) { 283 bool IsCdm(const base::FilePath& cdm_adapter_path) {
335 return !GetCdmPath(cdm_adapter_path).empty(); 284 return !GetCdmPath(cdm_adapter_path).empty();
336 } 285 }
337 286
338 } // namespace content 287 } // namespace content
OLDNEW
« no previous file with comments | « content/common/media/cdm_host_files.h ('k') | media/base/media_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698