Chromium Code Reviews| 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 import android.content.Intent; | |
| 9 | |
| 10 import org.chromium.base.ContextUtils; | |
| 11 import org.chromium.base.ThreadUtils; | |
| 12 import org.chromium.chrome.browser.browseractions.BrowserActionsTabCreatorManage r.BrowserActionsTabCreator; | |
| 13 import org.chromium.chrome.browser.tab.Tab; | |
| 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.TabModelOrderController; | |
| 23 import org.chromium.chrome.browser.tabmodel.TabModelSelectorBase; | |
| 24 import org.chromium.chrome.browser.tabmodel.TabModelSelectorTabObserver; | |
| 25 import org.chromium.chrome.browser.tabmodel.TabModelSelectorUma; | |
| 26 import org.chromium.chrome.browser.tabmodel.TabPersistencePolicy; | |
| 27 import org.chromium.chrome.browser.tabmodel.TabPersistentStore; | |
| 28 import org.chromium.chrome.browser.tabmodel.TabPersistentStore.TabPersistentStor eObserver; | |
| 29 import org.chromium.content_public.browser.LoadUrlParams; | |
| 30 | |
| 31 import java.util.concurrent.atomic.AtomicBoolean; | |
| 32 | |
| 33 /** | |
| 34 * The tab model selector creates Tabs for Browser Actions. Tabs created by it a re not shown in the | |
| 35 * foreground and don't need {@link TabContentMananger}. | |
| 36 */ | |
| 37 public class BrowsreActionsTabModelSelector | |
|
Yusuf
2017/07/18 22:59:47
I wonder if we should just move this to tabmodel/
Yusuf
2017/07/18 22:59:47
name typo
ltian
2017/08/07 23:24:12
Looks like the only thing need to change to public
ltian
2017/08/07 23:24:12
Ops, sorry.
| |
| 38 extends TabModelSelectorBase implements TabModelDelegate { | |
| 39 /** The singleton reference. */ | |
| 40 private static BrowsreActionsTabModelSelector sInstance; | |
| 41 | |
| 42 private final TabCreatorManager mTabCreatorManager; | |
| 43 | |
| 44 private final TabPersistentStore mTabSaver; | |
| 45 | |
| 46 private final TabModelOrderController mOrderController; | |
| 47 | |
| 48 private final TabModelSelectorUma mUma; | |
| 49 | |
| 50 /** Flag set to false when the asynchronous loading of tabs is finished. */ | |
| 51 private final AtomicBoolean mSessionRestoreInProgress = new AtomicBoolean(tr ue); | |
| 52 | |
| 53 // This flag signifies the object has gotten an onNativeReady callback and | |
|
Yusuf
2017/07/18 22:59:47
/** as well
ltian
2017/08/07 23:24:11
Done.
| |
| 54 // has not been destroyed. | |
| 55 private boolean mActiveState; | |
| 56 | |
| 57 /** | |
| 58 * Builds a {@link BrowsreActionsTabModelSelector} instance. | |
| 59 * | |
| 60 * @param activity An {@link Activity} instance. | |
| 61 * @param tabCreatorManager A {@link TabCreatorManager} instance. | |
| 62 * @param persistencePolicy A {@link TabPersistencePolicy} instance. | |
| 63 */ | |
| 64 private BrowsreActionsTabModelSelector(Activity activity, TabCreatorManager tabCreatorManager, | |
| 65 TabPersistencePolicy persistencePolicy) { | |
| 66 super(); | |
| 67 mTabCreatorManager = tabCreatorManager; | |
| 68 mUma = new TabModelSelectorUma(activity); | |
| 69 final TabPersistentStoreObserver persistentStoreObserver = | |
| 70 new TabPersistentStoreObserver() { | |
| 71 @Override | |
| 72 public void onStateLoaded() { | |
| 73 markTabStateInitialized(); | |
| 74 } | |
| 75 }; | |
| 76 mTabSaver = new TabPersistentStore( | |
| 77 persistencePolicy, this, mTabCreatorManager, persistentStoreObse rver); | |
|
Yusuf
2017/07/18 22:59:47
if we are going to just provide a new one in getIn
ltian
2017/08/07 23:24:11
Done.
| |
| 78 mOrderController = new TabModelOrderController(this); | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * @return The singleton instance of {@link BrowsreActionsTabModelSelector}. | |
| 83 */ | |
| 84 public static BrowsreActionsTabModelSelector getInstance( | |
| 85 Activity activity, TabCreatorManager tabCreatorManager) { | |
| 86 ThreadUtils.assertOnUiThread(); | |
| 87 if (sInstance == null) { | |
| 88 BrowserActionsTabPersistencePolicy persistencePolicy = | |
| 89 new BrowserActionsTabPersistencePolicy(); | |
| 90 sInstance = new BrowsreActionsTabModelSelector( | |
| 91 activity, tabCreatorManager, persistencePolicy); | |
| 92 } | |
| 93 return sInstance; | |
| 94 } | |
| 95 | |
| 96 @Override | |
| 97 public void markTabStateInitialized() { | |
| 98 super.markTabStateInitialized(); | |
| 99 if (!mSessionRestoreInProgress.getAndSet(false)) return; | |
| 100 | |
| 101 // This is the first time we set | |
| 102 // |mSessionRestoreInProgress|, so we need to broadcast. | |
| 103 TabModelImpl model = (TabModelImpl) getModel(false); | |
|
Yusuf
2017/07/18 22:59:47
override getModel return value explicitly instead
ltian
2017/08/07 23:24:11
Done.
| |
| 104 | |
| 105 if (model != null) { | |
| 106 model.broadcastSessionRestoreComplete(); | |
| 107 } else { | |
| 108 assert false : "Normal tab model is null after tab state loaded."; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 public void initializeSelector() { | |
|
Yusuf
2017/07/18 22:59:47
javadoc
ltian
2017/08/07 23:24:11
Done.
| |
| 113 assert !mActiveState : "onNativeLibraryReady called twice!"; | |
| 114 BrowserActionsTabCreator regularTabCreator = | |
| 115 (BrowserActionsTabCreator) mTabCreatorManager.getTabCreator(fals e); | |
| 116 BrowserActionsTabCreator incognitoTabCreator = | |
| 117 (BrowserActionsTabCreator) mTabCreatorManager.getTabCreator(true ); | |
| 118 TabModelImpl normalModel = new TabModelImpl(false, false, regularTabCrea tor, | |
| 119 incognitoTabCreator, mUma, mOrderController, null, mTabSaver, th is, false); | |
| 120 TabModel incognitoModel = | |
|
Yusuf
2017/07/18 22:59:47
we should make sure at least in first implementati
ltian
2017/08/07 23:24:11
This implementation initializes the incognito tab
| |
| 121 new IncognitoTabModel(new IncognitoTabModelImplCreator(regularTa bCreator, | |
| 122 incognitoTabCreator, mUma, mOrderController, null, mTabS aver, this)); | |
| 123 initialize(isIncognitoSelected(), normalModel, incognitoModel); | |
| 124 mActiveState = true; | |
| 125 new TabModelSelectorTabObserver(this) { | |
| 126 @Override | |
| 127 public void onLoadStopped(Tab tab, boolean toDifferentDocument) { | |
| 128 if (tab != null) mTabSaver.addTabToSaveQueue(tab); | |
| 129 Intent intent = BrowserActionsTabCreationService.getTabCreationI ntent( | |
| 130 BrowserActionsTabCreationService.ACTION_TAB_CREATION_FIN ISH); | |
| 131 ContextUtils.getApplicationContext().startService(intent); | |
| 132 } | |
| 133 }; | |
| 134 } | |
| 135 | |
| 136 /** | |
| 137 * @return Whether the selector has been initialized. | |
| 138 */ | |
| 139 public boolean isActiveState() { | |
| 140 return mActiveState; | |
| 141 } | |
| 142 | |
| 143 @Override | |
| 144 public Tab openNewTab( | |
| 145 LoadUrlParams loadUrlParams, TabLaunchType type, Tab parent, boolean incognito) { | |
| 146 return mTabCreatorManager.getTabCreator(incognito).createNewTab( | |
| 147 loadUrlParams, type, parent); | |
| 148 } | |
| 149 | |
| 150 @Override | |
| 151 public void requestToShowTab(Tab tab, TabSelectionType type) {} | |
| 152 | |
| 153 @Override | |
| 154 public boolean closeAllTabsRequest(boolean incognito) { | |
| 155 return false; | |
| 156 } | |
| 157 | |
| 158 @Override | |
| 159 public boolean isInOverviewMode() { | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 @Override | |
| 164 public boolean isSessionRestoreInProgress() { | |
| 165 return mSessionRestoreInProgress.get(); | |
| 166 } | |
| 167 | |
| 168 public void saveState() { | |
| 169 commitAllTabClosures(); | |
| 170 mTabSaver.saveState(); | |
| 171 } | |
| 172 | |
| 173 /** | |
| 174 * Load the saved tab state. This should be called before any new tabs are c reated. The saved | |
| 175 * tabs shall not be restored until {@link #restoreTabs} is called. | |
| 176 * @param ignoreIncognitoFiles Whether to skip loading incognito tabs. | |
| 177 */ | |
| 178 public void loadState(boolean ignoreIncognitoFiles) { | |
| 179 mTabSaver.loadState(ignoreIncognitoFiles); | |
| 180 } | |
| 181 | |
| 182 /** | |
| 183 * Restore the saved tabs which were loaded by {@link #loadState}. | |
| 184 * | |
| 185 * @param setActiveTab If true, synchronously load saved active tab and set it as the current | |
| 186 * active tab. | |
| 187 */ | |
| 188 public void restoreTabs(boolean setActiveTab) { | |
| 189 mTabSaver.restoreTabs(setActiveTab); | |
| 190 } | |
| 191 } | |
| OLD | NEW |