summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/view-source.js
blob: bbbe921fb307e7a5ed5f360881945b5eb312a2fe (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 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/. */

"use strict";

/**
 * Tries to open a Stylesheet file in the Style Editor. If the file is not
 * found, it is opened in source view instead.
 * Returns a promise resolving to a boolean indicating whether or not
 * the source was able to be displayed in the StyleEditor, as the built-in
 * Firefox View Source is the fallback.
 *
 * @param {Toolbox} toolbox
 * @param {string|Object} stylesheetFrontOrGeneratedURL
 * @param {number} generatedLine
 * @param {number} generatedColumn
 *
 * @return {Promise<boolean>}
 */
exports.viewSourceInStyleEditor = async function(
  toolbox,
  stylesheetFrontOrGeneratedURL,
  generatedLine,
  generatedColumn
) {
  const originalPanelId = toolbox.currentToolId;

  try {
    const panel = await toolbox.selectTool("styleeditor", "view-source", {
      // This will be only used in case the styleeditor wasn't loaded yet, to make the
      // initialization faster in case we already have a stylesheet resource. We still
      // need the rest of this function to handle subsequent calls and sourcemapped stylesheets.
      stylesheetToSelect: {
        stylesheet: stylesheetFrontOrGeneratedURL,
        line: generatedLine,
        column: generatedColumn,
      },
    });

    let stylesheetFront;
    if (typeof stylesheetFrontOrGeneratedURL === "string") {
      stylesheetFront = panel.getStylesheetFrontForGeneratedURL(
        stylesheetFrontOrGeneratedURL
      );
    } else {
      stylesheetFront = stylesheetFrontOrGeneratedURL;
    }

    const originalLocation = stylesheetFront
      ? await getOriginalLocation(
          toolbox,
          stylesheetFront.resourceId,
          generatedLine,
          generatedColumn
        )
      : null;

    if (originalLocation) {
      await panel.selectOriginalSheet(
        originalLocation.sourceId,
        originalLocation.line,
        originalLocation.column
      );
      return true;
    }

    if (stylesheetFront) {
      await panel.selectStyleSheet(
        stylesheetFront,
        generatedLine,
        generatedColumn
      );
      return true;
    }
  } catch (e) {
    console.error("Failed to view source in style editor", e);
  }

  // If we weren't able to select the stylesheet in the style editor, display it in a
  // view-source tab
  exports.viewSource(
    toolbox,
    typeof stylesheetFrontOrGeneratedURL === "string"
      ? stylesheetFrontOrGeneratedURL
      : stylesheetFrontOrGeneratedURL.href ||
          stylesheetFrontOrGeneratedURL.nodeHref,
    generatedLine
  );

  // As we might have moved to the styleeditor, switch back to the original panel
  await toolbox.selectTool(originalPanelId);

  return false;
};

/**
 * Tries to open a JavaScript file in the Debugger. If the file is not found,
 * it is opened in source view instead. Either the source URL or source actor ID
 * can be specified. If both are specified, the source actor ID is used.
 *
 * Returns a promise resolving to a boolean indicating whether or not
 * the source was able to be displayed in the Debugger, as the built-in Firefox
 * View Source is the fallback.
 *
 * @param {Toolbox} toolbox
 * @param {string} sourceURL
 * @param {number} sourceLine
 * @param {number} sourceColumn
 * @param {string} sourceID
 * @param {(string|object)} [reason=unknown]
 *
 * @return {Promise<boolean>}
 */
exports.viewSourceInDebugger = async function(
  toolbox,
  generatedURL,
  generatedLine,
  generatedColumn,
  sourceActorId,
  reason = "unknown"
) {
  const location = await getViewSourceInDebuggerLocation(
    toolbox,
    generatedURL,
    generatedLine,
    generatedColumn,
    sourceActorId
  );

  if (location) {
    const { id, line, column } = location;

    const dbg = await toolbox.selectTool("jsdebugger", reason);
    try {
      await dbg.selectSource(id, sourceActorId, line, column);
      return true;
    } catch (err) {
      console.error("Failed to view source in debugger", err);
    }
  }

  exports.viewSource(toolbox, generatedURL, generatedLine);
  return false;
};

async function getViewSourceInDebuggerLocation(
  toolbox,
  generatedURL,
  generatedLine,
  generatedColumn,
  sourceActorId
) {
  const dbg = await toolbox.loadTool("jsdebugger");

  const generatedSource = sourceActorId
    ? dbg.getSourceByActorId(sourceActorId)
    : dbg.getSourceByURL(generatedURL);
  if (
    !generatedSource ||
    // Note: We're not entirely sure when this can happen, so we may want
    // to revisit that at some point.
    dbg.getSourceActorsForSource(generatedSource.id).length === 0
  ) {
    return null;
  }

  const generatedLocation = {
    id: generatedSource.id,
    line: generatedLine,
    column: generatedColumn,
  };

  const originalLocation = await getOriginalLocation(
    toolbox,
    generatedLocation.id,
    generatedLocation.line,
    generatedLocation.column
  );

  if (!originalLocation) {
    return generatedLocation;
  }

  const originalSource = dbg.getLocationSource(originalLocation);

  if (!originalSource) {
    return generatedLocation;
  }

  return {
    id: originalSource.id,
    line: originalLocation.line,
    column: originalLocation.column,
  };
}

async function getOriginalLocation(
  toolbox,
  generatedID,
  generatedLine,
  generatedColumn
) {
  // If there is no line number, then there's no chance that we'll get back
  // a useful original location.
  if (typeof generatedLine !== "number") {
    return null;
  }

  let originalLocation = null;
  try {
    originalLocation = await toolbox.sourceMapLoader.getOriginalLocation({
      sourceId: generatedID,
      line: generatedLine,
      column: generatedColumn,
    });
    if (originalLocation && originalLocation.sourceId === generatedID) {
      originalLocation = null;
    }
  } catch (err) {
    console.error(
      "Failed to resolve sourcemapped location for the given source location",
      { generatedID, generatedLine, generatedColumn },
      err
    );
  }
  return originalLocation;
}

/**
 * Open a link in Firefox's View Source.
 *
 * @param {Toolbox} toolbox
 * @param {string} sourceURL
 * @param {number} sourceLine
 *
 * @return {Promise}
 */
exports.viewSource = async function(toolbox, sourceURL, sourceLine) {
  const utils = toolbox.gViewSourceUtils;
  utils.viewSource({
    URL: sourceURL,
    lineNumber: sourceLine || -1,
  });
};