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" 9 #include "base/files/file_path.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "net/base/completion_callback.h" 14 #include "net/base/net_export.h" 15 16 namespace base { 17 class SequencedTaskRunner; 18 } // namespace base 19 20 namespace net { 21 22 class DrainableIOBuffer; 23 class FileStream; 24 class IOBuffer; 25 class URLFetcherFileWriter; 26 class URLFetcherStringWriter; 27 28 // This class encapsulates all state involved in writing URLFetcher response 29 // bytes to the destination. 30 class NET_EXPORT URLFetcherResponseWriter { 31 public: 32 virtual ~URLFetcherResponseWriter() {} 33 34 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will 35 // be run later with the result. Calling this method again after a 36 // Initialize() success results in discarding already written data. 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; 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(); 55 }; 56 57 // URLFetcherResponseWriter implementation for std::string. 58 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { 59 public: 60 URLFetcherStringWriter(); 61 virtual ~URLFetcherStringWriter(); 62 63 const std::string& data() const { return data_; } 64 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; 71 virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE; 72 73 private: 74 std::string data_; 75 76 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); 77 }; 78 79 // URLFetcherResponseWriter implementation for files. 80 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { 81 public: 82 // |file_path| is used as the destination path. If |file_path| is empty, 83 // Initialize() will create a temporary file. 84 URLFetcherFileWriter( 85 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 86 const base::FilePath& file_path); 87 virtual ~URLFetcherFileWriter(); 88 89 const base::FilePath& file_path() const { return file_path_; } 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; 97 virtual URLFetcherFileWriter* AsFileWriter() OVERRIDE; 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 103 private: 104 // Called when a write has been done. 105 void DidWrite(const CompletionCallback& callback, int result); 106 107 // Closes the file if it is open and then delete it. 108 void CloseAndDeleteFile(); 109 110 // Callback which gets the result of a temporary file creation. 111 void DidCreateTempFile(const CompletionCallback& callback, 112 base::FilePath* temp_file_path, 113 bool success); 114 115 // Callback which gets the result of FileStream::Open. 116 void DidOpenFile(const CompletionCallback& callback, 117 int result); 118 119 // Callback which gets the result of closing a file. 120 void CloseComplete(const CompletionCallback& callback, int result); 121 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. 126 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 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 135 scoped_ptr<FileStream> file_stream_; 136 137 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); 138 }; 139 140 } // namespace net 141 142 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ 143