1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
12 Cu.import("resource://gre/modules/Services.jsm");
13 Cu.import("resource://gre/modules/Promise.jsm");
15 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
16 "resource://gre/modules/SystemAppProxy.jsm");
19 function debug(aMsg) {
20 dump("-- InterAppCommUIGlue: " + Date.now() + ": " + aMsg + "\n");
23 function InterAppCommUIGlue() {
24 // This matrix is to store the callerID (a random UUID) / deferral binding.
25 // An example of the object literal is shown below:
28 // "callerID1" : deferred1,
29 // "callerID2" : deferred2
33 // Listen to the result of selected apps from front-end.
34 SystemAppProxy.addEventListener ("mozIACContentEvent", function (aEvent) {
35 let detail = aEvent.detail;
36 if (detail.type != "inter-app-comm-permission") {
41 debug("mozIACContentEvent: " + JSON.stringify(detail));
44 let callerID = detail.chromeEventID;
45 let deferred = this._deferreds[callerID];
48 debug("Error! Cannot find the deferred for callerID: " + callerID);
53 delete this._deferreds[callerID];
54 deferred.resolve({ callerID: callerID,
55 keyword: detail.keyword,
56 manifestURL: detail.manifestURL,
57 selectedApps: detail.peers });
61 InterAppCommUIGlue.prototype = {
62 selectApps: function(aCallerID, aPubAppManifestURL, aKeyword, aAppsToSelect) {
63 let deferred = Promise.defer();
64 this._deferreds[aCallerID] = deferred;
66 SystemAppProxy._sendCustomEvent("mozIACChromeEvent",
67 { type: "inter-app-comm-permission",
68 chromeEventID: aCallerID,
69 manifestURL: aPubAppManifestURL,
71 peers: aAppsToSelect });
73 // TODO Bug 897169 Simulate the return of the app-selected result by
74 // the prompt, which always allows the connection. This dummy codes
75 // will be removed when the UX/UI for the prompt is ready.
76 SystemAppProxy._sendCustomEvent("mozIACContentEvent",
77 { type: "inter-app-comm-permission",
78 chromeEventID: aCallerID,
79 manifestURL: aPubAppManifestURL,
81 peers: aAppsToSelect });
83 return deferred.promise;
86 classID: Components.ID("{879ee66c-e246-11e3-9910-74d02b97e723}"),
88 QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterAppCommUIGlue])
91 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([InterAppCommUIGlue]);