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_FILE_IO_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_FILE_IO_H_
13 #include "base/functional/callback_forward.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "media/cdm/api/content_decryption_module.h"
17 #include "media/mojo/mojom/cdm_storage.mojom.h"
18 #include "media/mojo/services/media_mojo_export.h"
19 #include "mojo/public/cpp/bindings/associated_remote.h"
20 #include "mojo/public/cpp/bindings/pending_associated_remote.h"
21 #include "mojo/public/cpp/bindings/remote.h"
25 // Implements a cdm::FileIO that communicates with mojom::CdmStorage.
26 class MEDIA_MOJO_EXPORT MojoCdmFileIO : public cdm::FileIO {
30 // Notifies the delegate to close |cdm_file_io|.
31 virtual void CloseCdmFileIO(MojoCdmFileIO* cdm_file_io) = 0;
33 // Reports the size of file read by MojoCdmFileIO.
34 virtual void ReportFileReadSize(int file_size_bytes) = 0;
37 // The constructor and destructor of cdm::FileIO are protected so that the CDM
38 // cannot delete the object directly. Here we declare the constructor and
39 // destructor as public so that we can use std::unique_ptr<> for better memory
41 MojoCdmFileIO(Delegate* delegate,
42 cdm::FileIOClient* client,
43 mojo::Remote<mojom::CdmStorage> cdm_storage);
44 MojoCdmFileIO(const MojoCdmFileIO&) = delete;
45 MojoCdmFileIO operator=(const MojoCdmFileIO&) = delete;
46 ~MojoCdmFileIO() override;
48 // cdm::FileIO implementation.
49 void Open(const char* file_name, uint32_t file_name_size) final;
51 void Write(const uint8_t* data, uint32_t data_size) final;
55 // Allowed state transitions:
56 // kUnopened -> kOpening -> kOpened
57 // kUnopened -> kOpening -> kUnopened (if file in use)
58 // kUnopened -> kOpening -> kError (if file not available)
59 // kOpened -> kReading -> kOpened
60 // kOpened -> kWriting -> kOpened
61 // Once state = kError, only Close() can be called.
62 enum class State { kUnopened, kOpening, kOpened, kReading, kWriting, kError };
64 // Error that needs to be reported back to the client.
65 enum class ErrorType {
74 // Called when the file is opened (or not).
75 void OnFileOpened(mojom::CdmStorage::Status status,
76 mojo::PendingAssociatedRemote<mojom::CdmFile> cdm_file);
78 // Called when the read operation is done.
79 void OnFileRead(mojom::CdmFile::Status status,
80 const std::vector<uint8_t>& data);
82 // Called when the write operation is done.
83 void OnFileWritten(mojom::CdmFile::Status status);
85 // Called when an error occurs. Calls client_->OnXxxxComplete with kError
86 // or kInUse asynchronously. In some cases we could actually call them
87 // synchronously, but since these errors shouldn't happen in normal cases,
88 // we are not optimizing such cases.
89 void OnError(ErrorType error);
91 // Callback to notify client of error asynchronously.
92 void NotifyClientOfError(ErrorType error);
94 raw_ptr<Delegate> delegate_ = nullptr;
96 // Results of cdm::FileIO operations are sent asynchronously via |client_|.
97 raw_ptr<cdm::FileIOClient, DanglingUntriaged> client_ = nullptr;
99 mojo::Remote<mojom::CdmStorage> cdm_storage_;
101 // Keep track of the file being used. As this class can only be used for
102 // accessing a single file, once |file_name_| is set it shouldn't be changed.
103 // |file_name_| is only saved for logging purposes.
104 std::string file_name_;
106 // |cdm_file_| is used to read and write the file and is released when the
107 // file is closed so that CdmStorage can tell that the file is no longer being
109 mojo::AssociatedRemote<mojom::CdmFile> cdm_file_;
111 // Keep track of operations in progress.
112 State state_ = State::kUnopened;
114 // NOTE: Weak pointers must be invalidated before all other member variables.
115 base::WeakPtrFactory<MojoCdmFileIO> weak_factory_{this};
120 #endif // MEDIA_MOJO_SERVICES_MOJO_CDM_FILE_IO_H_