Commit 1bd10cbe authored by tedchoc's avatar tedchoc Committed by Commit bot

Add tab persistence support for CustomTabs.

BUG=606513

Review-Url: https://codereview.chromium.org/2296833002
Cr-Commit-Position: refs/heads/master@{#415786}
parent fd761037
...@@ -43,6 +43,17 @@ public class ApiCompatibilityUtils { ...@@ -43,6 +43,17 @@ public class ApiCompatibilityUtils {
private ApiCompatibilityUtils() { private ApiCompatibilityUtils() {
} }
/**
* @see Long#compare(long, long)
*/
public static int compareLong(long lhs, long rhs) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return Long.compare(lhs, rhs);
} else {
return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
}
}
/** /**
* Returns true if view's layout direction is right-to-left. * Returns true if view's layout direction is right-to-left.
* *
......
...@@ -58,7 +58,6 @@ import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; ...@@ -58,7 +58,6 @@ import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType;
import org.chromium.chrome.browser.tabmodel.TabModelObserver; import org.chromium.chrome.browser.tabmodel.TabModelObserver;
import org.chromium.chrome.browser.tabmodel.TabModelSelectorImpl; import org.chromium.chrome.browser.tabmodel.TabModelSelectorImpl;
import org.chromium.chrome.browser.tabmodel.TabPersistencePolicy; import org.chromium.chrome.browser.tabmodel.TabPersistencePolicy;
import org.chromium.chrome.browser.tabmodel.TabbedModeTabPersistencePolicy;
import org.chromium.chrome.browser.toolbar.ToolbarControlContainer; import org.chromium.chrome.browser.toolbar.ToolbarControlContainer;
import org.chromium.chrome.browser.util.ColorUtils; import org.chromium.chrome.browser.util.ColorUtils;
import org.chromium.chrome.browser.util.FeatureUtilities; import org.chromium.chrome.browser.util.FeatureUtilities;
...@@ -264,11 +263,8 @@ public class CustomTabActivity extends ChromeActivity { ...@@ -264,11 +263,8 @@ public class CustomTabActivity extends ChromeActivity {
@Override @Override
public void postInflationStartup() { public void postInflationStartup() {
super.postInflationStartup(); super.postInflationStartup();
// TODO(tedchoc): Replace with an actual CCT implementation for the persistence policy. TabPersistencePolicy persistencePolicy = new CustomTabTabPersistencePolicy(
// Currently, this relies on checks for CUSTOM_TABS_SELECTOR_INDEX in the getTaskId(), getSavedInstanceState() != null);
// main TabPersitentStore that ignores certain actions.
TabPersistencePolicy persistencePolicy = new TabbedModeTabPersistencePolicy(
this, TabModelSelectorImpl.CUSTOM_TABS_SELECTOR_INDEX);
setTabModelSelector(new TabModelSelectorImpl( setTabModelSelector(new TabModelSelectorImpl(
this, persistencePolicy, getWindowAndroid(), false)); this, persistencePolicy, getWindowAndroid(), false));
......
...@@ -1627,6 +1627,7 @@ public class Tab implements ViewGroup.OnHierarchyChangeListener, ...@@ -1627,6 +1627,7 @@ public class Tab implements ViewGroup.OnHierarchyChangeListener,
reparentingParams.finalizeTabReparenting(); reparentingParams.finalizeTabReparenting();
mIsDetachedForReparenting = false; mIsDetachedForReparenting = false;
mIsTabStateDirty = true;
for (TabObserver observer : mObservers) { for (TabObserver observer : mObservers) {
observer.onReparentingFinished(this); observer.onReparentingFinished(this);
......
...@@ -227,6 +227,17 @@ public class TabModelSelectorImpl extends TabModelSelectorBase implements TabMod ...@@ -227,6 +227,17 @@ public class TabModelSelectorImpl extends TabModelSelectorBase implements TabMod
}; };
} }
/**
* Exposed to allow tests to initialize the selector with different tab models.
* @param normalModel The normal tab model.
* @param incognitoModel The incognito tab model.
*/
@VisibleForTesting
public void initializeForTesting(TabModel normalModel, TabModel incognitoModel) {
initialize(isIncognitoSelected(), normalModel, incognitoModel);
mActiveState = true;
}
@Override @Override
public void setCloseAllTabsDelegate(CloseAllTabsDelegate delegate) { public void setCloseAllTabsDelegate(CloseAllTabsDelegate delegate) {
mCloseAllTabsDelegate = delegate; mCloseAllTabsDelegate = delegate;
......
...@@ -43,18 +43,21 @@ public interface TabPersistencePolicy { ...@@ -43,18 +43,21 @@ public interface TabPersistencePolicy {
String getStateToBeMergedFileName(); String getStateToBeMergedFileName();
/** /**
* Performs any necessary migration required before accessing the tab information. * Performs any necessary initialization required before accessing the tab information. This
* can include cleanups or migrations that must occur before the tab state information can be
* read reliably.
* *
* @param executor The executor that any asynchronous tasks should be run on. * @param executor The executor that any asynchronous tasks should be run on.
* @return Whether any migration is necessary. * @return Whether any blocking initialization is necessary.
*/ */
boolean performMigration(Executor executor); boolean performInitialization(Executor executor);
/** /**
* Waits for the task that migrates all state files to their new location to * Waits for the any pending initialization to finish.
* finish. *
* @see #performInitialization(Executor)
*/ */
void waitForMigrationToFinish(); void waitForInitializationToFinish();
/** /**
* @return Whether a merge is currently in progress. * @return Whether a merge is currently in progress.
......
...@@ -219,11 +219,12 @@ public class TabPersistentStore extends TabPersister { ...@@ -219,11 +219,12 @@ public class TabPersistentStore extends TabPersister {
mPreferences = ContextUtils.getAppSharedPreferences(); mPreferences = ContextUtils.getAppSharedPreferences();
assert isStateFile(policy.getStateFileName()) : "State file name is not valid"; assert isStateFile(policy.getStateFileName()) : "State file name is not valid";
boolean needsMigration = mPersistencePolicy.performMigration(AsyncTask.SERIAL_EXECUTOR); boolean needsInitialization = mPersistencePolicy.performInitialization(
AsyncTask.SERIAL_EXECUTOR);
if (mPersistencePolicy.isMergeInProgress()) return; if (mPersistencePolicy.isMergeInProgress()) return;
Executor executor = needsMigration Executor executor = needsInitialization
? AsyncTask.SERIAL_EXECUTOR : AsyncTask.THREAD_POOL_EXECUTOR; ? AsyncTask.SERIAL_EXECUTOR : AsyncTask.THREAD_POOL_EXECUTOR;
mPrefetchTabListTask = mPrefetchTabListTask =
...@@ -247,7 +248,7 @@ public class TabPersistentStore extends TabPersister { ...@@ -247,7 +248,7 @@ public class TabPersistentStore extends TabPersister {
*/ */
@VisibleForTesting @VisibleForTesting
public void waitForMigrationToFinish() { public void waitForMigrationToFinish() {
mPersistencePolicy.waitForMigrationToFinish(); mPersistencePolicy.waitForInitializationToFinish();
} }
/** /**
...@@ -998,7 +999,7 @@ public class TabPersistentStore extends TabPersister { ...@@ -998,7 +999,7 @@ public class TabPersistentStore extends TabPersister {
* @param forMerge Whether this state file was read as part of a merge. * @param forMerge Whether this state file was read as part of a merge.
* @return The next available tab ID based on the maximum ID referenced in this state file. * @return The next available tab ID based on the maximum ID referenced in this state file.
*/ */
protected static int readSavedStateFile( public static int readSavedStateFile(
DataInputStream stream, @Nullable OnTabStateReadCallback callback, DataInputStream stream, @Nullable OnTabStateReadCallback callback,
@Nullable SparseBooleanArray tabIds, boolean forMerge) throws IOException { @Nullable SparseBooleanArray tabIds, boolean forMerge) throws IOException {
if (stream == null) return 0; if (stream == null) return 0;
...@@ -1366,10 +1367,20 @@ public class TabPersistentStore extends TabPersister { ...@@ -1366,10 +1367,20 @@ public class TabPersistentStore extends TabPersister {
return TabPersistencePolicy.SAVED_STATE_FILE_PREFIX + uniqueId; return TabPersistencePolicy.SAVED_STATE_FILE_PREFIX + uniqueId;
} }
/**
* Parses the state file name and returns the unique ID encoded into it.
* @param stateFileName The state file name to be parsed.
* @return The unique ID used when generating the file name.
*/
public static String getStateFileUniqueId(String stateFileName) {
assert isStateFile(stateFileName);
return stateFileName.substring(TabPersistencePolicy.SAVED_STATE_FILE_PREFIX.length());
}
/** /**
* @return Whether the specified filename matches the expected pattern of the tab state files. * @return Whether the specified filename matches the expected pattern of the tab state files.
*/ */
private static boolean isStateFile(String fileName) { public static boolean isStateFile(String fileName) {
return fileName.startsWith(TabPersistencePolicy.SAVED_STATE_FILE_PREFIX); return fileName.startsWith(TabPersistencePolicy.SAVED_STATE_FILE_PREFIX);
} }
......
...@@ -143,7 +143,7 @@ public class TabbedModeTabPersistencePolicy implements TabPersistencePolicy { ...@@ -143,7 +143,7 @@ public class TabbedModeTabPersistencePolicy implements TabPersistencePolicy {
} }
@Override @Override
public boolean performMigration(Executor executor) { public boolean performInitialization(Executor executor) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
final boolean hasRunLegacyMigration = final boolean hasRunLegacyMigration =
...@@ -291,7 +291,7 @@ public class TabbedModeTabPersistencePolicy implements TabPersistencePolicy { ...@@ -291,7 +291,7 @@ public class TabbedModeTabPersistencePolicy implements TabPersistencePolicy {
} }
@Override @Override
public void waitForMigrationToFinish() { public void waitForInitializationToFinish() {
if (sMigrationTask == null) return; if (sMigrationTask == null) return;
try { try {
sMigrationTask.get(); sMigrationTask.get();
......
...@@ -269,6 +269,7 @@ chrome_java_sources = [ ...@@ -269,6 +269,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/customtabs/CustomTabObserver.java", "java/src/org/chromium/chrome/browser/customtabs/CustomTabObserver.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabsConnection.java", "java/src/org/chromium/chrome/browser/customtabs/CustomTabsConnection.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabsConnectionService.java", "java/src/org/chromium/chrome/browser/customtabs/CustomTabsConnectionService.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabTabPersistencePolicy.java",
"java/src/org/chromium/chrome/browser/customtabs/RequestThrottler.java", "java/src/org/chromium/chrome/browser/customtabs/RequestThrottler.java",
"java/src/org/chromium/chrome/browser/customtabs/SeparateTaskCustomTabActivity.java", "java/src/org/chromium/chrome/browser/customtabs/SeparateTaskCustomTabActivity.java",
"java/src/org/chromium/chrome/browser/customtabs/SeparateTaskCustomTabActivity0.java", "java/src/org/chromium/chrome/browser/customtabs/SeparateTaskCustomTabActivity0.java",
...@@ -1157,6 +1158,7 @@ chrome_test_java_sources = [ ...@@ -1157,6 +1158,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/customtabs/CustomTabExternalNavigationTest.java", "javatests/src/org/chromium/chrome/browser/customtabs/CustomTabExternalNavigationTest.java",
"javatests/src/org/chromium/chrome/browser/customtabs/CustomTabsConnectionTest.java", "javatests/src/org/chromium/chrome/browser/customtabs/CustomTabsConnectionTest.java",
"javatests/src/org/chromium/chrome/browser/customtabs/CustomTabsTestUtils.java", "javatests/src/org/chromium/chrome/browser/customtabs/CustomTabsTestUtils.java",
"javatests/src/org/chromium/chrome/browser/customtabs/CustomTabTabPersistencePolicyTest.java",
"javatests/src/org/chromium/chrome/browser/customtabs/RequestThrottlerTest.java", "javatests/src/org/chromium/chrome/browser/customtabs/RequestThrottlerTest.java",
"javatests/src/org/chromium/chrome/browser/document/LauncherActivityTest.java", "javatests/src/org/chromium/chrome/browser/document/LauncherActivityTest.java",
"javatests/src/org/chromium/chrome/browser/dom_distiller/DistillabilityServiceTest.java", "javatests/src/org/chromium/chrome/browser/dom_distiller/DistillabilityServiceTest.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment