Bug 1569077 - Part 1: Refactor layout debugger command line handling. r=dbaron
authorCameron McCormack <cam@mcc.id.au>
Sun, 25 Aug 2019 23:38:26 +0000
changeset 553567 8a7e48b3e117939cab046dac642c601e8b2eb4ef
parent 553566 1429658f80ca4dc9d2e02fbfa9fdafe7e10b1393
child 553568 38eb6450911610c73a6aee5f80ca1035e30caef6
push id2165
push userffxbld-merge
push dateMon, 14 Oct 2019 16:30:58 +0000
treeherdermozilla-release@0eae18af659f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs1569077
milestone70.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 1569077 - Part 1: Refactor layout debugger command line handling. r=dbaron Differential Revision: https://phabricator.services.mozilla.com/D39467
layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp
--- a/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp
+++ b/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp
@@ -17,51 +17,94 @@
 #include "nsIURI.h"
 
 nsLayoutDebugCLH::nsLayoutDebugCLH() {}
 
 nsLayoutDebugCLH::~nsLayoutDebugCLH() {}
 
 NS_IMPL_ISUPPORTS(nsLayoutDebugCLH, ICOMMANDLINEHANDLER)
 
-NS_IMETHODIMP
-nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) {
+static nsresult HandleFlagWithOptionalArgument(nsICommandLine* aCmdLine,
+                                               const nsAString& aName,
+                                               const nsAString& aDefaultValue,
+                                               nsAString& aValue,
+                                               bool& aFlagPresent) {
+  aValue = EmptyString();
+  aFlagPresent = false;
+
   nsresult rv;
+  int32_t idx;
 
-  int32_t idx;
-  rv = aCmdLine->FindFlag(NS_LITERAL_STRING("layoutdebug"), false, &idx);
+  rv = aCmdLine->FindFlag(aName, false, &idx);
   NS_ENSURE_SUCCESS(rv, rv);
   if (idx < 0) return NS_OK;
 
+  aFlagPresent = true;
+
   int32_t length;
   aCmdLine->GetLength(&length);
 
-  nsAutoString url;
+  bool argPresent = false;
+
   if (idx + 1 < length) {
-    rv = aCmdLine->GetArgument(idx + 1, url);
+    rv = aCmdLine->GetArgument(idx + 1, aValue);
     NS_ENSURE_SUCCESS(rv, rv);
-    if (!url.IsEmpty() && url.CharAt(0) == '-') url.Truncate();
+
+    if (!aValue.IsEmpty() && aValue.CharAt(0) == '-') {
+      aValue = EmptyString();
+    } else {
+      argPresent = true;
+    }
+  }
+
+  if (!argPresent) {
+    aValue = aDefaultValue;
   }
 
-  aCmdLine->RemoveArguments(idx, idx + !url.IsEmpty());
+  return aCmdLine->RemoveArguments(idx, idx + argPresent);
+}
+
+static nsresult AppendArg(nsIMutableArray* aArray, const nsAString& aString) {
+  nsCOMPtr<nsISupportsString> s =
+      do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
+  NS_ENSURE_TRUE(s, NS_ERROR_FAILURE);
+  s->SetData(aString);
+  return aArray->AppendElement(s);
+}
+
+NS_IMETHODIMP
+nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) {
+  nsresult rv;
+  bool flagPresent;
+
+  nsString url;
+
+  rv =
+      HandleFlagWithOptionalArgument(aCmdLine, NS_LITERAL_STRING("layoutdebug"),
+                                     EmptyString(), url, flagPresent);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!flagPresent) {
+    return NS_OK;
+  }
 
   nsCOMPtr<nsIMutableArray> argsArray = nsArray::Create();
 
   if (!url.IsEmpty()) {
     nsCOMPtr<nsIURI> uri;
+    nsAutoCString resolvedSpec;
+
     rv = aCmdLine->ResolveURI(url, getter_AddRefs(uri));
     NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsISupportsString> scriptableURL =
-        do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
-    NS_ENSURE_TRUE(scriptableURL, NS_ERROR_FAILURE);
-    nsAutoCString resolvedSpec;
+
     rv = uri->GetSpec(resolvedSpec);
     NS_ENSURE_SUCCESS(rv, rv);
-    scriptableURL->SetData(NS_ConvertUTF8toUTF16(resolvedSpec));
-    argsArray->AppendElement(scriptableURL);
+
+    rv = AppendArg(argsArray, NS_ConvertUTF8toUTF16(resolvedSpec));
+    NS_ENSURE_SUCCESS(rv, rv);
   }
 
   nsCOMPtr<nsIWindowWatcher> wwatch =
       do_GetService(NS_WINDOWWATCHER_CONTRACTID);
   NS_ENSURE_TRUE(wwatch, NS_ERROR_FAILURE);
 
   nsCOMPtr<mozIDOMWindowProxy> opened;
   wwatch->OpenWindow(nullptr, "chrome://layoutdebug/content/", "_blank",