Commit 0d2a553d authored by Side Yilmaz's avatar Side Yilmaz Committed by Commit Bot

Add profile supplier param to IdentityDiscController constructor.

This CL adds |mProfileSupplier| local variable that is observed for
profile changes. |mProfileSupplier| is used to pass profile param to
IdentityServicesProvider.

By this CL;
- |mProfileSupplier| notifies the class when the current profile is
changed.
- When current profile changes, setProfile function is triggered and
mIdentityManager is updated with the new profile.
- Introducing |getSyncAccountInfo| function that uses current profile
(i.e., off-the-record or regular) and returns account information. This
function returns null for OTR profiles.
- Passing current profile to tracker instead of using always regular
profile to fix unsafe usage of profile.
- Adding |testIdentityDiscWithSwitchToIncognito| to test identity disc
disappear when switching to incognito NTP from sign-in state.

Bug: 1041781, 1075562, 1048632
Change-Id: Icd443c03033b5a20c1ba0b680f57c05dfc21222d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2307253
Commit-Queue: Side YILMAZ <sideyilmaz@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarRamin Halavati <rhalavati@chromium.org>
Reviewed-by: default avatarPavel Yatsuk <pavely@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803158}
parent b58d86b1
...@@ -68,6 +68,8 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -68,6 +68,8 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
private final Context mContext; private final Context mContext;
private ActivityLifecycleDispatcher mActivityLifecycleDispatcher; private ActivityLifecycleDispatcher mActivityLifecycleDispatcher;
private final ObservableSupplier<Boolean> mBottomToolbarVisibilitySupplier; private final ObservableSupplier<Boolean> mBottomToolbarVisibilitySupplier;
private final ObservableSupplier<Profile> mProfileSupplier;
private final Callback<Profile> mProfileSupplierObserver = this::setProfile;
private @Nullable Callback<Boolean> mBottomToolbarVisibilityObserver; private @Nullable Callback<Boolean> mBottomToolbarVisibilityObserver;
...@@ -98,10 +100,12 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -98,10 +100,12 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
*/ */
public IdentityDiscController(Context context, public IdentityDiscController(Context context,
ActivityLifecycleDispatcher activityLifecycleDispatcher, ActivityLifecycleDispatcher activityLifecycleDispatcher,
ObservableSupplier<Boolean> bottomToolbarVisibilitySupplier) { ObservableSupplier<Boolean> bottomToolbarVisibilitySupplier,
ObservableSupplier<Profile> profileSupplier) {
mContext = context; mContext = context;
mActivityLifecycleDispatcher = activityLifecycleDispatcher; mActivityLifecycleDispatcher = activityLifecycleDispatcher;
mBottomToolbarVisibilitySupplier = bottomToolbarVisibilitySupplier; mBottomToolbarVisibilitySupplier = bottomToolbarVisibilitySupplier;
mProfileSupplier = profileSupplier;
mActivityLifecycleDispatcher.register(this); mActivityLifecycleDispatcher.register(this);
mButtonData = new ButtonData(false, null, mButtonData = new ButtonData(false, null,
...@@ -128,11 +132,10 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -128,11 +132,10 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
mActivityLifecycleDispatcher = null; mActivityLifecycleDispatcher = null;
mNativeIsInitialized = true; mNativeIsInitialized = true;
mIdentityManager = IdentityServicesProvider.get().getIdentityManager(); mProfileSupplier.addObserver(mProfileSupplierObserver);
mIdentityManager.addObserver(this);
mBottomToolbarVisibilityObserver = (bottomToolbarIsVisible) mBottomToolbarVisibilityObserver =
-> notifyObservers(mIdentityManager.getPrimaryAccountInfo() != null); (bottomToolbarIsVisible) -> notifyObservers(getSyncAccountInfo() != null);
mBottomToolbarVisibilitySupplier.addObserver(mBottomToolbarVisibilityObserver); mBottomToolbarVisibilitySupplier.addObserver(mBottomToolbarVisibilityObserver);
} }
...@@ -174,8 +177,7 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -174,8 +177,7 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
return; return;
} }
String email = CoreAccountInfo.getEmailFrom( String email = CoreAccountInfo.getEmailFrom(getSyncAccountInfo());
mIdentityManager.getPrimaryAccountInfo(ConsentLevel.SYNC));
boolean canShowIdentityDisc = email != null; boolean canShowIdentityDisc = email != null;
boolean menuBottomOnBottom = boolean menuBottomOnBottom =
bottomToolbarVisible && BottomToolbarVariationManager.isMenuButtonOnBottom(); bottomToolbarVisible && BottomToolbarVariationManager.isMenuButtonOnBottom();
...@@ -247,8 +249,7 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -247,8 +249,7 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
if (mState == IdentityDiscState.NONE) return; if (mState == IdentityDiscState.NONE) return;
assert mProfileDataCache[mState] != null; assert mProfileDataCache[mState] != null;
CoreAccountInfo accountInfo = mIdentityManager.getPrimaryAccountInfo(); if (accountEmail.equals(CoreAccountInfo.getEmailFrom(getSyncAccountInfo()))) {
if (accountEmail.equals(CoreAccountInfo.getEmailFrom(accountInfo))) {
notifyObservers(true); notifyObservers(true);
} }
} }
...@@ -291,6 +292,10 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -291,6 +292,10 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
mBottomToolbarVisibilitySupplier.removeObserver(mBottomToolbarVisibilityObserver); mBottomToolbarVisibilitySupplier.removeObserver(mBottomToolbarVisibilityObserver);
mBottomToolbarVisibilityObserver = null; mBottomToolbarVisibilityObserver = null;
} }
if (mNativeIsInitialized) {
mProfileSupplier.removeObserver(mProfileSupplierObserver);
}
} }
/** /**
...@@ -298,12 +303,36 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa ...@@ -298,12 +303,36 @@ public class IdentityDiscController implements NativeInitObserver, ProfileDataCa
* whether to show in-product help. * whether to show in-product help.
*/ */
private void recordIdentityDiscUsed() { private void recordIdentityDiscUsed() {
// TODO (https://crbug.com/1048632): Use the current profile (i.e., regular profile or assert mProfileSupplier != null && mProfileSupplier.get() != null;
// incognito profile) instead of always using regular profile. It works correctly now, but Tracker tracker = TrackerFactory.getTrackerForProfile(mProfileSupplier.get());
// it is not safe.
Profile profile = Profile.getLastUsedRegularProfile();
Tracker tracker = TrackerFactory.getTrackerForProfile(profile);
tracker.notifyEvent(EventConstants.IDENTITY_DISC_USED); tracker.notifyEvent(EventConstants.IDENTITY_DISC_USED);
RecordUserAction.record("MobileToolbarIdentityDiscTap"); RecordUserAction.record("MobileToolbarIdentityDiscTap");
} }
/**
* Returns the account info of mIdentityManager if current profile is regular, and
* null for off-the-record ones.
* @return account info for the current profile. Returns null for OTR profile.
*/
private CoreAccountInfo getSyncAccountInfo() {
return mIdentityManager != null ? mIdentityManager.getPrimaryAccountInfo(ConsentLevel.SYNC)
: null;
}
/**
* Triggered by mProfileSupplierObserver when profile is changed in mProfileSupplier.
* mIdentityManager is updated with the profile, as set to null if profile is off-the-record.
*/
private void setProfile(Profile profile) {
if (mIdentityManager != null) {
mIdentityManager.removeObserver(this);
}
if (profile.isOffTheRecord()) {
mIdentityManager = null;
} else {
mIdentityManager = IdentityServicesProvider.get().getIdentityManager(profile);
mIdentityManager.addObserver(this);
}
}
} }
...@@ -504,8 +504,9 @@ public class RootUiCoordinator ...@@ -504,8 +504,9 @@ public class RootUiCoordinator
new ObservableSupplierImpl<>(); new ObservableSupplierImpl<>();
bottomToolbarVisibilitySupplier.set(false); bottomToolbarVisibilitySupplier.set(false);
mIdentityDiscController = new IdentityDiscController( mIdentityDiscController =
mActivity, mActivity.getLifecycleDispatcher(), bottomToolbarVisibilitySupplier); new IdentityDiscController(mActivity, mActivity.getLifecycleDispatcher(),
bottomToolbarVisibilitySupplier, mProfileSupplier);
ShareButtonController shareButtonController = new ShareButtonController(mActivity, ShareButtonController shareButtonController = new ShareButtonController(mActivity,
mActivityTabProvider, mShareDelegateSupplier, new ShareUtils(), mActivityTabProvider, mShareDelegateSupplier, new ShareUtils(),
bottomToolbarVisibilitySupplier, mActivity.getLifecycleDispatcher(), bottomToolbarVisibilitySupplier, mActivity.getLifecycleDispatcher(),
......
...@@ -106,6 +106,18 @@ public class IdentityDiscControllerTest { ...@@ -106,6 +106,18 @@ public class IdentityDiscControllerTest {
withEffectiveVisibility(ViewMatchers.Visibility.GONE))); withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
} }
@Test
@MediumTest
public void testIdentityDiscWithSwitchToIncognito() {
mAccountManagerTestRule.addAndSignInTestAccount();
waitForView(allOf(withId(R.id.optional_toolbar_button), isDisplayed()));
// Identity Disc should not be visible, when switched from sign in state to incognito NTP.
mActivityTestRule.newIncognitoTabFromMenu();
waitForView(allOf(withId(R.id.optional_toolbar_button),
withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
}
private void leaveNTP() { private void leaveNTP() {
mActivityTestRule.loadUrl(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL); mActivityTestRule.loadUrl(ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
ChromeTabUtils.waitForTabPageLoaded(mTab, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL); ChromeTabUtils.waitForTabPageLoaded(mTab, ContentUrlConstants.ABOUT_BLANK_DISPLAY_URL);
......
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