1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/components/TabSource.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +"use strict" 1.9 + 1.10 +const { classes: Cc, interfaces: Ci, manager: Cm, utils: Cu, results: Cr } = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/Services.jsm"); 1.13 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.14 +Cu.import("resource://gre/modules/Prompt.jsm"); 1.15 + 1.16 +XPCOMUtils.defineLazyModuleGetter(this, "sendMessageToJava", 1.17 + "resource://gre/modules/Messaging.jsm"); 1.18 + 1.19 +function TabSource() { 1.20 +} 1.21 + 1.22 +TabSource.prototype = { 1.23 + classID: Components.ID("{5850c76e-b916-4218-b99a-31f004e0a7e7}"), 1.24 + classDescription: "Fennec Tab Source", 1.25 + contractID: "@mozilla.org/tab-source-service;1", 1.26 + QueryInterface: XPCOMUtils.generateQI([Ci.nsITabSource]), 1.27 + 1.28 + getTabToStream: function() { 1.29 + let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; 1.30 + let tabs = app.tabs; 1.31 + if (tabs == null || tabs.length == 0) { 1.32 + Services.console.logStringMessage("ERROR: No tabs"); 1.33 + return null; 1.34 + } 1.35 + 1.36 + let bundle = Services.strings.createBundle("chrome://browser/locale/browser.properties"); 1.37 + let title = bundle.GetStringFromName("tabshare.title") 1.38 + 1.39 + let prompt = new Prompt({ 1.40 + title: title, 1.41 + window: null 1.42 + }).setSingleChoiceItems(tabs.map(function(tab) { 1.43 + let label; 1.44 + if (tab.browser.contentTitle) 1.45 + label = tab.browser.contentTitle; 1.46 + else if (tab.browser.contentURI && tab.browser.contentURI.spec) 1.47 + label = tab.browser.contentURI.spec; 1.48 + else 1.49 + label = tab.originalURI; 1.50 + return { label: label, 1.51 + icon: "thumbnail:" + tab.id } 1.52 + })); 1.53 + 1.54 + let result = null; 1.55 + prompt.show(function(data) { 1.56 + result = data.button; 1.57 + }); 1.58 + 1.59 + // Spin this thread while we wait for a result. 1.60 + let thread = Services.tm.currentThread; 1.61 + while (result == null) { 1.62 + thread.processNextEvent(true); 1.63 + } 1.64 + 1.65 + if (result == -1) { 1.66 + return null; 1.67 + } 1.68 + return tabs[result].browser.contentWindow; 1.69 + }, 1.70 + 1.71 + notifyStreamStart: function(window) { 1.72 + let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; 1.73 + let tabs = app.tabs; 1.74 + for (var i in tabs) { 1.75 + if (tabs[i].browser.contentWindow == window) { 1.76 + sendMessageToJava({ type: "Tab:StreamStart", tabID: tabs[i].id }); 1.77 + } 1.78 + } 1.79 + }, 1.80 + 1.81 + notifyStreamStop: function(window) { 1.82 + let app = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; 1.83 + let tabs = app.tabs; 1.84 + for (let i in tabs) { 1.85 + if (tabs[i].browser.contentWindow == window) { 1.86 + sendMessageToJava({ type: "Tab:StreamStop", tabID: tabs[i].id }); 1.87 + } 1.88 + } 1.89 + } 1.90 +}; 1.91 + 1.92 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TabSource]);