OLD | NEW |
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 #include "net/base/file_stream.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" |
11 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
13 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
15 #include "base/threading/worker_pool.h" | 16 #include "base/threading/worker_pool.h" |
16 #include "net/base/file_stream_metrics.h" | 17 #include "net/base/file_stream_metrics.h" |
17 #include "net/base/file_stream_net_log_parameters.h" | 18 #include "net/base/file_stream_net_log_parameters.h" |
18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
20 | 21 |
(...skipping 12 matching lines...) Expand all Loading... |
33 } | 34 } |
34 | 35 |
35 void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { | 36 void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { |
36 LARGE_INTEGER offset; | 37 LARGE_INTEGER offset; |
37 offset.LowPart = overlapped->Offset; | 38 offset.LowPart = overlapped->Offset; |
38 offset.HighPart = overlapped->OffsetHigh; | 39 offset.HighPart = overlapped->OffsetHigh; |
39 offset.QuadPart += static_cast<LONGLONG>(count); | 40 offset.QuadPart += static_cast<LONGLONG>(count); |
40 SetOffset(overlapped, offset); | 41 SetOffset(overlapped, offset); |
41 } | 42 } |
42 | 43 |
43 int RecordAndMapError(int error, | |
44 FileErrorSource source, | |
45 bool record_uma, | |
46 const net::BoundNetLog& bound_net_log) { | |
47 net::Error net_error = MapSystemError(error); | |
48 | |
49 bound_net_log.AddEvent( | |
50 net::NetLog::TYPE_FILE_STREAM_ERROR, | |
51 base::Bind(&NetLogFileStreamErrorCallback, | |
52 source, error, net_error)); | |
53 | |
54 RecordFileError(error, source, record_uma); | |
55 | |
56 return net_error; | |
57 } | |
58 | |
59 // Opens a file with some network logging. | |
60 // The opened file and the result code are written to |file| and |result|. | |
61 void OpenFile(const FilePath& path, | |
62 int open_flags, | |
63 bool record_uma, | |
64 base::PlatformFile* file, | |
65 int* result, | |
66 const net::BoundNetLog& bound_net_log) { | |
67 std::string file_name = path.AsUTF8Unsafe(); | |
68 bound_net_log.BeginEvent( | |
69 net::NetLog::TYPE_FILE_STREAM_OPEN, | |
70 NetLog::StringCallback("file_name", &file_name)); | |
71 | |
72 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); | |
73 if (*file == base::kInvalidPlatformFileValue) { | |
74 DWORD error = GetLastError(); | |
75 LOG(WARNING) << "Failed to open file: " << error; | |
76 *result = RecordAndMapError(error, | |
77 FILE_ERROR_SOURCE_OPEN, | |
78 record_uma, | |
79 bound_net_log); | |
80 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
81 return; | |
82 } | |
83 } | |
84 | |
85 // Closes a file with some network logging. | |
86 void CloseFile(base::PlatformFile file, | |
87 const net::BoundNetLog& bound_net_log) { | |
88 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); | |
89 if (file == base::kInvalidPlatformFileValue) | |
90 return; | |
91 | |
92 CancelIo(file); | |
93 | |
94 if (!base::ClosePlatformFile(file)) | |
95 NOTREACHED(); | |
96 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
97 } | |
98 | |
99 // Closes a file with CloseFile() and signals the completion. | |
100 void CloseFileAndSignal(base::PlatformFile* file, | |
101 base::WaitableEvent* on_io_complete, | |
102 const net::BoundNetLog& bound_net_log) { | |
103 CloseFile(*file, bound_net_log); | |
104 *file = base::kInvalidPlatformFileValue; | |
105 on_io_complete->Signal(); | |
106 } | |
107 | |
108 // Invokes a given closure and signals the completion. | |
109 void InvokeAndSignal(const base::Closure& closure, | |
110 base::WaitableEvent* on_io_complete) { | |
111 closure.Run(); | |
112 on_io_complete->Signal(); | |
113 } | |
114 | |
115 } // namespace | 44 } // namespace |
116 | 45 |
117 // FileStreamWin::AsyncContext ---------------------------------------------- | 46 // FileStreamWin::AsyncContext ---------------------------------------------- |
118 | 47 |
119 class FileStreamWin::AsyncContext : public MessageLoopForIO::IOHandler { | 48 class FileStreamWin::AsyncContext : public MessageLoopForIO::IOHandler { |
120 public: | 49 public: |
121 explicit AsyncContext(const net::BoundNetLog& bound_net_log) | 50 explicit AsyncContext(const BoundNetLog& bound_net_log); |
122 : context_(), is_closing_(false), | 51 AsyncContext(base::PlatformFile file, |
123 record_uma_(false), bound_net_log_(bound_net_log), | 52 const BoundNetLog& bound_net_log, |
124 error_source_(FILE_ERROR_SOURCE_COUNT) { | 53 int open_flags); |
125 context_.handler = this; | 54 |
126 } | 55 // Destroys the context. It can be deleted in the method or deletion can be |
127 ~AsyncContext(); | 56 // deferred to WorkerPool if some asynchronous operation is now in progress |
| 57 // or if auto-closing is needed. |
| 58 void Destroy(); |
| 59 |
| 60 bool record_uma() { return record_uma_; } |
| 61 void set_record_uma(bool value) { record_uma_ = value; } |
| 62 base::PlatformFile file() { return file_; } |
| 63 bool auto_closed() { return auto_closed_; } |
| 64 void set_auto_closed(bool value) { auto_closed_ = value; } |
| 65 bool async_in_progress() { return async_in_progress_; } |
| 66 |
| 67 int RecordAndMapError(int error, FileErrorSource source); |
| 68 |
| 69 // Sync and async versions of all operations |
| 70 void OpenAsync(const FilePath& path, |
| 71 int open_flags, |
| 72 const CompletionCallback& callback); |
| 73 int OpenSync(const FilePath& path, int open_flags); |
| 74 |
| 75 void CloseAsync(const CompletionCallback& callback); |
| 76 void CloseSync(); |
| 77 |
| 78 void SeekAsync(Whence whence, |
| 79 int64 offset, |
| 80 const Int64CompletionCallback& callback); |
| 81 int64 SeekSync(Whence whence, int64 offset); |
| 82 |
| 83 int ReadAsync(IOBuffer* buf, |
| 84 int buf_len, |
| 85 const CompletionCallback& callback); |
| 86 int ReadSync(char* buf, int buf_len); |
| 87 |
| 88 int WriteAsync(IOBuffer* buf, |
| 89 int buf_len, |
| 90 const CompletionCallback& callback); |
| 91 int WriteSync(const char* buf, int buf_len); |
| 92 |
| 93 private: |
| 94 // Map system error into network error code and log it with |bound_net_log_|. |
| 95 // Method should be called with |net_log_lock_| locked. |
| 96 int MapAndLogError(int error, FileErrorSource source); |
| 97 |
| 98 // Opens a file with some network logging. |
| 99 // The result code is written to |result|. |
| 100 void OpenFileImpl(const FilePath& path, int open_flags, int* result); |
| 101 |
| 102 // Called when asynchronous Open() is completed. |
| 103 void OnOpenCompleted(const CompletionCallback& callback, int* result); |
| 104 |
| 105 // Called after any Open() is completed on thread where AsyncContext |
| 106 // is created. |
| 107 void RegisterInMessageLoop(); |
| 108 |
| 109 // Closes a file with some network logging. |
| 110 void CloseFileImpl(); |
| 111 |
| 112 // A helper method for Seek. |
| 113 void SeekFileImpl(Whence whence, int64 offset, int64* result); |
128 | 114 |
129 void IOCompletionIsPending(const CompletionCallback& callback, | 115 void IOCompletionIsPending(const CompletionCallback& callback, |
130 IOBuffer* buf); | 116 IOBuffer* buf); |
131 | 117 |
132 OVERLAPPED* overlapped() { return &context_.overlapped; } | 118 // Implementation of MessageLoopForIO::IOHandler |
133 const CompletionCallback& callback() const { return callback_; } | |
134 | |
135 void set_error_source(FileErrorSource source) { error_source_ = source; } | |
136 | |
137 void EnableErrorStatistics() { | |
138 record_uma_ = true; | |
139 } | |
140 | |
141 private: | |
142 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 119 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
143 DWORD bytes_read, DWORD error) OVERRIDE; | 120 DWORD bytes_read, |
144 | 121 DWORD error) OVERRIDE; |
145 MessageLoopForIO::IOContext context_; | 122 |
| 123 // Called when asynchronous Open(), Close() or Seek() |
| 124 // is completed. |result| contains the result or a network error code. |
| 125 template <typename R> |
| 126 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R* result); |
| 127 |
| 128 // Delete the context with asynchronous closing if necessary. |
| 129 void DeleteAbandoned(); |
| 130 |
| 131 MessageLoopForIO::IOContext io_context_; |
146 CompletionCallback callback_; | 132 CompletionCallback callback_; |
147 scoped_refptr<IOBuffer> in_flight_buf_; | 133 scoped_refptr<IOBuffer> in_flight_buf_; |
148 bool is_closing_; | 134 base::PlatformFile file_; |
| 135 bool auto_closed_; |
149 bool record_uma_; | 136 bool record_uma_; |
| 137 bool async_in_progress_; |
| 138 bool destroyed_; |
| 139 base::Lock net_log_lock_; |
150 const net::BoundNetLog bound_net_log_; | 140 const net::BoundNetLog bound_net_log_; |
151 FileErrorSource error_source_; | 141 FileErrorSource error_source_; |
152 }; | 142 }; |
153 | 143 |
154 FileStreamWin::AsyncContext::~AsyncContext() { | 144 FileStreamWin::AsyncContext::AsyncContext(const BoundNetLog& bound_net_log) |
155 is_closing_ = true; | 145 : io_context_(), |
156 bool waited = false; | 146 file_(base::kInvalidPlatformFileValue), |
157 base::TimeTicks start = base::TimeTicks::Now(); | 147 auto_closed_(true), |
158 while (!callback_.is_null()) { | 148 record_uma_(false), |
159 waited = true; | 149 async_in_progress_(false), |
160 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); | 150 destroyed_(false), |
161 } | 151 bound_net_log_(bound_net_log), |
162 if (waited) { | 152 error_source_(FILE_ERROR_SOURCE_COUNT) { |
163 // We want to see if we block the message loop for too long. | 153 io_context_.handler = this; |
164 UMA_HISTOGRAM_TIMES("AsyncIO.FileStreamClose", | 154 } |
165 base::TimeTicks::Now() - start); | 155 |
166 } | 156 FileStreamWin::AsyncContext::AsyncContext(base::PlatformFile file, |
| 157 const BoundNetLog& bound_net_log, |
| 158 int open_flags) |
| 159 : io_context_(), |
| 160 file_(file), |
| 161 auto_closed_(false), |
| 162 record_uma_(false), |
| 163 async_in_progress_(false), |
| 164 destroyed_(false), |
| 165 bound_net_log_(bound_net_log), |
| 166 error_source_(FILE_ERROR_SOURCE_COUNT) { |
| 167 io_context_.handler = this; |
| 168 if (open_flags & base::PLATFORM_FILE_ASYNC) |
| 169 RegisterInMessageLoop(); |
| 170 } |
| 171 |
| 172 void FileStreamWin::AsyncContext::Destroy() { |
| 173 { |
| 174 // By locking we don't allow any operation with |bound_net_log_| to be |
| 175 // in progress while this method is executed. Attempt to do something |
| 176 // with |bound_net_log_| will be done either before this method or after |
| 177 // we switch |context_->destroyed_| which will prohibit any operation on |
| 178 // |bound_net_log_|. |
| 179 base::AutoLock locked(net_log_lock_); |
| 180 destroyed_ = true; |
| 181 } |
| 182 CancelIo(file_); |
| 183 if (!async_in_progress_) |
| 184 DeleteAbandoned(); |
| 185 } |
| 186 |
| 187 int FileStreamWin::AsyncContext::RecordAndMapError(int error, |
| 188 FileErrorSource source) { |
| 189 int net_error; |
| 190 { |
| 191 base::AutoLock locked(net_log_lock_); |
| 192 net_error = MapAndLogError(error, source); |
| 193 } |
| 194 RecordFileError(error, source, record_uma_); |
| 195 return net_error; |
| 196 } |
| 197 |
| 198 void FileStreamWin::AsyncContext::OpenAsync( |
| 199 const FilePath& path, |
| 200 int open_flags, |
| 201 const CompletionCallback& callback) { |
| 202 DCHECK(!async_in_progress_); |
| 203 |
| 204 int* result = new int(OK); |
| 205 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 206 FROM_HERE, |
| 207 base::Bind(&AsyncContext::OpenFileImpl, base::Unretained(this), |
| 208 path, open_flags, result), |
| 209 base::Bind(&AsyncContext::OnOpenCompleted, |
| 210 base::Unretained(this), |
| 211 callback, base::Owned(result)), |
| 212 true /* task_is_slow */); |
| 213 DCHECK(posted); |
| 214 |
| 215 async_in_progress_ = true; |
| 216 } |
| 217 |
| 218 int FileStreamWin::AsyncContext::OpenSync(const FilePath& path, |
| 219 int open_flags) { |
| 220 int result = OK; |
| 221 OpenFileImpl(path, open_flags, &result); |
| 222 // TODO(satorux): Remove this once all async clients are migrated to use |
| 223 // Open(). crbug.com/114783 |
| 224 if (open_flags & base::PLATFORM_FILE_ASYNC) |
| 225 RegisterInMessageLoop(); |
| 226 return result; |
| 227 } |
| 228 |
| 229 void FileStreamWin::AsyncContext::CloseAsync( |
| 230 const CompletionCallback& callback) { |
| 231 DCHECK(!async_in_progress_); |
| 232 |
| 233 // Value OK will never be changed in AsyncContext::CloseFile() and is needed |
| 234 // here just to use the same AsyncContext::OnAsyncCompleted(). |
| 235 int* result = new int(OK); |
| 236 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 237 FROM_HERE, |
| 238 base::Bind(&AsyncContext::CloseFileImpl, base::Unretained(this)), |
| 239 base::Bind(&AsyncContext::OnAsyncCompleted<int>, |
| 240 base::Unretained(this), |
| 241 callback, base::Owned(result)), |
| 242 true /* task_is_slow */); |
| 243 DCHECK(posted); |
| 244 |
| 245 async_in_progress_ = true; |
| 246 } |
| 247 |
| 248 void FileStreamWin::AsyncContext::CloseSync() { |
| 249 DCHECK(!async_in_progress_); |
| 250 CloseFileImpl(); |
| 251 } |
| 252 |
| 253 void FileStreamWin::AsyncContext::SeekAsync( |
| 254 Whence whence, |
| 255 int64 offset, |
| 256 const Int64CompletionCallback& callback) { |
| 257 DCHECK(!async_in_progress_); |
| 258 |
| 259 int64* result = new int64(-1); |
| 260 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 261 FROM_HERE, |
| 262 base::Bind(&AsyncContext::SeekFileImpl, base::Unretained(this), |
| 263 whence, offset, result), |
| 264 base::Bind(&AsyncContext::OnAsyncCompleted<int64>, |
| 265 base::Unretained(this), |
| 266 callback, base::Owned(result)), |
| 267 true /* task is slow */); |
| 268 DCHECK(posted); |
| 269 |
| 270 async_in_progress_ = true; |
| 271 } |
| 272 |
| 273 int64 FileStreamWin::AsyncContext::SeekSync(Whence whence, int64 offset) { |
| 274 int64 result = -1; |
| 275 SeekFileImpl(whence, offset, &result); |
| 276 return result; |
| 277 } |
| 278 |
| 279 int FileStreamWin::AsyncContext::ReadAsync( |
| 280 IOBuffer* buf, |
| 281 int buf_len, |
| 282 const CompletionCallback& callback) { |
| 283 DCHECK(!async_in_progress_); |
| 284 error_source_ = FILE_ERROR_SOURCE_READ; |
| 285 |
| 286 int rv = 0; |
| 287 |
| 288 DWORD bytes_read; |
| 289 if (!ReadFile(file_, buf->data(), buf_len, |
| 290 &bytes_read, &io_context_.overlapped)) { |
| 291 DWORD error = GetLastError(); |
| 292 if (error == ERROR_IO_PENDING) { |
| 293 IOCompletionIsPending(callback, buf); |
| 294 rv = ERR_IO_PENDING; |
| 295 } else if (error == ERROR_HANDLE_EOF) { |
| 296 rv = 0; // Report EOF by returning 0 bytes read. |
| 297 } else { |
| 298 LOG(WARNING) << "ReadFile failed: " << error; |
| 299 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ); |
| 300 } |
| 301 } else { |
| 302 IOCompletionIsPending(callback, buf); |
| 303 rv = ERR_IO_PENDING; |
| 304 } |
| 305 return rv; |
| 306 } |
| 307 |
| 308 int FileStreamWin::AsyncContext::ReadSync(char* buf, int buf_len) { |
| 309 base::ThreadRestrictions::AssertIOAllowed(); |
| 310 |
| 311 int rv = 0; |
| 312 |
| 313 DWORD bytes_read; |
| 314 if (!ReadFile(file_, buf, buf_len, &bytes_read, NULL)) { |
| 315 DWORD error = GetLastError(); |
| 316 if (error == ERROR_HANDLE_EOF) { |
| 317 rv = 0; // Report EOF by returning 0 bytes read. |
| 318 } else { |
| 319 LOG(WARNING) << "ReadFile failed: " << error; |
| 320 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ); |
| 321 } |
| 322 } else { |
| 323 rv = static_cast<int>(bytes_read); |
| 324 } |
| 325 return rv; |
| 326 } |
| 327 |
| 328 int FileStreamWin::AsyncContext::WriteAsync( |
| 329 IOBuffer* buf, |
| 330 int buf_len, |
| 331 const CompletionCallback& callback) { |
| 332 error_source_ = FILE_ERROR_SOURCE_WRITE; |
| 333 |
| 334 int rv = 0; |
| 335 DWORD bytes_written = 0; |
| 336 if (!WriteFile(file_, buf->data(), buf_len, |
| 337 &bytes_written, &io_context_.overlapped)) { |
| 338 DWORD error = GetLastError(); |
| 339 if (error == ERROR_IO_PENDING) { |
| 340 IOCompletionIsPending(callback, buf); |
| 341 rv = ERR_IO_PENDING; |
| 342 } else { |
| 343 LOG(WARNING) << "WriteFile failed: " << error; |
| 344 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE); |
| 345 } |
| 346 } else { |
| 347 IOCompletionIsPending(callback, buf); |
| 348 rv = ERR_IO_PENDING; |
| 349 } |
| 350 return rv; |
| 351 } |
| 352 |
| 353 int FileStreamWin::AsyncContext::WriteSync(const char* buf, int buf_len) { |
| 354 base::ThreadRestrictions::AssertIOAllowed(); |
| 355 |
| 356 int rv = 0; |
| 357 DWORD bytes_written = 0; |
| 358 if (!WriteFile(file_, buf, buf_len, &bytes_written, NULL)) { |
| 359 DWORD error = GetLastError(); |
| 360 LOG(WARNING) << "WriteFile failed: " << error; |
| 361 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE); |
| 362 } else { |
| 363 rv = static_cast<int>(bytes_written); |
| 364 } |
| 365 return rv; |
| 366 } |
| 367 |
| 368 int FileStreamWin::AsyncContext::MapAndLogError(int error, |
| 369 FileErrorSource source) { |
| 370 net_log_lock_.AssertAcquired(); |
| 371 // The following check is against incorrect use or bug. File descriptor |
| 372 // shouldn't ever be closed outside of FileStream while it still tries to do |
| 373 // something with it. |
| 374 DCHECK(error != ERROR_INVALID_HANDLE); |
| 375 net::Error net_error = MapSystemError(error); |
| 376 |
| 377 if (!destroyed_) { |
| 378 bound_net_log_.AddEvent( |
| 379 net::NetLog::TYPE_FILE_STREAM_ERROR, |
| 380 base::Bind(&NetLogFileStreamErrorCallback, |
| 381 source, error, net_error)); |
| 382 } |
| 383 |
| 384 return net_error; |
| 385 } |
| 386 |
| 387 void FileStreamWin::AsyncContext::OpenFileImpl(const FilePath& path, |
| 388 int open_flags, |
| 389 int* result) { |
| 390 std::string file_name = path.AsUTF8Unsafe(); |
| 391 { |
| 392 base::AutoLock locked(net_log_lock_); |
| 393 // Bail out quickly if operation was already canceled |
| 394 if (destroyed_) |
| 395 return; |
| 396 |
| 397 bound_net_log_.BeginEvent( |
| 398 net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 399 NetLog::StringCallback("file_name", &file_name)); |
| 400 } |
| 401 |
| 402 file_ = base::CreatePlatformFile(path, open_flags, NULL, NULL); |
| 403 if (file_ == base::kInvalidPlatformFileValue) { |
| 404 DWORD error = GetLastError(); |
| 405 LOG(WARNING) << "Failed to open file: " << error; |
| 406 { |
| 407 base::AutoLock locked(net_log_lock_); |
| 408 if (!destroyed_) |
| 409 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 410 *result = MapAndLogError(error, FILE_ERROR_SOURCE_OPEN); |
| 411 } |
| 412 RecordFileError(error, FILE_ERROR_SOURCE_OPEN, record_uma_); |
| 413 } |
| 414 } |
| 415 |
| 416 void FileStreamWin::AsyncContext::OnOpenCompleted( |
| 417 const CompletionCallback& callback, |
| 418 int* result) { |
| 419 if (!destroyed_) |
| 420 RegisterInMessageLoop(); |
| 421 OnAsyncCompleted(callback, result); |
| 422 } |
| 423 |
| 424 void FileStreamWin::AsyncContext::RegisterInMessageLoop() { |
| 425 if (file_ != base::kInvalidPlatformFileValue) |
| 426 MessageLoopForIO::current()->RegisterIOHandler(file_, this); |
| 427 } |
| 428 |
| 429 void FileStreamWin::AsyncContext::CloseFileImpl() { |
| 430 { |
| 431 base::AutoLock locked(net_log_lock_); |
| 432 if (!destroyed_) |
| 433 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); |
| 434 } |
| 435 |
| 436 if (file_ == base::kInvalidPlatformFileValue) |
| 437 return; |
| 438 if (!base::ClosePlatformFile(file_)) |
| 439 NOTREACHED(); |
| 440 file_ = base::kInvalidPlatformFileValue; |
| 441 |
| 442 { |
| 443 base::AutoLock locked(net_log_lock_); |
| 444 if (!destroyed_) |
| 445 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 446 } |
| 447 } |
| 448 |
| 449 void FileStreamWin::AsyncContext::SeekFileImpl(Whence whence, |
| 450 int64 offset, |
| 451 int64* result) { |
| 452 base::ThreadRestrictions::AssertIOAllowed(); |
| 453 |
| 454 // If context has been already destroyed nobody waits for operation results. |
| 455 if (destroyed_) |
| 456 return; |
| 457 |
| 458 LARGE_INTEGER distance, res; |
| 459 distance.QuadPart = offset; |
| 460 DWORD move_method = static_cast<DWORD>(whence); |
| 461 if (!SetFilePointerEx(file_, distance, &res, move_method)) { |
| 462 DWORD error = GetLastError(); |
| 463 LOG(WARNING) << "SetFilePointerEx failed: " << error; |
| 464 *result = RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK); |
| 465 return; |
| 466 } |
| 467 SetOffset(&io_context_.overlapped, res); |
| 468 *result = res.QuadPart; |
167 } | 469 } |
168 | 470 |
169 void FileStreamWin::AsyncContext::IOCompletionIsPending( | 471 void FileStreamWin::AsyncContext::IOCompletionIsPending( |
170 const CompletionCallback& callback, | 472 const CompletionCallback& callback, |
171 IOBuffer* buf) { | 473 IOBuffer* buf) { |
172 DCHECK(callback_.is_null()); | 474 DCHECK(callback_.is_null()); |
173 callback_ = callback; | 475 callback_ = callback; |
174 in_flight_buf_ = buf; // Hold until the async operation ends. | 476 in_flight_buf_ = buf; // Hold until the async operation ends. |
| 477 async_in_progress_ = true; |
175 } | 478 } |
176 | 479 |
177 void FileStreamWin::AsyncContext::OnIOCompleted( | 480 void FileStreamWin::AsyncContext::OnIOCompleted( |
178 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { | 481 MessageLoopForIO::IOContext* context, |
179 DCHECK_EQ(&context_, context); | 482 DWORD bytes_read, |
| 483 DWORD error) { |
| 484 DCHECK_EQ(&io_context_, context); |
180 DCHECK(!callback_.is_null()); | 485 DCHECK(!callback_.is_null()); |
181 | 486 |
182 if (is_closing_) { | 487 if (destroyed_) { |
183 callback_.Reset(); | 488 callback_.Reset(); |
184 in_flight_buf_ = NULL; | 489 in_flight_buf_ = NULL; |
| 490 DeleteAbandoned(); |
185 return; | 491 return; |
186 } | 492 } |
187 | 493 |
188 int result = static_cast<int>(bytes_read); | 494 int result = static_cast<int>(bytes_read); |
189 if (error && error != ERROR_HANDLE_EOF) { | 495 if (error && error != ERROR_HANDLE_EOF) |
190 result = RecordAndMapError(error, error_source_, record_uma_, | 496 result = RecordAndMapError(error, error_source_); |
191 bound_net_log_); | |
192 } | |
193 | 497 |
194 if (bytes_read) | 498 if (bytes_read) |
195 IncrementOffset(&context->overlapped, bytes_read); | 499 IncrementOffset(&io_context_.overlapped, bytes_read); |
196 | 500 |
| 501 // Reset this before Run() as Run() may issue a new async operation. |
| 502 async_in_progress_ = false; |
197 CompletionCallback temp_callback = callback_; | 503 CompletionCallback temp_callback = callback_; |
198 callback_.Reset(); | 504 callback_.Reset(); |
199 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; | 505 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; |
200 in_flight_buf_ = NULL; | 506 in_flight_buf_ = NULL; |
201 temp_callback.Run(result); | 507 temp_callback.Run(result); |
202 } | 508 } |
203 | 509 |
| 510 template <typename R> |
| 511 void FileStreamWin::AsyncContext::OnAsyncCompleted( |
| 512 const base::Callback<void(R)>& callback, |
| 513 R* result) { |
| 514 if (destroyed_) { |
| 515 DeleteAbandoned(); |
| 516 } else { |
| 517 // Reset this before Run() as Run() may issue a new async operation. |
| 518 async_in_progress_ = false; |
| 519 callback.Run(*result); |
| 520 } |
| 521 } |
| 522 |
| 523 void FileStreamWin::AsyncContext::DeleteAbandoned() { |
| 524 if (auto_closed_ && file_ != base::kInvalidPlatformFileValue) { |
| 525 const bool posted = base::WorkerPool::PostTask( |
| 526 FROM_HERE, |
| 527 // Context should be deleted after closing, thus Owned(). |
| 528 base::Bind(&AsyncContext::CloseFileImpl, base::Owned(this)), |
| 529 true /* task_is_slow */); |
| 530 DCHECK(posted); |
| 531 } else { |
| 532 delete this; |
| 533 } |
| 534 } |
| 535 |
204 // FileStream ------------------------------------------------------------ | 536 // FileStream ------------------------------------------------------------ |
205 | 537 |
206 FileStreamWin::FileStreamWin(net::NetLog* net_log) | 538 FileStreamWin::FileStreamWin(net::NetLog* net_log) |
207 : file_(base::kInvalidPlatformFileValue), | 539 : context_(NULL), |
208 open_flags_(0), | 540 open_flags_(0), |
209 auto_closed_(true), | |
210 record_uma_(false), | |
211 bound_net_log_(net::BoundNetLog::Make(net_log, | 541 bound_net_log_(net::BoundNetLog::Make(net_log, |
212 net::NetLog::SOURCE_FILESTREAM)), | 542 net::NetLog::SOURCE_FILESTREAM)) { |
213 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 543 context_ = new AsyncContext(bound_net_log_); |
| 544 |
214 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | 545 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
215 } | 546 } |
216 | 547 |
217 FileStreamWin::FileStreamWin( | 548 FileStreamWin::FileStreamWin(base::PlatformFile file, |
218 base::PlatformFile file, int flags, net::NetLog* net_log) | 549 int flags, |
219 : file_(file), | 550 net::NetLog* net_log) |
| 551 : context_(NULL), |
220 open_flags_(flags), | 552 open_flags_(flags), |
221 auto_closed_(false), | |
222 record_uma_(false), | |
223 bound_net_log_(net::BoundNetLog::Make(net_log, | 553 bound_net_log_(net::BoundNetLog::Make(net_log, |
224 net::NetLog::SOURCE_FILESTREAM)), | 554 net::NetLog::SOURCE_FILESTREAM)) { |
225 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 555 context_ = new AsyncContext(file, bound_net_log_, open_flags_); |
| 556 |
226 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | 557 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
227 | |
228 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | |
229 // make sure we will perform asynchronous File IO to it. | |
230 if (flags & base::PLATFORM_FILE_ASYNC) { | |
231 async_context_.reset(new AsyncContext(bound_net_log_)); | |
232 MessageLoopForIO::current()->RegisterIOHandler(file_, | |
233 async_context_.get()); | |
234 } | |
235 } | 558 } |
236 | 559 |
237 FileStreamWin::~FileStreamWin() { | 560 FileStreamWin::~FileStreamWin() { |
238 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 561 if (context_->auto_closed() && !is_async()) |
239 // Block until the in-flight open/close operation is complete. | 562 CloseSync(); |
240 // TODO(satorux): Ideally we should not block. crbug.com/115067 | 563 context_->Destroy(); |
241 WaitForIOCompletion(); | |
242 | |
243 // Block until the last read/write operation is complete. | |
244 async_context_.reset(); | |
245 } | |
246 | |
247 if (auto_closed_) { | |
248 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
249 // Close the file in the background. | |
250 if (IsOpen()) { | |
251 const bool posted = base::WorkerPool::PostTask( | |
252 FROM_HERE, | |
253 base::Bind(&CloseFile, file_, bound_net_log_), | |
254 true /* task_is_slow */); | |
255 DCHECK(posted); | |
256 } | |
257 } else { | |
258 CloseSync(); | |
259 } | |
260 } | |
261 | 564 |
262 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); | 565 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
263 } | 566 } |
264 | 567 |
265 void FileStreamWin::Close(const CompletionCallback& callback) { | 568 void FileStreamWin::Close(const CompletionCallback& callback) { |
266 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 569 DCHECK(is_async()); |
267 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 570 context_->CloseAsync(callback); |
268 DCHECK(!on_io_complete_.get()); | |
269 on_io_complete_.reset(new base::WaitableEvent( | |
270 false /* manual_reset */, false /* initially_signaled */)); | |
271 | |
272 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
273 // destructor ensures that the close operation is complete with | |
274 // WaitForIOCompletion(). See also the destructor. | |
275 const bool posted = base::WorkerPool::PostTaskAndReply( | |
276 FROM_HERE, | |
277 base::Bind(&CloseFileAndSignal, &file_, on_io_complete_.get(), | |
278 bound_net_log_), | |
279 base::Bind(&FileStreamWin::OnClosed, | |
280 weak_ptr_factory_.GetWeakPtr(), | |
281 callback), | |
282 true /* task_is_slow */); | |
283 DCHECK(posted); | |
284 } | 571 } |
285 | 572 |
286 void FileStreamWin::CloseSync() { | 573 void FileStreamWin::CloseSync() { |
287 // The logic here is similar to CloseFile() but async_context_.reset() is | 574 // CloseSync() should be called on the correct thread even if it eventually |
288 // caled in this function. | 575 // ends up inside CloseAndCancelAsync(). |
| 576 base::ThreadRestrictions::AssertIOAllowed(); |
289 | 577 |
290 // Block until the in-flight open operation is complete. | 578 // TODO(satorux): Replace the following async stuff with a |
291 // TODO(satorux): Replace this with a DCHECK(open_flags & ASYNC) once this | 579 // DCHECK(is_async()) once all async clients are migrated to |
292 // once all async clients are migrated to use Close(). crbug.com/114783 | 580 // use Close(). crbug.com/114783 |
293 WaitForIOCompletion(); | |
294 | 581 |
295 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); | 582 if (context_->async_in_progress()) |
296 if (file_ != base::kInvalidPlatformFileValue) | 583 CloseAndCancelAsync(); |
297 CancelIo(file_); | 584 else |
298 | 585 context_->CloseSync(); |
299 // Block until the last read/write operation is complete. | |
300 async_context_.reset(); | |
301 | |
302 if (file_ != base::kInvalidPlatformFileValue) { | |
303 if (!base::ClosePlatformFile(file_)) | |
304 NOTREACHED(); | |
305 file_ = base::kInvalidPlatformFileValue; | |
306 | |
307 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); | |
308 } | |
309 } | 586 } |
310 | 587 |
311 int FileStreamWin::Open(const FilePath& path, int open_flags, | 588 void FileStreamWin::CloseAndCancelAsync() { |
| 589 if (!IsOpen()) |
| 590 return; |
| 591 |
| 592 DCHECK(is_async()); |
| 593 |
| 594 AsyncContext* old_ctx = context_; |
| 595 context_ = new AsyncContext(bound_net_log_); |
| 596 context_->set_record_uma(old_ctx->record_uma()); |
| 597 old_ctx->set_auto_closed(true); |
| 598 old_ctx->Destroy(); |
| 599 } |
| 600 |
| 601 int FileStreamWin::Open(const FilePath& path, |
| 602 int open_flags, |
312 const CompletionCallback& callback) { | 603 const CompletionCallback& callback) { |
313 if (IsOpen()) { | 604 if (IsOpen()) { |
314 DLOG(FATAL) << "File is already open!"; | 605 DLOG(FATAL) << "File is already open!"; |
315 return ERR_UNEXPECTED; | 606 return ERR_UNEXPECTED; |
316 } | 607 } |
317 | 608 |
318 open_flags_ = open_flags; | 609 open_flags_ = open_flags; |
319 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 610 DCHECK(is_async()); |
320 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 611 context_->OpenAsync(path, open_flags, callback); |
321 DCHECK(!on_io_complete_.get()); | |
322 on_io_complete_.reset(new base::WaitableEvent( | |
323 false /* manual_reset */, false /* initially_signaled */)); | |
324 | |
325 // Passing &file_ to a thread pool looks unsafe but it's safe here as the | |
326 // destructor ensures that the open operation is complete with | |
327 // WaitForIOCompletion(). See also the destructor. | |
328 int* result = new int(OK); | |
329 const bool posted = base::WorkerPool::PostTaskAndReply( | |
330 FROM_HERE, | |
331 base::Bind(&InvokeAndSignal, | |
332 base::Bind(&OpenFile, path, open_flags, record_uma_, &file_, | |
333 result, bound_net_log_), | |
334 on_io_complete_.get()), | |
335 base::Bind(&FileStreamWin::OnOpened, | |
336 weak_ptr_factory_.GetWeakPtr(), | |
337 callback, base::Owned(result)), | |
338 true /* task_is_slow */); | |
339 DCHECK(posted); | |
340 return ERR_IO_PENDING; | 612 return ERR_IO_PENDING; |
341 } | 613 } |
342 | 614 |
343 int FileStreamWin::OpenSync(const FilePath& path, int open_flags) { | 615 int FileStreamWin::OpenSync(const FilePath& path, int open_flags) { |
344 if (IsOpen()) { | 616 if (IsOpen()) { |
345 DLOG(FATAL) << "File is already open!"; | 617 DLOG(FATAL) << "File is already open!"; |
346 return ERR_UNEXPECTED; | 618 return ERR_UNEXPECTED; |
347 } | 619 } |
348 | 620 |
349 open_flags_ = open_flags; | 621 open_flags_ = open_flags; |
350 | 622 // TODO(satorux): Put a DCHECK once all async clients are migrated |
351 int result = OK; | 623 // to use Open(). crbug.com/114783 |
352 OpenFile(path, open_flags_, record_uma_, &file_, &result, bound_net_log_); | 624 // |
353 if (result != OK) | 625 // DCHECK(!is_async()); |
354 return result; | 626 return context_->OpenSync(path, open_flags_); |
355 | |
356 // TODO(satorux): Remove this once all async clients are migrated to use | |
357 // Open(). crbug.com/114783 | |
358 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | |
359 async_context_.reset(new AsyncContext(bound_net_log_)); | |
360 if (record_uma_) | |
361 async_context_->EnableErrorStatistics(); | |
362 MessageLoopForIO::current()->RegisterIOHandler(file_, | |
363 async_context_.get()); | |
364 } | |
365 | |
366 return OK; | |
367 } | 627 } |
368 | 628 |
369 bool FileStreamWin::IsOpen() const { | 629 bool FileStreamWin::IsOpen() const { |
370 return file_ != base::kInvalidPlatformFileValue; | 630 return context_->file() != base::kInvalidPlatformFileValue; |
371 } | 631 } |
372 | 632 |
373 int FileStreamWin::Seek(Whence whence, int64 offset, | 633 int FileStreamWin::Seek(Whence whence, int64 offset, |
374 const Int64CompletionCallback& callback) { | 634 const Int64CompletionCallback& callback) { |
375 if (!IsOpen()) | 635 if (!IsOpen()) |
376 return ERR_UNEXPECTED; | 636 return ERR_UNEXPECTED; |
377 | 637 |
378 // Make sure we're async and we have no other in-flight async operations. | 638 // Make sure we're async and we have no other in-flight async operations. |
379 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 639 DCHECK(is_async()); |
380 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 640 context_->SeekAsync(whence, offset, callback); |
381 DCHECK(!on_io_complete_.get()); | |
382 | |
383 int64* result = new int64(-1); | |
384 on_io_complete_.reset(new base::WaitableEvent( | |
385 false /* manual_reset */, false /* initially_signaled */)); | |
386 | |
387 const bool posted = base::WorkerPool::PostTaskAndReply( | |
388 FROM_HERE, | |
389 base::Bind(&InvokeAndSignal, | |
390 // Unretained should be fine as we wait for a signal on | |
391 // on_io_complete_ at the destructor. | |
392 base::Bind(&FileStreamWin::SeekFile, base::Unretained(this), | |
393 whence, offset, result), | |
394 on_io_complete_.get()), | |
395 base::Bind(&FileStreamWin::OnSeeked, | |
396 weak_ptr_factory_.GetWeakPtr(), | |
397 callback, base::Owned(result)), | |
398 true /* task is slow */); | |
399 DCHECK(posted); | |
400 return ERR_IO_PENDING; | 641 return ERR_IO_PENDING; |
401 } | 642 } |
402 | 643 |
403 int64 FileStreamWin::SeekSync(Whence whence, int64 offset) { | 644 int64 FileStreamWin::SeekSync(Whence whence, int64 offset) { |
404 if (!IsOpen()) | 645 if (!IsOpen()) |
405 return ERR_UNEXPECTED; | 646 return ERR_UNEXPECTED; |
406 | 647 |
407 DCHECK(!async_context_.get() || async_context_->callback().is_null()); | 648 DCHECK(!is_async() || !context_->async_in_progress()); |
408 int64 result = -1; | 649 return context_->SeekSync(whence, offset); |
409 SeekFile(whence, offset, &result); | |
410 return result; | |
411 } | 650 } |
412 | 651 |
413 int64 FileStreamWin::Available() { | 652 int64 FileStreamWin::Available() { |
414 base::ThreadRestrictions::AssertIOAllowed(); | 653 base::ThreadRestrictions::AssertIOAllowed(); |
415 | 654 |
416 if (!IsOpen()) | 655 if (!IsOpen()) |
417 return ERR_UNEXPECTED; | 656 return ERR_UNEXPECTED; |
418 | 657 |
419 int64 cur_pos = SeekSync(FROM_CURRENT, 0); | 658 int64 cur_pos = SeekSync(FROM_CURRENT, 0); |
420 if (cur_pos < 0) | 659 if (cur_pos < 0) |
421 return cur_pos; | 660 return cur_pos; |
422 | 661 |
423 LARGE_INTEGER file_size; | 662 LARGE_INTEGER file_size; |
424 if (!GetFileSizeEx(file_, &file_size)) { | 663 if (!GetFileSizeEx(context_->file(), &file_size)) { |
425 DWORD error = GetLastError(); | 664 DWORD error = GetLastError(); |
426 LOG(WARNING) << "GetFileSizeEx failed: " << error; | 665 LOG(WARNING) << "GetFileSizeEx failed: " << error; |
427 return RecordAndMapError(error, | 666 return context_->RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE); |
428 FILE_ERROR_SOURCE_GET_SIZE, | |
429 record_uma_, | |
430 bound_net_log_); | |
431 } | 667 } |
432 | 668 |
433 return file_size.QuadPart - cur_pos; | 669 return file_size.QuadPart - cur_pos; |
434 } | 670 } |
435 | 671 |
436 int FileStreamWin::Read( | 672 int FileStreamWin::Read(IOBuffer* buf, |
437 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 673 int buf_len, |
438 DCHECK(async_context_.get()); | 674 const CompletionCallback& callback) { |
439 | |
440 if (!IsOpen()) | 675 if (!IsOpen()) |
441 return ERR_UNEXPECTED; | 676 return ERR_UNEXPECTED; |
442 | 677 |
443 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 678 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
444 | 679 return context_->ReadAsync(buf, buf_len, callback); |
445 OVERLAPPED* overlapped = NULL; | |
446 DCHECK(!callback.is_null()); | |
447 DCHECK(async_context_->callback().is_null()); | |
448 overlapped = async_context_->overlapped(); | |
449 async_context_->set_error_source(FILE_ERROR_SOURCE_READ); | |
450 | |
451 int rv = 0; | |
452 | |
453 DWORD bytes_read; | |
454 if (!ReadFile(file_, buf->data(), buf_len, &bytes_read, overlapped)) { | |
455 DWORD error = GetLastError(); | |
456 if (error == ERROR_IO_PENDING) { | |
457 async_context_->IOCompletionIsPending(callback, buf); | |
458 rv = ERR_IO_PENDING; | |
459 } else if (error == ERROR_HANDLE_EOF) { | |
460 rv = 0; // Report EOF by returning 0 bytes read. | |
461 } else { | |
462 LOG(WARNING) << "ReadFile failed: " << error; | |
463 rv = RecordAndMapError(error, | |
464 FILE_ERROR_SOURCE_READ, | |
465 record_uma_, | |
466 bound_net_log_); | |
467 } | |
468 } else if (overlapped) { | |
469 async_context_->IOCompletionIsPending(callback, buf); | |
470 rv = ERR_IO_PENDING; | |
471 } else { | |
472 rv = static_cast<int>(bytes_read); | |
473 } | |
474 return rv; | |
475 } | 680 } |
476 | 681 |
477 int FileStreamWin::ReadSync(char* buf, int buf_len) { | 682 int FileStreamWin::ReadSync(char* buf, int buf_len) { |
478 DCHECK(!async_context_.get()); | |
479 base::ThreadRestrictions::AssertIOAllowed(); | |
480 | |
481 if (!IsOpen()) | 683 if (!IsOpen()) |
482 return ERR_UNEXPECTED; | 684 return ERR_UNEXPECTED; |
483 | 685 |
484 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 686 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
485 | 687 return context_->ReadSync(buf, buf_len); |
486 int rv = 0; | |
487 | |
488 DWORD bytes_read; | |
489 if (!ReadFile(file_, buf, buf_len, &bytes_read, NULL)) { | |
490 DWORD error = GetLastError(); | |
491 if (error == ERROR_HANDLE_EOF) { | |
492 rv = 0; // Report EOF by returning 0 bytes read. | |
493 } else { | |
494 LOG(WARNING) << "ReadFile failed: " << error; | |
495 rv = RecordAndMapError(error, | |
496 FILE_ERROR_SOURCE_READ, | |
497 record_uma_, | |
498 bound_net_log_); | |
499 } | |
500 } else { | |
501 rv = static_cast<int>(bytes_read); | |
502 } | |
503 return rv; | |
504 } | 688 } |
505 | 689 |
506 int FileStreamWin::ReadUntilComplete(char *buf, int buf_len) { | 690 int FileStreamWin::ReadUntilComplete(char *buf, int buf_len) { |
507 int to_read = buf_len; | 691 int to_read = buf_len; |
508 int bytes_total = 0; | 692 int bytes_total = 0; |
509 | 693 |
510 do { | 694 do { |
511 int bytes_read = ReadSync(buf, to_read); | 695 int bytes_read = ReadSync(buf, to_read); |
512 if (bytes_read <= 0) { | 696 if (bytes_read <= 0) { |
513 if (bytes_total == 0) | 697 if (bytes_total == 0) |
514 return bytes_read; | 698 return bytes_read; |
515 | 699 |
516 return bytes_total; | 700 return bytes_total; |
517 } | 701 } |
518 | 702 |
519 bytes_total += bytes_read; | 703 bytes_total += bytes_read; |
520 buf += bytes_read; | 704 buf += bytes_read; |
521 to_read -= bytes_read; | 705 to_read -= bytes_read; |
522 } while (bytes_total < buf_len); | 706 } while (bytes_total < buf_len); |
523 | 707 |
524 return bytes_total; | 708 return bytes_total; |
525 } | 709 } |
526 | 710 |
527 int FileStreamWin::Write( | 711 int FileStreamWin::Write(IOBuffer* buf, |
528 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 712 int buf_len, |
529 DCHECK(async_context_.get()); | 713 const CompletionCallback& callback) { |
530 | |
531 if (!IsOpen()) | 714 if (!IsOpen()) |
532 return ERR_UNEXPECTED; | 715 return ERR_UNEXPECTED; |
533 | 716 |
534 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 717 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
535 | 718 return context_->WriteAsync(buf, buf_len, callback); |
536 OVERLAPPED* overlapped = NULL; | |
537 DCHECK(!callback.is_null()); | |
538 DCHECK(async_context_->callback().is_null()); | |
539 overlapped = async_context_->overlapped(); | |
540 async_context_->set_error_source(FILE_ERROR_SOURCE_WRITE); | |
541 | |
542 int rv = 0; | |
543 DWORD bytes_written = 0; | |
544 if (!WriteFile(file_, buf->data(), buf_len, &bytes_written, overlapped)) { | |
545 DWORD error = GetLastError(); | |
546 if (error == ERROR_IO_PENDING) { | |
547 async_context_->IOCompletionIsPending(callback, buf); | |
548 rv = ERR_IO_PENDING; | |
549 } else { | |
550 LOG(WARNING) << "WriteFile failed: " << error; | |
551 rv = RecordAndMapError(error, | |
552 FILE_ERROR_SOURCE_WRITE, | |
553 record_uma_, | |
554 bound_net_log_); | |
555 } | |
556 } else if (overlapped) { | |
557 async_context_->IOCompletionIsPending(callback, buf); | |
558 rv = ERR_IO_PENDING; | |
559 } else { | |
560 rv = static_cast<int>(bytes_written); | |
561 } | |
562 return rv; | |
563 } | 719 } |
564 | 720 |
565 int FileStreamWin::WriteSync( | 721 int FileStreamWin::WriteSync(const char* buf, int buf_len) { |
566 const char* buf, int buf_len) { | |
567 DCHECK(!async_context_.get()); | |
568 base::ThreadRestrictions::AssertIOAllowed(); | |
569 | |
570 if (!IsOpen()) | 722 if (!IsOpen()) |
571 return ERR_UNEXPECTED; | 723 return ERR_UNEXPECTED; |
572 | 724 |
573 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 725 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
574 | 726 return context_->WriteSync(buf, buf_len); |
575 int rv = 0; | |
576 DWORD bytes_written = 0; | |
577 if (!WriteFile(file_, buf, buf_len, &bytes_written, NULL)) { | |
578 DWORD error = GetLastError(); | |
579 LOG(WARNING) << "WriteFile failed: " << error; | |
580 rv = RecordAndMapError(error, | |
581 FILE_ERROR_SOURCE_WRITE, | |
582 record_uma_, | |
583 bound_net_log_); | |
584 } else { | |
585 rv = static_cast<int>(bytes_written); | |
586 } | |
587 return rv; | |
588 } | 727 } |
589 | 728 |
590 int FileStreamWin::Flush() { | 729 int FileStreamWin::Flush() { |
591 base::ThreadRestrictions::AssertIOAllowed(); | 730 base::ThreadRestrictions::AssertIOAllowed(); |
592 | 731 |
593 if (!IsOpen()) | 732 if (!IsOpen()) |
594 return ERR_UNEXPECTED; | 733 return ERR_UNEXPECTED; |
595 | 734 |
596 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 735 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
597 if (FlushFileBuffers(file_)) { | 736 if (FlushFileBuffers(context_->file())) { |
598 return OK; | 737 return OK; |
599 } | 738 } |
600 | 739 |
601 return RecordAndMapError(GetLastError(), | 740 return context_->RecordAndMapError(GetLastError(), FILE_ERROR_SOURCE_FLUSH); |
602 FILE_ERROR_SOURCE_FLUSH, | |
603 record_uma_, | |
604 bound_net_log_); | |
605 } | 741 } |
606 | 742 |
607 int64 FileStreamWin::Truncate(int64 bytes) { | 743 int64 FileStreamWin::Truncate(int64 bytes) { |
608 base::ThreadRestrictions::AssertIOAllowed(); | 744 base::ThreadRestrictions::AssertIOAllowed(); |
609 | 745 |
610 if (!IsOpen()) | 746 if (!IsOpen()) |
611 return ERR_UNEXPECTED; | 747 return ERR_UNEXPECTED; |
612 | 748 |
613 // We'd better be open for writing. | 749 // We'd better be open for writing. |
614 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 750 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
615 | 751 |
616 // Seek to the position to truncate from. | 752 // Seek to the position to truncate from. |
617 int64 seek_position = SeekSync(FROM_BEGIN, bytes); | 753 int64 seek_position = SeekSync(FROM_BEGIN, bytes); |
618 if (seek_position != bytes) | 754 if (seek_position != bytes) |
619 return ERR_UNEXPECTED; | 755 return ERR_UNEXPECTED; |
620 | 756 |
621 // And truncate the file. | 757 // And truncate the file. |
622 BOOL result = SetEndOfFile(file_); | 758 BOOL result = SetEndOfFile(context_->file()); |
623 if (!result) { | 759 if (!result) { |
624 DWORD error = GetLastError(); | 760 DWORD error = GetLastError(); |
625 LOG(WARNING) << "SetEndOfFile failed: " << error; | 761 LOG(WARNING) << "SetEndOfFile failed: " << error; |
626 return RecordAndMapError(error, | 762 return context_->RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF); |
627 FILE_ERROR_SOURCE_SET_EOF, | |
628 record_uma_, | |
629 bound_net_log_); | |
630 } | 763 } |
631 | 764 |
632 // Success. | 765 // Success. |
633 return seek_position; | 766 return seek_position; |
634 } | 767 } |
635 | 768 |
636 void FileStreamWin::EnableErrorStatistics() { | 769 void FileStreamWin::EnableErrorStatistics() { |
637 record_uma_ = true; | 770 context_->set_record_uma(true); |
638 | |
639 if (async_context_.get()) | |
640 async_context_->EnableErrorStatistics(); | |
641 } | 771 } |
642 | 772 |
643 void FileStreamWin::SetBoundNetLogSource( | 773 void FileStreamWin::SetBoundNetLogSource( |
644 const net::BoundNetLog& owner_bound_net_log) { | 774 const net::BoundNetLog& owner_bound_net_log) { |
645 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | 775 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && |
646 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | 776 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { |
647 // Both |BoundNetLog|s are invalid. | 777 // Both |BoundNetLog|s are invalid. |
648 return; | 778 return; |
649 } | 779 } |
650 | 780 |
651 // Should never connect to itself. | 781 // Should never connect to itself. |
652 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); | 782 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); |
653 | 783 |
654 bound_net_log_.AddEvent( | 784 bound_net_log_.AddEvent( |
655 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | 785 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, |
656 owner_bound_net_log.source().ToEventParametersCallback()); | 786 owner_bound_net_log.source().ToEventParametersCallback()); |
657 | 787 |
658 owner_bound_net_log.AddEvent( | 788 owner_bound_net_log.AddEvent( |
659 net::NetLog::TYPE_FILE_STREAM_SOURCE, | 789 net::NetLog::TYPE_FILE_STREAM_SOURCE, |
660 bound_net_log_.source().ToEventParametersCallback()); | 790 bound_net_log_.source().ToEventParametersCallback()); |
661 } | 791 } |
662 | 792 |
663 base::PlatformFile FileStreamWin::GetPlatformFileForTesting() { | 793 base::PlatformFile FileStreamWin::GetPlatformFileForTesting() { |
664 return file_; | 794 return context_->file(); |
665 } | |
666 | |
667 void FileStreamWin::OnClosed(const CompletionCallback& callback) { | |
668 file_ = base::kInvalidPlatformFileValue; | |
669 | |
670 // Reset this before Run() as Run() may issue a new async operation. | |
671 ResetOnIOComplete(); | |
672 callback.Run(OK); | |
673 } | |
674 | |
675 void FileStreamWin::SeekFile(Whence whence, int64 offset, int64* result) { | |
676 LARGE_INTEGER distance, res; | |
677 distance.QuadPart = offset; | |
678 DWORD move_method = static_cast<DWORD>(whence); | |
679 if (!SetFilePointerEx(file_, distance, &res, move_method)) { | |
680 DWORD error = GetLastError(); | |
681 LOG(WARNING) << "SetFilePointerEx failed: " << error; | |
682 *result = RecordAndMapError(error, | |
683 FILE_ERROR_SOURCE_SEEK, | |
684 record_uma_, | |
685 bound_net_log_); | |
686 return; | |
687 } | |
688 if (async_context_.get()) { | |
689 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); | |
690 SetOffset(async_context_->overlapped(), res); | |
691 } | |
692 *result = res.QuadPart; | |
693 } | |
694 | |
695 void FileStreamWin::OnOpened(const CompletionCallback& callback, int* result) { | |
696 if (*result == OK) { | |
697 async_context_.reset(new AsyncContext(bound_net_log_)); | |
698 if (record_uma_) | |
699 async_context_->EnableErrorStatistics(); | |
700 MessageLoopForIO::current()->RegisterIOHandler(file_, | |
701 async_context_.get()); | |
702 } | |
703 | |
704 // Reset this before Run() as Run() may issue a new async operation. | |
705 ResetOnIOComplete(); | |
706 callback.Run(*result); | |
707 } | |
708 | |
709 void FileStreamWin::OnSeeked( | |
710 const Int64CompletionCallback& callback, | |
711 int64* result) { | |
712 // Reset this before Run() as Run() may issue a new async operation. | |
713 ResetOnIOComplete(); | |
714 callback.Run(*result); | |
715 } | |
716 | |
717 void FileStreamWin::ResetOnIOComplete() { | |
718 on_io_complete_.reset(); | |
719 weak_ptr_factory_.InvalidateWeakPtrs(); | |
720 } | |
721 | |
722 void FileStreamWin::WaitForIOCompletion() { | |
723 // http://crbug.com/115067 | |
724 base::ThreadRestrictions::ScopedAllowWait allow_wait; | |
725 if (on_io_complete_.get()) { | |
726 on_io_complete_->Wait(); | |
727 on_io_complete_.reset(); | |
728 } | |
729 } | 795 } |
730 | 796 |
731 } // namespace net | 797 } // namespace net |
OLD | NEW |