diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-09 17:28:56 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-09 17:28:56 +0000 |
commit | 50f91af3dddfefe8acc7ff92626a3bd979fe5ab5 (patch) | |
tree | 487f57bc7ca8e3d287f51bf435e2463d22fb544e | |
parent | 05d1265f5600e7b33bec08da28f45816b80c0784 (diff) | |
download | chromium_src-50f91af3dddfefe8acc7ff92626a3bd979fe5ab5.zip chromium_src-50f91af3dddfefe8acc7ff92626a3bd979fe5ab5.tar.gz chromium_src-50f91af3dddfefe8acc7ff92626a3bd979fe5ab5.tar.bz2 |
net: Remove unused FileStream methods
Remove synchronous FileStream methods (OpenSync, CloseSync, SeekSync, Available, ReadSync, ReadUntilComplete, WriteSync, Truncate, FlushSync).
Remove EnableErrrorStatistics() and SetBoundNetLogSource() whose only user was download::BaseFile.
Remove FileStream::Context::async_ because we are always async.
BUG=356979
TEST=git cl try
Review URL: https://codereview.chromium.org/225463003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262729 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/file_stream.cc | 168 | ||||
-rw-r--r-- | net/base/file_stream.h | 79 | ||||
-rw-r--r-- | net/base/file_stream_context.cc | 84 | ||||
-rw-r--r-- | net/base/file_stream_context.h | 36 | ||||
-rw-r--r-- | net/base/file_stream_context_posix.cc | 54 | ||||
-rw-r--r-- | net/base/file_stream_context_win.cc | 78 | ||||
-rw-r--r-- | net/base/file_stream_metrics.cc | 103 | ||||
-rw-r--r-- | net/base/file_stream_metrics.h | 44 | ||||
-rw-r--r-- | net/base/file_stream_metrics_posix.cc | 21 | ||||
-rw-r--r-- | net/base/file_stream_metrics_win.cc | 146 | ||||
-rw-r--r-- | net/base/file_stream_net_log_parameters.cc | 25 | ||||
-rw-r--r-- | net/base/file_stream_net_log_parameters.h | 27 | ||||
-rw-r--r-- | net/base/file_stream_unittest.cc | 351 | ||||
-rw-r--r-- | net/base/mock_file_stream.cc | 33 | ||||
-rw-r--r-- | net/base/mock_file_stream.h | 8 | ||||
-rw-r--r-- | net/net.gyp | 6 |
16 files changed, 103 insertions, 1160 deletions
diff --git a/net/base/file_stream.cc b/net/base/file_stream.cc index 2df7521..a0939eb 100644 --- a/net/base/file_stream.cc +++ b/net/base/file_stream.cc @@ -10,23 +10,18 @@ #include "base/threading/thread_restrictions.h" #include "base/threading/worker_pool.h" #include "net/base/file_stream_context.h" -#include "net/base/file_stream_net_log_parameters.h" #include "net/base/net_errors.h" namespace net { FileStream::FileStream(NetLog* net_log, const scoped_refptr<base::TaskRunner>& task_runner) - /* To allow never opened stream to be destroyed on any thread we set flags - as if stream was opened asynchronously. */ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)), context_(new Context(bound_net_log_, task_runner)) { bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE); } FileStream::FileStream(NetLog* net_log) - /* To allow never opened stream to be destroyed on any thread we set flags - as if stream was opened asynchronously. */ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)), context_(new Context(bound_net_log_, base::WorkerPool::GetTaskRunner(true /* slow */))) { @@ -38,14 +33,13 @@ FileStream::FileStream(base::PlatformFile file, NetLog* net_log, const scoped_refptr<base::TaskRunner>& task_runner) : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)), - context_(new Context(base::File(file), flags, bound_net_log_, - task_runner)) { + context_(new Context(base::File(file), bound_net_log_, task_runner)) { bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE); } FileStream::FileStream(base::PlatformFile file, int flags, NetLog* net_log) : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)), - context_(new Context(base::File(file), flags, bound_net_log_, + context_(new Context(base::File(file), bound_net_log_, base::WorkerPool::GetTaskRunner(true /* slow */))) { bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE); } @@ -66,13 +60,7 @@ FileStream::FileStream(base::File file, net::NetLog* net_log) } FileStream::~FileStream() { - if (context_->async()) { - context_.release()->Orphan(); - } else { - context_->CloseSync(); - context_.reset(); - } - + context_.release()->Orphan(); bound_net_log_.EndEvent(NetLog::TYPE_FILE_STREAM_ALIVE); } @@ -88,31 +76,11 @@ int FileStream::Open(const base::FilePath& path, int open_flags, return ERR_IO_PENDING; } -int FileStream::OpenSync(const base::FilePath& path, int open_flags) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (IsOpen()) { - DLOG(FATAL) << "File is already open!"; - return ERR_UNEXPECTED; - } - - DCHECK(!context_->async()); - return context_->OpenSync(path, open_flags); -} - int FileStream::Close(const CompletionCallback& callback) { - DCHECK(context_->async()); context_->CloseAsync(callback); return ERR_IO_PENDING; } -int FileStream::CloseSync() { - DCHECK(!context_->async()); - base::ThreadRestrictions::AssertIOAllowed(); - context_->CloseSync(); - return OK; -} - bool FileStream::IsOpen() const { return context_->file().IsValid(); } @@ -123,41 +91,10 @@ int FileStream::Seek(Whence whence, if (!IsOpen()) return ERR_UNEXPECTED; - // Make sure we're async. - DCHECK(context_->async()); context_->SeekAsync(whence, offset, callback); return ERR_IO_PENDING; } -int64 FileStream::SeekSync(Whence whence, int64 offset) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - // If we're in async, make sure we don't have a request in flight. - DCHECK(!context_->async() || !context_->async_in_progress()); - return context_->SeekSync(whence, offset); -} - -int64 FileStream::Available() { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - int64 cur_pos = SeekSync(FROM_CURRENT, 0); - if (cur_pos < 0) - return cur_pos; - - int64 size = context_->GetFileSize(); - if (size < 0) - return size; - - DCHECK_GE(size, cur_pos); - return size - cur_pos; -} - int FileStream::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { @@ -166,129 +103,30 @@ int FileStream::Read(IOBuffer* buf, // read(..., 0) will return 0, which indicates end-of-file. DCHECK_GT(buf_len, 0); - DCHECK(context_->async()); return context_->ReadAsync(buf, buf_len, callback); } -int FileStream::ReadSync(char* buf, int buf_len) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - DCHECK(!context_->async()); - // read(..., 0) will return 0, which indicates end-of-file. - DCHECK_GT(buf_len, 0); - - return context_->ReadSync(buf, buf_len); -} - -int FileStream::ReadUntilComplete(char *buf, int buf_len) { - base::ThreadRestrictions::AssertIOAllowed(); - - int to_read = buf_len; - int bytes_total = 0; - - do { - int bytes_read = ReadSync(buf, to_read); - if (bytes_read <= 0) { - if (bytes_total == 0) - return bytes_read; - - return bytes_total; - } - - bytes_total += bytes_read; - buf += bytes_read; - to_read -= bytes_read; - } while (bytes_total < buf_len); - - return bytes_total; -} - int FileStream::Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; - DCHECK(context_->async()); // write(..., 0) will return 0, which indicates end-of-file. DCHECK_GT(buf_len, 0); return context_->WriteAsync(buf, buf_len, callback); } -int FileStream::WriteSync(const char* buf, int buf_len) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - DCHECK(!context_->async()); - // write(..., 0) will return 0, which indicates end-of-file. - DCHECK_GT(buf_len, 0); - - return context_->WriteSync(buf, buf_len); -} - -int64 FileStream::Truncate(int64 bytes) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - // Seek to the position to truncate from. - int64 seek_position = SeekSync(FROM_BEGIN, bytes); - if (seek_position != bytes) - return ERR_UNEXPECTED; - - // And truncate the file. - return context_->Truncate(bytes); -} - int FileStream::Flush(const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; - // Make sure we're async. - DCHECK(context_->async()); - context_->FlushAsync(callback); return ERR_IO_PENDING; } -int FileStream::FlushSync() { - base::ThreadRestrictions::AssertIOAllowed(); - - if (!IsOpen()) - return ERR_UNEXPECTED; - - return context_->FlushSync(); -} - -void FileStream::EnableErrorStatistics() { - context_->set_record_uma(true); -} - -void FileStream::SetBoundNetLogSource(const BoundNetLog& owner_bound_net_log) { - if ((owner_bound_net_log.source().id == NetLog::Source::kInvalidId) && - (bound_net_log_.source().id == NetLog::Source::kInvalidId)) { - // Both |BoundNetLog|s are invalid. - return; - } - - // Should never connect to itself. - DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); - - bound_net_log_.AddEvent(NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, - owner_bound_net_log.source().ToEventParametersCallback()); - - owner_bound_net_log.AddEvent(NetLog::TYPE_FILE_STREAM_SOURCE, - bound_net_log_.source().ToEventParametersCallback()); -} - const base::File& FileStream::GetFileForTesting() const { return context_->file(); } diff --git a/net/base/file_stream.h b/net/base/file_stream.h index da62937..a903fec 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -82,25 +82,12 @@ class NET_EXPORT FileStream { virtual int Open(const base::FilePath& path, int open_flags, const CompletionCallback& callback); - // Call this method to open the FileStream synchronously. - // The remaining methods cannot be used unless this method returns OK. If - // the file cannot be opened then an error code is returned. open_flags is - // a bitfield of base::PlatformFileFlags - // - // If the file stream is not closed manually, the underlying file will be - // automatically closed when FileStream is destructed. - virtual int OpenSync(const base::FilePath& path, int open_flags); - // Returns ERR_IO_PENDING and closes the file asynchronously, calling // |callback| when done. // It is invalid to request any asynchronous operations while there is an // in-flight asynchronous operation. virtual int Close(const CompletionCallback& callback); - // Closes the file immediately and returns OK. If the file is open - // asynchronously, Close(const CompletionCallback&) should be used instead. - virtual int CloseSync(); - // Returns true if Open succeeded and Close has not been called. virtual bool IsOpen() const; @@ -113,16 +100,6 @@ class NET_EXPORT FileStream { virtual int Seek(Whence whence, int64 offset, const Int64CompletionCallback& callback); - // Adjust the position from where data is read synchronously. - // Upon success, the stream position relative to the start of the file is - // returned. Otherwise, an error code is returned. It is not valid to - // call SeekSync while a Read call has a pending completion. - virtual int64 SeekSync(Whence whence, int64 offset); - - // Returns the number of bytes available to read from the current stream - // position until the end of the file. Otherwise, an error code is returned. - virtual int64 Available(); - // Call this method to read data from the current stream position // asynchronously. Up to buf_len bytes will be copied into buf. (In // other words, partial reads are allowed.) Returns the number of bytes @@ -146,23 +123,6 @@ class NET_EXPORT FileStream { virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - // Call this method to read data from the current stream position - // synchronously. Up to buf_len bytes will be copied into buf. (In - // other words, partial reads are allowed.) Returns the number of bytes - // copied, 0 if at end-of-file, or an error code if the operation could - // not be performed. - // - // The file must not be opened with PLATFORM_FILE_ASYNC. - // This method must not be called if the stream was opened WRITE_ONLY. - virtual int ReadSync(char* buf, int buf_len); - - // Performs the same as ReadSync, but ensures that exactly buf_len bytes - // are copied into buf. A partial read may occur, but only as a result of - // end-of-file or fatal error. Returns the number of bytes copied into buf, - // 0 if at end-of-file and no bytes have been read into buf yet, - // or an error code if the operation could not be performed. - virtual int ReadUntilComplete(char *buf, int buf_len); - // Call this method to write data at the current stream position // asynchronously. Up to buf_len bytes will be written from buf. (In // other words, partial writes are allowed.) Returns the number of @@ -188,25 +148,6 @@ class NET_EXPORT FileStream { virtual int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - // Call this method to write data at the current stream position - // synchronously. Up to buf_len bytes will be written from buf. (In - // other words, partial writes are allowed.) Returns the number of - // bytes written, or an error code if the operation could not be - // performed. - // - // The file must not be opened with PLATFORM_FILE_ASYNC. - // This method must not be called if the stream was opened READ_ONLY. - // - // Zero byte writes are not allowed. - virtual int WriteSync(const char* buf, int buf_len); - - // Truncates the file to be |bytes| length. This is only valid for writable - // files. After truncation the file stream is positioned at |bytes|. The new - // position is returned, or a value < 0 on error. - // WARNING: one may not truncate a file beyond its current length on any - // platform with this call. - virtual int64 Truncate(int64 bytes); - // Forces out a filesystem sync on this file to make sure that the file was // written out to disk and is not currently sitting in the buffer. This does // not have to be called, it just forces one to happen at the time of @@ -228,26 +169,6 @@ class NET_EXPORT FileStream { // This method should not be called if the stream was opened READ_ONLY. virtual int Flush(const CompletionCallback& callback); - // Forces out a filesystem sync on this file to make sure that the file was - // written out to disk and is not currently sitting in the buffer. This does - // not have to be called, it just forces one to happen at the time of - // calling. - // - // Returns an error code if the operation could not be performed. - // - // This method should not be called if the stream was opened READ_ONLY. - virtual int FlushSync(); - - // Turns on UMA error statistics gathering. - void EnableErrorStatistics(); - - // Sets the source reference for net-internals logging. - // Creates source dependency events between |owner_bound_net_log| and - // |bound_net_log_|. Each gets an event showing the dependency on the other. - // If only one of those is valid, it gets an event showing that a change - // of ownership happened, but without details. - void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); - // Returns the underlying platform file for testing. const base::File& GetFileForTesting() const; diff --git a/net/base/file_stream_context.cc b/net/base/file_stream_context.cc index 5d1fdbb..1d7e493 100644 --- a/net/base/file_stream_context.cc +++ b/net/base/file_stream_context.cc @@ -8,22 +8,53 @@ #include "base/message_loop/message_loop_proxy.h" #include "base/task_runner_util.h" #include "base/threading/thread_restrictions.h" -#include "net/base/file_stream_net_log_parameters.h" +#include "base/values.h" #include "net/base/net_errors.h" #if defined(OS_ANDROID) #include "base/android/content_uri_utils.h" #endif +namespace net { + namespace { -void CallInt64ToInt(const net::CompletionCallback& callback, int64 result) { +void CallInt64ToInt(const CompletionCallback& callback, int64 result) { callback.Run(static_cast<int>(result)); } +const char* FileErrorSourceStrings[] = { + "OPEN", + "WRITE", + "READ", + "SEEK", + "FLUSH", + "SET_EOF", + "GET_SIZE", + "CLOSE" +}; + +COMPILE_ASSERT(ARRAYSIZE_UNSAFE(FileErrorSourceStrings) == + FILE_ERROR_SOURCE_COUNT, + file_error_source_enum_has_changed); + +// Creates NetLog parameters when a FileStream has an error. +base::Value* NetLogFileStreamErrorCallback( + FileErrorSource source, + int os_error, + Error net_error, + NetLog::LogLevel /* log_level */) { + base::DictionaryValue* dict = new base::DictionaryValue(); + + DCHECK_NE(FILE_ERROR_SOURCE_COUNT, source); + dict->SetString("operation", FileErrorSourceStrings[source]); + dict->SetInteger("os_error", os_error); + dict->SetInteger("net_error", net_error); + + return dict; } -namespace net { +} // namespace FileStream::Context::IOResult::IOResult() : result(OK), @@ -97,38 +128,6 @@ void FileStream::Context::OpenAsync(const base::FilePath& path, DCHECK(posted); async_in_progress_ = true; - - // TODO(rvargas): Figure out what to do here. For POSIX, async IO is - // implemented by doing blocking IO on another thread, so file_ is not really - // async, but this code has sync and async paths so it has random checks to - // figure out what mode to use. We should probably make this class async only, - // and make consumers of sync IO use base::File. - async_ = true; -} - -int FileStream::Context::OpenSync(const base::FilePath& path, int open_flags) { - DCHECK(!async_in_progress_); - - BeginOpenEvent(path); - OpenResult result = OpenFileImpl(path, open_flags); - if (result.file.IsValid()) { - file_ = result.file.Pass(); - // TODO(satorux): Remove this once all async clients are migrated to use - // Open(). crbug.com/114783 - if (open_flags & base::File::FLAG_ASYNC) - OnAsyncFileOpened(); - } else { - ProcessOpenError(result.error_code); - } - return result.error_code.result; -} - -void FileStream::Context::CloseSync() { - DCHECK(!async_in_progress_); - if (file_.IsValid()) { - file_.Close(); - bound_net_log_.EndEvent(NetLog::TYPE_FILE_STREAM_OPEN); - } } void FileStream::Context::CloseAsync(const CompletionCallback& callback) { @@ -165,12 +164,6 @@ void FileStream::Context::SeekAsync(Whence whence, async_in_progress_ = true; } -int64 FileStream::Context::SeekSync(Whence whence, int64 offset) { - IOResult result = SeekFileImpl(whence, offset); - RecordError(result, FILE_ERROR_SOURCE_SEEK); - return result.result; -} - void FileStream::Context::FlushAsync(const CompletionCallback& callback) { DCHECK(!async_in_progress_); @@ -187,12 +180,6 @@ void FileStream::Context::FlushAsync(const CompletionCallback& callback) { async_in_progress_ = true; } -int FileStream::Context::FlushSync() { - IOResult result = FlushFileImpl(); - RecordError(result, FILE_ERROR_SOURCE_FLUSH); - return result.result; -} - void FileStream::Context::RecordError(const IOResult& result, FileErrorSource source) const { if (result.result >= 0) { @@ -207,8 +194,6 @@ void FileStream::Context::RecordError(const IOResult& result, source, result.os_error, static_cast<net::Error>(result.result))); } - - RecordFileError(result.os_error, source, record_uma_); } void FileStream::Context::BeginOpenEvent(const base::FilePath& path) { @@ -311,4 +296,3 @@ void FileStream::Context::OnAsyncCompleted( } } // namespace net - diff --git a/net/base/file_stream_context.h b/net/base/file_stream_context.h index c70a8f6..92a6beb 100644 --- a/net/base/file_stream_context.h +++ b/net/base/file_stream_context.h @@ -33,7 +33,6 @@ #include "base/task_runner.h" #include "net/base/completion_callback.h" #include "net/base/file_stream.h" -#include "net/base/file_stream_metrics.h" #include "net/base/file_stream_whence.h" #include "net/base/net_log.h" @@ -49,6 +48,18 @@ namespace net { class IOBuffer; +enum FileErrorSource { + FILE_ERROR_SOURCE_OPEN = 0, + FILE_ERROR_SOURCE_WRITE, + FILE_ERROR_SOURCE_READ, + FILE_ERROR_SOURCE_SEEK, + FILE_ERROR_SOURCE_FLUSH, + FILE_ERROR_SOURCE_SET_EOF, + FILE_ERROR_SOURCE_GET_SIZE, + FILE_ERROR_SOURCE_CLOSE, + FILE_ERROR_SOURCE_COUNT, +}; + #if defined(OS_WIN) class FileStream::Context : public base::MessageLoopForIO::IOHandler { #elif defined(OS_POSIX) @@ -65,41 +76,26 @@ class FileStream::Context { Context(base::File file, const BoundNetLog& bound_net_log, const scoped_refptr<base::TaskRunner>& task_runner); - - // This is a deprecated constructor intended only to support callers that - // provide a PlatformFile. Such callers are to be updated to provide a File. - Context(base::File file, - int flags, - const BoundNetLog& bound_net_log, - const scoped_refptr<base::TaskRunner>& task_runner); #if defined(OS_WIN) virtual ~Context(); #elif defined(OS_POSIX) ~Context(); #endif - int64 GetFileSize() const; - int ReadAsync(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - int ReadSync(char* buf, int buf_len); int WriteAsync(IOBuffer* buf, int buf_len, const CompletionCallback& callback); - int WriteSync(const char* buf, int buf_len); - - int Truncate(int64 bytes); //////////////////////////////////////////////////////////////////////////// // Inline methods. //////////////////////////////////////////////////////////////////////////// - void set_record_uma(bool value) { record_uma_ = value; } const base::File& file() const { return file_; } bool async_in_progress() const { return async_in_progress_; } - bool async() const { return async_in_progress_ || async_ || file_.async(); } //////////////////////////////////////////////////////////////////////////// // Platform-independent methods implemented in file_stream_context.cc. @@ -113,19 +109,14 @@ class FileStream::Context { void OpenAsync(const base::FilePath& path, int open_flags, const CompletionCallback& callback); - int OpenSync(const base::FilePath& path, int open_flags); - - void CloseSync(); void CloseAsync(const CompletionCallback& callback); void SeekAsync(Whence whence, int64 offset, const Int64CompletionCallback& callback); - int64 SeekSync(Whence whence, int64 offset); void FlushAsync(const CompletionCallback& callback); - int FlushSync(); private: //////////////////////////////////////////////////////////////////////////// @@ -227,10 +218,8 @@ class FileStream::Context { #endif base::File file_; - bool record_uma_; bool async_in_progress_; bool orphaned_; - bool async_; // To be removed when flags are removed from the constructor. BoundNetLog bound_net_log_; scoped_refptr<base::TaskRunner> task_runner_; @@ -247,4 +236,3 @@ class FileStream::Context { } // namespace net #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ - diff --git a/net/base/file_stream_context_posix.cc b/net/base/file_stream_context_posix.cc index ac2c75e..f72b374 100644 --- a/net/base/file_stream_context_posix.cc +++ b/net/base/file_stream_context_posix.cc @@ -46,35 +46,18 @@ COMPILE_ASSERT(FROM_BEGIN == SEEK_SET && FileStream::Context::Context(const BoundNetLog& bound_net_log, const scoped_refptr<base::TaskRunner>& task_runner) - : record_uma_(false), - async_in_progress_(false), - orphaned_(false), - async_(false), - bound_net_log_(bound_net_log), - task_runner_(task_runner) { -} - -FileStream::Context::Context(base::File file, - const BoundNetLog& bound_net_log, - const scoped_refptr<base::TaskRunner>& task_runner) - : file_(file.Pass()), - record_uma_(false), - async_in_progress_(false), + : async_in_progress_(false), orphaned_(false), - async_(false), bound_net_log_(bound_net_log), task_runner_(task_runner) { } FileStream::Context::Context(base::File file, - int flags, const BoundNetLog& bound_net_log, const scoped_refptr<base::TaskRunner>& task_runner) : file_(file.Pass()), - record_uma_(false), async_in_progress_(false), orphaned_(false), - async_((flags & base::File::FLAG_ASYNC) == base::File::FLAG_ASYNC), bound_net_log_(bound_net_log), task_runner_(task_runner) { } @@ -82,17 +65,6 @@ FileStream::Context::Context(base::File file, FileStream::Context::~Context() { } -int64 FileStream::Context::GetFileSize() const { - struct stat info; - if (fstat(file_.GetPlatformFile(), &info) != 0) { - IOResult result = IOResult::FromOSError(errno); - RecordError(result, FILE_ERROR_SOURCE_GET_SIZE); - return result.result; - } - - return static_cast<int64>(info.st_size); -} - int FileStream::Context::ReadAsync(IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { @@ -113,13 +85,6 @@ int FileStream::Context::ReadAsync(IOBuffer* in_buf, return ERR_IO_PENDING; } -int FileStream::Context::ReadSync(char* in_buf, int buf_len) { - scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); - IOResult result = ReadFileImpl(buf, buf_len); - RecordError(result, FILE_ERROR_SOURCE_READ); - return result.result; -} - int FileStream::Context::WriteAsync(IOBuffer* in_buf, int buf_len, const CompletionCallback& callback) { @@ -140,23 +105,6 @@ int FileStream::Context::WriteAsync(IOBuffer* in_buf, return ERR_IO_PENDING; } -int FileStream::Context::WriteSync(const char* in_buf, int buf_len) { - scoped_refptr<IOBuffer> buf = new WrappedIOBuffer(in_buf); - IOResult result = WriteFileImpl(buf, buf_len); - RecordError(result, FILE_ERROR_SOURCE_WRITE); - return result.result; -} - -int FileStream::Context::Truncate(int64 bytes) { - if (ftruncate(file_.GetPlatformFile(), bytes) != 0) { - IOResult result = IOResult::FromOSError(errno); - RecordError(result, FILE_ERROR_SOURCE_SET_EOF); - return result.result; - } - - return bytes; -} - FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence, int64 offset) { off_t res = lseek(file_.GetPlatformFile(), static_cast<off_t>(offset), diff --git a/net/base/file_stream_context_win.cc b/net/base/file_stream_context_win.cc index cb7b41c..fe97263 100644 --- a/net/base/file_stream_context_win.cc +++ b/net/base/file_stream_context_win.cc @@ -40,10 +40,8 @@ void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { FileStream::Context::Context(const BoundNetLog& bound_net_log, const scoped_refptr<base::TaskRunner>& task_runner) : io_context_(), - record_uma_(false), async_in_progress_(false), orphaned_(false), - async_(false), bound_net_log_(bound_net_log), error_source_(FILE_ERROR_SOURCE_COUNT), task_runner_(task_runner) { @@ -56,53 +54,22 @@ FileStream::Context::Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner) : io_context_(), file_(file.Pass()), - record_uma_(false), async_in_progress_(false), orphaned_(false), - async_(false), bound_net_log_(bound_net_log), error_source_(FILE_ERROR_SOURCE_COUNT), task_runner_(task_runner) { io_context_.handler = this; memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped)); - if (file_.IsValid() && file_.async()) - OnAsyncFileOpened(); -} - -FileStream::Context::Context(base::File file, - int flags, - const BoundNetLog& bound_net_log, - const scoped_refptr<base::TaskRunner>& task_runner) - : io_context_(), - file_(file.Pass()), - record_uma_(false), - async_in_progress_(false), - orphaned_(false), - async_((flags & base::File::FLAG_ASYNC) == base::File::FLAG_ASYNC), - bound_net_log_(bound_net_log), - error_source_(FILE_ERROR_SOURCE_COUNT), - task_runner_(task_runner) { - io_context_.handler = this; - memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped)); - if (file_.IsValid() && (file_.async() || flags & base::File::FLAG_ASYNC)) + if (file_.IsValid()) { + // TODO(hashimoto): Check that file_ is async. OnAsyncFileOpened(); + } } FileStream::Context::~Context() { } -int64 FileStream::Context::GetFileSize() const { - LARGE_INTEGER file_size; - if (!GetFileSizeEx(file_.GetPlatformFile(), &file_size)) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "GetFileSizeEx failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_GET_SIZE); - return error.result; - } - - return file_size.QuadPart; -} - int FileStream::Context::ReadAsync(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { @@ -128,22 +95,6 @@ int FileStream::Context::ReadAsync(IOBuffer* buf, return ERR_IO_PENDING; } -int FileStream::Context::ReadSync(char* buf, int buf_len) { - DWORD bytes_read; - if (!ReadFile(file_.GetPlatformFile(), buf, buf_len, &bytes_read, NULL)) { - IOResult error = IOResult::FromOSError(GetLastError()); - if (error.os_error == ERROR_HANDLE_EOF) { - return 0; // Report EOF by returning 0 bytes read. - } else { - LOG(WARNING) << "ReadFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_READ); - } - return error.result; - } - - return bytes_read; -} - int FileStream::Context::WriteAsync(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { @@ -166,29 +117,6 @@ int FileStream::Context::WriteAsync(IOBuffer* buf, return ERR_IO_PENDING; } -int FileStream::Context::WriteSync(const char* buf, int buf_len) { - DWORD bytes_written = 0; - if (!WriteFile(file_.GetPlatformFile(), buf, buf_len, &bytes_written, NULL)) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "WriteFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_WRITE); - return error.result; - } - - return bytes_written; -} - -int FileStream::Context::Truncate(int64 bytes) { - if (!SetEndOfFile(file_.GetPlatformFile())) { - IOResult error = IOResult::FromOSError(GetLastError()); - LOG(WARNING) << "SetEndOfFile failed: " << error.os_error; - RecordError(error, FILE_ERROR_SOURCE_SET_EOF); - return error.result; - } - - return bytes; -} - void FileStream::Context::OnAsyncFileOpened() { base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(), this); diff --git a/net/base/file_stream_metrics.cc b/net/base/file_stream_metrics.cc deleted file mode 100644 index bff7174..0000000 --- a/net/base/file_stream_metrics.cc +++ /dev/null @@ -1,103 +0,0 @@ -// 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_metrics.h" - -#include "base/basictypes.h" -#include "base/logging.h" -#include "base/metrics/histogram.h" - -namespace net { - -namespace { - -const char* FileErrorSourceStrings[] = { - "OPEN", - "WRITE", - "READ", - "SEEK", - "FLUSH", - "SET_EOF", - "GET_SIZE", - "CLOSE" -}; - -COMPILE_ASSERT(ARRAYSIZE_UNSAFE(FileErrorSourceStrings) == - FILE_ERROR_SOURCE_COUNT, - file_error_source_enum_has_changed); - -void RecordFileErrorTypeCount(FileErrorSource source) { - UMA_HISTOGRAM_ENUMERATION( - "Net.FileErrorType_Counts", source, FILE_ERROR_SOURCE_COUNT); -} - -} // namespace - -void RecordFileError(int error, FileErrorSource source, bool record) { - if (!record) - return; - - RecordFileErrorTypeCount(source); - - int bucket = GetFileErrorUmaBucket(error); - - // Fixed values per platform. - static const int max_bucket = MaxFileErrorUmaBucket(); - static const int max_error = MaxFileErrorUmaValue(); - - switch(source) { - case FILE_ERROR_SOURCE_OPEN: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Open", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Open", bucket, max_bucket); - break; - - case FILE_ERROR_SOURCE_WRITE: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Write", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Write", bucket, max_bucket); - break; - - case FILE_ERROR_SOURCE_READ: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Read", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Read", bucket, max_bucket); - break; - - case FILE_ERROR_SOURCE_SEEK: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Seek", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Seek", bucket, max_bucket); - break; - - case FILE_ERROR_SOURCE_FLUSH: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Flush", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Flush", bucket, max_bucket); - break; - - case FILE_ERROR_SOURCE_SET_EOF: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_SetEof", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_SetEof", bucket, - max_bucket); - break; - - case FILE_ERROR_SOURCE_GET_SIZE: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_GetSize", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_GetSize", bucket, - max_bucket); - break; - - case FILE_ERROR_SOURCE_CLOSE: - UMA_HISTOGRAM_ENUMERATION("Net.FileError_Close", error, max_error); - UMA_HISTOGRAM_ENUMERATION("Net.FileErrorRange_Close", bucket, - max_bucket); - break; - - default: - break; - } -} - -const char* GetFileErrorSourceName(FileErrorSource source) { - DCHECK_NE(FILE_ERROR_SOURCE_COUNT, source); - return FileErrorSourceStrings[source]; -} - -} // namespace net diff --git a/net/base/file_stream_metrics.h b/net/base/file_stream_metrics.h deleted file mode 100644 index 4dab3dd..0000000 --- a/net/base/file_stream_metrics.h +++ /dev/null @@ -1,44 +0,0 @@ -// 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. - -// File error statistics gathering. - -#ifndef NET_BASE_FILE_STREAM_METRICS_H_ -#define NET_BASE_FILE_STREAM_METRICS_H_ - -namespace net { - -enum FileErrorSource { - FILE_ERROR_SOURCE_OPEN = 0, - FILE_ERROR_SOURCE_WRITE, - FILE_ERROR_SOURCE_READ, - FILE_ERROR_SOURCE_SEEK, - FILE_ERROR_SOURCE_FLUSH, - FILE_ERROR_SOURCE_SET_EOF, - FILE_ERROR_SOURCE_GET_SIZE, - FILE_ERROR_SOURCE_CLOSE, - FILE_ERROR_SOURCE_COUNT, -}; - -// UMA error statistics gathering. -// Put the error value into a bucket. -int GetFileErrorUmaBucket(int error); - -// The largest bucket number, plus 1. -int MaxFileErrorUmaBucket(); - -// The highest error value we want to individually report. -int MaxFileErrorUmaValue(); - -// |error| is a platform-specific error (Windows or Posix). -// |source| indicates the operation that resulted in the error. -// |record| is a flag indicating that we are interested in this error. -void RecordFileError(int error, FileErrorSource source, bool record); - -// Gets a description for the source of a file error. -const char* GetFileErrorSourceName(FileErrorSource source); - -} // namespace net - -#endif // NET_BASE_FILE_STREAM_METRICS_H_ diff --git a/net/base/file_stream_metrics_posix.cc b/net/base/file_stream_metrics_posix.cc deleted file mode 100644 index 7407d50..0000000 --- a/net/base/file_stream_metrics_posix.cc +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2011 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_metrics.h" - -namespace net { - -int GetFileErrorUmaBucket(int error) { - return 1; -} - -int MaxFileErrorUmaBucket() { - return 2; -} - -int MaxFileErrorUmaValue() { - return 160; -} - -} // namespace net diff --git a/net/base/file_stream_metrics_win.cc b/net/base/file_stream_metrics_win.cc deleted file mode 100644 index c397e4b..0000000 --- a/net/base/file_stream_metrics_win.cc +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright (c) 2011 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_metrics.h" - -#include <windows.h> - -#include "base/basictypes.h" - -namespace net { - -namespace { - -struct Range { - int low; - int high; -}; - -// The error range list is extracted from WinError.h. -// -// NOTE: The gaps between the ranges need to be recorded too. -// They will have odd-numbered buckets. -const Range kErrorRangeList[] = { - { 0, 321 }, // 2. - { 335, 371 }, // 4. - { 383, 387 }, // 6. - { 399, 404 }, // etc. - { 415, 418 }, - { 431, 433 }, - { 447, 868 }, - { 994, 1471 }, - { 1500, 1513 }, - { 1536, 1553 }, - { 1601, 1654 }, - { 1700, 1834 }, - { 1898, 1938 }, - { 2000, 2024 }, - { 2048, 2085 }, - { 2108, 2110 }, - { 2202, 2203 }, - { 2250, 2251 }, - { 2401, 2405 }, - { 3000, 3021 }, - { 3950, 3951 }, - { 4000, 4007 }, - { 4050, 4066 }, - { 4096, 4116 }, - { 4200, 4215 }, - { 4300, 4353 }, - { 4390, 4395 }, - { 4500, 4501 }, - { 4864, 4905 }, - { 5001, 5090 }, - { 5890, 5953 }, - { 6000, 6023 }, - { 6118, 6119 }, - { 6200, 6201 }, - { 6600, 6649 }, - { 6700, 6732 }, - { 6800, 6856 }, - { 7001, 7071 }, - { 8001, 8018 }, - { 8192, 8263 }, - { 8301, 8640 }, - { 8704, 8705 }, - { 8960, 9053 }, - { 9216, 9218 }, - { 9263, 9276 }, - { 9472, 9506 }, - { 9550, 9573 }, - { 9600, 9622 }, - { 9650, 9656 }, - { 9688, 9723 }, - { 9750, 9754 }, - { 9800, 9802 }, - { 9850, 9853 }, - { 9900, 9907 }, - { 10000, 10072 }, - { 10091, 10113 }, - { 11001, 11034 }, - { 12288, 12335 }, - { 12544, 12559 }, - { 12595, 12597 }, - { 12801, 12803 }, - { 13000, 13026 }, - { 13800, 13933 }, - { 14000, 14111 }, - { 15000, 15039 }, - { 15080, 15086 }, - { 15100, 15109 }, - { 15200, 15208 }, - { 15250, 15251 }, - { 15299, 15302 }, - { 16385, 16436 }, - { 18432, 18454 }, - { 20480, 20486 }, - { 24577, 24607 }, - { 28673, 28698 }, - { 32790, 32816 }, - { 33281, 33322 }, - { 35005, 35024 }, - { 36000, 36004 }, - { 40010, 40011 }, - { 40067, 40069 }, - { 53248, 53293 }, - { 53376, 53382 }, - { 57344, 57360 }, - { 57377, 57394 }, - { 65535, 65536 } // 2 * kNumErrorRanges. -}; -const size_t kNumErrorRanges = ARRAYSIZE_UNSAFE(kErrorRangeList); - -} // namespace - -// Windows has very many errors. We're not interested in most of them, but we -// don't know which ones are significant. -// This function maps error ranges to specific buckets. -// If we get hits on the buckets, we can add those values to the values we -// record individually. -// If we get values *between* the buckets, we record those as buckets too. -int GetFileErrorUmaBucket(int error) { - error = HRESULT_CODE(error); - - // This is a linear search, but of a short fixed-size array. - // It also gets called infrequently, on errors. - for (size_t n = 0; n < kNumErrorRanges; ++n) { - if (error < kErrorRangeList[n].low) - return (2 * (n + 1)) - 1; // In gap before the range. - if (error <= kErrorRangeList[n].high) - return 2 * (n + 1); // In the range. - } - - // After the last bucket. - return 2 * kNumErrorRanges + 1; -} - -int MaxFileErrorUmaBucket() { - return 2 * kNumErrorRanges + 2; -} - -int MaxFileErrorUmaValue() { - return kErrorRangeList[0].high + 1; -} - -} // namespace net diff --git a/net/base/file_stream_net_log_parameters.cc b/net/base/file_stream_net_log_parameters.cc deleted file mode 100644 index e85b625..0000000 --- a/net/base/file_stream_net_log_parameters.cc +++ /dev/null @@ -1,25 +0,0 @@ -// 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_net_log_parameters.h" - -#include "base/values.h" - -namespace net { - -base::Value* NetLogFileStreamErrorCallback( - FileErrorSource source, - int os_error, - net::Error net_error, - NetLog::LogLevel /* log_level */) { - base::DictionaryValue* dict = new base::DictionaryValue(); - - dict->SetString("operation", GetFileErrorSourceName(source)); - dict->SetInteger("os_error", os_error); - dict->SetInteger("net_error", net_error); - - return dict; -} - -} // namespace net diff --git a/net/base/file_stream_net_log_parameters.h b/net/base/file_stream_net_log_parameters.h deleted file mode 100644 index 09f96c8..0000000 --- a/net/base/file_stream_net_log_parameters.h +++ /dev/null @@ -1,27 +0,0 @@ -// 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. - -// File stream error reporting. - -#ifndef NET_BASE_FILE_STREAM_NET_LOG_PARAMETERS_H_ -#define NET_BASE_FILE_STREAM_NET_LOG_PARAMETERS_H_ - -#include <string> - -#include "net/base/file_stream_metrics.h" -#include "net/base/net_errors.h" -#include "net/base/net_log.h" - -namespace net { - -// Creates NetLog parameters when a FileStream has an error. -base::Value* NetLogFileStreamErrorCallback( - FileErrorSource source, - int os_error, - net::Error net_error, - NetLog::LogLevel log_level); - -} // namespace net - -#endif // NET_BASE_FILE_STREAM_NET_LOG_PARAMETERS_H_ diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index 4252c51..5a8c279 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -50,11 +50,11 @@ class FileStreamTest : public PlatformTest { base::WriteFile(temp_file_path_, kTestData, kTestDataSize); } virtual void TearDown() { - EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); - // FileStreamContexts must be asynchronously closed on the file task runner // before they can be deleted. Pump the RunLoop to avoid leaks. base::RunLoop().RunUntilIdle(); + EXPECT_TRUE(base::DeleteFile(temp_file_path_, false)); + PlatformTest::TearDown(); } @@ -66,18 +66,6 @@ class FileStreamTest : public PlatformTest { namespace { -TEST_F(FileStreamTest, BasicOpenExplicitClose) { - FileStream stream(NULL); - int rv = stream.OpenSync(temp_file_path(), - base::File::FLAG_OPEN | base::File::FLAG_READ); - EXPECT_EQ(OK, rv); - EXPECT_TRUE(stream.IsOpen()); - EXPECT_TRUE(stream.GetFileForTesting().IsValid()); - EXPECT_EQ(OK, stream.CloseSync()); - EXPECT_FALSE(stream.IsOpen()); - EXPECT_FALSE(stream.GetFileForTesting().IsValid()); -} - TEST_F(FileStreamTest, AsyncOpenExplicitClose) { TestCompletionCallback callback; FileStream stream(NULL); @@ -116,87 +104,71 @@ TEST_F(FileStreamTest, AsyncOpenExplicitCloseOrphaned) { // Test the use of FileStream with a file handle provided at construction. TEST_F(FileStreamTest, UseFileHandle) { + int rv = 0; + TestCompletionCallback callback; + TestInt64CompletionCallback callback64; // 1. Test reading with a file handle. ASSERT_EQ(kTestDataSize, base::WriteFile(temp_file_path(), kTestData, kTestDataSize)); - int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ; + int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | + base::File::FLAG_ASYNC; base::File file(temp_file_path(), flags); // Seek to the beginning of the file and read. scoped_ptr<FileStream> read_stream( new FileStream(file.Pass(), NULL, base::MessageLoopProxy::current())); - ASSERT_EQ(0, read_stream->SeekSync(FROM_BEGIN, 0)); - ASSERT_EQ(kTestDataSize, read_stream->Available()); + ASSERT_EQ(ERR_IO_PENDING, + read_stream->Seek(FROM_BEGIN, 0, callback64.callback())); + ASSERT_EQ(0, callback64.WaitForResult()); // Read into buffer and compare. - char buffer[kTestDataSize]; - ASSERT_EQ(kTestDataSize, - read_stream->ReadSync(buffer, kTestDataSize)); - ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); + scoped_refptr<IOBufferWithSize> read_buffer = + new IOBufferWithSize(kTestDataSize); + rv = read_stream->Read(read_buffer.get(), kTestDataSize, callback.callback()); + ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); + ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); read_stream.reset(); // 2. Test writing with a file handle. base::DeleteFile(temp_file_path(), false); - flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE; + flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | + base::File::FLAG_ASYNC; file.Initialize(temp_file_path(), flags); scoped_ptr<FileStream> write_stream( new FileStream(file.Pass(), NULL, base::MessageLoopProxy::current())); - ASSERT_EQ(0, write_stream->SeekSync(FROM_BEGIN, 0)); - ASSERT_EQ(kTestDataSize, - write_stream->WriteSync(kTestData, kTestDataSize)); + ASSERT_EQ(ERR_IO_PENDING, + write_stream->Seek(FROM_BEGIN, 0, callback64.callback())); + ASSERT_EQ(0, callback64.WaitForResult()); + scoped_refptr<IOBufferWithSize> write_buffer = CreateTestDataBuffer(); + rv = write_stream->Write(write_buffer.get(), kTestDataSize, + callback.callback()); + ASSERT_EQ(kTestDataSize, callback.GetResult(rv)); write_stream.reset(); // Read into buffer and compare to make sure the handle worked fine. ASSERT_EQ(kTestDataSize, - base::ReadFile(temp_file_path(), buffer, kTestDataSize)); - ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); + base::ReadFile(temp_file_path(), read_buffer->data(), + kTestDataSize)); + ASSERT_EQ(0, memcmp(kTestData, read_buffer->data(), kTestDataSize)); } TEST_F(FileStreamTest, UseClosedStream) { + int rv = 0; + TestCompletionCallback callback; + TestInt64CompletionCallback callback64; + FileStream stream(NULL, base::MessageLoopProxy::current()); EXPECT_FALSE(stream.IsOpen()); // Try seeking... - int64 new_offset = stream.SeekSync(FROM_BEGIN, 5); - EXPECT_EQ(ERR_UNEXPECTED, new_offset); - - // Try available... - int64 avail = stream.Available(); - EXPECT_EQ(ERR_UNEXPECTED, avail); + rv = stream.Seek(FROM_BEGIN, 5, callback64.callback()); + EXPECT_EQ(ERR_UNEXPECTED, callback64.GetResult(rv)); // Try reading... - char buf[10]; - int rv = stream.ReadSync(buf, arraysize(buf)); - EXPECT_EQ(ERR_UNEXPECTED, rv); -} - -TEST_F(FileStreamTest, BasicRead) { - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - - FileStream stream(NULL, base::MessageLoopProxy::current()); - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; - int rv = stream.OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - int64 total_bytes_avail = stream.Available(); - EXPECT_EQ(file_size, total_bytes_avail); - - int total_bytes_read = 0; - - std::string data_read; - for (;;) { - char buf[4]; - rv = stream.ReadSync(buf, arraysize(buf)); - EXPECT_LE(0, rv); - if (rv <= 0) - break; - total_bytes_read += rv; - data_read.append(buf, rv); - } - EXPECT_EQ(file_size, total_bytes_read); - EXPECT_EQ(kTestData, data_read); + scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(10); + rv = stream.Read(buf, buf->size(), callback.callback()); + EXPECT_EQ(ERR_UNEXPECTED, callback.GetResult(rv)); } TEST_F(FileStreamTest, AsyncRead) { @@ -211,9 +183,6 @@ TEST_F(FileStreamTest, AsyncRead) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); - int64 total_bytes_avail = stream.Available(); - EXPECT_EQ(file_size, total_bytes_avail); - int total_bytes_read = 0; std::string data_read; @@ -245,9 +214,6 @@ TEST_F(FileStreamTest, AsyncRead_EarlyDelete) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - scoped_refptr<IOBufferWithSize> buf = new IOBufferWithSize(4); rv = stream->Read(buf.get(), buf->size(), callback.callback()); stream.reset(); // Delete instead of closing it. @@ -261,39 +227,6 @@ TEST_F(FileStreamTest, AsyncRead_EarlyDelete) { } } -TEST_F(FileStreamTest, BasicRead_FromOffset) { - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - - FileStream stream(NULL, base::MessageLoopProxy::current()); - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; - int rv = stream.OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - const int64 kOffset = 3; - int64 new_offset = stream.SeekSync(FROM_BEGIN, kOffset); - EXPECT_EQ(kOffset, new_offset); - - int64 total_bytes_avail = stream.Available(); - EXPECT_EQ(file_size - kOffset, total_bytes_avail); - - int64 total_bytes_read = 0; - - std::string data_read; - for (;;) { - char buf[4]; - rv = stream.ReadSync(buf, arraysize(buf)); - EXPECT_LE(0, rv); - if (rv <= 0) - break; - total_bytes_read += rv; - data_read.append(buf, rv); - } - EXPECT_EQ(file_size - kOffset, total_bytes_read); - EXPECT_TRUE(data_read == kTestData + kOffset); - EXPECT_EQ(kTestData + kOffset, data_read); -} - TEST_F(FileStreamTest, AsyncRead_FromOffset) { int64 file_size; EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); @@ -313,9 +246,6 @@ TEST_F(FileStreamTest, AsyncRead_FromOffset) { int64 new_offset = callback64.WaitForResult(); EXPECT_EQ(kOffset, new_offset); - int64 total_bytes_avail = stream.Available(); - EXPECT_EQ(file_size - kOffset, total_bytes_avail); - int total_bytes_read = 0; std::string data_read; @@ -334,28 +264,6 @@ TEST_F(FileStreamTest, AsyncRead_FromOffset) { EXPECT_EQ(kTestData + kOffset, data_read); } -TEST_F(FileStreamTest, SeekAround) { - FileStream stream(NULL, base::MessageLoopProxy::current()); - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; - int rv = stream.OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - const int64 kOffset = 3; - int64 new_offset = stream.SeekSync(FROM_BEGIN, kOffset); - EXPECT_EQ(kOffset, new_offset); - - new_offset = stream.SeekSync(FROM_CURRENT, kOffset); - EXPECT_EQ(2 * kOffset, new_offset); - - new_offset = stream.SeekSync(FROM_CURRENT, -kOffset); - EXPECT_EQ(kOffset, new_offset); - - const int kTestDataLen = arraysize(kTestData) - 1; - - new_offset = stream.SeekSync(FROM_END, -kTestDataLen); - EXPECT_EQ(0, new_offset); -} - TEST_F(FileStreamTest, AsyncSeekAround) { FileStream stream(NULL, base::MessageLoopProxy::current()); int flags = base::File::FLAG_OPEN | base::File::FLAG_ASYNC | @@ -391,25 +299,6 @@ TEST_F(FileStreamTest, AsyncSeekAround) { EXPECT_EQ(0, new_offset); } -TEST_F(FileStreamTest, BasicWrite) { - scoped_ptr<FileStream> stream( - new FileStream(NULL, base::MessageLoopProxy::current())); - int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; - int rv = stream->OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(0, file_size); - - rv = stream->WriteSync(kTestData, kTestDataSize); - EXPECT_EQ(kTestDataSize, rv); - stream.reset(); - - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(kTestDataSize, file_size); -} - TEST_F(FileStreamTest, AsyncWrite) { FileStream stream(NULL, base::MessageLoopProxy::current()); int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | @@ -471,29 +360,6 @@ TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) { } } -TEST_F(FileStreamTest, BasicWrite_FromOffset) { - scoped_ptr<FileStream> stream( - new FileStream(NULL, base::MessageLoopProxy::current())); - int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE; - int rv = stream->OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(kTestDataSize, file_size); - - const int64 kOffset = 0; - int64 new_offset = stream->SeekSync(FROM_END, kOffset); - EXPECT_EQ(kTestDataSize, new_offset); - - rv = stream->WriteSync(kTestData, kTestDataSize); - EXPECT_EQ(kTestDataSize, rv); - stream.reset(); - - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(kTestDataSize * 2, file_size); -} - TEST_F(FileStreamTest, AsyncWrite_FromOffset) { int64 file_size; EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); @@ -533,89 +399,6 @@ TEST_F(FileStreamTest, AsyncWrite_FromOffset) { EXPECT_EQ(file_size, kTestDataSize * 2); } -TEST_F(FileStreamTest, BasicReadWrite) { - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - - scoped_ptr<FileStream> stream( - new FileStream(NULL, base::MessageLoopProxy::current())); - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | - base::File::FLAG_WRITE; - int rv = stream->OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - - int total_bytes_read = 0; - - std::string data_read; - for (;;) { - char buf[4]; - rv = stream->ReadSync(buf, arraysize(buf)); - EXPECT_LE(0, rv); - if (rv <= 0) - break; - total_bytes_read += rv; - data_read.append(buf, rv); - } - EXPECT_EQ(file_size, total_bytes_read); - EXPECT_TRUE(data_read == kTestData); - - rv = stream->WriteSync(kTestData, kTestDataSize); - EXPECT_EQ(kTestDataSize, rv); - stream.reset(); - - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(kTestDataSize * 2, file_size); -} - -TEST_F(FileStreamTest, BasicWriteRead) { - int64 file_size; - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - - scoped_ptr<FileStream> stream( - new FileStream(NULL, base::MessageLoopProxy::current())); - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | - base::File::FLAG_WRITE; - int rv = stream->OpenSync(temp_file_path(), flags); - EXPECT_EQ(OK, rv); - - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - - int64 offset = stream->SeekSync(FROM_END, 0); - EXPECT_EQ(offset, file_size); - - rv = stream->WriteSync(kTestData, kTestDataSize); - EXPECT_EQ(kTestDataSize, rv); - - offset = stream->SeekSync(FROM_BEGIN, 0); - EXPECT_EQ(0, offset); - - int64 total_bytes_read = 0; - - std::string data_read; - for (;;) { - char buf[4]; - rv = stream->ReadSync(buf, arraysize(buf)); - EXPECT_LE(0, rv); - if (rv <= 0) - break; - total_bytes_read += rv; - data_read.append(buf, rv); - } - stream.reset(); - - EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); - EXPECT_EQ(kTestDataSize * 2, file_size); - EXPECT_EQ(kTestDataSize * 2, total_bytes_read); - - const std::string kExpectedFileData = - std::string(kTestData) + std::string(kTestData); - EXPECT_EQ(kExpectedFileData, data_read); -} - TEST_F(FileStreamTest, BasicAsyncReadWrite) { int64 file_size; EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size)); @@ -629,9 +412,6 @@ TEST_F(FileStreamTest, BasicAsyncReadWrite) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - int64 total_bytes_read = 0; std::string data_read; @@ -685,9 +465,6 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - TestInt64CompletionCallback callback64; rv = stream->Seek(FROM_END, 0, callback64.callback()); ASSERT_EQ(ERR_IO_PENDING, rv); @@ -796,7 +573,14 @@ class TestWriteReadCompletionCallback { *total_bytes_read_ += total_bytes_read; *data_read_ += data_read; } else { // We're done writing all data. Start reading the data. - stream_->SeekSync(FROM_BEGIN, 0); + TestInt64CompletionCallback callback64; + EXPECT_EQ(ERR_IO_PENDING, + stream_->Seek(FROM_BEGIN, 0, callback64.callback())); + { + base::MessageLoop::ScopedNestableTaskAllower allow( + base::MessageLoop::current()); + EXPECT_LE(0, callback64.WaitForResult()); + } TestCompletionCallback callback; for (;;) { @@ -848,11 +632,9 @@ TEST_F(FileStreamTest, AsyncWriteRead) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, open_callback.WaitForResult()); - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - - int64 offset = stream->SeekSync(FROM_END, 0); - EXPECT_EQ(offset, file_size); + TestInt64CompletionCallback callback64; + EXPECT_EQ(ERR_IO_PENDING, stream->Seek(FROM_END, 0, callback64.callback())); + EXPECT_EQ(file_size, callback64.WaitForResult()); int total_bytes_written = 0; int total_bytes_read = 0; @@ -954,11 +736,9 @@ TEST_F(FileStreamTest, AsyncWriteClose) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, open_callback.WaitForResult()); - int64 total_bytes_avail = stream->Available(); - EXPECT_EQ(file_size, total_bytes_avail); - - int64 offset = stream->SeekSync(FROM_END, 0); - EXPECT_EQ(offset, file_size); + TestInt64CompletionCallback callback64; + EXPECT_EQ(ERR_IO_PENDING, stream->Seek(FROM_END, 0, callback64.callback())); + EXPECT_EQ(file_size, callback64.WaitForResult()); int total_bytes_written = 0; TestWriteCloseCompletionCallback callback(stream.get(), &total_bytes_written); @@ -976,34 +756,6 @@ TEST_F(FileStreamTest, AsyncWriteClose) { EXPECT_EQ(kTestDataSize * 2, file_size); } -// Tests truncating a file. -TEST_F(FileStreamTest, Truncate) { - int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE; - - scoped_ptr<FileStream> write_stream( - new FileStream(NULL, base::MessageLoopProxy::current())); - ASSERT_EQ(OK, write_stream->OpenSync(temp_file_path(), flags)); - - // Write some data to the file. - const char test_data[] = "0123456789"; - write_stream->WriteSync(test_data, arraysize(test_data)); - - // Truncate the file. - ASSERT_EQ(4, write_stream->Truncate(4)); - - // Write again. - write_stream->WriteSync(test_data, 4); - - // Close the stream. - write_stream.reset(); - - // Read in the contents and make sure we get back what we expected. - std::string read_contents; - EXPECT_TRUE(base::ReadFileToString(temp_file_path(), &read_contents)); - - EXPECT_EQ("01230123", read_contents); -} - TEST_F(FileStreamTest, AsyncOpenAndDelete) { scoped_ptr<FileStream> stream( new FileStream(NULL, base::MessageLoopProxy::current())); @@ -1096,9 +848,6 @@ TEST_F(FileStreamTest, ContentUriAsyncRead) { EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); - int64 total_bytes_avail = stream.Available(); - EXPECT_EQ(file_size, total_bytes_avail); - int total_bytes_read = 0; std::string data_read; diff --git a/net/base/mock_file_stream.cc b/net/base/mock_file_stream.cc index 33d3c11..3d95994 100644 --- a/net/base/mock_file_stream.cc +++ b/net/base/mock_file_stream.cc @@ -41,11 +41,6 @@ MockFileStream::MockFileStream( MockFileStream::~MockFileStream() { } -int MockFileStream::OpenSync(const base::FilePath& path, int open_flags) { - path_ = path; - return ReturnError(FileStream::OpenSync(path, open_flags)); -} - int MockFileStream::Seek(Whence whence, int64 offset, const Int64CompletionCallback& callback) { Int64CompletionCallback wrapped_callback = @@ -56,14 +51,6 @@ int MockFileStream::Seek(Whence whence, int64 offset, return ErrorCallback64(wrapped_callback); } -int64 MockFileStream::SeekSync(Whence whence, int64 offset) { - return ReturnError64(FileStream::SeekSync(whence, offset)); -} - -int64 MockFileStream::Available() { - return ReturnError64(FileStream::Available()); -} - int MockFileStream::Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { @@ -75,14 +62,6 @@ int MockFileStream::Read(IOBuffer* buf, return ErrorCallback(wrapped_callback); } -int MockFileStream::ReadSync(char* buf, int buf_len) { - return ReturnError(FileStream::ReadSync(buf, buf_len)); -} - -int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { - return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); -} - int MockFileStream::Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) { @@ -94,14 +73,6 @@ int MockFileStream::Write(IOBuffer* buf, return ErrorCallback(wrapped_callback); } -int MockFileStream::WriteSync(const char* buf, int buf_len) { - return ReturnError(FileStream::WriteSync(buf, buf_len)); -} - -int64 MockFileStream::Truncate(int64 bytes) { - return ReturnError64(FileStream::Truncate(bytes)); -} - int MockFileStream::Flush(const CompletionCallback& callback) { CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(), @@ -111,10 +82,6 @@ int MockFileStream::Flush(const CompletionCallback& callback) { return ErrorCallback(wrapped_callback); } -int MockFileStream::FlushSync() { - return ReturnError(FileStream::FlushSync()); -} - void MockFileStream::ThrottleCallbacks() { CHECK(!throttled_); throttled_ = true; diff --git a/net/base/mock_file_stream.h b/net/base/mock_file_stream.h index 4427e44..aa44d2f 100644 --- a/net/base/mock_file_stream.h +++ b/net/base/mock_file_stream.h @@ -29,23 +29,15 @@ class MockFileStream : public net::FileStream { virtual ~MockFileStream(); // FileStream methods. - virtual int OpenSync(const base::FilePath& path, int open_flags) OVERRIDE; virtual int Seek(net::Whence whence, int64 offset, const Int64CompletionCallback& callback) OVERRIDE; - virtual int64 SeekSync(net::Whence whence, int64 offset) OVERRIDE; - virtual int64 Available() OVERRIDE; virtual int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback) OVERRIDE; - virtual int ReadSync(char* buf, int buf_len) OVERRIDE; - virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; virtual int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback) OVERRIDE; - virtual int WriteSync(const char* buf, int buf_len) OVERRIDE; - virtual int64 Truncate(int64 bytes) OVERRIDE; virtual int Flush(const CompletionCallback& callback) OVERRIDE; - virtual int FlushSync() OVERRIDE; void set_forced_error_async(int error) { forced_error_ = error; diff --git a/net/net.gyp b/net/net.gyp index d1a83aa..2f84d4e8 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -116,12 +116,6 @@ 'base/file_stream_context.h', 'base/file_stream_context_posix.cc', 'base/file_stream_context_win.cc', - 'base/file_stream_metrics.cc', - 'base/file_stream_metrics.h', - 'base/file_stream_metrics_posix.cc', - 'base/file_stream_metrics_win.cc', - 'base/file_stream_net_log_parameters.cc', - 'base/file_stream_net_log_parameters.h', 'base/file_stream_whence.h', 'base/filename_util.cc', 'base/filename_util.h', |