Bug 1363482: Part 8 - Preload preferences files off-thread during startup. r?erahm draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 12 May 2017 15:13:00 -0700
changeset 656271 182584e5dbc0c2f2f702ea82f3201fb7ad1ab883
parent 656270 87d3fab70d220b04756bc85195a699687e6163c7
child 656272 188b268214e7058af2eb39daf079e086b9112ed9
push id77140
push usermaglione.k@gmail.com
push dateWed, 30 Aug 2017 23:07:13 +0000
reviewerserahm
bugs1363482
milestone57.0a1
Bug 1363482: Part 8 - Preload preferences files off-thread during startup. r?erahm MozReview-Commit-ID: 8NsxRw5KcZY
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -5,19 +5,21 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/dom/PContent.h"
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/HashFunctions.h"
+#include "mozilla/ScopeExit.h"
 #include "mozilla/ServoStyleSet.h"
 #include "mozilla/SyncRunnable.h"
 #include "mozilla/Telemetry.h"
+#include "mozilla/URLPreloader.h"
 #include "mozilla/UniquePtrExtensions.h"
 
 #include "nsXULAppAPI.h"
 
 #include "mozilla/Preferences.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsDataHashtable.h"
 #include "nsDirectoryServiceDefs.h"
@@ -1260,56 +1262,34 @@ Preferences::WritePrefFile(nsIFile* aFil
   // as AllowOffMainThreadSave() returns a consistent value for the
   // lifetime of the parent process.
   PrefSaveData prefsData = pref_savePrefs(gHashTable);
   return PreferencesWriter::Write(aFile, prefsData);
 }
 
 static nsresult openPrefFile(nsIFile* aFile)
 {
-  nsCOMPtr<nsIInputStream> inStr;
-
-  nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), aFile);
-  if (NS_FAILED(rv))
-    return rv;
-
-  int64_t fileSize64;
-  rv = aFile->GetFileSize(&fileSize64);
-  if (NS_FAILED(rv))
-    return rv;
-  NS_ENSURE_TRUE(fileSize64 <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
-
-  uint32_t fileSize = (uint32_t)fileSize64;
-  auto fileBuffer = MakeUniqueFallible<char[]>(fileSize);
-  if (fileBuffer == nullptr)
-    return NS_ERROR_OUT_OF_MEMORY;
-
   PrefParseState ps;
   PREF_InitParseState(&ps, PREF_ReaderCallback, ReportToConsole, nullptr);
+  auto cleanup = MakeScopeExit([&] () {
+    PREF_FinalizeParseState(&ps);
+  });
 
-  // Read is not guaranteed to return a buf the size of fileSize,
-  // but usually will.
-  nsresult rv2 = NS_OK;
-  uint32_t offset = 0;
-  for (;;) {
-    uint32_t amtRead = 0;
-    rv = inStr->Read(fileBuffer.get(), fileSize, &amtRead);
-    if (NS_FAILED(rv) || amtRead == 0)
-      break;
-    if (!PREF_ParseBuf(&ps, fileBuffer.get(), amtRead))
-      rv2 = NS_ERROR_FILE_CORRUPTED;
-    offset += amtRead;
-    if (offset == fileSize) {
-      break;
-    }
+  auto result = URLPreloader::ReadFile(aFile);
+  if (result.isErr()) {
+    return result.unwrapErr();
   }
 
-  PREF_FinalizeParseState(&ps);
+  const auto& data = result.unwrap();
+  if (!PREF_ParseBuf(&ps, data.get(), data.Length())) {
+    return NS_ERROR_FILE_CORRUPTED;
+  }
 
-  return NS_FAILED(rv) ? rv : rv2;
+  return NS_OK;
+
 }
 
 /*
  * some stuff that gets called from Pref_Init()
  */
 
 static int
 pref_CompareFileNames(nsIFile* aFile1, nsIFile* aFile2, void* /*unused*/)
@@ -1456,22 +1436,25 @@ static nsresult pref_LoadPrefsInDirList(
     else
       pref_LoadPrefsInDir(path, nullptr, 0);
   }
   return NS_OK;
 }
 
 static nsresult pref_ReadPrefFromJar(nsZipArchive* jarReader, const char *name)
 {
-  nsZipItemPtr<char> manifest(jarReader, name, true);
-  NS_ENSURE_TRUE(manifest.Buffer(), NS_ERROR_NOT_AVAILABLE);
+  auto result = URLPreloader::ReadZip(jarReader, nsDependentCString(name));
+  if (result.isErr()) {
+    return NS_ERROR_NOT_AVAILABLE;
+  }
+  const auto& manifest = result.unwrap();
 
   PrefParseState ps;
   PREF_InitParseState(&ps, PREF_ReaderCallback, ReportToConsole, nullptr);
-  PREF_ParseBuf(&ps, manifest, manifest.Length());
+  PREF_ParseBuf(&ps, manifest.get(), manifest.Length());
   PREF_FinalizeParseState(&ps);
 
   return NS_OK;
 }
 
 //----------------------------------------------------------------------------------------
 // Initialize default preference JavaScript buffers from
 // appropriate TEXT resources