Bug 1363482: Part 8 - Preload preferences files off-thread during startup. r?erahm
MozReview-Commit-ID: 8NsxRw5KcZY
--- 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