1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/mojo/services/cdm_service_broker.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 #include "media/cdm/cdm_module.h"
12 #include "media/media_buildflags.h"
16 #include "sandbox/mac/seatbelt_extension.h"
17 #endif // BUILDFLAG(IS_MAC)
19 #if BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
20 #include "media/cdm/cdm_host_file.h"
25 CdmServiceBroker::CdmServiceBroker(
26 std::unique_ptr<CdmService::Client> client,
27 mojo::PendingReceiver<mojom::CdmServiceBroker> receiver)
28 : client_(std::move(client)), receiver_(this, std::move(receiver)) {
33 CdmServiceBroker::~CdmServiceBroker() = default;
35 void CdmServiceBroker::GetService(
36 const base::FilePath& cdm_path,
38 mojo::PendingRemote<mojom::SeatbeltExtensionTokenProvider> token_provider,
39 #endif // BUILDFLAG(IS_MAC)
40 mojo::PendingReceiver<mojom::CdmService> service_receiver) {
42 DVLOG(1) << __func__ << ": CdmService can only be bound once";
46 bool success = InitializeAndEnsureSandboxed(
48 std::move(token_provider),
49 #endif // BUILDFLAG(IS_MAC)
57 DCHECK(!cdm_service_);
58 cdm_service_ = std::make_unique<CdmService>(std::move(client_),
59 std::move(service_receiver));
62 bool CdmServiceBroker::InitializeAndEnsureSandboxed(
64 mojo::PendingRemote<mojom::SeatbeltExtensionTokenProvider> token_provider,
65 #endif // BUILDFLAG(IS_MAC)
66 const base::FilePath& cdm_path) {
67 DVLOG(1) << __func__ << ": cdm_path = " << cdm_path.value();
71 std::vector<std::unique_ptr<sandbox::SeatbeltExtension>> extensions;
74 std::vector<sandbox::SeatbeltExtensionToken> tokens;
75 CHECK(mojo::Remote<mojom::SeatbeltExtensionTokenProvider>(
76 std::move(token_provider))
77 ->GetTokens(&tokens));
79 for (auto&& token : tokens) {
80 DVLOG(3) << "token: " << token.token();
81 auto extension = sandbox::SeatbeltExtension::FromToken(std::move(token));
82 if (!extension->Consume()) {
83 DVLOG(1) << "Failed to consume sandbox seatbelt extension. This could "
84 "happen if --no-sandbox is specified.";
86 extensions.push_back(std::move(extension));
89 #endif // BUILDFLAG(IS_MAC)
91 CdmModule* instance = CdmModule::GetInstance();
93 #if BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
94 std::vector<CdmHostFilePath> cdm_host_file_paths;
95 client_->AddCdmHostFilePaths(&cdm_host_file_paths);
96 bool success = instance->Initialize(cdm_path, cdm_host_file_paths);
98 bool success = instance->Initialize(cdm_path);
99 #endif // BUILDFLAG(ENABLE_CDM_HOST_VERIFICATION)
101 // This may trigger the sandbox to be sealed. After this call, the process is
103 client_->EnsureSandboxed();
105 #if BUILDFLAG(IS_MAC)
106 for (auto&& extension : extensions)
108 #endif // BUILDFLAG(IS_MAC)
110 // Always called within the sandbox.
112 instance->InitializeCdmModule();