| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 int* result, | 76 int* result, |
| 77 const net::BoundNetLog& bound_net_log) { | 77 const net::BoundNetLog& bound_net_log) { |
| 78 std::string file_name = path.AsUTF8Unsafe(); | 78 std::string file_name = path.AsUTF8Unsafe(); |
| 79 bound_net_log.BeginEvent( | 79 bound_net_log.BeginEvent( |
| 80 net::NetLog::TYPE_FILE_STREAM_OPEN, | 80 net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 81 NetLog::StringCallback("file_name", &file_name)); | 81 NetLog::StringCallback("file_name", &file_name)); |
| 82 | 82 |
| 83 *result = OK; | 83 *result = OK; |
| 84 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); | 84 *file = base::CreatePlatformFile(path, open_flags, NULL, NULL); |
| 85 if (*file == base::kInvalidPlatformFileValue) { | 85 if (*file == base::kInvalidPlatformFileValue) { |
| 86 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); | 86 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 87 *result = RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma, | 87 *result = RecordAndMapError(errno, FILE_ERROR_SOURCE_OPEN, record_uma, |
| 88 bound_net_log); | 88 bound_net_log); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Opens a file using OpenFile() and then signals the completion. | 92 // Opens a file using OpenFile() and then signals the completion. |
| 93 void OpenFileAndSignal(const FilePath& path, | 93 void OpenFileAndSignal(const FilePath& path, |
| 94 int open_flags, | 94 int open_flags, |
| 95 bool record_uma, | 95 bool record_uma, |
| 96 base::PlatformFile* file, | 96 base::PlatformFile* file, |
| 97 int* result, | 97 int* result, |
| 98 base::WaitableEvent* on_io_complete, | 98 base::WaitableEvent* on_io_complete, |
| 99 const net::BoundNetLog& bound_net_log) { | 99 const net::BoundNetLog& bound_net_log) { |
| 100 OpenFile(path, open_flags, record_uma, file, result, bound_net_log); | 100 OpenFile(path, open_flags, record_uma, file, result, bound_net_log); |
| 101 on_io_complete->Signal(); | 101 on_io_complete->Signal(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // Closes a file with some network logging. | 104 // Closes a file with some network logging. |
| 105 void CloseFile(base::PlatformFile file, | 105 void CloseFile(base::PlatformFile file, |
| 106 const net::BoundNetLog& bound_net_log) { | 106 const net::BoundNetLog& bound_net_log) { |
| 107 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE, NULL); | 107 bound_net_log.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE); |
| 108 if (file == base::kInvalidPlatformFileValue) | 108 if (file == base::kInvalidPlatformFileValue) |
| 109 return; | 109 return; |
| 110 | 110 |
| 111 if (!base::ClosePlatformFile(file)) | 111 if (!base::ClosePlatformFile(file)) |
| 112 NOTREACHED(); | 112 NOTREACHED(); |
| 113 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); | 113 bound_net_log.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN); |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Closes a file with CloseFile() and signals the completion. | 116 // Closes a file with CloseFile() and signals the completion. |
| 117 void CloseFileAndSignal(base::PlatformFile* file, | 117 void CloseFileAndSignal(base::PlatformFile* file, |
| 118 base::WaitableEvent* on_io_complete, | 118 base::WaitableEvent* on_io_complete, |
| 119 const net::BoundNetLog& bound_net_log) { | 119 const net::BoundNetLog& bound_net_log) { |
| 120 CloseFile(*file, bound_net_log); | 120 CloseFile(*file, bound_net_log); |
| 121 *file = base::kInvalidPlatformFileValue; | 121 *file = base::kInvalidPlatformFileValue; |
| 122 on_io_complete->Signal(); | 122 on_io_complete->Signal(); |
| 123 } | 123 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 // FileStreamPosix ------------------------------------------------------------ | 250 // FileStreamPosix ------------------------------------------------------------ |
| 251 | 251 |
| 252 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) | 252 FileStreamPosix::FileStreamPosix(net::NetLog* net_log) |
| 253 : file_(base::kInvalidPlatformFileValue), | 253 : file_(base::kInvalidPlatformFileValue), |
| 254 open_flags_(0), | 254 open_flags_(0), |
| 255 auto_closed_(true), | 255 auto_closed_(true), |
| 256 record_uma_(false), | 256 record_uma_(false), |
| 257 bound_net_log_(net::BoundNetLog::Make(net_log, | 257 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 258 net::NetLog::SOURCE_FILESTREAM)), | 258 net::NetLog::SOURCE_FILESTREAM)), |
| 259 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 259 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 260 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | 260 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
| 261 } | 261 } |
| 262 | 262 |
| 263 FileStreamPosix::FileStreamPosix( | 263 FileStreamPosix::FileStreamPosix( |
| 264 base::PlatformFile file, int flags, net::NetLog* net_log) | 264 base::PlatformFile file, int flags, net::NetLog* net_log) |
| 265 : file_(file), | 265 : file_(file), |
| 266 open_flags_(flags), | 266 open_flags_(flags), |
| 267 auto_closed_(false), | 267 auto_closed_(false), |
| 268 record_uma_(false), | 268 record_uma_(false), |
| 269 bound_net_log_(net::BoundNetLog::Make(net_log, | 269 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 270 net::NetLog::SOURCE_FILESTREAM)), | 270 net::NetLog::SOURCE_FILESTREAM)), |
| 271 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 271 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 272 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | 272 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
| 273 } | 273 } |
| 274 | 274 |
| 275 FileStreamPosix::~FileStreamPosix() { | 275 FileStreamPosix::~FileStreamPosix() { |
| 276 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 276 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 277 // Block until the last open/close/read/write operation is complete. | 277 // Block until the last open/close/read/write operation is complete. |
| 278 // TODO(satorux): Ideally we should not block. crbug.com/115067 | 278 // TODO(satorux): Ideally we should not block. crbug.com/115067 |
| 279 WaitForIOCompletion(); | 279 WaitForIOCompletion(); |
| 280 } | 280 } |
| 281 | 281 |
| 282 if (auto_closed_) { | 282 if (auto_closed_) { |
| 283 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 283 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 284 // Close the file in the background. | 284 // Close the file in the background. |
| 285 if (IsOpen()) { | 285 if (IsOpen()) { |
| 286 const bool posted = base::WorkerPool::PostTask( | 286 const bool posted = base::WorkerPool::PostTask( |
| 287 FROM_HERE, | 287 FROM_HERE, |
| 288 base::Bind(&CloseFile, file_, bound_net_log_), | 288 base::Bind(&CloseFile, file_, bound_net_log_), |
| 289 true /* task_is_slow */); | 289 true /* task_is_slow */); |
| 290 DCHECK(posted); | 290 DCHECK(posted); |
| 291 } | 291 } |
| 292 } else { | 292 } else { |
| 293 CloseSync(); | 293 CloseSync(); |
| 294 } | 294 } |
| 295 } | 295 } |
| 296 | 296 |
| 297 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | 297 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE); |
| 298 } | 298 } |
| 299 | 299 |
| 300 void FileStreamPosix::Close(const CompletionCallback& callback) { | 300 void FileStreamPosix::Close(const CompletionCallback& callback) { |
| 301 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); | 301 DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); |
| 302 | 302 |
| 303 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 303 DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |
| 304 DCHECK(!on_io_complete_.get()); | 304 DCHECK(!on_io_complete_.get()); |
| 305 on_io_complete_.reset(new base::WaitableEvent( | 305 on_io_complete_.reset(new base::WaitableEvent( |
| 306 false /* manual_reset */, false /* initially_signaled */)); | 306 false /* manual_reset */, false /* initially_signaled */)); |
| 307 | 307 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 void FileStreamPosix::WaitForIOCompletion() { | 639 void FileStreamPosix::WaitForIOCompletion() { |
| 640 // http://crbug.com/115067 | 640 // http://crbug.com/115067 |
| 641 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 641 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 642 if (on_io_complete_.get()) { | 642 if (on_io_complete_.get()) { |
| 643 on_io_complete_->Wait(); | 643 on_io_complete_->Wait(); |
| 644 on_io_complete_.reset(); | 644 on_io_complete_.reset(); |
| 645 } | 645 } |
| 646 } | 646 } |
| 647 | 647 |
| 648 } // namespace net | 648 } // namespace net |
| OLD | NEW |