Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: net/url_request/url_fetcher_response_writer.h

Issue 2425673006: Make URLFetcherFileWriter::Finish() skip closing file when there is an error (Closed)
Patch Set: Cancel pending callback when Finish(net_error<0) is called Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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()). If
50 virtual int Finish(const CompletionCallback& callback) = 0; 50 // |net_error| is not OK, graceful shutdown might be skipped. If
mmenke 2016/10/19 20:49:34 nit: Maybe reword the 2nd and third sentences? "
xunjieli 2016/10/20 15:57:49 Done.
51 // ERR_IO_PENDING is returned, |callback| will be run later with the result.
52 virtual int Finish(int net_error, const CompletionCallback& callback) = 0;
51 53
52 // Returns this instance's pointer as URLFetcherStringWriter when possible. 54 // Returns this instance's pointer as URLFetcherStringWriter when possible.
53 virtual URLFetcherStringWriter* AsStringWriter(); 55 virtual URLFetcherStringWriter* AsStringWriter();
54 56
55 // Returns this instance's pointer as URLFetcherFileWriter when possible. 57 // Returns this instance's pointer as URLFetcherFileWriter when possible.
56 virtual URLFetcherFileWriter* AsFileWriter(); 58 virtual URLFetcherFileWriter* AsFileWriter();
57 }; 59 };
58 60
59 // URLFetcherResponseWriter implementation for std::string. 61 // URLFetcherResponseWriter implementation for std::string.
60 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { 62 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter {
61 public: 63 public:
62 URLFetcherStringWriter(); 64 URLFetcherStringWriter();
63 ~URLFetcherStringWriter() override; 65 ~URLFetcherStringWriter() override;
64 66
65 const std::string& data() const { return data_; } 67 const std::string& data() const { return data_; }
66 68
67 // URLFetcherResponseWriter overrides: 69 // URLFetcherResponseWriter overrides:
68 int Initialize(const CompletionCallback& callback) override; 70 int Initialize(const CompletionCallback& callback) override;
69 int Write(IOBuffer* buffer, 71 int Write(IOBuffer* buffer,
70 int num_bytes, 72 int num_bytes,
71 const CompletionCallback& callback) override; 73 const CompletionCallback& callback) override;
72 int Finish(const CompletionCallback& callback) override; 74 int Finish(int net_error, const CompletionCallback& callback) override;
73 URLFetcherStringWriter* AsStringWriter() override; 75 URLFetcherStringWriter* AsStringWriter() override;
74 76
75 private: 77 private:
76 std::string data_; 78 std::string data_;
77 79
78 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); 80 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter);
79 }; 81 };
80 82
81 // URLFetcherResponseWriter implementation for files. 83 // URLFetcherResponseWriter implementation for files.
82 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { 84 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter {
83 public: 85 public:
84 // |file_path| is used as the destination path. If |file_path| is empty, 86 // |file_path| is used as the destination path. If |file_path| is empty,
85 // Initialize() will create a temporary file. 87 // Initialize() will create a temporary file.
86 URLFetcherFileWriter( 88 URLFetcherFileWriter(
87 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 89 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
88 const base::FilePath& file_path); 90 const base::FilePath& file_path);
89 ~URLFetcherFileWriter() override; 91 ~URLFetcherFileWriter() override;
90 92
91 const base::FilePath& file_path() const { return file_path_; } 93 const base::FilePath& file_path() const { return file_path_; }
92 94
93 // URLFetcherResponseWriter overrides: 95 // URLFetcherResponseWriter overrides:
94 int Initialize(const CompletionCallback& callback) override; 96 int Initialize(const CompletionCallback& callback) override;
95 int Write(IOBuffer* buffer, 97 int Write(IOBuffer* buffer,
96 int num_bytes, 98 int num_bytes,
97 const CompletionCallback& callback) override; 99 const CompletionCallback& callback) override;
98 int Finish(const CompletionCallback& callback) override; 100 int Finish(int net_error, const CompletionCallback& callback) override;
99 URLFetcherFileWriter* AsFileWriter() override; 101 URLFetcherFileWriter* AsFileWriter() override;
100 102
101 // Drops ownership of the file at |file_path_|. 103 // Drops ownership of the file at |file_path_|.
102 // This class will not delete it or write to it again. 104 // This class will not delete it or write to it again.
103 void DisownFile(); 105 void DisownFile();
104 106
105 private: 107 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. 108 // Closes the file if it is open and then delete it.
110 void CloseAndDeleteFile(); 109 void CloseAndDeleteFile();
111 110
112 // Callback which gets the result of a temporary file creation. 111 // Callback which gets the result of a temporary file creation.
113 void DidCreateTempFile(const CompletionCallback& callback, 112 void DidCreateTempFile(base::FilePath* temp_file_path,
114 base::FilePath* temp_file_path,
115 bool success); 113 bool success);
116 114
117 // Callback which gets the result of FileStream::Open. 115 // Run |callback_| if it is non-null when FileStream::Open or
118 void DidOpenFile(const CompletionCallback& callback, 116 // FileStream::Write is completed.
119 int result); 117 void OnIOCompleted(int result);
120 118
121 // Callback which gets the result of closing a file. 119 // Callback which gets the result of closing a file.
122 void CloseComplete(const CompletionCallback& callback, int result); 120 void CloseComplete(int result);
123 121
124 // Task runner on which file operations should happen. 122 // Task runner on which file operations should happen.
125 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 123 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
126 124
127 // Destination file path. 125 // Destination file path.
128 // Initialize() creates a temporary file if this variable is empty. 126 // Initialize() creates a temporary file if this variable is empty.
129 base::FilePath file_path_; 127 base::FilePath file_path_;
130 128
131 // True when this instance is responsible to delete the file at |file_path_|. 129 // True when this instance is responsible to delete the file at |file_path_|.
132 bool owns_file_; 130 bool owns_file_;
133 131
134 std::unique_ptr<FileStream> file_stream_; 132 std::unique_ptr<FileStream> file_stream_;
135 133
134 CompletionCallback callback_;
135
136 // Callbacks are created for use with base::FileUtilProxy. 136 // Callbacks are created for use with base::FileUtilProxy.
137 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_; 137 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
138 138
139 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); 139 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter);
140 }; 140 };
141 141
142 } // namespace net 142 } // namespace net
143 143
144 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ 144 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698