Bumping manifests a=b2g-bump
[gecko.git] / b2g / components / InterAppCommUIGlue.js
blob4cdeadc9ec80c17e33b11df46a8cfafdb23cc98c
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/. */
5 "use strict"
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");
18 const DEBUG = false;
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:
26   //
27   // {
28   //   "callerID1" : deferred1,
29   //   "callerID2" : deferred2
30   // }
31   this._deferreds = {};
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") {
37       return;
38     }
40     if (DEBUG) {
41       debug("mozIACContentEvent: " + JSON.stringify(detail));
42     }
44     let callerID = detail.chromeEventID;
45     let deferred = this._deferreds[callerID];
46     if (!deferred) {
47       if (DEBUG) {
48         debug("Error! Cannot find the deferred for callerID: " + callerID);
49       }
50       return;
51     }
53     delete this._deferreds[callerID];
54     deferred.resolve({ callerID: callerID,
55                        keyword: detail.keyword,
56                        manifestURL: detail.manifestURL,
57                        selectedApps: detail.peers });
58   }.bind(this));
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,
70                                       keyword: aKeyword,
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,
80                                       keyword: aKeyword,
81                                       peers: aAppsToSelect });
83     return deferred.promise;
84   },
86   classID: Components.ID("{879ee66c-e246-11e3-9910-74d02b97e723}"),
88   QueryInterface: XPCOMUtils.generateQI([Ci.nsIInterAppCommUIGlue])
91 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([InterAppCommUIGlue]);