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