diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-24 01:56:59 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-24 01:56:59 +0000 |
commit | 84e0309f5a255e2795dc3369d7624248ef8d82ca (patch) | |
tree | 5e929fb29f62adca33320cdab1583ea49822dcb7 /net/base | |
parent | 3dc9517eac9ff2f9e08dab91a2e1646345b75d6e (diff) | |
download | chromium_src-84e0309f5a255e2795dc3369d7624248ef8d82ca.zip chromium_src-84e0309f5a255e2795dc3369d7624248ef8d82ca.tar.gz chromium_src-84e0309f5a255e2795dc3369d7624248ef8d82ca.tar.bz2 |
net: Split file_stream.h to file_stream_posix.h and win.h
POSIX and Windows have different implementations (i.e.
private member variables and functions). Having separate
header files is cleaner than managing the differences
with #ifdefs
BUG=none
TEST=everything builds as before
Review URL: http://codereview.chromium.org/9431014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123401 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/file_stream.cc | 93 | ||||
-rw-r--r-- | net/base/file_stream.h | 52 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 63 | ||||
-rw-r--r-- | net/base/file_stream_posix.h | 80 | ||||
-rw-r--r-- | net/base/file_stream_unittest.cc | 6 | ||||
-rw-r--r-- | net/base/file_stream_whence.h | 21 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 61 | ||||
-rw-r--r-- | net/base/file_stream_win.h | 80 |
8 files changed, 354 insertions, 102 deletions
diff --git a/net/base/file_stream.cc b/net/base/file_stream.cc new file mode 100644 index 0000000..818fb2e --- /dev/null +++ b/net/base/file_stream.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/file_stream.h" + +namespace net { + +FileStream::FileStream(net::NetLog* net_log) + : impl_(net_log) { +} + +FileStream::FileStream( + base::PlatformFile file, int flags, net::NetLog* net_log) + : impl_(file, flags, net_log) { +} + +FileStream::~FileStream() { +} + +void FileStream::Close(const CompletionCallback& callback) { + impl_.Close(callback); +} + +void FileStream::CloseSync() { + impl_.CloseSync(); +} + +int FileStream::Open(const FilePath& path, int open_flags, + const CompletionCallback& callback) { + return impl_.Open(path, open_flags, callback); +} + +int FileStream::OpenSync(const FilePath& path, int open_flags) { + return impl_.OpenSync(path, open_flags); +} + +bool FileStream::IsOpen() const { + return impl_.IsOpen(); +} + +int64 FileStream::Seek(Whence whence, int64 offset) { + return impl_.Seek(whence, offset); +} + +int64 FileStream::Available() { + return impl_.Available(); +} + +int FileStream::Read( + IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { + return impl_.Read(in_buf, buf_len, callback); +} + +int FileStream::ReadSync(char* buf, int buf_len) { + return impl_.ReadSync(buf, buf_len); +} + +int FileStream::ReadUntilComplete(char *buf, int buf_len) { + return impl_.ReadUntilComplete(buf, buf_len); +} + +int FileStream::Write( + IOBuffer* buf, int buf_len, const CompletionCallback& callback) { + return impl_.Write(buf, buf_len, callback); +} + +int FileStream::WriteSync(const char* buf, int buf_len) { + return impl_.WriteSync(buf, buf_len); +} + +int64 FileStream::Truncate(int64 bytes) { + return impl_.Truncate(bytes); +} + +int FileStream::Flush() { + return impl_.Flush(); +} + +void FileStream::EnableErrorStatistics() { + impl_.EnableErrorStatistics(); +} + +void FileStream::SetBoundNetLogSource( + const net::BoundNetLog& owner_bound_net_log) { + impl_.SetBoundNetLogSource(owner_bound_net_log); +} + +base::PlatformFile FileStream::GetPlatformFileForTesting() { + return impl_.GetPlatformFileForTesting(); +} + +} // namespace net diff --git a/net/base/file_stream.h b/net/base/file_stream.h index fa44fd1..a4380ec 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -11,13 +11,16 @@ #define NET_BASE_FILE_STREAM_H_ #pragma once -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" #include "base/platform_file.h" -#include "base/synchronization/waitable_event.h" #include "net/base/completion_callback.h" +#include "net/base/file_stream_whence.h" #include "net/base/net_export.h" #include "net/base/net_log.h" +#if defined(OS_WIN) +#include "net/base/file_stream_win.h" +#elif defined(OS_POSIX) +#include "net/base/file_stream_posix.h" +#endif class FilePath; @@ -25,14 +28,6 @@ namespace net { class IOBuffer; -// TODO(darin): Move this to a more generic location. -// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. -enum Whence { - FROM_BEGIN = 0, - FROM_CURRENT = 1, - FROM_END = 2 -}; - class NET_EXPORT FileStream { public: // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|) @@ -210,39 +205,16 @@ class NET_EXPORT FileStream { // of ownership happened, but without details. void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); - private: - friend class FileStreamTest; - - // Called when the file_ is opened asynchronously. |file| contains the - // platform file opened. |result| contains the result as a network error - // code. - void OnOpened(base::PlatformFile *file, int* result); - - // Called when the file_ is closed asynchronously. - void OnClosed(); + // Returns the underlying platform file for testing. + base::PlatformFile GetPlatformFileForTesting(); + private: #if defined(OS_WIN) - class AsyncContext; - friend class AsyncContext; - // This member is used to support asynchronous reads. It is non-null when - // the FileStream was opened with PLATFORM_FILE_ASYNC. - scoped_ptr<AsyncContext> async_context_; -#else - // Called when Read() or Write() is completed (used only for POSIX). - // |result| contains the result as a network error code. - void OnIOComplete(int* result); - - scoped_ptr<base::WaitableEvent> on_io_complete_; + FileStreamWin impl_; +#elif defined(OS_POSIX) + FileStreamPosix impl_; #endif - base::PlatformFile file_; - int open_flags_; - bool auto_closed_; - bool record_uma_; - net::BoundNetLog bound_net_log_; - base::WeakPtrFactory<FileStream> weak_ptr_factory_; - CompletionCallback callback_; - DISALLOW_COPY_AND_ASSIGN(FileStream); }; diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index fc81494..ae8780b 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -124,7 +124,7 @@ void ReadFile(base::PlatformFile file, *result = res; } -// Helper function used for FileStream::Read(). +// Helper function used for FileStreamPosix::Read(). void ReadFileFromIOBuffer(base::PlatformFile file, scoped_refptr<IOBuffer> buf, int buf_len, @@ -155,7 +155,7 @@ void WriteFile(base::PlatformFile file, *result = res; } -// Helper function used for FileStream::Write(). +// Helper function used for FileStreamPosix::Write(). void WriteFileToIOBuffer(base::PlatformFile file, scoped_refptr<IOBuffer> buf, int buf_len, @@ -184,9 +184,9 @@ int FlushFile(base::PlatformFile file, } // namespace -// FileStream ------------------------------------------------------------ +// FileStreamPosix ------------------------------------------------------------ -FileStream::FileStream(net::NetLog* net_log) +FileStreamPosix::FileStreamPosix(net::NetLog* net_log) : file_(base::kInvalidPlatformFileValue), open_flags_(0), auto_closed_(true), @@ -197,7 +197,8 @@ FileStream::FileStream(net::NetLog* net_log) bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); } -FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) +FileStreamPosix::FileStreamPosix( + base::PlatformFile file, int flags, net::NetLog* net_log) : file_(file), open_flags_(flags), auto_closed_(false), @@ -208,7 +209,7 @@ FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); } -FileStream::~FileStream() { +FileStreamPosix::~FileStreamPosix() { if (auto_closed_) { if (open_flags_ & base::PLATFORM_FILE_ASYNC) { // Block until the last read/write operation is complete, if needed. @@ -229,7 +230,7 @@ FileStream::~FileStream() { bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); } -void FileStream::Close(const CompletionCallback& callback) { +void FileStreamPosix::Close(const CompletionCallback& callback) { DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); DCHECK(callback_.is_null()); @@ -237,12 +238,12 @@ void FileStream::Close(const CompletionCallback& callback) { const bool posted = base::WorkerPool::PostTaskAndReply( FROM_HERE, base::Bind(&CloseFile, file_, bound_net_log_), - base::Bind(&FileStream::OnClosed, weak_ptr_factory_.GetWeakPtr()), + base::Bind(&FileStreamPosix::OnClosed, weak_ptr_factory_.GetWeakPtr()), true /* task_is_slow */); DCHECK(posted); } -void FileStream::CloseSync() { +void FileStreamPosix::CloseSync() { // Abort any existing asynchronous operations. // TODO(satorux): Replace this with a DCHECK once once all async clients @@ -255,7 +256,7 @@ void FileStream::CloseSync() { file_ = base::kInvalidPlatformFileValue; } -int FileStream::Open(const FilePath& path, int open_flags, +int FileStreamPosix::Open(const FilePath& path, int open_flags, const CompletionCallback& callback) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; @@ -275,7 +276,7 @@ int FileStream::Open(const FilePath& path, int open_flags, FROM_HERE, base::Bind(&OpenFile, path, open_flags, record_uma_, file, result, bound_net_log_), - base::Bind(&FileStream::OnOpened, + base::Bind(&FileStreamPosix::OnOpened, weak_ptr_factory_.GetWeakPtr(), base::Owned(file), base::Owned(result)), @@ -284,7 +285,7 @@ int FileStream::Open(const FilePath& path, int open_flags, return ERR_IO_PENDING; } -int FileStream::OpenSync(const FilePath& path, int open_flags) { +int FileStreamPosix::OpenSync(const FilePath& path, int open_flags) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; return ERR_UNEXPECTED; @@ -301,11 +302,11 @@ int FileStream::OpenSync(const FilePath& path, int open_flags) { return result; } -bool FileStream::IsOpen() const { +bool FileStreamPosix::IsOpen() const { return file_ != base::kInvalidPlatformFileValue; } -int64 FileStream::Seek(Whence whence, int64 offset) { +int64 FileStreamPosix::Seek(Whence whence, int64 offset) { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -327,7 +328,7 @@ int64 FileStream::Seek(Whence whence, int64 offset) { return res; } -int64 FileStream::Available() { +int64 FileStreamPosix::Available() { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -351,7 +352,7 @@ int64 FileStream::Available() { return size - cur_pos; } -int FileStream::Read( +int FileStreamPosix::Read( IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -373,7 +374,7 @@ int FileStream::Read( FROM_HERE, base::Bind(&ReadFileFromIOBuffer, file_, buf, buf_len, record_uma_, result, on_io_complete_.get(), bound_net_log_), - base::Bind(&FileStream::OnIOComplete, + base::Bind(&FileStreamPosix::OnIOComplete, weak_ptr_factory_.GetWeakPtr(), base::Owned(result)), true /* task is slow */); @@ -381,7 +382,7 @@ int FileStream::Read( return ERR_IO_PENDING; } -int FileStream::ReadSync(char* buf, int buf_len) { +int FileStreamPosix::ReadSync(char* buf, int buf_len) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -395,7 +396,7 @@ int FileStream::ReadSync(char* buf, int buf_len) { return result; } -int FileStream::ReadUntilComplete(char *buf, int buf_len) { +int FileStreamPosix::ReadUntilComplete(char *buf, int buf_len) { int to_read = buf_len; int bytes_total = 0; @@ -416,7 +417,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { return bytes_total; } -int FileStream::Write( +int FileStreamPosix::Write( IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -437,7 +438,7 @@ int FileStream::Write( FROM_HERE, base::Bind(&WriteFileToIOBuffer, file_, buf, buf_len, record_uma_, result, on_io_complete_.get(), bound_net_log_), - base::Bind(&FileStream::OnIOComplete, + base::Bind(&FileStreamPosix::OnIOComplete, weak_ptr_factory_.GetWeakPtr(), base::Owned(result)), true /* task is slow */); @@ -445,7 +446,7 @@ int FileStream::Write( return ERR_IO_PENDING; } -int FileStream::WriteSync( +int FileStreamPosix::WriteSync( const char* buf, int buf_len) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -459,7 +460,7 @@ int FileStream::WriteSync( return result; } -int64 FileStream::Truncate(int64 bytes) { +int64 FileStreamPosix::Truncate(int64 bytes) { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -484,18 +485,18 @@ int64 FileStream::Truncate(int64 bytes) { bound_net_log_); } -int FileStream::Flush() { +int FileStreamPosix::Flush() { if (!IsOpen()) return ERR_UNEXPECTED; return FlushFile(file_, record_uma_, bound_net_log_); } -void FileStream::EnableErrorStatistics() { +void FileStreamPosix::EnableErrorStatistics() { record_uma_ = true; } -void FileStream::SetBoundNetLogSource( +void FileStreamPosix::SetBoundNetLogSource( const net::BoundNetLog& owner_bound_net_log) { if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { @@ -519,7 +520,11 @@ void FileStream::SetBoundNetLogSource( bound_net_log_.source()))); } -void FileStream::OnClosed() { +base::PlatformFile FileStreamPosix::GetPlatformFileForTesting() { + return file_; +} + +void FileStreamPosix::OnClosed() { file_ = base::kInvalidPlatformFileValue; CompletionCallback temp = callback_; @@ -527,7 +532,7 @@ void FileStream::OnClosed() { temp.Run(OK); } -void FileStream::OnOpened(base::PlatformFile* file, int* result) { +void FileStreamPosix::OnOpened(base::PlatformFile* file, int* result) { file_ = *file; CompletionCallback temp = callback_; @@ -535,7 +540,7 @@ void FileStream::OnOpened(base::PlatformFile* file, int* result) { temp.Run(*result); } -void FileStream::OnIOComplete(int* result) { +void FileStreamPosix::OnIOComplete(int* result) { CompletionCallback temp_callback = callback_; callback_.Reset(); diff --git a/net/base/file_stream_posix.h b/net/base/file_stream_posix.h new file mode 100644 index 0000000..674bd23 --- /dev/null +++ b/net/base/file_stream_posix.h @@ -0,0 +1,80 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file implements FileStream for POSIX. + +#ifndef NET_BASE_FILE_STREAM_POSIX_H_ +#define NET_BASE_FILE_STREAM_POSIX_H_ +#pragma once + +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "base/synchronization/waitable_event.h" +#include "base/platform_file.h" +#include "net/base/completion_callback.h" +#include "net/base/file_stream_whence.h" +#include "net/base/net_export.h" +#include "net/base/net_log.h" + +class FilePath; + +namespace net { + +class IOBuffer; + +class NET_EXPORT FileStreamPosix { + public: + explicit FileStreamPosix(net::NetLog* net_log); + FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log); + ~FileStreamPosix(); + + // FileStream implementations. + void Close(const CompletionCallback& callback); + void CloseSync(); + int Open(const FilePath& path, int open_flags, + const CompletionCallback& callback); + int OpenSync(const FilePath& path, int open_flags); + bool IsOpen() const; + int64 Seek(Whence whence, int64 offset); + int64 Available(); + int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); + int ReadSync(char* buf, int buf_len); + int ReadUntilComplete(char *buf, int buf_len); + int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); + int WriteSync(const char* buf, int buf_len); + int64 Truncate(int64 bytes); + int Flush(); + void EnableErrorStatistics(); + void SetBoundNetLogSource( + const net::BoundNetLog& owner_bound_net_log); + base::PlatformFile GetPlatformFileForTesting(); + + private: + // Called when the file_ is opened asynchronously. |file| contains the + // platform file opened. |result| contains the result as a network error + // code. + void OnOpened(base::PlatformFile *file, int* result); + + // Called when the file_ is closed asynchronously. + void OnClosed(); + + // Called when Read() or Write() is completed (used only for POSIX). + // |result| contains the result as a network error code. + void OnIOComplete(int* result); + + base::PlatformFile file_; + int open_flags_; + bool auto_closed_; + bool record_uma_; + net::BoundNetLog bound_net_log_; + base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; + CompletionCallback callback_; + scoped_ptr<base::WaitableEvent> on_io_complete_; + + DISALLOW_COPY_AND_ASSIGN(FileStreamPosix); +}; + +} // namespace net + +#endif // NET_BASE_FILE_STREAM_POSIX_H diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index 274d267..093659d 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -90,10 +90,6 @@ class FileStreamTest : public PlatformTest { const FilePath temp_file_path() const { return temp_file_path_; } - static base::PlatformFile GetFile(const FileStream& stream) { - return stream.file_; - } - private: FilePath temp_file_path_; }; @@ -108,7 +104,7 @@ TEST_F(FileStreamTest, BasicOpenClose) { base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); EXPECT_EQ(OK, rv); EXPECT_TRUE(stream.IsOpen()); - file = GetFile(stream); + file = stream.GetPlatformFileForTesting(); } EXPECT_NE(base::kInvalidPlatformFileValue, file); base::PlatformFileInfo info; diff --git a/net/base/file_stream_whence.h b/net/base/file_stream_whence.h new file mode 100644 index 0000000..e256d6f --- /dev/null +++ b/net/base/file_stream_whence.h @@ -0,0 +1,21 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_BASE_FILE_STREAM_WHENCE_H_ +#define NET_BASE_FILE_STREAM_WHENCE_H_ +#pragma once + +namespace net { + +// TODO(darin): Move this to a more generic location. +// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. +enum Whence { + FROM_BEGIN = 0, + FROM_CURRENT = 1, + FROM_END = 2 +}; + +} // namespace net + +#endif // NET_BASE_FILE_STREAM_WHENCE_H_ diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index 9051d95..732919e 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -100,9 +100,9 @@ void CloseFile(base::PlatformFile file, } // namespace -// FileStream::AsyncContext ---------------------------------------------- +// FileStreamWin::AsyncContext ---------------------------------------------- -class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { +class FileStreamWin::AsyncContext : public MessageLoopForIO::IOHandler { public: explicit AsyncContext(const net::BoundNetLog& bound_net_log) : context_(), is_closing_(false), @@ -137,7 +137,7 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { FileErrorSource error_source_; }; -FileStream::AsyncContext::~AsyncContext() { +FileStreamWin::AsyncContext::~AsyncContext() { is_closing_ = true; bool waited = false; base::TimeTicks start = base::TimeTicks::Now(); @@ -152,7 +152,7 @@ FileStream::AsyncContext::~AsyncContext() { } } -void FileStream::AsyncContext::IOCompletionIsPending( +void FileStreamWin::AsyncContext::IOCompletionIsPending( const CompletionCallback& callback, IOBuffer* buf) { DCHECK(callback_.is_null()); @@ -160,7 +160,7 @@ void FileStream::AsyncContext::IOCompletionIsPending( in_flight_buf_ = buf; // Hold until the async operation ends. } -void FileStream::AsyncContext::OnIOCompleted( +void FileStreamWin::AsyncContext::OnIOCompleted( MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { DCHECK_EQ(&context_, context); DCHECK(!callback_.is_null()); @@ -189,7 +189,7 @@ void FileStream::AsyncContext::OnIOCompleted( // FileStream ------------------------------------------------------------ -FileStream::FileStream(net::NetLog* net_log) +FileStreamWin::FileStreamWin(net::NetLog* net_log) : file_(base::kInvalidPlatformFileValue), open_flags_(0), auto_closed_(true), @@ -200,7 +200,8 @@ FileStream::FileStream(net::NetLog* net_log) bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); } -FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) +FileStreamWin::FileStreamWin( + base::PlatformFile file, int flags, net::NetLog* net_log) : file_(file), open_flags_(flags), auto_closed_(false), @@ -219,7 +220,7 @@ FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) } } -FileStream::~FileStream() { +FileStreamWin::~FileStreamWin() { if (auto_closed_) { if (async_context_.get()) { // Block until the last read/write operation is complete, if needed. @@ -241,7 +242,7 @@ FileStream::~FileStream() { bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); } -void FileStream::Close(const CompletionCallback& callback) { +void FileStreamWin::Close(const CompletionCallback& callback) { DCHECK(callback_.is_null()); callback_ = callback; @@ -253,12 +254,12 @@ void FileStream::Close(const CompletionCallback& callback) { const bool posted = base::WorkerPool::PostTaskAndReply( FROM_HERE, base::Bind(&CloseFile, file_, bound_net_log_), - base::Bind(&FileStream::OnClosed, weak_ptr_factory_.GetWeakPtr()), + base::Bind(&FileStreamWin::OnClosed, weak_ptr_factory_.GetWeakPtr()), true /* task_is_slow */); DCHECK(posted); } -void FileStream::CloseSync() { +void FileStreamWin::CloseSync() { // The logic here is similar to CloseFile() but async_context_.reset() is // caled in this function. @@ -279,7 +280,7 @@ void FileStream::CloseSync() { } } -int FileStream::Open(const FilePath& path, int open_flags, +int FileStreamWin::Open(const FilePath& path, int open_flags, const CompletionCallback& callback) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; @@ -299,7 +300,7 @@ int FileStream::Open(const FilePath& path, int open_flags, FROM_HERE, base::Bind(&OpenFile, path, open_flags, record_uma_, bound_net_log_, file, result), - base::Bind(&FileStream::OnOpened, + base::Bind(&FileStreamWin::OnOpened, weak_ptr_factory_.GetWeakPtr(), base::Owned(file), base::Owned(result)), @@ -308,7 +309,7 @@ int FileStream::Open(const FilePath& path, int open_flags, return ERR_IO_PENDING; } -int FileStream::OpenSync(const FilePath& path, int open_flags) { +int FileStreamWin::OpenSync(const FilePath& path, int open_flags) { if (IsOpen()) { DLOG(FATAL) << "File is already open!"; return ERR_UNEXPECTED; @@ -334,11 +335,11 @@ int FileStream::OpenSync(const FilePath& path, int open_flags) { return OK; } -bool FileStream::IsOpen() const { +bool FileStreamWin::IsOpen() const { return file_ != base::kInvalidPlatformFileValue; } -int64 FileStream::Seek(Whence whence, int64 offset) { +int64 FileStreamWin::Seek(Whence whence, int64 offset) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -362,7 +363,7 @@ int64 FileStream::Seek(Whence whence, int64 offset) { return result.QuadPart; } -int64 FileStream::Available() { +int64 FileStreamWin::Available() { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -385,7 +386,7 @@ int64 FileStream::Available() { return file_size.QuadPart - cur_pos; } -int FileStream::Read( +int FileStreamWin::Read( IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(async_context_.get()); @@ -426,7 +427,7 @@ int FileStream::Read( return rv; } -int FileStream::ReadSync(char* buf, int buf_len) { +int FileStreamWin::ReadSync(char* buf, int buf_len) { DCHECK(!async_context_.get()); base::ThreadRestrictions::AssertIOAllowed(); @@ -455,7 +456,7 @@ int FileStream::ReadSync(char* buf, int buf_len) { return rv; } -int FileStream::ReadUntilComplete(char *buf, int buf_len) { +int FileStreamWin::ReadUntilComplete(char *buf, int buf_len) { int to_read = buf_len; int bytes_total = 0; @@ -476,7 +477,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { return bytes_total; } -int FileStream::Write( +int FileStreamWin::Write( IOBuffer* buf, int buf_len, const CompletionCallback& callback) { DCHECK(async_context_.get()); @@ -514,7 +515,7 @@ int FileStream::Write( return rv; } -int FileStream::WriteSync( +int FileStreamWin::WriteSync( const char* buf, int buf_len) { DCHECK(!async_context_.get()); base::ThreadRestrictions::AssertIOAllowed(); @@ -539,7 +540,7 @@ int FileStream::WriteSync( return rv; } -int FileStream::Flush() { +int FileStreamWin::Flush() { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -556,7 +557,7 @@ int FileStream::Flush() { bound_net_log_); } -int64 FileStream::Truncate(int64 bytes) { +int64 FileStreamWin::Truncate(int64 bytes) { base::ThreadRestrictions::AssertIOAllowed(); if (!IsOpen()) @@ -585,14 +586,14 @@ int64 FileStream::Truncate(int64 bytes) { return seek_position; } -void FileStream::EnableErrorStatistics() { +void FileStreamWin::EnableErrorStatistics() { record_uma_ = true; if (async_context_.get()) async_context_->EnableErrorStatistics(); } -void FileStream::SetBoundNetLogSource( +void FileStreamWin::SetBoundNetLogSource( const net::BoundNetLog& owner_bound_net_log) { if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { @@ -616,7 +617,11 @@ void FileStream::SetBoundNetLogSource( bound_net_log_.source()))); } -void FileStream::OnClosed() { +base::PlatformFile FileStreamWin::GetPlatformFileForTesting() { + return file_; +} + +void FileStreamWin::OnClosed() { file_ = base::kInvalidPlatformFileValue; CompletionCallback temp = callback_; @@ -624,7 +629,7 @@ void FileStream::OnClosed() { temp.Run(OK); } -void FileStream::OnOpened(base::PlatformFile* file, int* result) { +void FileStreamWin::OnOpened(base::PlatformFile* file, int* result) { file_ = *file; if (*result == OK) { diff --git a/net/base/file_stream_win.h b/net/base/file_stream_win.h new file mode 100644 index 0000000..cc6f565 --- /dev/null +++ b/net/base/file_stream_win.h @@ -0,0 +1,80 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This file implements FileStream for Windows. + +#ifndef NET_BASE_FILE_STREAM_WIN_H_ +#define NET_BASE_FILE_STREAM_WIN_H_ +#pragma once + +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "base/platform_file.h" +#include "net/base/completion_callback.h" +#include "net/base/file_stream_whence.h" +#include "net/base/net_export.h" +#include "net/base/net_log.h" + +class FilePath; + +namespace net { + +class IOBuffer; + +class NET_EXPORT FileStreamWin { + public: + explicit FileStreamWin(net::NetLog* net_log); + FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); + ~FileStreamWin(); + + // FileStream implementations. + void Close(const CompletionCallback& callback); + void CloseSync(); + int Open(const FilePath& path, int open_flags, + const CompletionCallback& callback); + int OpenSync(const FilePath& path, int open_flags); + bool IsOpen() const; + int64 Seek(Whence whence, int64 offset); + int64 Available(); + int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); + int ReadSync(char* buf, int buf_len); + int ReadUntilComplete(char *buf, int buf_len); + int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); + int WriteSync(const char* buf, int buf_len); + int64 Truncate(int64 bytes); + int Flush(); + void EnableErrorStatistics(); + void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); + base::PlatformFile GetPlatformFileForTesting(); + + private: + class AsyncContext; + friend class AsyncContext; + + // Called when the file_ is opened asynchronously. |file| contains the + // platform file opened. |result| contains the result as a network error + // code. + void OnOpened(base::PlatformFile *file, int* result); + + // Called when the file_ is closed asynchronously. + void OnClosed(); + + // This member is used to support asynchronous reads. It is non-null when + // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. + scoped_ptr<AsyncContext> async_context_; + + base::PlatformFile file_; + int open_flags_; + bool auto_closed_; + bool record_uma_; + net::BoundNetLog bound_net_log_; + base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; + CompletionCallback callback_; + + DISALLOW_COPY_AND_ASSIGN(FileStreamWin); +}; + +} // namespace net + +#endif // NET_BASE_FILE_STREAM_WIN_H_ |