OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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.browser.browseractions; |
| 6 |
| 7 import android.app.Activity; |
| 8 |
| 9 import org.chromium.base.ThreadUtils; |
| 10 import org.chromium.base.VisibleForTesting; |
| 11 import org.chromium.chrome.browser.browseractions.BrowserActionsTabCreatorManage
r.BrowserActionsTabCreator; |
| 12 import org.chromium.chrome.browser.tab.Tab; |
| 13 import org.chromium.chrome.browser.tabmodel.EmptyTabModelObserver; |
| 14 import org.chromium.chrome.browser.tabmodel.IncognitoTabModel; |
| 15 import org.chromium.chrome.browser.tabmodel.IncognitoTabModelImplCreator; |
| 16 import org.chromium.chrome.browser.tabmodel.TabCreatorManager; |
| 17 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 18 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
| 19 import org.chromium.chrome.browser.tabmodel.TabModel.TabSelectionType; |
| 20 import org.chromium.chrome.browser.tabmodel.TabModelDelegate; |
| 21 import org.chromium.chrome.browser.tabmodel.TabModelImpl; |
| 22 import org.chromium.chrome.browser.tabmodel.TabModelObserver; |
| 23 import org.chromium.chrome.browser.tabmodel.TabModelOrderController; |
| 24 import org.chromium.chrome.browser.tabmodel.TabModelSelectorBase; |
| 25 import org.chromium.chrome.browser.tabmodel.TabPersistentStore; |
| 26 import org.chromium.chrome.browser.tabmodel.TabPersistentStore.TabPersistentStor
eObserver; |
| 27 import org.chromium.content_public.browser.LoadUrlParams; |
| 28 |
| 29 import java.util.concurrent.atomic.AtomicBoolean; |
| 30 |
| 31 /** |
| 32 * The tab model selector creates Tabs for Browser Actions. Tabs created by it a
re not shown in the |
| 33 * foreground and don't need {@link TabContentMananger}. |
| 34 */ |
| 35 public class BrowserActionsTabModelSelector |
| 36 extends TabModelSelectorBase implements TabModelDelegate { |
| 37 /** The singleton reference. */ |
| 38 private static BrowserActionsTabModelSelector sInstance; |
| 39 |
| 40 private final TabCreatorManager mTabCreatorManager; |
| 41 |
| 42 private final TabPersistentStore mTabSaver; |
| 43 |
| 44 private final TabModelOrderController mOrderController; |
| 45 |
| 46 /** Flag set to false when the asynchronous loading of tabs is finished. */ |
| 47 private final AtomicBoolean mSessionRestoreInProgress = new AtomicBoolean(tr
ue); |
| 48 |
| 49 /** This flag signifies the object has gotten an onNativeReady callback and |
| 50 /* has not been destroyed. */ |
| 51 private boolean mActiveState; |
| 52 |
| 53 /** |
| 54 * Builds a {@link BrowserActionsTabModelSelector} instance. |
| 55 * |
| 56 * @param activity An {@link Activity} instance. |
| 57 * @param tabCreatorManager A {@link TabCreatorManager} instance. |
| 58 */ |
| 59 private BrowserActionsTabModelSelector(Activity activity, TabCreatorManager
tabCreatorManager) { |
| 60 super(); |
| 61 mTabCreatorManager = tabCreatorManager; |
| 62 final TabPersistentStoreObserver persistentStoreObserver = |
| 63 new TabPersistentStoreObserver() { |
| 64 @Override |
| 65 public void onStateLoaded() { |
| 66 markTabStateInitialized(); |
| 67 } |
| 68 }; |
| 69 BrowserActionsTabPersistencePolicy persistencePolicy = |
| 70 new BrowserActionsTabPersistencePolicy(); |
| 71 mTabSaver = new TabPersistentStore( |
| 72 persistencePolicy, this, mTabCreatorManager, persistentStoreObse
rver); |
| 73 mOrderController = new TabModelOrderController(this); |
| 74 } |
| 75 |
| 76 /** |
| 77 * @return The singleton instance of {@link BrowserActionsTabModelSelector}. |
| 78 */ |
| 79 public static BrowserActionsTabModelSelector getInstance( |
| 80 Activity activity, TabCreatorManager tabCreatorManager) { |
| 81 ThreadUtils.assertOnUiThread(); |
| 82 if (sInstance == null) { |
| 83 sInstance = new BrowserActionsTabModelSelector(activity, tabCreatorM
anager); |
| 84 } |
| 85 return sInstance; |
| 86 } |
| 87 |
| 88 @Override |
| 89 public void markTabStateInitialized() { |
| 90 super.markTabStateInitialized(); |
| 91 if (!mSessionRestoreInProgress.getAndSet(false)) return; |
| 92 |
| 93 // This is the first time we set |
| 94 // |mSessionRestoreInProgress|, so we need to broadcast. |
| 95 TabModelImpl model = getModel(); |
| 96 |
| 97 if (model != null) { |
| 98 model.broadcastSessionRestoreComplete(); |
| 99 } else { |
| 100 assert false : "Normal tab model is null after tab state loaded."; |
| 101 } |
| 102 } |
| 103 |
| 104 /** |
| 105 * Initializes the selectors for the {@link BrowserActionsTabModelSelector}. |
| 106 */ |
| 107 public void initializeSelector() { |
| 108 assert !mActiveState : "onNativeLibraryReady called twice!"; |
| 109 BrowserActionsTabCreator regularTabCreator = |
| 110 (BrowserActionsTabCreator) mTabCreatorManager.getTabCreator(fals
e); |
| 111 BrowserActionsTabCreator incognitoTabCreator = |
| 112 (BrowserActionsTabCreator) mTabCreatorManager.getTabCreator(true
); |
| 113 TabModelImpl normalModel = new TabModelImpl(false, false, regularTabCrea
tor, |
| 114 incognitoTabCreator, null, mOrderController, null, mTabSaver, th
is, false); |
| 115 TabModel incognitoModel = |
| 116 new IncognitoTabModel(new IncognitoTabModelImplCreator(regularTa
bCreator, |
| 117 incognitoTabCreator, null, mOrderController, null, mTabS
aver, this)); |
| 118 initialize(isIncognitoSelected(), normalModel, incognitoModel); |
| 119 mActiveState = true; |
| 120 TabModelObserver tabModelObserver = new EmptyTabModelObserver() { |
| 121 @Override |
| 122 public void didAddTab(Tab tab, TabLaunchType type) { |
| 123 if (type == TabLaunchType.FROM_BROWSER_ACTIONS) { |
| 124 notifyNewTabCreated(tab); |
| 125 } |
| 126 } |
| 127 }; |
| 128 normalModel.addObserver(tabModelObserver); |
| 129 } |
| 130 |
| 131 /** |
| 132 * @return Whether the selector has been initialized. |
| 133 */ |
| 134 public boolean isActiveState() { |
| 135 return mActiveState; |
| 136 } |
| 137 |
| 138 private void notifyNewTabCreated(Tab tab) { |
| 139 if (tab != null) mTabSaver.addTabToSaveQueue(tab); |
| 140 BrowserActionsService.sendIntent( |
| 141 BrowserActionsService.ACTION_TAB_CREATION_FINISH, Tab.INVALID_TA
B_ID); |
| 142 } |
| 143 |
| 144 @Override |
| 145 public Tab openNewTab( |
| 146 LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent, boolean
incognito) { |
| 147 return mTabCreatorManager.getTabCreator(incognito).createNewTab( |
| 148 loadUrlParams, type, parent); |
| 149 } |
| 150 |
| 151 @Override |
| 152 public void requestToShowTab(Tab tab, TabSelectionType type) {} |
| 153 |
| 154 @Override |
| 155 public boolean closeAllTabsRequest(boolean incognito) { |
| 156 return false; |
| 157 } |
| 158 |
| 159 @Override |
| 160 public boolean isInOverviewMode() { |
| 161 return false; |
| 162 } |
| 163 |
| 164 @Override |
| 165 public boolean isSessionRestoreInProgress() { |
| 166 return mSessionRestoreInProgress.get(); |
| 167 } |
| 168 |
| 169 public TabModelImpl getModel() { |
| 170 return (TabModelImpl) getModel(false); |
| 171 } |
| 172 |
| 173 public void saveState() { |
| 174 commitAllTabClosures(); |
| 175 mTabSaver.saveState(); |
| 176 } |
| 177 |
| 178 /** |
| 179 * Load the saved tab state. This should be called before any new tabs are c
reated. The saved |
| 180 * tabs shall not be restored until {@link #restoreTabs} is called. |
| 181 * @param ignoreIncognitoFiles Whether to skip loading incognito tabs. |
| 182 */ |
| 183 public void loadState(boolean ignoreIncognitoFiles) { |
| 184 mTabSaver.loadState(ignoreIncognitoFiles); |
| 185 } |
| 186 |
| 187 /** |
| 188 * Restore the saved tabs which were loaded by {@link #loadState}. |
| 189 * |
| 190 * @param setActiveTab If true, synchronously load saved active tab and set
it as the current |
| 191 * active tab. |
| 192 */ |
| 193 public void restoreTabs(boolean setActiveTab) { |
| 194 mTabSaver.restoreTabs(setActiveTab); |
| 195 } |
| 196 |
| 197 @Override |
| 198 @VisibleForTesting |
| 199 protected void clearData() { |
| 200 mActiveState = false; |
| 201 sInstance = null; |
| 202 super.clearData(); |
| 203 } |
| 204 } |
OLD | NEW |