From 712f28410189dbcd7774e8e29cd452cec3291f04 Mon Sep 17 00:00:00 2001 From: Cameron McCormack Date: Sun, 25 Aug 2019 23:38:28 +0000 Subject: [PATCH] Bug 1569077 - Part 2: Add --autoclose argument to the layout debugger. r=dbaron Differential Revision: https://phabricator.services.mozilla.com/D39468 --HG-- extra : moz-landing-system : lando --- .../layout-debug/src/nsLayoutDebugCLH.cpp | 41 ++++++++++++++++++- .../layout-debug/ui/content/layoutdebug.js | 37 ++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp b/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp index e6b34f0a7867..5b2ae0cb5c09 100644 --- a/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp +++ b/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp @@ -62,6 +62,27 @@ static nsresult HandleFlagWithOptionalArgument(nsICommandLine* aCmdLine, return aCmdLine->RemoveArguments(idx, idx + argPresent); } +static nsresult HandleFlagWithOptionalArgument(nsICommandLine* aCmdLine, + const nsAString& aName, + double aDefaultValue, + double& aValue, + bool& aFlagPresent) { + nsresult rv; + nsString s; + + rv = HandleFlagWithOptionalArgument(aCmdLine, aName, NS_LITERAL_STRING("0"), + s, aFlagPresent); + NS_ENSURE_SUCCESS(rv, rv); + + if (!aFlagPresent) { + aValue = 0.0; + return NS_OK; + } + + aValue = s.ToDouble(&rv); + return rv; +} + static nsresult AppendArg(nsIMutableArray* aArray, const nsAString& aString) { nsCOMPtr s = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID); @@ -76,6 +97,8 @@ nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) { bool flagPresent; nsString url; + bool autoclose = false; + double delay = 0.0; rv = HandleFlagWithOptionalArgument(aCmdLine, NS_LITERAL_STRING("layoutdebug"), @@ -86,6 +109,10 @@ nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) { return NS_OK; } + rv = HandleFlagWithOptionalArgument(aCmdLine, NS_LITERAL_STRING("autoclose"), + 0.0, delay, autoclose); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr argsArray = nsArray::Create(); if (!url.IsEmpty()) { @@ -102,6 +129,14 @@ nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) { NS_ENSURE_SUCCESS(rv, rv); } + if (autoclose) { + nsString arg; + arg.AppendPrintf("autoclose=%f", delay); + + rv = AppendArg(argsArray, arg); + NS_ENSURE_SUCCESS(rv, rv); + } + nsCOMPtr wwatch = do_GetService(NS_WINDOWWATCHER_CONTRACTID); NS_ENSURE_TRUE(wwatch, NS_ERROR_FAILURE); @@ -115,6 +150,10 @@ nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) { NS_IMETHODIMP nsLayoutDebugCLH::GetHelpInfo(nsACString& aResult) { - aResult.AssignLiteral(" --layoutdebug [] Start with Layout Debugger\n"); + aResult.AssignLiteral( + " --layoutdebug [] Start with Layout Debugger\n" + " --autoclose [] Automatically close the Layout Debugger once\n" + " the page has loaded, after delaying the specified\n" + " number of seconds (which defaults to 0).\n"); return NS_OK; } diff --git a/layout/tools/layout-debug/ui/content/layoutdebug.js b/layout/tools/layout-debug/ui/content/layoutdebug.js index acc4994f5c36..768c64d48115 100644 --- a/layout/tools/layout-debug/ui/content/layoutdebug.js +++ b/layout/tools/layout-debug/ui/content/layoutdebug.js @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +var gArgs; var gBrowser; var gURLBar; var gDebugger; @@ -170,6 +171,17 @@ nsLDBBrowserContentListener.prototype = { this.setButtonEnabled(this.mStopButton, false); this.mStatusText.value = gURLBar.value + " loaded"; this.mLoading = false; + if (gArgs.autoclose && gBrowser.currentURI.spec != "about:blank") { + // We check for about:blank just to avoid one or two STATE_STOP + // notifications that occur before the loadURI() call completes. + // This does mean that --autoclose doesn't work when the URL on + // the command line is about:blank (or not specified), but that's + // not a big deal. + setTimeout( + () => Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit), + gArgs.delay * 1000 + ); + } } }, @@ -213,6 +225,26 @@ nsLDBBrowserContentListener.prototype = { mLoading: false, }; +function parseArguments() { + let args = { + url: null, + autoclose: false, + delay: 0, + }; + if (window.arguments) { + args.url = window.arguments[0]; + for (let i = 1; i < window.arguments.length; ++i) { + if (/^autoclose=(.*)$/.test(window.arguments[i])) { + args.autoclose = true; + args.delay = +RegExp.$1; + } else { + throw `Unknown option ${window.arguments[i]}`; + } + } + } + return args; +} + function OnLDBLoad() { gBrowser = document.getElementById("browser"); gURLBar = document.getElementById("urlbar"); @@ -229,8 +261,9 @@ function OnLDBLoad() { return null; }; - if (window.arguments && window.arguments[0]) { - loadURI(window.arguments[0]); + gArgs = parseArguments(); + if (gArgs.url) { + loadURI(gArgs.url); } }