| Index: net/base/file_stream_win.cc
|
| diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc
|
| index c08521e913a47c1ee3adcf9b77ea969365d28f54..cab0e7cb13d1c7a6f65af4098fa2a5d6734f3ed0 100644
|
| --- a/net/base/file_stream_win.cc
|
| +++ b/net/base/file_stream_win.cc
|
| @@ -52,8 +52,9 @@ static int MapErrorCode(DWORD err) {
|
|
|
| class FileStream::AsyncContext : public MessageLoopForIO::IOHandler {
|
| public:
|
| - AsyncContext(FileStream* owner)
|
| - : owner_(owner), context_(), callback_(NULL), is_closing_(false) {
|
| + AsyncContext(FileStream* owner, int class_flags)
|
| + : owner_(owner), context_(), callback_(NULL), is_closing_(false),
|
| + class_flags_(class_flags) {
|
| context_.handler = this;
|
| }
|
| ~AsyncContext();
|
| @@ -63,6 +64,13 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler {
|
| OVERLAPPED* overlapped() { return &context_.overlapped; }
|
| CompletionCallback* callback() const { return callback_; }
|
|
|
| + void EnableRecording(bool enable, int class_flags) {
|
| + if (enable)
|
| + class_flags_ |= class_flags;
|
| + else
|
| + class_flags_ &= ~class_flags;
|
| + }
|
| +
|
| private:
|
| virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
|
| DWORD bytes_read, DWORD error);
|
| @@ -71,6 +79,7 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler {
|
| MessageLoopForIO::IOContext context_;
|
| CompletionCallback* callback_;
|
| bool is_closing_;
|
| + int class_flags_;
|
| };
|
|
|
| FileStream::AsyncContext::~AsyncContext() {
|
| @@ -105,8 +114,10 @@ void FileStream::AsyncContext::OnIOCompleted(
|
| }
|
|
|
| int result = static_cast<int>(bytes_read);
|
| - if (error && error != ERROR_HANDLE_EOF)
|
| + if (error && error != ERROR_HANDLE_EOF) {
|
| + RecordFileError(error, FILE_ERROR_TYPES_READ, class_flags_);
|
| result = MapErrorCode(error);
|
| + }
|
|
|
| if (bytes_read)
|
| IncrementOffset(&context->overlapped, bytes_read);
|
| @@ -121,17 +132,40 @@ void FileStream::AsyncContext::OnIOCompleted(
|
| FileStream::FileStream()
|
| : file_(INVALID_HANDLE_VALUE),
|
| open_flags_(0),
|
| - auto_closed_(true) {
|
| + auto_closed_(true),
|
| + class_flags_(0) {
|
| +}
|
| +
|
| +FileStream::FileStream(int class_flags)
|
| + : file_(INVALID_HANDLE_VALUE),
|
| + open_flags_(0),
|
| + auto_closed_(true),
|
| + class_flags_(class_flags) {
|
| }
|
|
|
| FileStream::FileStream(base::PlatformFile file, int flags)
|
| : file_(file),
|
| open_flags_(flags),
|
| - auto_closed_(false) {
|
| + auto_closed_(false),
|
| + class_flags_(0) {
|
| // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to
|
| // make sure we will perform asynchronous File IO to it.
|
| if (flags & base::PLATFORM_FILE_ASYNC) {
|
| - async_context_.reset(new AsyncContext(this));
|
| + async_context_.reset(new AsyncContext(this, class_flags_));
|
| + MessageLoopForIO::current()->RegisterIOHandler(file_,
|
| + async_context_.get());
|
| + }
|
| +}
|
| +
|
| +FileStream::FileStream(base::PlatformFile file, int flags, int class_flags)
|
| + : file_(file),
|
| + open_flags_(flags),
|
| + auto_closed_(false),
|
| + class_flags_(class_flags) {
|
| + // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to
|
| + // make sure we will perform asynchronous File IO to it.
|
| + if (flags & base::PLATFORM_FILE_ASYNC) {
|
| + async_context_.reset(new AsyncContext(this, class_flags_));
|
| MessageLoopForIO::current()->RegisterIOHandler(file_,
|
| async_context_.get());
|
| }
|
| @@ -164,11 +198,12 @@ int FileStream::Open(const FilePath& path, int open_flags) {
|
| if (file_ == INVALID_HANDLE_VALUE) {
|
| DWORD error = GetLastError();
|
| LOG(WARNING) << "Failed to open file: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_OPEN, class_flags_);
|
| return MapErrorCode(error);
|
| }
|
|
|
| if (open_flags_ & base::PLATFORM_FILE_ASYNC) {
|
| - async_context_.reset(new AsyncContext(this));
|
| + async_context_.reset(new AsyncContext(this, class_flags_));
|
| MessageLoopForIO::current()->RegisterIOHandler(file_,
|
| async_context_.get());
|
| }
|
| @@ -181,8 +216,12 @@ bool FileStream::IsOpen() const {
|
| }
|
|
|
| int64 FileStream::Seek(Whence whence, int64 offset) {
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
| +
|
| DCHECK(!async_context_.get() || !async_context_->callback());
|
|
|
| LARGE_INTEGER distance, result;
|
| @@ -191,6 +230,7 @@ int64 FileStream::Seek(Whence whence, int64 offset) {
|
| if (!SetFilePointerEx(file_, distance, &result, move_method)) {
|
| DWORD error = GetLastError();
|
| LOG(WARNING) << "SetFilePointerEx failed: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_SEEK, class_flags_);
|
| return MapErrorCode(error);
|
| }
|
| if (async_context_.get())
|
| @@ -201,8 +241,11 @@ int64 FileStream::Seek(Whence whence, int64 offset) {
|
| int64 FileStream::Available() {
|
| base::ThreadRestrictions::AssertIOAllowed();
|
|
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
|
|
| int64 cur_pos = Seek(FROM_CURRENT, 0);
|
| if (cur_pos < 0)
|
| @@ -212,6 +255,7 @@ int64 FileStream::Available() {
|
| if (!GetFileSizeEx(file_, &file_size)) {
|
| DWORD error = GetLastError();
|
| LOG(WARNING) << "GetFileSizeEx failed: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_GET_SIZE, class_flags_);
|
| return MapErrorCode(error);
|
| }
|
|
|
| @@ -220,8 +264,12 @@ int64 FileStream::Available() {
|
|
|
| int FileStream::Read(
|
| char* buf, int buf_len, CompletionCallback* callback) {
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
| +
|
| DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
|
|
|
| OVERLAPPED* overlapped = NULL;
|
| @@ -246,6 +294,7 @@ int FileStream::Read(
|
| rv = 0; // Report EOF by returning 0 bytes read.
|
| } else {
|
| LOG(WARNING) << "ReadFile failed: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_READ, class_flags_);
|
| rv = MapErrorCode(error);
|
| }
|
| } else if (overlapped) {
|
| @@ -280,8 +329,11 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) {
|
|
|
| int FileStream::Write(
|
| const char* buf, int buf_len, CompletionCallback* callback) {
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
|
|
|
| OVERLAPPED* overlapped = NULL;
|
| @@ -303,6 +355,7 @@ int FileStream::Write(
|
| rv = ERR_IO_PENDING;
|
| } else {
|
| LOG(WARNING) << "WriteFile failed: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_WRITE, class_flags_);
|
| rv = MapErrorCode(error);
|
| }
|
| } else if (overlapped) {
|
| @@ -317,8 +370,11 @@ int FileStream::Write(
|
| int FileStream::Flush() {
|
| base::ThreadRestrictions::AssertIOAllowed();
|
|
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
|
|
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
|
| if (FlushFileBuffers(file_)) {
|
| @@ -327,6 +383,7 @@ int FileStream::Flush() {
|
|
|
| int rv;
|
| DWORD error = GetLastError();
|
| + RecordFileError(error, FILE_ERROR_TYPES_FLUSH, class_flags_);
|
| rv = MapErrorCode(error);
|
| return rv;
|
| }
|
| @@ -334,8 +391,11 @@ int FileStream::Flush() {
|
| int64 FileStream::Truncate(int64 bytes) {
|
| base::ThreadRestrictions::AssertIOAllowed();
|
|
|
| - if (!IsOpen())
|
| + if (!IsOpen()) {
|
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_TYPES_IS_NOT_OPEN,
|
| + class_flags_);
|
| return ERR_UNEXPECTED;
|
| + }
|
|
|
| // We better be open for reading.
|
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
|
| @@ -350,6 +410,7 @@ int64 FileStream::Truncate(int64 bytes) {
|
| if (!result) {
|
| DWORD error = GetLastError();
|
| LOG(WARNING) << "SetEndOfFile failed: " << error;
|
| + RecordFileError(error, FILE_ERROR_TYPES_SET_EOF, class_flags_);
|
| return MapErrorCode(error);
|
| }
|
|
|
| @@ -357,4 +418,14 @@ int64 FileStream::Truncate(int64 bytes) {
|
| return seek_position;
|
| }
|
|
|
| +void FileStream::EnableRecording(bool enable, int class_flags) {
|
| + if (enable)
|
| + class_flags_ |= class_flags;
|
| + else
|
| + class_flags_ &= ~class_flags;
|
| +
|
| + if (async_context_.get())
|
| + async_context_->EnableRecording(enable, class_flags);
|
| +}
|
| +
|
| } // namespace net
|
|
|