base: Introduce generic base::PreReadFile().

https://crrev.com/c/1741782 introduced base::win::PreReadFile(). This CL
moves the function to base::PreReadFile(), and adds a fadvise()-based
implementation for Linux, ChromeOS and Android L+.

Bug: 1001838
Change-Id: Ib81175284d883db34535bf80fc13b4f26f0ba1ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1781513
Commit-Queue: Victor Costan <[email protected]>
Reviewed-by: Greg Thompson <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Cr-Commit-Position: refs/heads/master@{#694610}
diff --git a/sql/database.cc b/sql/database.cc
index d5b4b5bd..964fd61 100644
--- a/sql/database.cc
+++ b/sql/database.cc
@@ -36,10 +36,6 @@
 #include "sql/vfs_wrapper.h"
 #include "third_party/sqlite/sqlite3.h"
 
-#if defined(OS_WIN)
-#include "base/win/file_pre_reader.h"
-#endif
-
 namespace {
 
 // Spin for up to a second waiting for the lock to clear when setting
@@ -348,9 +344,6 @@
 }
 
 void Database::Preload() {
-  base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
-  InitScopedBlockingCall(&scoped_blocking_call);
-
   if (base::FeatureList::IsEnabled(features::kSqlSkipPreload))
     return;
 
@@ -359,9 +352,9 @@
     return;
   }
 
-#if defined(OS_WIN)
-  base::win::PreReadFile(DbPath(), false);
-#else
+  base::Optional<base::ScopedBlockingCall> scoped_blocking_call;
+  InitScopedBlockingCall(&scoped_blocking_call);
+
   // The constructor and set_page_size() ensure that page_size_ is never zero.
   const int page_size = page_size_;
   DCHECK(page_size);
@@ -372,25 +365,7 @@
   if (preload_size < 1)
     return;
 
-  sqlite3_file* file = nullptr;
-  sqlite3_int64 file_size = 0;
-  int rc = GetSqlite3FileAndSize(db_, &file, &file_size);
-  if (rc != SQLITE_OK)
-    return;
-
-  // Don't preload more than the file contains.
-  if (preload_size > file_size)
-    preload_size = file_size;
-
-  std::unique_ptr<char[]> buf(new char[page_size]);
-  for (sqlite3_int64 pos = 0; pos < preload_size; pos += page_size) {
-    rc = file->pMethods->xRead(file, buf.get(), page_size, pos);
-
-    // TODO(shess): Consider calling OnSqliteError().
-    if (rc != SQLITE_OK)
-      return;
-  }
-#endif
+  base::PreReadFile(DbPath(), /*is_executable=*/false, preload_size);
 }
 
 // SQLite keeps unused pages associated with a database in a cache.  It asks