1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 // vim:cindent:tabstop=4:expandtab:shiftwidth=4:
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsLayoutDebugCLH.h"
8 #include "nsArray.h"
9 #include "nsString.h"
10 #include "plstr.h"
11 #include "nsCOMPtr.h"
12 #include "nsIWindowWatcher.h"
13 #include "nsIServiceManager.h"
14 #include "nsIDOMWindow.h"
15 #include "nsISupportsPrimitives.h"
16 #include "nsICommandLine.h"
17 
nsLayoutDebugCLH()18 nsLayoutDebugCLH::nsLayoutDebugCLH() {}
19 
~nsLayoutDebugCLH()20 nsLayoutDebugCLH::~nsLayoutDebugCLH() {}
21 
NS_IMPL_ISUPPORTS(nsLayoutDebugCLH,ICOMMANDLINEHANDLER)22 NS_IMPL_ISUPPORTS(nsLayoutDebugCLH, ICOMMANDLINEHANDLER)
23 
24 NS_IMETHODIMP
25 nsLayoutDebugCLH::Handle(nsICommandLine* aCmdLine) {
26   nsresult rv;
27 
28   int32_t idx;
29   rv = aCmdLine->FindFlag(NS_LITERAL_STRING("layoutdebug"), false, &idx);
30   NS_ENSURE_SUCCESS(rv, rv);
31   if (idx < 0) return NS_OK;
32 
33   int32_t length;
34   aCmdLine->GetLength(&length);
35 
36   nsAutoString url;
37   if (idx + 1 < length) {
38     rv = aCmdLine->GetArgument(idx + 1, url);
39     NS_ENSURE_SUCCESS(rv, rv);
40     if (!url.IsEmpty() && url.CharAt(0) == '-') url.Truncate();
41   }
42 
43   aCmdLine->RemoveArguments(idx, idx + !url.IsEmpty());
44 
45   nsCOMPtr<nsIMutableArray> argsArray = nsArray::Create();
46 
47   if (!url.IsEmpty()) {
48     nsCOMPtr<nsISupportsString> scriptableURL =
49         do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
50     NS_ENSURE_TRUE(scriptableURL, NS_ERROR_FAILURE);
51 
52     scriptableURL->SetData(url);
53     argsArray->AppendElement(scriptableURL);
54   }
55 
56   nsCOMPtr<nsIWindowWatcher> wwatch =
57       do_GetService(NS_WINDOWWATCHER_CONTRACTID);
58   NS_ENSURE_TRUE(wwatch, NS_ERROR_FAILURE);
59 
60   nsCOMPtr<mozIDOMWindowProxy> opened;
61   wwatch->OpenWindow(nullptr, "chrome://layoutdebug/content/", "_blank",
62                      "chrome,dialog=no,all", argsArray, getter_AddRefs(opened));
63   aCmdLine->SetPreventDefault(true);
64   return NS_OK;
65 }
66 
67 NS_IMETHODIMP
GetHelpInfo(nsACString & aResult)68 nsLayoutDebugCLH::GetHelpInfo(nsACString& aResult) {
69   aResult.AssignLiteral("  -layoutdebug [<url>] Start with Layout Debugger\n");
70   return NS_OK;
71 }
72