1 // Copyright 2017 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 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "build/build_config.h"
15 #include "media/cdm/cdm_auxiliary_helper.h"
16 #include "media/media_buildflags.h"
17 #include "media/mojo/mojom/cdm_document_service.mojom.h"
18 #include "media/mojo/mojom/cdm_storage.mojom.h"
19 #include "media/mojo/mojom/frame_interface_factory.mojom.h"
20 #include "media/mojo/mojom/output_protection.mojom.h"
21 #include "media/mojo/services/media_mojo_export.h"
22 #include "media/mojo/services/mojo_cdm_file_io.h"
23 #include "mojo/public/cpp/bindings/remote.h"
27 // Helper class that connects the CDM to various auxiliary services. All
28 // additional services (FileIO, memory allocation, output protection, and
29 // platform verification) are lazily created.
30 class MEDIA_MOJO_EXPORT MojoCdmHelper final : public CdmAuxiliaryHelper,
31 public MojoCdmFileIO::Delegate {
33 explicit MojoCdmHelper(mojom::FrameInterfaceFactory* frame_interfaces);
34 MojoCdmHelper(const MojoCdmHelper&) = delete;
35 MojoCdmHelper operator=(const MojoCdmHelper&) = delete;
36 ~MojoCdmHelper() final;
38 // CdmAuxiliaryHelper implementation.
39 void SetFileReadCB(FileReadCB file_read_cb) final;
40 cdm::FileIO* CreateCdmFileIO(cdm::FileIOClient* client) final;
41 url::Origin GetCdmOrigin() final;
42 cdm::Buffer* CreateCdmBuffer(size_t capacity) final;
43 std::unique_ptr<VideoFrameImpl> CreateCdmVideoFrame() final;
44 void QueryStatus(QueryStatusCB callback) final;
45 void EnableProtection(uint32_t desired_protection_mask,
46 EnableProtectionCB callback) final;
47 void ChallengePlatform(const std::string& service_id,
48 const std::string& challenge,
49 ChallengePlatformCB callback) final;
50 void GetStorageId(uint32_t version, StorageIdCB callback) final;
52 void GetMediaFoundationCdmData(GetMediaFoundationCdmDataCB callback) final;
53 void SetCdmClientToken(const std::vector<uint8_t>& client_token) final;
54 void OnCdmEvent(CdmEvent event, HRESULT hresult) final;
55 #endif // BUILDFLAG(IS_WIN)
57 // MojoCdmFileIO::Delegate implementation.
58 void CloseCdmFileIO(MojoCdmFileIO* cdm_file_io) final;
59 void ReportFileReadSize(int file_size_bytes) final;
62 // All services are created lazily.
63 void ConnectToOutputProtection();
64 void ConnectToCdmDocumentService();
66 CdmAllocator* GetAllocator();
68 // Provides interfaces when needed.
69 raw_ptr<mojom::FrameInterfaceFactory> frame_interfaces_;
71 // Connections to the additional services. Will try to reconnect if
72 // disconnected, to handle cases like page refresh, where the document is
73 // destroyed but RenderFrameHostImpl is not.
74 mojo::Remote<mojom::OutputProtection> output_protection_;
75 mojo::Remote<mojom::CdmDocumentService> cdm_document_service_;
77 std::unique_ptr<CdmAllocator> allocator_;
79 FileReadCB file_read_cb_;
81 // A list of open cdm::FileIO objects.
82 // TODO(xhwang): Switch to use UniquePtrComparator.
83 std::vector<std::unique_ptr<MojoCdmFileIO>> cdm_file_io_set_;
85 base::WeakPtrFactory<MojoCdmHelper> weak_factory_{this};
90 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_