Commit 1e25e2ef authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Signin][Android] Remove ChromeSigninController from ChromeBackupAgent

Replace ChromeSigninController in ChromeBackupAgent with
IdentityManager.GetPrimaryAccountInfo(). This is safe to do, because
the native side is already loaded - the same method accesses JNI methods
above.

Bug: 1046412
Change-Id: Icfa1522bf0f8ec552d6211dd4dad4b12e7f74d6b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2025468Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarAlice Wang <aliceywang@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738514}
parent b5f8a32a
...@@ -25,8 +25,10 @@ import org.chromium.chrome.browser.firstrun.FirstRunStatus; ...@@ -25,8 +25,10 @@ import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.init.AsyncInitTaskRunner; import org.chromium.chrome.browser.init.AsyncInitTaskRunner;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer; import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys; import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.components.signin.AccountManagerFacade; import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ChromeSigninController; import org.chromium.components.signin.ChromeSigninController;
import org.chromium.components.signin.base.CoreAccountInfo;
import org.chromium.content_public.browser.UiThreadTaskTraits; import org.chromium.content_public.browser.UiThreadTaskTraits;
import org.chromium.content_public.common.ContentProcessInfo; import org.chromium.content_public.common.ContentProcessInfo;
...@@ -234,9 +236,12 @@ public class ChromeBackupAgent extends BackupAgent { ...@@ -234,9 +236,12 @@ public class ChromeBackupAgent extends BackupAgent {
} }
// Finally add the user id. // Finally add the user id.
CoreAccountInfo accountInfo =
IdentityServicesProvider.get().getIdentityManager().getPrimaryAccountInfo();
// TODO(https://crbug.com/1046412): Inline SIGNED_IN_ACCOUNT_KEY in this class.
backupNames.add(ANDROID_DEFAULT_PREFIX + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY); backupNames.add(ANDROID_DEFAULT_PREFIX + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY);
backupValues.add(ApiCompatibilityUtils.getBytesUtf8( backupValues.add(ApiCompatibilityUtils.getBytesUtf8(
sharedPrefs.getString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, ""))); accountInfo == null ? "" : accountInfo.getEmail()));
BackupState newBackupState = new BackupState(backupNames, backupValues); BackupState newBackupState = new BackupState(backupNames, backupValues);
......
...@@ -14,7 +14,9 @@ import org.chromium.base.ContextUtils; ...@@ -14,7 +14,9 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.StrictModeContext; import org.chromium.base.StrictModeContext;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.components.signin.ChromeSigninController; import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.components.signin.base.CoreAccountInfo;
import org.chromium.components.signin.identitymanager.IdentityManager;
/** /**
* Class for watching for changes to the Android preferences that are backed up using Android * Class for watching for changes to the Android preferences that are backed up using Android
...@@ -46,14 +48,8 @@ public class ChromeBackupWatcher { ...@@ -46,14 +48,8 @@ public class ChromeBackupWatcher {
} }
sharedPrefs.edit().putBoolean(FIRST_BACKUP_DONE, true).apply(); sharedPrefs.edit().putBoolean(FIRST_BACKUP_DONE, true).apply();
} }
sharedPrefs.registerOnSharedPreferenceChangeListener( sharedPrefs.registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {
(sharedPreferences, key) -> { // Update the backup if any of the backed up Android preferences change.
// Update the backup if the user id or any of the backed up Android
// preferences change.
if (key.equals(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY)) {
onBackupPrefsChanged();
return;
}
for (String pref : ChromeBackupAgent.BACKUP_ANDROID_BOOL_PREFS) { for (String pref : ChromeBackupAgent.BACKUP_ANDROID_BOOL_PREFS) {
if (key.equals(pref)) { if (key.equals(pref)) {
onBackupPrefsChanged(); onBackupPrefsChanged();
...@@ -61,6 +57,19 @@ public class ChromeBackupWatcher { ...@@ -61,6 +57,19 @@ public class ChromeBackupWatcher {
} }
} }
}); });
// Update the backup if the sign-in status changes.
IdentityServicesProvider.get().getIdentityManager().addObserver(
new IdentityManager.Observer() {
@Override
public void onPrimaryAccountSet(CoreAccountInfo account) {
onBackupPrefsChanged();
}
@Override
public void onPrimaryAccountCleared(CoreAccountInfo account) {
onBackupPrefsChanged();
}
});
} }
@CalledByNative @CalledByNative
......
...@@ -29,7 +29,7 @@ public class IdentityServicesProvider { ...@@ -29,7 +29,7 @@ public class IdentityServicesProvider {
} }
@VisibleForTesting @VisibleForTesting
static void setInstanceForTests(IdentityServicesProvider provider) { public static void setInstanceForTests(IdentityServicesProvider provider) {
sIdentityServicesProvider = provider; sIdentityServicesProvider = provider;
} }
......
...@@ -48,7 +48,11 @@ import org.chromium.chrome.browser.firstrun.FirstRunStatus; ...@@ -48,7 +48,11 @@ import org.chromium.chrome.browser.firstrun.FirstRunStatus;
import org.chromium.chrome.browser.init.AsyncInitTaskRunner; import org.chromium.chrome.browser.init.AsyncInitTaskRunner;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer; import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys; import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.components.signin.ChromeSigninController; import org.chromium.components.signin.ChromeSigninController;
import org.chromium.components.signin.base.CoreAccountId;
import org.chromium.components.signin.base.CoreAccountInfo;
import org.chromium.components.signin.identitymanager.IdentityManager;
import org.chromium.content_public.common.ContentProcessInfo; import org.chromium.content_public.common.ContentProcessInfo;
import java.io.File; import java.io.File;
...@@ -91,15 +95,20 @@ public class ChromeBackupAgentTest { ...@@ -91,15 +95,20 @@ public class ChromeBackupAgentTest {
public JniMocker mocker = new JniMocker(); public JniMocker mocker = new JniMocker();
@Mock @Mock
private ChromeBackupAgent.Natives mChromeBackupAgentJniMock; private ChromeBackupAgent.Natives mChromeBackupAgentJniMock;
@Mock
private IdentityManager mIdentityManagerMock;
private ChromeBackupAgent mAgent; private ChromeBackupAgent mAgent;
private AsyncInitTaskRunner mTaskRunner; private AsyncInitTaskRunner mTaskRunner;
private final CoreAccountInfo mAccountInfo =
new CoreAccountInfo(new CoreAccountId("gaia_id_user1"), "user1", "gaia_id_user1");
private void setUpTestPrefs(SharedPreferences prefs) { private void setUpTestPrefs(SharedPreferences prefs) {
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(ChromePreferenceKeys.FIRST_RUN_FLOW_COMPLETE, true); editor.putBoolean(ChromePreferenceKeys.FIRST_RUN_FLOW_COMPLETE, true);
editor.putBoolean(ChromePreferenceKeys.FIRST_RUN_FLOW_SIGNIN_SETUP, false); editor.putBoolean(ChromePreferenceKeys.FIRST_RUN_FLOW_SIGNIN_SETUP, false);
editor.putString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, "user1"); when(mIdentityManagerMock.getPrimaryAccountInfo()).thenReturn(mAccountInfo);
editor.apply(); editor.apply();
} }
...@@ -123,6 +132,11 @@ public class ChromeBackupAgentTest { ...@@ -123,6 +132,11 @@ public class ChromeBackupAgentTest {
when(mChromeBackupAgentJniMock.getBoolBackupValues(mAgent)) when(mChromeBackupAgentJniMock.getBoolBackupValues(mAgent))
.thenReturn(new boolean[] {true}); .thenReturn(new boolean[] {true});
IdentityServicesProvider identityServicesProvider = mock(IdentityServicesProvider.class);
IdentityServicesProvider.setInstanceForTests(identityServicesProvider);
when(identityServicesProvider.getIdentityManager()).thenReturn(mIdentityManagerMock);
when(mIdentityManagerMock.getPrimaryAccountInfo()).thenReturn(null);
// Mock initializing the browser // Mock initializing the browser
doReturn(true).when(mAgent).initializeBrowser(); doReturn(true).when(mAgent).initializeBrowser();
...@@ -164,7 +178,7 @@ public class ChromeBackupAgentTest { ...@@ -164,7 +178,7 @@ public class ChromeBackupAgentTest {
.writeEntityHeader( .writeEntityHeader(
"AndroidDefault." + ChromePreferenceKeys.FIRST_RUN_FLOW_SIGNIN_SETUP, 1); "AndroidDefault." + ChromePreferenceKeys.FIRST_RUN_FLOW_SIGNIN_SETUP, 1);
verify(backupData).writeEntityData(new byte[] {0}, 1); verify(backupData).writeEntityData(new byte[] {0}, 1);
byte[] unameBytes = ApiCompatibilityUtils.getBytesUtf8("user1"); byte[] unameBytes = ApiCompatibilityUtils.getBytesUtf8(mAccountInfo.getEmail());
verify(backupData) verify(backupData)
.writeEntityHeader("AndroidDefault." + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, .writeEntityHeader("AndroidDefault." + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY,
unameBytes.length); unameBytes.length);
...@@ -368,7 +382,7 @@ public class ChromeBackupAgentTest { ...@@ -368,7 +382,7 @@ public class ChromeBackupAgentTest {
"AndroidDefault." + ChromePreferenceKeys.FIRST_RUN_FLOW_COMPLETE, "AndroidDefault." + ChromePreferenceKeys.FIRST_RUN_FLOW_COMPLETE,
"AndroidDefault.junk", "AndroidDefault.junk",
"AndroidDefault." + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY}; "AndroidDefault." + ChromeSigninController.SIGNED_IN_ACCOUNT_KEY};
byte[] unameBytes = ApiCompatibilityUtils.getBytesUtf8("user1"); byte[] unameBytes = ApiCompatibilityUtils.getBytesUtf8(mAccountInfo.getEmail());
final byte[][] values = {{0}, {1}, {1}, {23, 42}, unameBytes}; final byte[][] values = {{0}, {1}, {1}, {23, 42}, unameBytes};
when(backupData.getKey()).thenAnswer(new Answer<String>() { when(backupData.getKey()).thenAnswer(new Answer<String>() {
private int mPos; private int mPos;
......
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