blob: da68b55170e480fb7aa7329dc46147afc26e7c8b [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.os.Bundle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.util.Log;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080021
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070022import org.codeaurora.swe.GeolocationPermissions;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080023import org.codeaurora.swe.WebView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import java.util.ArrayList;
Elliott Slaughter3d6df162010-08-25 13:17:44 -070026import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080027import java.util.List;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import java.util.Vector;
29
30class TabControl {
31 // Log Tag
32 private static final String LOGTAG = "TabControl";
Michael Kolbc831b632011-05-11 09:30:34 -070033
John Reckd8c74522011-06-14 08:45:00 -070034 // next Tab ID, starting at 1
35 private static long sNextId = 1;
Michael Kolbc831b632011-05-11 09:30:34 -070036
37 private static final String POSITIONS = "positions";
38 private static final String CURRENT = "current";
39
John Reck8ee633f2011-08-09 16:00:35 -070040 public static interface OnThumbnailUpdatedListener {
41 void onThumbnailUpdated(Tab t);
42 }
43
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 // Maximum number of tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070045 private int mMaxTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 // Private array of WebViews that are used as tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070047 private ArrayList<Tab> mTabs;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048 // Queue of most recently viewed tabs.
Michael Kolb6e4653e2010-09-27 16:22:38 -070049 private ArrayList<Tab> mTabQueue;
The Android Open Source Project0c908882009-03-03 19:32:16 -080050 // Current position in mTabs.
51 private int mCurrentTab = -1;
Michael Kolb8233fac2010-10-26 16:08:53 -070052 // the main browser controller
53 private final Controller mController;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070054 // number of incognito tabs
55 private int mNumIncognito = 0;
Michael Kolb8233fac2010-10-26 16:08:53 -070056
John Reck8ee633f2011-08-09 16:00:35 -070057 private OnThumbnailUpdatedListener mOnThumbnailUpdatedListener;
The Android Open Source Project0c908882009-03-03 19:32:16 -080058
59 /**
Michael Kolb8233fac2010-10-26 16:08:53 -070060 * Construct a new TabControl object
The Android Open Source Project0c908882009-03-03 19:32:16 -080061 */
Michael Kolb8233fac2010-10-26 16:08:53 -070062 TabControl(Controller controller) {
63 mController = controller;
Michael Kolb8233fac2010-10-26 16:08:53 -070064 mMaxTabs = mController.getMaxTabs();
Michael Kolb6e4653e2010-09-27 16:22:38 -070065 mTabs = new ArrayList<Tab>(mMaxTabs);
66 mTabQueue = new ArrayList<Tab>(mMaxTabs);
The Android Open Source Project0c908882009-03-03 19:32:16 -080067 }
68
John Reck52be4782011-08-26 15:37:29 -070069 synchronized static long getNextId() {
Michael Kolbc831b632011-05-11 09:30:34 -070070 return sNextId++;
71 }
72
Michael Kolb68775752010-08-19 12:42:01 -070073 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 * Return the current tab's main WebView. This will always return the main
75 * WebView for a given tab and not a subwindow.
76 * @return The current tab's WebView.
77 */
78 WebView getCurrentWebView() {
79 Tab t = getTab(mCurrentTab);
80 if (t == null) {
81 return null;
82 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070083 return t.getWebView();
Ben Murdochbff2d602009-07-01 20:19:05 +010084 }
85
86 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080087 * Return the current tab's top-level WebView. This can return a subwindow
88 * if one exists.
89 * @return The top-level WebView of the current tab.
90 */
91 WebView getCurrentTopWebView() {
92 Tab t = getTab(mCurrentTab);
93 if (t == null) {
94 return null;
95 }
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 return t.getTopWindow();
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 }
98
99 /**
100 * Return the current tab's subwindow if it exists.
101 * @return The subwindow of the current tab or null if it doesn't exist.
102 */
103 WebView getCurrentSubWindow() {
104 Tab t = getTab(mCurrentTab);
105 if (t == null) {
106 return null;
107 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700108 return t.getSubWebView();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 }
110
111 /**
Michael Kolb1bf23132010-11-19 12:55:12 -0800112 * return the list of tabs
113 */
114 List<Tab> getTabs() {
115 return mTabs;
116 }
117
118 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700119 * Return the tab at the specified position.
120 * @return The Tab for the specified position or null if the tab does not
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 * exist.
122 */
Michael Kolbc831b632011-05-11 09:30:34 -0700123 Tab getTab(int position) {
124 if (position >= 0 && position < mTabs.size()) {
125 return mTabs.get(position);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800126 }
127 return null;
128 }
129
130 /**
131 * Return the current tab.
132 * @return The current tab.
133 */
134 Tab getCurrentTab() {
135 return getTab(mCurrentTab);
136 }
137
138 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700139 * Return the current tab position.
140 * @return The current tab position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800141 */
Michael Kolbc831b632011-05-11 09:30:34 -0700142 int getCurrentPosition() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800143 return mCurrentTab;
144 }
Michael Kolbfe251992010-07-08 15:41:55 -0700145
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700147 * Given a Tab, find it's position
The Android Open Source Project0c908882009-03-03 19:32:16 -0800148 * @param Tab to find
Michael Kolbc831b632011-05-11 09:30:34 -0700149 * @return position of Tab or -1 if not found
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 */
Michael Kolbc831b632011-05-11 09:30:34 -0700151 int getTabPosition(Tab tab) {
Patrick Scottae641ac2009-04-20 13:51:49 -0400152 if (tab == null) {
153 return -1;
154 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800155 return mTabs.indexOf(tab);
156 }
157
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 boolean canCreateNewTab() {
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100159 return mMaxTabs > mTabs.size();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160 }
161
The Android Open Source Project0c908882009-03-03 19:32:16 -0800162 /**
Elliott Slaughtere440a882010-08-20 13:54:45 -0700163 * Returns true if there are any incognito tabs open.
164 * @return True when any incognito tabs are open, false otherwise.
165 */
166 boolean hasAnyOpenIncognitoTabs() {
167 for (Tab tab : mTabs) {
Michael Kolbc831b632011-05-11 09:30:34 -0700168 if (tab.getWebView() != null
169 && tab.getWebView().isPrivateBrowsingEnabled()) {
Elliott Slaughtere440a882010-08-20 13:54:45 -0700170 return true;
171 }
172 }
173 return false;
174 }
175
Michael Kolb14612442011-06-24 13:06:29 -0700176 void addPreloadedTab(Tab tab) {
Mathew Inwoode09305e2011-09-02 12:03:26 +0100177 for (Tab current : mTabs) {
178 if (current != null && current.getId() == tab.getId()) {
179 throw new IllegalStateException("Tab with id " + tab.getId() + " already exists: "
180 + current.toString());
181 }
182 }
Michael Kolb14612442011-06-24 13:06:29 -0700183 mTabs.add(tab);
184 tab.setController(mController);
185 mController.onSetWebView(tab, tab.getWebView());
186 tab.putInBackground();
187 }
188
Elliott Slaughtere440a882010-08-20 13:54:45 -0700189 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700190 * Create a new tab.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800191 * @return The newly createTab or null if we have reached the maximum
192 * number of open tabs.
193 */
Michael Kolb7bcafde2011-05-09 13:55:59 -0700194 Tab createNewTab(boolean privateBrowsing) {
John Reck1cf4b792011-07-26 10:22:22 -0700195 return createNewTab(null, privateBrowsing);
196 }
197
198 Tab createNewTab(Bundle state, boolean privateBrowsing) {
199 int size = mTabs.size();
200 // Return false if we have maxed out on tabs
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100201 if (!canCreateNewTab()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800202 return null;
203 }
Narayan Kamathc3ad9ea2011-07-26 15:20:21 +0100204
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700205 final WebView w = createNewWebView(privateBrowsing);
Steve Block2bc69912009-07-30 14:45:13 +0100206
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207 // Create a new tab and add it to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700208 Tab t = new Tab(mController, w, state);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800209 mTabs.add(t);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700210 if (privateBrowsing) {
211 mNumIncognito += 1;
212 }
The Android Open Source Project4e5f5872009-03-09 11:52:14 -0700213 // Initially put the tab in the background.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700214 t.putInBackground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800215 return t;
216 }
217
218 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700219 * Create a new tab with default values for closeOnExit(false),
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700220 * appId(null), url(null), and privateBrowsing(false).
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700221 */
222 Tab createNewTab() {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700223 return createNewTab(false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700224 }
225
John Reckd8c74522011-06-14 08:45:00 -0700226 SnapshotTab createSnapshotTab(long snapshotId) {
John Reckd8c74522011-06-14 08:45:00 -0700227 SnapshotTab t = new SnapshotTab(mController, snapshotId);
John Reckd8c74522011-06-14 08:45:00 -0700228 mTabs.add(t);
229 return t;
230 }
231
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700232 /**
Leon Scrogginsfde97462010-01-11 13:06:21 -0500233 * Remove the parent child relationships from all tabs.
234 */
235 void removeParentChildRelationShips() {
236 for (Tab tab : mTabs) {
237 tab.removeFromTree();
238 }
239 }
240
241 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800242 * Remove the tab from the list. If the tab is the current tab shown, the
243 * last created tab will be shown.
244 * @param t The tab to be removed.
245 */
246 boolean removeTab(Tab t) {
247 if (t == null) {
248 return false;
249 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250
Patrick Scottd944d4d2010-01-27 16:39:11 -0500251 // Grab the current tab before modifying the list.
252 Tab current = getCurrentTab();
253
254 // Remove t from our list of tabs.
255 mTabs.remove(t);
256
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700257 //Clear incognito geolocation state if this is the last incognito tab.
258 if (t.isPrivateBrowsingEnabled()) {
259 mNumIncognito -= 1;
260 if (mNumIncognito == 0) {
261 GeolocationPermissions.onIncognitoTabsRemoved();
262 }
263 }
264
Patrick Scottd944d4d2010-01-27 16:39:11 -0500265 // Put the tab in the background only if it is the current one.
266 if (current == t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267 t.putInBackground();
268 mCurrentTab = -1;
Patrick Scottd944d4d2010-01-27 16:39:11 -0500269 } else {
270 // If a tab that is earlier in the list gets removed, the current
271 // index no longer points to the correct tab.
Michael Kolbc831b632011-05-11 09:30:34 -0700272 mCurrentTab = getTabPosition(current);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800273 }
274
Grace Kloba22ac16e2009-10-07 18:00:23 -0700275 // destroy the tab
276 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800277 // clear it's references to parent and children
278 t.removeFromTree();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800279
The Android Open Source Project0c908882009-03-03 19:32:16 -0800280 // Remove it from the queue of viewed tabs.
281 mTabQueue.remove(t);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800282 return true;
283 }
284
285 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800286 * Destroy all the tabs and subwindows
287 */
288 void destroy() {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800289 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700290 t.destroy();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800291 }
292 mTabs.clear();
293 mTabQueue.clear();
294 }
295
296 /**
297 * Returns the number of tabs created.
298 * @return The number of tabs created.
299 */
300 int getTabCount() {
301 return mTabs.size();
302 }
303
The Android Open Source Project0c908882009-03-03 19:32:16 -0800304 /**
Michael Kolbc831b632011-05-11 09:30:34 -0700305 * save the tab state:
306 * current position
307 * position sorted array of tab ids
308 * for each tab id, save the tab state
309 * @param outState
John Reckaed9c542011-05-27 16:08:53 -0700310 * @param saveImages
The Android Open Source Project0c908882009-03-03 19:32:16 -0800311 */
John Reck1cf4b792011-07-26 10:22:22 -0700312 void saveState(Bundle outState) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800313 final int numTabs = getTabCount();
John Reck24f18262011-06-17 14:47:20 -0700314 if (numTabs == 0) {
315 return;
316 }
Michael Kolbc831b632011-05-11 09:30:34 -0700317 long[] ids = new long[numTabs];
318 int i = 0;
319 for (Tab tab : mTabs) {
John Reck1cf4b792011-07-26 10:22:22 -0700320 Bundle tabState = tab.saveState();
Tarun Nainani8eb00912014-07-17 12:28:32 -0700321 if (tabState != null && tab.isPrivateBrowsingEnabled() == false) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700322 ids[i++] = tab.getId();
John Reck52be4782011-08-26 15:37:29 -0700323 String key = Long.toString(tab.getId());
324 if (outState.containsKey(key)) {
325 // Dump the tab state for debugging purposes
326 for (Tab dt : mTabs) {
327 Log.e(LOGTAG, dt.toString());
328 }
329 throw new IllegalStateException(
330 "Error saving state, duplicate tab ids!");
331 }
332 outState.putBundle(key, tabState);
Michael Kolb3ae7f742011-07-13 15:18:25 -0700333 } else {
334 ids[i++] = -1;
John Reck52be4782011-08-26 15:37:29 -0700335 // Since we won't be restoring the thumbnail, delete it
336 tab.deleteThumbnail();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800337 }
338 }
John Reck24f18262011-06-17 14:47:20 -0700339 if (!outState.isEmpty()) {
340 outState.putLongArray(POSITIONS, ids);
341 Tab current = getCurrentTab();
342 long cid = -1;
343 if (current != null) {
344 cid = current.getId();
345 }
346 outState.putLong(CURRENT, cid);
Michael Kolbdbf39812011-06-10 14:06:40 -0700347 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800348 }
349
350 /**
Patrick Scott7d50a932011-02-04 09:27:26 -0500351 * Check if the state can be restored. If the state can be restored, the
Michael Kolbc831b632011-05-11 09:30:34 -0700352 * current tab id is returned. This can be passed to restoreState below
Patrick Scott7d50a932011-02-04 09:27:26 -0500353 * in order to restore the correct tab. Otherwise, -1 is returned and the
354 * state cannot be restored.
355 */
Michael Kolbc831b632011-05-11 09:30:34 -0700356 long canRestoreState(Bundle inState, boolean restoreIncognitoTabs) {
357 final long[] ids = (inState == null) ? null : inState.getLongArray(POSITIONS);
358 if (ids == null) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500359 return -1;
360 }
Michael Kolbc831b632011-05-11 09:30:34 -0700361 final long oldcurrent = inState.getLong(CURRENT);
362 long current = -1;
Michael Kolb3ae7f742011-07-13 15:18:25 -0700363 if (restoreIncognitoTabs || (hasState(oldcurrent, inState) && !isIncognito(oldcurrent, inState))) {
John Reck1cf4b792011-07-26 10:22:22 -0700364 current = oldcurrent;
Patrick Scott7d50a932011-02-04 09:27:26 -0500365 } else {
Michael Kolbc831b632011-05-11 09:30:34 -0700366 // pick first non incognito tab
367 for (long id : ids) {
Michael Kolb3ae7f742011-07-13 15:18:25 -0700368 if (hasState(id, inState) && !isIncognito(id, inState)) {
Michael Kolbc831b632011-05-11 09:30:34 -0700369 current = id;
Patrick Scott7d50a932011-02-04 09:27:26 -0500370 break;
371 }
372 }
373 }
Michael Kolbc831b632011-05-11 09:30:34 -0700374 return current;
Patrick Scott7d50a932011-02-04 09:27:26 -0500375 }
376
Michael Kolb3ae7f742011-07-13 15:18:25 -0700377 private boolean hasState(long id, Bundle state) {
378 if (id == -1) return false;
379 Bundle tab = state.getBundle(Long.toString(id));
380 return ((tab != null) && !tab.isEmpty());
381 }
382
383 private boolean isIncognito(long id, Bundle state) {
384 Bundle tabstate = state.getBundle(Long.toString(id));
385 if ((tabstate != null) && !tabstate.isEmpty()) {
386 return tabstate.getBoolean(Tab.INCOGNITO);
387 }
388 return false;
389 }
390
Patrick Scott7d50a932011-02-04 09:27:26 -0500391 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800392 * Restore the state of all the tabs.
Michael Kolbc831b632011-05-11 09:30:34 -0700393 * @param currentId The tab id to restore.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800394 * @param inState The saved state of all the tabs.
Michael Kolb1bf23132010-11-19 12:55:12 -0800395 * @param restoreIncognitoTabs Restoring private browsing tabs
396 * @param restoreAll All webviews get restored, not just the current tab
397 * (this does not override handling of incognito tabs)
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398 */
Michael Kolbc831b632011-05-11 09:30:34 -0700399 void restoreState(Bundle inState, long currentId,
Patrick Scott7d50a932011-02-04 09:27:26 -0500400 boolean restoreIncognitoTabs, boolean restoreAll) {
Michael Kolbc831b632011-05-11 09:30:34 -0700401 if (currentId == -1) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500402 return;
403 }
Michael Kolbc831b632011-05-11 09:30:34 -0700404 long[] ids = inState.getLongArray(POSITIONS);
405 long maxId = -Long.MAX_VALUE;
406 HashMap<Long, Tab> tabMap = new HashMap<Long, Tab>();
407 for (long id : ids) {
408 if (id > maxId) {
409 maxId = id;
410 }
411 final String idkey = Long.toString(id);
412 Bundle state = inState.getBundle(idkey);
John Reckd8c74522011-06-14 08:45:00 -0700413 if (state == null || state.isEmpty()) {
414 // Skip tab
415 continue;
416 } else if (!restoreIncognitoTabs
Michael Kolbc831b632011-05-11 09:30:34 -0700417 && state.getBoolean(Tab.INCOGNITO)) {
418 // ignore tab
419 } else if (id == currentId || restoreAll) {
John Reck1cf4b792011-07-26 10:22:22 -0700420 Tab t = createNewTab(state, false);
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100421 if (t == null) {
422 // We could "break" at this point, but we want
423 // sNextId to be set correctly.
424 continue;
425 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700426 //handle restored pages that may require a JS interface
427 t.handleJsInterface(t.getWebView(), t.getUrl());
428
Michael Kolbc831b632011-05-11 09:30:34 -0700429 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500430 // Me must set the current tab before restoring the state
431 // so that all the client classes are set.
Michael Kolbc831b632011-05-11 09:30:34 -0700432 if (id == currentId) {
Patrick Scott7d50a932011-02-04 09:27:26 -0500433 setCurrentTab(t);
434 }
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700435 } else {
Patrick Scott7d50a932011-02-04 09:27:26 -0500436 // Create a new tab and don't restore the state yet, add it
437 // to the tab list
John Reck1cf4b792011-07-26 10:22:22 -0700438 Tab t = new Tab(mController, state);
Michael Kolbc831b632011-05-11 09:30:34 -0700439 tabMap.put(id, t);
Patrick Scott7d50a932011-02-04 09:27:26 -0500440 mTabs.add(t);
441 // added the tab to the front as they are not current
442 mTabQueue.add(0, t);
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700443 }
Michael Kolbc831b632011-05-11 09:30:34 -0700444 }
Narayan Kamath79e5d8d2011-07-20 14:12:38 +0100445
446 // make sure that there is no id overlap between the restored
447 // and new tabs
448 sNextId = maxId + 1;
449
John Reckd8c74522011-06-14 08:45:00 -0700450 if (mCurrentTab == -1) {
451 if (getTabCount() > 0) {
452 setCurrentTab(getTab(0));
John Reckd8c74522011-06-14 08:45:00 -0700453 }
454 }
Michael Kolbc831b632011-05-11 09:30:34 -0700455 // restore parent/child relationships
456 for (long id : ids) {
457 final Tab tab = tabMap.get(id);
458 final Bundle b = inState.getBundle(Long.toString(id));
459 if ((b != null) && (tab != null)) {
460 final long parentId = b.getLong(Tab.PARENTTAB, -1);
461 if (parentId != -1) {
462 final Tab parent = tabMap.get(parentId);
Patrick Scott7d50a932011-02-04 09:27:26 -0500463 if (parent != null) {
Michael Kolbc831b632011-05-11 09:30:34 -0700464 parent.addChildTab(tab);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800465 }
466 }
467 }
468 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800469 }
470
471 /**
Grace Klobaf56f68d2010-04-11 22:53:42 -0700472 * Free the memory in this order, 1) free the background tabs; 2) free the
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 * WebView cache;
474 */
475 void freeMemory() {
Grace Kloba92c18a52009-07-31 23:48:32 -0700476 if (getTabCount() == 0) return;
477
Grace Klobaf56f68d2010-04-11 22:53:42 -0700478 // free the least frequently used background tabs
479 Vector<Tab> tabs = getHalfLeastUsedTabs(getCurrentTab());
480 if (tabs.size() > 0) {
481 Log.w(LOGTAG, "Free " + tabs.size() + " tabs in the browser");
482 for (Tab t : tabs) {
483 // store the WebView's state.
484 t.saveState();
485 // destroy the tab
486 t.destroy();
487 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800488 return;
489 }
490
Derek Sollenberger4433d032009-06-10 15:37:21 -0400491 // free the WebView's unused memory (this includes the cache)
492 Log.w(LOGTAG, "Free WebView's unused memory and cache");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800493 WebView view = getCurrentWebView();
494 if (view != null) {
Derek Sollenberger4433d032009-06-10 15:37:21 -0400495 view.freeMemory();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800496 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800497 }
498
Grace Klobaf56f68d2010-04-11 22:53:42 -0700499 private Vector<Tab> getHalfLeastUsedTabs(Tab current) {
500 Vector<Tab> tabsToGo = new Vector<Tab>();
501
Patrick Scott2a67de42009-08-31 09:48:37 -0400502 // Don't do anything if we only have 1 tab or if the current tab is
503 // null.
504 if (getTabCount() == 1 || current == null) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700505 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800506 }
507
Grace Klobaf56f68d2010-04-11 22:53:42 -0700508 if (mTabQueue.size() == 0) {
509 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511
Grace Klobaf56f68d2010-04-11 22:53:42 -0700512 // Rip through the queue starting at the beginning and tear down half of
513 // available tabs which are not the current tab or the parent of the
514 // current tab.
515 int openTabCount = 0;
516 for (Tab t : mTabQueue) {
517 if (t != null && t.getWebView() != null) {
518 openTabCount++;
Michael Kolbc831b632011-05-11 09:30:34 -0700519 if (t != current && t != current.getParent()) {
Grace Klobaf56f68d2010-04-11 22:53:42 -0700520 tabsToGo.add(t);
521 }
522 }
523 }
524
525 openTabCount /= 2;
526 if (tabsToGo.size() > openTabCount) {
527 tabsToGo.setSize(openTabCount);
528 }
529
530 return tabsToGo;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800531 }
532
Michael Kolbff6a7482011-07-26 16:37:15 -0700533 Tab getLeastUsedTab(Tab current) {
534 if (getTabCount() == 1 || current == null) {
535 return null;
536 }
537 if (mTabQueue.size() == 0) {
538 return null;
539 }
540 // find a tab which is not the current tab or the parent of the
541 // current tab
542 for (Tab t : mTabQueue) {
543 if (t != null && t.getWebView() != null) {
544 if (t != current && t != current.getParent()) {
545 return t;
546 }
547 }
548 }
549 return null;
550 }
551
The Android Open Source Project0c908882009-03-03 19:32:16 -0800552 /**
553 * Show the tab that contains the given WebView.
554 * @param view The WebView used to find the tab.
555 */
556 Tab getTabFromView(WebView view) {
Michael Kolbc831b632011-05-11 09:30:34 -0700557 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 if (t.getSubWebView() == view || t.getWebView() == view) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800559 return t;
560 }
561 }
562 return null;
563 }
564
565 /**
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700566 * Return the tab with the matching application id.
567 * @param id The application identifier.
568 */
Michael Kolbc831b632011-05-11 09:30:34 -0700569 Tab getTabFromAppId(String id) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700570 if (id == null) {
571 return null;
572 }
Michael Kolbc831b632011-05-11 09:30:34 -0700573 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 if (id.equals(t.getAppId())) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700575 return t;
576 }
577 }
578 return null;
579 }
580
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 /**
582 * Stop loading in all opened WebView including subWindows.
583 */
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700584 void stopAllLoading() {
Michael Kolbc831b632011-05-11 09:30:34 -0700585 for (Tab t : mTabs) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 final WebView webview = t.getWebView();
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700587 if (webview != null) {
588 webview.stopLoading();
589 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 final WebView subview = t.getSubWebView();
591 if (subview != null) {
Mattias Nilsson9727af82012-06-14 17:07:56 +0200592 subview.stopLoading();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
Grace Kloba5d0e02e2009-10-05 15:15:36 -0700594 }
595 }
596
John Reckdb22ec42011-06-29 11:31:24 -0700597 // This method checks if a tab matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400598 private boolean tabMatchesUrl(Tab t, String url) {
John Reckdb22ec42011-06-29 11:31:24 -0700599 return url.equals(t.getUrl()) || url.equals(t.getOriginalUrl());
Patrick Scottcd115892009-07-16 09:42:58 -0400600 }
601
602 /**
John Reckdb22ec42011-06-29 11:31:24 -0700603 * Return the tab that matches the given url.
Patrick Scottcd115892009-07-16 09:42:58 -0400604 * @param url The url to search for.
605 */
John Reckdb22ec42011-06-29 11:31:24 -0700606 Tab findTabWithUrl(String url) {
Patrick Scottcd115892009-07-16 09:42:58 -0400607 if (url == null) {
608 return null;
609 }
610 // Check the current tab first.
John Reckdb22ec42011-06-29 11:31:24 -0700611 Tab currentTab = getCurrentTab();
612 if (currentTab != null && tabMatchesUrl(currentTab, url)) {
613 return currentTab;
Patrick Scottcd115892009-07-16 09:42:58 -0400614 }
615 // Now check all the rest.
Michael Kolbc831b632011-05-11 09:30:34 -0700616 for (Tab tab : mTabs) {
617 if (tabMatchesUrl(tab, url)) {
John Reckdb22ec42011-06-29 11:31:24 -0700618 return tab;
Patrick Scottcd115892009-07-16 09:42:58 -0400619 }
620 }
621 return null;
622 }
623
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700624 /**
John Reck30c714c2010-12-16 17:30:34 -0800625 * Recreate the main WebView of the given tab.
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700626 */
John Reck30c714c2010-12-16 17:30:34 -0800627 void recreateWebView(Tab t) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700628 final WebView w = t.getWebView();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700629 if (w != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 t.destroy();
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700631 }
632 // Create a new WebView. If this tab is the current tab, we need to put
633 // back all the clients so force it to be the current tab.
Panos Thomasa9a5a582014-03-18 19:20:08 -0700634 t.setWebView(createNewWebView(t.isPrivateBrowsingEnabled()), false);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700635 if (getCurrentTab() == t) {
636 setCurrentTab(t, true);
637 }
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700638 }
639
640 /**
641 * Creates a new WebView and registers it with the global settings.
642 */
643 private WebView createNewWebView() {
Elliott Slaughterf0f03952010-07-14 18:10:36 -0700644 return createNewWebView(false);
645 }
646
647 /**
648 * Creates a new WebView and registers it with the global settings.
649 * @param privateBrowsing When true, enables private browsing in the new
650 * WebView.
651 */
652 private WebView createNewWebView(boolean privateBrowsing) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 return mController.getWebViewFactory().createWebView(privateBrowsing);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700654 }
655
656 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -0800657 * Put the current tab in the background and set newTab as the current tab.
658 * @param newTab The new tab. If newTab is null, the current tab is not
659 * set.
660 */
661 boolean setCurrentTab(Tab newTab) {
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700662 return setCurrentTab(newTab, false);
663 }
664
665 /**
666 * If force is true, this method skips the check for newTab == current.
667 */
668 private boolean setCurrentTab(Tab newTab, boolean force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669 Tab current = getTab(mCurrentTab);
The Android Open Source Projectf59ec872009-03-13 13:04:24 -0700670 if (current == newTab && !force) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800671 return true;
672 }
673 if (current != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 current.putInBackground();
675 mCurrentTab = -1;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 if (newTab == null) {
678 return false;
679 }
680
681 // Move the newTab to the end of the queue
682 int index = mTabQueue.indexOf(newTab);
683 if (index != -1) {
684 mTabQueue.remove(index);
685 }
686 mTabQueue.add(newTab);
687
The Android Open Source Project0c908882009-03-03 19:32:16 -0800688 // Display the new current tab
689 mCurrentTab = mTabs.indexOf(newTab);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 WebView mainView = newTab.getWebView();
John Reckd8c74522011-06-14 08:45:00 -0700691 boolean needRestore = !newTab.isSnapshot() && (mainView == null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800692 if (needRestore) {
693 // Same work as in createNewTab() except don't do new Tab()
Panos Thomasa9a5a582014-03-18 19:20:08 -0700694 mainView = createNewWebView(newTab.isPrivateBrowsingEnabled());
Steve Block2bc69912009-07-30 14:45:13 +0100695 newTab.setWebView(mainView);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800696 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 newTab.putInForeground();
The Android Open Source Project0c908882009-03-03 19:32:16 -0800698 return true;
699 }
Michael Kolbfe251992010-07-08 15:41:55 -0700700
John Reck8ee633f2011-08-09 16:00:35 -0700701 public void setOnThumbnailUpdatedListener(OnThumbnailUpdatedListener listener) {
702 mOnThumbnailUpdatedListener = listener;
703 for (Tab t : mTabs) {
704 WebView web = t.getWebView();
705 if (web != null) {
706 web.setPictureListener(listener != null ? t : null);
707 }
708 }
709 }
710
711 public OnThumbnailUpdatedListener getOnThumbnailUpdatedListener() {
712 return mOnThumbnailUpdatedListener;
713 }
714
The Android Open Source Project0c908882009-03-03 19:32:16 -0800715}