Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / android / shell / java / src / org / chromium / chrome / shell / TabManager.java
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
5 package org.chromium.chrome.shell;
6
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;
13
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;
25
26 /**
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.
30  */
31 public class TabManager extends LinearLayout {
32     private static final String DEFAULT_URL = "http://www.google.com";
33
34     private ViewGroup mContentViewHolder;
35     private ContentViewRenderView mContentViewRenderView;
36     private ChromeShellToolbar mToolbar;
37
38     private ChromeShellTab mCurrentTab;
39
40     private String mStartupUrl = DEFAULT_URL;
41
42     private ChromeShellTabModelSelector mTabModelSelector;
43
44     private final EmptyTabModelObserver mTabModelObserver = new EmptyTabModelObserver() {
45         @Override
46         public void didSelectTab(Tab tab, TabSelectionType type, int lastId) {
47             assert tab instanceof ChromeShellTab;
48             setCurrentTab((ChromeShellTab) tab);
49             mTabModelSelector.hideTabSwitcher();
50         }
51
52         @Override
53         public void willCloseTab(Tab tab, boolean animate) {
54             if (tab == mCurrentTab) setCurrentTab(null);
55         }
56     };
57
58     /**
59      * @param context The Context the view is running in.
60      * @param attrs   The attributes of the XML tag that is inflating the view.
61      */
62     public TabManager(Context context, AttributeSet attrs) {
63         super(context, attrs);
64     }
65
66     /**
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.
70      */
71     public void initialize(WindowAndroid window, ContentVideoViewClient videoViewClient) {
72         assert window != null;
73         assert videoViewClient != null;
74
75         mContentViewHolder = (ViewGroup) findViewById(R.id.content_container);
76
77         mTabModelSelector = new ChromeShellTabModelSelector(
78                 window, videoViewClient, mContentViewHolder, this);
79         mTabModelSelector.getModel(false).addObserver(mTabModelObserver);
80
81         mToolbar = (ChromeShellToolbar) findViewById(R.id.toolbar);
82         mToolbar.setTabManager(this);
83         mContentViewRenderView = new ContentViewRenderView(getContext()) {
84             @Override
85             protected void onReadyToRender() {
86                 if (mCurrentTab == null) createTab(mStartupUrl, TabLaunchType.FROM_RESTORE);
87             }
88         };
89         mContentViewRenderView.onNativeLibraryLoaded(window);
90         mContentViewHolder.addView(mContentViewRenderView,
91                 new FrameLayout.LayoutParams(
92                         FrameLayout.LayoutParams.MATCH_PARENT,
93                         FrameLayout.LayoutParams.MATCH_PARENT));
94     }
95
96     /**
97      * @param startupUrl The URL that the first tab should navigate to.
98      */
99     public void setStartupUrl(String startupUrl) {
100         mStartupUrl = startupUrl;
101     }
102
103     /**
104      * Enter or leave overlay video mode.
105      * @param enabled Whether overlay mode is enabled.
106      */
107     public void setOverlayVideoMode(boolean enabled) {
108         if (mContentViewRenderView == null) return;
109         mContentViewRenderView.setOverlayVideoMode(enabled);
110     }
111
112     /**
113      * @return The currently visible {@link ChromeShellTab}.
114      */
115     public ChromeShellTab getCurrentTab() {
116         return mCurrentTab;
117     }
118
119     /**
120      * Ensures that at least one tab exists, by opening a new one if necessary.
121      */
122     public void ensureTabExists() {
123         if (mTabModelSelector.getCurrentModel().getCount() == 0) {
124             createNewTab();
125         }
126     }
127
128     /**
129      * Opens a new blank tab.
130      */
131     public void createNewTab() {
132         createTab("about:blank", TabLaunchType.FROM_MENU_OR_OVERVIEW);
133     }
134
135     /**
136      * Closes all current tabs.
137      */
138     public void closeAllTabs() {
139         mTabModelSelector.getCurrentModel().closeAllTabs();
140     }
141
142     /**
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.
146      */
147     public Tab createTab(String url, TabLaunchType type) {
148         if (!isContentViewRenderViewInitialized()) return null;
149
150         LoadUrlParams loadUrlParams = new LoadUrlParams(url);
151         Tab tab = mTabModelSelector.openNewTab(loadUrlParams, type, null, false);
152         tab.addObserver(new EmptyTabObserver() {
153             @Override
154             public void onToggleFullscreenMode(Tab tab, boolean enable) {
155                 mToolbar.setVisibility(enable ? GONE : VISIBLE);
156             }
157         });
158         return tab;
159     }
160
161     private boolean isContentViewRenderViewInitialized() {
162         return mContentViewRenderView != null && mContentViewRenderView.isInitialized();
163     }
164
165     private void setCurrentTab(ChromeShellTab tab) {
166         if (mCurrentTab != null) {
167             mContentViewHolder.removeView(mCurrentTab.getView());
168         }
169
170         mCurrentTab = tab;
171
172         mToolbar.showTab(mCurrentTab);
173
174         if (mCurrentTab != null) setupContent();
175     }
176
177     private void setupContent() {
178         View view = mCurrentTab.getView();
179         ContentViewCore contentViewCore = mCurrentTab.getContentViewCore();
180         mContentViewHolder.addView(view);
181         mContentViewRenderView.setCurrentContentViewCore(contentViewCore);
182         view.requestFocus();
183         contentViewCore.onShow();
184     }
185
186     /**
187      * Toggles the tab switcher visibility.
188      */
189     public void toggleTabSwitcher() {
190         mTabModelSelector.toggleTabSwitcher();
191     }
192
193     public boolean isTabSwitcherVisible() {
194         return mTabModelSelector.isTabSwitcherVisible();
195     }
196
197     public void hideTabSwitcher() {
198         mTabModelSelector.hideTabSwitcher();
199     }
200
201     /**
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.
205      */
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();
210         if (tab != null) {
211             tab.loadUrl(loadUrlParams);
212             return tab;
213         }
214         return createTab(url, TabLaunchType.FROM_KEYBOARD);
215     }
216 }