| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_
H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_
H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_
H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_
H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "webkit/browser/fileapi/file_stream_writer.h" | 13 #include "webkit/browser/fileapi/file_stream_writer.h" |
| 14 #include "webkit/browser/fileapi/file_system_url.h" | 14 #include "webkit/browser/fileapi/file_system_url.h" |
| 15 | 15 |
| 16 namespace chromeos { | 16 namespace chromeos { |
| 17 namespace file_system_provider { | 17 namespace file_system_provider { |
| 18 | 18 |
| 19 class ProvidedFileSystemInterface; | 19 class ProvidedFileSystemInterface; |
| 20 | 20 |
| 21 // Implements a streamed file writer. It is lazily initialized by the first call | 21 // Implements a streamed file writer. It is lazily initialized by the first call |
| 22 // to Write(). | 22 // to Write(). |
| 23 class FileStreamWriter : public fileapi::FileStreamWriter { | 23 class FileStreamWriter : public fileapi::FileStreamWriter { |
| 24 public: | 24 public: |
| 25 typedef base::Callback< | |
| 26 void(base::WeakPtr<ProvidedFileSystemInterface> file_system, | |
| 27 const base::FilePath& file_path, | |
| 28 int file_handle, | |
| 29 base::File::Error result)> OpenFileCompletedCallback; | |
| 30 | |
| 31 FileStreamWriter(const fileapi::FileSystemURL& url, int64 initial_offset); | 25 FileStreamWriter(const fileapi::FileSystemURL& url, int64 initial_offset); |
| 32 | 26 |
| 33 virtual ~FileStreamWriter(); | 27 virtual ~FileStreamWriter(); |
| 34 | 28 |
| 35 // fileapi::FileStreamWriter overrides. | 29 // fileapi::FileStreamWriter overrides. |
| 36 virtual int Write(net::IOBuffer* buf, | 30 virtual int Write(net::IOBuffer* buf, |
| 37 int buf_len, | 31 int buf_len, |
| 38 const net::CompletionCallback& callback) OVERRIDE; | 32 const net::CompletionCallback& callback) OVERRIDE; |
| 39 virtual int Cancel(const net::CompletionCallback& callback) OVERRIDE; | 33 virtual int Cancel(const net::CompletionCallback& callback) OVERRIDE; |
| 40 virtual int Flush(const net::CompletionCallback& callback) OVERRIDE; | 34 virtual int Flush(const net::CompletionCallback& callback) OVERRIDE; |
| 41 | 35 |
| 42 private: | 36 private: |
| 37 // Helper class for executing operations on the provided file system. All |
| 38 // of its methods must be called on UI thread. Callbacks are called on IO |
| 39 // thread. |
| 40 class OperationRunner; |
| 41 |
| 43 // State of the file stream writer. | 42 // State of the file stream writer. |
| 44 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED }; | 43 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED }; |
| 45 | 44 |
| 45 // Called when OperationRunner::WriteOnUIThread is completed. |
| 46 void OnWriteFileCompleted(int buffer_length, | 46 void OnWriteFileCompleted(int buffer_length, |
| 47 const net::CompletionCallback& callback, | 47 const net::CompletionCallback& callback, |
| 48 base::File::Error result); | 48 base::File::Error result); |
| 49 | 49 |
| 50 // Called when Write() operation is completed with either a success of an | 50 // Called when Write() operation is completed with either a success or an |
| 51 // error. | 51 // error. |
| 52 void OnWriteCompleted(net::CompletionCallback callback, int result); | 52 void OnWriteCompleted(net::CompletionCallback callback, int result); |
| 53 | 53 |
| 54 // Called when Abort() operation is completed with either a success or an |
| 55 // error. |
| 56 void OnAbortCompleted(const net::CompletionCallback& callback, |
| 57 base::File::Error result); |
| 58 |
| 54 // Initializes the writer by opening the file. When completed with success, | 59 // Initializes the writer by opening the file. When completed with success, |
| 55 // runs the |pending_closure|. Otherwise, calls the |error_callback|. | 60 // runs the |pending_closure|. Otherwise, calls the |error_callback|. |
| 56 void Initialize(const base::Closure& pending_closure, | 61 void Initialize(const base::Closure& pending_closure, |
| 57 const net::CompletionCallback& error_callback); | 62 const net::CompletionCallback& error_callback); |
| 58 | 63 |
| 59 // Called when opening a file is completed with either a success or an error. | 64 // Called when opening a file is completed with either a success or an error. |
| 60 void OnOpenFileCompleted( | 65 void OnOpenFileCompleted( |
| 61 const base::Closure& pending_closure, | 66 const base::Closure& pending_closure, |
| 62 const net::CompletionCallback& error_callback, | 67 const net::CompletionCallback& error_callback, |
| 63 base::WeakPtr<ProvidedFileSystemInterface> file_system, | |
| 64 const base::FilePath& file_path, | |
| 65 int file_handle, | |
| 66 base::File::Error result); | 68 base::File::Error result); |
| 67 | 69 |
| 68 // Same as Write(), but called after initializing is completed. | 70 // Same as Write(), but called after initializing is completed. |
| 69 void WriteAfterInitialized(scoped_refptr<net::IOBuffer> buffer, | 71 void WriteAfterInitialized(scoped_refptr<net::IOBuffer> buffer, |
| 70 int buffer_length, | 72 int buffer_length, |
| 71 const net::CompletionCallback& callback); | 73 const net::CompletionCallback& callback); |
| 72 | 74 |
| 73 fileapi::FileSystemURL url_; | 75 fileapi::FileSystemURL url_; |
| 74 int64 current_offset_; | 76 int64 current_offset_; |
| 77 scoped_refptr<OperationRunner> runner_; |
| 75 State state_; | 78 State state_; |
| 76 | 79 |
| 77 // Set during initialization (in case of a success). | |
| 78 base::WeakPtr<ProvidedFileSystemInterface> file_system_; | |
| 79 base::FilePath file_path_; | |
| 80 int file_handle_; | |
| 81 | |
| 82 base::WeakPtrFactory<FileStreamWriter> weak_ptr_factory_; | 80 base::WeakPtrFactory<FileStreamWriter> weak_ptr_factory_; |
| 83 DISALLOW_COPY_AND_ASSIGN(FileStreamWriter); | 81 DISALLOW_COPY_AND_ASSIGN(FileStreamWriter); |
| 84 }; | 82 }; |
| 85 | 83 |
| 86 } // namespace file_system_provider | 84 } // namespace file_system_provider |
| 87 } // namespace chromeos | 85 } // namespace chromeos |
| 88 | 86 |
| 89 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRIT
ER_H_ | 87 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRIT
ER_H_ |
| OLD | NEW |