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