1// Copyright 2014 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 5package org.chromium.chrome.browser.tabmodel; 6 7import org.chromium.chrome.browser.Tab; 8import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; 9import org.chromium.content.browser.ContentViewCore; 10 11/** 12 * A set of convenience methods used for interacting with {@link TabList}s and {@link TabModel}s. 13 */ 14public class TabModelUtils { 15 private TabModelUtils() { } 16 17 /** 18 * @param model The {@link TabModel} to act on. 19 * @param index The index of the {@link Tab} to close. 20 * @return {@code true} if the {@link Tab} was found. 21 */ 22 public static boolean closeTabByIndex(TabModel model, int index) { 23 Tab tab = model.getTabAt(index); 24 if (tab == null) return false; 25 26 return model.closeTab(tab); 27 } 28 29 /** 30 * @param model The {@link TabModel} to act on. 31 * @param tabId The id of the {@link Tab} to close. 32 * @return {@code true} if the {@link Tab} was found. 33 */ 34 public static boolean closeTabById(TabModel model, int tabId) { 35 return closeTabById(model, tabId, false); 36 } 37 38 /** 39 * @param model The {@link TabModel} to act on. 40 * @param tabId The id of the {@link Tab} to close. 41 * @param canUndo Whether or not this closure can be undone. 42 * @return {@code true} if the {@link Tab} was found. 43 */ 44 public static boolean closeTabById(TabModel model, int tabId, boolean canUndo) { 45 Tab tab = TabModelUtils.getTabById(model, tabId); 46 if (tab == null) return false; 47 48 return model.closeTab(tab, true, false, canUndo); 49 } 50 51 /** 52 * @param model The {@link TabModel} to act on. 53 * @return {@code true} if the {@link Tab} was found. 54 */ 55 public static boolean closeCurrentTab(TabModel model) { 56 Tab tab = TabModelUtils.getCurrentTab(model); 57 if (tab == null) return false; 58 59 return model.closeTab(tab); 60 } 61 62 /** 63 * Find the index of the {@link Tab} with the specified id. 64 * @param model The {@link TabModel} to act on. 65 * @param tabId The id of the {@link Tab} to find. 66 * @return Specified {@link Tab} index or {@link TabList#INVALID_TAB_INDEX} if the 67 * {@link Tab} is not found 68 */ 69 public static int getTabIndexById(TabList model, int tabId) { 70 int count = model.getCount(); 71 72 for (int i = 0; i < count; i++) { 73 Tab tab = model.getTabAt(i); 74 if (tab.getId() == tabId) return i; 75 } 76 77 return TabModel.INVALID_TAB_INDEX; 78 } 79 80 /** 81 * Find the {@link Tab} with the specified id. 82 * @param model The {@link TabModel} to act on. 83 * @param tabId The id of the {@link Tab} to find. 84 * @return Specified {@link Tab} or {@code null} if the {@link Tab} is not found 85 */ 86 public static Tab getTabById(TabList model, int tabId) { 87 int index = getTabIndexById(model, tabId); 88 if (index == TabModel.INVALID_TAB_INDEX) return null; 89 return model.getTabAt(index); 90 } 91 92 /** 93 * Find the {@link Tab} index whose URL matches the specified URL. 94 * @param model The {@link TabModel} to act on. 95 * @param url The URL to search for. 96 * @return Specified {@link Tab} or {@code null} if the {@link Tab} is not found 97 */ 98 public static int getTabIndexByUrl(TabList model, String url) { 99 int count = model.getCount(); 100 101 for (int i = 0; i < count; i++) { 102 if (model.getTabAt(i).getUrl().contentEquals(url)) return i; 103 } 104 105 return TabModel.INVALID_TAB_INDEX; 106 } 107 108 /** 109 * Get the currently selected {@link Tab} id. 110 * @param model The {@link TabModel} to act on. 111 * @return The id of the currently selected {@link Tab}. 112 */ 113 public static int getCurrentTabId(TabList model) { 114 Tab tab = getCurrentTab(model); 115 if (tab == null) return Tab.INVALID_TAB_ID; 116 117 return tab.getId(); 118 } 119 120 /** 121 * Get the currently selected {@link Tab}. 122 * @param model The {@link TabModel} to act on. 123 * @returns The current {@link Tab} or {@code null} if no {@link Tab} is selected 124 */ 125 public static Tab getCurrentTab(TabList model) { 126 int index = model.index(); 127 if (index == TabModel.INVALID_TAB_INDEX) return null; 128 129 return model.getTabAt(index); 130 } 131 132 /** 133 * @param model The {@link TabModel} to act on. 134 * @return The currently active {@link ContentViewCore}, or {@code null} if no {@link Tab} 135 * is selected or the selected {@link Tab} has no current {@link ContentViewCore}. 136 */ 137 public static ContentViewCore getCurrentContentViewCore(TabList model) { 138 Tab tab = getCurrentTab(model); 139 if (tab == null) return null; 140 141 return tab.getContentViewCore(); 142 } 143 144 /** 145 * A helper method that automatically passes {@link TabSelectionType#FROM_USER} as the selection 146 * type to {@link TabModel#setIndex(int, TabSelectionType)}. 147 * @param model The {@link TabModel} to act on. 148 * @param index The index of the {@link Tab} to select. 149 */ 150 public static void setIndex(TabModel model, int index) { 151 model.setIndex(index, TabSelectionType.FROM_USER); 152 } 153 154}