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


CrOS-Libchrome-Original-Commit: a0830591020c23ad911aa287390a4ae942c5dc41
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 @@
       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 @@
 
 // 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 @@
   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 @@
   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 @@
   stat_wrapper_t file_info;
 #if defined(OS_ANDROID)
   if (file_path.IsContentUri()) {
-    ScopedFD fd(OpenContentUriForRead(file_path));
-    if (!fd.is_valid())
+    File file = OpenContentUriForRead(file_path);
+    if (!file.IsValid())
       return false;
-    if (CallFstat(fd.get(), &file_info) != 0)
-      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 @@
 
   // 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 @@
   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..dc7616b 100644
--- a/base/files/file.h
+++ b/base/files/file.h
@@ -265,6 +265,8 @@
   // 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 @@
 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 @@
     }
   }
 
-  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 @@
   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 @@
   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 @@
     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, 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 @@
 
 // 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 @@
             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 @@
   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 @@
 }
 
 #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"));