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