Skip to content

Commit

Permalink
net: Remove unused FileStream methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hashimoto@chromium.org committed Apr 9, 2014
1 parent 05d1265 commit 50f91af
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 1,160 deletions.
168 changes: 3 additions & 165 deletions net/base/file_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 */))) {
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down
79 changes: 0 additions & 79 deletions net/base/file_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;

Expand Down
Loading

0 comments on commit 50f91af

Please sign in to comment.