michael@0: /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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 michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict" michael@0: michael@0: const { classes: Cc, interfaces: Ci, manager: Cm, utils: Cu, results: Cr } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Prompt.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "sendMessageToJava", michael@0: "resource://gre/modules/Messaging.jsm"); michael@0: michael@0: function TabSource() { michael@0: } michael@0: michael@0: TabSource.prototype = { michael@0: classID: Components.ID("{5850c76e-b916-4218-b99a-31f004e0a7e7}"), michael@0: classDescription: "Fennec Tab Source", michael@0: contractID: "@mozilla.org/tab-source-service;1", michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsITabSource]), michael@0: michael@0: getTabToStream: function() { michael@0: let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; michael@0: let tabs = app.tabs; michael@0: if (tabs == null || tabs.length == 0) { michael@0: Services.console.logStringMessage("ERROR: No tabs"); michael@0: return null; michael@0: } michael@0: michael@0: let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); michael@0: let title = bundle.GetStringFromName("tabshare.title") michael@0: michael@0: let prompt = new Prompt({ michael@0: title: title, michael@0: window: null michael@0: }).setSingleChoiceItems(tabs.map(function(tab) { michael@0: let label; michael@0: if (tab.browser.contentTitle) michael@0: label = tab.browser.contentTitle; michael@0: else if (tab.browser.contentURI && tab.browser.contentURI.spec) michael@0: label = tab.browser.contentURI.spec; michael@0: else michael@0: label = tab.originalURI; michael@0: return { label: label, michael@0: icon: "thumbnail:" + tab.id } michael@0: })); michael@0: michael@0: let result = null; michael@0: prompt.show(function(data) { michael@0: result = data.button; michael@0: }); michael@0: michael@0: // Spin this thread while we wait for a result. michael@0: let thread = Services.tm.currentThread; michael@0: while (result == null) { michael@0: thread.processNextEvent(true); michael@0: } michael@0: michael@0: if (result == -1) { michael@0: return null; michael@0: } michael@0: return tabs[result].browser.contentWindow; michael@0: }, michael@0: michael@0: notifyStreamStart: function(window) { michael@0: let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; michael@0: let tabs = app.tabs; michael@0: for (var i in tabs) { michael@0: if (tabs[i].browser.contentWindow == window) { michael@0: sendMessageToJava({ type: "Tab:StreamStart", tabID: tabs[i].id }); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: notifyStreamStop: function(window) { michael@0: let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; michael@0: let tabs = app.tabs; michael@0: for (let i in tabs) { michael@0: if (tabs[i].browser.contentWindow == window) { michael@0: sendMessageToJava({ type: "Tab:StreamStop", tabID: tabs[i].id }); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TabSource]);