Bug 1250896 - Refactor SourceActor to use protocol.js;r=jryans

This commit is contained in:
Eddy Bruel 2016-03-09 14:24:38 +01:00
Родитель d5530d2198
Коммит 399122456e
1 изменённых файлов: 68 добавлений и 91 удалений

Просмотреть файл

@ -10,6 +10,7 @@ const { Cc, Ci } = require("chrome");
const { BreakpointActor, setBreakpointAtEntryPoints } = require("devtools/server/actors/breakpoint");
const { OriginalLocation, GeneratedLocation } = require("devtools/server/actors/common");
const { createValueGrip } = require("devtools/server/actors/object");
const { ActorClass, Arg, RetVal, method } = require("devtools/server/protocol");
const DevToolsUtils = require("devtools/shared/DevToolsUtils");
const { assert, fetch } = DevToolsUtils;
const { dirname, joinURI } = require("devtools/shared/path");
@ -137,41 +138,35 @@ function resolveURIToLocalPath(aURI) {
* @param String contentType
* Optional. The content type of this source, if immediately available.
*/
function SourceActor({ source, thread, originalUrl, generatedSource,
isInlineSource, contentType }) {
this._threadActor = thread;
this._originalUrl = originalUrl;
this._source = source;
this._generatedSource = generatedSource;
this._contentType = contentType;
this._isInlineSource = isInlineSource;
let SourceActor = ActorClass({
typeName: "source",
this.onSource = this.onSource.bind(this);
this._invertSourceMap = this._invertSourceMap.bind(this);
this._encodeAndSetSourceMapURL = this._encodeAndSetSourceMapURL.bind(this);
this._getSourceText = this._getSourceText.bind(this);
initialize: function ({ source, thread, originalUrl, generatedSource,
isInlineSource, contentType }) {
this._threadActor = thread;
this._originalUrl = originalUrl;
this._source = source;
this._generatedSource = generatedSource;
this._contentType = contentType;
this._isInlineSource = isInlineSource;
this._mapSourceToAddon();
this.onSource = this.onSource.bind(this);
this._invertSourceMap = this._invertSourceMap.bind(this);
this._encodeAndSetSourceMapURL = this._encodeAndSetSourceMapURL.bind(this);
this._getSourceText = this._getSourceText.bind(this);
if (this.threadActor.sources.isPrettyPrinted(this.url)) {
this._init = this.onPrettyPrint({
indent: this.threadActor.sources.prettyPrintIndent(this.url)
}).then(null, error => {
DevToolsUtils.reportException("SourceActor", error);
});
} else {
this._init = null;
}
}
this._mapSourceToAddon();
SourceActor.prototype = {
constructor: SourceActor,
actorPrefix: "source",
_oldSourceMap: null,
_init: null,
_addonID: null,
_addonPath: null,
if (this.threadActor.sources.isPrettyPrinted(this.url)) {
this._init = this.prettyPrint(
this.threadActor.sources.prettyPrintIndent(this.url)
).then(null, error => {
DevToolsUtils.reportException("SourceActor", error);
});
} else {
this._init = null;
}
},
get isSourceMapped() {
return !this.isInlineSource && (
@ -367,12 +362,7 @@ SourceActor.prototype = {
* Get all executable lines from the current source
* @return Array - Executable lines of the current script
**/
getExecutableLines: function () {
// Check if the original source is source mapped
let packet = {
from: this.actorID
};
getExecutableLines: method(function () {
function sortLines(lines) {
// Converting the Set into an array
lines = [...lines];
@ -399,15 +389,13 @@ SourceActor.prototype = {
}
}
packet.lines = sortLines(lines);
return packet;
return sortLines(lines);
});
}
let lines = this.getExecutableOffsets(this.source, true);
packet.lines = sortLines(lines);
return packet;
},
return sortLines(lines);
}, { response: { lines: RetVal("json") } }),
/**
* Extract all executable offsets from the given script
@ -429,12 +417,11 @@ SourceActor.prototype = {
/**
* Handler for the "source" packet.
*/
onSource: function () {
onSource: method(function () {
return resolve(this._init)
.then(this._getSourceText)
.then(({ content, contentType }) => {
return {
from: this.actorID,
source: createValueGrip(content, this.threadActor.threadLifetimePool,
this.threadActor.objectGrip),
contentType: contentType
@ -442,19 +429,19 @@ SourceActor.prototype = {
})
.then(null, aError => {
reportError(aError, "Got an exception during SA_onSource: ");
return {
"from": this.actorID,
"error": this.url,
"message": "Could not load the source for " + this.url + ".\n"
+ DevToolsUtils.safeErrorString(aError)
};
throw new Error("Could not load the source for " + this.url + ".\n" +
DevToolsUtils.safeErrorString(aError));
});
},
}, {
request: { type: "source" },
response: RetVal("json")
}),
/**
* Handler for the "prettyPrint" packet.
*/
onPrettyPrint: function ({ indent }) {
prettyPrint: method(function (indent) {
dump("EN IS HET EEN KEER NIET KUT " + JSON.stringify(indent) + "\n");
this.threadActor.sources.prettyPrint(this.url, indent);
return this._getSourceText()
.then(this._sendToPrettyPrintWorker(indent))
@ -468,14 +455,13 @@ SourceActor.prototype = {
})
.then(this.onSource)
.then(null, error => {
this.onDisablePrettyPrint();
return {
from: this.actorID,
error: "prettyPrintError",
message: DevToolsUtils.safeErrorString(error)
};
this.disablePrettyPrint();
throw new Error(DevToolsUtils.safeErrorString(error));
});
},
}, {
request: { indent: Arg(0, "number") },
response: RetVal("json")
}),
/**
* Return a function that sends a request to the pretty print worker, waits on
@ -568,7 +554,7 @@ SourceActor.prototype = {
/**
* Handler for the "disablePrettyPrint" packet.
*/
onDisablePrettyPrint: function () {
disablePrettyPrint: method(function () {
let source = this.generatedSource || this.source;
let sources = this.threadActor.sources;
let sm = sources.getSourceMap(source);
@ -584,33 +570,29 @@ SourceActor.prototype = {
this.threadActor.sources.disablePrettyPrint(this.url);
return this.onSource();
},
}, {
response: RetVal("json")
}),
/**
* Handler for the "blackbox" packet.
*/
onBlackBox: function (aRequest) {
blackbox: method(function () {
this.threadActor.sources.blackBox(this.url);
let packet = {
from: this.actorID
};
if (this.threadActor.state == "paused"
&& this.threadActor.youngestFrame
&& this.threadActor.youngestFrame.script.url == this.url) {
packet.pausedInSource = true;
return true;
}
return packet;
},
return false;
}, { response: { pausedInSource: RetVal("boolean") } }),
/**
* Handler for the "unblackbox" packet.
*/
onUnblackBox: function (aRequest) {
unblackbox: method(function () {
this.threadActor.sources.unblackBox(this.url);
return {
from: this.actorID
};
},
}),
/**
* Handle a request to set a breakpoint.
@ -622,15 +604,11 @@ SourceActor.prototype = {
* A promise that resolves to a JSON object representing the
* response.
*/
onSetBreakpoint: function (request) {
setBreakpoint: method(function (line, column, condition) {
if (this.threadActor.state !== "paused") {
return {
error: "wrongState",
message: "Cannot set breakpoint while debuggee is running."
};
throw new Error("Cannot set breakpoint while debuggee is running.");
}
let { location: { line, column }, condition } = request;
let location = new OriginalLocation(this, line, column);
return this._getOrCreateBreakpointActor(
location,
@ -648,7 +626,16 @@ SourceActor.prototype = {
return response;
});
},
}, {
request: {
location: {
line: Arg(0, "number"),
column: Arg(1, "nullable:number")
},
condition: Arg(2, "nullable:string")
},
response: RetVal("json")
}),
/**
* Get or create a BreakpointActor for the given location in the original
@ -866,17 +853,7 @@ SourceActor.prototype = {
setBreakpointAtEntryPoints(actor, entryPoints);
return true;
}
};
SourceActor.prototype.requestTypes = {
"source": SourceActor.prototype.onSource,
"blackbox": SourceActor.prototype.onBlackBox,
"unblackbox": SourceActor.prototype.onUnblackBox,
"prettyPrint": SourceActor.prototype.onPrettyPrint,
"disablePrettyPrint": SourceActor.prototype.onDisablePrettyPrint,
"getExecutableLines": SourceActor.prototype.getExecutableLines,
"setBreakpoint": SourceActor.prototype.onSetBreakpoint
};
});
exports.SourceActor = SourceActor;