diff options
author | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-29 22:59:21 +0000 |
---|---|---|
committer | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-29 22:59:21 +0000 |
commit | 47a881b49c8e38c20bf58769f0496e1578c5284e (patch) | |
tree | 7bd922eec9ffa62394f2297ec361b487ffc2a81c /net/base | |
parent | 5780a2841d931b7d36827fe52189423ebb1884ba (diff) | |
download | chromium_src-47a881b49c8e38c20bf58769f0496e1578c5284e.zip chromium_src-47a881b49c8e38c20bf58769f0496e1578c5284e.tar.gz chromium_src-47a881b49c8e38c20bf58769f0496e1578c5284e.tar.bz2 |
Detect file system errors during downloads.
Moving towards giving the user feedback when a file system error occurs during a download.
Split from CL 7134019.
BUG=85240
TEST=None
Review URL: http://codereview.chromium.org/7646025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98725 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/file_stream.h | 22 | ||||
-rwxr-xr-x | net/base/mock_file_stream.cc | 50 | ||||
-rwxr-xr-x | net/base/mock_file_stream.h | 75 |
3 files changed, 136 insertions, 11 deletions
diff --git a/net/base/file_stream.h b/net/base/file_stream.h index da42afb..a58557c 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -40,31 +40,31 @@ class NET_EXPORT FileStream { // is destructed. FileStream(base::PlatformFile file, int flags); - ~FileStream(); + virtual ~FileStream(); // Call this method to close the FileStream. It is OK to call Close // multiple times. Redundant calls are ignored. // Note that if there are any pending async operations, they'll be aborted. - void Close(); + virtual void Close(); // Call this method to open the FileStream. 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 - int Open(const FilePath& path, int open_flags); + virtual int Open(const FilePath& path, int open_flags); // Returns true if Open succeeded and Close has not been called. - bool IsOpen() const; + virtual bool IsOpen() const; // Adjust the position from where data is read. 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 Seek while a Read call // has a pending completion. - int64 Seek(Whence whence, int64 offset); + virtual int64 Seek(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. - int64 Available(); + virtual int64 Available(); // Call this method to read data from the current stream position. Up to // buf_len bytes will be copied into buf. (In other words, partial reads are @@ -85,14 +85,14 @@ class NET_EXPORT FileStream { // This method should not be called if the stream was opened WRITE_ONLY. // // You can pass NULL as the callback for synchronous I/O. - int Read(char* buf, int buf_len, CompletionCallback* callback); + virtual int Read(char* buf, int buf_len, CompletionCallback* callback); // Performs the same as Read, 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. - int ReadUntilComplete(char *buf, int buf_len); + virtual int ReadUntilComplete(char *buf, int buf_len); // Call this method to write data at the current stream position. Up to // buf_len bytes will be written from buf. (In other words, partial writes are @@ -113,14 +113,14 @@ class NET_EXPORT FileStream { // This method should not be called if the stream was opened READ_ONLY. // // You can pass NULL as the callback for synchronous I/O. - int Write(const char* buf, int buf_len, CompletionCallback* callback); + virtual int Write(const char* buf, int buf_len, CompletionCallback* callback); // 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 retured, or a value < 0 on error. // WARNING: one may not truncate a file beyond its current length on any // platform with this call. - int64 Truncate(int64 bytes); + 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 @@ -130,7 +130,7 @@ class NET_EXPORT FileStream { /// Returns an error code if the operation could not be performed. // // This method should not be called if the stream was opened READ_ONLY. - int Flush(); + virtual int Flush(); private: class AsyncContext; diff --git a/net/base/mock_file_stream.cc b/net/base/mock_file_stream.cc new file mode 100755 index 0000000..1a8f1b8 --- /dev/null +++ b/net/base/mock_file_stream.cc @@ -0,0 +1,50 @@ +// 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/mock_file_stream.h" + +namespace net { + +namespace testing { + +int MockFileStream::Open(const FilePath& path, int open_flags) { + path_ = path; + return ReturnError(net::FileStream::Open(path, open_flags)); +} + +int64 MockFileStream::Seek(net::Whence whence, int64 offset) { + return ReturnError64(net::FileStream::Seek(whence, offset)); +} + +int64 MockFileStream::Available() { + return ReturnError64(net::FileStream::Available()); +} + +int MockFileStream::Read(char* buf, + int buf_len, + net::CompletionCallback* callback) { + return ReturnError(net::FileStream::Read(buf, buf_len, callback)); +} + +int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { + return ReturnError(net::FileStream::ReadUntilComplete(buf, buf_len)); +} + +int MockFileStream::Write(const char* buf, + int buf_len, + net::CompletionCallback* callback) { + return ReturnError(net::FileStream::Write(buf, buf_len, callback)); +} + +int64 MockFileStream::Truncate(int64 bytes) { + return ReturnError64(net::FileStream::Truncate(bytes)); +} + +int MockFileStream::Flush() { + return ReturnError(net::FileStream::Flush()); +} + +} // namespace testing + +} // namespace net diff --git a/net/base/mock_file_stream.h b/net/base/mock_file_stream.h new file mode 100755 index 0000000..45c09ab --- /dev/null +++ b/net/base/mock_file_stream.h @@ -0,0 +1,75 @@ +// 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. + +// This file defines MockFileStream, a test class for FileStream. + +#ifndef NET_BASE_MOCK_FILE_STREAM_H_ +#define NET_BASE_MOCK_FILE_STREAM_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/file_path.h" +#include "net/base/file_stream.h" +#include "net/base/net_errors.h" + +namespace net { + +namespace testing { + +class MockFileStream : public net::FileStream { + public: + MockFileStream() : forced_error_(net::OK) {} + + MockFileStream(base::PlatformFile file, int flags) + : net::FileStream(file, flags), forced_error_(net::OK) {} + + // FileStream methods. + virtual int Open(const FilePath& path, int open_flags) OVERRIDE; + virtual int64 Seek(net::Whence whence, int64 offset) OVERRIDE; + virtual int64 Available() OVERRIDE; + virtual int Read(char* buf, + int buf_len, + net::CompletionCallback* callback) OVERRIDE; + virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; + virtual int Write(const char* buf, + int buf_len, + net::CompletionCallback* callback) OVERRIDE; + virtual int64 Truncate(int64 bytes) OVERRIDE; + virtual int Flush() OVERRIDE; + + void set_forced_error(int error) { forced_error_ = error; } + void clear_forced_error() { forced_error_ = net::OK; } + const FilePath& get_path() const { return path_; } + + private: + int ReturnError(int function_error) { + if (forced_error_ != net::OK) { + int ret = forced_error_; + clear_forced_error(); + return ret; + } + + return function_error; + } + + int64 ReturnError64(int64 function_error) { + if (forced_error_ != net::OK) { + int64 ret = forced_error_; + clear_forced_error(); + return ret; + } + + return function_error; + } + + int forced_error_; + FilePath path_; +}; + +} // namespace testing + +} // namespace net + +#endif // NET_BASE_MOCK_FILE_STREAM_H_ |