2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2014-02-06 08:36:44 -08:00
|
|
|
"use strict";
|
2011-10-06 07:51:03 -07:00
|
|
|
|
2012-09-08 10:20:59 -07:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-09-11 07:10:52 -07:00
|
|
|
if (typeof(Ci) == 'undefined') {
|
|
|
|
var Ci = Components.interfaces;
|
|
|
|
}
|
|
|
|
|
2014-01-07 07:04:06 -08:00
|
|
|
if (typeof(Cc) == 'undefined') {
|
|
|
|
var Cc = Components.classes;
|
|
|
|
}
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
/**
|
|
|
|
* Special Powers Exception - used to throw exceptions nicely
|
|
|
|
**/
|
|
|
|
function SpecialPowersException(aMsg) {
|
|
|
|
this.message = aMsg;
|
|
|
|
this.name = "SpecialPowersException";
|
|
|
|
}
|
|
|
|
|
|
|
|
SpecialPowersException.prototype.toString = function() {
|
|
|
|
return this.name + ': "' + this.message + '"';
|
|
|
|
};
|
|
|
|
|
2013-05-31 16:58:58 -07:00
|
|
|
this.SpecialPowersObserverAPI = function SpecialPowersObserverAPI() {
|
2011-10-06 07:51:03 -07:00
|
|
|
this._crashDumpDir = null;
|
|
|
|
this._processCrashObserversRegistered = false;
|
2013-09-14 19:09:13 -07:00
|
|
|
this._chromeScriptListeners = [];
|
2011-10-06 07:51:03 -07:00
|
|
|
}
|
|
|
|
|
2012-09-08 10:20:59 -07:00
|
|
|
function parseKeyValuePairs(text) {
|
|
|
|
var lines = text.split('\n');
|
|
|
|
var data = {};
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
if (lines[i] == '')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// can't just .split() because the value might contain = characters
|
|
|
|
let eq = lines[i].indexOf('=');
|
|
|
|
if (eq != -1) {
|
|
|
|
let [key, value] = [lines[i].substring(0, eq),
|
|
|
|
lines[i].substring(eq + 1)];
|
|
|
|
if (key && value)
|
|
|
|
data[key] = value.replace(/\\n/g, "\n").replace(/\\\\/g, "\\");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseKeyValuePairsFromFile(file) {
|
|
|
|
var fstream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
fstream.init(file, -1, 0, 0);
|
|
|
|
var is = Cc["@mozilla.org/intl/converter-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIConverterInputStream);
|
|
|
|
is.init(fstream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
|
|
|
|
var str = {};
|
|
|
|
var contents = '';
|
|
|
|
while (is.readString(4096, str) != 0) {
|
|
|
|
contents += str.value;
|
|
|
|
}
|
|
|
|
is.close();
|
|
|
|
fstream.close();
|
|
|
|
return parseKeyValuePairs(contents);
|
|
|
|
}
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
SpecialPowersObserverAPI.prototype = {
|
|
|
|
|
|
|
|
_observe: function(aSubject, aTopic, aData) {
|
2014-01-16 06:37:02 -08:00
|
|
|
function addDumpIDToMessage(propertyName) {
|
|
|
|
var id = aSubject.getPropertyAsAString(propertyName);
|
|
|
|
if (id) {
|
|
|
|
message.dumpIDs.push({id: id, extension: "dmp"});
|
|
|
|
message.dumpIDs.push({id: id, extension: "extra"});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
switch(aTopic) {
|
|
|
|
case "plugin-crashed":
|
|
|
|
case "ipc:content-shutdown":
|
|
|
|
var message = { type: "crash-observed", dumpIDs: [] };
|
2012-09-08 10:20:59 -07:00
|
|
|
aSubject = aSubject.QueryInterface(Ci.nsIPropertyBag2);
|
2011-10-06 07:51:03 -07:00
|
|
|
if (aTopic == "plugin-crashed") {
|
|
|
|
addDumpIDToMessage("pluginDumpID");
|
|
|
|
addDumpIDToMessage("browserDumpID");
|
2012-09-08 10:20:59 -07:00
|
|
|
|
|
|
|
let pluginID = aSubject.getPropertyAsAString("pluginDumpID");
|
|
|
|
let extra = this._getExtraData(pluginID);
|
|
|
|
if (extra && ("additional_minidumps" in extra)) {
|
|
|
|
let dumpNames = extra.additional_minidumps.split(',');
|
|
|
|
for (let name of dumpNames) {
|
|
|
|
message.dumpIDs.push({id: pluginID + "-" + name, extension: "dmp"});
|
|
|
|
}
|
|
|
|
}
|
2011-10-06 07:51:03 -07:00
|
|
|
} else { // ipc:content-shutdown
|
|
|
|
addDumpIDToMessage("dumpID");
|
|
|
|
}
|
|
|
|
this._sendAsyncMessage("SPProcessCrashService", message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getCrashDumpDir: function() {
|
|
|
|
if (!this._crashDumpDir) {
|
2012-09-08 10:20:59 -07:00
|
|
|
this._crashDumpDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
|
2011-10-06 07:51:03 -07:00
|
|
|
this._crashDumpDir.append("minidumps");
|
|
|
|
}
|
|
|
|
return this._crashDumpDir;
|
|
|
|
},
|
|
|
|
|
2012-09-08 10:20:59 -07:00
|
|
|
_getExtraData: function(dumpId) {
|
|
|
|
let extraFile = this._getCrashDumpDir().clone();
|
|
|
|
extraFile.append(dumpId + ".extra");
|
|
|
|
if (!extraFile.exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return parseKeyValuePairsFromFile(extraFile);
|
|
|
|
},
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
_deleteCrashDumpFiles: function(aFilenames) {
|
|
|
|
var crashDumpDir = this._getCrashDumpDir();
|
|
|
|
if (!crashDumpDir.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var success = aFilenames.length != 0;
|
|
|
|
aFilenames.forEach(function(crashFilename) {
|
|
|
|
var file = crashDumpDir.clone();
|
|
|
|
file.append(crashFilename);
|
|
|
|
if (file.exists()) {
|
|
|
|
file.remove(false);
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return success;
|
|
|
|
},
|
|
|
|
|
|
|
|
_findCrashDumpFiles: function(aToIgnore) {
|
|
|
|
var crashDumpDir = this._getCrashDumpDir();
|
|
|
|
var entries = crashDumpDir.exists() && crashDumpDir.directoryEntries;
|
|
|
|
if (!entries) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
var crashDumpFiles = [];
|
|
|
|
while (entries.hasMoreElements()) {
|
2012-09-08 10:20:59 -07:00
|
|
|
var file = entries.getNext().QueryInterface(Ci.nsIFile);
|
2011-10-06 07:51:03 -07:00
|
|
|
var path = String(file.path);
|
|
|
|
if (path.match(/\.(dmp|extra)$/) && !aToIgnore[path]) {
|
|
|
|
crashDumpFiles.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return crashDumpFiles.concat();
|
|
|
|
},
|
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
_getURI: function (url) {
|
2012-09-08 10:20:59 -07:00
|
|
|
return Services.io.newURI(url, null, null);
|
2012-06-01 10:21:12 -07:00
|
|
|
},
|
|
|
|
|
2014-02-06 08:36:44 -08:00
|
|
|
_readUrlAsString: function(aUrl) {
|
|
|
|
// Fetch script content as we can't use scriptloader's loadSubScript
|
|
|
|
// to evaluate http:// urls...
|
|
|
|
var scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
|
|
|
|
.getService(Ci.nsIScriptableInputStream);
|
|
|
|
var channel = Services.io.newChannel(aUrl, null, null);
|
|
|
|
var input = channel.open();
|
|
|
|
scriptableStream.init(input);
|
|
|
|
|
|
|
|
var str;
|
|
|
|
var buffer = [];
|
|
|
|
|
|
|
|
while ((str = scriptableStream.read(4096))) {
|
|
|
|
buffer.push(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
var output = buffer.join("");
|
|
|
|
|
|
|
|
scriptableStream.close();
|
|
|
|
input.close();
|
|
|
|
|
|
|
|
var status;
|
|
|
|
try {
|
|
|
|
channel.QueryInterface(Ci.nsIHttpChannel);
|
|
|
|
status = channel.responseStatus;
|
|
|
|
} catch(e) {
|
|
|
|
/* The channel is not a nsIHttpCHannel, but that's fine */
|
|
|
|
dump("-*- _readUrlAsString: Got an error while fetching " +
|
|
|
|
"chrome script '" + aUrl + "': (" + e.name + ") " + e.message + ". " +
|
|
|
|
"Ignoring.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == 404) {
|
|
|
|
throw new SpecialPowersException(
|
|
|
|
"Error while executing chrome script '" + aUrl + "':\n" +
|
|
|
|
"The script doesn't exists. Ensure you have registered it in " +
|
|
|
|
"'support-files' in your mochitest.ini.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
/**
|
|
|
|
* messageManager callback function
|
|
|
|
* This will get requests from our API in the window and process them in chrome for it
|
|
|
|
**/
|
|
|
|
_receiveMessageAPI: function(aMessage) {
|
2014-01-16 06:53:50 -08:00
|
|
|
// We explicitly return values in the below code so that this function
|
|
|
|
// doesn't trigger a flurry of warnings about "does not always return
|
|
|
|
// a value".
|
2011-10-06 07:51:03 -07:00
|
|
|
switch(aMessage.name) {
|
|
|
|
case "SPPrefService":
|
2012-09-08 10:20:59 -07:00
|
|
|
var prefs = Services.prefs;
|
2011-10-06 07:51:03 -07:00
|
|
|
var prefType = aMessage.json.prefType.toUpperCase();
|
|
|
|
var prefName = aMessage.json.prefName;
|
|
|
|
var prefValue = "prefValue" in aMessage.json ? aMessage.json.prefValue : null;
|
|
|
|
|
|
|
|
if (aMessage.json.op == "get") {
|
|
|
|
if (!prefName || !prefType)
|
|
|
|
throw new SpecialPowersException("Invalid parameters for get in SPPrefService");
|
2013-08-29 13:18:53 -07:00
|
|
|
|
|
|
|
// return null if the pref doesn't exist
|
|
|
|
if (prefs.getPrefType(prefName) == prefs.PREF_INVALID)
|
2014-01-16 06:53:50 -08:00
|
|
|
return null;
|
2011-10-06 07:51:03 -07:00
|
|
|
} else if (aMessage.json.op == "set") {
|
|
|
|
if (!prefName || !prefType || prefValue === null)
|
|
|
|
throw new SpecialPowersException("Invalid parameters for set in SPPrefService");
|
|
|
|
} else if (aMessage.json.op == "clear") {
|
|
|
|
if (!prefName)
|
|
|
|
throw new SpecialPowersException("Invalid parameters for clear in SPPrefService");
|
|
|
|
} else {
|
|
|
|
throw new SpecialPowersException("Invalid operation for SPPrefService");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we make the call
|
|
|
|
switch(prefType) {
|
|
|
|
case "BOOL":
|
|
|
|
if (aMessage.json.op == "get")
|
|
|
|
return(prefs.getBoolPref(prefName));
|
|
|
|
else
|
|
|
|
return(prefs.setBoolPref(prefName, prefValue));
|
|
|
|
case "INT":
|
|
|
|
if (aMessage.json.op == "get")
|
|
|
|
return(prefs.getIntPref(prefName));
|
|
|
|
else
|
|
|
|
return(prefs.setIntPref(prefName, prefValue));
|
|
|
|
case "CHAR":
|
|
|
|
if (aMessage.json.op == "get")
|
|
|
|
return(prefs.getCharPref(prefName));
|
|
|
|
else
|
|
|
|
return(prefs.setCharPref(prefName, prefValue));
|
|
|
|
case "COMPLEX":
|
|
|
|
if (aMessage.json.op == "get")
|
|
|
|
return(prefs.getComplexValue(prefName, prefValue[0]));
|
|
|
|
else
|
|
|
|
return(prefs.setComplexValue(prefName, prefValue[0], prefValue[1]));
|
|
|
|
case "":
|
|
|
|
if (aMessage.json.op == "clear") {
|
|
|
|
prefs.clearUserPref(prefName);
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined;
|
2011-10-06 07:51:03 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2011-10-06 07:51:03 -07:00
|
|
|
|
|
|
|
case "SPProcessCrashService":
|
|
|
|
switch (aMessage.json.op) {
|
|
|
|
case "register-observer":
|
|
|
|
this._addProcessCrashObservers();
|
|
|
|
break;
|
|
|
|
case "unregister-observer":
|
|
|
|
this._removeProcessCrashObservers();
|
|
|
|
break;
|
|
|
|
case "delete-crash-dump-files":
|
|
|
|
return this._deleteCrashDumpFiles(aMessage.json.filenames);
|
|
|
|
case "find-crash-dump-files":
|
|
|
|
return this._findCrashDumpFiles(aMessage.json.crashDumpFilesToIgnore);
|
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Invalid operation for SPProcessCrashService");
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2011-10-06 07:51:03 -07:00
|
|
|
|
2012-06-01 10:21:12 -07:00
|
|
|
case "SPPermissionManager":
|
2012-08-23 11:23:48 -07:00
|
|
|
let msg = aMessage.json;
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2012-09-08 10:20:59 -07:00
|
|
|
let secMan = Services.scriptSecurityManager;
|
2012-08-23 11:23:48 -07:00
|
|
|
let principal = secMan.getAppCodebasePrincipal(this._getURI(msg.url), msg.appId, msg.isInBrowserElement);
|
|
|
|
|
|
|
|
switch (msg.op) {
|
2012-06-01 10:21:12 -07:00
|
|
|
case "add":
|
2012-09-08 10:20:59 -07:00
|
|
|
Services.perms.addFromPrincipal(principal, msg.type, msg.permission);
|
2012-06-01 10:21:12 -07:00
|
|
|
break;
|
|
|
|
case "remove":
|
2012-09-08 10:20:59 -07:00
|
|
|
Services.perms.removeFromPrincipal(principal, msg.type);
|
2012-06-01 10:21:12 -07:00
|
|
|
break;
|
2013-03-04 17:17:24 -08:00
|
|
|
case "has":
|
|
|
|
let hasPerm = Services.perms.testPermissionFromPrincipal(principal, msg.type);
|
|
|
|
if (hasPerm == Ci.nsIPermissionManager.ALLOW_ACTION)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case "test":
|
|
|
|
let testPerm = Services.perms.testPermissionFromPrincipal(principal, msg.type, msg.value);
|
|
|
|
if (testPerm == msg.value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
break;
|
2012-06-01 10:21:12 -07:00
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Invalid operation for " +
|
|
|
|
"SPPermissionManager");
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2012-06-01 10:21:12 -07:00
|
|
|
|
2013-04-04 17:58:44 -07:00
|
|
|
case "SPWebAppService":
|
|
|
|
let Webapps = {};
|
|
|
|
Components.utils.import("resource://gre/modules/Webapps.jsm", Webapps);
|
|
|
|
switch (aMessage.json.op) {
|
|
|
|
case "set-launchable":
|
|
|
|
let val = Webapps.DOMApplicationRegistry.allAppsLaunchable;
|
|
|
|
Webapps.DOMApplicationRegistry.allAppsLaunchable = aMessage.json.launchable;
|
|
|
|
return val;
|
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Invalid operation for SPWebAppsService");
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2013-04-04 17:58:44 -07:00
|
|
|
|
2013-05-12 21:10:58 -07:00
|
|
|
case "SPObserverService":
|
|
|
|
switch (aMessage.json.op) {
|
|
|
|
case "notify":
|
|
|
|
let topic = aMessage.json.observerTopic;
|
|
|
|
let data = aMessage.json.observerData
|
|
|
|
Services.obs.notifyObservers(null, topic, data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Invalid operation for SPObserverervice");
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2013-05-12 21:10:58 -07:00
|
|
|
|
2013-09-14 19:09:13 -07:00
|
|
|
case "SPLoadChromeScript":
|
|
|
|
var url = aMessage.json.url;
|
|
|
|
var id = aMessage.json.id;
|
|
|
|
|
2014-02-06 08:36:44 -08:00
|
|
|
var jsScript = this._readUrlAsString(url);
|
2013-09-14 19:09:13 -07:00
|
|
|
|
|
|
|
// Setup a chrome sandbox that has access to sendAsyncMessage
|
|
|
|
// and addMessageListener in order to communicate with
|
|
|
|
// the mochitest.
|
|
|
|
var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
|
|
|
|
var sb = Components.utils.Sandbox(systemPrincipal);
|
|
|
|
var mm = aMessage.target
|
|
|
|
.QueryInterface(Ci.nsIFrameLoaderOwner)
|
|
|
|
.frameLoader
|
|
|
|
.messageManager;
|
|
|
|
sb.sendAsyncMessage = (name, message) => {
|
|
|
|
mm.sendAsyncMessage("SPChromeScriptMessage",
|
|
|
|
{ id: id, name: name, message: message });
|
|
|
|
};
|
|
|
|
sb.addMessageListener = (name, listener) => {
|
|
|
|
this._chromeScriptListeners.push({ id: id, name: name, listener: listener });
|
|
|
|
};
|
2014-03-25 09:03:21 -07:00
|
|
|
|
|
|
|
// Also expose assertion functions
|
|
|
|
let reporter = function (err, message, stack) {
|
|
|
|
// Pipe assertions back to parent process
|
|
|
|
mm.sendAsyncMessage("SPChromeScriptAssert",
|
|
|
|
{ id: id, url: url, err: err, message: message,
|
|
|
|
stack: stack });
|
|
|
|
};
|
|
|
|
Object.defineProperty(sb, "assert", {
|
|
|
|
get: function () {
|
|
|
|
let scope = Components.utils.createObjectIn(sb);
|
|
|
|
Services.scriptloader.loadSubScript("resource://specialpowers/Assert.jsm",
|
|
|
|
scope);
|
|
|
|
|
|
|
|
let assert = new scope.Assert(reporter);
|
|
|
|
delete sb.assert;
|
|
|
|
return sb.assert = assert;
|
|
|
|
},
|
|
|
|
configurable: true
|
|
|
|
});
|
|
|
|
|
2013-09-14 19:09:13 -07:00
|
|
|
// Evaluate the chrome script
|
|
|
|
try {
|
|
|
|
Components.utils.evalInSandbox(jsScript, sb, "1.8", url, 1);
|
|
|
|
} catch(e) {
|
|
|
|
throw new SpecialPowersException("Error while executing chrome " +
|
2014-02-06 08:36:44 -08:00
|
|
|
"script '" + url + "':\n" + e + "\n" +
|
|
|
|
e.fileName + ":" + e.lineNumber);
|
2013-09-14 19:09:13 -07:00
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2013-09-14 19:09:13 -07:00
|
|
|
|
|
|
|
case "SPChromeScriptMessage":
|
|
|
|
var id = aMessage.json.id;
|
|
|
|
var name = aMessage.json.name;
|
|
|
|
var message = aMessage.json.message;
|
|
|
|
this._chromeScriptListeners
|
|
|
|
.filter(o => (o.name == name && o.id == id))
|
|
|
|
.forEach(o => o.listener(message));
|
2014-01-16 06:53:50 -08:00
|
|
|
return undefined; // See comment at the beginning of this function.
|
2013-09-14 19:09:13 -07:00
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Unrecognized Special Powers API");
|
|
|
|
}
|
2014-01-16 06:53:50 -08:00
|
|
|
|
|
|
|
// We throw an exception before reaching this explicit return because
|
|
|
|
// we should never be arriving here anyway.
|
|
|
|
throw new SpecialPowersException("Unreached code");
|
|
|
|
return undefined;
|
2011-10-06 07:51:03 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|