Bug 1358846: Part 6 - Clean up error handling. r=me
☠☠ backed out by ebad7f51280d ☠ ☠
authorKris Maglione <maglione.k@gmail.com>
Sat, 29 Apr 2017 19:03:40 -0700
changeset 358054 7dcb80a051a3c6d70561b2965795cdc886b6028b
parent 358053 26825f1e33dd2ef48bef737d97d9ba9af700417f
child 358055 b533d7f9b9c2c574175cbdd6a965bd85133a25ef
push id31808
push usercbook@mozilla.com
push dateFri, 12 May 2017 12:37:49 +0000
treeherdermozilla-central@030c0a7c8781 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersme
bugs1358846
milestone55.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 1358846: Part 6 - Clean up error handling. r=me MozReview-Commit-ID: ApF7H3NTIU8
toolkit/mozapps/extensions/AddonManagerStartup.cpp
--- a/toolkit/mozapps/extensions/AddonManagerStartup.cpp
+++ b/toolkit/mozapps/extensions/AddonManagerStartup.cpp
@@ -22,16 +22,50 @@
 #include "nsAppRunner.h"
 #include "nsIAddonInterposition.h"
 #include "nsXULAppAPI.h"
 
 #include <stdlib.h>
 
 namespace mozilla {
 
+template <>
+class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>
+{
+  nsresult mErrorValue;
+
+  template<typename V, typename E2> friend class Result;
+
+public:
+  explicit GenericErrorResult(nsresult aErrorValue) : mErrorValue(aErrorValue) {}
+
+  operator nsresult() { return mErrorValue; }
+};
+
+static inline Result<Ok, nsresult>
+WrapNSResult(PRStatus aRv)
+{
+    if (aRv != PR_SUCCESS) {
+        return Err(NS_ERROR_FAILURE);
+    }
+    return Ok();
+}
+
+static inline Result<Ok, nsresult>
+WrapNSResult(nsresult aRv)
+{
+    if (NS_FAILED(aRv)) {
+        return Err(aRv);
+    }
+    return Ok();
+}
+
+#define NS_TRY(expr) MOZ_TRY(WrapNSResult(expr))
+
+
 using Compression::LZ4;
 
 #ifdef XP_WIN
 #  define READ_BINARYMODE "rb"
 #else
 #  define READ_BINARYMODE "r"
 #endif
 
@@ -114,49 +148,49 @@ ReadFile(const char* path)
     result.Truncate();
   }
 
   return result;
 }
 
 /**
  * Reads the contents of a LZ4-compressed file, as stored by the OS.File
- * module, and stores the decompressed contents in result.
+ * module, and returns the decompressed contents on success.
  *
- * Returns true on success, or false on failure. A nonexistent or empty file
- * is treated as success. A corrupt or non-LZ4 file is treated as failure.
+ * A nonexistent or empty file is treated as success. A corrupt or non-LZ4
+ * file is treated as failure.
  */
-static bool
-ReadFileLZ4(const char* path, nsCString& result)
+static Result<nsCString, nsresult>
+ReadFileLZ4(const char* path)
 {
   static const char MAGIC_NUMBER[] = "mozLz40";
   constexpr auto HEADER_SIZE = sizeof(MAGIC_NUMBER) + 4;
 
+  nsCString result;
+
   nsCString lz4 = ReadFile(path);
   if (lz4.IsEmpty()) {
-    result.Truncate();
-    return true;
+    return result;
   }
 
   // Note: We want to include the null terminator here.
   nsDependentCSubstring magic(MAGIC_NUMBER, sizeof(MAGIC_NUMBER));
 
   if (lz4.Length() < HEADER_SIZE || StringHead(lz4, magic.Length()) != magic) {
-    return false;
+    return Err(NS_ERROR_UNEXPECTED);
   }
 
   auto size = LittleEndian::readUint32(lz4.get() + magic.Length());
 
   if (!result.SetLength(size, fallible) ||
       !LZ4::decompress(lz4.get() + HEADER_SIZE, result.BeginWriting(), size)) {
-    result.Truncate();
-    return false;
+    return Err(NS_ERROR_UNEXPECTED);
   }
 
-  return true;
+  return result;
 }
 
 
 static bool
 ParseJSON(JSContext* cx, nsACString& jsonData, JS::MutableHandleValue result)
 {
   NS_ConvertUTF8toUTF16 str(jsonData);
   jsonData.Truncate();
@@ -468,30 +502,25 @@ AddonManagerStartup::AddInstallLocation(
     nsCOMPtr<nsIFile> manifest = CloneAndAppend(file, "chrome.manifest");
     XRE_AddManifestLocation(type, manifest);
   }
 }
 
 nsresult
 AddonManagerStartup::ReadStartupData(JSContext* cx, JS::MutableHandleValue locations)
 {
-  nsresult rv;
-
   locations.set(JS::UndefinedValue());
 
   nsCOMPtr<nsIFile> file = CloneAndAppend(ProfileDir(), "addonStartup.json.lz4");
 
   nsCString path;
-  rv = file->GetNativePath(path);
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_TRY(file->GetNativePath(path));
 
   nsCString data;
-  if (!ReadFileLZ4(path.get(), data)) {
-    return NS_ERROR_FAILURE;
-  }
+  MOZ_TRY_VAR(data, ReadFileLZ4(path.get()));
 
   if (data.IsEmpty() || !ParseJSON(cx, data, locations)) {
     return NS_OK;
   }
 
   if (!locations.isObject()) {
     return NS_ERROR_UNEXPECTED;
   }