utils.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'stability': 'unstable'
  7. };
  8. // NOTE: This file should only deal with xul/native tabs
  9. const { Ci } = require('chrome');
  10. const { defer } = require("../lang/functional");
  11. const { windows, isBrowser } = require('../window/utils');
  12. const { isPrivateBrowsingSupported } = require('../self');
  13. const { isGlobalPBSupported } = require('../private-browsing/utils');
  14. // Bug 834961: ignore private windows when they are not supported
  15. function getWindows() windows(null, { includePrivate: isPrivateBrowsingSupported || isGlobalPBSupported });
  16. function activateTab(tab, window) {
  17. let gBrowser = getTabBrowserForTab(tab);
  18. // normal case
  19. if (gBrowser) {
  20. gBrowser.selectedTab = tab;
  21. }
  22. // fennec ?
  23. else if (window && window.BrowserApp) {
  24. window.BrowserApp.selectTab(tab);
  25. }
  26. return null;
  27. }
  28. exports.activateTab = activateTab;
  29. function getTabBrowser(window) {
  30. return window.gBrowser;
  31. }
  32. exports.getTabBrowser = getTabBrowser;
  33. function getTabContainer(window) {
  34. return getTabBrowser(window).tabContainer;
  35. }
  36. exports.getTabContainer = getTabContainer;
  37. /**
  38. * Returns the tabs for the `window` if given, or the tabs
  39. * across all the browser's windows otherwise.
  40. *
  41. * @param {nsIWindow} [window]
  42. * A reference to a window
  43. *
  44. * @returns {Array} an array of Tab objects
  45. */
  46. function getTabs(window) {
  47. if (arguments.length === 0) {
  48. return getWindows().filter(isBrowser).reduce(function(tabs, window) {
  49. return tabs.concat(getTabs(window))
  50. }, []);
  51. }
  52. // fennec
  53. if (window.BrowserApp)
  54. return window.BrowserApp.tabs;
  55. // firefox - default
  56. return Array.slice(getTabContainer(window).children);
  57. }
  58. exports.getTabs = getTabs;
  59. function getActiveTab(window) {
  60. return getSelectedTab(window);
  61. }
  62. exports.getActiveTab = getActiveTab;
  63. function getOwnerWindow(tab) {
  64. // normal case
  65. if (tab.ownerDocument)
  66. return tab.ownerDocument.defaultView;
  67. // try fennec case
  68. return getWindowHoldingTab(tab);
  69. }
  70. exports.getOwnerWindow = getOwnerWindow;
  71. // fennec
  72. function getWindowHoldingTab(rawTab) {
  73. for each (let window in getWindows()) {
  74. // this function may be called when not using fennec,
  75. // but BrowserApp is only defined on Fennec
  76. if (!window.BrowserApp)
  77. continue;
  78. for each (let tab in window.BrowserApp.tabs) {
  79. if (tab === rawTab)
  80. return window;
  81. }
  82. }
  83. return null;
  84. }
  85. function openTab(window, url, options) {
  86. options = options || {};
  87. // fennec?
  88. if (window.BrowserApp) {
  89. return window.BrowserApp.addTab(url, {
  90. selected: options.inBackground ? false : true,
  91. pinned: options.isPinned || false,
  92. isPrivate: options.isPrivate || false
  93. });
  94. }
  95. // firefox
  96. let newTab = window.gBrowser.addTab(url);
  97. if (!options.inBackground) {
  98. activateTab(newTab);
  99. }
  100. return newTab;
  101. };
  102. exports.openTab = openTab;
  103. function isTabOpen(tab) {
  104. // try normal case then fennec case
  105. return !!((tab.linkedBrowser) || getWindowHoldingTab(tab));
  106. }
  107. exports.isTabOpen = isTabOpen;
  108. function closeTab(tab) {
  109. let gBrowser = getTabBrowserForTab(tab);
  110. // normal case?
  111. if (gBrowser) {
  112. // Bug 699450: the tab may already have been detached
  113. if (!tab.parentNode)
  114. return;
  115. return gBrowser.removeTab(tab);
  116. }
  117. let window = getWindowHoldingTab(tab);
  118. // fennec?
  119. if (window && window.BrowserApp) {
  120. // Bug 699450: the tab may already have been detached
  121. if (!tab.browser)
  122. return;
  123. return window.BrowserApp.closeTab(tab);
  124. }
  125. return null;
  126. }
  127. exports.closeTab = closeTab;
  128. function getURI(tab) {
  129. if (tab.browser) // fennec
  130. return tab.browser.currentURI.spec;
  131. return tab.linkedBrowser.currentURI.spec;
  132. }
  133. exports.getURI = getURI;
  134. function getTabBrowserForTab(tab) {
  135. let outerWin = getOwnerWindow(tab);
  136. if (outerWin)
  137. return getOwnerWindow(tab).gBrowser;
  138. return null;
  139. }
  140. exports.getTabBrowserForTab = getTabBrowserForTab;
  141. function getBrowserForTab(tab) {
  142. if (tab.browser) // fennec
  143. return tab.browser;
  144. return tab.linkedBrowser;
  145. }
  146. exports.getBrowserForTab = getBrowserForTab;
  147. function getTabId(tab) {
  148. if (tab.browser) // fennec
  149. return tab.id
  150. return String.split(tab.linkedPanel, 'panel').pop();
  151. }
  152. exports.getTabId = getTabId;
  153. function getTabTitle(tab) {
  154. return getBrowserForTab(tab).contentDocument.title || tab.label || "";
  155. }
  156. exports.getTabTitle = getTabTitle;
  157. function setTabTitle(tab, title) {
  158. title = String(title);
  159. if (tab.browser)
  160. tab.browser.contentDocument.title = title;
  161. tab.label = String(title);
  162. }
  163. exports.setTabTitle = setTabTitle;
  164. function getTabContentWindow(tab) {
  165. return getBrowserForTab(tab).contentWindow;
  166. }
  167. exports.getTabContentWindow = getTabContentWindow;
  168. /**
  169. * Returns all tabs' content windows across all the browsers' windows
  170. */
  171. function getAllTabContentWindows() {
  172. return getTabs().map(getTabContentWindow);
  173. }
  174. exports.getAllTabContentWindows = getAllTabContentWindows;
  175. // gets the tab containing the provided window
  176. function getTabForContentWindow(window) {
  177. // Retrieve the topmost frame container. It can be either <xul:browser>,
  178. // <xul:iframe/> or <html:iframe/>. But in our case, it should be xul:browser.
  179. let browser;
  180. try {
  181. browser = window.QueryInterface(Ci.nsIInterfaceRequestor)
  182. .getInterface(Ci.nsIWebNavigation)
  183. .QueryInterface(Ci.nsIDocShell)
  184. .chromeEventHandler;
  185. } catch(e) {
  186. // Bug 699450: The tab may already have been detached so that `window` is
  187. // in a almost destroyed state and can't be queryinterfaced anymore.
  188. }
  189. // Is null for toplevel documents
  190. if (!browser) {
  191. return null;
  192. }
  193. // Retrieve the owner window, should be browser.xul one
  194. let chromeWindow = browser.ownerDocument.defaultView;
  195. // Ensure that it is top-level browser window.
  196. // We need extra checks because of Mac hidden window that has a broken
  197. // `gBrowser` global attribute.
  198. if ('gBrowser' in chromeWindow && chromeWindow.gBrowser &&
  199. 'browsers' in chromeWindow.gBrowser) {
  200. // Looks like we are on Firefox Desktop
  201. // Then search for the position in tabbrowser in order to get the tab object
  202. let browsers = chromeWindow.gBrowser.browsers;
  203. let i = browsers.indexOf(browser);
  204. if (i !== -1)
  205. return chromeWindow.gBrowser.tabs[i];
  206. return null;
  207. }
  208. // Fennec
  209. else if ('BrowserApp' in chromeWindow) {
  210. return getTabForWindow(window);
  211. }
  212. return null;
  213. }
  214. exports.getTabForContentWindow = getTabForContentWindow;
  215. // used on fennec
  216. function getTabForWindow(window) {
  217. for each (let { BrowserApp } in getWindows()) {
  218. if (!BrowserApp)
  219. continue;
  220. for each (let tab in BrowserApp.tabs) {
  221. if (tab.browser.contentWindow == window.top)
  222. return tab;
  223. }
  224. }
  225. return null;
  226. }
  227. function getTabURL(tab) {
  228. if (tab.browser) // fennec
  229. return String(tab.browser.currentURI.spec);
  230. return String(getBrowserForTab(tab).currentURI.spec);
  231. }
  232. exports.getTabURL = getTabURL;
  233. function setTabURL(tab, url) {
  234. url = String(url);
  235. if (tab.browser)
  236. return tab.browser.loadURI(url);
  237. return getBrowserForTab(tab).loadURI(url);
  238. }
  239. // "TabOpen" event is fired when it's still "about:blank" is loaded in the
  240. // changing `location` property of the `contentDocument` has no effect since
  241. // seems to be either ignored or overridden by internal listener, there for
  242. // location change is enqueued for the next turn of event loop.
  243. exports.setTabURL = defer(setTabURL);
  244. function getTabContentType(tab) {
  245. return getBrowserForTab(tab).contentDocument.contentType;
  246. }
  247. exports.getTabContentType = getTabContentType;
  248. function getSelectedTab(window) {
  249. if (window.BrowserApp) // fennec?
  250. return window.BrowserApp.selectedTab;
  251. if (window.gBrowser)
  252. return window.gBrowser.selectedTab;
  253. return null;
  254. }
  255. exports.getSelectedTab = getSelectedTab;
  256. function getTabForBrowser(browser) {
  257. for each (let window in getWindows()) {
  258. // this function may be called when not using fennec
  259. if (!window.BrowserApp)
  260. continue;
  261. for each (let tab in window.BrowserApp.tabs) {
  262. if (tab.browser === browser)
  263. return tab;
  264. }
  265. }
  266. return null;
  267. }
  268. exports.getTabForBrowser = getTabForBrowser;
  269. function pin(tab) {
  270. let gBrowser = getTabBrowserForTab(tab);
  271. // TODO: Implement Fennec support
  272. if (gBrowser) gBrowser.pinTab(tab);
  273. }
  274. exports.pin = pin;
  275. function unpin(tab) {
  276. let gBrowser = getTabBrowserForTab(tab);
  277. // TODO: Implement Fennec support
  278. if (gBrowser) gBrowser.unpinTab(tab);
  279. }
  280. exports.unpin = unpin;
  281. function isPinned(tab) !!tab.pinned
  282. exports.isPinned = isPinned;
  283. function reload(tab) {
  284. let gBrowser = getTabBrowserForTab(tab);
  285. // Firefox
  286. if (gBrowser) gBrowser.unpinTab(tab);
  287. // Fennec
  288. else if (tab.browser) tab.browser.reload();
  289. }
  290. exports.reload = reload
  291. function getIndex(tab) {
  292. let gBrowser = getTabBrowserForTab(tab);
  293. // Firefox
  294. if (gBrowser) {
  295. let document = getBrowserForTab(tab).contentDocument;
  296. return gBrowser.getBrowserIndexForDocument(document);
  297. }
  298. // Fennec
  299. else {
  300. let window = getWindowHoldingTab(tab)
  301. let tabs = window.BrowserApp.tabs;
  302. for (let i = tabs.length; i >= 0; i--)
  303. if (tabs[i] === tab) return i;
  304. }
  305. }
  306. exports.getIndex = getIndex;
  307. function move(tab, index) {
  308. let gBrowser = getTabBrowserForTab(tab);
  309. // Firefox
  310. if (gBrowser) gBrowser.moveTabTo(tab, index);
  311. // TODO: Implement fennec support
  312. }
  313. exports.move = move;