Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / devtools / client / fronts / source.js
blobd8a195bbb2e70d2fcc1fd65967b5b933f1f26844
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const { sourceSpec } = require("resource://devtools/shared/specs/source.js");
8 const {
9 FrontClassWithSpec,
10 registerFront,
11 } = require("resource://devtools/shared/protocol.js");
12 const {
13 ArrayBufferFront,
14 } = require("resource://devtools/client/fronts/array-buffer.js");
16 /**
17 * A SourceFront provides a way to access the source text of a script.
19 * @param client DevToolsClient
20 * The DevTools Client instance.
21 * @param form Object
22 * The form sent across the remote debugging protocol.
24 class SourceFront extends FrontClassWithSpec(sourceSpec) {
25 constructor(client, form) {
26 super(client);
27 if (form) {
28 this._url = form.url;
29 // this is here for the time being, until the source front is managed
30 // via protocol.js marshalling
31 this.actorID = form.actor;
35 form(json) {
36 this._url = json.url;
39 get actor() {
40 return this.actorID;
43 get url() {
44 return this._url;
47 // Alias for source.blackbox to avoid changing protocol.js packets
48 blackBox(range) {
49 return this.blackbox(range);
52 // Alias for source.unblackbox to avoid changing protocol.js packets
53 unblackBox() {
54 return this.unblackbox();
57 /**
58 * Get a Front for either an ArrayBuffer or LongString
59 * for this SourceFront's source.
61 async source() {
62 const response = await super.source();
63 return this._onSourceResponse(response);
66 _onSourceResponse(response) {
67 const { contentType, source } = response;
68 if (source instanceof ArrayBufferFront) {
69 return source.slice(0, source.length).then(function (resp) {
70 if (resp.error) {
71 return resp;
73 // Keeping str as a string, ArrayBuffer/Uint8Array will not survive
74 // setIn/mergeIn operations.
75 const str = atob(resp.encoded);
76 const newResponse = {
77 source: {
78 binary: str,
79 toString: () => "[wasm]",
81 contentType,
83 return newResponse;
84 });
87 return source.substring(0, source.length).then(function (resp) {
88 if (resp.error) {
89 return resp;
92 const newResponse = {
93 source: resp,
94 contentType,
96 return newResponse;
97 });
101 exports.SourceFront = SourceFront;
102 registerFront(SourceFront);