| OLD | NEW |
| 1 // Copyright (c) 2011 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/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "net/base/file_stream_metrics.h" | 14 #include "net/base/file_stream_metrics.h" |
| 15 #include "net/base/file_stream_net_log_parameters.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 // Ensure that we can just use our Whence values directly. | 20 // Ensure that we can just use our Whence values directly. |
| 20 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); | 21 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); |
| 21 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); | 22 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); |
| 22 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); | 23 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); |
| 23 | 24 |
| 24 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { | 25 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { |
| 25 overlapped->Offset = offset.LowPart; | 26 overlapped->Offset = offset.LowPart; |
| 26 overlapped->OffsetHigh = offset.HighPart; | 27 overlapped->OffsetHigh = offset.HighPart; |
| 27 } | 28 } |
| 28 | 29 |
| 29 static void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { | 30 static void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { |
| 30 LARGE_INTEGER offset; | 31 LARGE_INTEGER offset; |
| 31 offset.LowPart = overlapped->Offset; | 32 offset.LowPart = overlapped->Offset; |
| 32 offset.HighPart = overlapped->OffsetHigh; | 33 offset.HighPart = overlapped->OffsetHigh; |
| 33 offset.QuadPart += static_cast<LONGLONG>(count); | 34 offset.QuadPart += static_cast<LONGLONG>(count); |
| 34 SetOffset(overlapped, offset); | 35 SetOffset(overlapped, offset); |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 int RecordAndMapError(int error, FileErrorSource source, bool record_uma) { | 40 int RecordAndMapError(int error, |
| 41 FileErrorSource source, |
| 42 bool record_uma, |
| 43 const net::BoundNetLog& bound_net_log) { |
| 44 bound_net_log.AddEvent( |
| 45 net::NetLog::TYPE_FILE_STREAM_ERROR, |
| 46 make_scoped_refptr( |
| 47 new FileStreamErrorParameters( |
| 48 GetFileErrorSourceName(source), |
| 49 error, |
| 50 MapSystemError(error)))); |
| 51 |
| 40 RecordFileError(error, source, record_uma); | 52 RecordFileError(error, source, record_uma); |
| 41 return MapSystemError(error); | 53 return MapSystemError(error); |
| 42 } | 54 } |
| 43 | 55 |
| 44 } // namespace | 56 } // namespace |
| 45 | 57 |
| 46 // FileStream::AsyncContext ---------------------------------------------- | 58 // FileStream::AsyncContext ---------------------------------------------- |
| 47 | 59 |
| 48 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 60 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| 49 public: | 61 public: |
| 50 AsyncContext(FileStream* owner) | 62 AsyncContext(FileStream* owner) |
| 51 : owner_(owner), context_(), is_closing_(false), | 63 : owner_(owner), context_(), is_closing_(false), |
| 52 record_uma_(false), error_source_(FILE_ERROR_SOURCE_COUNT) { | 64 record_uma_(false), |
| 65 error_source_(FILE_ERROR_SOURCE_COUNT) { |
| 53 context_.handler = this; | 66 context_.handler = this; |
| 54 } | 67 } |
| 55 ~AsyncContext(); | 68 ~AsyncContext(); |
| 56 | 69 |
| 57 void IOCompletionIsPending(const CompletionCallback& callback); | 70 void IOCompletionIsPending(const CompletionCallback& callback); |
| 58 | 71 |
| 59 OVERLAPPED* overlapped() { return &context_.overlapped; } | 72 OVERLAPPED* overlapped() { return &context_.overlapped; } |
| 60 const CompletionCallback& callback() const { return callback_; } | 73 const CompletionCallback& callback() const { return callback_; } |
| 61 | 74 |
| 62 void set_error_source(FileErrorSource source) { error_source_ = source; } | 75 void set_error_source(FileErrorSource source) { error_source_ = source; } |
| 76 void set_bound_net_log(const net::BoundNetLog& bound_net_log) { |
| 77 bound_net_log_ = bound_net_log; |
| 78 } |
| 63 | 79 |
| 64 void EnableErrorStatistics() { | 80 void EnableErrorStatistics() { |
| 65 record_uma_ = true; | 81 record_uma_ = true; |
| 66 } | 82 } |
| 67 | 83 |
| 68 private: | 84 private: |
| 69 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 85 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 70 DWORD bytes_read, DWORD error); | 86 DWORD bytes_read, DWORD error); |
| 71 | 87 |
| 72 FileStream* owner_; | 88 FileStream* owner_; |
| 73 MessageLoopForIO::IOContext context_; | 89 MessageLoopForIO::IOContext context_; |
| 74 CompletionCallback callback_; | 90 CompletionCallback callback_; |
| 75 bool is_closing_; | 91 bool is_closing_; |
| 76 bool record_uma_; | 92 bool record_uma_; |
| 93 net::BoundNetLog bound_net_log_; |
| 77 FileErrorSource error_source_; | 94 FileErrorSource error_source_; |
| 78 }; | 95 }; |
| 79 | 96 |
| 80 FileStream::AsyncContext::~AsyncContext() { | 97 FileStream::AsyncContext::~AsyncContext() { |
| 81 is_closing_ = true; | 98 is_closing_ = true; |
| 82 bool waited = false; | 99 bool waited = false; |
| 83 base::TimeTicks start = base::TimeTicks::Now(); | 100 base::TimeTicks start = base::TimeTicks::Now(); |
| 84 while (!callback_.is_null()) { | 101 while (!callback_.is_null()) { |
| 85 waited = true; | 102 waited = true; |
| 86 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); | 103 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 102 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { | 119 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { |
| 103 DCHECK_EQ(&context_, context); | 120 DCHECK_EQ(&context_, context); |
| 104 DCHECK(!callback_.is_null()); | 121 DCHECK(!callback_.is_null()); |
| 105 | 122 |
| 106 if (is_closing_) { | 123 if (is_closing_) { |
| 107 callback_.Reset(); | 124 callback_.Reset(); |
| 108 return; | 125 return; |
| 109 } | 126 } |
| 110 | 127 |
| 111 int result = static_cast<int>(bytes_read); | 128 int result = static_cast<int>(bytes_read); |
| 112 if (error && error != ERROR_HANDLE_EOF) | 129 if (error && error != ERROR_HANDLE_EOF) { |
| 113 result = RecordAndMapError(error, error_source_, record_uma_); | 130 result = RecordAndMapError(error, error_source_, record_uma_, |
| 131 bound_net_log_); |
| 132 } |
| 114 | 133 |
| 115 if (bytes_read) | 134 if (bytes_read) |
| 116 IncrementOffset(&context->overlapped, bytes_read); | 135 IncrementOffset(&context->overlapped, bytes_read); |
| 117 | 136 |
| 118 CompletionCallback temp; | 137 CompletionCallback temp; |
| 119 std::swap(temp, callback_); | 138 std::swap(temp, callback_); |
| 120 temp.Run(result); | 139 temp.Run(result); |
| 121 } | 140 } |
| 122 | 141 |
| 123 // FileStream ------------------------------------------------------------ | 142 // FileStream ------------------------------------------------------------ |
| 124 | 143 |
| 125 FileStream::FileStream() | 144 FileStream::FileStream(net::NetLog* net_log) |
| 126 : file_(INVALID_HANDLE_VALUE), | 145 : file_(base::kInvalidPlatformFileValue), |
| 127 open_flags_(0), | 146 open_flags_(0), |
| 128 auto_closed_(true), | 147 auto_closed_(true), |
| 129 record_uma_(false) { | 148 record_uma_(false), |
| 149 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 150 net::NetLog::SOURCE_FILESTREAM)) { |
| 151 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 130 } | 152 } |
| 131 | 153 |
| 132 FileStream::FileStream(base::PlatformFile file, int flags) | 154 FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) |
| 133 : file_(file), | 155 : file_(file), |
| 134 open_flags_(flags), | 156 open_flags_(flags), |
| 135 auto_closed_(false), | 157 auto_closed_(false), |
| 136 record_uma_(false) { | 158 record_uma_(false), |
| 159 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 160 net::NetLog::SOURCE_FILESTREAM)) { |
| 161 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 162 |
| 137 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 163 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to |
| 138 // make sure we will perform asynchronous File IO to it. | 164 // make sure we will perform asynchronous File IO to it. |
| 139 if (flags & base::PLATFORM_FILE_ASYNC) { | 165 if (flags & base::PLATFORM_FILE_ASYNC) { |
| 140 async_context_.reset(new AsyncContext(this)); | 166 async_context_.reset(new AsyncContext(this)); |
| 141 MessageLoopForIO::current()->RegisterIOHandler(file_, | 167 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 142 async_context_.get()); | 168 async_context_.get()); |
| 143 } | 169 } |
| 144 } | 170 } |
| 145 | 171 |
| 146 FileStream::~FileStream() { | 172 FileStream::~FileStream() { |
| 147 if (auto_closed_) | 173 if (auto_closed_) |
| 148 Close(); | 174 Close(); |
| 175 |
| 176 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 149 } | 177 } |
| 150 | 178 |
| 151 void FileStream::Close() { | 179 void FileStream::Close() { |
| 180 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE, NULL); |
| 152 if (file_ != INVALID_HANDLE_VALUE) | 181 if (file_ != INVALID_HANDLE_VALUE) |
| 153 CancelIo(file_); | 182 CancelIo(file_); |
| 154 | 183 |
| 155 async_context_.reset(); | 184 async_context_.reset(); |
| 156 if (file_ != INVALID_HANDLE_VALUE) { | 185 if (file_ != INVALID_HANDLE_VALUE) { |
| 157 CloseHandle(file_); | 186 CloseHandle(file_); |
| 158 file_ = INVALID_HANDLE_VALUE; | 187 file_ = INVALID_HANDLE_VALUE; |
| 188 |
| 189 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); |
| 159 } | 190 } |
| 160 } | 191 } |
| 161 | 192 |
| 162 int FileStream::Open(const FilePath& path, int open_flags) { | 193 int FileStream::Open(const FilePath& path, int open_flags) { |
| 163 if (IsOpen()) { | 194 if (IsOpen()) { |
| 164 DLOG(FATAL) << "File is already open!"; | 195 DLOG(FATAL) << "File is already open!"; |
| 165 return ERR_UNEXPECTED; | 196 return ERR_UNEXPECTED; |
| 166 } | 197 } |
| 167 | 198 |
| 199 bound_net_log_.BeginEvent( |
| 200 net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 201 make_scoped_refptr( |
| 202 new net::NetLogStringParameter("file_name", |
| 203 path.AsUTF8Unsafe()))); |
| 204 |
| 168 open_flags_ = open_flags; | 205 open_flags_ = open_flags; |
| 169 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); | 206 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); |
| 170 if (file_ == INVALID_HANDLE_VALUE) { | 207 if (file_ == INVALID_HANDLE_VALUE) { |
| 171 DWORD error = GetLastError(); | 208 DWORD error = GetLastError(); |
| 172 LOG(WARNING) << "Failed to open file: " << error; | 209 LOG(WARNING) << "Failed to open file: " << error; |
| 173 return RecordAndMapError(error, FILE_ERROR_SOURCE_OPEN, record_uma_); | 210 int net_error = RecordAndMapError(error, |
| 211 FILE_ERROR_SOURCE_OPEN, |
| 212 record_uma_, |
| 213 bound_net_log_); |
| 214 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); |
| 215 return net_error; |
| 174 } | 216 } |
| 175 | 217 |
| 176 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 218 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 177 async_context_.reset(new AsyncContext(this)); | 219 async_context_.reset(new AsyncContext(this)); |
| 178 if (record_uma_) | 220 if (record_uma_) |
| 179 async_context_->EnableErrorStatistics(); | 221 async_context_->EnableErrorStatistics(); |
| 180 MessageLoopForIO::current()->RegisterIOHandler(file_, | 222 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 181 async_context_.get()); | 223 async_context_.get()); |
| 182 } | 224 } |
| 183 | 225 |
| 184 return OK; | 226 return OK; |
| 185 } | 227 } |
| 186 | 228 |
| 187 bool FileStream::IsOpen() const { | 229 bool FileStream::IsOpen() const { |
| 188 return file_ != INVALID_HANDLE_VALUE; | 230 return file_ != INVALID_HANDLE_VALUE; |
| 189 } | 231 } |
| 190 | 232 |
| 191 int64 FileStream::Seek(Whence whence, int64 offset) { | 233 int64 FileStream::Seek(Whence whence, int64 offset) { |
| 192 if (!IsOpen()) | 234 if (!IsOpen()) |
| 193 return ERR_UNEXPECTED; | 235 return ERR_UNEXPECTED; |
| 194 | 236 |
| 195 DCHECK(!async_context_.get() || async_context_->callback().is_null()); | 237 DCHECK(!async_context_.get() || async_context_->callback().is_null()); |
| 196 | 238 |
| 197 LARGE_INTEGER distance, result; | 239 LARGE_INTEGER distance, result; |
| 198 distance.QuadPart = offset; | 240 distance.QuadPart = offset; |
| 199 DWORD move_method = static_cast<DWORD>(whence); | 241 DWORD move_method = static_cast<DWORD>(whence); |
| 200 if (!SetFilePointerEx(file_, distance, &result, move_method)) { | 242 if (!SetFilePointerEx(file_, distance, &result, move_method)) { |
| 201 DWORD error = GetLastError(); | 243 DWORD error = GetLastError(); |
| 202 LOG(WARNING) << "SetFilePointerEx failed: " << error; | 244 LOG(WARNING) << "SetFilePointerEx failed: " << error; |
| 203 return RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK, record_uma_); | 245 return RecordAndMapError(error, |
| 246 FILE_ERROR_SOURCE_SEEK, |
| 247 record_uma_, |
| 248 bound_net_log_); |
| 204 } | 249 } |
| 205 if (async_context_.get()) { | 250 if (async_context_.get()) { |
| 206 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); | 251 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); |
| 252 async_context_->set_bound_net_log(bound_net_log_); |
| 207 SetOffset(async_context_->overlapped(), result); | 253 SetOffset(async_context_->overlapped(), result); |
| 208 } | 254 } |
| 209 return result.QuadPart; | 255 return result.QuadPart; |
| 210 } | 256 } |
| 211 | 257 |
| 212 int64 FileStream::Available() { | 258 int64 FileStream::Available() { |
| 213 base::ThreadRestrictions::AssertIOAllowed(); | 259 base::ThreadRestrictions::AssertIOAllowed(); |
| 214 | 260 |
| 215 if (!IsOpen()) | 261 if (!IsOpen()) |
| 216 return ERR_UNEXPECTED; | 262 return ERR_UNEXPECTED; |
| 217 | 263 |
| 218 int64 cur_pos = Seek(FROM_CURRENT, 0); | 264 int64 cur_pos = Seek(FROM_CURRENT, 0); |
| 219 if (cur_pos < 0) | 265 if (cur_pos < 0) |
| 220 return cur_pos; | 266 return cur_pos; |
| 221 | 267 |
| 222 LARGE_INTEGER file_size; | 268 LARGE_INTEGER file_size; |
| 223 if (!GetFileSizeEx(file_, &file_size)) { | 269 if (!GetFileSizeEx(file_, &file_size)) { |
| 224 DWORD error = GetLastError(); | 270 DWORD error = GetLastError(); |
| 225 LOG(WARNING) << "GetFileSizeEx failed: " << error; | 271 LOG(WARNING) << "GetFileSizeEx failed: " << error; |
| 226 return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE, record_uma_); | 272 return RecordAndMapError(error, |
| 273 FILE_ERROR_SOURCE_GET_SIZE, |
| 274 record_uma_, |
| 275 bound_net_log_); |
| 227 } | 276 } |
| 228 | 277 |
| 229 return file_size.QuadPart - cur_pos; | 278 return file_size.QuadPart - cur_pos; |
| 230 } | 279 } |
| 231 | 280 |
| 232 int FileStream::Read( | 281 int FileStream::Read( |
| 233 char* buf, int buf_len, const CompletionCallback& callback) { | 282 char* buf, int buf_len, const CompletionCallback& callback) { |
| 234 if (!IsOpen()) | 283 if (!IsOpen()) |
| 235 return ERR_UNEXPECTED; | 284 return ERR_UNEXPECTED; |
| 236 | 285 |
| 237 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 286 DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| 238 | 287 |
| 239 OVERLAPPED* overlapped = NULL; | 288 OVERLAPPED* overlapped = NULL; |
| 240 if (async_context_.get()) { | 289 if (async_context_.get()) { |
| 241 DCHECK(!callback.is_null()); | 290 DCHECK(!callback.is_null()); |
| 242 DCHECK(async_context_->callback().is_null()); | 291 DCHECK(async_context_->callback().is_null()); |
| 243 overlapped = async_context_->overlapped(); | 292 overlapped = async_context_->overlapped(); |
| 244 async_context_->set_error_source(FILE_ERROR_SOURCE_READ); | 293 async_context_->set_error_source(FILE_ERROR_SOURCE_READ); |
| 294 async_context_->set_bound_net_log(bound_net_log_); |
| 245 } else { | 295 } else { |
| 246 DCHECK(callback.is_null()); | 296 DCHECK(callback.is_null()); |
| 247 base::ThreadRestrictions::AssertIOAllowed(); | 297 base::ThreadRestrictions::AssertIOAllowed(); |
| 248 } | 298 } |
| 249 | 299 |
| 250 int rv; | 300 int rv; |
| 251 | 301 |
| 252 DWORD bytes_read; | 302 DWORD bytes_read; |
| 253 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { | 303 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { |
| 254 DWORD error = GetLastError(); | 304 DWORD error = GetLastError(); |
| 255 if (async_context_.get() && error == ERROR_IO_PENDING) { | 305 if (async_context_.get() && error == ERROR_IO_PENDING) { |
| 256 async_context_->IOCompletionIsPending(callback); | 306 async_context_->IOCompletionIsPending(callback); |
| 257 rv = ERR_IO_PENDING; | 307 rv = ERR_IO_PENDING; |
| 258 } else if (error == ERROR_HANDLE_EOF) { | 308 } else if (error == ERROR_HANDLE_EOF) { |
| 259 rv = 0; // Report EOF by returning 0 bytes read. | 309 rv = 0; // Report EOF by returning 0 bytes read. |
| 260 } else { | 310 } else { |
| 261 LOG(WARNING) << "ReadFile failed: " << error; | 311 LOG(WARNING) << "ReadFile failed: " << error; |
| 262 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ, record_uma_); | 312 rv = RecordAndMapError(error, |
| 313 FILE_ERROR_SOURCE_READ, |
| 314 record_uma_, |
| 315 bound_net_log_); |
| 263 } | 316 } |
| 264 } else if (overlapped) { | 317 } else if (overlapped) { |
| 265 async_context_->IOCompletionIsPending(callback); | 318 async_context_->IOCompletionIsPending(callback); |
| 266 rv = ERR_IO_PENDING; | 319 rv = ERR_IO_PENDING; |
| 267 } else { | 320 } else { |
| 268 rv = static_cast<int>(bytes_read); | 321 rv = static_cast<int>(bytes_read); |
| 269 } | 322 } |
| 270 return rv; | 323 return rv; |
| 271 } | 324 } |
| 272 | 325 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 297 return ERR_UNEXPECTED; | 350 return ERR_UNEXPECTED; |
| 298 | 351 |
| 299 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 352 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 300 | 353 |
| 301 OVERLAPPED* overlapped = NULL; | 354 OVERLAPPED* overlapped = NULL; |
| 302 if (async_context_.get()) { | 355 if (async_context_.get()) { |
| 303 DCHECK(!callback.is_null()); | 356 DCHECK(!callback.is_null()); |
| 304 DCHECK(async_context_->callback().is_null()); | 357 DCHECK(async_context_->callback().is_null()); |
| 305 overlapped = async_context_->overlapped(); | 358 overlapped = async_context_->overlapped(); |
| 306 async_context_->set_error_source(FILE_ERROR_SOURCE_WRITE); | 359 async_context_->set_error_source(FILE_ERROR_SOURCE_WRITE); |
| 360 async_context_->set_bound_net_log(bound_net_log_); |
| 307 } else { | 361 } else { |
| 308 DCHECK(callback.is_null()); | 362 DCHECK(callback.is_null()); |
| 309 base::ThreadRestrictions::AssertIOAllowed(); | 363 base::ThreadRestrictions::AssertIOAllowed(); |
| 310 } | 364 } |
| 311 | 365 |
| 312 int rv; | 366 int rv; |
| 313 DWORD bytes_written; | 367 DWORD bytes_written; |
| 314 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { | 368 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { |
| 315 DWORD error = GetLastError(); | 369 DWORD error = GetLastError(); |
| 316 if (async_context_.get() && error == ERROR_IO_PENDING) { | 370 if (async_context_.get() && error == ERROR_IO_PENDING) { |
| 317 async_context_->IOCompletionIsPending(callback); | 371 async_context_->IOCompletionIsPending(callback); |
| 318 rv = ERR_IO_PENDING; | 372 rv = ERR_IO_PENDING; |
| 319 } else { | 373 } else { |
| 320 LOG(WARNING) << "WriteFile failed: " << error; | 374 LOG(WARNING) << "WriteFile failed: " << error; |
| 321 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE, record_uma_); | 375 rv = RecordAndMapError(error, |
| 376 FILE_ERROR_SOURCE_WRITE, |
| 377 record_uma_, |
| 378 bound_net_log_); |
| 322 } | 379 } |
| 323 } else if (overlapped) { | 380 } else if (overlapped) { |
| 324 async_context_->IOCompletionIsPending(callback); | 381 async_context_->IOCompletionIsPending(callback); |
| 325 rv = ERR_IO_PENDING; | 382 rv = ERR_IO_PENDING; |
| 326 } else { | 383 } else { |
| 327 rv = static_cast<int>(bytes_written); | 384 rv = static_cast<int>(bytes_written); |
| 328 } | 385 } |
| 329 return rv; | 386 return rv; |
| 330 } | 387 } |
| 331 | 388 |
| 332 int FileStream::Flush() { | 389 int FileStream::Flush() { |
| 333 base::ThreadRestrictions::AssertIOAllowed(); | 390 base::ThreadRestrictions::AssertIOAllowed(); |
| 334 | 391 |
| 335 if (!IsOpen()) | 392 if (!IsOpen()) |
| 336 return ERR_UNEXPECTED; | 393 return ERR_UNEXPECTED; |
| 337 | 394 |
| 338 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 395 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 339 if (FlushFileBuffers(file_)) { | 396 if (FlushFileBuffers(file_)) { |
| 340 return OK; | 397 return OK; |
| 341 } | 398 } |
| 342 | 399 |
| 343 return RecordAndMapError(GetLastError(), | 400 return RecordAndMapError(GetLastError(), |
| 344 FILE_ERROR_SOURCE_FLUSH, | 401 FILE_ERROR_SOURCE_FLUSH, |
| 345 record_uma_); | 402 record_uma_, |
| 403 bound_net_log_); |
| 346 } | 404 } |
| 347 | 405 |
| 348 int64 FileStream::Truncate(int64 bytes) { | 406 int64 FileStream::Truncate(int64 bytes) { |
| 349 base::ThreadRestrictions::AssertIOAllowed(); | 407 base::ThreadRestrictions::AssertIOAllowed(); |
| 350 | 408 |
| 351 if (!IsOpen()) | 409 if (!IsOpen()) |
| 352 return ERR_UNEXPECTED; | 410 return ERR_UNEXPECTED; |
| 353 | 411 |
| 354 // We better be open for reading. | 412 // We'd better be open for writing. |
| 355 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 413 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| 356 | 414 |
| 357 // Seek to the position to truncate from. | 415 // Seek to the position to truncate from. |
| 358 int64 seek_position = Seek(FROM_BEGIN, bytes); | 416 int64 seek_position = Seek(FROM_BEGIN, bytes); |
| 359 if (seek_position != bytes) | 417 if (seek_position != bytes) |
| 360 return ERR_UNEXPECTED; | 418 return ERR_UNEXPECTED; |
| 361 | 419 |
| 362 // And truncate the file. | 420 // And truncate the file. |
| 363 BOOL result = SetEndOfFile(file_); | 421 BOOL result = SetEndOfFile(file_); |
| 364 if (!result) { | 422 if (!result) { |
| 365 DWORD error = GetLastError(); | 423 DWORD error = GetLastError(); |
| 366 LOG(WARNING) << "SetEndOfFile failed: " << error; | 424 LOG(WARNING) << "SetEndOfFile failed: " << error; |
| 367 return RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF, record_uma_); | 425 return RecordAndMapError(error, |
| 426 FILE_ERROR_SOURCE_SET_EOF, |
| 427 record_uma_, |
| 428 bound_net_log_); |
| 368 } | 429 } |
| 369 | 430 |
| 370 // Success. | 431 // Success. |
| 371 return seek_position; | 432 return seek_position; |
| 372 } | 433 } |
| 373 | 434 |
| 374 void FileStream::EnableErrorStatistics() { | 435 void FileStream::EnableErrorStatistics() { |
| 375 record_uma_ = true; | 436 record_uma_ = true; |
| 376 | 437 |
| 377 if (async_context_.get()) | 438 if (async_context_.get()) |
| 378 async_context_->EnableErrorStatistics(); | 439 async_context_->EnableErrorStatistics(); |
| 379 } | 440 } |
| 380 | 441 |
| 442 void FileStream::SetBoundNetLogSource( |
| 443 const net::BoundNetLog& owner_bound_net_log) { |
| 444 if (owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) |
| 445 return; |
| 446 |
| 447 if (owner_bound_net_log.source().id == bound_net_log_.source().id) |
| 448 return; |
| 449 |
| 450 bound_net_log_.AddEvent( |
| 451 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, |
| 452 make_scoped_refptr( |
| 453 new net::NetLogSourceParameter("source_dependency", |
| 454 owner_bound_net_log.source()))); |
| 455 |
| 456 owner_bound_net_log.AddEvent( |
| 457 net::NetLog::TYPE_FILE_STREAM_SOURCE, |
| 458 make_scoped_refptr( |
| 459 new net::NetLogSourceParameter("source_dependency", |
| 460 bound_net_log_.source()))); |
| 461 } |
| 462 |
| 381 } // namespace net | 463 } // namespace net |
| OLD | NEW |