summaryrefslogtreecommitdiffstats
path: root/layout/tools/layout-debug/src/nsLayoutDebugCLH.cpp
blob: c50f3af44ac17861ce1d5b5e964412e8c65e9668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// vim:cindent:tabstop=4:expandtab:shiftwidth=4:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

#include "nsLayoutDebugCLH.h"
#include "mozIDOMWindow.h"
#include "nsArray.h"
#include "nsString.h"
#include "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsIWindowWatcher.h"
#include "nsISupportsPrimitives.h"
#include "nsICommandLine.h"
#include "nsIURI.h"
#include "nsServiceManagerUtils.h"

nsLayoutDebugCLH::nsLayoutDebugCLH() = default;

nsLayoutDebugCLH::~nsLayoutDebugCLH() = default;

NS_IMPL_ISUPPORTS(nsLayoutDebugCLH, ICOMMANDLINEHANDLER)

static nsresult HandleFlagWithOptionalArgument(nsICommandLine* aCmdLine,
                                               const nsAString& aName,
                                               const nsAString& aDefaultValue,
                                               nsAString& aValue,
                                               bool& aFlagPresent) {
  aValue.Truncate();
  aFlagPresent = false;

  nsresult rv;
  int32_t 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);

  bool argPresent = false;

  if (idx + 1 < length) {
    rv = aCmdLine->GetArgument(idx + 1, aValue);
    NS_ENSURE_SUCCESS(rv, rv);

    if (!aValue.IsEmpty() && aValue.CharAt(0) == '-') {
      aValue.Truncate();
    } else {
      argPresent = true;
    }
  }

  if (!argPresent) {
    aValue = aDefaultValue;
  }

  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, u"0"_ns, 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<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;
  bool autoclose = false;
  double delay = 0.0;
  bool captureProfile = false;
  nsString profileFilename;
  bool paged = false;

  rv = HandleFlagWithOptionalArgument(aCmdLine, u"layoutdebug"_ns,
                                      u"about:blank"_ns, url, flagPresent);
  NS_ENSURE_SUCCESS(rv, rv);

  if (!flagPresent) {
    return NS_OK;
  }

  rv = HandleFlagWithOptionalArgument(aCmdLine, u"autoclose"_ns, 0.0, delay,
                                      autoclose);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = HandleFlagWithOptionalArgument(aCmdLine, u"capture-profile"_ns,
                                      u"profile.json"_ns, profileFilename,
                                      captureProfile);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = aCmdLine->HandleFlag(u"paged"_ns, false, &paged);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIMutableArray> argsArray = nsArray::Create();

  nsCOMPtr<nsIURI> uri;
  nsAutoCString resolvedSpec;

  rv = aCmdLine->ResolveURI(url, getter_AddRefs(uri));
  NS_ENSURE_SUCCESS(rv, rv);

  rv = uri->GetSpec(resolvedSpec);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = AppendArg(argsArray, NS_ConvertUTF8toUTF16(resolvedSpec));
  NS_ENSURE_SUCCESS(rv, rv);

  if (autoclose) {
    nsString arg;
    arg.AppendPrintf("autoclose=%f", delay);

    rv = AppendArg(argsArray, arg);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  if (captureProfile) {
    nsString arg;
    arg.AppendLiteral("profile=");
    arg.Append(profileFilename);

    rv = AppendArg(argsArray, arg);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  if (paged) {
    rv = AppendArg(argsArray, u"paged"_ns);
    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/layoutdebug.xhtml"_ns, "_blank"_ns,
      "chrome,dialog=no,all"_ns, argsArray, getter_AddRefs(opened));
  aCmdLine->SetPreventDefault(true);
  return NS_OK;
}

NS_IMETHODIMP
nsLayoutDebugCLH::GetHelpInfo(nsACString& aResult) {
  aResult.AssignLiteral(
      "  --layoutdebug [<url>] Start with Layout Debugger\n"
      "  --autoclose [<seconds>] Automatically close the Layout Debugger once\n"
      "                     the page has loaded, after delaying the specified\n"
      "                     number of seconds (which defaults to 0).\n"
      "  --capture-profile [<filename>] Capture a profile of the Layout\n"
      "                     Debugger using the Gecko Profiler, and save the\n"
      "                     profile to the specified file (which defaults to\n"
      "                     profile.json).\n"
      "  --paged Layout the page in paginated mode.\n");
  return NS_OK;
}