| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ | 5 #ifndef MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
| 6 #define MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ | 6 #define MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "media/cdm/ppapi/api/content_decryption_module.h" | 13 #include "media/cdm/ppapi/api/content_decryption_module.h" |
| 14 #include "ppapi/c/ppb_file_io.h" | 14 #include "ppapi/c/ppb_file_io.h" |
| 15 #include "ppapi/cpp/file_io.h" | 15 #include "ppapi/cpp/file_io.h" |
| 16 #include "ppapi/cpp/file_ref.h" | 16 #include "ppapi/cpp/file_ref.h" |
| 17 #include "ppapi/cpp/instance.h" | 17 #include "ppapi/cpp/instance.h" |
| 18 #include "ppapi/cpp/module.h" | 18 #include "ppapi/cpp/module.h" |
| 19 #include "ppapi/cpp/private/isolated_file_system_private.h" | 19 #include "ppapi/cpp/private/isolated_file_system_private.h" |
| 20 #include "ppapi/utility/completion_callback_factory.h" | 20 #include "ppapi/utility/completion_callback_factory.h" |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 | 23 |
| 24 // Due to PPAPI limitations, all functions must be called on the main thread. | 24 // Due to PPAPI limitations, all functions must be called on the main thread. |
| 25 // |
| 26 // Implementation notes about states: |
| 27 // 1, When a method is called in an invalid state (e.g. Read() before Open() is |
| 28 // called, Write() before Open() finishes or Open() after Open()), kError |
| 29 // will be returned. The state of |this| will not change. |
| 30 // 2, When the file is opened by another CDM instance, or when we call Read()/ |
| 31 // Write() during a pending Read()/Write(), kInUse will be returned. The |
| 32 // state of |this| will not change. |
| 33 // 3, When a pepper operation failed (either synchronously or asynchronously), |
| 34 // kError will be returned. The state of |this| will be set to ERROR. |
| 35 // 4. Any operation in ERROR state will end up with kError. |
| 25 class CdmFileIOImpl : public cdm::FileIO { | 36 class CdmFileIOImpl : public cdm::FileIO { |
| 26 public: | 37 public: |
| 27 // A class that helps release |file_lock_map_|. | 38 // A class that helps release |file_lock_map_|. |
| 28 // There should be only one instance of ResourceTracker in a process. Also, | 39 // There should be only one instance of ResourceTracker in a process. Also, |
| 29 // ResourceTracker should outlive all CdmFileIOImpl instances. | 40 // ResourceTracker should outlive all CdmFileIOImpl instances. |
| 30 class ResourceTracker { | 41 class ResourceTracker { |
| 31 public: | 42 public: |
| 32 ResourceTracker(); | 43 ResourceTracker(); |
| 33 ~ResourceTracker(); | 44 ~ResourceTracker(); |
| 34 private: | 45 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); | 46 DISALLOW_COPY_AND_ASSIGN(ResourceTracker); |
| 36 }; | 47 }; |
| 37 | 48 |
| 38 // After the first successful file read, call |first_file_read_cb| to report | 49 // After the first successful file read, call |first_file_read_cb| to report |
| 39 // the file size. |first_file_read_cb| takes one parameter: the file size in | 50 // the file size. |first_file_read_cb| takes one parameter: the file size in |
| 40 // bytes. | 51 // bytes. |
| 41 CdmFileIOImpl(cdm::FileIOClient* client, | 52 CdmFileIOImpl(cdm::FileIOClient* client, |
| 42 PP_Instance pp_instance, | 53 PP_Instance pp_instance, |
| 43 const pp::CompletionCallback& first_file_read_cb); | 54 const pp::CompletionCallback& first_file_read_cb); |
| 44 | 55 |
| 45 // cdm::FileIO implementation. | 56 // cdm::FileIO implementation. |
| 46 virtual void Open(const char* file_name, uint32_t file_name_size) OVERRIDE; | 57 virtual void Open(const char* file_name, uint32_t file_name_size) OVERRIDE; |
| 47 virtual void Read() OVERRIDE; | 58 virtual void Read() OVERRIDE; |
| 48 virtual void Write(const uint8_t* data, uint32_t data_size) OVERRIDE; | 59 virtual void Write(const uint8_t* data, uint32_t data_size) OVERRIDE; |
| 49 virtual void Close() OVERRIDE; | 60 virtual void Close() OVERRIDE; |
| 50 | 61 |
| 51 private: | 62 private: |
| 63 // TODO(xhwang): Introduce more detailed states for UMA logging if needed. |
| 52 enum State { | 64 enum State { |
| 53 FILE_UNOPENED, | 65 FILE_UNOPENED, |
| 54 OPENING_FILE_SYSTEM, | 66 OPENING_FILE_SYSTEM, |
| 55 OPENING_FILE, | |
| 56 FILE_OPENED, | 67 FILE_OPENED, |
| 57 READING_FILE, | 68 READING_FILE, |
| 58 WRITING_FILE, | 69 WRITING_FILE, |
| 59 FILE_CLOSED | 70 FILE_CLOSED, |
| 71 ERROR |
| 60 }; | 72 }; |
| 61 | 73 |
| 62 enum ErrorType { | 74 enum ErrorType { |
| 63 OPEN_WHILE_IN_USE, | 75 OPEN_WHILE_IN_USE, |
| 64 READ_WHILE_IN_USE, | 76 READ_WHILE_IN_USE, |
| 65 WRITE_WHILE_IN_USE, | 77 WRITE_WHILE_IN_USE, |
| 66 OPEN_ERROR, | 78 OPEN_ERROR, |
| 67 READ_ERROR, | 79 READ_ERROR, |
| 68 WRITE_ERROR | 80 WRITE_ERROR |
| 69 }; | 81 }; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 99 | 111 |
| 100 // Acquires the file lock. Returns true if the lock is successfully acquired. | 112 // Acquires the file lock. Returns true if the lock is successfully acquired. |
| 101 // After the lock is acquired, other cdm::FileIO objects in the same process | 113 // After the lock is acquired, other cdm::FileIO objects in the same process |
| 102 // and in the same origin will get kInUse when trying to open the same file. | 114 // and in the same origin will get kInUse when trying to open the same file. |
| 103 bool AcquireFileLock(); | 115 bool AcquireFileLock(); |
| 104 | 116 |
| 105 // Releases the file lock so that the file can be opened by other cdm::FileIO | 117 // Releases the file lock so that the file can be opened by other cdm::FileIO |
| 106 // objects. | 118 // objects. |
| 107 void ReleaseFileLock(); | 119 void ReleaseFileLock(); |
| 108 | 120 |
| 121 // Helper functions for Open(). |
| 109 void OpenFileSystem(); | 122 void OpenFileSystem(); |
| 110 void OnFileSystemOpened(int32_t result, pp::FileSystem file_system); | 123 void OnFileSystemOpened(int32_t result, pp::FileSystem file_system); |
| 111 void OpenFile(); | 124 |
| 112 void OnFileOpened(int32_t result); | 125 // Helper functions for Read(). |
| 126 void OpenFileForRead(); |
| 127 void OnFileOpenedForRead(int32_t result); |
| 113 void ReadFile(); | 128 void ReadFile(); |
| 114 void OnFileRead(int32_t bytes_read); | 129 void OnFileRead(int32_t bytes_read); |
| 115 void SetLength(uint32_t length); | |
| 116 void OnLengthSet(int32_t result); | |
| 117 void WriteFile(); | |
| 118 void OnFileWritten(int32_t bytes_written); | |
| 119 | 130 |
| 120 void CloseFile(); | 131 // Helper functions for Write(). We always write data to a temporary file, |
| 132 // then rename the temporary file to the target file. This can prevent data |
| 133 // corruption if |this| is closed while waiting for writing to complete. |
| 134 void OpenTempFileForWrite(); |
| 135 void OnTempFileOpenedForWrite(int32_t result); |
| 136 void WriteTempFile(); |
| 137 void OnTempFileWritten(int32_t bytes_written); |
| 138 void RenameTempFile(); |
| 139 void OnTempFileRenamed(int32_t result); |
| 121 | 140 |
| 122 // Calls client_->OnXxxxComplete with kError asynchronously. In some cases we | 141 // Reset |this| to a clean state. |
| 123 // could actually call them synchronously, but since these errors shouldn't | 142 void Reset(); |
| 124 // happen in normal cases, we are not optimizing such cases. | 143 |
| 144 // For real open/read/write errors, Reset() and set the |state_| to ERROR. |
| 145 // Calls client_->OnXxxxComplete with kError or kInUse asynchronously. In some |
| 146 // cases we could actually call them synchronously, but since these errors |
| 147 // shouldn't happen in normal cases, we are not optimizing such cases. |
| 125 void OnError(ErrorType error_type); | 148 void OnError(ErrorType error_type); |
| 126 | 149 |
| 127 // Callback to notify client of error asynchronously. | 150 // Callback to notify client of error asynchronously. |
| 128 void NotifyClientOfError(int32_t result, ErrorType error_type); | 151 void NotifyClientOfError(int32_t result, ErrorType error_type); |
| 129 | 152 |
| 130 State state_; | 153 State state_; |
| 131 | 154 |
| 132 // Non-owning pointer. | 155 // Non-owning pointer. |
| 133 cdm::FileIOClient* const client_; | 156 cdm::FileIOClient* const client_; |
| 134 | 157 |
| 135 const pp::InstanceHandle pp_instance_handle_; | 158 const pp::InstanceHandle pp_instance_handle_; |
| 136 | 159 |
| 160 // Format: /<original_file_name> |
| 137 std::string file_name_; | 161 std::string file_name_; |
| 138 | 162 |
| 139 // A string ID that uniquely identifies a file in the user's profile. | 163 // A string ID that uniquely identifies a file in the user's profile. |
| 140 // It consists of the origin of the document URL (including scheme, host and | 164 // It consists of the origin of the document URL (including scheme, host and |
| 141 // port, delimited by colons) and the |file_name_|. | 165 // port, delimited by colons) and the |file_name_|. |
| 142 // For example: http:example.com:8080/foo_file.txt | 166 // For example: http:example.com:8080/foo_file.txt |
| 143 std::string file_id_; | 167 std::string file_id_; |
| 144 | 168 |
| 145 pp::IsolatedFileSystemPrivate isolated_file_system_; | 169 pp::IsolatedFileSystemPrivate isolated_file_system_; |
| 146 pp::FileSystem file_system_; | 170 pp::FileSystem file_system_; |
| 171 |
| 172 // Shared between read and write. During read, |file_ref_| refers to the real |
| 173 // file to read data from. During write, it refers to the temporary file to |
| 174 // write data into. |
| 147 pp::FileIO file_io_; | 175 pp::FileIO file_io_; |
| 148 pp::FileRef file_ref_; | 176 pp::FileRef file_ref_; |
| 149 | 177 |
| 150 pp::CompletionCallbackFactory<CdmFileIOImpl> callback_factory_; | |
| 151 | |
| 152 // A temporary buffer to hold (partial) data to write or the data that has | 178 // A temporary buffer to hold (partial) data to write or the data that has |
| 153 // been read. The size of |io_buffer_| is always "bytes to write" or "bytes to | 179 // been read. The size of |io_buffer_| is always "bytes to write" or "bytes to |
| 154 // read". Use "char" instead of "unit8_t" because PPB_FileIO uses char* for | 180 // read". Use "char" instead of "unit8_t" because PPB_FileIO uses char* for |
| 155 // binary data read and write. | 181 // binary data read and write. |
| 156 std::vector<char> io_buffer_; | 182 std::vector<char> io_buffer_; |
| 157 | 183 |
| 158 // Offset into the file for reading/writing data. When writing data to the | 184 // Offset into the file for reading/writing data. When writing data to the |
| 159 // file, this is also the offset to the |io_buffer_|. | 185 // file, this is also the offset to the |io_buffer_|. |
| 160 size_t io_offset_; | 186 size_t io_offset_; |
| 161 | 187 |
| 162 // Buffer to hold all read data requested. This buffer is passed to |client_| | 188 // Buffer to hold all read data requested. This buffer is passed to |client_| |
| 163 // when read completes. | 189 // when read completes. |
| 164 std::vector<char> cumulative_read_buffer_; | 190 std::vector<char> cumulative_read_buffer_; |
| 165 | 191 |
| 166 bool first_file_read_reported_; | 192 bool first_file_read_reported_; |
| 167 | 193 |
| 168 // Callback to report the file size in bytes after the first successful read. | 194 // Callback to report the file size in bytes after the first successful read. |
| 169 pp::CompletionCallback first_file_read_cb_; | 195 pp::CompletionCallback first_file_read_cb_; |
| 170 | 196 |
| 197 pp::CompletionCallbackFactory<CdmFileIOImpl> callback_factory_; |
| 198 |
| 171 DISALLOW_COPY_AND_ASSIGN(CdmFileIOImpl); | 199 DISALLOW_COPY_AND_ASSIGN(CdmFileIOImpl); |
| 172 }; | 200 }; |
| 173 | 201 |
| 174 } // namespace media | 202 } // namespace media |
| 175 | 203 |
| 176 #endif // MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ | 204 #endif // MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_ |
| OLD | NEW |