| 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 net::Error net_error = MapSystemError(error); |
| 45 |
| 46 bound_net_log.AddEvent( |
| 47 net::NetLog::TYPE_FILE_STREAM_ERROR, |
| 48 make_scoped_refptr( |
| 49 new FileStreamErrorParameters(GetFileErrorSourceName(source), |
| 50 error, |
| 51 net_error))); |
| 52 |
| 40 RecordFileError(error, source, record_uma); | 53 RecordFileError(error, source, record_uma); |
| 41 return MapSystemError(error); | 54 |
| 55 return net_error; |
| 42 } | 56 } |
| 43 | 57 |
| 44 } // namespace | 58 } // namespace |
| 45 | 59 |
| 46 // FileStream::AsyncContext ---------------------------------------------- | 60 // FileStream::AsyncContext ---------------------------------------------- |
| 47 | 61 |
| 48 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 62 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| 49 public: | 63 public: |
| 50 AsyncContext(FileStream* owner) | 64 explicit AsyncContext(const net::BoundNetLog& bound_net_log) |
| 51 : owner_(owner), context_(), is_closing_(false), | 65 : context_(), is_closing_(false), |
| 52 record_uma_(false), error_source_(FILE_ERROR_SOURCE_COUNT) { | 66 record_uma_(false), bound_net_log_(bound_net_log), |
| 67 error_source_(FILE_ERROR_SOURCE_COUNT) { |
| 53 context_.handler = this; | 68 context_.handler = this; |
| 54 } | 69 } |
| 55 ~AsyncContext(); | 70 ~AsyncContext(); |
| 56 | 71 |
| 57 void IOCompletionIsPending(const CompletionCallback& callback); | 72 void IOCompletionIsPending(const CompletionCallback& callback); |
| 58 | 73 |
| 59 OVERLAPPED* overlapped() { return &context_.overlapped; } | 74 OVERLAPPED* overlapped() { return &context_.overlapped; } |
| 60 const CompletionCallback& callback() const { return callback_; } | 75 const CompletionCallback& callback() const { return callback_; } |
| 61 | 76 |
| 62 void set_error_source(FileErrorSource source) { error_source_ = source; } | 77 void set_error_source(FileErrorSource source) { error_source_ = source; } |
| 63 | 78 |
| 64 void EnableErrorStatistics() { | 79 void EnableErrorStatistics() { |
| 65 record_uma_ = true; | 80 record_uma_ = true; |
| 66 } | 81 } |
| 67 | 82 |
| 68 private: | 83 private: |
| 69 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 84 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 70 DWORD bytes_read, DWORD error); | 85 DWORD bytes_read, DWORD error) OVERRIDE; |
| 71 | 86 |
| 72 FileStream* owner_; | |
| 73 MessageLoopForIO::IOContext context_; | 87 MessageLoopForIO::IOContext context_; |
| 74 CompletionCallback callback_; | 88 CompletionCallback callback_; |
| 75 bool is_closing_; | 89 bool is_closing_; |
| 76 bool record_uma_; | 90 bool record_uma_; |
| 91 const net::BoundNetLog bound_net_log_; |
| 77 FileErrorSource error_source_; | 92 FileErrorSource error_source_; |
| 78 }; | 93 }; |
| 79 | 94 |
| 80 FileStream::AsyncContext::~AsyncContext() { | 95 FileStream::AsyncContext::~AsyncContext() { |
| 81 is_closing_ = true; | 96 is_closing_ = true; |
| 82 bool waited = false; | 97 bool waited = false; |
| 83 base::TimeTicks start = base::TimeTicks::Now(); | 98 base::TimeTicks start = base::TimeTicks::Now(); |
| 84 while (!callback_.is_null()) { | 99 while (!callback_.is_null()) { |
| 85 waited = true; | 100 waited = true; |
| 86 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); | 101 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 102 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { | 117 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { |
| 103 DCHECK_EQ(&context_, context); | 118 DCHECK_EQ(&context_, context); |
| 104 DCHECK(!callback_.is_null()); | 119 DCHECK(!callback_.is_null()); |
| 105 | 120 |
| 106 if (is_closing_) { | 121 if (is_closing_) { |
| 107 callback_.Reset(); | 122 callback_.Reset(); |
| 108 return; | 123 return; |
| 109 } | 124 } |
| 110 | 125 |
| 111 int result = static_cast<int>(bytes_read); | 126 int result = static_cast<int>(bytes_read); |
| 112 if (error && error != ERROR_HANDLE_EOF) | 127 if (error && error != ERROR_HANDLE_EOF) { |
| 113 result = RecordAndMapError(error, error_source_, record_uma_); | 128 result = RecordAndMapError(error, error_source_, record_uma_, |
| 129 bound_net_log_); |
| 130 } |
| 114 | 131 |
| 115 if (bytes_read) | 132 if (bytes_read) |
| 116 IncrementOffset(&context->overlapped, bytes_read); | 133 IncrementOffset(&context->overlapped, bytes_read); |
| 117 | 134 |
| 118 CompletionCallback temp; | 135 CompletionCallback temp; |
| 119 std::swap(temp, callback_); | 136 std::swap(temp, callback_); |
| 120 temp.Run(result); | 137 temp.Run(result); |
| 121 } | 138 } |
| 122 | 139 |
| 123 // FileStream ------------------------------------------------------------ | 140 // FileStream ------------------------------------------------------------ |
| 124 | 141 |
| 125 FileStream::FileStream() | 142 FileStream::FileStream(net::NetLog* net_log) |
| 126 : file_(INVALID_HANDLE_VALUE), | 143 : file_(base::kInvalidPlatformFileValue), |
| 127 open_flags_(0), | 144 open_flags_(0), |
| 128 auto_closed_(true), | 145 auto_closed_(true), |
| 129 record_uma_(false) { | 146 record_uma_(false), |
| 147 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 148 net::NetLog::SOURCE_FILESTREAM)) { |
| 149 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 130 } | 150 } |
| 131 | 151 |
| 132 FileStream::FileStream(base::PlatformFile file, int flags) | 152 FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) |
| 133 : file_(file), | 153 : file_(file), |
| 134 open_flags_(flags), | 154 open_flags_(flags), |
| 135 auto_closed_(false), | 155 auto_closed_(false), |
| 136 record_uma_(false) { | 156 record_uma_(false), |
| 157 bound_net_log_(net::BoundNetLog::Make(net_log, |
| 158 net::NetLog::SOURCE_FILESTREAM)) { |
| 159 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 160 |
| 137 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 161 // 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. | 162 // make sure we will perform asynchronous File IO to it. |
| 139 if (flags & base::PLATFORM_FILE_ASYNC) { | 163 if (flags & base::PLATFORM_FILE_ASYNC) { |
| 140 async_context_.reset(new AsyncContext(this)); | 164 async_context_.reset(new AsyncContext(bound_net_log_)); |
| 141 MessageLoopForIO::current()->RegisterIOHandler(file_, | 165 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 142 async_context_.get()); | 166 async_context_.get()); |
| 143 } | 167 } |
| 144 } | 168 } |
| 145 | 169 |
| 146 FileStream::~FileStream() { | 170 FileStream::~FileStream() { |
| 147 if (auto_closed_) | 171 if (auto_closed_) |
| 148 Close(); | 172 Close(); |
| 173 |
| 174 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); |
| 149 } | 175 } |
| 150 | 176 |
| 151 void FileStream::Close() { | 177 void FileStream::Close() { |
| 178 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE, NULL); |
| 152 if (file_ != INVALID_HANDLE_VALUE) | 179 if (file_ != INVALID_HANDLE_VALUE) |
| 153 CancelIo(file_); | 180 CancelIo(file_); |
| 154 | 181 |
| 155 async_context_.reset(); | 182 async_context_.reset(); |
| 156 if (file_ != INVALID_HANDLE_VALUE) { | 183 if (file_ != INVALID_HANDLE_VALUE) { |
| 157 CloseHandle(file_); | 184 CloseHandle(file_); |
| 158 file_ = INVALID_HANDLE_VALUE; | 185 file_ = INVALID_HANDLE_VALUE; |
| 186 |
| 187 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); |
| 159 } | 188 } |
| 160 } | 189 } |
| 161 | 190 |
| 162 int FileStream::Open(const FilePath& path, int open_flags) { | 191 int FileStream::Open(const FilePath& path, int open_flags) { |
| 163 if (IsOpen()) { | 192 if (IsOpen()) { |
| 164 DLOG(FATAL) << "File is already open!"; | 193 DLOG(FATAL) << "File is already open!"; |
| 165 return ERR_UNEXPECTED; | 194 return ERR_UNEXPECTED; |
| 166 } | 195 } |
| 167 | 196 |
| 197 bound_net_log_.BeginEvent( |
| 198 net::NetLog::TYPE_FILE_STREAM_OPEN, |
| 199 make_scoped_refptr( |
| 200 new net::NetLogStringParameter("file_name", |
| 201 path.AsUTF8Unsafe()))); |
| 202 |
| 168 open_flags_ = open_flags; | 203 open_flags_ = open_flags; |
| 169 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); | 204 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); |
| 170 if (file_ == INVALID_HANDLE_VALUE) { | 205 if (file_ == INVALID_HANDLE_VALUE) { |
| 171 DWORD error = GetLastError(); | 206 DWORD error = GetLastError(); |
| 172 LOG(WARNING) << "Failed to open file: " << error; | 207 LOG(WARNING) << "Failed to open file: " << error; |
| 173 return RecordAndMapError(error, FILE_ERROR_SOURCE_OPEN, record_uma_); | 208 int net_error = RecordAndMapError(error, |
| 209 FILE_ERROR_SOURCE_OPEN, |
| 210 record_uma_, |
| 211 bound_net_log_); |
| 212 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); |
| 213 return net_error; |
| 174 } | 214 } |
| 175 | 215 |
| 176 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 216 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| 177 async_context_.reset(new AsyncContext(this)); | 217 async_context_.reset(new AsyncContext(bound_net_log_)); |
| 178 if (record_uma_) | 218 if (record_uma_) |
| 179 async_context_->EnableErrorStatistics(); | 219 async_context_->EnableErrorStatistics(); |
| 180 MessageLoopForIO::current()->RegisterIOHandler(file_, | 220 MessageLoopForIO::current()->RegisterIOHandler(file_, |
| 181 async_context_.get()); | 221 async_context_.get()); |
| 182 } | 222 } |
| 183 | 223 |
| 184 return OK; | 224 return OK; |
| 185 } | 225 } |
| 186 | 226 |
| 187 bool FileStream::IsOpen() const { | 227 bool FileStream::IsOpen() const { |
| 188 return file_ != INVALID_HANDLE_VALUE; | 228 return file_ != INVALID_HANDLE_VALUE; |
| 189 } | 229 } |
| 190 | 230 |
| 191 int64 FileStream::Seek(Whence whence, int64 offset) { | 231 int64 FileStream::Seek(Whence whence, int64 offset) { |
| 192 if (!IsOpen()) | 232 if (!IsOpen()) |
| 193 return ERR_UNEXPECTED; | 233 return ERR_UNEXPECTED; |
| 194 | 234 |
| 195 DCHECK(!async_context_.get() || async_context_->callback().is_null()); | 235 DCHECK(!async_context_.get() || async_context_->callback().is_null()); |
| 196 | 236 |
| 197 LARGE_INTEGER distance, result; | 237 LARGE_INTEGER distance, result; |
| 198 distance.QuadPart = offset; | 238 distance.QuadPart = offset; |
| 199 DWORD move_method = static_cast<DWORD>(whence); | 239 DWORD move_method = static_cast<DWORD>(whence); |
| 200 if (!SetFilePointerEx(file_, distance, &result, move_method)) { | 240 if (!SetFilePointerEx(file_, distance, &result, move_method)) { |
| 201 DWORD error = GetLastError(); | 241 DWORD error = GetLastError(); |
| 202 LOG(WARNING) << "SetFilePointerEx failed: " << error; | 242 LOG(WARNING) << "SetFilePointerEx failed: " << error; |
| 203 return RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK, record_uma_); | 243 return RecordAndMapError(error, |
| 244 FILE_ERROR_SOURCE_SEEK, |
| 245 record_uma_, |
| 246 bound_net_log_); |
| 204 } | 247 } |
| 205 if (async_context_.get()) { | 248 if (async_context_.get()) { |
| 206 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); | 249 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); |
| 207 SetOffset(async_context_->overlapped(), result); | 250 SetOffset(async_context_->overlapped(), result); |
| 208 } | 251 } |
| 209 return result.QuadPart; | 252 return result.QuadPart; |
| 210 } | 253 } |
| 211 | 254 |
| 212 int64 FileStream::Available() { | 255 int64 FileStream::Available() { |
| 213 base::ThreadRestrictions::AssertIOAllowed(); | 256 base::ThreadRestrictions::AssertIOAllowed(); |
| 214 | 257 |
| 215 if (!IsOpen()) | 258 if (!IsOpen()) |
| 216 return ERR_UNEXPECTED; | 259 return ERR_UNEXPECTED; |
| 217 | 260 |
| 218 int64 cur_pos = Seek(FROM_CURRENT, 0); | 261 int64 cur_pos = Seek(FROM_CURRENT, 0); |
| 219 if (cur_pos < 0) | 262 if (cur_pos < 0) |
| 220 return cur_pos; | 263 return cur_pos; |
| 221 | 264 |
| 222 LARGE_INTEGER file_size; | 265 LARGE_INTEGER file_size; |
| 223 if (!GetFileSizeEx(file_, &file_size)) { | 266 if (!GetFileSizeEx(file_, &file_size)) { |
| 224 DWORD error = GetLastError(); | 267 DWORD error = GetLastError(); |
| 225 LOG(WARNING) << "GetFileSizeEx failed: " << error; | 268 LOG(WARNING) << "GetFileSizeEx failed: " << error; |
| 226 return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE, record_uma_); | 269 return RecordAndMapError(error, |
| 270 FILE_ERROR_SOURCE_GET_SIZE, |
| 271 record_uma_, |
| 272 bound_net_log_); |
| 227 } | 273 } |
| 228 | 274 |
| 229 return file_size.QuadPart - cur_pos; | 275 return file_size.QuadPart - cur_pos; |
| 230 } | 276 } |
| 231 | 277 |
| 232 int FileStream::Read( | 278 int FileStream::Read( |
| 233 char* buf, int buf_len, const CompletionCallback& callback) { | 279 char* buf, int buf_len, const CompletionCallback& callback) { |
| 234 if (!IsOpen()) | 280 if (!IsOpen()) |
| 235 return ERR_UNEXPECTED; | 281 return ERR_UNEXPECTED; |
| 236 | 282 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 252 DWORD bytes_read; | 298 DWORD bytes_read; |
| 253 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { | 299 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { |
| 254 DWORD error = GetLastError(); | 300 DWORD error = GetLastError(); |
| 255 if (async_context_.get() && error == ERROR_IO_PENDING) { | 301 if (async_context_.get() && error == ERROR_IO_PENDING) { |
| 256 async_context_->IOCompletionIsPending(callback); | 302 async_context_->IOCompletionIsPending(callback); |
| 257 rv = ERR_IO_PENDING; | 303 rv = ERR_IO_PENDING; |
| 258 } else if (error == ERROR_HANDLE_EOF) { | 304 } else if (error == ERROR_HANDLE_EOF) { |
| 259 rv = 0; // Report EOF by returning 0 bytes read. | 305 rv = 0; // Report EOF by returning 0 bytes read. |
| 260 } else { | 306 } else { |
| 261 LOG(WARNING) << "ReadFile failed: " << error; | 307 LOG(WARNING) << "ReadFile failed: " << error; |
| 262 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ, record_uma_); | 308 rv = RecordAndMapError(error, |
| 309 FILE_ERROR_SOURCE_READ, |
| 310 record_uma_, |
| 311 bound_net_log_); |
| 263 } | 312 } |
| 264 } else if (overlapped) { | 313 } else if (overlapped) { |
| 265 async_context_->IOCompletionIsPending(callback); | 314 async_context_->IOCompletionIsPending(callback); |
| 266 rv = ERR_IO_PENDING; | 315 rv = ERR_IO_PENDING; |
| 267 } else { | 316 } else { |
| 268 rv = static_cast<int>(bytes_read); | 317 rv = static_cast<int>(bytes_read); |
| 269 } | 318 } |
| 270 return rv; | 319 return rv; |
| 271 } | 320 } |
| 272 | 321 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 bound_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 bound_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 writing. |
| 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 bound_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( |
| 438 const net::BoundNetLog& owner_bound_net_log) { |
| 439 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && |
| 440 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { |
| 441 // Both |BoundNetLog|s are invalid. |
| 442 return; |
| 443 } |
| 444 |
| 445 // Should never connect to itself. |
| 446 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); |
| 447 |
| 448 bound_net_log_.AddEvent( |
| 449 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, |
| 450 make_scoped_refptr( |
| 451 new net::NetLogSourceParameter("source_dependency", |
| 452 owner_bound_net_log.source()))); |
| 453 |
| 454 owner_bound_net_log.AddEvent( |
| 455 net::NetLog::TYPE_FILE_STREAM_SOURCE, |
| 456 make_scoped_refptr( |
| 457 new net::NetLogSourceParameter("source_dependency", |
| 458 bound_net_log_.source()))); |
| 459 } |
| 460 |
| 381 } // namespace net | 461 } // namespace net |
| OLD | NEW |