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