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
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 = [];
21 }
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 // Listen the resulting choice from the front-end. If there is no choice,
45 // let's return -1, which means the user has cancelled the dialog.
46 SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) {
47 if (evt.detail.id != id)
48 return;
50 SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice);
51 activity.callback.handleEvent(evt.detail.value !== undefined
52 ? evt.detail.value
53 : -1);
54 });
56 SystemAppProxy.dispatchEvent(detail);
57 },
59 chooseActivity: function ap_chooseActivity(aName, aActivities, aCallback) {
60 this.activities.push({
61 name: aName,
62 list: aActivities,
63 callback: aCallback
64 });
65 Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL);
66 },
68 classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"),
70 QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable])
71 }
73 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]);