Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict" |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", |
michael@0 | 15 | "resource://gre/modules/SystemAppProxy.jsm"); |
michael@0 | 16 | |
michael@0 | 17 | function ActivitiesDialog() { |
michael@0 | 18 | this._id = 0; |
michael@0 | 19 | |
michael@0 | 20 | this.activities = []; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | ActivitiesDialog.prototype = { |
michael@0 | 24 | run: function ap_run() { |
michael@0 | 25 | let id = "activity-choice" + this._id++; |
michael@0 | 26 | let activity = this.activities.shift(); |
michael@0 | 27 | |
michael@0 | 28 | let choices = []; |
michael@0 | 29 | activity.list.forEach(function(item) { |
michael@0 | 30 | choices.push({ manifest: item.manifest, icon: item.icon }); |
michael@0 | 31 | }); |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | // Keep up the frond-end of an activity choice. The messages contains |
michael@0 | 35 | // a list of {names, icons} for applications able to handle this particular |
michael@0 | 36 | // activity. The front-end should display a UI to pick one. |
michael@0 | 37 | let detail = { |
michael@0 | 38 | type: "activity-choice", |
michael@0 | 39 | id: id, |
michael@0 | 40 | name: activity.name, |
michael@0 | 41 | choices: choices |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | // Listen the resulting choice from the front-end. If there is no choice, |
michael@0 | 45 | // let's return -1, which means the user has cancelled the dialog. |
michael@0 | 46 | SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) { |
michael@0 | 47 | if (evt.detail.id != id) |
michael@0 | 48 | return; |
michael@0 | 49 | |
michael@0 | 50 | SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice); |
michael@0 | 51 | activity.callback.handleEvent(evt.detail.value !== undefined |
michael@0 | 52 | ? evt.detail.value |
michael@0 | 53 | : -1); |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | SystemAppProxy.dispatchEvent(detail); |
michael@0 | 57 | }, |
michael@0 | 58 | |
michael@0 | 59 | chooseActivity: function ap_chooseActivity(aName, aActivities, aCallback) { |
michael@0 | 60 | this.activities.push({ |
michael@0 | 61 | name: aName, |
michael@0 | 62 | list: aActivities, |
michael@0 | 63 | callback: aCallback |
michael@0 | 64 | }); |
michael@0 | 65 | Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL); |
michael@0 | 66 | }, |
michael@0 | 67 | |
michael@0 | 68 | classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"), |
michael@0 | 69 | |
michael@0 | 70 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable]) |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]); |
michael@0 | 74 |