michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.util.HashMap; michael@0: import java.util.Iterator; michael@0: import java.util.List; michael@0: import java.util.concurrent.CopyOnWriteArrayList; michael@0: import java.util.concurrent.atomic.AtomicInteger; michael@0: michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.db.BrowserDB; michael@0: import org.mozilla.gecko.favicons.Favicons; michael@0: import org.mozilla.gecko.fxa.FirefoxAccounts; michael@0: import org.mozilla.gecko.mozglue.JNITarget; michael@0: import org.mozilla.gecko.mozglue.RobocopTarget; michael@0: import org.mozilla.gecko.sync.setup.SyncAccounts; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: import org.mozilla.gecko.util.ThreadUtils; michael@0: michael@0: import android.accounts.Account; michael@0: import android.accounts.AccountManager; michael@0: import android.accounts.OnAccountsUpdateListener; michael@0: import android.content.ContentResolver; michael@0: import android.content.Context; michael@0: import android.database.ContentObserver; michael@0: import android.graphics.Bitmap; michael@0: import android.graphics.Color; michael@0: import android.net.Uri; michael@0: import android.os.Handler; michael@0: import android.util.Log; michael@0: michael@0: public class Tabs implements GeckoEventListener { michael@0: private static final String LOGTAG = "GeckoTabs"; michael@0: michael@0: // mOrder and mTabs are always of the same cardinality, and contain the same values. michael@0: private final CopyOnWriteArrayList mOrder = new CopyOnWriteArrayList(); michael@0: michael@0: // All writes to mSelectedTab must be synchronized on the Tabs instance. michael@0: // In general, it's preferred to always use selectTab()). michael@0: private volatile Tab mSelectedTab; michael@0: michael@0: // All accesses to mTabs must be synchronized on the Tabs instance. michael@0: private final HashMap mTabs = new HashMap(); michael@0: michael@0: private AccountManager mAccountManager; michael@0: private OnAccountsUpdateListener mAccountListener = null; michael@0: michael@0: public static final int LOADURL_NONE = 0; michael@0: public static final int LOADURL_NEW_TAB = 1 << 0; michael@0: public static final int LOADURL_USER_ENTERED = 1 << 1; michael@0: public static final int LOADURL_PRIVATE = 1 << 2; michael@0: public static final int LOADURL_PINNED = 1 << 3; michael@0: public static final int LOADURL_DELAY_LOAD = 1 << 4; michael@0: public static final int LOADURL_DESKTOP = 1 << 5; michael@0: public static final int LOADURL_BACKGROUND = 1 << 6; michael@0: public static final int LOADURL_EXTERNAL = 1 << 7; michael@0: michael@0: private static final long PERSIST_TABS_AFTER_MILLISECONDS = 1000 * 5; michael@0: michael@0: private static AtomicInteger sTabId = new AtomicInteger(0); michael@0: private volatile boolean mInitialTabsAdded; michael@0: michael@0: private Context mAppContext; michael@0: private ContentObserver mContentObserver; michael@0: michael@0: private final Runnable mPersistTabsRunnable = new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: try { michael@0: final Context context = getAppContext(); michael@0: boolean syncIsSetup = SyncAccounts.syncAccountsExist(context) || michael@0: FirefoxAccounts.firefoxAccountsExist(context); michael@0: if (syncIsSetup) { michael@0: TabsAccessor.persistLocalTabs(getContentResolver(), getTabsInOrder()); michael@0: } michael@0: } catch (SecurityException se) {} // will fail without android.permission.GET_ACCOUNTS michael@0: } michael@0: }; michael@0: michael@0: private Tabs() { michael@0: registerEventListener("Session:RestoreEnd"); michael@0: registerEventListener("SessionHistory:New"); michael@0: registerEventListener("SessionHistory:Back"); michael@0: registerEventListener("SessionHistory:Forward"); michael@0: registerEventListener("SessionHistory:Goto"); michael@0: registerEventListener("SessionHistory:Purge"); michael@0: registerEventListener("Tab:Added"); michael@0: registerEventListener("Tab:Close"); michael@0: registerEventListener("Tab:Select"); michael@0: registerEventListener("Content:LocationChange"); michael@0: registerEventListener("Content:SecurityChange"); michael@0: registerEventListener("Content:ReaderEnabled"); michael@0: registerEventListener("Content:StateChange"); michael@0: registerEventListener("Content:LoadError"); michael@0: registerEventListener("Content:PageShow"); michael@0: registerEventListener("DOMContentLoaded"); michael@0: registerEventListener("DOMTitleChanged"); michael@0: registerEventListener("Link:Favicon"); michael@0: registerEventListener("Link:Feed"); michael@0: registerEventListener("Link:OpenSearch"); michael@0: registerEventListener("DesktopMode:Changed"); michael@0: registerEventListener("Tab:ViewportMetadata"); michael@0: registerEventListener("Tab:StreamStart"); michael@0: registerEventListener("Tab:StreamStop"); michael@0: michael@0: } michael@0: michael@0: public synchronized void attachToContext(Context context) { michael@0: final Context appContext = context.getApplicationContext(); michael@0: if (mAppContext == appContext) { michael@0: return; michael@0: } michael@0: michael@0: if (mAppContext != null) { michael@0: // This should never happen. michael@0: Log.w(LOGTAG, "The application context has changed!"); michael@0: } michael@0: michael@0: mAppContext = appContext; michael@0: mAccountManager = AccountManager.get(appContext); michael@0: michael@0: mAccountListener = new OnAccountsUpdateListener() { michael@0: @Override michael@0: public void onAccountsUpdated(Account[] accounts) { michael@0: persistAllTabs(); michael@0: } michael@0: }; michael@0: michael@0: // The listener will run on the background thread (see 2nd argument). michael@0: mAccountManager.addOnAccountsUpdatedListener(mAccountListener, ThreadUtils.getBackgroundHandler(), false); michael@0: michael@0: if (mContentObserver != null) { michael@0: BrowserDB.registerBookmarkObserver(getContentResolver(), mContentObserver); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Gets the tab count corresponding to the private state of the selected michael@0: * tab. michael@0: * michael@0: * If the selected tab is a non-private tab, this will return the number of michael@0: * non-private tabs; likewise, if this is a private tab, this will return michael@0: * the number of private tabs. michael@0: * michael@0: * @return the number of tabs in the current private state michael@0: */ michael@0: public synchronized int getDisplayCount() { michael@0: // Once mSelectedTab is non-null, it cannot be null for the remainder michael@0: // of the object's lifetime. michael@0: boolean getPrivate = mSelectedTab != null && mSelectedTab.isPrivate(); michael@0: int count = 0; michael@0: for (Tab tab : mOrder) { michael@0: if (tab.isPrivate() == getPrivate) { michael@0: count++; michael@0: } michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: public int isOpen(String url) { michael@0: for (Tab tab : mOrder) { michael@0: if (tab.getURL().equals(url)) { michael@0: return tab.getId(); michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: michael@0: // Must be synchronized to avoid racing on mContentObserver. michael@0: private void lazyRegisterBookmarkObserver() { michael@0: if (mContentObserver == null) { michael@0: mContentObserver = new ContentObserver(null) { michael@0: @Override michael@0: public void onChange(boolean selfChange) { michael@0: for (Tab tab : mOrder) { michael@0: tab.updateBookmark(); michael@0: } michael@0: } michael@0: }; michael@0: BrowserDB.registerBookmarkObserver(getContentResolver(), mContentObserver); michael@0: } michael@0: } michael@0: michael@0: private Tab addTab(int id, String url, boolean external, int parentId, String title, boolean isPrivate) { michael@0: final Tab tab = isPrivate ? new PrivateTab(mAppContext, id, url, external, parentId, title) : michael@0: new Tab(mAppContext, id, url, external, parentId, title); michael@0: synchronized (this) { michael@0: lazyRegisterBookmarkObserver(); michael@0: mTabs.put(id, tab); michael@0: mOrder.add(tab); michael@0: } michael@0: michael@0: // Suppress the ADDED event to prevent animation of tabs created via session restore. michael@0: if (mInitialTabsAdded) { michael@0: notifyListeners(tab, TabEvents.ADDED); michael@0: } michael@0: michael@0: return tab; michael@0: } michael@0: michael@0: public synchronized void removeTab(int id) { michael@0: if (mTabs.containsKey(id)) { michael@0: Tab tab = getTab(id); michael@0: mOrder.remove(tab); michael@0: mTabs.remove(id); michael@0: } michael@0: } michael@0: michael@0: public synchronized Tab selectTab(int id) { michael@0: if (!mTabs.containsKey(id)) michael@0: return null; michael@0: michael@0: final Tab oldTab = getSelectedTab(); michael@0: final Tab tab = mTabs.get(id); michael@0: michael@0: // This avoids a NPE below, but callers need to be careful to michael@0: // handle this case. michael@0: if (tab == null || oldTab == tab) { michael@0: return null; michael@0: } michael@0: michael@0: mSelectedTab = tab; michael@0: notifyListeners(tab, TabEvents.SELECTED); michael@0: michael@0: if (oldTab != null) { michael@0: notifyListeners(oldTab, TabEvents.UNSELECTED); michael@0: } michael@0: michael@0: // Pass a message to Gecko to update tab state in BrowserApp. michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Selected", String.valueOf(tab.getId()))); michael@0: return tab; michael@0: } michael@0: michael@0: private int getIndexOf(Tab tab) { michael@0: return mOrder.lastIndexOf(tab); michael@0: } michael@0: michael@0: private Tab getNextTabFrom(Tab tab, boolean getPrivate) { michael@0: int numTabs = mOrder.size(); michael@0: int index = getIndexOf(tab); michael@0: for (int i = index + 1; i < numTabs; i++) { michael@0: Tab next = mOrder.get(i); michael@0: if (next.isPrivate() == getPrivate) { michael@0: return next; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: private Tab getPreviousTabFrom(Tab tab, boolean getPrivate) { michael@0: int index = getIndexOf(tab); michael@0: for (int i = index - 1; i >= 0; i--) { michael@0: Tab prev = mOrder.get(i); michael@0: if (prev.isPrivate() == getPrivate) { michael@0: return prev; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * Gets the selected tab. michael@0: * michael@0: * The selected tab can be null if we're doing a session restore after a michael@0: * crash and Gecko isn't ready yet. michael@0: * michael@0: * @return the selected tab, or null if no tabs exist michael@0: */ michael@0: public Tab getSelectedTab() { michael@0: return mSelectedTab; michael@0: } michael@0: michael@0: public boolean isSelectedTab(Tab tab) { michael@0: return tab != null && tab == mSelectedTab; michael@0: } michael@0: michael@0: public boolean isSelectedTabId(int tabId) { michael@0: final Tab selected = mSelectedTab; michael@0: return selected != null && selected.getId() == tabId; michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public synchronized Tab getTab(int id) { michael@0: if (id == -1) michael@0: return null; michael@0: michael@0: if (mTabs.size() == 0) michael@0: return null; michael@0: michael@0: if (!mTabs.containsKey(id)) michael@0: return null; michael@0: michael@0: return mTabs.get(id); michael@0: } michael@0: michael@0: /** Close tab and then select the default next tab */ michael@0: @RobocopTarget michael@0: public synchronized void closeTab(Tab tab) { michael@0: closeTab(tab, getNextTab(tab)); michael@0: } michael@0: michael@0: /** Close tab and then select nextTab */ michael@0: public synchronized void closeTab(final Tab tab, Tab nextTab) { michael@0: if (tab == null) michael@0: return; michael@0: michael@0: int tabId = tab.getId(); michael@0: removeTab(tabId); michael@0: michael@0: if (nextTab == null) { michael@0: nextTab = loadUrl(AboutPages.HOME, LOADURL_NEW_TAB); michael@0: } michael@0: michael@0: selectTab(nextTab.getId()); michael@0: michael@0: tab.onDestroy(); michael@0: michael@0: // Pass a message to Gecko to update tab state in BrowserApp michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Closed", String.valueOf(tabId))); michael@0: } michael@0: michael@0: /** Return the tab that will be selected by default after this one is closed */ michael@0: public Tab getNextTab(Tab tab) { michael@0: Tab selectedTab = getSelectedTab(); michael@0: if (selectedTab != tab) michael@0: return selectedTab; michael@0: michael@0: boolean getPrivate = tab.isPrivate(); michael@0: Tab nextTab = getNextTabFrom(tab, getPrivate); michael@0: if (nextTab == null) michael@0: nextTab = getPreviousTabFrom(tab, getPrivate); michael@0: if (nextTab == null && getPrivate) { michael@0: // If there are no private tabs remaining, get the last normal tab michael@0: Tab lastTab = mOrder.get(mOrder.size() - 1); michael@0: if (!lastTab.isPrivate()) { michael@0: nextTab = lastTab; michael@0: } else { michael@0: nextTab = getPreviousTabFrom(lastTab, false); michael@0: } michael@0: } michael@0: michael@0: Tab parent = getTab(tab.getParentId()); michael@0: if (parent != null) { michael@0: // If the next tab is a sibling, switch to it. Otherwise go back to the parent. michael@0: if (nextTab != null && nextTab.getParentId() == tab.getParentId()) michael@0: return nextTab; michael@0: else michael@0: return parent; michael@0: } michael@0: return nextTab; michael@0: } michael@0: michael@0: public Iterable getTabsInOrder() { michael@0: return mOrder; michael@0: } michael@0: michael@0: /** michael@0: * @return the current GeckoApp instance, or throws if michael@0: * we aren't correctly initialized. michael@0: */ michael@0: private synchronized Context getAppContext() { michael@0: if (mAppContext == null) { michael@0: throw new IllegalStateException("Tabs not initialized with a GeckoApp instance."); michael@0: } michael@0: return mAppContext; michael@0: } michael@0: michael@0: public ContentResolver getContentResolver() { michael@0: return getAppContext().getContentResolver(); michael@0: } michael@0: michael@0: // Make Tabs a singleton class. michael@0: private static class TabsInstanceHolder { michael@0: private static final Tabs INSTANCE = new Tabs(); michael@0: } michael@0: michael@0: @RobocopTarget michael@0: public static Tabs getInstance() { michael@0: return Tabs.TabsInstanceHolder.INSTANCE; michael@0: } michael@0: michael@0: // GeckoEventListener implementation michael@0: @Override michael@0: public void handleMessage(String event, JSONObject message) { michael@0: Log.d(LOGTAG, "handleMessage: " + event); michael@0: try { michael@0: if (event.equals("Session:RestoreEnd")) { michael@0: notifyListeners(null, TabEvents.RESTORED); michael@0: return; michael@0: } michael@0: michael@0: // All other events handled below should contain a tabID property michael@0: int id = message.getInt("tabID"); michael@0: Tab tab = getTab(id); michael@0: michael@0: // "Tab:Added" is a special case because tab will be null if the tab was just added michael@0: if (event.equals("Tab:Added")) { michael@0: String url = message.isNull("uri") ? null : message.getString("uri"); michael@0: michael@0: if (message.getBoolean("stub")) { michael@0: if (tab == null) { michael@0: // Tab was already closed; abort michael@0: return; michael@0: } michael@0: } else { michael@0: tab = addTab(id, url, message.getBoolean("external"), michael@0: message.getInt("parentId"), michael@0: message.getString("title"), michael@0: message.getBoolean("isPrivate")); michael@0: michael@0: // If we added the tab as a stub, we should have already michael@0: // selected it, so ignore this flag for stubbed tabs. michael@0: if (message.getBoolean("selected")) michael@0: selectTab(id); michael@0: } michael@0: michael@0: if (message.getBoolean("delayLoad")) michael@0: tab.setState(Tab.STATE_DELAYED); michael@0: if (message.getBoolean("desktopMode")) michael@0: tab.setDesktopMode(true); michael@0: return; michael@0: } michael@0: michael@0: // Tab was already closed; abort michael@0: if (tab == null) michael@0: return; michael@0: michael@0: if (event.startsWith("SessionHistory:")) { michael@0: event = event.substring("SessionHistory:".length()); michael@0: tab.handleSessionHistoryMessage(event, message); michael@0: } else if (event.equals("Tab:Close")) { michael@0: closeTab(tab); michael@0: } else if (event.equals("Tab:Select")) { michael@0: selectTab(tab.getId()); michael@0: } else if (event.equals("Content:LocationChange")) { michael@0: tab.handleLocationChange(message); michael@0: } else if (event.equals("Content:SecurityChange")) { michael@0: tab.updateIdentityData(message.getJSONObject("identity")); michael@0: notifyListeners(tab, TabEvents.SECURITY_CHANGE); michael@0: } else if (event.equals("Content:ReaderEnabled")) { michael@0: tab.setReaderEnabled(true); michael@0: notifyListeners(tab, TabEvents.READER_ENABLED); michael@0: } else if (event.equals("Content:StateChange")) { michael@0: int state = message.getInt("state"); michael@0: if ((state & GeckoAppShell.WPL_STATE_IS_NETWORK) != 0) { michael@0: if ((state & GeckoAppShell.WPL_STATE_START) != 0) { michael@0: boolean restoring = message.getBoolean("restoring"); michael@0: tab.handleDocumentStart(restoring, message.getString("uri")); michael@0: notifyListeners(tab, Tabs.TabEvents.START); michael@0: } else if ((state & GeckoAppShell.WPL_STATE_STOP) != 0) { michael@0: tab.handleDocumentStop(message.getBoolean("success")); michael@0: notifyListeners(tab, Tabs.TabEvents.STOP); michael@0: } michael@0: } michael@0: } else if (event.equals("Content:LoadError")) { michael@0: tab.handleContentLoaded(); michael@0: notifyListeners(tab, Tabs.TabEvents.LOAD_ERROR); michael@0: } else if (event.equals("Content:PageShow")) { michael@0: notifyListeners(tab, TabEvents.PAGE_SHOW); michael@0: } else if (event.equals("DOMContentLoaded")) { michael@0: tab.handleContentLoaded(); michael@0: String backgroundColor = message.getString("bgColor"); michael@0: if (backgroundColor != null) { michael@0: tab.setBackgroundColor(backgroundColor); michael@0: } else { michael@0: // Default to white if no color is given michael@0: tab.setBackgroundColor(Color.WHITE); michael@0: } michael@0: tab.setErrorType(message.optString("errorType")); michael@0: notifyListeners(tab, Tabs.TabEvents.LOADED); michael@0: } else if (event.equals("DOMTitleChanged")) { michael@0: tab.updateTitle(message.getString("title")); michael@0: } else if (event.equals("Link:Favicon")) { michael@0: tab.updateFaviconURL(message.getString("href"), message.getInt("size")); michael@0: notifyListeners(tab, TabEvents.LINK_FAVICON); michael@0: } else if (event.equals("Link:Feed")) { michael@0: tab.setHasFeeds(true); michael@0: notifyListeners(tab, TabEvents.LINK_FEED); michael@0: } else if (event.equals("Link:OpenSearch")) { michael@0: boolean visible = message.getBoolean("visible"); michael@0: tab.setHasOpenSearch(visible); michael@0: } else if (event.equals("DesktopMode:Changed")) { michael@0: tab.setDesktopMode(message.getBoolean("desktopMode")); michael@0: notifyListeners(tab, TabEvents.DESKTOP_MODE_CHANGE); michael@0: } else if (event.equals("Tab:ViewportMetadata")) { michael@0: tab.setZoomConstraints(new ZoomConstraints(message)); michael@0: tab.setIsRTL(message.getBoolean("isRTL")); michael@0: notifyListeners(tab, TabEvents.VIEWPORT_CHANGE); michael@0: } else if (event.equals("Tab:StreamStart")) { michael@0: tab.setRecording(true); michael@0: notifyListeners(tab, TabEvents.RECORDING_CHANGE); michael@0: } else if (event.equals("Tab:StreamStop")) { michael@0: tab.setRecording(false); michael@0: notifyListeners(tab, TabEvents.RECORDING_CHANGE); michael@0: } michael@0: michael@0: } catch (Exception e) { michael@0: Log.w(LOGTAG, "handleMessage threw for " + event, e); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Set the favicon for any tabs loaded with this page URL. michael@0: */ michael@0: public void updateFaviconForURL(String pageURL, Bitmap favicon) { michael@0: // The tab might be pointing to another URL by the time the michael@0: // favicon is finally loaded, in which case we won't find the tab. michael@0: // See also: Bug 920331. michael@0: for (Tab tab : mOrder) { michael@0: String tabURL = tab.getURL(); michael@0: if (pageURL.equals(tabURL)) { michael@0: tab.setFaviconLoadId(Favicons.NOT_LOADING); michael@0: if (tab.updateFavicon(favicon)) { michael@0: notifyListeners(tab, TabEvents.FAVICON); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: public void refreshThumbnails() { michael@0: final ThumbnailHelper helper = ThumbnailHelper.getInstance(); michael@0: ThreadUtils.postToBackgroundThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: for (final Tab tab : mOrder) { michael@0: helper.getAndProcessThumbnailFor(tab); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: public interface OnTabsChangedListener { michael@0: public void onTabChanged(Tab tab, TabEvents msg, Object data); michael@0: } michael@0: michael@0: private static final List TABS_CHANGED_LISTENERS = new CopyOnWriteArrayList(); michael@0: michael@0: public static void registerOnTabsChangedListener(OnTabsChangedListener listener) { michael@0: TABS_CHANGED_LISTENERS.add(listener); michael@0: } michael@0: michael@0: public static void unregisterOnTabsChangedListener(OnTabsChangedListener listener) { michael@0: TABS_CHANGED_LISTENERS.remove(listener); michael@0: } michael@0: michael@0: public enum TabEvents { michael@0: CLOSED, michael@0: START, michael@0: LOADED, michael@0: LOAD_ERROR, michael@0: STOP, michael@0: FAVICON, michael@0: THUMBNAIL, michael@0: TITLE, michael@0: SELECTED, michael@0: UNSELECTED, michael@0: ADDED, michael@0: RESTORED, michael@0: LOCATION_CHANGE, michael@0: MENU_UPDATED, michael@0: PAGE_SHOW, michael@0: LINK_FAVICON, michael@0: LINK_FEED, michael@0: SECURITY_CHANGE, michael@0: READER_ENABLED, michael@0: DESKTOP_MODE_CHANGE, michael@0: VIEWPORT_CHANGE, michael@0: RECORDING_CHANGE michael@0: } michael@0: michael@0: public void notifyListeners(Tab tab, TabEvents msg) { michael@0: notifyListeners(tab, msg, ""); michael@0: } michael@0: michael@0: public void notifyListeners(final Tab tab, final TabEvents msg, final Object data) { michael@0: if (tab == null && michael@0: msg != TabEvents.RESTORED) { michael@0: throw new IllegalArgumentException("onTabChanged:" + msg + " must specify a tab."); michael@0: } michael@0: michael@0: ThreadUtils.postToUiThread(new Runnable() { michael@0: @Override michael@0: public void run() { michael@0: onTabChanged(tab, msg, data); michael@0: michael@0: if (TABS_CHANGED_LISTENERS.isEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: Iterator items = TABS_CHANGED_LISTENERS.iterator(); michael@0: while (items.hasNext()) { michael@0: items.next().onTabChanged(tab, msg, data); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: private void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) { michael@0: switch (msg) { michael@0: case LOCATION_CHANGE: michael@0: queuePersistAllTabs(); michael@0: break; michael@0: case RESTORED: michael@0: mInitialTabsAdded = true; michael@0: break; michael@0: michael@0: // When one tab is deselected, another one is always selected, so only michael@0: // queue a single persist operation. When tabs are added/closed, they michael@0: // are also selected/unselected, so it would be redundant to also listen michael@0: // for ADDED/CLOSED events. michael@0: case SELECTED: michael@0: queuePersistAllTabs(); michael@0: case UNSELECTED: michael@0: tab.onChange(); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: // This method persists the current ordered list of tabs in our tabs content provider. michael@0: public void persistAllTabs() { michael@0: ThreadUtils.postToBackgroundThread(mPersistTabsRunnable); michael@0: } michael@0: michael@0: /** michael@0: * Queues a request to persist tabs after PERSIST_TABS_AFTER_MILLISECONDS michael@0: * milliseconds have elapsed. If any existing requests are already queued then michael@0: * those requests are removed. michael@0: */ michael@0: private void queuePersistAllTabs() { michael@0: Handler backgroundHandler = ThreadUtils.getBackgroundHandler(); michael@0: backgroundHandler.removeCallbacks(mPersistTabsRunnable); michael@0: backgroundHandler.postDelayed(mPersistTabsRunnable, PERSIST_TABS_AFTER_MILLISECONDS); michael@0: } michael@0: michael@0: private void registerEventListener(String event) { michael@0: GeckoAppShell.getEventDispatcher().registerEventListener(event, this); michael@0: } michael@0: michael@0: /** michael@0: * Looks for an open tab with the given URL. michael@0: * @param url the URL of the tab we're looking for michael@0: * michael@0: * @return first Tab with the given URL, or null if there is no such tab. michael@0: */ michael@0: public Tab getFirstTabForUrl(String url) { michael@0: return getFirstTabForUrlHelper(url, null); michael@0: } michael@0: michael@0: /** michael@0: * Looks for an open tab with the given URL and private state. michael@0: * @param url the URL of the tab we're looking for michael@0: * @param isPrivate if true, only look for tabs that are private. if false, michael@0: * only look for tabs that are non-private. michael@0: * michael@0: * @return first Tab with the given URL, or null if there is no such tab. michael@0: */ michael@0: public Tab getFirstTabForUrl(String url, boolean isPrivate) { michael@0: return getFirstTabForUrlHelper(url, isPrivate); michael@0: } michael@0: michael@0: private Tab getFirstTabForUrlHelper(String url, Boolean isPrivate) { michael@0: if (url == null) { michael@0: return null; michael@0: } michael@0: michael@0: for (Tab tab : mOrder) { michael@0: if (isPrivate != null && isPrivate != tab.isPrivate()) { michael@0: continue; michael@0: } michael@0: String tabUrl = tab.getURL(); michael@0: if (AboutPages.isAboutReader(tabUrl)) { michael@0: tabUrl = ReaderModeUtils.getUrlFromAboutReader(tabUrl); michael@0: } michael@0: if (url.equals(tabUrl)) { michael@0: return tab; michael@0: } michael@0: } michael@0: michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * Loads a tab with the given URL in the currently selected tab. michael@0: * michael@0: * @param url URL of page to load, or search term used if searchEngine is given michael@0: */ michael@0: @RobocopTarget michael@0: public Tab loadUrl(String url) { michael@0: return loadUrl(url, LOADURL_NONE); michael@0: } michael@0: michael@0: /** michael@0: * Loads a tab with the given URL. michael@0: * michael@0: * @param url URL of page to load, or search term used if searchEngine is given michael@0: * @param flags flags used to load tab michael@0: * michael@0: * @return the Tab if a new one was created; null otherwise michael@0: */ michael@0: public Tab loadUrl(String url, int flags) { michael@0: return loadUrl(url, null, -1, flags); michael@0: } michael@0: michael@0: /** michael@0: * Loads a tab with the given URL. michael@0: * michael@0: * @param url URL of page to load, or search term used if searchEngine is given michael@0: * @param searchEngine if given, the search engine with this name is used michael@0: * to search for the url string; if null, the URL is loaded directly michael@0: * @param parentId ID of this tab's parent, or -1 if it has no parent michael@0: * @param flags flags used to load tab michael@0: * michael@0: * @return the Tab if a new one was created; null otherwise michael@0: */ michael@0: public Tab loadUrl(String url, String searchEngine, int parentId, int flags) { michael@0: JSONObject args = new JSONObject(); michael@0: Tab added = null; michael@0: boolean delayLoad = (flags & LOADURL_DELAY_LOAD) != 0; michael@0: michael@0: // delayLoad implies background tab michael@0: boolean background = delayLoad || (flags & LOADURL_BACKGROUND) != 0; michael@0: michael@0: try { michael@0: boolean isPrivate = (flags & LOADURL_PRIVATE) != 0; michael@0: boolean userEntered = (flags & LOADURL_USER_ENTERED) != 0; michael@0: boolean desktopMode = (flags & LOADURL_DESKTOP) != 0; michael@0: boolean external = (flags & LOADURL_EXTERNAL) != 0; michael@0: michael@0: args.put("url", url); michael@0: args.put("engine", searchEngine); michael@0: args.put("parentId", parentId); michael@0: args.put("userEntered", userEntered); michael@0: args.put("newTab", (flags & LOADURL_NEW_TAB) != 0); michael@0: args.put("isPrivate", isPrivate); michael@0: args.put("pinned", (flags & LOADURL_PINNED) != 0); michael@0: args.put("delayLoad", delayLoad); michael@0: args.put("desktopMode", desktopMode); michael@0: args.put("selected", !background); michael@0: michael@0: if ((flags & LOADURL_NEW_TAB) != 0) { michael@0: int tabId = getNextTabId(); michael@0: args.put("tabID", tabId); michael@0: michael@0: // The URL is updated for the tab once Gecko responds with the michael@0: // Tab:Added message. We can preliminarily set the tab's URL as michael@0: // long as it's a valid URI. michael@0: String tabUrl = (url != null && Uri.parse(url).getScheme() != null) ? url : null; michael@0: michael@0: added = addTab(tabId, tabUrl, external, parentId, url, isPrivate); michael@0: added.setDesktopMode(desktopMode); michael@0: } michael@0: } catch (Exception e) { michael@0: Log.w(LOGTAG, "Error building JSON arguments for loadUrl.", e); michael@0: } michael@0: michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Load", args.toString())); michael@0: michael@0: if (added == null) { michael@0: return null; michael@0: } michael@0: michael@0: if (!delayLoad && !background) { michael@0: selectTab(added.getId()); michael@0: } michael@0: michael@0: // TODO: surely we could just fetch *any* cached icon? michael@0: if (AboutPages.isDefaultIconPage(url)) { michael@0: Log.d(LOGTAG, "Setting about: tab favicon inline."); michael@0: added.updateFavicon(getAboutPageFavicon(url)); michael@0: } michael@0: michael@0: return added; michael@0: } michael@0: michael@0: /** michael@0: * These favicons are only used for the URL bar, so michael@0: * we fetch with that size. michael@0: * michael@0: * This method completes on the calling thread. michael@0: */ michael@0: private Bitmap getAboutPageFavicon(final String url) { michael@0: int faviconSize = Math.round(mAppContext.getResources().getDimension(R.dimen.browser_toolbar_favicon_size)); michael@0: return Favicons.getSizedFaviconForPageFromCache(url, faviconSize); michael@0: } michael@0: michael@0: /** michael@0: * Open the url as a new tab, and mark the selected tab as its "parent". michael@0: * michael@0: * If the url is already open in a tab, the existing tab is selected. michael@0: * Use this for tabs opened by the browser chrome, so users can press the michael@0: * "Back" button to return to the previous tab. michael@0: * michael@0: * @param url URL of page to load michael@0: */ michael@0: public void loadUrlInTab(String url) { michael@0: Iterable tabs = getTabsInOrder(); michael@0: for (Tab tab : tabs) { michael@0: if (url.equals(tab.getURL())) { michael@0: selectTab(tab.getId()); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // getSelectedTab() can return null if no tab has been created yet michael@0: // (i.e., we're restoring a session after a crash). In these cases, michael@0: // don't mark any tabs as a parent. michael@0: int parentId = -1; michael@0: Tab selectedTab = getSelectedTab(); michael@0: if (selectedTab != null) { michael@0: parentId = selectedTab.getId(); michael@0: } michael@0: michael@0: loadUrl(url, null, parentId, LOADURL_NEW_TAB); michael@0: } michael@0: michael@0: /** michael@0: * Gets the next tab ID. michael@0: */ michael@0: @JNITarget michael@0: public static int getNextTabId() { michael@0: return sTabId.getAndIncrement(); michael@0: } michael@0: }