Chromium Code Reviews| 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). | |
| 6 #define _FILE_OFFSET_BITS 64 | |
| 7 | |
| 8 #include "net/base/file_stream_context.h" | 5 #include "net/base/file_stream_context.h" |
| 9 | 6 |
| 10 #include <errno.h> | 7 #include <errno.h> |
| 11 #include <fcntl.h> | |
| 12 #include <sys/stat.h> | |
| 13 #include <sys/types.h> | |
| 14 #include <unistd.h> | |
| 15 | 8 |
| 16 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 17 #include "base/bind.h" | 10 #include "base/bind.h" |
| 18 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 19 #include "base/callback.h" | 12 #include "base/callback.h" |
| 20 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 21 #include "base/location.h" | 14 #include "base/location.h" |
| 22 #include "base/logging.h" | 15 #include "base/logging.h" |
| 23 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 24 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 25 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 26 #include "base/task_runner_util.h" | 19 #include "base/task_runner_util.h" |
| 27 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 28 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 29 | 22 |
| 30 #if defined(OS_ANDROID) | |
| 31 // Android's bionic libc only supports the LFS transitional API. | |
| 32 #define off_t off64_t | |
| 33 #define lseek lseek64 | |
| 34 #define stat stat64 | |
| 35 #define fstat fstat64 | |
| 36 #endif | |
| 37 | |
| 38 namespace net { | 23 namespace net { |
| 39 | 24 |
| 40 // We cast back and forth, so make sure it's the size we're expecting. | |
| 41 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); | |
| 42 | |
| 43 // Make sure our Whence mappings match the system headers. | |
| 44 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && | |
| 45 FROM_CURRENT == SEEK_CUR && | |
| 46 FROM_END == SEEK_END, whence_matches_system); | |
| 47 | |
| 48 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner) | 25 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner) |
| 49 : async_in_progress_(false), | 26 : async_in_progress_(false), |
| 50 orphaned_(false), | 27 orphaned_(false), |
| 51 task_runner_(task_runner) { | 28 task_runner_(task_runner), |
| 29 buf_len_(0) { | |
| 52 } | 30 } |
| 53 | 31 |
| 54 FileStream::Context::Context(base::File file, | 32 FileStream::Context::Context(base::File file, |
| 55 const scoped_refptr<base::TaskRunner>& task_runner) | 33 const scoped_refptr<base::TaskRunner>& task_runner) |
| 56 : file_(file.Pass()), | 34 : file_(file.Pass()), |
| 57 async_in_progress_(false), | 35 async_in_progress_(false), |
| 58 orphaned_(false), | 36 orphaned_(false), |
| 59 task_runner_(task_runner) { | 37 task_runner_(task_runner), |
| 38 buf_len_(0) { | |
| 60 } | 39 } |
| 61 | 40 |
| 62 FileStream::Context::~Context() { | 41 FileStream::Context::~Context() { |
| 63 } | 42 } |
| 64 | 43 |
| 65 int FileStream::Context::ReadAsync(IOBuffer* in_buf, | 44 int FileStream::Context::Read(IOBuffer* in_buf, |
| 66 int buf_len, | 45 int buf_len, |
| 67 const CompletionCallback& callback) { | 46 const CompletionCallback& callback) { |
| 68 DCHECK(!async_in_progress_); | 47 DCHECK(!async_in_progress_); |
| 69 | 48 |
| 70 scoped_refptr<IOBuffer> buf = in_buf; | 49 scoped_refptr<IOBuffer> buf = in_buf; |
| 50 in_flight_buf_ = NULL; | |
|
wtc
2014/06/16 23:01:44
Also reset buf_len_ to 0.
rvargas (doing something else)
2014/06/18 01:36:38
removed
| |
| 71 const bool posted = base::PostTaskAndReplyWithResult( | 51 const bool posted = base::PostTaskAndReplyWithResult( |
| 72 task_runner_.get(), | 52 task_runner_.get(), |
| 73 FROM_HERE, | 53 FROM_HERE, |
| 74 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len), | 54 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len), |
| 75 base::Bind(&Context::OnAsyncCompleted, | 55 base::Bind(&Context::OnAsyncCompleted, |
| 76 base::Unretained(this), | 56 base::Unretained(this), |
| 77 IntToInt64(callback))); | 57 IntToInt64(callback))); |
| 78 DCHECK(posted); | 58 DCHECK(posted); |
| 79 | 59 |
| 80 async_in_progress_ = true; | 60 async_in_progress_ = true; |
| 81 return ERR_IO_PENDING; | 61 return ERR_IO_PENDING; |
| 82 } | 62 } |
| 83 | 63 |
| 84 int FileStream::Context::WriteAsync(IOBuffer* in_buf, | 64 int FileStream::Context::ReadNonBlocking(IOBuffer* buf, |
| 85 int buf_len, | 65 int buf_len, |
| 86 const CompletionCallback& callback) { | 66 const CompletionCallback& callback) { |
| 67 DCHECK(!async_in_progress_); | |
| 68 | |
| 69 in_flight_buf_ = buf; | |
| 70 buf_len_ = buf_len; | |
| 71 callback_ = callback; | |
| 72 | |
| 73 base::MessageLoopForIO::current()->WatchFileDescriptor( | |
| 74 file_.GetPlatformFile(), false /* persistent */, | |
| 75 base::MessageLoopForIO::WATCH_READ, &file_watcher_, this); | |
| 76 | |
| 77 async_in_progress_ = true; | |
| 78 return ERR_IO_PENDING; | |
| 79 } | |
| 80 | |
| 81 int FileStream::Context::Write(IOBuffer* in_buf, | |
| 82 int buf_len, | |
| 83 const CompletionCallback& callback) { | |
| 87 DCHECK(!async_in_progress_); | 84 DCHECK(!async_in_progress_); |
| 88 | 85 |
| 89 scoped_refptr<IOBuffer> buf = in_buf; | 86 scoped_refptr<IOBuffer> buf = in_buf; |
| 87 in_flight_buf_ = NULL; | |
|
wtc
2014/06/16 23:01:44
Also reset buf_len_ to 0.
rvargas (doing something else)
2014/06/18 01:36:38
removed
| |
| 90 const bool posted = base::PostTaskAndReplyWithResult( | 88 const bool posted = base::PostTaskAndReplyWithResult( |
| 91 task_runner_.get(), | 89 task_runner_.get(), |
| 92 FROM_HERE, | 90 FROM_HERE, |
| 93 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len), | 91 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len), |
| 94 base::Bind(&Context::OnAsyncCompleted, | 92 base::Bind(&Context::OnAsyncCompleted, |
| 95 base::Unretained(this), | 93 base::Unretained(this), |
| 96 IntToInt64(callback))); | 94 IntToInt64(callback))); |
| 97 DCHECK(posted); | 95 DCHECK(posted); |
| 98 | 96 |
| 99 async_in_progress_ = true; | 97 async_in_progress_ = true; |
| 100 return ERR_IO_PENDING; | 98 return ERR_IO_PENDING; |
| 101 } | 99 } |
| 102 | 100 |
| 103 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence, | 101 int FileStream::Context::WriteNonBlocking(IOBuffer* buf, |
| 104 int64 offset) { | 102 int buf_len, |
| 105 off_t res = lseek(file_.GetPlatformFile(), static_cast<off_t>(offset), | 103 const CompletionCallback& callback) { |
| 106 static_cast<int>(whence)); | 104 DCHECK(!async_in_progress_); |
| 107 if (res == static_cast<off_t>(-1)) | |
| 108 return IOResult::FromOSError(errno); | |
| 109 | 105 |
| 110 return IOResult(res, 0); | 106 in_flight_buf_ = buf; |
| 107 buf_len_ = buf_len; | |
| 108 callback_ = callback; | |
| 109 | |
| 110 base::MessageLoopForIO::current()->WatchFileDescriptor( | |
| 111 file_.GetPlatformFile(), false /* persistent */, | |
| 112 base::MessageLoopForIO::WATCH_WRITE, &file_watcher_, this); | |
| 113 | |
| 114 async_in_progress_ = true; | |
| 115 return ERR_IO_PENDING; | |
| 111 } | 116 } |
| 112 | 117 |
| 113 FileStream::Context::IOResult FileStream::Context::FlushFileImpl() { | 118 FileStream::Context::IOResult FileStream::Context::SeekFileImpl( |
| 114 ssize_t res = HANDLE_EINTR(fsync(file_.GetPlatformFile())); | 119 base::File::Whence whence, |
| 120 int64 offset) { | |
| 121 int64 res = file_.Seek(whence, offset); | |
| 115 if (res == -1) | 122 if (res == -1) |
| 116 return IOResult::FromOSError(errno); | 123 return IOResult::FromOSError(errno); |
| 117 | 124 |
| 118 return IOResult(res, 0); | 125 return IOResult(res, 0); |
| 119 } | 126 } |
| 120 | 127 |
| 128 void FileStream::Context::OnFileOpened() { | |
| 129 } | |
| 130 | |
| 131 void FileStream::Context::CancelIO() { | |
| 132 if (!in_flight_buf_) | |
| 133 return; | |
| 134 | |
| 135 // Just waiting to issue a non-blocking op. Stop the watcher right away | |
| 136 // because CloseAndDelete() will only delete this object after the file is | |
| 137 // closed on the task runner. | |
| 138 async_in_progress_ = false; | |
| 139 file_watcher_.StopWatchingFileDescriptor(); | |
| 140 CloseAndDelete(); | |
| 141 } | |
| 142 | |
| 121 FileStream::Context::IOResult FileStream::Context::ReadFileImpl( | 143 FileStream::Context::IOResult FileStream::Context::ReadFileImpl( |
| 122 scoped_refptr<IOBuffer> buf, | 144 scoped_refptr<IOBuffer> buf, |
| 123 int buf_len) { | 145 int buf_len) { |
| 124 // Loop in the case of getting interrupted by a signal. | 146 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len); |
| 125 ssize_t res = HANDLE_EINTR(read(file_.GetPlatformFile(), buf->data(), | |
| 126 static_cast<size_t>(buf_len))); | |
| 127 if (res == -1) | 147 if (res == -1) |
| 128 return IOResult::FromOSError(errno); | 148 return IOResult::FromOSError(errno); |
| 129 | 149 |
| 130 return IOResult(res, 0); | 150 return IOResult(res, 0); |
| 131 } | 151 } |
| 132 | 152 |
| 133 FileStream::Context::IOResult FileStream::Context::WriteFileImpl( | 153 FileStream::Context::IOResult FileStream::Context::WriteFileImpl( |
| 134 scoped_refptr<IOBuffer> buf, | 154 scoped_refptr<IOBuffer> buf, |
| 135 int buf_len) { | 155 int buf_len) { |
| 136 ssize_t res = HANDLE_EINTR(write(file_.GetPlatformFile(), buf->data(), | 156 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len); |
| 137 buf_len)); | |
| 138 if (res == -1) | 157 if (res == -1) |
| 139 return IOResult::FromOSError(errno); | 158 return IOResult::FromOSError(errno); |
| 140 | 159 |
| 141 return IOResult(res, 0); | 160 return IOResult(res, 0); |
| 142 } | 161 } |
| 143 | 162 |
| 163 void FileStream::Context::OnFileCanReadWithoutBlocking(int fd) { | |
|
wtc
2014/06/16 23:01:44
IMPORTANT: are OnFileCanReadWithoutBlocking and On
rvargas (doing something else)
2014/06/16 23:41:44
These methods are the implementation of the Messag
| |
| 164 DCHECK_EQ(file_.GetPlatformFile(), fd); | |
| 165 DCHECK(async_in_progress_); | |
| 166 async_in_progress_ = false; | |
| 167 Read(in_flight_buf_.get(), buf_len_, callback_); | |
| 168 } | |
| 169 | |
| 170 void FileStream::Context::OnFileCanWriteWithoutBlocking(int fd) { | |
| 171 DCHECK_EQ(file_.GetPlatformFile(), fd); | |
| 172 DCHECK(async_in_progress_); | |
| 173 async_in_progress_ = false; | |
| 174 Write(in_flight_buf_.get(), buf_len_, callback_); | |
| 175 } | |
| 176 | |
| 144 } // namespace net | 177 } // namespace net |
| OLD | NEW |