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

Side by Side Diff: net/base/file_stream_posix.cc

Issue 10701050: net: Implement canceling of all async operations in FileStream. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // For 64-bit file access (off_t = off64_t, lseek64, etc). 5 // For 64-bit file access (off_t = off64_t, lseek64, etc).
6 #define _FILE_OFFSET_BITS 64 6 #define _FILE_OFFSET_BITS 64
7 7
8 #include "net/base/file_stream.h" 8 #include "net/base/file_stream.h"
9 9
10 #include <sys/types.h> 10 #include <sys/types.h>
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <fcntl.h> 12 #include <fcntl.h>
13 #include <unistd.h> 13 #include <unistd.h>
14 #include <errno.h> 14 #include <errno.h>
15 15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/bind_helpers.h" 18 #include "base/bind_helpers.h"
19 #include "base/callback.h" 19 #include "base/callback.h"
20 #include "base/eintr_wrapper.h" 20 #include "base/eintr_wrapper.h"
21 #include "base/file_path.h" 21 #include "base/file_path.h"
22 #include "base/logging.h" 22 #include "base/logging.h"
23 #include "base/memory/ref_counted.h"
23 #include "base/message_loop.h" 24 #include "base/message_loop.h"
24 #include "base/metrics/histogram.h" 25 #include "base/metrics/histogram.h"
25 #include "base/string_util.h" 26 #include "base/string_util.h"
26 #include "base/threading/thread_restrictions.h" 27 #include "base/threading/thread_restrictions.h"
27 #include "base/threading/worker_pool.h" 28 #include "base/threading/worker_pool.h"
28 #include "base/synchronization/waitable_event.h"
29 #include "net/base/file_stream_metrics.h" 29 #include "net/base/file_stream_metrics.h"
30 #include "net/base/file_stream_net_log_parameters.h" 30 #include "net/base/file_stream_net_log_parameters.h"
31 #include "net/base/io_buffer.h" 31 #include "net/base/io_buffer.h"
32 #include "net/base/net_errors.h" 32 #include "net/base/net_errors.h"
33 33
34 #if defined(OS_ANDROID) 34 #if defined(OS_ANDROID)
35 // Android's bionic libc only supports the LFS transitional API. 35 // Android's bionic libc only supports the LFS transitional API.
36 #define off_t off64_t 36 #define off_t off64_t
37 #define lseek lseek64 37 #define lseek lseek64
38 #define stat stat64 38 #define stat stat64
39 #define fstat fstat64 39 #define fstat fstat64
40 #endif 40 #endif
41 41
42 namespace net { 42 namespace net {
43 43
44 // We cast back and forth, so make sure it's the size we're expecting. 44 // We cast back and forth, so make sure it's the size we're expecting.
45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); 45 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit);
46 46
47 // Make sure our Whence mappings match the system headers. 47 // Make sure our Whence mappings match the system headers.
48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && 48 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET &&
49 FROM_CURRENT == SEEK_CUR && 49 FROM_CURRENT == SEEK_CUR &&
50 FROM_END == SEEK_END, whence_matches_system); 50 FROM_END == SEEK_END, whence_matches_system);
51 51
52 namespace { 52 class FileStreamPosix::AsyncContext {
53 53 public:
54 int RecordAndMapError(int error, 54 explicit AsyncContext(const BoundNetLog& bound_net_log);
55 FileErrorSource source, 55 AsyncContext(base::PlatformFile file, const BoundNetLog& bound_net_log);
56 bool record_uma, 56
57 const net::BoundNetLog& bound_net_log) { 57 // Destroys the context. It can be deleted in the method or deletion can be
58 net::Error net_error = MapSystemError(error); 58 // deferred to WorkerPool if some asynchronous operation is now in progress
59 59 // or if auto-closing is needed.
60 bound_net_log.AddEvent( 60 void Destroy();
61 net::NetLog::TYPE_FILE_STREAM_ERROR, 61
62 base::Bind(&NetLogFileStreamErrorCallback, 62 bool record_uma() { return record_uma_; }
63 source, error, net_error)); 63 void set_record_uma(bool value) { record_uma_ = value; }
64 64 base::PlatformFile file() { return file_; }
65 RecordFileError(error, source, record_uma); 65 bool auto_closed() { return auto_closed_; }
66 66 void set_auto_closed(bool value) { auto_closed_ = value; }
67 bool async_in_progress() { return async_in_progress_; }
68
69 int RecordAndMapError(int error, FileErrorSource source);
70
71 // Sync and async versions of all operations
72 void OpenAsync(const FilePath& path,
73 int open_flags,
74 const CompletionCallback& callback);
75 int OpenSync(const FilePath& path, int open_flags);
76
77 void CloseAsync(const CompletionCallback& callback);
78 void CloseSync();
79
80 void SeekAsync(Whence whence,
81 int64 offset,
82 const Int64CompletionCallback& callback);
83 int64 SeekSync(Whence whence, int64 offset);
84
85 void ReadAsync(IOBuffer* buf,
86 int buf_len,
87 const CompletionCallback& callback);
88 int ReadSync(char* buf, int buf_len);
89
90 void WriteAsync(IOBuffer* in_buf,
91 int buf_len,
92 const CompletionCallback& callback);
93 int WriteSync(const char* in_buf, int buf_len);
94
95 private:
96 // Map system error into network error code and log it with |bound_net_log_|.
97 // Method should be called with |net_log_lock_| locked.
98 int MapAndLogError(int error, FileErrorSource source);
99
100 // Opens a file with some network logging.
101 // The result code is written to |result|.
102 void OpenFileImpl(const FilePath& path, int open_flags, int* result);
103
104 // Closes a file with some network logging.
105 void CloseFileImpl();
106
107 // Adjusts the position from where the data is read.
108 void SeekFileImpl(Whence whence, int64 offset, int64* result);
109
110 // ReadFile() is a simple wrapper around read() that handles EINTR signals
111 // and calls RecordAndMapError() to map errno to net error codes.
112 void ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result);
113
114 // WriteFile() is a simple wrapper around write() that handles EINTR signals
115 // and calls MapSystemError() to map errno to net error codes. It tries
116 // to write to completion.
117 void WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result);
118
119 // Called when asynchronous Close(), Open(), Read(), Write() or Seek()
120 // is completed. |result| contains the result or a network error code.
121 template <typename R>
122 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R* result);
123
124 // Delete the context with asynchronous closing if necessary.
125 void DeleteAbandoned();
126
127 base::PlatformFile file_;
128 bool auto_closed_;
129 bool record_uma_;
130 bool async_in_progress_;
131 bool destroyed_;
132 base::Lock net_log_lock_;
133 BoundNetLog bound_net_log_;
134 };
135
136
137 FileStreamPosix::AsyncContext::AsyncContext(const BoundNetLog& bound_net_log)
138 : file_(base::kInvalidPlatformFileValue),
139 auto_closed_(true),
140 record_uma_(false),
141 async_in_progress_(false),
142 destroyed_(false),
143 bound_net_log_(bound_net_log) {
144 }
145
146 FileStreamPosix::AsyncContext::AsyncContext(base::PlatformFile file,
147 const BoundNetLog& bound_net_log)
148 : file_(file),
149 auto_closed_(false),
150 record_uma_(false),
151 async_in_progress_(false),
152 destroyed_(false),
153 bound_net_log_(bound_net_log) {
154 }
155
156 void FileStreamPosix::AsyncContext::Destroy() {
157 {
158 // By locking we don't allow any operation with |bound_net_log_| to be
159 // in progress while this method is executed. Attempt to do something
160 // with |bound_net_log_| will be done either before this method or after
161 // we switch |context_->destroyed_| which will prohibit any operation on
162 // |bound_net_log_|.
163 base::AutoLock locked(net_log_lock_);
164 destroyed_ = true;
165 }
166 if (!async_in_progress_)
167 DeleteAbandoned();
168 }
169
170 int FileStreamPosix::AsyncContext::RecordAndMapError(int error,
171 FileErrorSource source) {
172 int net_error;
173 {
174 base::AutoLock locked(net_log_lock_);
175 net_error = MapAndLogError(error, source);
176 }
177 RecordFileError(error, source, record_uma_);
67 return net_error; 178 return net_error;
68 } 179 }
69 180
70 // Opens a file with some network logging. 181 void FileStreamPosix::AsyncContext::OpenAsync(
71 // The opened file and the result code are written to |file| and |result|. 182 const FilePath& path,
72 void OpenFile(const FilePath& path, 183 int open_flags,
73 int open_flags, 184 const CompletionCallback& callback) {
74 bool record_uma, 185 DCHECK(!async_in_progress_);
75 base::PlatformFile* file, 186
76 int* result, 187 int* result = new int(OK);
77 const net::BoundNetLog& bound_net_log) {
78 std::string file_name = path.AsUTF8Unsafe();
79 bound_net_log.BeginEvent(
80 net::NetLog::TYPE_FILE_STREAM_OPEN,
81 NetLog::StringCallback("file_name", &file_name));
82
83 *result = OK;
84 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL);
85 if (*file == base::kInvalidPlatformFileValue) {
86 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN);
87 *result = RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma,
88 bound_net_log);
89 }
90 }
91
92 // Opens a file using OpenFile() and then signals the completion.
93 void OpenFileAndSignal(const FilePath& path,
94 int open_flags,
95 bool record_uma,
96 base::PlatformFile* file,
97 int* result,
98 base::WaitableEvent* on_io_complete,
99 const net::BoundNetLog& bound_net_log) {
100 OpenFile(path, open_flags, record_uma, file, result, bound_net_log);
101 on_io_complete->Signal();
102 }
103
104 // Closes a file with some network logging.
105 void CloseFile(base::PlatformFile file,
106 const net::BoundNetLog& bound_net_log) {
107 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE);
108 if (file == base::kInvalidPlatformFileValue)
109 return;
110
111 if (!base::ClosePlatformFile(file))
112 NOTREACHED();
113 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN);
114 }
115
116 // Closes a file with CloseFile() and signals the completion.
117 void CloseFileAndSignal(base::PlatformFile* file,
118 base::WaitableEvent* on_io_complete,
119 const net::BoundNetLog& bound_net_log) {
120 CloseFile(*file, bound_net_log);
121 *file = base::kInvalidPlatformFileValue;
122 on_io_complete->Signal();
123 }
124
125 // Adjusts the position from where the data is read.
126 void SeekFile(base::PlatformFile file,
127 Whence whence,
128 int64 offset,
129 int64* result,
130 bool record_uma,
131 const net::BoundNetLog& bound_net_log) {
132 off_t res = lseek(file, static_cast<off_t>(offset),
133 static_cast<int>(whence));
134 if (res == static_cast<off_t>(-1)) {
135 *result = RecordAndMapError(errno,
136 FILE_ERROR_SOURCE_SEEK,
137 record_uma,
138 bound_net_log);
139 return;
140 }
141 *result = res;
142 }
143
144 // Seeks a file by calling SeekSync() and signals the completion.
145 void SeekFileAndSignal(base::PlatformFile file,
146 Whence whence,
147 int64 offset,
148 int64* result,
149 bool record_uma,
150 base::WaitableEvent* on_io_complete,
151 const net::BoundNetLog& bound_net_log) {
152 SeekFile(file, whence, offset, result, record_uma, bound_net_log);
153 on_io_complete->Signal();
154 }
155
156 // ReadFile() is a simple wrapper around read() that handles EINTR signals and
157 // calls MapSystemError() to map errno to net error codes.
158 void ReadFile(base::PlatformFile file,
159 char* buf,
160 int buf_len,
161 bool record_uma,
162 int* result,
163 const net::BoundNetLog& bound_net_log) {
164 base::ThreadRestrictions::AssertIOAllowed();
165 // read(..., 0) returns 0 to indicate end-of-file.
166
167 // Loop in the case of getting interrupted by a signal.
168 ssize_t res = HANDLE_EINTR(read(file, buf, static_cast<size_t>(buf_len)));
169 if (res == -1) {
170 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_READ,
171 record_uma, bound_net_log);
172 }
173 *result = res;
174 }
175
176 // Reads a file using ReadFile() and signals the completion.
177 void ReadFileAndSignal(base::PlatformFile file,
178 scoped_refptr<IOBuffer> buf,
179 int buf_len,
180 bool record_uma,
181 int* result,
182 base::WaitableEvent* on_io_complete,
183 const net::BoundNetLog& bound_net_log) {
184 ReadFile(file, buf->data(), buf_len, record_uma, result, bound_net_log);
185 on_io_complete->Signal();
186 }
187
188 // WriteFile() is a simple wrapper around write() that handles EINTR signals and
189 // calls MapSystemError() to map errno to net error codes. It tries to write to
190 // completion.
191 void WriteFile(base::PlatformFile file,
192 const char* buf,
193 int buf_len,
194 bool record_uma,
195 int* result,
196 const net::BoundNetLog& bound_net_log) {
197 base::ThreadRestrictions::AssertIOAllowed();
198
199 ssize_t res = HANDLE_EINTR(write(file, buf, buf_len));
200 if (res == -1) {
201 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE, record_uma,
202 bound_net_log);
203 }
204 *result = res;
205 }
206
207 // Writes a file using WriteFile() and signals the completion.
208 void WriteFileAndSignal(base::PlatformFile file,
209 scoped_refptr<IOBuffer> buf,
210 int buf_len,
211 bool record_uma,
212 int* result,
213 base::WaitableEvent* on_io_complete,
214 const net::BoundNetLog& bound_net_log) {
215 WriteFile(file, buf->data(), buf_len, record_uma, result, bound_net_log);
216 on_io_complete->Signal();
217 }
218
219 // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and
220 // calls MapSystemError() to map errno to net error codes. It tries to flush to
221 // completion.
222 int FlushFile(base::PlatformFile file,
223 bool record_uma,
224 const net::BoundNetLog& bound_net_log) {
225 base::ThreadRestrictions::AssertIOAllowed();
226 ssize_t res = HANDLE_EINTR(fsync(file));
227 if (res == -1) {
228 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma,
229 bound_net_log);
230 }
231 return res;
232 }
233
234 // Called when Read(), Write() or Seek() is completed.
235 // |result| contains the result or a network error code.
236 template <typename R>
237 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream,
238 const base::Callback<void(R)>& callback,
239 R* result) {
240 if (!stream.get())
241 return;
242
243 // Reset this before Run() as Run() may issue a new async operation.
244 stream->ResetOnIOComplete();
245 callback.Run(*result);
246 }
247
248 } // namespace
249
250 // FileStreamPosix ------------------------------------------------------------
251
252 FileStreamPosix::FileStreamPosix(net::NetLog* net_log)
253 : file_(base::kInvalidPlatformFileValue),
254 open_flags_(0),
255 auto_closed_(true),
256 record_uma_(false),
257 bound_net_log_(net::BoundNetLog::Make(net_log,
258 net::NetLog::SOURCE_FILESTREAM)),
259 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
260 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
261 }
262
263 FileStreamPosix::FileStreamPosix(
264 base::PlatformFile file, int flags, net::NetLog* net_log)
265 : file_(file),
266 open_flags_(flags),
267 auto_closed_(false),
268 record_uma_(false),
269 bound_net_log_(net::BoundNetLog::Make(net_log,
270 net::NetLog::SOURCE_FILESTREAM)),
271 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
272 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
273 }
274
275 FileStreamPosix::~FileStreamPosix() {
276 if (open_flags_ & base::PLATFORM_FILE_ASYNC) {
277 // Block until the last open/close/read/write operation is complete.
278 // TODO(satorux): Ideally we should not block. crbug.com/115067
279 WaitForIOCompletion();
280 }
281
282 if (auto_closed_) {
283 if (open_flags_ & base::PLATFORM_FILE_ASYNC) {
284 // Close the file in the background.
285 if (IsOpen()) {
286 const bool posted = base::WorkerPool::PostTask(
287 FROM_HERE,
288 base::Bind(&CloseFile, file_, bound_net_log_),
289 true /* task_is_slow */);
290 DCHECK(posted);
291 }
292 } else {
293 CloseSync();
294 }
295 }
296
297 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
298 }
299
300 void FileStreamPosix::Close(const CompletionCallback& callback) {
301 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC);
302
303 DCHECK(!weak_ptr_factory_.HasWeakPtrs());
304 DCHECK(!on_io_complete_.get());
305 on_io_complete_.reset(new base::WaitableEvent(
306 false /* manual_reset */, false /* initially_signaled */));
307
308 // Passing &file_ to a thread pool looks unsafe but it's safe here as the
309 // destructor ensures that the close operation is complete with
310 // WaitForIOCompletion(). See also the destructor.
311 const bool posted = base::WorkerPool::PostTaskAndReply( 188 const bool posted = base::WorkerPool::PostTaskAndReply(
312 FROM_HERE, 189 FROM_HERE,
313 base::Bind(&CloseFileAndSignal, &file_, on_io_complete_.get(), 190 base::Bind(&AsyncContext::OpenFileImpl, base::Unretained(this),
314 bound_net_log_), 191 path, open_flags, result),
315 base::Bind(&FileStreamPosix::OnClosed, 192 base::Bind(&AsyncContext::OnAsyncCompleted<int>,
316 weak_ptr_factory_.GetWeakPtr(), 193 base::Unretained(this),
317 callback), 194 callback, base::Owned(result)),
318 true /* task_is_slow */); 195 true /* task_is_slow */);
319
320 DCHECK(posted); 196 DCHECK(posted);
197
198 async_in_progress_ = true;
199 }
200
201 int FileStreamPosix::AsyncContext::OpenSync(const FilePath& path,
202 int open_flags) {
203 int result = OK;
204 OpenFileImpl(path, open_flags, &result);
205 return result;
206 }
207
208 void FileStreamPosix::AsyncContext::CloseAsync(
209 const CompletionCallback& callback) {
210 DCHECK(!async_in_progress_);
211
212 // Value OK will never be changed in AsyncContext::CloseFile() and is needed
213 // here just to use the same AsyncContext::OnAsyncCompleted().
214 int* result = new int(OK);
215 const bool posted = base::WorkerPool::PostTaskAndReply(
216 FROM_HERE,
217 base::Bind(&AsyncContext::CloseFileImpl, base::Unretained(this)),
218 base::Bind(&AsyncContext::OnAsyncCompleted<int>,
219 base::Unretained(this),
220 callback, base::Owned(result)),
221 true /* task_is_slow */);
222 DCHECK(posted);
223
224 async_in_progress_ = true;
225 }
226
227 void FileStreamPosix::AsyncContext::CloseSync() {
228 DCHECK(!async_in_progress_);
229 CloseFileImpl();
230 }
231
232 void FileStreamPosix::AsyncContext::SeekAsync(
233 Whence whence,
234 int64 offset,
235 const Int64CompletionCallback& callback) {
236 DCHECK(!async_in_progress_);
237
238 int64* result = new int64(-1);
239 const bool posted = base::WorkerPool::PostTaskAndReply(
240 FROM_HERE,
241 base::Bind(&AsyncContext::SeekFileImpl, base::Unretained(this),
242 whence, offset, result),
243 base::Bind(&AsyncContext::OnAsyncCompleted<int64>,
244 base::Unretained(this),
245 callback, base::Owned(result)),
246 true /* task is slow */);
247 DCHECK(posted);
248
249 async_in_progress_ = true;
250 }
251
252 int64 FileStreamPosix::AsyncContext::SeekSync(Whence whence, int64 offset) {
253 off_t result = -1;
254 SeekFileImpl(whence, offset, &result);
255 return result;
256 }
257
258 void FileStreamPosix::AsyncContext::ReadAsync(
259 IOBuffer* in_buf,
260 int buf_len,
261 const CompletionCallback& callback) {
262 DCHECK(!async_in_progress_);
263
264 int* result = new int(OK);
265 scoped_refptr<IOBuffer> buf = in_buf;
266 const bool posted = base::WorkerPool::PostTaskAndReply(
267 FROM_HERE,
268 base::Bind(&AsyncContext::ReadFileImpl, base::Unretained(this),
269 buf, buf_len, result),
270 base::Bind(&AsyncContext::OnAsyncCompleted<int>,
271 base::Unretained(this),
272 callback, base::Owned(result)),
273 true /* task is slow */);
274 DCHECK(posted);
275
276 async_in_progress_ = true;
277 }
278
279 int FileStreamPosix::AsyncContext::ReadSync(char* in_buf, int buf_len) {
280 int result = OK;
281 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf);
282 ReadFileImpl(buf, buf_len, &result);
283 return result;
284 }
285
286 void FileStreamPosix::AsyncContext::WriteAsync(
287 IOBuffer* in_buf,
288 int buf_len,
289 const CompletionCallback& callback) {
290 DCHECK(!async_in_progress_);
291
292 int* result = new int(OK);
293 scoped_refptr<IOBuffer> buf = in_buf;
294 const bool posted = base::WorkerPool::PostTaskAndReply(
295 FROM_HERE,
296 base::Bind(&AsyncContext::WriteFileImpl, base::Unretained(this),
297 buf, buf_len, result),
298 base::Bind(&AsyncContext::OnAsyncCompleted<int>,
299 base::Unretained(this),
300 callback, base::Owned(result)),
301 true /* task is slow */);
302 DCHECK(posted);
303
304 async_in_progress_ = true;
305 }
306
307 int FileStreamPosix::AsyncContext::WriteSync(const char* in_buf, int buf_len) {
308 int result = OK;
309 scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf);
310 WriteFileImpl(buf, buf_len, &result);
311 return result;
312 }
313
314 int FileStreamPosix::AsyncContext::MapAndLogError(int error,
315 FileErrorSource source) {
316 net_log_lock_.AssertAcquired();
317 // The following check is against incorrect use or bug. File descriptor
318 // shouldn't ever be closed outside of FileStream while it still tries to do
319 // something with it.
320 DCHECK(error != EBADF);
321 net::Error net_error = MapSystemError(error);
322
323 if (!destroyed_) {
324 bound_net_log_.AddEvent(
325 net::NetLog::TYPE_FILE_STREAM_ERROR,
326 base::Bind(&NetLogFileStreamErrorCallback,
327 source, error, net_error));
328 }
329
330 return net_error;
331 }
332
333 void FileStreamPosix::AsyncContext::OpenFileImpl(const FilePath& path,
334 int open_flags,
335 int* result) {
336 std::string file_name = path.AsUTF8Unsafe();
337 {
338 base::AutoLock locked(net_log_lock_);
339 // Bail out quickly if operation was already canceled
340 if (destroyed_)
341 return;
342
343 bound_net_log_.BeginEvent(
344 net::NetLog::TYPE_FILE_STREAM_OPEN,
345 NetLog::StringCallback("file_name", &file_name));
346 }
347
348 *result = OK;
349 file_ = base::CreatePlatformFile(path, open_flags, NULL, NULL);
350 if (file_ == base::kInvalidPlatformFileValue) {
351 int error = errno;
352 {
353 base::AutoLock locked(net_log_lock_);
354 if (!destroyed_)
355 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN);
356 *result = MapAndLogError(error, FILE_ERROR_SOURCE_OPEN);
357 }
358 RecordFileError(error, FILE_ERROR_SOURCE_OPEN, record_uma_);
359 }
360 }
361
362 void FileStreamPosix::AsyncContext::CloseFileImpl() {
363 {
364 base::AutoLock locked(net_log_lock_);
365 if (!destroyed_)
366 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE);
367 }
368
369 if (file_ == base::kInvalidPlatformFileValue)
370 return;
371 if (!base::ClosePlatformFile(file_))
372 NOTREACHED();
373 file_ = base::kInvalidPlatformFileValue;
374
375 {
376 base::AutoLock locked(net_log_lock_);
377 if (!destroyed_)
378 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN);
379 }
380 }
381
382 void FileStreamPosix::AsyncContext::SeekFileImpl(Whence whence,
383 int64 offset,
384 int64* result) {
385 base::ThreadRestrictions::AssertIOAllowed();
386
387 // If context has been already destroyed nobody waits for operation results.
388 if (destroyed_)
389 return;
390
391 off_t res = lseek(file_, static_cast<off_t>(offset),
392 static_cast<int>(whence));
393 if (res == static_cast<off_t>(-1))
394 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_SEEK);
395 *result = res;
396 }
397
398 void FileStreamPosix::AsyncContext::ReadFileImpl(scoped_refptr<IOBuffer> buf,
399 int buf_len,
400 int* result) {
401 base::ThreadRestrictions::AssertIOAllowed();
402
403 // If context has been already destroyed nobody waits for operation results.
404 if (destroyed_)
405 return;
406
407 // Loop in the case of getting interrupted by a signal.
408 ssize_t res = HANDLE_EINTR(read(file_, buf->data(),
409 static_cast<size_t>(buf_len)));
410 if (res == -1)
411 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_READ);
412 *result = res;
413 }
414
415 void FileStreamPosix::AsyncContext::WriteFileImpl(scoped_refptr<IOBuffer> buf,
416 int buf_len,
417 int* result) {
418 base::ThreadRestrictions::AssertIOAllowed();
419
420 // If context has been already destroyed nobody waits for operation results.
421 if (destroyed_)
422 return;
423
424 ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len));
425 if (res == -1)
426 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_WRITE);
427 *result = res;
428 }
429
430 template <typename R>
431 void FileStreamPosix::AsyncContext::OnAsyncCompleted(
432 const base::Callback<void(R)>& callback,
433 R* result) {
434 if (destroyed_) {
435 DeleteAbandoned();
436 } else {
437 // Reset this before Run() as Run() may issue a new async operation.
438 async_in_progress_ = false;
439 callback.Run(*result);
440 }
441 }
442
443 void FileStreamPosix::AsyncContext::DeleteAbandoned() {
444 if (auto_closed_ && file_ != base::kInvalidPlatformFileValue) {
445 const bool posted = base::WorkerPool::PostTask(
446 FROM_HERE,
447 // Context should be deleted after closing, thus Owned().
448 base::Bind(&AsyncContext::CloseFileImpl, base::Owned(this)),
449 true /* task_is_slow */);
450 DCHECK(posted);
451 } else {
452 delete this;
453 }
454 }
455
456
457 // FileStreamPosix ------------------------------------------------------------
458
459 FileStreamPosix::FileStreamPosix(net::NetLog* net_log)
460 : context_(NULL),
461 open_flags_(0),
462 bound_net_log_(net::BoundNetLog::Make(net_log,
463 net::NetLog::SOURCE_FILESTREAM)) {
464 context_ = new AsyncContext(bound_net_log_);
465
466 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
467 }
468
469 FileStreamPosix::FileStreamPosix(base::PlatformFile file,
470 int flags,
471 net::NetLog* net_log)
472 : context_(NULL),
473 open_flags_(flags),
474 bound_net_log_(net::BoundNetLog::Make(net_log,
475 net::NetLog::SOURCE_FILESTREAM)) {
476 context_ = new AsyncContext(file, bound_net_log_);
477
478 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
479 }
480
481 FileStreamPosix::~FileStreamPosix() {
482 if (IsOpen() && context_->auto_closed() && !is_async())
483 CloseSync();
484 context_->Destroy();
485
486 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE);
487 }
488
489 void FileStreamPosix::Close(const CompletionCallback& callback) {
490 DCHECK(is_async());
491 context_->CloseAsync(callback);
321 } 492 }
322 493
323 void FileStreamPosix::CloseSync() { 494 void FileStreamPosix::CloseSync() {
495 // CloseSync() should be called on the correct thread even if it eventually
496 // ends up inside CloseAndCancelAsync().
497 base::ThreadRestrictions::AssertIOAllowed();
498
324 // TODO(satorux): Replace the following async stuff with a 499 // TODO(satorux): Replace the following async stuff with a
325 // DCHECK(open_flags & ASYNC) once once all async clients are migrated to 500 // DCHECK(open_flags & ASYNC) once all async clients are migrated to
326 // use Close(). crbug.com/114783 501 // use Close(). crbug.com/114783
327 502 if (context_->async_in_progress())
328 // Abort any existing asynchronous operations. 503 CloseAndCancelAsync();
329 weak_ptr_factory_.InvalidateWeakPtrs(); 504 else
330 // Block until the last open/read/write operation is complete. 505 context_->CloseSync();
331 // TODO(satorux): Ideally we should not block. crbug.com/115067 506 }
332 WaitForIOCompletion(); 507
333 508 void FileStreamPosix::CloseAndCancelAsync() {
334 CloseFile(file_, bound_net_log_); 509 if (!IsOpen())
335 file_ = base::kInvalidPlatformFileValue; 510 return;
336 } 511
337 512 DCHECK(is_async());
338 int FileStreamPosix::Open(const FilePath& path, int open_flags, 513
339 const CompletionCallback& callback) { 514 AsyncContext* old_ctx = context_;
515 context_ = new AsyncContext(bound_net_log_);
516 context_->set_record_uma(old_ctx->record_uma());
517 old_ctx->set_auto_closed(true);
518 old_ctx->Destroy();
519 }
520
521 int FileStreamPosix::Open(const FilePath& path,
522 int open_flags,
523 const CompletionCallback& callback) {
340 if (IsOpen()) { 524 if (IsOpen()) {
341 DLOG(FATAL) << "File is already open!"; 525 DLOG(FATAL) << "File is already open!";
342 return ERR_UNEXPECTED; 526 return ERR_UNEXPECTED;
343 } 527 }
344 528
345 open_flags_ = open_flags; 529 open_flags_ = open_flags;
346 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); 530 DCHECK(is_async());
347 531 context_->OpenAsync(path, open_flags, callback);
348 DCHECK(!weak_ptr_factory_.HasWeakPtrs());
349 DCHECK(!on_io_complete_.get());
350 on_io_complete_.reset(new base::WaitableEvent(
351 false /* manual_reset */, false /* initially_signaled */));
352
353 // Passing &file_ to a thread pool looks unsafe but it's safe here as the
354 // destructor ensures that the open operation is complete with
355 // WaitForIOCompletion(). See also the destructor.
356 int* result = new int(OK);
357 const bool posted = base::WorkerPool::PostTaskAndReply(
358 FROM_HERE,
359 base::Bind(&OpenFileAndSignal,
360 path, open_flags, record_uma_, &file_, result,
361 on_io_complete_.get(), bound_net_log_),
362 base::Bind(&OnIOComplete<int>, weak_ptr_factory_.GetWeakPtr(),
363 callback, base::Owned(result)),
364 true /* task_is_slow */);
365 DCHECK(posted);
366 return ERR_IO_PENDING; 532 return ERR_IO_PENDING;
367 } 533 }
368 534
369 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { 535 int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) {
370 if (IsOpen()) { 536 if (IsOpen()) {
371 DLOG(FATAL) << "File is already open!"; 537 DLOG(FATAL) << "File is already open!";
372 return ERR_UNEXPECTED; 538 return ERR_UNEXPECTED;
373 } 539 }
374 540
375 open_flags_ = open_flags; 541 open_flags_ = open_flags;
376 // TODO(satorux): Put a DCHECK once once all async clients are migrated 542 // TODO(satorux): Put a DCHECK once all async clients are migrated
377 // to use Open(). crbug.com/114783 543 // to use Open(). crbug.com/114783
378 // 544 //
379 // DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); 545 // DCHECK(!is_async());
380 546 return context_->OpenSync(path, open_flags_);
381 int result = OK;
382 OpenFile(path, open_flags_, record_uma_, &file_, &result, bound_net_log_);
383 return result;
384 } 547 }
385 548
386 bool FileStreamPosix::IsOpen() const { 549 bool FileStreamPosix::IsOpen() const {
387 return file_ != base::kInvalidPlatformFileValue; 550 return context_->file() != base::kInvalidPlatformFileValue;
388 } 551 }
389 552
390 int FileStreamPosix::Seek(Whence whence, int64 offset, 553 int FileStreamPosix::Seek(Whence whence,
554 int64 offset,
391 const Int64CompletionCallback& callback) { 555 const Int64CompletionCallback& callback) {
392 if (!IsOpen()) 556 if (!IsOpen())
393 return ERR_UNEXPECTED; 557 return ERR_UNEXPECTED;
394 558
395 // Make sure we're async and we have no other in-flight async operations. 559 // Make sure we're async.
396 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); 560 DCHECK(is_async());
397 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); 561 context_->SeekAsync(whence, offset, callback);
398 DCHECK(!on_io_complete_.get());
399
400 on_io_complete_.reset(new base::WaitableEvent(
401 false /* manual_reset */, false /* initially_signaled */));
402
403 int64* result = new int64(-1);
404 const bool posted = base::WorkerPool::PostTaskAndReply(
405 FROM_HERE,
406 base::Bind(&SeekFileAndSignal, file_, whence, offset, result,
407 record_uma_, on_io_complete_.get(), bound_net_log_),
408 base::Bind(&OnIOComplete<int64>,
409 weak_ptr_factory_.GetWeakPtr(),
410 callback, base::Owned(result)),
411 true /* task is slow */);
412 DCHECK(posted);
413 return ERR_IO_PENDING; 562 return ERR_IO_PENDING;
414 } 563 }
415 564
416 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) { 565 int64 FileStreamPosix::SeekSync(Whence whence, int64 offset) {
417 base::ThreadRestrictions::AssertIOAllowed();
418
419 if (!IsOpen()) 566 if (!IsOpen())
420 return ERR_UNEXPECTED; 567 return ERR_UNEXPECTED;
421 568
422 // If we're in async, make sure we don't have a request in flight. 569 // If we're in async, make sure we don't have a request in flight.
423 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC) || 570 DCHECK(!is_async() || !context_->async_in_progress());
424 !on_io_complete_.get()); 571 return context_->SeekSync(whence, offset);
425
426 off_t result = -1;
427 SeekFile(file_, whence, offset, &result, record_uma_, bound_net_log_);
428 return result;
429 } 572 }
430 573
431 int64 FileStreamPosix::Available() { 574 int64 FileStreamPosix::Available() {
432 base::ThreadRestrictions::AssertIOAllowed(); 575 base::ThreadRestrictions::AssertIOAllowed();
433 576
434 if (!IsOpen()) 577 if (!IsOpen())
435 return ERR_UNEXPECTED; 578 return ERR_UNEXPECTED;
436 579
437 int64 cur_pos = SeekSync(FROM_CURRENT, 0); 580 int64 cur_pos = SeekSync(FROM_CURRENT, 0);
438 if (cur_pos < 0) 581 if (cur_pos < 0)
439 return cur_pos; 582 return cur_pos;
440 583
441 struct stat info; 584 struct stat info;
442 if (fstat(file_, &info) != 0) { 585 if (fstat(context_->file(), &info) != 0)
443 return RecordAndMapError(errno, 586 return context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_GET_SIZE);
444 FILE_ERROR_SOURCE_GET_SIZE,
445 record_uma_,
446 bound_net_log_);
447 }
448 587
449 int64 size = static_cast<int64>(info.st_size); 588 int64 size = static_cast<int64>(info.st_size);
450 DCHECK_GT(size, cur_pos); 589 DCHECK_GT(size, cur_pos);
451 590
452 return size - cur_pos; 591 return size - cur_pos;
453 } 592 }
454 593
455 int FileStreamPosix::Read( 594 int FileStreamPosix::Read(IOBuffer* buf,
456 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { 595 int buf_len,
596 const CompletionCallback& callback) {
457 if (!IsOpen()) 597 if (!IsOpen())
458 return ERR_UNEXPECTED; 598 return ERR_UNEXPECTED;
459 599
460 // read(..., 0) will return 0, which indicates end-of-file. 600 // read(..., 0) will return 0, which indicates end-of-file.
461 DCHECK_GT(buf_len, 0); 601 DCHECK_GT(buf_len, 0);
462 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); 602 DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
463 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); 603 DCHECK(is_async());
464 604
465 // Make sure we don't have a request in flight. 605 context_->ReadAsync(buf, buf_len, callback);
466 DCHECK(!weak_ptr_factory_.HasWeakPtrs());
467 DCHECK(!on_io_complete_.get());
468
469 on_io_complete_.reset(new base::WaitableEvent(
470 false /* manual_reset */, false /* initially_signaled */));
471
472 int* result = new int(OK);
473 scoped_refptr<IOBuffer> buf = in_buf;
474 const bool posted = base::WorkerPool::PostTaskAndReply(
475 FROM_HERE,
476 base::Bind(&ReadFileAndSignal, file_, buf, buf_len,
477 record_uma_, result, on_io_complete_.get(), bound_net_log_),
478 base::Bind(&OnIOComplete<int>,
479 weak_ptr_factory_.GetWeakPtr(),
480 callback, base::Owned(result)),
481 true /* task is slow */);
482 DCHECK(posted);
483 return ERR_IO_PENDING; 606 return ERR_IO_PENDING;
484 } 607 }
485 608
486 int FileStreamPosix::ReadSync(char* buf, int buf_len) { 609 int FileStreamPosix::ReadSync(char* buf, int buf_len) {
487 if (!IsOpen()) 610 if (!IsOpen())
488 return ERR_UNEXPECTED; 611 return ERR_UNEXPECTED;
489 612
490 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); 613 DCHECK(!is_async());
491 // read(..., 0) will return 0, which indicates end-of-file. 614 // read(..., 0) will return 0, which indicates end-of-file.
492 DCHECK_GT(buf_len, 0); 615 DCHECK_GT(buf_len, 0);
493 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); 616 DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
494 617
495 int result = OK; 618 return context_->ReadSync(buf, buf_len);
496 ReadFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_);
497 return result;
498 } 619 }
499 620
500 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { 621 int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) {
501 int to_read = buf_len; 622 int to_read = buf_len;
502 int bytes_total = 0; 623 int bytes_total = 0;
503 624
504 do { 625 do {
505 int bytes_read = ReadSync(buf, to_read); 626 int bytes_read = ReadSync(buf, to_read);
506 if (bytes_read <= 0) { 627 if (bytes_read <= 0) {
507 if (bytes_total == 0) 628 if (bytes_total == 0)
508 return bytes_read; 629 return bytes_read;
509 630
510 return bytes_total; 631 return bytes_total;
511 } 632 }
512 633
513 bytes_total += bytes_read; 634 bytes_total += bytes_read;
514 buf += bytes_read; 635 buf += bytes_read;
515 to_read -= bytes_read; 636 to_read -= bytes_read;
516 } while (bytes_total < buf_len); 637 } while (bytes_total < buf_len);
517 638
518 return bytes_total; 639 return bytes_total;
519 } 640 }
520 641
521 int FileStreamPosix::Write( 642 int FileStreamPosix::Write(IOBuffer* buf,
522 IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { 643 int buf_len,
644 const CompletionCallback& callback) {
523 if (!IsOpen()) 645 if (!IsOpen())
524 return ERR_UNEXPECTED; 646 return ERR_UNEXPECTED;
525 647
526 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); 648 DCHECK(is_async());
527 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); 649 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
528 // write(..., 0) will return 0, which indicates end-of-file. 650 // write(..., 0) will return 0, which indicates end-of-file.
529 DCHECK_GT(buf_len, 0); 651 DCHECK_GT(buf_len, 0);
530 652
531 // Make sure we don't have a request in flight. 653 context_->WriteAsync(buf, buf_len, callback);
532 DCHECK(!weak_ptr_factory_.HasWeakPtrs());
533 DCHECK(!on_io_complete_.get());
534 on_io_complete_.reset(new base::WaitableEvent(
535 false /* manual_reset */, false /* initially_signaled */));
536
537 int* result = new int(OK);
538 scoped_refptr<IOBuffer> buf = in_buf;
539 const bool posted = base::WorkerPool::PostTaskAndReply(
540 FROM_HERE,
541 base::Bind(&WriteFileAndSignal, file_, buf, buf_len,
542 record_uma_, result, on_io_complete_.get(), bound_net_log_),
543 base::Bind(&OnIOComplete<int>,
544 weak_ptr_factory_.GetWeakPtr(),
545 callback, base::Owned(result)),
546 true /* task is slow */);
547 DCHECK(posted);
548 return ERR_IO_PENDING; 654 return ERR_IO_PENDING;
549 } 655 }
550 656
551 int FileStreamPosix::WriteSync( 657 int FileStreamPosix::WriteSync(const char* buf, int buf_len) {
552 const char* buf, int buf_len) {
553 if (!IsOpen()) 658 if (!IsOpen())
554 return ERR_UNEXPECTED; 659 return ERR_UNEXPECTED;
555 660
556 DCHECK(!(open_flags_ & base::PLATFORM_FILE_ASYNC)); 661 DCHECK(!is_async());
557 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); 662 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
558 // write(..., 0) will return 0, which indicates end-of-file. 663 // write(..., 0) will return 0, which indicates end-of-file.
559 DCHECK_GT(buf_len, 0); 664 DCHECK_GT(buf_len, 0);
560 665
561 int result = OK; 666 return context_->WriteSync(buf, buf_len);
562 WriteFile(file_, buf, buf_len, record_uma_, &result, bound_net_log_);
563 return result;
564 } 667 }
565 668
566 int64 FileStreamPosix::Truncate(int64 bytes) { 669 int64 FileStreamPosix::Truncate(int64 bytes) {
567 base::ThreadRestrictions::AssertIOAllowed(); 670 base::ThreadRestrictions::AssertIOAllowed();
568 671
569 if (!IsOpen()) 672 if (!IsOpen())
570 return ERR_UNEXPECTED; 673 return ERR_UNEXPECTED;
571 674
572 // We'd better be open for writing. 675 // We'd better be open for writing.
573 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); 676 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
574 677
575 // Seek to the position to truncate from. 678 // Seek to the position to truncate from.
576 int64 seek_position = SeekSync(FROM_BEGIN, bytes); 679 int64 seek_position = SeekSync(FROM_BEGIN, bytes);
577 if (seek_position != bytes) 680 if (seek_position != bytes)
578 return ERR_UNEXPECTED; 681 return ERR_UNEXPECTED;
579 682
580 // And truncate the file. 683 // And truncate the file.
581 int result = ftruncate(file_, bytes); 684 int result = ftruncate(context_->file(), bytes);
582 if (result == 0) 685 if (result == 0)
583 return seek_position; 686 return seek_position;
584 687
585 return RecordAndMapError(errno, 688 return context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_SET_EOF);
586 FILE_ERROR_SOURCE_SET_EOF,
587 record_uma_,
588 bound_net_log_);
589 } 689 }
590 690
591 int FileStreamPosix::Flush() { 691 int FileStreamPosix::Flush() {
692 base::ThreadRestrictions::AssertIOAllowed();
693
592 if (!IsOpen()) 694 if (!IsOpen())
593 return ERR_UNEXPECTED; 695 return ERR_UNEXPECTED;
594 696
595 return FlushFile(file_, record_uma_, bound_net_log_); 697 ssize_t res = HANDLE_EINTR(fsync(context_->file()));
698 if (res == -1)
699 res = context_->RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH);
700 return res;
596 } 701 }
597 702
598 void FileStreamPosix::EnableErrorStatistics() { 703 void FileStreamPosix::EnableErrorStatistics() {
599 record_uma_ = true; 704 context_->set_record_uma(true);
600 } 705 }
601 706
602 void FileStreamPosix::SetBoundNetLogSource( 707 void FileStreamPosix::SetBoundNetLogSource(
603 const net::BoundNetLog& owner_bound_net_log) { 708 const net::BoundNetLog& owner_bound_net_log) {
604 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && 709 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) &&
605 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { 710 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) {
606 // Both |BoundNetLog|s are invalid. 711 // Both |BoundNetLog|s are invalid.
607 return; 712 return;
608 } 713 }
609 714
610 // Should never connect to itself. 715 // Should never connect to itself.
611 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); 716 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id);
612 717
613 bound_net_log_.AddEvent( 718 bound_net_log_.AddEvent(
614 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, 719 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER,
615 owner_bound_net_log.source().ToEventParametersCallback()); 720 owner_bound_net_log.source().ToEventParametersCallback());
616 721
617 owner_bound_net_log.AddEvent( 722 owner_bound_net_log.AddEvent(
618 net::NetLog::TYPE_FILE_STREAM_SOURCE, 723 net::NetLog::TYPE_FILE_STREAM_SOURCE,
619 bound_net_log_.source().ToEventParametersCallback()); 724 bound_net_log_.source().ToEventParametersCallback());
620 } 725 }
621 726
622 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { 727 base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() {
623 return file_; 728 return context_->file();
624 }
625
626 void FileStreamPosix::ResetOnIOComplete() {
627 on_io_complete_.reset();
628 weak_ptr_factory_.InvalidateWeakPtrs();
629 }
630
631 void FileStreamPosix::OnClosed(const CompletionCallback& callback) {
632 file_ = base::kInvalidPlatformFileValue;
633
634 // Reset this before Run() as Run() may issue a new async operation.
635 ResetOnIOComplete();
636 callback.Run(OK);
637 }
638
639 void FileStreamPosix::WaitForIOCompletion() {
640 // http://crbug.com/115067
641 base::ThreadRestrictions::ScopedAllowWait allow_wait;
642 if (on_io_complete_.get()) {
643 on_io_complete_->Wait();
644 on_io_complete_.reset();
645 }
646 } 729 }
647 730
648 } // namespace net 731 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698