michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict" michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", michael@0: "resource://gre/modules/SystemAppProxy.jsm"); michael@0: michael@0: function ActivitiesDialog() { michael@0: this._id = 0; michael@0: michael@0: this.activities = []; michael@0: } michael@0: michael@0: ActivitiesDialog.prototype = { michael@0: run: function ap_run() { michael@0: let id = "activity-choice" + this._id++; michael@0: let activity = this.activities.shift(); michael@0: michael@0: let choices = []; michael@0: activity.list.forEach(function(item) { michael@0: choices.push({ manifest: item.manifest, icon: item.icon }); michael@0: }); michael@0: michael@0: michael@0: // Keep up the frond-end of an activity choice. The messages contains michael@0: // a list of {names, icons} for applications able to handle this particular michael@0: // activity. The front-end should display a UI to pick one. michael@0: let detail = { michael@0: type: "activity-choice", michael@0: id: id, michael@0: name: activity.name, michael@0: choices: choices michael@0: }; michael@0: michael@0: // Listen the resulting choice from the front-end. If there is no choice, michael@0: // let's return -1, which means the user has cancelled the dialog. michael@0: SystemAppProxy.addEventListener("mozContentEvent", function act_getChoice(evt) { michael@0: if (evt.detail.id != id) michael@0: return; michael@0: michael@0: SystemAppProxy.removeEventListener("mozContentEvent", act_getChoice); michael@0: activity.callback.handleEvent(evt.detail.value !== undefined michael@0: ? evt.detail.value michael@0: : -1); michael@0: }); michael@0: michael@0: SystemAppProxy.dispatchEvent(detail); michael@0: }, michael@0: michael@0: chooseActivity: function ap_chooseActivity(aName, aActivities, aCallback) { michael@0: this.activities.push({ michael@0: name: aName, michael@0: list: aActivities, michael@0: callback: aCallback michael@0: }); michael@0: Services.tm.currentThread.dispatch(this, Ci.nsIEventTarget.DISPATCH_NORMAL); michael@0: }, michael@0: michael@0: classID: Components.ID("{70a83123-7467-4389-a309-3e81c74ad002}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIActivityUIGlue, Ci.nsIRunnable]) michael@0: } michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ActivitiesDialog]); michael@0: