[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / mojo_cdm_file_io.h
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.
4
5 #ifndef MEDIA_MOJO_SERVICES_MOJO_CDM_FILE_IO_H_
6 #define MEDIA_MOJO_SERVICES_MOJO_CDM_FILE_IO_H_
7
8 #include <stdint.h>
9
10 #include <string>
11 #include <vector>
12
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"
22
23 namespace media {
24
25 // Implements a cdm::FileIO that communicates with mojom::CdmStorage.
26 class MEDIA_MOJO_EXPORT MojoCdmFileIO : public cdm::FileIO {
27  public:
28   class Delegate {
29    public:
30     // Notifies the delegate to close |cdm_file_io|.
31     virtual void CloseCdmFileIO(MojoCdmFileIO* cdm_file_io) = 0;
32
33     // Reports the size of file read by MojoCdmFileIO.
34     virtual void ReportFileReadSize(int file_size_bytes) = 0;
35   };
36
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
40   // management.
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;
47
48   // cdm::FileIO implementation.
49   void Open(const char* file_name, uint32_t file_name_size) final;
50   void Read() final;
51   void Write(const uint8_t* data, uint32_t data_size) final;
52   void Close() final;
53
54  private:
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 };
63
64   // Error that needs to be reported back to the client.
65   enum class ErrorType {
66     kOpenError,
67     kOpenInUse,
68     kReadError,
69     kReadInUse,
70     kWriteError,
71     kWriteInUse
72   };
73
74   // Called when the file is opened (or not).
75   void OnFileOpened(mojom::CdmStorage::Status status,
76                     mojo::PendingAssociatedRemote<mojom::CdmFile> cdm_file);
77
78   // Called when the read operation is done.
79   void OnFileRead(mojom::CdmFile::Status status,
80                   const std::vector<uint8_t>& data);
81
82   // Called when the write operation is done.
83   void OnFileWritten(mojom::CdmFile::Status status);
84
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);
90
91   // Callback to notify client of error asynchronously.
92   void NotifyClientOfError(ErrorType error);
93
94   raw_ptr<Delegate> delegate_ = nullptr;
95
96   // Results of cdm::FileIO operations are sent asynchronously via |client_|.
97   raw_ptr<cdm::FileIOClient, DanglingUntriaged> client_ = nullptr;
98
99   mojo::Remote<mojom::CdmStorage> cdm_storage_;
100
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_;
105
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
108   // used.
109   mojo::AssociatedRemote<mojom::CdmFile> cdm_file_;
110
111   // Keep track of operations in progress.
112   State state_ = State::kUnopened;
113
114   // NOTE: Weak pointers must be invalidated before all other member variables.
115   base::WeakPtrFactory<MojoCdmFileIO> weak_factory_{this};
116 };
117
118 }  // namespace media
119
120 #endif  // MEDIA_MOJO_SERVICES_MOJO_CDM_FILE_IO_H_