1 // Copyright 2017 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 
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "media/cdm/cdm_auxiliary_helper.h"
15 #include "media/media_buildflags.h"
16 #include "media/mojo/mojom/cdm_storage.mojom.h"
17 #include "media/mojo/mojom/output_protection.mojom.h"
18 #include "media/mojo/mojom/platform_verification.mojom.h"
19 #include "media/mojo/services/media_mojo_export.h"
20 #include "media/mojo/services/mojo_cdm_file_io.h"
21 #include "mojo/public/cpp/bindings/remote.h"
22 
23 #if BUILDFLAG(ENABLE_CDM_PROXY)
24 #include "media/mojo/mojom/cdm_proxy.mojom.h"
25 #include "media/mojo/services/mojo_cdm_proxy.h"
26 #endif
27 
28 namespace service_manager {
29 namespace mojom {
30 class InterfaceProvider;
31 }
32 }  // namespace service_manager
33 
34 namespace media {
35 
36 // Helper class that connects the CDM to various auxiliary services. All
37 // additional services (FileIO, memory allocation, output protection, and
38 // platform verification) are lazily created.
39 class MEDIA_MOJO_EXPORT MojoCdmHelper final : public CdmAuxiliaryHelper,
40                                               public MojoCdmFileIO::Delegate {
41  public:
42   explicit MojoCdmHelper(
43       service_manager::mojom::InterfaceProvider* interface_provider);
44   ~MojoCdmHelper() final;
45 
46   // CdmAuxiliaryHelper implementation.
47   void SetFileReadCB(FileReadCB file_read_cb) final;
48   cdm::FileIO* CreateCdmFileIO(cdm::FileIOClient* client) final;
49 #if BUILDFLAG(ENABLE_CDM_PROXY)
50   cdm::CdmProxy* CreateCdmProxy(cdm::CdmProxyClient* client) final;
51   int GetCdmProxyCdmId() final;
52 #endif
53   cdm::Buffer* CreateCdmBuffer(size_t capacity) final;
54   std::unique_ptr<VideoFrameImpl> CreateCdmVideoFrame() final;
55   void QueryStatus(QueryStatusCB callback) final;
56   void EnableProtection(uint32_t desired_protection_mask,
57                         EnableProtectionCB callback) final;
58   void ChallengePlatform(const std::string& service_id,
59                          const std::string& challenge,
60                          ChallengePlatformCB callback) final;
61   void GetStorageId(uint32_t version, StorageIdCB callback) final;
62 
63   // MojoCdmFileIO::Delegate implementation.
64   void CloseCdmFileIO(MojoCdmFileIO* cdm_file_io) final;
65   void ReportFileReadSize(int file_size_bytes) final;
66 
67  private:
68   // All services are created lazily.
69   void ConnectToCdmStorage();
70   CdmAllocator* GetAllocator();
71   void ConnectToOutputProtection();
72   void ConnectToPlatformVerification();
73 
74   // Provides interfaces when needed.
75   service_manager::mojom::InterfaceProvider* interface_provider_;
76 
77   // Connections to the additional services. For the mojom classes, if a
78   // connection error occurs, we will not be able to reconnect to the
79   // service as the document has been destroyed (see FrameServiceBase) or
80   // the browser crashed, so there's no point in trying to reconnect.
81   mojo::Remote<mojom::CdmStorage> cdm_storage_remote_;
82   std::unique_ptr<CdmAllocator> allocator_;
83   mojo::Remote<mojom::OutputProtection> output_protection_;
84   mojo::Remote<mojom::PlatformVerification> platform_verification_;
85 
86   FileReadCB file_read_cb_;
87 
88   // A list of open cdm::FileIO objects.
89   // TODO(xhwang): Switch to use UniquePtrComparator.
90   std::vector<std::unique_ptr<MojoCdmFileIO>> cdm_file_io_set_;
91 
92 #if BUILDFLAG(ENABLE_CDM_PROXY)
93   std::unique_ptr<MojoCdmProxy> cdm_proxy_;
94 #endif
95 
96   base::WeakPtrFactory<MojoCdmHelper> weak_factory_{this};
97   DISALLOW_COPY_AND_ASSIGN(MojoCdmHelper);
98 };
99 
100 }  // namespace media
101 
102 #endif  // MEDIA_MOJO_SERVICES_MOJO_CDM_HELPER_H_
103