Bug 1363482: Part 10 - Preload addonStartup.json off-thread during startup. r=rhelmer
authorKris Maglione <maglione.k@gmail.com>
Fri, 12 May 2017 14:58:02 -0700
changeset 427919 a9a24a28013bb346dc75044d02d4266842349e6c
parent 427918 2f14f9759a376c1c9c0173e47b649cab47c9215e
child 427920 3e55e4de032f16f2e6cda431c602d600f2d87f75
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrhelmer
bugs1363482
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1363482: Part 10 - Preload addonStartup.json off-thread during startup. r=rhelmer MozReview-Commit-ID: 4jYpr9kXKq9
toolkit/mozapps/extensions/AddonManagerStartup.cpp
--- a/toolkit/mozapps/extensions/AddonManagerStartup.cpp
+++ b/toolkit/mozapps/extensions/AddonManagerStartup.cpp
@@ -14,16 +14,17 @@
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/EndianUtils.h"
 #include "mozilla/Compression.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/ResultExtensions.h"
 #include "mozilla/ScopeExit.h"
 #include "mozilla/Services.h"
+#include "mozilla/URLPreloader.h"
 #include "mozilla/Unused.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/dom/ipc/StructuredCloneData.h"
 
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsAppRunner.h"
 #include "nsContentUtils.h"
 #include "nsChromeRegistry.h"
@@ -99,38 +100,16 @@ CloneAndAppend(nsIFile* aFile, const cha
 
 static bool
 IsNormalFile(nsIFile* file)
 {
   bool result;
   return NS_SUCCEEDED(file->IsFile(&result)) && result;
 }
 
-static Result<nsCString, nsresult>
-ReadFile(nsIFile* file)
-{
-  nsCString result;
-
-  AutoFDClose fd;
-  MOZ_TRY(file->OpenNSPRFileDesc(PR_RDONLY, 0, &fd.rwget()));
-
-  auto size = PR_Seek64(fd, 0, PR_SEEK_END);
-  PR_Seek64(fd, 0, PR_SEEK_SET);
-
-  result.SetLength(size);
-
-  auto len = PR_Read(fd, result.BeginWriting(), size);
-
-  if (size != len) {
-    return Err(NS_ERROR_FAILURE);
-  }
-
-  return result;
-}
-
 static const char STRUCTURED_CLONE_MAGIC[] = "mozJSSCLz40v001";
 
 template <typename T>
 static Result<nsCString, nsresult>
 DecodeLZ4(const nsACString& lz4, const T& magicNumber)
 {
   constexpr auto HEADER_SIZE = sizeof(magicNumber) + 4;
 
@@ -184,29 +163,24 @@ EncodeLZ4(const nsACString& data, const 
 }
 
 static_assert(sizeof STRUCTURED_CLONE_MAGIC % 8 == 0,
               "Magic number should be an array of uint64_t");
 
 /**
  * Reads the contents of a LZ4-compressed file, as stored by the OS.File
  * module, and returns the decompressed contents on success.
- *
- * A nonexistent or empty file is treated as success. A corrupt or non-LZ4
- * file is treated as failure.
  */
 static Result<nsCString, nsresult>
 ReadFileLZ4(nsIFile* file)
 {
   static const char MAGIC_NUMBER[] = "mozLz40";
 
-  nsCString result;
-
   nsCString lz4;
-  MOZ_TRY_VAR(lz4, ReadFile(file));
+  MOZ_TRY_VAR(lz4, URLPreloader::ReadFile(file));
 
   if (lz4.IsEmpty()) {
     return lz4;
   }
 
   return DecodeLZ4(lz4, MAGIC_NUMBER);
 }
 
@@ -581,17 +555,22 @@ AddonManagerStartup::AddInstallLocation(
 nsresult
 AddonManagerStartup::ReadStartupData(JSContext* cx, JS::MutableHandleValue locations)
 {
   locations.set(JS::UndefinedValue());
 
   nsCOMPtr<nsIFile> file = CloneAndAppend(ProfileDir(), "addonStartup.json.lz4");
 
   nsCString data;
-  MOZ_TRY_VAR(data, ReadFileLZ4(file));
+  auto res = ReadFileLZ4(file);
+  if (res.isOk()) {
+    data = res.unwrap();
+  } else if (res.unwrapErr() != NS_ERROR_FILE_NOT_FOUND) {
+    return res.unwrapErr();
+  }
 
   if (data.IsEmpty() || !ParseJSON(cx, data, locations)) {
     return NS_OK;
   }
 
   if (!locations.isObject()) {
     return NS_ERROR_UNEXPECTED;
   }