Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | XPCOMUtils.defineLazyServiceGetter(this, "cpmm", |
michael@0 | 15 | "@mozilla.org/childprocessmessagemanager;1", |
michael@0 | 16 | "nsISyncMessageSender"); |
michael@0 | 17 | |
michael@0 | 18 | function debug(aMsg) { |
michael@0 | 19 | //dump("-- ActivityProxy " + Date.now() + " : " + aMsg + "\n"); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * nsIActivityProxy implementation |
michael@0 | 24 | * We keep a reference to the C++ Activity object, and |
michael@0 | 25 | * communicate with the Message Manager to know when to |
michael@0 | 26 | * fire events on it. |
michael@0 | 27 | */ |
michael@0 | 28 | function ActivityProxy() { |
michael@0 | 29 | debug("ActivityProxy"); |
michael@0 | 30 | this.activity = null; |
michael@0 | 31 | let inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime) |
michael@0 | 32 | .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; |
michael@0 | 33 | debug("inParent: " + inParent); |
michael@0 | 34 | Cu.import(inParent ? "resource://gre/modules/Webapps.jsm" |
michael@0 | 35 | : "resource://gre/modules/AppsServiceChild.jsm"); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | ActivityProxy.prototype = { |
michael@0 | 39 | startActivity: function actProxy_startActivity(aActivity, aOptions, aWindow) { |
michael@0 | 40 | debug("startActivity"); |
michael@0 | 41 | |
michael@0 | 42 | this.window = aWindow; |
michael@0 | 43 | this.activity = aActivity; |
michael@0 | 44 | this.id = Cc["@mozilla.org/uuid-generator;1"] |
michael@0 | 45 | .getService(Ci.nsIUUIDGenerator) |
michael@0 | 46 | .generateUUID().toString(); |
michael@0 | 47 | // Retrieve the app's manifest url from the principal, so that we can |
michael@0 | 48 | // later notify when the activity handler called postResult or postError |
michael@0 | 49 | let principal = aWindow.document.nodePrincipal; |
michael@0 | 50 | let appId = principal.appId; |
michael@0 | 51 | let manifestURL = (appId != Ci.nsIScriptSecurityManager.NO_APP_ID && |
michael@0 | 52 | appId != Ci.nsIScriptSecurityManager.UNKNOWN_APP_ID) |
michael@0 | 53 | ? DOMApplicationRegistry.getManifestURLByLocalId(appId) |
michael@0 | 54 | : null; |
michael@0 | 55 | cpmm.sendAsyncMessage("Activity:Start", { id: this.id, |
michael@0 | 56 | options: { |
michael@0 | 57 | name: aOptions.name, |
michael@0 | 58 | data: aOptions.data |
michael@0 | 59 | }, |
michael@0 | 60 | manifestURL: manifestURL, |
michael@0 | 61 | pageURL: aWindow.document.location.href }); |
michael@0 | 62 | |
michael@0 | 63 | cpmm.addMessageListener("Activity:FireSuccess", this); |
michael@0 | 64 | cpmm.addMessageListener("Activity:FireError", this); |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | receiveMessage: function actProxy_receiveMessage(aMessage) { |
michael@0 | 68 | debug("Got message: " + aMessage.name); |
michael@0 | 69 | let msg = aMessage.json; |
michael@0 | 70 | if (msg.id != this.id) |
michael@0 | 71 | return; |
michael@0 | 72 | debug("msg=" + JSON.stringify(msg)); |
michael@0 | 73 | |
michael@0 | 74 | switch(aMessage.name) { |
michael@0 | 75 | case "Activity:FireSuccess": |
michael@0 | 76 | debug("FireSuccess"); |
michael@0 | 77 | Services.DOMRequest.fireSuccess(this.activity, |
michael@0 | 78 | Cu.cloneInto(msg.result, this.window)); |
michael@0 | 79 | Services.obs.notifyObservers(null, "Activity:Success", null); |
michael@0 | 80 | break; |
michael@0 | 81 | case "Activity:FireError": |
michael@0 | 82 | debug("FireError"); |
michael@0 | 83 | Services.DOMRequest.fireError(this.activity, msg.error); |
michael@0 | 84 | Services.obs.notifyObservers(null, "Activity:Error", null); |
michael@0 | 85 | break; |
michael@0 | 86 | } |
michael@0 | 87 | // We can only get one FireSuccess / FireError message, so cleanup as soon as possible. |
michael@0 | 88 | this.cleanup(); |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | cleanup: function actProxy_cleanup() { |
michael@0 | 92 | debug("cleanup"); |
michael@0 | 93 | if (cpmm && !this.cleanedUp) { |
michael@0 | 94 | cpmm.removeMessageListener("Activity:FireSuccess", this); |
michael@0 | 95 | cpmm.removeMessageListener("Activity:FireError", this); |
michael@0 | 96 | } |
michael@0 | 97 | this.cleanedUp = true; |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | classID: Components.ID("{ba9bd5cb-76a0-4ecf-a7b3-d2f7c43c5949}"), |
michael@0 | 101 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityProxy]) |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivityProxy]); |