1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Show a list of all tabs in the same process as this one.
6function init() {
7  chrome.windows.getCurrent({populate: true}, function(currentWindow) {
8    chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
9      var current = currentWindow.tabs.filter(function(tab) {
10        return tab.active;
11      })[0];
12      chrome.experimental.processes.getProcessIdForTab(current.id,
13        function(pid) {
14          var outputDiv = document.getElementById("tab-list");
15          var titleDiv = document.getElementById("title");
16          titleDiv.innerHTML = "<b>Tabs in Process " + pid + ":</b>";
17          displayTabInfo(currentWindow.id, current, outputDiv);
18          displaySameProcessTabs(current, pid, outputDiv);
19        }
20      );
21
22    });
23  });
24}
25
26function displaySameProcessTabs(selectedTab, processId, outputDiv) {
27  // Loop over all windows and their tabs
28  var tabs = [];
29  chrome.windows.getAll({ populate: true }, function(windowList) {
30    for (var i = 0; i < windowList.length; i++) {
31      for (var j = 0; j < windowList[i].tabs.length; j++) {
32        var tab = windowList[i].tabs[j];
33        if (tab.id != selectedTab.id) {
34          tabs.push(tab);
35        }
36      }
37    }
38
39    // Display tab in list if it is in the same process
40    tabs.forEach(function(tab) {
41      chrome.experimental.processes.getProcessIdForTab(tab.id,
42        function(pid) {
43          if (pid == processId) {
44            displayTabInfo(tab.windowId, tab, outputDiv);
45          }
46        }
47      );
48    });
49  });
50}
51
52// Print a link to a given tab
53function displayTabInfo(windowId, tab, outputDiv) {
54  if (tab.favIconUrl != undefined) {
55    outputDiv.innerHTML += "<img src='chrome://favicon/" + tab.url + "'>\n";
56  }
57  outputDiv.innerHTML +=
58    "<b><a href='#' onclick='showTab(window, " + windowId + ", " + tab.id +
59    ")'>" + tab.title + "</a></b><br>\n" +
60    "<i>" + tab.url + "</i><br>\n";
61}
62
63// Bring the selected tab to the front
64function showTab(origWindow, windowId, tabId) {
65  // TODO: Bring the window to the front.  (See http://crbug.com/31434)
66  //chrome.windows.update(windowId, {focused: true});
67  chrome.tabs.update(tabId, { selected: true });
68  origWindow.close();
69}
70
71// Kick things off.
72document.addEventListener('DOMContentLoaded', init);
73