OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/file_stream_context.h" |
| 6 |
| 7 #include "base/location.h" |
| 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/task_runner_util.h" |
| 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/threading/worker_pool.h" |
| 12 #include "net/base/file_stream_net_log_parameters.h" |
| 13 #include "net/base/net_errors.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 void CallInt64ToInt(const net::CompletionCallback& callback, int64 result) { |
| 18 callback.Run(static_cast<int>(result)); |
| 19 } |
| 20 |
| 21 } |
| 22 |
| 23 namespace net { |
| 24 |
| 25 void FileStream::Context::Orphan() { |
| 26 DCHECK(!orphaned_); |
| 27 |
| 28 orphaned_ = true; |
| 29 if (file_ != base::kInvalidPlatformFileValue) |
| 30 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 31 |
| 32 if (!async_in_progress_) { |
| 33 CloseAndDelete(); |
| 34 } else if (file_ != base::kInvalidPlatformFileValue) { |
| 35 CancelIo(file_); |
| 36 } |
| 37 } |
| 38 |
| 39 void FileStream::Context::OpenAsync(const FilePath& path, |
| 40 int open_flags, |
| 41 const CompletionCallback& callback) { |
| 42 DCHECK(!async_in_progress_); |
| 43 |
| 44 BeginOpenEvent(path); |
| 45 |
| 46 const bool posted = base::PostTaskAndReplyWithResult( |
| 47 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), |
| 48 FROM_HERE, |
| 49 base::Bind(&Context::OpenFileImpl, |
| 50 base::Unretained(this), path, open_flags), |
| 51 base::Bind(&Context::OnOpenCompleted, |
| 52 base::Unretained(this), callback)); |
| 53 DCHECK(posted); |
| 54 |
| 55 async_in_progress_ = true; |
| 56 } |
| 57 |
| 58 int FileStream::Context::OpenSync(const FilePath& path, int open_flags) { |
| 59 DCHECK(!async_in_progress_); |
| 60 |
| 61 BeginOpenEvent(path); |
| 62 OpenResult result = OpenFileImpl(path, open_flags); |
| 63 file_ = result.file; |
| 64 if (file_ == base::kInvalidPlatformFileValue) { |
| 65 result.error_code = ProcessOpenError(result.error_code); |
| 66 } else { |
| 67 // TODO(satorux): Remove this once all async clients are migrated to use |
| 68 // Open(). crbug.com/114783 |
| 69 if (open_flags & base::PLATFORM_FILE_ASYNC) |
| 70 OnAsyncFileOpened(); |
| 71 } |
| 72 return result.error_code; |
| 73 } |
| 74 |
| 75 void FileStream::Context::CloseSync() { |
| 76 DCHECK(!async_in_progress_); |
| 77 if (file_ != base::kInvalidPlatformFileValue) { |
| 78 base::ClosePlatformFile(file_); |
| 79 file_ = base::kInvalidPlatformFileValue; |
| 80 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 81 } |
| 82 } |
| 83 |
| 84 void FileStream::Context::SeekAsync(Whence whence, |
| 85 int64 offset, |
| 86 const Int64CompletionCallback& callback) { |
| 87 DCHECK(!async_in_progress_); |
| 88 |
| 89 const bool posted = base::PostTaskAndReplyWithResult( |
| 90 base::WorkerPool::GetTaskRunner(true /* task is slow */), |
| 91 FROM_HERE, |
| 92 base::Bind(&Context::SeekFileImpl, |
| 93 base::Unretained(this), whence, offset), |
| 94 base::Bind(&Context::ProcessAsyncResult, |
| 95 base::Unretained(this), callback, FILE_ERROR_SOURCE_SEEK)); |
| 96 DCHECK(posted); |
| 97 |
| 98 async_in_progress_ = true; |
| 99 } |
| 100 |
| 101 int64 FileStream::Context::SeekSync(Whence whence, int64 offset) { |
| 102 int64 result = SeekFileImpl(whence, offset); |
| 103 CheckForIOError(&result, FILE_ERROR_SOURCE_SEEK); |
| 104 return result; |
| 105 } |
| 106 |
| 107 int FileStream::Context::RecordAndMapError(int error, |
| 108 FileErrorSource source) const { |
| 109 // The following check is against incorrect use or bug. File descriptor |
| 110 // shouldn't ever be closed outside of FileStream while it still tries to do |
| 111 // something with it. |
| 112 DCHECK(error != ERROR_BAD_FILE); |
| 113 net::Error net_error = MapSystemError(error); |
| 114 |
| 115 if (!orphaned_) { |
| 116 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_ERROR, |
| 117 base::Bind(&NetLogFileStreamErrorCallback, |
| 118 source, error, net_error)); |
| 119 } |
| 120 RecordFileError(error, source, record_uma_); |
| 121 return net_error; |
| 122 } |
| 123 |
| 124 void FileStream::Context::BeginOpenEvent(const FilePath& path) { |
| 125 std::string file_name = path.AsUTF8Unsafe(); |
| 126 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 127 NetLog::StringCallback("file_name", &file_name)); |
| 128 } |
| 129 |
| 130 FileStream::Context::OpenResult FileStream::Context::OpenFileImpl( |
| 131 const FilePath& path, int open_flags) { |
| 132 OpenResult result; |
| 133 result.error_code = OK; |
| 134 result.file = base::CreatePlatformFile(path, open_flags, NULL, NULL); |
| 135 if (result.file == base::kInvalidPlatformFileValue) |
| 136 result.error_code = GetLastErrno(); |
| 137 |
| 138 return result; |
| 139 } |
| 140 |
| 141 int FileStream::Context::ProcessOpenError(int error_code) { |
| 142 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 143 return RecordAndMapError(error_code, FILE_ERROR_SOURCE_OPEN); |
| 144 } |
| 145 |
| 146 void FileStream::Context::OnOpenCompleted(const CompletionCallback& callback, |
| 147 OpenResult result) { |
| 148 file_ = result.file; |
| 149 if (file_ == base::kInvalidPlatformFileValue) |
| 150 result.error_code = ProcessOpenError(result.error_code); |
| 151 else if (!orphaned_) |
| 152 OnAsyncFileOpened(); |
| 153 OnAsyncCompleted(IntToInt64(callback), result.error_code); |
| 154 } |
| 155 |
| 156 void FileStream::Context::CloseAndDelete() { |
| 157 DCHECK(!async_in_progress_); |
| 158 |
| 159 if (file_ == base::kInvalidPlatformFileValue) { |
| 160 delete this; |
| 161 } else { |
| 162 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 163 FROM_HERE, |
| 164 base::Bind(base::IgnoreResult(&base::ClosePlatformFile), file_), |
| 165 base::Bind(&Context::OnCloseCompleted, base::Unretained(this)), |
| 166 true /* task_is_slow */); |
| 167 DCHECK(posted); |
| 168 file_ = base::kInvalidPlatformFileValue; |
| 169 } |
| 170 } |
| 171 |
| 172 void FileStream::Context::OnCloseCompleted() { |
| 173 delete this; |
| 174 } |
| 175 |
| 176 Int64CompletionCallback FileStream::Context::IntToInt64( |
| 177 const CompletionCallback& callback) { |
| 178 return base::Bind(&CallInt64ToInt, callback); |
| 179 } |
| 180 |
| 181 void FileStream::Context::CheckForIOError(int64* result, |
| 182 FileErrorSource source) { |
| 183 if (*result < 0) |
| 184 *result = RecordAndMapError(static_cast<int>(*result), source); |
| 185 } |
| 186 |
| 187 void FileStream::Context::ProcessAsyncResult( |
| 188 const Int64CompletionCallback& callback, |
| 189 FileErrorSource source, |
| 190 int64 result) { |
| 191 CheckForIOError(&result, source); |
| 192 OnAsyncCompleted(callback, result); |
| 193 } |
| 194 |
| 195 void FileStream::Context::OnAsyncCompleted( |
| 196 const Int64CompletionCallback& callback, |
| 197 int64 result) { |
| 198 // Reset this before Run() as Run() may issue a new async operation. Also it |
| 199 // should be reset before CloseAsync() because it shouldn't run if any async |
| 200 // operation is in progress. |
| 201 async_in_progress_ = false; |
| 202 if (orphaned_) |
| 203 CloseAndDelete(); |
| 204 else |
| 205 callback.Run(result); |
| 206 } |
| 207 |
| 208 } // namespace net |
| 209 |
OLD | NEW |