Commit d30e1d32 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Android: TabParentIntent for intent handling

TabParentIntent encapsulates an Intent object with its triggering
logic for bring up the activity a tab was opened from.

Bug: 925242
Change-Id: Ib33e9f77744696b9026408c388e6e38b1345029b
Reviewed-on: https://chromium-review.googlesource.com/c/1472270
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634376}
parent 504b4aab
...@@ -76,6 +76,9 @@ public class EmptyTabObserver implements TabObserver { ...@@ -76,6 +76,9 @@ public class EmptyTabObserver implements TabObserver {
@Override @Override
public void onContextualActionBarVisibilityChanged(Tab tab, boolean visible) { } public void onContextualActionBarVisibilityChanged(Tab tab, boolean visible) { }
@Override
public void onCloseContents(Tab tab) {}
@Override @Override
public void onLoadStarted(Tab tab, boolean toDifferentDocument) { } public void onLoadStarted(Tab tab, boolean toDifferentDocument) { }
......
...@@ -181,13 +181,6 @@ public class Tab ...@@ -181,13 +181,6 @@ public class Tab
*/ */
private int mRootId; private int mRootId;
/**
* If this tab was opened from another tab in another Activity, this is the Intent that can be
* fired to bring the parent Activity back.
* TODO(dfalcantara): Remove this mechanism when we have a global TabManager.
*/
private Intent mParentIntent;
/** /**
* Whether the tab should be grouped with its parent tab. * Whether the tab should be grouped with its parent tab.
*/ */
...@@ -2474,21 +2467,6 @@ public class Tab ...@@ -2474,21 +2467,6 @@ public class Tab
return 0; return 0;
} }
/**
* Sets the Intent that can be fired to restart the Activity of this Tab's parent.
* Should only be called if the Tab was launched via a different Activity.
*/
public void setParentIntent(Intent parentIntent) {
mParentIntent = parentIntent;
}
/**
* @return Intent that can be fired to restart the parent Activity.
*/
protected Intent getParentIntent() {
return mParentIntent;
}
/** /**
* Creates a new, "frozen" tab from a saved state. This can be used for background tabs restored * Creates a new, "frozen" tab from a saved state. This can be used for background tabs restored
* on cold start that should be loaded when switched to. initialize() needs to be called * on cold start that should be loaded when switched to. initialize() needs to be called
......
...@@ -161,6 +161,12 @@ public interface TabObserver { ...@@ -161,6 +161,12 @@ public interface TabObserver {
// WebContentsDelegateAndroid methods --------------------------------------------------------- // WebContentsDelegateAndroid methods ---------------------------------------------------------
/**
* Called when the WebContents is closed.
* @param tab The notifying {@link Tab}.
*/
void onCloseContents(Tab tab);
/** /**
* Called when the WebContents starts loading. Different from * Called when the WebContents starts loading. Different from
* {@link #onPageLoadStarted(Tab, String)}, if the user is navigated to a different url while * {@link #onPageLoadStarted(Tab, String)}, if the user is navigated to a different url while
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.tab;
import android.content.Intent;
import org.chromium.base.UserData;
import org.chromium.base.UserDataHost;
/**
* A holder of {@link Intent} object to be used to bring back the parent {@link Activity}
* from which the associated tab was opened.
*/
public final class TabParentIntent extends EmptyTabObserver implements UserData {
private static final Class<TabParentIntent> USER_DATA_KEY = TabParentIntent.class;
private final Tab mTab;
/**
* If the associated tab was opened from another tab in another Activity, this is the Intent
* that can be fired to bring the parent Activity back.
*/
private Intent mParentIntent;
public static TabParentIntent from(Tab tab) {
UserDataHost host = tab.getUserDataHost();
TabParentIntent tabParentIntent = host.getUserData(USER_DATA_KEY);
if (tabParentIntent == null) {
tabParentIntent = host.setUserData(USER_DATA_KEY, new TabParentIntent(tab));
}
return tabParentIntent;
}
private TabParentIntent(Tab tab) {
mTab = tab;
mTab.addObserver(this);
}
@Override
public void onCloseContents(Tab tab) {
boolean isSelected = mTab.getTabModelSelector().getCurrentTab() == tab;
// If the parent Tab belongs to another Activity, fire the Intent to bring it back.
if (isSelected && mParentIntent != null && tab.getActivity().getIntent() != mParentIntent) {
tab.getActivity().startActivity(mParentIntent);
}
}
public void set(Intent intent) {
mParentIntent = intent;
}
@Override
public void destroy() {
mTab.removeObserver(this);
}
}
...@@ -6,7 +6,6 @@ package org.chromium.chrome.browser.tab; ...@@ -6,7 +6,6 @@ package org.chromium.chrome.browser.tab;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
...@@ -30,7 +29,6 @@ import org.chromium.chrome.R; ...@@ -30,7 +29,6 @@ import org.chromium.chrome.R;
import org.chromium.chrome.browser.AppHooks; import org.chromium.chrome.browser.AppHooks;
import org.chromium.chrome.browser.ChromeActivity; import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.SwipeRefreshHandler; import org.chromium.chrome.browser.SwipeRefreshHandler;
import org.chromium.chrome.browser.document.DocumentUtils;
import org.chromium.chrome.browser.document.DocumentWebContentsDelegate; import org.chromium.chrome.browser.document.DocumentWebContentsDelegate;
import org.chromium.chrome.browser.findinpage.FindMatchRectsDetails; import org.chromium.chrome.browser.findinpage.FindMatchRectsDetails;
import org.chromium.chrome.browser.findinpage.FindNotificationDetails; import org.chromium.chrome.browser.findinpage.FindNotificationDetails;
...@@ -77,47 +75,23 @@ public class TabWebContentsDelegateAndroid extends WebContentsDelegateAndroid { ...@@ -77,47 +75,23 @@ public class TabWebContentsDelegateAndroid extends WebContentsDelegateAndroid {
/** Used for logging. */ /** Used for logging. */
private static final String TAG = "WebContentsDelegate"; private static final String TAG = "WebContentsDelegate";
private final Runnable mCloseContentsRunnable;
protected final Tab mTab; protected final Tab mTab;
protected Handler mHandler;
private FindResultListener mFindResultListener; private FindResultListener mFindResultListener;
private FindMatchRectsListener mFindMatchRectsListener; private FindMatchRectsListener mFindMatchRectsListener;
private @WebDisplayMode int mDisplayMode = WebDisplayMode.BROWSER; private @WebDisplayMode int mDisplayMode = WebDisplayMode.BROWSER;
protected Handler mHandler;
private final Runnable mCloseContentsRunnable = new Runnable() {
@Override
public void run() {
boolean isSelected = mTab.getTabModelSelector().getCurrentTab() == mTab;
mTab.getTabModelSelector().closeTab(mTab);
// If the parent Tab belongs to another Activity, fire the Intent to bring it back.
if (isSelected && mTab.getParentIntent() != null
&& mTab.getActivity().getIntent() != mTab.getParentIntent()) {
mTab.getActivity().startActivity(mTab.getParentIntent());
}
}
/** If the API allows it, returns whether a Task still exists for the parent Activity. */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean isParentInAndroidOverview() {
ActivityManager activityManager = (ActivityManager) mTab.getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.AppTask task : activityManager.getAppTasks()) {
Intent taskIntent = DocumentUtils.getBaseIntentFromTask(task);
if (taskIntent != null && taskIntent.filterEquals(mTab.getParentIntent())) {
return true;
}
}
return false;
}
};
public TabWebContentsDelegateAndroid(Tab tab) { public TabWebContentsDelegateAndroid(Tab tab) {
mTab = tab; mTab = tab;
mHandler = new Handler(); mHandler = new Handler();
mCloseContentsRunnable = () -> {
// TODO(jinsukkim): Move |closeTab| to TabModelSelector by making it observe its tabs.
mTab.getTabModelSelector().closeTab(mTab);
RewindableIterator<TabObserver> observers = mTab.getTabObservers();
while (observers.hasNext()) observers.next().onCloseContents(mTab);
};
} }
/** /**
......
...@@ -16,6 +16,7 @@ import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; ...@@ -16,6 +16,7 @@ import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager;
import org.chromium.chrome.browser.net.spdyproxy.DataReductionProxySettings; import org.chromium.chrome.browser.net.spdyproxy.DataReductionProxySettings;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabDelegateFactory; import org.chromium.chrome.browser.tab.TabDelegateFactory;
import org.chromium.chrome.browser.tab.TabParentIntent;
import org.chromium.chrome.browser.tab.TabRedirectHandler; import org.chromium.chrome.browser.tab.TabRedirectHandler;
import org.chromium.chrome.browser.tab.TabState; import org.chromium.chrome.browser.tab.TabState;
import org.chromium.chrome.browser.util.IntentUtils; import org.chromium.chrome.browser.util.IntentUtils;
...@@ -132,7 +133,7 @@ public class ChromeTabCreator extends TabCreatorManager.TabCreator { ...@@ -132,7 +133,7 @@ public class ChromeTabCreator extends TabCreatorManager.TabCreator {
!openInForeground); !openInForeground);
tab.initialize( tab.initialize(
webContents, mTabContentManager, delegateFactory, !openInForeground, false); webContents, mTabContentManager, delegateFactory, !openInForeground, false);
tab.setParentIntent(parentIntent); TabParentIntent.from(tab).set(parentIntent);
webContents.resumeLoadingCreatedWebContents(); webContents.resumeLoadingCreatedWebContents();
} else if (!openInForeground && SysUtils.isLowEndDevice()) { } else if (!openInForeground && SysUtils.isLowEndDevice()) {
// On low memory devices the tabs opened in background are not loaded automatically // On low memory devices the tabs opened in background are not loaded automatically
......
...@@ -1562,6 +1562,7 @@ chrome_java_sources = [ ...@@ -1562,6 +1562,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/tab/TabIdManager.java", "java/src/org/chromium/chrome/browser/tab/TabIdManager.java",
"java/src/org/chromium/chrome/browser/tab/TabImportanceManager.java", "java/src/org/chromium/chrome/browser/tab/TabImportanceManager.java",
"java/src/org/chromium/chrome/browser/tab/TabObserver.java", "java/src/org/chromium/chrome/browser/tab/TabObserver.java",
"java/src/org/chromium/chrome/browser/tab/TabParentIntent.java",
"java/src/org/chromium/chrome/browser/tab/TabRedirectHandler.java", "java/src/org/chromium/chrome/browser/tab/TabRedirectHandler.java",
"java/src/org/chromium/chrome/browser/tab/TabState.java", "java/src/org/chromium/chrome/browser/tab/TabState.java",
"java/src/org/chromium/chrome/browser/tab/TabStateBrowserControlsVisibilityDelegate.java", "java/src/org/chromium/chrome/browser/tab/TabStateBrowserControlsVisibilityDelegate.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