| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 5 #ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
| 6 #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 6 #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 // Initialize() success results in discarding already written data. | 38 // Initialize() success results in discarding already written data. |
| 39 virtual int Initialize(const CompletionCallback& callback) = 0; | 39 virtual int Initialize(const CompletionCallback& callback) = 0; |
| 40 | 40 |
| 41 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes | 41 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes |
| 42 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be | 42 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be |
| 43 // run later with the result. | 43 // run later with the result. |
| 44 virtual int Write(IOBuffer* buffer, | 44 virtual int Write(IOBuffer* buffer, |
| 45 int num_bytes, | 45 int num_bytes, |
| 46 const CompletionCallback& callback) = 0; | 46 const CompletionCallback& callback) = 0; |
| 47 | 47 |
| 48 // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run | 48 // Finishes writing. If |net_error| is not OK, this method can be called |
| 49 // later with the result. | 49 // in the middle of another operation (eg. Initialize() and Write()). On |
| 50 virtual int Finish(const CompletionCallback& callback) = 0; | 50 // errors (|net_error| not OK), this method may be called before the previous |
| 51 // operation completed. In this case, URLFetcherResponseWriter may skip |
| 52 // graceful shutdown and completion of the pending operation. After such a |
| 53 // failure, the URLFetcherResponseWriter may be reused. If ERR_IO_PENDING is |
| 54 // returned, |callback| will be run later with the result. |
| 55 virtual int Finish(int net_error, const CompletionCallback& callback) = 0; |
| 51 | 56 |
| 52 // Returns this instance's pointer as URLFetcherStringWriter when possible. | 57 // Returns this instance's pointer as URLFetcherStringWriter when possible. |
| 53 virtual URLFetcherStringWriter* AsStringWriter(); | 58 virtual URLFetcherStringWriter* AsStringWriter(); |
| 54 | 59 |
| 55 // Returns this instance's pointer as URLFetcherFileWriter when possible. | 60 // Returns this instance's pointer as URLFetcherFileWriter when possible. |
| 56 virtual URLFetcherFileWriter* AsFileWriter(); | 61 virtual URLFetcherFileWriter* AsFileWriter(); |
| 57 }; | 62 }; |
| 58 | 63 |
| 59 // URLFetcherResponseWriter implementation for std::string. | 64 // URLFetcherResponseWriter implementation for std::string. |
| 60 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { | 65 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { |
| 61 public: | 66 public: |
| 62 URLFetcherStringWriter(); | 67 URLFetcherStringWriter(); |
| 63 ~URLFetcherStringWriter() override; | 68 ~URLFetcherStringWriter() override; |
| 64 | 69 |
| 65 const std::string& data() const { return data_; } | 70 const std::string& data() const { return data_; } |
| 66 | 71 |
| 67 // URLFetcherResponseWriter overrides: | 72 // URLFetcherResponseWriter overrides: |
| 68 int Initialize(const CompletionCallback& callback) override; | 73 int Initialize(const CompletionCallback& callback) override; |
| 69 int Write(IOBuffer* buffer, | 74 int Write(IOBuffer* buffer, |
| 70 int num_bytes, | 75 int num_bytes, |
| 71 const CompletionCallback& callback) override; | 76 const CompletionCallback& callback) override; |
| 72 int Finish(const CompletionCallback& callback) override; | 77 int Finish(int net_error, const CompletionCallback& callback) override; |
| 73 URLFetcherStringWriter* AsStringWriter() override; | 78 URLFetcherStringWriter* AsStringWriter() override; |
| 74 | 79 |
| 75 private: | 80 private: |
| 76 std::string data_; | 81 std::string data_; |
| 77 | 82 |
| 78 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); | 83 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); |
| 79 }; | 84 }; |
| 80 | 85 |
| 81 // URLFetcherResponseWriter implementation for files. | 86 // URLFetcherResponseWriter implementation for files. |
| 82 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { | 87 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { |
| 83 public: | 88 public: |
| 84 // |file_path| is used as the destination path. If |file_path| is empty, | 89 // |file_path| is used as the destination path. If |file_path| is empty, |
| 85 // Initialize() will create a temporary file. | 90 // Initialize() will create a temporary file. |
| 86 URLFetcherFileWriter( | 91 URLFetcherFileWriter( |
| 87 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 92 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 88 const base::FilePath& file_path); | 93 const base::FilePath& file_path); |
| 89 ~URLFetcherFileWriter() override; | 94 ~URLFetcherFileWriter() override; |
| 90 | 95 |
| 91 const base::FilePath& file_path() const { return file_path_; } | 96 const base::FilePath& file_path() const { return file_path_; } |
| 92 | 97 |
| 93 // URLFetcherResponseWriter overrides: | 98 // URLFetcherResponseWriter overrides: |
| 94 int Initialize(const CompletionCallback& callback) override; | 99 int Initialize(const CompletionCallback& callback) override; |
| 95 int Write(IOBuffer* buffer, | 100 int Write(IOBuffer* buffer, |
| 96 int num_bytes, | 101 int num_bytes, |
| 97 const CompletionCallback& callback) override; | 102 const CompletionCallback& callback) override; |
| 98 int Finish(const CompletionCallback& callback) override; | 103 int Finish(int net_error, const CompletionCallback& callback) override; |
| 99 URLFetcherFileWriter* AsFileWriter() override; | 104 URLFetcherFileWriter* AsFileWriter() override; |
| 100 | 105 |
| 101 // Drops ownership of the file at |file_path_|. | 106 // Drops ownership of the file at |file_path_|. |
| 102 // This class will not delete it or write to it again. | 107 // This class will not delete it or write to it again. |
| 103 void DisownFile(); | 108 void DisownFile(); |
| 104 | 109 |
| 105 private: | 110 private: |
| 106 // Called when a write has been done. | |
| 107 void DidWrite(const CompletionCallback& callback, int result); | |
| 108 | |
| 109 // Closes the file if it is open and then delete it. | 111 // Closes the file if it is open and then delete it. |
| 110 void CloseAndDeleteFile(); | 112 void CloseAndDeleteFile(); |
| 111 | 113 |
| 112 // Callback which gets the result of a temporary file creation. | 114 // Callback which gets the result of a temporary file creation. |
| 113 void DidCreateTempFile(const CompletionCallback& callback, | 115 void DidCreateTempFile(base::FilePath* temp_file_path, bool success); |
| 114 base::FilePath* temp_file_path, | |
| 115 bool success); | |
| 116 | 116 |
| 117 // Callback which gets the result of FileStream::Open. | 117 // Run |callback_| if it is non-null when FileStream::Open or |
| 118 void DidOpenFile(const CompletionCallback& callback, | 118 // FileStream::Write is completed. |
| 119 int result); | 119 void OnIOCompleted(int result); |
| 120 | 120 |
| 121 // Callback which gets the result of closing a file. | 121 // Callback which gets the result of closing a file. |
| 122 void CloseComplete(const CompletionCallback& callback, int result); | 122 void CloseComplete(int result); |
| 123 | 123 |
| 124 // Task runner on which file operations should happen. | 124 // Task runner on which file operations should happen. |
| 125 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 125 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
| 126 | 126 |
| 127 // Destination file path. | 127 // Destination file path. |
| 128 // Initialize() creates a temporary file if this variable is empty. | 128 // Initialize() creates a temporary file if this variable is empty. |
| 129 base::FilePath file_path_; | 129 base::FilePath file_path_; |
| 130 | 130 |
| 131 // True when this instance is responsible to delete the file at |file_path_|. | 131 // True when this instance is responsible to delete the file at |file_path_|. |
| 132 bool owns_file_; | 132 bool owns_file_; |
| 133 | 133 |
| 134 std::unique_ptr<FileStream> file_stream_; | 134 std::unique_ptr<FileStream> file_stream_; |
| 135 | 135 |
| 136 CompletionCallback callback_; |
| 137 |
| 136 // Callbacks are created for use with base::FileUtilProxy. | 138 // Callbacks are created for use with base::FileUtilProxy. |
| 137 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_; | 139 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_; |
| 138 | 140 |
| 139 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); | 141 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); |
| 140 }; | 142 }; |
| 141 | 143 |
| 142 } // namespace net | 144 } // namespace net |
| 143 | 145 |
| 144 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 146 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
| OLD | NEW |