Bumping manifests a=b2g-bump
[gecko.git] / b2g / components / ActivitiesGlue.js
blobba70bd1ef9a89586f7fe08aeb33049ef88b75427
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");
14 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
15                                   "resource://gre/modules/SystemAppProxy.jsm");
17 function ActivitiesDialog() {
18   this._id = 0;
20   this.activities = [];
23 ActivitiesDialog.prototype = {
24   run: function ap_run() {
25     let id = "activity-choice" + this._id++;
26     let activity = this.activities.shift();
28     let choices = [];
29     activity.list.forEach(function(item) {
30       choices.push({ manifest: item.manifest, icon: item.icon });
31     });
34     // Keep up the frond-end of an activity choice. The messages contains
35     // a list of {names, icons} for applications able to handle this particular
36     // activity. The front-end should display a UI to pick one.
37     let detail = {
38       type: "activity-choice",
39       id: id,
40       name: activity.name,
41       choices: choices
42     };
44     if (activity.type) {
45       detail.activityType = activity.type;
46     }
48     // Listen the resulting choice from the front-end. If there is no choice,
49     // let's return -1, which means the user has cancelled the dialog.
50     SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) {
51       if (evt.detail.id != id)
52         return;
54       SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice);
55       activity.callback.handleEvent(Ci.nsIActivityUIGlueCallback.WEBAPPS_ACTIVITY,
56                                     evt.detail.value !== undefined
57                                       ? evt.detail.value
58                                       : -1);
59     });
61     SystemAppProxy.dispatchEvent(detail);
62   },
64   chooseActivity: function ap_chooseActivity(aOptions, aActivities, aCallback) {
65     // B2G does not have an alternate activity system, make no choice and return.
66     if (aActivities.length === 0) {
67       aCallback.handleEvent(Ci.nsIActivityUIGlueCallback.WEBAPPS_ACTIVITY, -1);
68       return;
69     }
71     let activity = {
72       name: aOptions.name,
73       list: aActivities,
74       callback: aCallback
75     };
77     if (aOptions.data && aOptions.data.type) {
78       activity.type = aOptions.data.type;
79     }
81     this.activities.push(activity);
82     Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL);
83   },
85   classID: Components.ID("{3a54788b-48cc-4ab4-93d6-0d6a8ef74f8e}"),
87   QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable])
90 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]);