2012-07-20 08:41:30 -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/. */
|
|
|
|
|
|
|
|
"use strict";
|
2012-08-24 21:07:58 -07:00
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2012-08-24 21:07:58 -07:00
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2012-08-27 07:13:02 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
|
|
|
|
"@mozilla.org/childprocessmessagemanager;1",
|
|
|
|
"nsISyncMessageSender");
|
2012-07-20 08:41:30 -07:00
|
|
|
|
|
|
|
function debug(aMsg) {
|
|
|
|
//dump("-- ActivityProxy " + Date.now() + " : " + aMsg + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsIActivityProxy implementation
|
|
|
|
* We keep a reference to the C++ Activity object, and
|
2012-08-24 21:07:58 -07:00
|
|
|
* communicate with the Message Manager to know when to
|
2012-07-20 08:41:30 -07:00
|
|
|
* fire events on it.
|
|
|
|
*/
|
|
|
|
function ActivityProxy() {
|
|
|
|
debug("ActivityProxy");
|
|
|
|
this.activity = null;
|
2012-10-17 13:44:39 -07:00
|
|
|
let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
|
|
|
|
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
|
|
|
|
debug("inParent: " + inParent);
|
|
|
|
Cu.import(inParent ? "resource://gre/modules/Webapps.jsm"
|
|
|
|
: "resource://gre/modules/AppsServiceChild.jsm");
|
2012-07-20 08:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ActivityProxy.prototype = {
|
2012-08-24 21:07:58 -07:00
|
|
|
startActivity: function actProxy_startActivity(aActivity, aOptions, aWindow) {
|
2012-07-20 08:41:30 -07:00
|
|
|
debug("startActivity");
|
|
|
|
|
2012-08-24 21:07:58 -07:00
|
|
|
this.window = aWindow;
|
2012-07-20 08:41:30 -07:00
|
|
|
this.activity = aActivity;
|
|
|
|
this.id = Cc["@mozilla.org/uuid-generator;1"]
|
|
|
|
.getService(Ci.nsIUUIDGenerator)
|
|
|
|
.generateUUID().toString();
|
2012-10-15 12:26:49 -07:00
|
|
|
// Retrieve the app's manifest url from the principal, so that we can
|
|
|
|
// later notify when the activity handler called postResult or postError
|
|
|
|
let principal = aWindow.document.nodePrincipal;
|
|
|
|
let appId = principal.appId;
|
|
|
|
let manifestURL = (appId != Ci.nsIScriptSecurityManager.NO_APP_ID &&
|
|
|
|
appId != Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID)
|
|
|
|
? DOMApplicationRegistry.getManifestURLByLocalId(appId)
|
|
|
|
: null;
|
2014-05-29 17:24:49 -07:00
|
|
|
|
|
|
|
// Only let certified apps enumerate providers for this filter.
|
|
|
|
if (aOptions.getFilterResults === true &&
|
|
|
|
principal.appStatus != Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
|
2014-09-21 00:08:00 -07:00
|
|
|
Services.DOMRequest.fireErrorAsync(this.activity, "SecurityError");
|
2014-05-29 17:24:49 -07:00
|
|
|
Services.obs.notifyObservers(null, "Activity:Error", null);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-20 08:41:30 -07:00
|
|
|
|
2014-09-21 00:09:00 -07:00
|
|
|
// Only let certified app to initiate this activitiy.
|
|
|
|
if (aOptions.name === 'internal-system-engineering-mode' &&
|
|
|
|
principal.appStatus != Ci.nsIPrincipal.APP_STATUS_CERTIFIED) {
|
|
|
|
Services.DOMRequest.fireErrorAsync(this.activity, "SecurityError");
|
|
|
|
Services.obs.notifyObservers(null, "Activity:Error", null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-20 08:41:30 -07:00
|
|
|
cpmm.addMessageListener("Activity:FireSuccess", this);
|
|
|
|
cpmm.addMessageListener("Activity:FireError", this);
|
2014-05-29 17:24:49 -07:00
|
|
|
|
|
|
|
cpmm.sendAsyncMessage("Activity:Start",
|
|
|
|
{
|
|
|
|
id: this.id,
|
|
|
|
options: {
|
|
|
|
name: aOptions.name,
|
|
|
|
data: aOptions.data
|
|
|
|
},
|
|
|
|
getFilterResults: aOptions.getFilterResults,
|
|
|
|
manifestURL: manifestURL,
|
|
|
|
pageURL: aWindow.document.location.href });
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function actProxy_receiveMessage(aMessage) {
|
|
|
|
debug("Got message: " + aMessage.name);
|
|
|
|
let msg = aMessage.json;
|
|
|
|
if (msg.id != this.id)
|
|
|
|
return;
|
|
|
|
debug("msg=" + JSON.stringify(msg));
|
|
|
|
|
|
|
|
switch(aMessage.name) {
|
|
|
|
case "Activity:FireSuccess":
|
|
|
|
debug("FireSuccess");
|
2012-08-24 21:07:58 -07:00
|
|
|
Services.DOMRequest.fireSuccess(this.activity,
|
2014-02-01 11:06:59 -08:00
|
|
|
Cu.cloneInto(msg.result, this.window));
|
2013-11-22 04:45:05 -08:00
|
|
|
Services.obs.notifyObservers(null, "Activity:Success", null);
|
2012-07-20 08:41:30 -07:00
|
|
|
break;
|
|
|
|
case "Activity:FireError":
|
|
|
|
debug("FireError");
|
|
|
|
Services.DOMRequest.fireError(this.activity, msg.error);
|
2013-11-22 04:45:05 -08:00
|
|
|
Services.obs.notifyObservers(null, "Activity:Error", null);
|
2012-07-20 08:41:30 -07:00
|
|
|
break;
|
|
|
|
}
|
2012-10-18 15:30:57 -07:00
|
|
|
// We can only get one FireSuccess / FireError message, so cleanup as soon as possible.
|
|
|
|
this.cleanup();
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
cleanup: function actProxy_cleanup() {
|
|
|
|
debug("cleanup");
|
2013-10-10 12:48:27 -07:00
|
|
|
if (cpmm && !this.cleanedUp) {
|
2012-10-18 15:30:57 -07:00
|
|
|
cpmm.removeMessageListener("Activity:FireSuccess", this);
|
|
|
|
cpmm.removeMessageListener("Activity:FireError", this);
|
|
|
|
}
|
|
|
|
this.cleanedUp = true;
|
2012-07-20 08:41:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
classID: Components.ID("{ba9bd5cb-76a0-4ecf-a7b3-d2f7c43c5949}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityProxy])
|
|
|
|
}
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivityProxy]);
|