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
michael@0 | 1 | /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | "use strict" |
michael@0 | 6 | |
michael@0 | 7 | const { classes: Cc, interfaces: Ci, manager: Cm, utils: Cu, results: Cr } = Components; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Prompt.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | XPCOMUtils.defineLazyModuleGetter(this, "sendMessageToJava", |
michael@0 | 14 | "resource://gre/modules/Messaging.jsm"); |
michael@0 | 15 | |
michael@0 | 16 | function TabSource() { |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | TabSource.prototype = { |
michael@0 | 20 | classID: Components.ID("{5850c76e-b916-4218-b99a-31f004e0a7e7}"), |
michael@0 | 21 | classDescription: "Fennec Tab Source", |
michael@0 | 22 | contractID: "@mozilla.org/tab-source-service;1", |
michael@0 | 23 | QueryInterface: XPCOMUtils.generateQI([Ci.nsITabSource]), |
michael@0 | 24 | |
michael@0 | 25 | getTabToStream: function() { |
michael@0 | 26 | let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; |
michael@0 | 27 | let tabs = app.tabs; |
michael@0 | 28 | if (tabs == null || tabs.length == 0) { |
michael@0 | 29 | Services.console.logStringMessage("ERROR: No tabs"); |
michael@0 | 30 | return null; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); |
michael@0 | 34 | let title = bundle.GetStringFromName("tabshare.title") |
michael@0 | 35 | |
michael@0 | 36 | let prompt = new Prompt({ |
michael@0 | 37 | title: title, |
michael@0 | 38 | window: null |
michael@0 | 39 | }).setSingleChoiceItems(tabs.map(function(tab) { |
michael@0 | 40 | let label; |
michael@0 | 41 | if (tab.browser.contentTitle) |
michael@0 | 42 | label = tab.browser.contentTitle; |
michael@0 | 43 | else if (tab.browser.contentURI && tab.browser.contentURI.spec) |
michael@0 | 44 | label = tab.browser.contentURI.spec; |
michael@0 | 45 | else |
michael@0 | 46 | label = tab.originalURI; |
michael@0 | 47 | return { label: label, |
michael@0 | 48 | icon: "thumbnail:" + tab.id } |
michael@0 | 49 | })); |
michael@0 | 50 | |
michael@0 | 51 | let result = null; |
michael@0 | 52 | prompt.show(function(data) { |
michael@0 | 53 | result = data.button; |
michael@0 | 54 | }); |
michael@0 | 55 | |
michael@0 | 56 | // Spin this thread while we wait for a result. |
michael@0 | 57 | let thread = Services.tm.currentThread; |
michael@0 | 58 | while (result == null) { |
michael@0 | 59 | thread.processNextEvent(true); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | if (result == -1) { |
michael@0 | 63 | return null; |
michael@0 | 64 | } |
michael@0 | 65 | return tabs[result].browser.contentWindow; |
michael@0 | 66 | }, |
michael@0 | 67 | |
michael@0 | 68 | notifyStreamStart: function(window) { |
michael@0 | 69 | let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; |
michael@0 | 70 | let tabs = app.tabs; |
michael@0 | 71 | for (var i in tabs) { |
michael@0 | 72 | if (tabs[i].browser.contentWindow == window) { |
michael@0 | 73 | sendMessageToJava({ type: "Tab:StreamStart", tabID: tabs[i].id }); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | }, |
michael@0 | 77 | |
michael@0 | 78 | notifyStreamStop: function(window) { |
michael@0 | 79 | let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; |
michael@0 | 80 | let tabs = app.tabs; |
michael@0 | 81 | for (let i in tabs) { |
michael@0 | 82 | if (tabs[i].browser.contentWindow == window) { |
michael@0 | 83 | sendMessageToJava({ type: "Tab:StreamStop", tabID: tabs[i].id }); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TabSource]); |