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 // 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> |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 const net::BoundNetLog& bound_net_log) { | 224 const net::BoundNetLog& bound_net_log) { |
225 base::ThreadRestrictions::AssertIOAllowed(); | 225 base::ThreadRestrictions::AssertIOAllowed(); |
226 ssize_t res = HANDLE_EINTR(fsync(file)); | 226 ssize_t res = HANDLE_EINTR(fsync(file)); |
227 if (res == -1) { | 227 if (res == -1) { |
228 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma, | 228 res = RecordAndMapError(errno, FILE_ERROR_SOURCE_FLUSH, record_uma, |
229 bound_net_log); | 229 bound_net_log); |
230 } | 230 } |
231 return res; | 231 return res; |
232 } | 232 } |
233 | 233 |
| 234 // Flushes a file using FlushFile() and signals the completion. |
| 235 void FlushFileAndSignal(base::PlatformFile file, |
| 236 int* result, |
| 237 bool record_uma, |
| 238 base::WaitableEvent* on_io_complete, |
| 239 const net::BoundNetLog& bound_net_log) { |
| 240 *result = FlushFile(file, record_uma, bound_net_log); |
| 241 on_io_complete->Signal(); |
| 242 } |
| 243 |
234 // Called when Read(), Write() or Seek() is completed. | 244 // Called when Read(), Write() or Seek() is completed. |
235 // |result| contains the result or a network error code. | 245 // |result| contains the result or a network error code. |
236 template <typename R> | 246 template <typename R> |
237 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream, | 247 void OnIOComplete(const base::WeakPtr<FileStreamPosix>& stream, |
238 const base::Callback<void(R)>& callback, | 248 const base::Callback<void(R)>& callback, |
239 R* result) { | 249 R* result) { |
240 if (!stream.get()) | 250 if (!stream.get()) |
241 return; | 251 return; |
242 | 252 |
243 // Reset this before Run() as Run() may issue a new async operation. | 253 // Reset this before Run() as Run() may issue a new async operation. |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 int result = ftruncate(file_, bytes); | 591 int result = ftruncate(file_, bytes); |
582 if (result == 0) | 592 if (result == 0) |
583 return seek_position; | 593 return seek_position; |
584 | 594 |
585 return RecordAndMapError(errno, | 595 return RecordAndMapError(errno, |
586 FILE_ERROR_SOURCE_SET_EOF, | 596 FILE_ERROR_SOURCE_SET_EOF, |
587 record_uma_, | 597 record_uma_, |
588 bound_net_log_); | 598 bound_net_log_); |
589 } | 599 } |
590 | 600 |
591 int FileStreamPosix::Flush() { | 601 int FileStreamPosix::Flush(const CompletionCallback& callback) { |
| 602 if (!IsOpen()) |
| 603 return ERR_UNEXPECTED; |
| 604 |
| 605 // Make sure we're async and we have no other in-flight async operations. |
| 606 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| 607 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
| 608 DCHECK(!on_io_complete_.get()); |
| 609 |
| 610 on_io_complete_.reset(new base::WaitableEvent( |
| 611 false /* manual_reset */, false /* initially_signaled */)); |
| 612 |
| 613 int* result = new int(OK); |
| 614 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 615 FROM_HERE, |
| 616 base::Bind(&FlushFileAndSignal, file_, result, |
| 617 record_uma_, on_io_complete_.get(), bound_net_log_), |
| 618 base::Bind(&OnIOComplete<int>, |
| 619 weak_ptr_factory_.GetWeakPtr(), |
| 620 callback, base::Owned(result)), |
| 621 true /* task is slow */); |
| 622 DCHECK(posted); |
| 623 return ERR_IO_PENDING; |
| 624 } |
| 625 |
| 626 int FileStreamPosix::FlushSync() { |
592 if (!IsOpen()) | 627 if (!IsOpen()) |
593 return ERR_UNEXPECTED; | 628 return ERR_UNEXPECTED; |
594 | 629 |
595 return FlushFile(file_, record_uma_, bound_net_log_); | 630 return FlushFile(file_, record_uma_, bound_net_log_); |
596 } | 631 } |
597 | 632 |
598 void FileStreamPosix::EnableErrorStatistics() { | 633 void FileStreamPosix::EnableErrorStatistics() { |
599 record_uma_ = true; | 634 record_uma_ = true; |
600 } | 635 } |
601 | 636 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 void FileStreamPosix::WaitForIOCompletion() { | 674 void FileStreamPosix::WaitForIOCompletion() { |
640 // http://crbug.com/115067 | 675 // http://crbug.com/115067 |
641 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 676 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
642 if (on_io_complete_.get()) { | 677 if (on_io_complete_.get()) { |
643 on_io_complete_->Wait(); | 678 on_io_complete_->Wait(); |
644 on_io_complete_.reset(); | 679 on_io_complete_.reset(); |
645 } | 680 } |
646 } | 681 } |
647 | 682 |
648 } // namespace net | 683 } // namespace net |
OLD | NEW |