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.shell;
7 import android.content.Context;
8 import android.util.AttributeSet;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.FrameLayout;
12 import android.widget.LinearLayout;
14 import org.chromium.chrome.browser.EmptyTabObserver;
15 import org.chromium.chrome.browser.Tab;
16 import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver;
17 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
18 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType;
19 import org.chromium.content.browser.ContentVideoViewClient;
20 import org.chromium.content.browser.ContentViewCore;
21 import org.chromium.content.browser.ContentViewRenderView;
22 import org.chromium.content_public.browser.LoadUrlParams;
23 import org.chromium.ui.base.PageTransition;
24 import org.chromium.ui.base.WindowAndroid;
27 * The TabManager hooks together all of the related {@link View}s that are used to represent
28 * a {@link ChromeShellTab}. It properly builds a {@link ChromeShellTab} and makes sure that the
29 * {@link ChromeShellToolbar} and {@link ContentViewRenderView} show the proper content.
31 public class TabManager extends LinearLayout {
32 private static final String DEFAULT_URL = "http://www.google.com";
34 private ViewGroup mContentViewHolder;
35 private ContentViewRenderView mContentViewRenderView;
36 private ChromeShellToolbar mToolbar;
38 private ChromeShellTab mCurrentTab;
40 private String mStartupUrl = DEFAULT_URL;
42 private ChromeShellTabModelSelector mTabModelSelector;
44 private final EmptyTabModelObserver mTabModelObserver = new EmptyTabModelObserver() {
46 public void didSelectTab(Tab tab, TabSelectionType type, int lastId) {
47 assert tab instanceof ChromeShellTab;
48 setCurrentTab((ChromeShellTab) tab);
49 mTabModelSelector.hideTabSwitcher();
53 public void willCloseTab(Tab tab, boolean animate) {
54 if (tab == mCurrentTab) setCurrentTab(null);
59 * @param context The Context the view is running in.
60 * @param attrs The attributes of the XML tag that is inflating the view.
62 public TabManager(Context context, AttributeSet attrs) {
63 super(context, attrs);
67 * Initialize the components required for Tab creation.
68 * @param window The window used to generate all ContentViews.
69 * @param videoViewClient The client to handle interactions from ContentVideoViews.
71 public void initialize(WindowAndroid window, ContentVideoViewClient videoViewClient) {
72 assert window != null;
73 assert videoViewClient != null;
75 mContentViewHolder = (ViewGroup) findViewById(R.id.content_container);
77 mTabModelSelector = new ChromeShellTabModelSelector(
78 window, videoViewClient, mContentViewHolder, this);
79 mTabModelSelector.getModel(false).addObserver(mTabModelObserver);
81 mToolbar = (ChromeShellToolbar) findViewById(R.id.toolbar);
82 mToolbar.setTabManager(this);
83 mContentViewRenderView = new ContentViewRenderView(getContext()) {
85 protected void onReadyToRender() {
86 if (mCurrentTab == null) createTab(mStartupUrl, TabLaunchType.FROM_RESTORE);
89 mContentViewRenderView.onNativeLibraryLoaded(window);
90 mContentViewHolder.addView(mContentViewRenderView,
91 new FrameLayout.LayoutParams(
92 FrameLayout.LayoutParams.MATCH_PARENT,
93 FrameLayout.LayoutParams.MATCH_PARENT));
97 * @param startupUrl The URL that the first tab should navigate to.
99 public void setStartupUrl(String startupUrl) {
100 mStartupUrl = startupUrl;
104 * Enter or leave overlay video mode.
105 * @param enabled Whether overlay mode is enabled.
107 public void setOverlayVideoMode(boolean enabled) {
108 if (mContentViewRenderView == null) return;
109 mContentViewRenderView.setOverlayVideoMode(enabled);
113 * @return The currently visible {@link ChromeShellTab}.
115 public ChromeShellTab getCurrentTab() {
120 * Ensures that at least one tab exists, by opening a new one if necessary.
122 public void ensureTabExists() {
123 if (mTabModelSelector.getCurrentModel().getCount() == 0) {
129 * Opens a new blank tab.
131 public void createNewTab() {
132 createTab("about:blank", TabLaunchType.FROM_MENU_OR_OVERVIEW);
136 * Closes all current tabs.
138 public void closeAllTabs() {
139 mTabModelSelector.getCurrentModel().closeAllTabs();
143 * Creates a {@link ChromeShellTab} with a URL specified by {@code url}.
144 * @param url The URL the new {@link ChromeShellTab} should start with.
145 * @return The newly created tab, or null if the content view is uninitialized.
147 public Tab createTab(String url, TabLaunchType type) {
148 if (!isContentViewRenderViewInitialized()) return null;
150 LoadUrlParams loadUrlParams = new LoadUrlParams(url);
151 Tab tab = mTabModelSelector.openNewTab(loadUrlParams, type, null, false);
152 tab.addObserver(new EmptyTabObserver() {
154 public void onToggleFullscreenMode(Tab tab, boolean enable) {
155 mToolbar.setVisibility(enable ? GONE : VISIBLE);
161 private boolean isContentViewRenderViewInitialized() {
162 return mContentViewRenderView != null && mContentViewRenderView.isInitialized();
165 private void setCurrentTab(ChromeShellTab tab) {
166 if (mCurrentTab != null) {
167 mContentViewHolder.removeView(mCurrentTab.getView());
172 mToolbar.showTab(mCurrentTab);
174 if (mCurrentTab != null) setupContent();
177 private void setupContent() {
178 View view = mCurrentTab.getView();
179 ContentViewCore contentViewCore = mCurrentTab.getContentViewCore();
180 mContentViewHolder.addView(view);
181 mContentViewRenderView.setCurrentContentViewCore(contentViewCore);
183 contentViewCore.onShow();
187 * Toggles the tab switcher visibility.
189 public void toggleTabSwitcher() {
190 mTabModelSelector.toggleTabSwitcher();
193 public boolean isTabSwitcherVisible() {
194 return mTabModelSelector.isTabSwitcherVisible();
197 public void hideTabSwitcher() {
198 mTabModelSelector.hideTabSwitcher();
202 * Opens a URL in the current tab if one exists, or in a new tab otherwise.
203 * @param url The URL to open.
204 * @return The tab used to open the provided URL.
206 public Tab openUrl(String url) {
207 LoadUrlParams loadUrlParams = new LoadUrlParams(url);
208 loadUrlParams.setTransitionType(PageTransition.TYPED | PageTransition.FROM_ADDRESS_BAR);
209 Tab tab = mTabModelSelector.getCurrentTab();
211 tab.loadUrl(loadUrlParams);
214 return createTab(url, TabLaunchType.FROM_KEYBOARD);