Commit ba7450e8 authored by Rohit Agarwal's avatar Rohit Agarwal Committed by Commit Bot

Add support for UnownedUserData{Host} in CustomTabIncognitoManager.

This CL adds the support of linking CustomTabIncognitoManager instance
with a WindowAndroid object instance. This would enable querying the
non primary OTR profile from the CustomTabIncognitoManager instance.

This CL is step [1] for the refactoring the TabModelImpl to take in
profile as an argument.

[1] go/incognito-cct-profile-passing-support

Bug: 1099642
Change-Id: I0b4e0e3959247925f41292ce8b15855aa8fe4347
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2396336
Commit-Queue: Rohit Agarwal <roagarwal@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806127}
parent a627b31b
...@@ -9,6 +9,8 @@ import android.view.WindowManager; ...@@ -9,6 +9,8 @@ import android.view.WindowManager;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import org.chromium.base.CommandLine; import org.chromium.base.CommandLine;
import org.chromium.base.UnownedUserData;
import org.chromium.base.UnownedUserDataKey;
import org.chromium.chrome.browser.app.ChromeActivity; import org.chromium.chrome.browser.app.ChromeActivity;
import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider; import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController; import org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController;
...@@ -23,6 +25,7 @@ import org.chromium.chrome.browser.profiles.OTRProfileID; ...@@ -23,6 +25,7 @@ import org.chromium.chrome.browser.profiles.OTRProfileID;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHost; import org.chromium.chrome.browser.tabmodel.IncognitoTabHost;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry; import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry;
import org.chromium.ui.base.WindowAndroid;
import javax.inject.Inject; import javax.inject.Inject;
...@@ -32,13 +35,18 @@ import javax.inject.Inject; ...@@ -32,13 +35,18 @@ import javax.inject.Inject;
* |isEnabledIncognitoCCT| returns true. * |isEnabledIncognitoCCT| returns true.
*/ */
@ActivityScope @ActivityScope
public class CustomTabIncognitoManager implements NativeInitObserver, Destroyable { public class CustomTabIncognitoManager implements NativeInitObserver, Destroyable, UnownedUserData {
private static final String TAG = "CctIncognito"; private static final String TAG = "CctIncognito";
/** The key for accessing this object on an {@link org.chromium.base.UnownedUserDataHost}. */
private static final UnownedUserDataKey<CustomTabIncognitoManager> KEY =
new UnownedUserDataKey<>(CustomTabIncognitoManager.class);
private final ChromeActivity<?> mChromeActivity; private final ChromeActivity<?> mChromeActivity;
private final CustomTabActivityNavigationController mNavigationController; private final CustomTabActivityNavigationController mNavigationController;
private final BrowserServicesIntentDataProvider mIntentDataProvider; private final BrowserServicesIntentDataProvider mIntentDataProvider;
private final CustomTabActivityTabProvider mTabProvider; private final CustomTabActivityTabProvider mTabProvider;
private final WindowAndroid mWindowAndroid;
private OTRProfileID mOTRProfileID; private OTRProfileID mOTRProfileID;
@Nullable @Nullable
...@@ -46,15 +54,46 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl ...@@ -46,15 +54,46 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
@Inject @Inject
public CustomTabIncognitoManager(ChromeActivity<?> customTabActivity, public CustomTabIncognitoManager(ChromeActivity<?> customTabActivity,
BrowserServicesIntentDataProvider intentDataProvider, WindowAndroid windowAndroid, BrowserServicesIntentDataProvider intentDataProvider,
CustomTabActivityNavigationController navigationController, CustomTabActivityNavigationController navigationController,
CustomTabActivityTabProvider tabProvider, CustomTabActivityTabProvider tabProvider,
ActivityLifecycleDispatcher lifecycleDispatcher) { ActivityLifecycleDispatcher lifecycleDispatcher) {
mChromeActivity = customTabActivity; mChromeActivity = customTabActivity;
mWindowAndroid = windowAndroid;
mIntentDataProvider = intentDataProvider; mIntentDataProvider = intentDataProvider;
mNavigationController = navigationController; mNavigationController = navigationController;
mTabProvider = tabProvider; mTabProvider = tabProvider;
lifecycleDispatcher.register(this); lifecycleDispatcher.register(this);
attach(mWindowAndroid, this);
}
/**
* Get the Activity's {@link CustomTabIncognitoManager} from the provided {@link
* WindowAndroid}.
* @param window The window to get the manager from.
* @return The Activity's {@link CustomTabIncognitoManager}.
*/
public static @Nullable CustomTabIncognitoManager from(WindowAndroid window) {
return KEY.retrieveDataFromHost(window.getUnownedUserDataHost());
}
/**
* Make this instance of CustomTabIncognitoManager available through the activity's window.
* @param window A {@link WindowAndroid} to attach to.
* @param manager The {@link CustomTabIncognitoManager} to attach.
*/
private static void attach(WindowAndroid window, CustomTabIncognitoManager manager) {
KEY.attachToHost(window.getUnownedUserDataHost(), manager);
}
/**
* Detach the provided CustomTabIncognitoManager from any host it is associated with.
* @param manager The {@link CustomTabIncognitoManager} to detach.
*/
private static void detach(CustomTabIncognitoManager manager) {
KEY.detachFromAllHosts(manager);
} }
public boolean isEnabledIncognitoCCT() { public boolean isEnabledIncognitoCCT() {
...@@ -79,12 +118,15 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl ...@@ -79,12 +118,15 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
if (mIncognitoTabHost != null) { if (mIncognitoTabHost != null) {
IncognitoTabHostRegistry.getInstance().unregister(mIncognitoTabHost); IncognitoTabHostRegistry.getInstance().unregister(mIncognitoTabHost);
} }
if (mOTRProfileID != null) { if (mOTRProfileID != null) {
Profile.getLastUsedRegularProfile() Profile.getLastUsedRegularProfile()
.getOffTheRecordProfile(mOTRProfileID) .getOffTheRecordProfile(mOTRProfileID)
.destroyWhenAppropriate(); .destroyWhenAppropriate();
mOTRProfileID = null; mOTRProfileID = null;
} }
detach(this);
} }
private void initializeIncognito() { private void initializeIncognito() {
......
...@@ -35,6 +35,7 @@ import org.chromium.components.browser_ui.notifications.NotificationManagerProxy ...@@ -35,6 +35,7 @@ import org.chromium.components.browser_ui.notifications.NotificationManagerProxy
import org.chromium.components.browser_ui.notifications.NotificationManagerProxyImpl; import org.chromium.components.browser_ui.notifications.NotificationManagerProxyImpl;
import org.chromium.content_public.browser.ScreenOrientationProvider; import org.chromium.content_public.browser.ScreenOrientationProvider;
import org.chromium.ui.base.ActivityWindowAndroid; import org.chromium.ui.base.ActivityWindowAndroid;
import org.chromium.ui.base.WindowAndroid;
import javax.inject.Named; import javax.inject.Named;
...@@ -103,6 +104,11 @@ public class ChromeActivityCommonsModule { ...@@ -103,6 +104,11 @@ public class ChromeActivityCommonsModule {
return mActivity; return mActivity;
} }
@Provides
public WindowAndroid provideWindowAndroid() {
return mActivity.getWindowAndroid();
}
@Provides @Provides
@Named(ACTIVITY_CONTEXT) @Named(ACTIVITY_CONTEXT)
public Context provideContext() { public Context provideContext() {
......
...@@ -12,6 +12,7 @@ import android.content.Intent; ...@@ -12,6 +12,7 @@ import android.content.Intent;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import org.chromium.base.ApplicationStatus; import org.chromium.base.ApplicationStatus;
...@@ -20,12 +21,14 @@ import org.chromium.base.IntentUtils; ...@@ -20,12 +21,14 @@ import org.chromium.base.IntentUtils;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.ChromeTabbedActivity; import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.IntentHandler; import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.customtabs.CustomTabIncognitoManager;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.TabStateFileManager; import org.chromium.chrome.browser.tab.TabStateFileManager;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHost; import org.chromium.chrome.browser.tabmodel.IncognitoTabHost;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry; import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry;
import org.chromium.chrome.browser.tabpersistence.TabStateDirectory; import org.chromium.chrome.browser.tabpersistence.TabStateDirectory;
import org.chromium.chrome.browser.util.AndroidTaskUtils; import org.chromium.chrome.browser.util.AndroidTaskUtils;
import org.chromium.ui.base.WindowAndroid;
import java.io.File; import java.io.File;
import java.util.HashSet; import java.util.HashSet;
...@@ -161,6 +164,26 @@ public class IncognitoUtils { ...@@ -161,6 +164,26 @@ public class IncognitoUtils {
intent, IntentHandler.EXTRA_INVOKED_FROM_LAUNCH_NEW_INCOGNITO_TAB, false); intent, IntentHandler.EXTRA_INVOKED_FROM_LAUNCH_NEW_INCOGNITO_TAB, false);
} }
/**
* Returns the non primary OTR profile if any that is associated with a |windowAndroid|
* instance, otherwise null.
* <p>
* A non primary OTR profile is associated only for the case of incognito CustomTabActivity.
* <p>
* @param windowAndroid The {@link WindowAndroid} instance for which the non primary OTR
* profile is queried.
*/
public static @Nullable Profile getNonPrimaryOTRProfileFromWindowAndroid(
@Nullable WindowAndroid windowAndroid) {
if (windowAndroid == null) return null;
CustomTabIncognitoManager customTabIncognitoManager =
CustomTabIncognitoManager.from(windowAndroid);
if (customTabIncognitoManager == null) return null;
return customTabIncognitoManager.getProfile();
}
@VisibleForTesting @VisibleForTesting
public static void setEnabledForTesting(Boolean enabled) { public static void setEnabledForTesting(Boolean enabled) {
sIsEnabledForTesting = enabled; sIsEnabledForTesting = enabled;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
package org.chromium.chrome.browser.toolbar; package org.chromium.chrome.browser.toolbar;
import static org.chromium.chrome.browser.incognito.IncognitoUtils.getNonPrimaryOTRProfileFromWindowAndroid;
import android.content.Context; import android.content.Context;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.TextUtils; import android.text.TextUtils;
...@@ -276,12 +278,18 @@ public class LocationBarModel implements ToolbarDataProvider, ToolbarCommonPrope ...@@ -276,12 +278,18 @@ public class LocationBarModel implements ToolbarDataProvider, ToolbarCommonPrope
public Profile getProfile() { public Profile getProfile() {
Profile lastUsedRegularProfile = Profile.getLastUsedRegularProfile(); Profile lastUsedRegularProfile = Profile.getLastUsedRegularProfile();
if (mIsIncognito) { if (mIsIncognito) {
// If the mTab belongs to a CustomTabActivity then we return the non-primary OTR profile
// which is associated with it. For all other cases we return the primary OTR profile.
Profile nonPrimaryOTRProfile =
getNonPrimaryOTRProfileFromWindowAndroid(mTab.getWindowAndroid());
if (nonPrimaryOTRProfile != null) return nonPrimaryOTRProfile;
// When in overview mode with no open tabs, there has not been created an // When in overview mode with no open tabs, there has not been created an
// OffTheRecordProfile yet. #getOffTheRecordProfile will create a profile if none // OTR profile yet. #getOffTheRecordProfile will create a profile if none
// exists. // exists.
assert lastUsedRegularProfile.hasOffTheRecordProfile() assert lastUsedRegularProfile.hasPrimaryOTRProfile() || isInOverviewAndShowingOmnibox();
|| isInOverviewAndShowingOmnibox(); // Return the primary OTR profile.
return lastUsedRegularProfile.getOffTheRecordProfile(); return lastUsedRegularProfile.getPrimaryOTRProfile();
} }
return lastUsedRegularProfile; return lastUsedRegularProfile;
} }
......
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