|
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/. */ |
|
4 |
|
5 "use strict" |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 Cu.import("resource://gre/modules/Services.jsm"); |
|
13 |
|
14 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", |
|
15 "resource://gre/modules/SystemAppProxy.jsm"); |
|
16 |
|
17 function ActivitiesDialog() { |
|
18 this._id = 0; |
|
19 |
|
20 this.activities = []; |
|
21 } |
|
22 |
|
23 ActivitiesDialog.prototype = { |
|
24 run: function ap_run() { |
|
25 let id = "activity-choice" + this._id++; |
|
26 let activity = this.activities.shift(); |
|
27 |
|
28 let choices = []; |
|
29 activity.list.forEach(function(item) { |
|
30 choices.push({ manifest: item.manifest, icon: item.icon }); |
|
31 }); |
|
32 |
|
33 |
|
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 }; |
|
43 |
|
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; |
|
49 |
|
50 SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice); |
|
51 activity.callback.handleEvent(evt.detail.value !== undefined |
|
52 ? evt.detail.value |
|
53 : -1); |
|
54 }); |
|
55 |
|
56 SystemAppProxy.dispatchEvent(detail); |
|
57 }, |
|
58 |
|
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 }, |
|
67 |
|
68 classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"), |
|
69 |
|
70 QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable]) |
|
71 } |
|
72 |
|
73 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]); |
|
74 |