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

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

Issue 23500006: URLFetcher won't call delegate until file is closed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reorder two methods. Created 7 years, 3 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 #include "net/url_request/url_fetcher_response_writer.h" 5 #include "net/url_request/url_fetcher_response_writer.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/task_runner.h" 9 #include "base/task_runner.h"
10 #include "base/task_runner_util.h" 10 #include "base/task_runner_util.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 weak_factory_.GetWeakPtr(), 95 weak_factory_.GetWeakPtr(),
96 callback)); 96 callback));
97 if (result < 0 && result != ERR_IO_PENDING) { 97 if (result < 0 && result != ERR_IO_PENDING) {
98 error_code_ = result; 98 error_code_ = result;
99 CloseAndDeleteFile(); 99 CloseAndDeleteFile();
100 } 100 }
101 return result; 101 return result;
102 } 102 }
103 103
104 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) { 104 int URLFetcherFileWriter::Finish(const CompletionCallback& callback) {
105 int result = file_stream_->Close(base::Bind(
106 &URLFetcherFileWriter::CloseComplete, base::Unretained(this), callback));
mmenke 2013/09/10 18:32:52 The base::Unretained changes the safe usage model
waffles 2013/09/10 21:41:34 Done.
107 if (result != ERR_IO_PENDING)
108 file_stream_.reset();
109 return result;
110 }
111
112 void URLFetcherFileWriter::CloseComplete(const CompletionCallback& callback,
113 int result) {
114 // Either we closed the file or we can't; either way destroy the file_stream.
105 file_stream_.reset(); 115 file_stream_.reset();
106 return OK; 116 callback.Run(result);
107 } 117 }
108 118
109 void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback, 119 void URLFetcherFileWriter::DidWrite(const CompletionCallback& callback,
110 int result) { 120 int result) {
111 if (result < 0) { 121 if (result < 0) {
112 error_code_ = result; 122 error_code_ = result;
113 CloseAndDeleteFile(); 123 CloseAndDeleteFile();
114 } 124 }
115 callback.Run(result); 125 callback.Run(result);
116 } 126 }
117 127
118 void URLFetcherFileWriter::DisownFile() { 128 void URLFetcherFileWriter::DisownFile() {
119 // Disowning is done by the delegate's OnURLFetchComplete method. 129 // Disowning is done by the delegate's OnURLFetchComplete method.
120 // The file should be closed by the time that method is called. 130 // The file should be closed by the time that method is called.
121 DCHECK(!file_stream_); 131 DCHECK(!file_stream_);
122 132
123 owns_file_ = false; 133 owns_file_ = false;
124 } 134 }
125 135
126 void URLFetcherFileWriter::CloseAndDeleteFile() { 136 void URLFetcherFileWriter::CloseAndDeleteFile() {
127 if (!owns_file_) 137 if (!owns_file_)
128 return; 138 return;
129 139
130 file_stream_.reset(); 140 file_stream_.reset();
131 DisownFile(); 141 DisownFile();
132 file_task_runner_->PostTask(FROM_HERE, 142 file_task_runner_->PostTask(FROM_HERE,
133 base::Bind(base::IgnoreResult(&base::DeleteFile), 143 base::Bind(base::IgnoreResult(&base::DeleteFile),
mmenke 2013/09/10 18:32:52 Kinda related to this CL: Think it's worth a comm
waffles 2013/09/10 21:41:34 Hm, except that file_task_runner_ is only guarante
mmenke 2013/09/10 22:25:16 Looks like you're right. It actually used to use
waffles 2013/09/10 22:33:22 I'd rather not fix it in this CL; I definitely don
mmenke 2013/09/10 22:38:19 That's fine, I'll do it. I want to investigate wh
134 file_path_, 144 file_path_,
135 false /* recursive */)); 145 false /* recursive */));
136 } 146 }
137 147
138 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback, 148 void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback,
139 base::FilePath* temp_file_path, 149 base::FilePath* temp_file_path,
140 bool success) { 150 bool success) {
141 if (!success) { 151 if (!success) {
142 error_code_ = ERR_FILE_NOT_FOUND; 152 error_code_ = ERR_FILE_NOT_FOUND;
143 callback.Run(error_code_); 153 callback.Run(error_code_);
(...skipping 18 matching lines...) Expand all
162 total_bytes_written_ = 0; 172 total_bytes_written_ = 0;
163 owns_file_ = true; 173 owns_file_ = true;
164 } else { 174 } else {
165 error_code_ = result; 175 error_code_ = result;
166 CloseAndDeleteFile(); 176 CloseAndDeleteFile();
167 } 177 }
168 callback.Run(result); 178 callback.Run(result);
169 } 179 }
170 180
171 } // namespace net 181 } // namespace net
OLDNEW
« net/base/file_stream_unittest.cc ('K') | « net/url_request/url_fetcher_response_writer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698