OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
| 6 #define MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "media/cdm/ppapi/api/content_decryption_module.h" |
| 14 #include "ppapi/c/ppb_file_io.h" |
| 15 #include "ppapi/cpp/file_io.h" |
| 16 #include "ppapi/cpp/file_ref.h" |
| 17 #include "ppapi/cpp/instance.h" |
| 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/private/isolated_file_system_private.h" |
| 20 #include "ppapi/utility/completion_callback_factory.h" |
| 21 |
| 22 namespace media { |
| 23 |
| 24 // Due to PPAPI limitations, all functions must be called on the main thread. |
| 25 class CdmFileIOImpl : public cdm::FileIO { |
| 26 public: |
| 27 CdmFileIOImpl(cdm::FileIOClient* client, PP_Instance pp_instance); |
| 28 virtual ~CdmFileIOImpl(); |
| 29 |
| 30 // cdm::FileIO implementation. |
| 31 virtual void Open(const char* file_name, uint32_t file_name_size) OVERRIDE; |
| 32 virtual void Read() OVERRIDE; |
| 33 virtual void Write(const uint8_t* data, uint32_t data_size) OVERRIDE; |
| 34 virtual void Close() OVERRIDE; |
| 35 |
| 36 private: |
| 37 enum State { |
| 38 FILE_UNOPENED, |
| 39 OPENING_FILE_SYSTEM, |
| 40 OPENING_FILE, |
| 41 FILE_OPENED, |
| 42 READING_FILE, |
| 43 WRITING_FILE, |
| 44 FILE_CLOSED |
| 45 }; |
| 46 |
| 47 enum ErrorType { |
| 48 OPEN_WHILE_IN_USE, |
| 49 READ_WHILE_IN_USE, |
| 50 WRITE_WHILE_IN_USE, |
| 51 OPEN_ERROR, |
| 52 READ_ERROR, |
| 53 WRITE_ERROR |
| 54 }; |
| 55 |
| 56 void OpenFileSystem(); |
| 57 void OnFileSystemOpened(int32_t result, pp::FileSystem file_system); |
| 58 void OpenFile(); |
| 59 void OnFileOpened(int32_t result); |
| 60 void ReadFile(); |
| 61 void OnFileRead(int32_t bytes_read); |
| 62 void SetLength(uint32_t length); |
| 63 void OnLengthSet(int32_t result); |
| 64 void WriteFile(); |
| 65 void OnFileWritten(int32_t bytes_written); |
| 66 |
| 67 void CloseFile(); |
| 68 |
| 69 // Calls client_->OnXxxxComplete with kError asynchronously. In some cases we |
| 70 // could actually call them synchronously, but since these errors shouldn't |
| 71 // happen in normal cases, we are not optimizing such cases. |
| 72 void OnError(ErrorType error_type); |
| 73 // Callback to notify client of error asynchronously. |
| 74 void NotifyClientOfError(int32_t result, ErrorType error_type); |
| 75 |
| 76 State state_; |
| 77 |
| 78 // Non-owning pointer. |
| 79 cdm::FileIOClient* const client_; |
| 80 |
| 81 const pp::InstanceHandle pp_instance_handle_; |
| 82 |
| 83 std::string file_name_; |
| 84 pp::IsolatedFileSystemPrivate isolated_file_system_; |
| 85 pp::FileSystem file_system_; |
| 86 pp::FileIO file_io_; |
| 87 pp::FileRef file_ref_; |
| 88 |
| 89 pp::CompletionCallbackFactory<CdmFileIOImpl> callback_factory_; |
| 90 |
| 91 // A temporary buffer to hold (partial) data to write or the data that has |
| 92 // been read. The size of |io_buffer_| is always "bytes to write" or "bytes to |
| 93 // read". Use "char" instead of "unit8_t" because PPB_FileIO uses char* for |
| 94 // binary data read and write. |
| 95 std::vector<char> io_buffer_; |
| 96 |
| 97 // Offset into the file for reading/writing data. When writing data to the |
| 98 // file, this is also the offset to the |io_buffer_|. |
| 99 size_t io_offset_; |
| 100 |
| 101 // Buffer to hold all read data requested. This buffer is passed to |client_| |
| 102 // when read completes. |
| 103 std::vector<char> cumulative_read_buffer_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(CdmFileIOImpl); |
| 106 }; |
| 107 |
| 108 } // namespace media |
| 109 |
| 110 #endif // MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
OLD | NEW |