cdm_host_files.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "media/cdm/cdm_host_files.h"
  5. #include <memory>
  6. #include <vector>
  7. #include "base/command_line.h"
  8. #include "base/files/file.h"
  9. #include "base/files/file_path.h"
  10. #include "base/lazy_instance.h"
  11. #include "base/logging.h"
  12. #include "base/memory/ptr_util.h"
  13. #include "base/native_library.h"
  14. #include "base/path_service.h"
  15. #include "base/scoped_native_library.h"
  16. #include "build/build_config.h"
  17. #include "media/cdm/api/content_decryption_module_ext.h"
  18. #include "media/cdm/cdm_paths.h"
  19. namespace media {
  20. namespace {
  21. // TODO(xhwang): Move this to a common place if needed.
  22. const base::FilePath::CharType kSignatureFileExtension[] =
  23. FILE_PATH_LITERAL(".sig");
  24. // Returns the signature file path given the |file_path|. This function should
  25. // only be used when the signature file and the file are located in the same
  26. // directory, which is the case for the CDM.
  27. base::FilePath GetSigFilePath(const base::FilePath& file_path) {
  28. return file_path.AddExtension(kSignatureFileExtension);
  29. }
  30. } // namespace
  31. CdmHostFiles::CdmHostFiles() {
  32. DVLOG(1) << __func__;
  33. }
  34. CdmHostFiles::~CdmHostFiles() {
  35. DVLOG(1) << __func__;
  36. }
  37. void CdmHostFiles::Initialize(
  38. const base::FilePath& cdm_path,
  39. const std::vector<CdmHostFilePath>& cdm_host_file_paths) {
  40. OpenCdmFile(cdm_path);
  41. OpenCommonFiles(cdm_host_file_paths);
  42. }
  43. CdmHostFiles::Status CdmHostFiles::InitVerification(
  44. base::NativeLibrary cdm_library) {
  45. DVLOG(1) << __func__;
  46. DCHECK(cdm_library);
  47. // Get function pointer exported by the CDM.
  48. // See media/cdm/api/content_decryption_module_ext.h.
  49. using InitVerificationFunc =
  50. bool (*)(const cdm::HostFile* cdm_host_files, uint32_t num_files);
  51. static const char kInitVerificationFuncName[] = "VerifyCdmHost_0";
  52. InitVerificationFunc init_verification_func =
  53. reinterpret_cast<InitVerificationFunc>(
  54. base::GetFunctionPointerFromNativeLibrary(cdm_library,
  55. kInitVerificationFuncName));
  56. if (!init_verification_func) {
  57. LOG(ERROR) << "Function " << kInitVerificationFuncName << " not found.";
  58. CloseAllFiles();
  59. return Status::kGetFunctionFailed;
  60. }
  61. // Fills |cdm_host_files| with common and CDM specific files.
  62. std::vector<cdm::HostFile> cdm_host_files;
  63. TakePlatformFiles(&cdm_host_files);
  64. // std::vector::data() is not guaranteed to be nullptr when empty().
  65. const cdm::HostFile* cdm_host_files_ptr =
  66. cdm_host_files.empty() ? nullptr : cdm_host_files.data();
  67. // Call |init_verification_func| on the CDM with |cdm_host_files|. Note that
  68. // the ownership of these files are transferred to the CDM, which will close
  69. // the files immediately after use.
  70. DVLOG(1) << __func__ << ": Calling " << kInitVerificationFuncName
  71. << "() with " << cdm_host_files.size() << " files.";
  72. for (const auto& host_file : cdm_host_files) {
  73. DVLOG(1) << " - File Path: " << host_file.file_path;
  74. DVLOG(1) << " - File: " << host_file.file;
  75. DVLOG(1) << " - Sig File: " << host_file.sig_file;
  76. }
  77. if (!init_verification_func(cdm_host_files_ptr, cdm_host_files.size())) {
  78. DVLOG(1) << "Failed to verify CDM host.";
  79. CloseAllFiles();
  80. return Status::kInitVerificationFailed;
  81. }
  82. // Close all files not passed to the CDM.
  83. CloseAllFiles();
  84. return Status::kSuccess;
  85. }
  86. void CdmHostFiles::CloseAllFiles() {
  87. common_files_.clear();
  88. cdm_specific_files_.clear();
  89. }
  90. void CdmHostFiles::OpenCommonFiles(
  91. const std::vector<CdmHostFilePath>& cdm_host_file_paths) {
  92. DCHECK(common_files_.empty());
  93. for (const auto& value : cdm_host_file_paths) {
  94. common_files_.push_back(
  95. CdmHostFile::Create(value.file_path, value.sig_file_path));
  96. }
  97. }
  98. void CdmHostFiles::OpenCdmFile(const base::FilePath& cdm_path) {
  99. DCHECK(!cdm_path.empty());
  100. cdm_specific_files_.push_back(
  101. CdmHostFile::Create(cdm_path, GetSigFilePath(cdm_path)));
  102. }
  103. void CdmHostFiles::TakePlatformFiles(
  104. std::vector<cdm::HostFile>* cdm_host_files) {
  105. DCHECK(cdm_host_files->empty());
  106. // Populate an array of cdm::HostFile.
  107. for (const auto& file : common_files_)
  108. cdm_host_files->push_back(file->TakePlatformFile());
  109. for (const auto& file : cdm_specific_files_)
  110. cdm_host_files->push_back(file->TakePlatformFile());
  111. }
  112. } // namespace media