[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
| 6 | #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
| 7 | |
| 8 | #include "base/basictypes.h" |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 13 | #include "net/base/completion_callback.h" |
[email protected] | 8688240 | 2013-10-16 10:28:10 | [diff] [blame] | 14 | #include "net/base/net_export.h" |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 15 | |
| 16 | namespace base { |
[email protected] | 0b5ab509 | 2013-11-12 01:09:56 | [diff] [blame] | 17 | class SequencedTaskRunner; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 18 | } // namespace base |
| 19 | |
| 20 | namespace net { |
| 21 | |
| 22 | class DrainableIOBuffer; |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 23 | class FileStream; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 24 | class IOBuffer; |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 25 | class URLFetcherFileWriter; |
| 26 | class URLFetcherStringWriter; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 27 | |
| 28 | // This class encapsulates all state involved in writing URLFetcher response |
| 29 | // bytes to the destination. |
[email protected] | 8688240 | 2013-10-16 10:28:10 | [diff] [blame] | 30 | class NET_EXPORT URLFetcherResponseWriter { |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 31 | public: |
| 32 | virtual ~URLFetcherResponseWriter() {} |
| 33 | |
| 34 | // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 35 | // be run later with the result. Calling this method again after a |
| 36 | // Initialize() success results in discarding already written data. |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 37 | virtual int Initialize(const CompletionCallback& callback) = 0; |
| 38 | |
| 39 | // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes |
| 40 | // written or an error code. If ERR_IO_PENDING is returned, |callback| will be |
| 41 | // run later with the result. |
| 42 | virtual int Write(IOBuffer* buffer, |
| 43 | int num_bytes, |
| 44 | const CompletionCallback& callback) = 0; |
| 45 | |
| 46 | // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run |
| 47 | // later with the result. |
| 48 | virtual int Finish(const CompletionCallback& callback) = 0; |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 49 | |
| 50 | // Returns this instance's pointer as URLFetcherStringWriter when possible. |
| 51 | virtual URLFetcherStringWriter* AsStringWriter(); |
| 52 | |
| 53 | // Returns this instance's pointer as URLFetcherFileWriter when possible. |
| 54 | virtual URLFetcherFileWriter* AsFileWriter(); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | // URLFetcherResponseWriter implementation for std::string. |
[email protected] | 8688240 | 2013-10-16 10:28:10 | [diff] [blame] | 58 | class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 59 | public: |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 60 | URLFetcherStringWriter(); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 61 | virtual ~URLFetcherStringWriter(); |
| 62 | |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 63 | const std::string& data() const { return data_; } |
| 64 | |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 65 | // URLFetcherResponseWriter overrides: |
| 66 | virtual int Initialize(const CompletionCallback& callback) OVERRIDE; |
| 67 | virtual int Write(IOBuffer* buffer, |
| 68 | int num_bytes, |
| 69 | const CompletionCallback& callback) OVERRIDE; |
| 70 | virtual int Finish(const CompletionCallback& callback) OVERRIDE; |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 71 | virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 72 | |
| 73 | private: |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 74 | std::string data_; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 75 | |
| 76 | DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); |
| 77 | }; |
| 78 | |
| 79 | // URLFetcherResponseWriter implementation for files. |
[email protected] | 8688240 | 2013-10-16 10:28:10 | [diff] [blame] | 80 | class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 81 | public: |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 82 | // |file_path| is used as the destination path. If |file_path| is empty, |
| 83 | // Initialize() will create a temporary file. |
[email protected] | 0b5ab509 | 2013-11-12 01:09:56 | [diff] [blame] | 84 | URLFetcherFileWriter( |
| 85 | scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 86 | const base::FilePath& file_path); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 87 | virtual ~URLFetcherFileWriter(); |
| 88 | |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 89 | const base::FilePath& file_path() const { return file_path_; } |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 90 | |
| 91 | // URLFetcherResponseWriter overrides: |
| 92 | virtual int Initialize(const CompletionCallback& callback) OVERRIDE; |
| 93 | virtual int Write(IOBuffer* buffer, |
| 94 | int num_bytes, |
| 95 | const CompletionCallback& callback) OVERRIDE; |
| 96 | virtual int Finish(const CompletionCallback& callback) OVERRIDE; |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 97 | virtual URLFetcherFileWriter* AsFileWriter() OVERRIDE; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 98 | |
| 99 | // Drops ownership of the file at |file_path_|. |
| 100 | // This class will not delete it or write to it again. |
| 101 | void DisownFile(); |
| 102 | |
[email protected] | 9225b06 | 2013-10-11 06:02:48 | [diff] [blame] | 103 | private: |
| 104 | // Called when a write has been done. |
| 105 | void DidWrite(const CompletionCallback& callback, int result); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 106 | |
| 107 | // Closes the file if it is open and then delete it. |
| 108 | void CloseAndDeleteFile(); |
| 109 | |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 110 | // Callback which gets the result of a temporary file creation. |
| 111 | void DidCreateTempFile(const CompletionCallback& callback, |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 112 | base::FilePath* temp_file_path, |
| 113 | bool success); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 114 | |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 115 | // Callback which gets the result of FileStream::Open. |
| 116 | void DidOpenFile(const CompletionCallback& callback, |
| 117 | int result); |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 118 | |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 119 | // Callback which gets the result of closing a file. |
| 120 | void CloseComplete(const CompletionCallback& callback, int result); |
| 121 | |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 122 | // Callbacks are created for use with base::FileUtilProxy. |
| 123 | base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_; |
| 124 | |
| 125 | // Task runner on which file operations should happen. |
[email protected] | 0b5ab509 | 2013-11-12 01:09:56 | [diff] [blame] | 126 | scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 127 | |
| 128 | // Destination file path. |
| 129 | // Initialize() creates a temporary file if this variable is empty. |
| 130 | base::FilePath file_path_; |
| 131 | |
| 132 | // True when this instance is responsible to delete the file at |file_path_|. |
| 133 | bool owns_file_; |
| 134 | |
[email protected] | d22bad4 | 2013-04-04 13:39:12 | [diff] [blame] | 135 | scoped_ptr<FileStream> file_stream_; |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 136 | |
[email protected] | fb501bc | 2013-03-05 08:38:15 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); |
| 138 | }; |
| 139 | |
| 140 | } // namespace net |
| 141 | |
| 142 | #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |