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/. */
|
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
|
|
|
},
|
|
|
|
|
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) {
|
|
|
|
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)
|
|
|
|
return;
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-09-14 19:09:13 -07:00
|
|
|
case "SPLoadChromeScript":
|
|
|
|
var url = aMessage.json.url;
|
|
|
|
var id = aMessage.json.id;
|
|
|
|
|
|
|
|
// 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(url, null, null);
|
|
|
|
var input = channel.open();
|
|
|
|
scriptableStream.init(input);
|
|
|
|
var jsScript = scriptableStream.read(input.available());
|
|
|
|
scriptableStream.close();
|
|
|
|
input.close();
|
|
|
|
|
|
|
|
// 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 });
|
|
|
|
};
|
|
|
|
// Evaluate the chrome script
|
|
|
|
try {
|
|
|
|
Components.utils.evalInSandbox(jsScript, sb, "1.8", url, 1);
|
|
|
|
} catch(e) {
|
|
|
|
throw new SpecialPowersException("Error while executing chrome " +
|
|
|
|
"script '" + url + "':\n" + e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
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));
|
|
|
|
break;
|
|
|
|
|
2011-10-06 07:51:03 -07:00
|
|
|
default:
|
|
|
|
throw new SpecialPowersException("Unrecognized Special Powers API");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|