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.
5 package org.chromium.chrome.browser.tabmodel;
7 import org.chromium.chrome.browser.Tab;
8 import org.chromium.chrome.browser.profiles.Profile;
11 * TabModel organizes all the open tabs and allows you to create new ones. There are two TabModels
12 * in the app at this time: normal and incognito. More could be added to allow for windows or
15 public interface TabModel extends TabList {
17 * A list of the various ways tabs can be launched.
19 public enum TabLaunchType {
20 FROM_LINK, // Opened from a link.
21 FROM_EXTERNAL_APP, // Opened by and external app.
22 FROM_MENU_OR_OVERVIEW, // Opened from the options menu or the tab stack overview.
23 FROM_RESTORE, // Opened after restoring state from storage.
24 // Opened from the long press menu. Like FROM_MENU but also sets up a parent/child
25 // relationship like FROM_LINK. FOREGROUND and BACKGROUND indicates whether the current tab
26 // should be automatically switched to the new tab or not.
27 FROM_LONGPRESS_FOREGROUND,
28 FROM_LONGPRESS_BACKGROUND,
29 FROM_INSTANT, // Tab was created by instant.
30 FROM_KEYBOARD // Opened from a physical keyboard via shortcut.
34 * A list of the various ways tabs can eb selected.
36 public enum TabSelectionType {
37 FROM_CLOSE, // Selection of adjacent tab when the active tab is closed in foreground.
38 FROM_EXIT, // Selection of adjacent tab when the active tab is closed upon app exit.
39 FROM_NEW, // Selection of newly created tab (e.g. for a url intent or NTP).
40 FROM_USER // User-originated switch to existing tab or selection of main tab on app
45 * @return The profile associated with the current model.
47 public Profile getProfile();
50 * Unregisters and destroys the specified tab, and then switches to the previous tab.
51 * @param tab The non-null tab to close
52 * @return true if the tab was found
54 public boolean closeTab(Tab tab);
57 * Unregisters and destroys the specified tab, and then switches to the previous tab.
59 * @param tab The non-null tab to close
60 * @param animate true iff the closing animation should be displayed
61 * @param uponExit true iff the tab is being closed upon application exit (after user presses
62 * the system back button)
63 * @param canUndo Whether or not this action can be undone. If this is {@code true} and
64 * {@link #supportsPendingClosures()} is {@code true}, this {@link Tab}
65 * will not actually be closed until {@link #commitTabClosure(int)} or
66 * {@link #commitAllTabClosures()} is called, but it will be effectively removed
67 * from this list. To get a comprehensive list of all tabs, including ones that
68 * have been partially closed, use the {@link TabList} from
69 * {@link #getComprehensiveModel()}.
70 * @return true if the tab was found
72 public boolean closeTab(Tab tab, boolean animate, boolean uponExit, boolean canUndo);
75 * Returns which tab would be selected if the specified tab {@code id} were closed.
76 * @param id The ID of tab which would be closed.
77 * @return The id of the next tab that would be visible.
79 public Tab getNextTabIfClosed(int id);
82 * Close all the tabs on this model.
84 public void closeAllTabs();
87 * Close all tabs on this model. If allowDelegation is true, the model has the option
88 * of not closing all tabs and delegating the closure to another class.
89 * @param allowDelegation true iff the model may delegate the close all request.
90 * false iff the model must close all tabs.
91 * @param uponExit true iff the tabs are being closed upon application exit (after user presses
92 * the system back button)
94 public void closeAllTabs(boolean allowDelegation, boolean uponExit);
97 * @return Whether or not this model supports pending closures.
99 public boolean supportsPendingClosures();
102 * Commits all pending closures, closing all tabs that had a chance to be undone.
104 public void commitAllTabClosures();
107 * Commits a pending closure specified by {@code tabId}.
108 * @param tabId The id of the {@link Tab} to commit the pending closure.
110 public void commitTabClosure(int tabId);
113 * Cancels a pending {@link Tab} closure, bringing the tab back into this model. Note that this
114 * will select the rewound {@link Tab}.
115 * @param tabId The id of the {@link Tab} to undo.
117 public void cancelTabClosure(int tabId);
120 * @return The complete {@link TabList} this {@link TabModel} represents. Note that this may
121 * be different than this actual {@link TabModel} if it supports pending closures
122 * {@link #supportsPendingClosures()}, as this will include all pending closure tabs.
124 public TabList getComprehensiveModel();
127 * Selects a tab by its index.
128 * @param i The index of the tab to select.
129 * @param type The type of selection.
131 public void setIndex(int i, final TabSelectionType type);
134 * Moves a tab to a new index.
135 * @param id The id of the tab to move.
136 * @param newIndex The new place to put the tab.
138 public void moveTab(int id, int newIndex);
141 * To be called when this model should be destroyed. The model should no longer be used after
144 public void destroy();
147 * Adds a newly created tab to this model.
148 * @param tab The tab to be added.
149 * @param index The index where the tab should be inserted. The model may override the index.
150 * @param type How the tab was opened.
152 void addTab(Tab tab, int index, TabLaunchType type);
155 * Subscribes a {@link TabModelObserver} to be notified about changes to this model.
156 * @param observer The observer to be subscribed.
158 void addObserver(TabModelObserver observer);
161 * Unsubscribes a previously subscribed {@link TabModelObserver}.
162 * @param observer The observer to be unsubscribed.
164 void removeObserver(TabModelObserver observer);