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;
import androidx.annotation.Nullable;
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.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController;
......@@ -23,6 +25,7 @@ import org.chromium.chrome.browser.profiles.OTRProfileID;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHost;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry;
import org.chromium.ui.base.WindowAndroid;
import javax.inject.Inject;
......@@ -32,13 +35,18 @@ import javax.inject.Inject;
* |isEnabledIncognitoCCT| returns true.
*/
@ActivityScope
public class CustomTabIncognitoManager implements NativeInitObserver, Destroyable {
public class CustomTabIncognitoManager implements NativeInitObserver, Destroyable, UnownedUserData {
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 CustomTabActivityNavigationController mNavigationController;
private final BrowserServicesIntentDataProvider mIntentDataProvider;
private final CustomTabActivityTabProvider mTabProvider;
private final WindowAndroid mWindowAndroid;
private OTRProfileID mOTRProfileID;
@Nullable
......@@ -46,15 +54,46 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
@Inject
public CustomTabIncognitoManager(ChromeActivity<?> customTabActivity,
BrowserServicesIntentDataProvider intentDataProvider,
WindowAndroid windowAndroid, BrowserServicesIntentDataProvider intentDataProvider,
CustomTabActivityNavigationController navigationController,
CustomTabActivityTabProvider tabProvider,
ActivityLifecycleDispatcher lifecycleDispatcher) {
mChromeActivity = customTabActivity;
mWindowAndroid = windowAndroid;
mIntentDataProvider = intentDataProvider;
mNavigationController = navigationController;
mTabProvider = tabProvider;
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() {
......@@ -79,12 +118,15 @@ public class CustomTabIncognitoManager implements NativeInitObserver, Destroyabl
if (mIncognitoTabHost != null) {
IncognitoTabHostRegistry.getInstance().unregister(mIncognitoTabHost);
}
if (mOTRProfileID != null) {
Profile.getLastUsedRegularProfile()
.getOffTheRecordProfile(mOTRProfileID)
.destroyWhenAppropriate();
mOTRProfileID = null;
}
detach(this);
}
private void initializeIncognito() {
......
......@@ -35,6 +35,7 @@ import org.chromium.components.browser_ui.notifications.NotificationManagerProxy
import org.chromium.components.browser_ui.notifications.NotificationManagerProxyImpl;
import org.chromium.content_public.browser.ScreenOrientationProvider;
import org.chromium.ui.base.ActivityWindowAndroid;
import org.chromium.ui.base.WindowAndroid;
import javax.inject.Named;
......@@ -103,6 +104,11 @@ public class ChromeActivityCommonsModule {
return mActivity;
}
@Provides
public WindowAndroid provideWindowAndroid() {
return mActivity.getWindowAndroid();
}
@Provides
@Named(ACTIVITY_CONTEXT)
public Context provideContext() {
......
......@@ -12,6 +12,7 @@ import android.content.Intent;
import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ApplicationStatus;
......@@ -20,12 +21,14 @@ import org.chromium.base.IntentUtils;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.ChromeTabbedActivity;
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.tab.TabStateFileManager;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHost;
import org.chromium.chrome.browser.tabmodel.IncognitoTabHostRegistry;
import org.chromium.chrome.browser.tabpersistence.TabStateDirectory;
import org.chromium.chrome.browser.util.AndroidTaskUtils;
import org.chromium.ui.base.WindowAndroid;
import java.io.File;
import java.util.HashSet;
......@@ -161,6 +164,26 @@ public class IncognitoUtils {
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
public static void setEnabledForTesting(Boolean enabled) {
sIsEnabledForTesting = enabled;
......
......@@ -4,6 +4,8 @@
package org.chromium.chrome.browser.toolbar;
import static org.chromium.chrome.browser.incognito.IncognitoUtils.getNonPrimaryOTRProfileFromWindowAndroid;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
......@@ -276,12 +278,18 @@ public class LocationBarModel implements ToolbarDataProvider, ToolbarCommonPrope
public Profile getProfile() {
Profile lastUsedRegularProfile = Profile.getLastUsedRegularProfile();
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
// OffTheRecordProfile yet. #getOffTheRecordProfile will create a profile if none
// OTR profile yet. #getOffTheRecordProfile will create a profile if none
// exists.
assert lastUsedRegularProfile.hasOffTheRecordProfile()
|| isInOverviewAndShowingOmnibox();
return lastUsedRegularProfile.getOffTheRecordProfile();
assert lastUsedRegularProfile.hasPrimaryOTRProfile() || isInOverviewAndShowingOmnibox();
// Return the primary OTR profile.
return lastUsedRegularProfile.getPrimaryOTRProfile();
}
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