1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/activities/src/ActivityProxy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,104 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const Cc = Components.classes; 1.11 +const Ci = Components.interfaces; 1.12 +const Cu = Components.utils; 1.13 + 1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.15 +Cu.import("resource://gre/modules/Services.jsm"); 1.16 + 1.17 +XPCOMUtils.defineLazyServiceGetter(this, "cpmm", 1.18 + "@mozilla.org/childprocessmessagemanager;1", 1.19 + "nsISyncMessageSender"); 1.20 + 1.21 +function debug(aMsg) { 1.22 + //dump("-- ActivityProxy " + Date.now() + " : " + aMsg + "\n"); 1.23 +} 1.24 + 1.25 +/** 1.26 + * nsIActivityProxy implementation 1.27 + * We keep a reference to the C++ Activity object, and 1.28 + * communicate with the Message Manager to know when to 1.29 + * fire events on it. 1.30 + */ 1.31 +function ActivityProxy() { 1.32 + debug("ActivityProxy"); 1.33 + this.activity = null; 1.34 + let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) 1.35 + .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; 1.36 + debug("inParent: " + inParent); 1.37 + Cu.import(inParent ? "resource://gre/modules/Webapps.jsm" 1.38 + : "resource://gre/modules/AppsServiceChild.jsm"); 1.39 +} 1.40 + 1.41 +ActivityProxy.prototype = { 1.42 + startActivity: function actProxy_startActivity(aActivity, aOptions, aWindow) { 1.43 + debug("startActivity"); 1.44 + 1.45 + this.window = aWindow; 1.46 + this.activity = aActivity; 1.47 + this.id = Cc["@mozilla.org/uuid-generator;1"] 1.48 + .getService(Ci.nsIUUIDGenerator) 1.49 + .generateUUID().toString(); 1.50 + // Retrieve the app's manifest url from the principal, so that we can 1.51 + // later notify when the activity handler called postResult or postError 1.52 + let principal = aWindow.document.nodePrincipal; 1.53 + let appId = principal.appId; 1.54 + let manifestURL = (appId != Ci.nsIScriptSecurityManager.NO_APP_ID && 1.55 + appId != Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) 1.56 + ? DOMApplicationRegistry.getManifestURLByLocalId(appId) 1.57 + : null; 1.58 + cpmm.sendAsyncMessage("Activity:Start", { id: this.id, 1.59 + options: { 1.60 + name: aOptions.name, 1.61 + data: aOptions.data 1.62 + }, 1.63 + manifestURL: manifestURL, 1.64 + pageURL: aWindow.document.location.href }); 1.65 + 1.66 + cpmm.addMessageListener("Activity:FireSuccess", this); 1.67 + cpmm.addMessageListener("Activity:FireError", this); 1.68 + }, 1.69 + 1.70 + receiveMessage: function actProxy_receiveMessage(aMessage) { 1.71 + debug("Got message: " + aMessage.name); 1.72 + let msg = aMessage.json; 1.73 + if (msg.id != this.id) 1.74 + return; 1.75 + debug("msg=" + JSON.stringify(msg)); 1.76 + 1.77 + switch(aMessage.name) { 1.78 + case "Activity:FireSuccess": 1.79 + debug("FireSuccess"); 1.80 + Services.DOMRequest.fireSuccess(this.activity, 1.81 + Cu.cloneInto(msg.result, this.window)); 1.82 + Services.obs.notifyObservers(null, "Activity:Success", null); 1.83 + break; 1.84 + case "Activity:FireError": 1.85 + debug("FireError"); 1.86 + Services.DOMRequest.fireError(this.activity, msg.error); 1.87 + Services.obs.notifyObservers(null, "Activity:Error", null); 1.88 + break; 1.89 + } 1.90 + // We can only get one FireSuccess / FireError message, so cleanup as soon as possible. 1.91 + this.cleanup(); 1.92 + }, 1.93 + 1.94 + cleanup: function actProxy_cleanup() { 1.95 + debug("cleanup"); 1.96 + if (cpmm && !this.cleanedUp) { 1.97 + cpmm.removeMessageListener("Activity:FireSuccess", this); 1.98 + cpmm.removeMessageListener("Activity:FireError", this); 1.99 + } 1.100 + this.cleanedUp = true; 1.101 + }, 1.102 + 1.103 + classID: Components.ID("{ba9bd5cb-76a0-4ecf-a7b3-d2f7c43c5949}"), 1.104 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityProxy]) 1.105 +} 1.106 + 1.107 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivityProxy]);