summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-21 00:41:15 +0000
committerrvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-21 00:41:15 +0000
commita0830591020c23ad911aa287390a4ae942c5dc41 (patch)
tree650128541ee17d686b45cb0b163db4e38522f015
parentdd1dad31497696ef81234339e6e3cf6f5af26eea (diff)
downloadchromium_src-a0830591020c23ad911aa287390a4ae942c5dc41.zip
chromium_src-a0830591020c23ad911aa287390a4ae942c5dc41.tar.gz
chromium_src-a0830591020c23ad911aa287390a4ae942c5dc41.tar.bz2
net: Update FileStream to use base::File instead of PlatformFile.
As collateral damage, OpenContentUriForRead is now returning File instead of a plain file descriptor. BUG=322664 TEST=net_unittests R=willchan@chromium.org Review URL: https://codereview.chromium.org/189393002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258472 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/android/content_uri_utils.cc7
-rw-r--r--base/android/content_uri_utils.h3
-rw-r--r--base/file_util_posix.cc17
-rw-r--r--base/file_util_unittest.cc10
-rw-r--r--base/files/file.h2
-rw-r--r--base/files/file_posix.cc17
-rw-r--r--base/files/file_unittest.cc38
-rw-r--r--net/base/file_stream.cc80
-rw-r--r--net/base/file_stream.h24
-rw-r--r--net/base/file_stream_context.cc108
-rw-r--r--net/base/file_stream_context.h34
-rw-r--r--net/base/file_stream_context_posix.cc43
-rw-r--r--net/base/file_stream_context_win.cc59
-rw-r--r--net/base/file_stream_unittest.cc325
-rw-r--r--net/base/upload_file_element_reader.cc4
-rw-r--r--net/url_request/url_fetcher_response_writer.cc8
-rw-r--r--net/url_request/url_request_file_job.cc6
17 files changed, 378 insertions, 407 deletions
diff --git a/base/android/content_uri_utils.cc b/base/android/content_uri_utils.cc
index 64d6ad2..0e0c0ea 100644
--- a/base/android/content_uri_utils.cc
+++ b/base/android/content_uri_utils.cc
@@ -6,7 +6,6 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
-#include "base/platform_file.h"
#include "jni/ContentUriUtils_jni.h"
using base::android::ConvertUTF8ToJavaString;
@@ -25,15 +24,15 @@ bool ContentUriExists(const FilePath& content_uri) {
env, base::android::GetApplicationContext(), j_uri.obj());
}
-int OpenContentUriForRead(const FilePath& content_uri) {
+File OpenContentUriForRead(const FilePath& content_uri) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> j_uri =
ConvertUTF8ToJavaString(env, content_uri.value());
jint fd = Java_ContentUriUtils_openContentUriForRead(
env, base::android::GetApplicationContext(), j_uri.obj());
if (fd < 0)
- return base::kInvalidPlatformFileValue;
- return fd;
+ return File();
+ return File(fd);
}
} // namespace base
diff --git a/base/android/content_uri_utils.h b/base/android/content_uri_utils.h
index ec820ef..827ec92 100644
--- a/base/android/content_uri_utils.h
+++ b/base/android/content_uri_utils.h
@@ -9,6 +9,7 @@
#include "base/base_export.h"
#include "base/basictypes.h"
+#include "base/files/file.h"
#include "base/files/file_path.h"
namespace base {
@@ -17,7 +18,7 @@ bool RegisterContentUriUtils(JNIEnv* env);
// Opens a content uri for read and returns the file descriptor to the caller.
// Returns -1 if the uri is invalid.
-BASE_EXPORT int OpenContentUriForRead(const FilePath& content_uri);
+BASE_EXPORT File OpenContentUriForRead(const FilePath& content_uri);
// Check whether a content uri exists.
BASE_EXPORT bool ContentUriExists(const FilePath& content_uri);
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 2570172..03afea7 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -71,7 +71,7 @@ static int CallLstat(const char *path, stat_wrapper_t *sb) {
ThreadRestrictions::AssertIOAllowed();
return lstat(path, sb);
}
-#else
+#else // defined(OS_BSD) || defined(OS_MACOSX)
typedef struct stat64 stat_wrapper_t;
static int CallStat(const char *path, stat_wrapper_t *sb) {
ThreadRestrictions::AssertIOAllowed();
@@ -81,13 +81,7 @@ static int CallLstat(const char *path, stat_wrapper_t *sb) {
ThreadRestrictions::AssertIOAllowed();
return lstat64(path, sb);
}
-#if defined(OS_ANDROID)
-static int CallFstat(int fd, stat_wrapper_t *sb) {
- ThreadRestrictions::AssertIOAllowed();
- return fstat64(fd, sb);
-}
-#endif
-#endif
+#endif // !(defined(OS_BSD) || defined(OS_MACOSX))
// Helper for NormalizeFilePath(), defined below.
bool RealPath(const FilePath& path, FilePath* real_path) {
@@ -634,11 +628,10 @@ bool GetFileInfo(const FilePath& file_path, File::Info* results) {
stat_wrapper_t file_info;
#if defined(OS_ANDROID)
if (file_path.IsContentUri()) {
- ScopedFD fd(OpenContentUriForRead(file_path));
- if (!fd.is_valid())
- return false;
- if (CallFstat(fd.get(), &file_info) != 0)
+ File file = OpenContentUriForRead(file_path);
+ if (!file.IsValid())
return false;
+ return file.GetInfo(results);
} else {
#endif // defined(OS_ANDROID)
if (CallStat(file_path.value().c_str(), &file_info) != 0)
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 4bdd90f..642c9d5 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -2451,9 +2451,9 @@ TEST_F(FileUtilTest, ValidContentUriTest) {
// We should be able to read the file.
char* buffer = new char[image_size];
- int fd = OpenContentUriForRead(path);
- EXPECT_LT(0, fd);
- EXPECT_TRUE(ReadFromFD(fd, buffer, image_size));
+ File file = OpenContentUriForRead(path);
+ EXPECT_TRUE(file.IsValid());
+ EXPECT_TRUE(file.ReadAtCurrentPos(buffer, image_size));
delete[] buffer;
}
@@ -2466,8 +2466,8 @@ TEST_F(FileUtilTest, NonExistentContentUriTest) {
EXPECT_FALSE(GetFileSize(path, &size));
// We should not be able to read the file.
- int fd = OpenContentUriForRead(path);
- EXPECT_EQ(-1, fd);
+ File file = OpenContentUriForRead(path);
+ EXPECT_FALSE(file.IsValid());
}
#endif
diff --git a/base/files/file.h b/base/files/file.h
index 9c2479d..dc7616bb 100644
--- a/base/files/file.h
+++ b/base/files/file.h
@@ -265,6 +265,8 @@ class BASE_EXPORT File {
// Unlock a file previously locked.
Error Unlock();
+ bool async() const { return async_; }
+
#if defined(OS_WIN)
static Error OSErrorToFileError(DWORD last_error);
#elif defined(OS_POSIX)
diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
index f62491d..46d6d94 100644
--- a/base/files/file_posix.cc
+++ b/base/files/file_posix.cc
@@ -125,7 +125,6 @@ static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) {
void File::InitializeUnsafe(const FilePath& name, uint32 flags) {
base::ThreadRestrictions::AssertIOAllowed();
DCHECK(!IsValid());
- DCHECK(!(flags & FLAG_ASYNC));
int open_flags = 0;
if (flags & FLAG_CREATE)
@@ -191,17 +190,19 @@ void File::InitializeUnsafe(const FilePath& name, uint32 flags) {
}
}
- if (descriptor >= 0 && (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)))
+ if (descriptor < 0) {
+ error_details_ = File::OSErrorToFileError(errno);
+ return;
+ }
+
+ if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
created_ = true;
- if ((descriptor >= 0) && (flags & FLAG_DELETE_ON_CLOSE))
+ if (flags & FLAG_DELETE_ON_CLOSE)
unlink(name.value().c_str());
- if (descriptor >= 0)
- error_details_ = FILE_OK;
- else
- error_details_ = File::OSErrorToFileError(errno);
-
+ async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
+ error_details_ = FILE_OK;
file_.reset(descriptor);
}
#endif // !defined(OS_NACL)
diff --git a/base/files/file_unittest.cc b/base/files/file_unittest.cc
index c0579ad..59cba31 100644
--- a/base/files/file_unittest.cc
+++ b/base/files/file_unittest.cc
@@ -11,7 +11,7 @@
using base::File;
using base::FilePath;
-TEST(File, Create) {
+TEST(FileTest, Create) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("create_file_1");
@@ -87,7 +87,25 @@ TEST(File, Create) {
EXPECT_FALSE(base::PathExists(file_path));
}
-TEST(File, DeleteOpenFile) {
+TEST(FileTest, Async) {
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ FilePath file_path = temp_dir.path().AppendASCII("create_file");
+
+ {
+ File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_ASYNC);
+ EXPECT_TRUE(file.IsValid());
+ EXPECT_TRUE(file.async());
+ }
+
+ {
+ File file(file_path, base::File::FLAG_OPEN_ALWAYS);
+ EXPECT_TRUE(file.IsValid());
+ EXPECT_FALSE(file.async());
+ }
+}
+
+TEST(FileTest, DeleteOpenFile) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("create_file_1");
@@ -114,7 +132,7 @@ TEST(File, DeleteOpenFile) {
EXPECT_FALSE(base::PathExists(file_path));
}
-TEST(File, ReadWrite) {
+TEST(FileTest, ReadWrite) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("read_write_file");
@@ -186,7 +204,7 @@ TEST(File, ReadWrite) {
EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]);
}
-TEST(File, Append) {
+TEST(FileTest, Append) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("append_file");
@@ -234,7 +252,7 @@ TEST(File, Append) {
}
-TEST(File, Length) {
+TEST(FileTest, Length) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("truncate_file");
@@ -283,9 +301,9 @@ TEST(File, Length) {
// Flakily fails: http://crbug.com/86494
#if defined(OS_ANDROID)
-TEST(File, TouchGetInfo) {
+TEST(FileTest, TouchGetInfo) {
#else
-TEST(File, DISABLED_TouchGetInfo) {
+TEST(FileTest, DISABLED_TouchGetInfo) {
#endif
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
@@ -349,7 +367,7 @@ TEST(File, DISABLED_TouchGetInfo) {
creation_time.ToInternalValue());
}
-TEST(File, ReadAtCurrentPosition) {
+TEST(FileTest, ReadAtCurrentPosition) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("read_at_current_position");
@@ -373,7 +391,7 @@ TEST(File, ReadAtCurrentPosition) {
EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData));
}
-TEST(File, WriteAtCurrentPosition) {
+TEST(FileTest, WriteAtCurrentPosition) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("write_at_current_position");
@@ -397,7 +415,7 @@ TEST(File, WriteAtCurrentPosition) {
}
#if defined(OS_WIN)
-TEST(File, GetInfoForDirectory) {
+TEST(FileTest, GetInfoForDirectory) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test"));
diff --git a/net/base/file_stream.cc b/net/base/file_stream.cc
index fd2eb4a..2df7521 100644
--- a/net/base/file_stream.cc
+++ b/net/base/file_stream.cc
@@ -19,8 +19,7 @@ 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. */
- : open_flags_(base::PLATFORM_FILE_ASYNC),
- bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ : 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);
}
@@ -28,8 +27,7 @@ FileStream::FileStream(NetLog* net_log,
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. */
- : open_flags_(base::PLATFORM_FILE_ASYNC),
- bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
context_(new Context(bound_net_log_,
base::WorkerPool::GetTaskRunner(true /* slow */))) {
bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
@@ -39,29 +37,40 @@ FileStream::FileStream(base::PlatformFile file,
int flags,
NetLog* net_log,
const scoped_refptr<base::TaskRunner>& task_runner)
- : open_flags_(flags),
- bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
- context_(new Context(file, bound_net_log_, open_flags_, task_runner)) {
+ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ context_(new Context(base::File(file), flags, bound_net_log_,
+ task_runner)) {
bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
}
FileStream::FileStream(base::PlatformFile file, int flags, NetLog* net_log)
- : open_flags_(flags),
- bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
- context_(new Context(file,
- bound_net_log_,
- open_flags_,
+ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ context_(new Context(base::File(file), flags, bound_net_log_,
+ base::WorkerPool::GetTaskRunner(true /* slow */))) {
+ bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
+}
+
+FileStream::FileStream(base::File file,
+ net::NetLog* net_log,
+ const scoped_refptr<base::TaskRunner>& task_runner)
+ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ context_(new Context(file.Pass(), bound_net_log_, task_runner)) {
+ bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
+}
+
+FileStream::FileStream(base::File file, net::NetLog* net_log)
+ : bound_net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_FILESTREAM)),
+ context_(new Context(file.Pass(), bound_net_log_,
base::WorkerPool::GetTaskRunner(true /* slow */))) {
bound_net_log_.BeginEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
}
FileStream::~FileStream() {
- if (!is_async()) {
- base::ThreadRestrictions::AssertIOAllowed();
+ if (context_->async()) {
+ context_.release()->Orphan();
+ } else {
context_->CloseSync();
context_.reset();
- } else {
- context_.release()->Orphan();
}
bound_net_log_.EndEvent(NetLog::TYPE_FILE_STREAM_ALIVE);
@@ -74,8 +83,7 @@ int FileStream::Open(const base::FilePath& path, int open_flags,
return ERR_UNEXPECTED;
}
- open_flags_ = open_flags;
- DCHECK(is_async());
+ DCHECK(open_flags & base::File::FLAG_ASYNC);
context_->OpenAsync(path, open_flags, callback);
return ERR_IO_PENDING;
}
@@ -88,26 +96,25 @@ int FileStream::OpenSync(const base::FilePath& path, int open_flags) {
return ERR_UNEXPECTED;
}
- open_flags_ = open_flags;
- DCHECK(!is_async());
- return context_->OpenSync(path, open_flags_);
+ DCHECK(!context_->async());
+ return context_->OpenSync(path, open_flags);
}
int FileStream::Close(const CompletionCallback& callback) {
- DCHECK(is_async());
+ DCHECK(context_->async());
context_->CloseAsync(callback);
return ERR_IO_PENDING;
}
int FileStream::CloseSync() {
- DCHECK(!is_async());
+ DCHECK(!context_->async());
base::ThreadRestrictions::AssertIOAllowed();
context_->CloseSync();
return OK;
}
bool FileStream::IsOpen() const {
- return context_->file() != base::kInvalidPlatformFileValue;
+ return context_->file().IsValid();
}
int FileStream::Seek(Whence whence,
@@ -117,7 +124,7 @@ int FileStream::Seek(Whence whence,
return ERR_UNEXPECTED;
// Make sure we're async.
- DCHECK(is_async());
+ DCHECK(context_->async());
context_->SeekAsync(whence, offset, callback);
return ERR_IO_PENDING;
}
@@ -129,7 +136,7 @@ int64 FileStream::SeekSync(Whence whence, int64 offset) {
return ERR_UNEXPECTED;
// If we're in async, make sure we don't have a request in flight.
- DCHECK(!is_async() || !context_->async_in_progress());
+ DCHECK(!context_->async() || !context_->async_in_progress());
return context_->SeekSync(whence, offset);
}
@@ -159,8 +166,7 @@ int FileStream::Read(IOBuffer* buf,
// read(..., 0) will return 0, which indicates end-of-file.
DCHECK_GT(buf_len, 0);
- DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
- DCHECK(is_async());
+ DCHECK(context_->async());
return context_->ReadAsync(buf, buf_len, callback);
}
@@ -171,10 +177,9 @@ int FileStream::ReadSync(char* buf, int buf_len) {
if (!IsOpen())
return ERR_UNEXPECTED;
- DCHECK(!is_async());
+ DCHECK(!context_->async());
// read(..., 0) will return 0, which indicates end-of-file.
DCHECK_GT(buf_len, 0);
- DCHECK(open_flags_ & base::PLATFORM_FILE_READ);
return context_->ReadSync(buf, buf_len);
}
@@ -208,8 +213,7 @@ int FileStream::Write(IOBuffer* buf,
if (!IsOpen())
return ERR_UNEXPECTED;
- DCHECK(is_async());
- DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
+ DCHECK(context_->async());
// write(..., 0) will return 0, which indicates end-of-file.
DCHECK_GT(buf_len, 0);
@@ -222,8 +226,7 @@ int FileStream::WriteSync(const char* buf, int buf_len) {
if (!IsOpen())
return ERR_UNEXPECTED;
- DCHECK(!is_async());
- DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
+ DCHECK(!context_->async());
// write(..., 0) will return 0, which indicates end-of-file.
DCHECK_GT(buf_len, 0);
@@ -236,9 +239,6 @@ int64 FileStream::Truncate(int64 bytes) {
if (!IsOpen())
return ERR_UNEXPECTED;
- // We'd better be open for writing.
- DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
-
// Seek to the position to truncate from.
int64 seek_position = SeekSync(FROM_BEGIN, bytes);
if (seek_position != bytes)
@@ -252,9 +252,8 @@ int FileStream::Flush(const CompletionCallback& callback) {
if (!IsOpen())
return ERR_UNEXPECTED;
- DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
// Make sure we're async.
- DCHECK(is_async());
+ DCHECK(context_->async());
context_->FlushAsync(callback);
return ERR_IO_PENDING;
@@ -266,7 +265,6 @@ int FileStream::FlushSync() {
if (!IsOpen())
return ERR_UNEXPECTED;
- DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE);
return context_->FlushSync();
}
@@ -291,7 +289,7 @@ void FileStream::SetBoundNetLogSource(const BoundNetLog& owner_bound_net_log) {
bound_net_log_.source().ToEventParametersCallback());
}
-base::PlatformFile FileStream::GetPlatformFileForTesting() {
+const base::File& FileStream::GetFileForTesting() const {
return context_->file();
}
diff --git a/net/base/file_stream.h b/net/base/file_stream.h
index 9fe2747..da62937 100644
--- a/net/base/file_stream.h
+++ b/net/base/file_stream.h
@@ -10,6 +10,7 @@
#ifndef NET_BASE_FILE_STREAM_H_
#define NET_BASE_FILE_STREAM_H_
+#include "base/files/file.h"
#include "base/platform_file.h"
#include "base/task_runner.h"
#include "net/base/completion_callback.h"
@@ -45,14 +46,23 @@ class NET_EXPORT FileStream {
// Uses |task_runner| for asynchronous operations.
// Note: the new FileStream object takes ownership of the PlatformFile and
// will close it on destruction.
+ // This constructor is deprecated.
+ // TODO(rvargas): remove all references to PlatformFile.
FileStream(base::PlatformFile file,
int flags,
net::NetLog* net_log,
const scoped_refptr<base::TaskRunner>& task_runner);
// Same as above, but runs async tasks in base::WorkerPool.
+ // This constructor is deprecated.
FileStream(base::PlatformFile file, int flags, net::NetLog* net_log);
+ // Non-deprecated versions of the previous two constructors.
+ FileStream(base::File file,
+ net::NetLog* net_log,
+ const scoped_refptr<base::TaskRunner>& task_runner);
+ FileStream(base::File file, net::NetLog* net_log);
+
// The underlying file is closed automatically.
virtual ~FileStream();
@@ -239,22 +249,18 @@ class NET_EXPORT FileStream {
void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log);
// Returns the underlying platform file for testing.
- base::PlatformFile GetPlatformFileForTesting();
+ const base::File& GetFileForTesting() const;
private:
class Context;
- bool is_async() const { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); }
-
- int open_flags_;
net::BoundNetLog bound_net_log_;
- // Context performing I/O operations. It was extracted into separate class
+ // Context performing I/O operations. It was extracted into a separate class
// to perform asynchronous operations because FileStream can be destroyed
- // before completion of async operation. Also if async FileStream is destroyed
- // without explicit closing file should be closed asynchronously without
- // delaying FileStream's destructor. To perform all that separate object is
- // necessary.
+ // before completion of an async operation. Also if a FileStream is destroyed
+ // without explicitly calling Close, the file should be closed asynchronously
+ // without delaying FileStream's destructor.
scoped_ptr<Context> context_;
DISALLOW_COPY_AND_ASSIGN(FileStream);
diff --git a/net/base/file_stream_context.cc b/net/base/file_stream_context.cc
index e7fe100..43825e3 100644
--- a/net/base/file_stream_context.cc
+++ b/net/base/file_stream_context.cc
@@ -41,27 +41,44 @@ FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError(
return IOResult(MapSystemError(os_error), os_error);
}
-FileStream::Context::OpenResult::OpenResult()
- : file(base::kInvalidPlatformFileValue) {
+// ---------------------------------------------------------------------
+
+FileStream::Context::OpenResult::OpenResult() {
}
-FileStream::Context::OpenResult::OpenResult(base::PlatformFile file,
+FileStream::Context::OpenResult::OpenResult(base::File file,
IOResult error_code)
- : file(file),
+ : file(file.Pass()),
error_code(error_code) {
}
+FileStream::Context::OpenResult::OpenResult(RValue other)
+ : file(other.object->file.Pass()),
+ error_code(other.object->error_code) {
+}
+
+FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=(
+ RValue other) {
+ if (this != other.object) {
+ file = other.object->file.Pass();
+ error_code = other.object->error_code;
+ }
+ return *this;
+}
+
+// ---------------------------------------------------------------------
+
void FileStream::Context::Orphan() {
DCHECK(!orphaned_);
orphaned_ = true;
- if (file_ != base::kInvalidPlatformFileValue)
+ if (file_.IsValid())
bound_net_log_.EndEvent(NetLog::TYPE_FILE_STREAM_OPEN);
if (!async_in_progress_) {
CloseAndDelete();
- } else if (file_ != base::kInvalidPlatformFileValue) {
- CancelIo(file_);
+ } else if (file_.IsValid()) {
+ CancelIo(file_.GetPlatformFile());
}
}
@@ -69,10 +86,9 @@ void FileStream::Context::OpenAsync(const base::FilePath& path,
int open_flags,
const CompletionCallback& callback) {
DCHECK(!async_in_progress_);
-
BeginOpenEvent(path);
- const bool posted = base::PostTaskAndReplyWithResult(
+ bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(
@@ -81,6 +97,13 @@ 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) {
@@ -88,30 +111,29 @@ int FileStream::Context::OpenSync(const base::FilePath& path, int open_flags) {
BeginOpenEvent(path);
OpenResult result = OpenFileImpl(path, open_flags);
- file_ = result.file;
- if (file_ == base::kInvalidPlatformFileValue) {
- ProcessOpenError(result.error_code);
- } else {
+ 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::PLATFORM_FILE_ASYNC)
+ 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_ != base::kInvalidPlatformFileValue) {
- base::ClosePlatformFile(file_);
- file_ = base::kInvalidPlatformFileValue;
+ if (file_.IsValid()) {
+ file_.Close();
bound_net_log_.EndEvent(NetLog::TYPE_FILE_STREAM_OPEN);
}
}
void FileStream::Context::CloseAsync(const CompletionCallback& callback) {
DCHECK(!async_in_progress_);
- const bool posted = base::PostTaskAndReplyWithResult(
+ bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(&Context::CloseFileImpl, base::Unretained(this)),
@@ -129,7 +151,7 @@ void FileStream::Context::SeekAsync(Whence whence,
const Int64CompletionCallback& callback) {
DCHECK(!async_in_progress_);
- const bool posted = base::PostTaskAndReplyWithResult(
+ bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(
@@ -152,7 +174,7 @@ int64 FileStream::Context::SeekSync(Whence whence, int64 offset) {
void FileStream::Context::FlushAsync(const CompletionCallback& callback) {
DCHECK(!async_in_progress_);
- const bool posted = base::PostTaskAndReplyWithResult(
+ bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(&Context::FlushFileImpl, base::Unretained(this)),
@@ -197,12 +219,16 @@ void FileStream::Context::BeginOpenEvent(const base::FilePath& path) {
FileStream::Context::OpenResult FileStream::Context::OpenFileImpl(
const base::FilePath& path, int open_flags) {
- base::PlatformFile file;
+#if defined(OS_POSIX)
+ // Always use blocking IO.
+ open_flags &= ~base::File::FLAG_ASYNC;
+#endif
+ base::File file;
#if defined(OS_ANDROID)
if (path.IsContentUri()) {
// Check that only Read flags are set.
- DCHECK_EQ(open_flags & ~base::PLATFORM_FILE_ASYNC,
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
+ DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC,
+ base::File::FLAG_OPEN | base::File::FLAG_READ);
file = base::OpenContentUriForRead(path);
} else {
#endif // defined(OS_ANDROID)
@@ -210,15 +236,22 @@ FileStream::Context::OpenResult FileStream::Context::OpenFileImpl(
// independently from FileStream's destructor. It can cause problems for
// users wanting to delete the file right after FileStream deletion. Thus
// we are always adding SHARE_DELETE flag to accommodate such use case.
- open_flags |= base::PLATFORM_FILE_SHARE_DELETE;
- file = base::CreatePlatformFile(path, open_flags, NULL, NULL);
+ // TODO(rvargas): This sounds like a bug, as deleting the file would
+ // presumably happen on the wrong thread. There should be an async delete.
+ open_flags |= base::File::FLAG_SHARE_DELETE;
+ file.Initialize(path, open_flags);
#if defined(OS_ANDROID)
}
#endif // defined(OS_ANDROID)
- if (file == base::kInvalidPlatformFileValue)
- return OpenResult(file, IOResult::FromOSError(GetLastErrno()));
+ if (!file.IsValid())
+ return OpenResult(base::File(), IOResult::FromOSError(GetLastErrno()));
+
+ return OpenResult(file.Pass(), IOResult(OK, 0));
+}
- return OpenResult(file, IOResult(OK, 0));
+FileStream::Context::IOResult FileStream::Context::CloseFileImpl() {
+ file_.Close();
+ return IOResult(OK, 0);
}
void FileStream::Context::ProcessOpenError(const IOResult& error_code) {
@@ -228,26 +261,27 @@ void FileStream::Context::ProcessOpenError(const IOResult& error_code) {
void FileStream::Context::OnOpenCompleted(const CompletionCallback& callback,
OpenResult open_result) {
- file_ = open_result.file;
- if (file_ == base::kInvalidPlatformFileValue)
+ if (!open_result.file.IsValid()) {
ProcessOpenError(open_result.error_code);
- else if (!orphaned_)
+ } else if (!orphaned_) {
+ file_ = open_result.file.Pass();
OnAsyncFileOpened();
+ }
OnAsyncCompleted(IntToInt64(callback), open_result.error_code.result);
}
void FileStream::Context::CloseAndDelete() {
DCHECK(!async_in_progress_);
- if (file_ == base::kInvalidPlatformFileValue) {
- delete this;
- } else {
- const bool posted = task_runner_->PostTaskAndReply(
+ if (file_.IsValid()) {
+ bool posted = task_runner_.get()->PostTaskAndReply(
FROM_HERE,
- base::Bind(base::IgnoreResult(&base::ClosePlatformFile), file_),
+ base::Bind(base::IgnoreResult(&Context::CloseFileImpl),
+ base::Unretained(this)),
base::Bind(&Context::OnCloseCompleted, base::Unretained(this)));
DCHECK(posted);
- file_ = base::kInvalidPlatformFileValue;
+ } else {
+ delete this;
}
}
diff --git a/net/base/file_stream_context.h b/net/base/file_stream_context.h
index 3ce8a5b6..e7711c9 100644
--- a/net/base/file_stream_context.h
+++ b/net/base/file_stream_context.h
@@ -27,8 +27,9 @@
#ifndef NET_BASE_FILE_STREAM_CONTEXT_H_
#define NET_BASE_FILE_STREAM_CONTEXT_H_
+#include "base/files/file.h"
#include "base/message_loop/message_loop.h"
-#include "base/platform_file.h"
+#include "base/move.h"
#include "base/task_runner.h"
#include "net/base/completion_callback.h"
#include "net/base/file_stream.h"
@@ -61,9 +62,15 @@ class FileStream::Context {
Context(const BoundNetLog& bound_net_log,
const scoped_refptr<base::TaskRunner>& task_runner);
- Context(base::PlatformFile file,
+ 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,
- int open_flags,
const scoped_refptr<base::TaskRunner>& task_runner);
#if defined(OS_WIN)
virtual ~Context();
@@ -90,8 +97,9 @@ class FileStream::Context {
////////////////////////////////////////////////////////////////////////////
void set_record_uma(bool value) { record_uma_ = value; }
- base::PlatformFile file() const { return file_; }
+ 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.
@@ -134,9 +142,15 @@ class FileStream::Context {
};
struct OpenResult {
+ MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue)
+ public:
OpenResult();
- OpenResult(base::PlatformFile file, IOResult error_code);
- base::PlatformFile file;
+ OpenResult(base::File file, IOResult error_code);
+ // C++03 move emulation of this type.
+ OpenResult(RValue other);
+ OpenResult& operator=(RValue other);
+
+ base::File file;
IOResult error_code;
};
@@ -147,6 +161,8 @@ class FileStream::Context {
OpenResult OpenFileImpl(const base::FilePath& path, int open_flags);
+ IOResult CloseFileImpl();
+
void ProcessOpenError(const IOResult& result);
void OnOpenCompleted(const CompletionCallback& callback,
OpenResult open_result);
@@ -193,9 +209,6 @@ class FileStream::Context {
// Flushes all data written to the stream.
IOResult FlushFileImpl();
- // Closes the file.
- IOResult CloseFileImpl();
-
#if defined(OS_WIN)
void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf);
@@ -214,10 +227,11 @@ class FileStream::Context {
IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
#endif
- base::PlatformFile file_;
+ 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_;
diff --git a/net/base/file_stream_context_posix.cc b/net/base/file_stream_context_posix.cc
index 6e6bc6e..ac2c75e 100644
--- a/net/base/file_stream_context_posix.cc
+++ b/net/base/file_stream_context_posix.cc
@@ -46,22 +46,35 @@ COMPILE_ASSERT(FROM_BEGIN == SEEK_SET &&
FileStream::Context::Context(const BoundNetLog& bound_net_log,
const scoped_refptr<base::TaskRunner>& task_runner)
- : file_(base::kInvalidPlatformFileValue),
+ : 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),
orphaned_(false),
+ async_(false),
bound_net_log_(bound_net_log),
task_runner_(task_runner) {
}
-FileStream::Context::Context(base::PlatformFile file,
+FileStream::Context::Context(base::File file,
+ int flags,
const BoundNetLog& bound_net_log,
- int /* open_flags */,
const scoped_refptr<base::TaskRunner>& task_runner)
- : file_(file),
+ : 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) {
}
@@ -71,7 +84,7 @@ FileStream::Context::~Context() {
int64 FileStream::Context::GetFileSize() const {
struct stat info;
- if (fstat(file_, &info) != 0) {
+ if (fstat(file_.GetPlatformFile(), &info) != 0) {
IOResult result = IOResult::FromOSError(errno);
RecordError(result, FILE_ERROR_SOURCE_GET_SIZE);
return result.result;
@@ -135,7 +148,7 @@ int FileStream::Context::WriteSync(const char* in_buf, int buf_len) {
}
int FileStream::Context::Truncate(int64 bytes) {
- if (ftruncate(file_, bytes) != 0) {
+ if (ftruncate(file_.GetPlatformFile(), bytes) != 0) {
IOResult result = IOResult::FromOSError(errno);
RecordError(result, FILE_ERROR_SOURCE_SET_EOF);
return result.result;
@@ -146,7 +159,7 @@ int FileStream::Context::Truncate(int64 bytes) {
FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
int64 offset) {
- off_t res = lseek(file_, static_cast<off_t>(offset),
+ off_t res = lseek(file_.GetPlatformFile(), static_cast<off_t>(offset),
static_cast<int>(whence));
if (res == static_cast<off_t>(-1))
return IOResult::FromOSError(errno);
@@ -155,7 +168,7 @@ FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
}
FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
- ssize_t res = HANDLE_EINTR(fsync(file_));
+ ssize_t res = HANDLE_EINTR(fsync(file_.GetPlatformFile()));
if (res == -1)
return IOResult::FromOSError(errno);
@@ -166,7 +179,7 @@ FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
scoped_refptr<IOBuffer> buf,
int buf_len) {
// Loop in the case of getting interrupted by a signal.
- ssize_t res = HANDLE_EINTR(read(file_, buf->data(),
+ ssize_t res = HANDLE_EINTR(read(file_.GetPlatformFile(), buf->data(),
static_cast<size_t>(buf_len)));
if (res == -1)
return IOResult::FromOSError(errno);
@@ -177,20 +190,12 @@ FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
scoped_refptr<IOBuffer> buf,
int buf_len) {
- ssize_t res = HANDLE_EINTR(write(file_, buf->data(), buf_len));
+ ssize_t res = HANDLE_EINTR(write(file_.GetPlatformFile(), buf->data(),
+ buf_len));
if (res == -1)
return IOResult::FromOSError(errno);
return IOResult(res, 0);
}
-FileStream::Context::IOResult FileStream::Context::CloseFileImpl() {
- bool success = base::ClosePlatformFile(file_);
- file_ = base::kInvalidPlatformFileValue;
- if (!success)
- return IOResult::FromOSError(errno);
-
- return IOResult(OK, 0);
-}
-
} // namespace net
diff --git a/net/base/file_stream_context_win.cc b/net/base/file_stream_context_win.cc
index 4a666c7..cb7b41c 100644
--- a/net/base/file_stream_context_win.cc
+++ b/net/base/file_stream_context_win.cc
@@ -40,10 +40,10 @@ void IncrementOffset(OVERLAPPED* overlapped, DWORD count) {
FileStream::Context::Context(const BoundNetLog& bound_net_log,
const scoped_refptr<base::TaskRunner>& task_runner)
: io_context_(),
- file_(base::kInvalidPlatformFileValue),
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) {
@@ -51,24 +51,41 @@ FileStream::Context::Context(const BoundNetLog& bound_net_log,
memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped));
}
-FileStream::Context::Context(base::PlatformFile file,
+FileStream::Context::Context(base::File file,
const BoundNetLog& bound_net_log,
- int open_flags,
const scoped_refptr<base::TaskRunner>& task_runner)
: io_context_(),
- file_(file),
+ 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_ != base::kInvalidPlatformFileValue &&
- (open_flags & base::PLATFORM_FILE_ASYNC)) {
+ 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))
OnAsyncFileOpened();
- }
}
FileStream::Context::~Context() {
@@ -76,7 +93,7 @@ FileStream::Context::~Context() {
int64 FileStream::Context::GetFileSize() const {
LARGE_INTEGER file_size;
- if (!GetFileSizeEx(file_, &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);
@@ -93,7 +110,7 @@ int FileStream::Context::ReadAsync(IOBuffer* buf,
error_source_ = FILE_ERROR_SOURCE_READ;
DWORD bytes_read;
- if (!ReadFile(file_, buf->data(), buf_len,
+ if (!ReadFile(file_.GetPlatformFile(), buf->data(), buf_len,
&bytes_read, &io_context_.overlapped)) {
IOResult error = IOResult::FromOSError(GetLastError());
if (error.os_error == ERROR_IO_PENDING) {
@@ -113,7 +130,7 @@ int FileStream::Context::ReadAsync(IOBuffer* buf,
int FileStream::Context::ReadSync(char* buf, int buf_len) {
DWORD bytes_read;
- if (!ReadFile(file_, buf, buf_len, &bytes_read, NULL)) {
+ 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.
@@ -133,7 +150,7 @@ int FileStream::Context::WriteAsync(IOBuffer* buf,
error_source_ = FILE_ERROR_SOURCE_WRITE;
DWORD bytes_written = 0;
- if (!WriteFile(file_, buf->data(), buf_len,
+ if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
&bytes_written, &io_context_.overlapped)) {
IOResult error = IOResult::FromOSError(GetLastError());
if (error.os_error == ERROR_IO_PENDING) {
@@ -151,7 +168,7 @@ int FileStream::Context::WriteAsync(IOBuffer* buf,
int FileStream::Context::WriteSync(const char* buf, int buf_len) {
DWORD bytes_written = 0;
- if (!WriteFile(file_, buf, buf_len, &bytes_written, NULL)) {
+ 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);
@@ -162,7 +179,7 @@ int FileStream::Context::WriteSync(const char* buf, int buf_len) {
}
int FileStream::Context::Truncate(int64 bytes) {
- if (!SetEndOfFile(file_)) {
+ if (!SetEndOfFile(file_.GetPlatformFile())) {
IOResult error = IOResult::FromOSError(GetLastError());
LOG(WARNING) << "SetEndOfFile failed: " << error.os_error;
RecordError(error, FILE_ERROR_SOURCE_SET_EOF);
@@ -173,7 +190,8 @@ int FileStream::Context::Truncate(int64 bytes) {
}
void FileStream::Context::OnAsyncFileOpened() {
- base::MessageLoopForIO::current()->RegisterIOHandler(file_, this);
+ base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(),
+ this);
}
FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
@@ -181,7 +199,7 @@ FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
LARGE_INTEGER distance, res;
distance.QuadPart = offset;
DWORD move_method = static_cast<DWORD>(whence);
- if (SetFilePointerEx(file_, distance, &res, move_method)) {
+ if (SetFilePointerEx(file_.GetPlatformFile(), distance, &res, move_method)) {
SetOffset(&io_context_.overlapped, res);
return IOResult(res.QuadPart, 0);
}
@@ -190,16 +208,7 @@ FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
}
FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
- if (FlushFileBuffers(file_))
- return IOResult(OK, 0);
-
- return IOResult::FromOSError(GetLastError());
-}
-
-FileStream::Context::IOResult FileStream::Context::CloseFileImpl() {
- bool success = base::ClosePlatformFile(file_);
- file_ = base::kInvalidPlatformFileValue;
- if (success)
+ if (FlushFileBuffers(file_.GetPlatformFile()))
return IOResult(OK, 0);
return IOResult::FromOSError(GetLastError());
diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc
index d86010d..4252c51 100644
--- a/net/base/file_stream_unittest.cc
+++ b/net/base/file_stream_unittest.cc
@@ -7,10 +7,10 @@
#include "base/bind.h"
#include "base/callback.h"
#include "base/file_util.h"
+#include "base/files/file.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/path_service.h"
-#include "base/platform_file.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
@@ -66,117 +66,65 @@ class FileStreamTest : public PlatformTest {
namespace {
-TEST_F(FileStreamTest, BasicOpenClose) {
- base::PlatformFile file = base::kInvalidPlatformFileValue;
- {
- FileStream stream(NULL, base::MessageLoopProxy::current());
- int rv = stream.OpenSync(temp_file_path(),
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
- EXPECT_EQ(OK, rv);
- EXPECT_TRUE(stream.IsOpen());
- file = stream.GetPlatformFileForTesting();
- }
- EXPECT_NE(base::kInvalidPlatformFileValue, file);
- base::PlatformFileInfo info;
- // The file should be closed.
- EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
-}
-
TEST_F(FileStreamTest, BasicOpenExplicitClose) {
- base::PlatformFile file = base::kInvalidPlatformFileValue;
FileStream stream(NULL);
int rv = stream.OpenSync(temp_file_path(),
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ);
+ base::File::FLAG_OPEN | base::File::FLAG_READ);
EXPECT_EQ(OK, rv);
EXPECT_TRUE(stream.IsOpen());
- file = stream.GetPlatformFileForTesting();
- EXPECT_NE(base::kInvalidPlatformFileValue, file);
+ EXPECT_TRUE(stream.GetFileForTesting().IsValid());
EXPECT_EQ(OK, stream.CloseSync());
EXPECT_FALSE(stream.IsOpen());
- base::PlatformFileInfo info;
- // The file should be closed.
- EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
+ EXPECT_FALSE(stream.GetFileForTesting().IsValid());
}
TEST_F(FileStreamTest, AsyncOpenExplicitClose) {
- base::PlatformFile file = base::kInvalidPlatformFileValue;
TestCompletionCallback callback;
FileStream stream(NULL);
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN |
+ base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(stream.IsOpen());
- file = stream.GetPlatformFileForTesting();
+ EXPECT_TRUE(stream.GetFileForTesting().IsValid());
EXPECT_EQ(ERR_IO_PENDING, stream.Close(callback.callback()));
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_FALSE(stream.IsOpen());
- base::PlatformFileInfo info;
- // The file should be closed.
- EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
+ EXPECT_FALSE(stream.GetFileForTesting().IsValid());
}
TEST_F(FileStreamTest, AsyncOpenExplicitCloseOrphaned) {
- base::PlatformFile file = base::kInvalidPlatformFileValue;
TestCompletionCallback callback;
- base::PlatformFileInfo info;
scoped_ptr<FileStream> stream(new FileStream(
NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(stream->IsOpen());
- file = stream->GetPlatformFileForTesting();
+ EXPECT_TRUE(stream->GetFileForTesting().IsValid());
EXPECT_EQ(ERR_IO_PENDING, stream->Close(callback.callback()));
stream.reset();
// File isn't actually closed yet.
- EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
base::RunLoop runloop;
runloop.RunUntilIdle();
// The file should now be closed, though the callback has not been called.
- EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
-}
-
-TEST_F(FileStreamTest, FileHandleNotLeftOpen) {
- bool created = false;
- ASSERT_EQ(kTestDataSize,
- base::WriteFile(temp_file_path(), kTestData, kTestDataSize));
- int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ;
- base::PlatformFile file = base::CreatePlatformFile(
- temp_file_path(), flags, &created, NULL);
-
- {
- // Seek to the beginning of the file and read.
- FileStream read_stream(file, flags, NULL,
- base::MessageLoopProxy::current());
- EXPECT_TRUE(read_stream.IsOpen());
- }
-
- EXPECT_NE(base::kInvalidPlatformFileValue, file);
- base::PlatformFileInfo info;
- // The file should be closed.
- EXPECT_FALSE(base::GetPlatformFileInfo(file, &info));
}
// Test the use of FileStream with a file handle provided at construction.
TEST_F(FileStreamTest, UseFileHandle) {
- bool created = false;
-
// 1. Test reading with a file handle.
ASSERT_EQ(kTestDataSize,
- base::WriteFile(temp_file_path(), kTestData, kTestDataSize));
- int flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ;
- base::PlatformFile file = base::CreatePlatformFile(
- temp_file_path(), flags, &created, NULL);
+ base::WriteFile(temp_file_path(), kTestData, kTestDataSize));
+ int flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ;
+ base::File file(temp_file_path(), flags);
// Seek to the beginning of the file and read.
scoped_ptr<FileStream> read_stream(
- new FileStream(file, flags, NULL, base::MessageLoopProxy::current()));
+ new FileStream(file.Pass(), NULL, base::MessageLoopProxy::current()));
ASSERT_EQ(0, read_stream->SeekSync(FROM_BEGIN, 0));
ASSERT_EQ(kTestDataSize, read_stream->Available());
// Read into buffer and compare.
@@ -188,11 +136,11 @@ TEST_F(FileStreamTest, UseFileHandle) {
// 2. Test writing with a file handle.
base::DeleteFile(temp_file_path(), false);
- flags = base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE;
- file = base::CreatePlatformFile(temp_file_path(), flags, &created, NULL);
+ flags = base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE;
+ file.Initialize(temp_file_path(), flags);
scoped_ptr<FileStream> write_stream(
- new FileStream(file, flags, NULL, base::MessageLoopProxy::current()));
+ 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));
@@ -225,12 +173,10 @@ TEST_F(FileStreamTest, UseClosedStream) {
TEST_F(FileStreamTest, BasicRead) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
int rv = stream.OpenSync(temp_file_path(), flags);
EXPECT_EQ(OK, rv);
@@ -255,13 +201,11 @@ TEST_F(FileStreamTest, BasicRead) {
TEST_F(FileStreamTest, AsyncRead) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -290,14 +234,12 @@ TEST_F(FileStreamTest, AsyncRead) {
TEST_F(FileStreamTest, AsyncRead_EarlyDelete) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -321,12 +263,10 @@ TEST_F(FileStreamTest, AsyncRead_EarlyDelete) {
TEST_F(FileStreamTest, BasicRead_FromOffset) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
int rv = stream.OpenSync(temp_file_path(), flags);
EXPECT_EQ(OK, rv);
@@ -356,13 +296,11 @@ TEST_F(FileStreamTest, BasicRead_FromOffset) {
TEST_F(FileStreamTest, AsyncRead_FromOffset) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -398,8 +336,7 @@ TEST_F(FileStreamTest, AsyncRead_FromOffset) {
TEST_F(FileStreamTest, SeekAround) {
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
int rv = stream.OpenSync(temp_file_path(), flags);
EXPECT_EQ(OK, rv);
@@ -421,9 +358,8 @@ TEST_F(FileStreamTest, SeekAround) {
TEST_F(FileStreamTest, AsyncSeekAround) {
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_ASYNC |
- base::PLATFORM_FILE_READ;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_ASYNC |
+ base::File::FLAG_READ;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -458,38 +394,33 @@ TEST_F(FileStreamTest, AsyncSeekAround) {
TEST_F(FileStreamTest, BasicWrite) {
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_CREATE_ALWAYS |
- base::PLATFORM_FILE_WRITE;
+ 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;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ 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();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ 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::PLATFORM_FILE_CREATE_ALWAYS |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(0, file_size);
int total_bytes_written = 0;
@@ -498,8 +429,8 @@ TEST_F(FileStreamTest, AsyncWrite) {
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
- rv = stream.Write(
- drainable.get(), drainable->BytesRemaining(), callback.callback());
+ rv = stream.Write(drainable.get(), drainable->BytesRemaining(),
+ callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
@@ -508,25 +439,22 @@ TEST_F(FileStreamTest, AsyncWrite) {
drainable->DidConsume(rv);
total_bytes_written += rv;
}
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(file_size, total_bytes_written);
}
TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) {
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_CREATE_ALWAYS |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_EQ(OK, callback.WaitForResult());
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(0, file_size);
scoped_refptr<IOBufferWithSize> buf = CreateTestDataBuffer();
@@ -538,8 +466,7 @@ TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) {
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback.have_result());
} else {
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(file_size, rv);
}
}
@@ -547,14 +474,12 @@ TEST_F(FileStreamTest, AsyncWrite_EarlyDelete) {
TEST_F(FileStreamTest, BasicWrite_FromOffset) {
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_WRITE;
+ 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;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize, file_size);
const int64 kOffset = 0;
@@ -565,20 +490,17 @@ TEST_F(FileStreamTest, BasicWrite_FromOffset) {
EXPECT_EQ(kTestDataSize, rv);
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
TEST_F(FileStreamTest, AsyncWrite_FromOffset) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -597,8 +519,8 @@ TEST_F(FileStreamTest, AsyncWrite_FromOffset) {
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
- rv = stream.Write(
- drainable.get(), drainable->BytesRemaining(), callback.callback());
+ rv = stream.Write(drainable.get(), drainable->BytesRemaining(),
+ callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
@@ -607,21 +529,18 @@ TEST_F(FileStreamTest, AsyncWrite_FromOffset) {
drainable->DidConsume(rv);
total_bytes_written += rv;
}
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(file_size, kTestDataSize * 2);
}
TEST_F(FileStreamTest, BasicReadWrite) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE;
+ 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);
@@ -647,21 +566,18 @@ TEST_F(FileStreamTest, BasicReadWrite) {
EXPECT_EQ(kTestDataSize, rv);
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
TEST_F(FileStreamTest, BasicWriteRead) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE;
+ 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);
@@ -691,8 +607,7 @@ TEST_F(FileStreamTest, BasicWriteRead) {
}
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
@@ -703,15 +618,12 @@ TEST_F(FileStreamTest, BasicWriteRead) {
TEST_F(FileStreamTest, BasicAsyncReadWrite) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -743,8 +655,8 @@ TEST_F(FileStreamTest, BasicAsyncReadWrite) {
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
- rv = stream->Write(
- drainable.get(), drainable->BytesRemaining(), callback.callback());
+ rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
+ callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
@@ -756,22 +668,18 @@ TEST_F(FileStreamTest, BasicAsyncReadWrite) {
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
TEST_F(FileStreamTest, BasicAsyncWriteRead) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream->Open(temp_file_path(), flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -792,8 +700,8 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) {
scoped_refptr<DrainableIOBuffer> drainable =
new DrainableIOBuffer(buf.get(), buf->size());
while (total_bytes_written != kTestDataSize) {
- rv = stream->Write(
- drainable.get(), drainable->BytesRemaining(), callback.callback());
+ rv = stream->Write(drainable.get(), drainable->BytesRemaining(),
+ callback.callback());
if (rv == ERR_IO_PENDING)
rv = callback.WaitForResult();
EXPECT_LT(0, rv);
@@ -826,8 +734,7 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) {
}
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
@@ -930,15 +837,12 @@ class TestWriteReadCompletionCallback {
TEST_F(FileStreamTest, AsyncWriteRead) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -965,8 +869,7 @@ TEST_F(FileStreamTest, AsyncWriteRead) {
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
EXPECT_EQ(kTestDataSize * 2, total_bytes_read);
@@ -1040,15 +943,12 @@ class TestWriteCloseCompletionCallback {
TEST_F(FileStreamTest, AsyncWriteClose) {
int64 file_size;
- bool ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -1072,14 +972,13 @@ TEST_F(FileStreamTest, AsyncWriteClose) {
stream.reset();
- ok = base::GetFileSize(temp_file_path(), &file_size);
- EXPECT_TRUE(ok);
+ EXPECT_TRUE(base::GetFileSize(temp_file_path(), &file_size));
EXPECT_EQ(kTestDataSize * 2, file_size);
}
// Tests truncating a file.
TEST_F(FileStreamTest, Truncate) {
- int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE;
+ int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE;
scoped_ptr<FileStream> write_stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
@@ -1108,9 +1007,8 @@ TEST_F(FileStreamTest, Truncate) {
TEST_F(FileStreamTest, AsyncOpenAndDelete) {
scoped_ptr<FileStream> stream(
new FileStream(NULL, base::MessageLoopProxy::current()));
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
+ base::File::FLAG_ASYNC;
TestCompletionCallback open_callback;
int rv = stream->Open(temp_file_path(), flags, open_callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
@@ -1126,18 +1024,14 @@ TEST_F(FileStreamTest, AsyncOpenAndDelete) {
// Verify that async Write() errors are mapped correctly.
TEST_F(FileStreamTest, AsyncWriteError) {
// Try opening file as read-only and then writing to it using FileStream.
- base::PlatformFile file = base::CreatePlatformFile(
- temp_file_path(),
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC,
- NULL,
- NULL);
- ASSERT_NE(base::kInvalidPlatformFileValue, file);
-
- int flags = base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC;
+ uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
+
+ base::File file(temp_file_path(), flags);
+ ASSERT_TRUE(file.IsValid());
+
scoped_ptr<FileStream> stream(
- new FileStream(file, flags, NULL, base::MessageLoopProxy::current()));
+ new FileStream(file.Pass(), NULL, base::MessageLoopProxy::current()));
scoped_refptr<IOBuffer> buf = new IOBuffer(1);
buf->data()[0] = 0;
@@ -1148,24 +1042,21 @@ TEST_F(FileStreamTest, AsyncWriteError) {
rv = callback.WaitForResult();
EXPECT_LT(rv, 0);
- base::ClosePlatformFile(file);
+ stream.reset();
+ base::RunLoop().RunUntilIdle();
}
// Verify that async Read() errors are mapped correctly.
TEST_F(FileStreamTest, AsyncReadError) {
// Try opening file for write and then reading from it using FileStream.
- base::PlatformFile file = base::CreatePlatformFile(
- temp_file_path(),
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
- base::PLATFORM_FILE_ASYNC,
- NULL,
- NULL);
- ASSERT_NE(base::kInvalidPlatformFileValue, file);
-
- int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ uint32 flags = base::File::FLAG_OPEN | base::File::FLAG_WRITE |
+ base::File::FLAG_ASYNC;
+
+ base::File file(temp_file_path(), flags);
+ ASSERT_TRUE(file.IsValid());
+
scoped_ptr<FileStream> stream(
- new FileStream(file, flags, NULL, base::MessageLoopProxy::current()));
+ new FileStream(file.Pass(), NULL, base::MessageLoopProxy::current()));
scoped_refptr<IOBuffer> buf = new IOBuffer(1);
TestCompletionCallback callback;
@@ -1174,7 +1065,8 @@ TEST_F(FileStreamTest, AsyncReadError) {
rv = callback.WaitForResult();
EXPECT_LT(rv, 0);
- base::ClosePlatformFile(file);
+ stream.reset();
+ base::RunLoop().RunUntilIdle();
}
#if defined(OS_ANDROID)
@@ -1197,9 +1089,8 @@ TEST_F(FileStreamTest, ContentUriAsyncRead) {
EXPECT_LT(0, file_size);
FileStream stream(NULL, base::MessageLoopProxy::current());
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
TestCompletionCallback callback;
int rv = stream.Open(path, flags, callback.callback());
EXPECT_EQ(ERR_IO_PENDING, rv);
diff --git a/net/base/upload_file_element_reader.cc b/net/base/upload_file_element_reader.cc
index 87c276a..75dd422 100644
--- a/net/base/upload_file_element_reader.cc
+++ b/net/base/upload_file_element_reader.cc
@@ -53,8 +53,8 @@ int UploadFileElementReader::Init(const CompletionCallback& callback) {
file_stream_.reset(new FileStream(NULL, task_runner_.get()));
int result = file_stream_->Open(
path_,
- base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC,
+ base::File::FLAG_OPEN | base::File::FLAG_READ |
+ base::File::FLAG_ASYNC,
base::Bind(&UploadFileElementReader::OnOpenCompleted,
weak_ptr_factory_.GetWeakPtr(),
callback));
diff --git a/net/url_request/url_fetcher_response_writer.cc b/net/url_request/url_fetcher_response_writer.cc
index 7d3c271..7db614f 100644
--- a/net/url_request/url_fetcher_response_writer.cc
+++ b/net/url_request/url_fetcher_response_writer.cc
@@ -80,8 +80,8 @@ int URLFetcherFileWriter::Initialize(const CompletionCallback& callback) {
} else {
result = file_stream_->Open(
file_path_,
- base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC |
- base::PLATFORM_FILE_CREATE_ALWAYS,
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC |
+ base::File::FLAG_CREATE_ALWAYS,
base::Bind(&URLFetcherFileWriter::DidOpenFile,
weak_factory_.GetWeakPtr(),
callback));
@@ -162,8 +162,8 @@ void URLFetcherFileWriter::DidCreateTempFile(const CompletionCallback& callback,
owns_file_ = true;
const int result = file_stream_->Open(
file_path_,
- base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC |
- base::PLATFORM_FILE_OPEN,
+ base::File::FLAG_WRITE | base::File::FLAG_ASYNC |
+ base::File::FLAG_OPEN,
base::Bind(&URLFetcherFileWriter::DidOpenFile,
weak_factory_.GetWeakPtr(),
callback));
diff --git a/net/url_request/url_request_file_job.cc b/net/url_request/url_request_file_job.cc
index f0963b6..7db8d81 100644
--- a/net/url_request/url_request_file_job.cc
+++ b/net/url_request/url_request_file_job.cc
@@ -229,9 +229,9 @@ void URLRequestFileJob::DidFetchMetaInfo(const FileMetaInfo* meta_info) {
return;
}
- int flags = base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_READ |
- base::PLATFORM_FILE_ASYNC;
+ int flags = base::File::FLAG_OPEN |
+ base::File::FLAG_READ |
+ base::File::FLAG_ASYNC;
int rv = stream_->Open(file_path_, flags,
base::Bind(&URLRequestFileJob::DidOpen,
weak_ptr_factory_.GetWeakPtr()));