Skip to content

Commit

Permalink
Add plumbing to Settings to be passed the current Profile.
Browse files Browse the repository at this point in the history
This should be used instead of calling getLastUsedRegularProfile()
everywhere.

BUG=1410601

Change-Id: Ia3a7748f35247ed798e517653631811071d7c412
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4200999
Reviewed-by: Theresa Sullivan <twellington@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1098208}
  • Loading branch information
Ted Choc authored and Chromium LUCI CQ committed Jan 28, 2023
1 parent 9c616cc commit 0f10936
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 44 deletions.
Expand Up @@ -73,7 +73,7 @@
*/
public class MainSettings extends PreferenceFragmentCompat
implements TemplateUrlService.LoadListener, SyncService.SyncStateChangedListener,
SigninManager.SignInStateObserver {
SigninManager.SignInStateObserver, ProfileDependentSetting {
public static final String PREF_SYNC_PROMO = "sync_promo";
public static final String PREF_ACCOUNT_AND_GOOGLE_SERVICES_SECTION =
"account_and_google_services_section";
Expand All @@ -100,6 +100,7 @@ public class MainSettings extends PreferenceFragmentCompat
private SignInPreference mSignInPreference;
private ChromeBasePreference mManageSync;
private @Nullable PasswordCheck mPasswordCheck;
private Profile mProfile;
private ObservableSupplier<ModalDialogManager> mModalDialogManagerSupplier;

public MainSettings() {
Expand Down Expand Up @@ -139,8 +140,7 @@ public void onDestroy() {
@Override
public void onStart() {
super.onStart();
SigninManager signinManager = IdentityServicesProvider.get().getSigninManager(
Profile.getLastUsedRegularProfile());
SigninManager signinManager = IdentityServicesProvider.get().getSigninManager(mProfile);
if (signinManager.isSigninSupported()) {
signinManager.addSignInStateObserver(this);
}
Expand All @@ -153,8 +153,7 @@ public void onStart() {
@Override
public void onStop() {
super.onStop();
SigninManager signinManager = IdentityServicesProvider.get().getSigninManager(
Profile.getLastUsedRegularProfile());
SigninManager signinManager = IdentityServicesProvider.get().getSigninManager(mProfile);
if (signinManager.isSigninSupported()) {
signinManager.removeSignInStateObserver(this);
}
Expand All @@ -170,6 +169,11 @@ public void onResume() {
updatePreferences();
}

@Override
public void setProfile(Profile profile) {
mProfile = profile;
}

private void createPreferences() {
SettingsUtils.addPreferencesFromResource(this, R.xml.main_preferences);

Expand Down Expand Up @@ -239,9 +243,7 @@ private void setManagedPreferenceDelegateForPreference(String key) {
}

private void updatePreferences() {
if (IdentityServicesProvider.get()
.getSigninManager(Profile.getLastUsedRegularProfile())
.isSigninSupported()) {
if (IdentityServicesProvider.get().getSigninManager(mProfile).isSigninSupported()) {
addPreferenceIfAbsent(PREF_SIGN_IN);
} else {
removePreferenceIfPresent(PREF_SIGN_IN);
Expand Down Expand Up @@ -283,17 +285,15 @@ private void removePreferenceIfPresent(String key) {

private void updateManageSyncPreference() {
String primaryAccountName = CoreAccountInfo.getEmailFrom(
IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile())
.getPrimaryAccountInfo(ConsentLevel.SIGNIN));
IdentityServicesProvider.get().getIdentityManager(mProfile).getPrimaryAccountInfo(
ConsentLevel.SIGNIN));
boolean showManageSync = primaryAccountName != null;
mManageSync.setVisible(showManageSync);
if (!showManageSync) return;

boolean isSyncConsentAvailable =
IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile())
.getPrimaryAccountInfo(ConsentLevel.SYNC)
IdentityServicesProvider.get().getIdentityManager(mProfile).getPrimaryAccountInfo(
ConsentLevel.SYNC)
!= null;
mManageSync.setIcon(SyncSettingsUtils.getSyncStatusIcon(getActivity()));
mManageSync.setSummary(SyncSettingsUtils.getSyncStatusSummary(getActivity()));
Expand Down Expand Up @@ -342,8 +342,7 @@ private void updatePasswordsPreference() {
}
passwordsPreference.setOnPreferenceClickListener(preference -> {
if (shouldShowNewLabelForPasswordsPreference()) {
UserPrefs.get(Profile.getLastUsedRegularProfile())
.setBoolean(Pref.PASSWORDS_PREF_WITH_NEW_LABEL_USED, true);
UserPrefs.get(mProfile).setBoolean(Pref.PASSWORDS_PREF_WITH_NEW_LABEL_USED, true);
}
PasswordManagerLauncher.showPasswordSettings(getActivity(),
ManagePasswordsReferrer.CHROME_SETTINGS, mModalDialogManagerSupplier);
Expand All @@ -353,8 +352,7 @@ private void updatePasswordsPreference() {

private boolean shouldShowNewLabelForPasswordsPreference() {
return usesUnifiedPasswordManagerUI() && hasChosenToSyncPasswords(SyncService.get())
&& !UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(Pref.PASSWORDS_PREF_WITH_NEW_LABEL_USED);
&& !UserPrefs.get(mProfile).getBoolean(Pref.PASSWORDS_PREF_WITH_NEW_LABEL_USED);
}

// TODO(crbug.com/1217070): remove this method once UPM feature is rolled out.
Expand Down Expand Up @@ -424,8 +422,8 @@ public boolean isPreferenceControlledByPolicy(Preference preference) {
return TemplateUrlServiceFactory.get().isDefaultSearchManaged();
}
if (usesUnifiedPasswordManagerUI() && PREF_PASSWORDS.equals(preference.getKey())) {
return UserPrefs.get(Profile.getLastUsedRegularProfile())
.isManagedPreference(Pref.CREDENTIALS_ENABLE_SERVICE);
return UserPrefs.get(mProfile).isManagedPreference(
Pref.CREDENTIALS_ENABLE_SERVICE);
}
return false;
}
Expand Down
Expand Up @@ -132,6 +132,8 @@ public interface OnBackPressedListener {
@Nullable
private UiConfig mUiConfig;

private Profile mProfile;

@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -143,6 +145,7 @@ protected void onCreate(Bundle savedInstanceState) {
// killed, or for tests. This should happen before super.onCreate() because it might
// recreate a fragment, and a fragment might depend on the native library.
ChromeBrowserInitializer.getInstance().handleSynchronousStartup();
mProfile = Profile.getLastUsedRegularProfile();

super.onCreate(savedInstanceState);

Expand Down Expand Up @@ -392,9 +395,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
} else if (item.getItemId() == R.id.menu_id_general_help) {
HelpAndFeedbackLauncherImpl.getInstance().show(this,
getString(R.string.help_context_settings), Profile.getLastUsedRegularProfile(),
null);
HelpAndFeedbackLauncherImpl.getInstance().show(
this, getString(R.string.help_context_settings), mProfile, null);
return true;
}
return super.onOptionsItemSelected(item);
Expand All @@ -409,14 +411,16 @@ private boolean handleBackPressed() {

@Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof ProfileDependentSetting) {
((ProfileDependentSetting) fragment).setProfile(mProfile);
}
if (fragment instanceof MainSettings) {
((MainSettings) fragment)
.setModalDialogManagerSupplier(getModalDialogManagerSupplier());
}
if (fragment instanceof SiteSettingsPreferenceFragment) {
((SiteSettingsPreferenceFragment) fragment)
.setSiteSettingsDelegate(new ChromeSiteSettingsDelegate(
this, Profile.getLastUsedRegularProfile()));
.setSiteSettingsDelegate(new ChromeSiteSettingsDelegate(this, mProfile));
}
if (fragment instanceof FragmentSettingsLauncher) {
FragmentSettingsLauncher fragmentSettingsLauncher = (FragmentSettingsLauncher) fragment;
Expand Down Expand Up @@ -450,15 +454,14 @@ public void onAttachFragment(Fragment fragment) {
settings.setSettingsLauncher(mSettingsLauncher);
}
if (fragment instanceof ImageDescriptionsSettings) {
Profile profile = Profile.getLastUsedRegularProfile();
ImageDescriptionsSettings imageFragment = (ImageDescriptionsSettings) fragment;
Bundle extras = imageFragment.getArguments();
if (extras != null) {
extras.putBoolean(ImageDescriptionsSettings.IMAGE_DESCRIPTIONS,
ImageDescriptionsController.getInstance().imageDescriptionsEnabled(
profile));
mProfile));
extras.putBoolean(ImageDescriptionsSettings.IMAGE_DESCRIPTIONS_DATA_POLICY,
ImageDescriptionsController.getInstance().onlyOnWifiEnabled(profile));
ImageDescriptionsController.getInstance().onlyOnWifiEnabled(mProfile));
}
imageFragment.setDelegate(ImageDescriptionsController.getInstance().getDelegate());
}
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.chromium.base.ContextUtils;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.profiles.ProfileManagerUtils;
import org.chromium.chrome.browser.settings.SettingsActivityUnitTest.ShadowProfileManagerUtils;
import org.chromium.components.browser_ui.settings.CustomDividerFragment;
Expand All @@ -56,10 +57,13 @@ protected static void flushPersistentDataForAllProfiles() {}

@Mock
public ChromeBrowserInitializer mInitializer;
@Mock
public Profile mProfile;

@Before
public void setup() {
ChromeBrowserInitializer.setForTesting(mInitializer);
Profile.setLastUsedProfileForTesting(mProfile);
}

@After
Expand All @@ -69,6 +73,7 @@ public void tearDown() {
mActivityScenario = null;
}
ChromeBrowserInitializer.setForTesting(null);
Profile.setLastUsedProfileForTesting(null);
}

@Test
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.chromium.chrome.browser.preferences.PrefChangeRegistrar;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.ChromeManagedPreferenceDelegate;
import org.chromium.chrome.browser.settings.ProfileDependentSetting;
import org.chromium.chrome.browser.translate.TranslateBridge;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.browser_ui.settings.FragmentSettingsLauncher;
Expand All @@ -38,7 +39,8 @@
* seamlessly find and manage their languages preferences across platforms.
*/
public class LanguageSettings extends PreferenceFragmentCompat
implements SelectLanguageFragment.Launcher, FragmentSettingsLauncher {
implements SelectLanguageFragment.Launcher, FragmentSettingsLauncher,
ProfileDependentSetting {
// Return codes from launching Intents on preferences.
private static final int REQUEST_CODE_ADD_ACCEPT_LANGUAGE = 1;
private static final int REQUEST_CODE_CHANGE_APP_LANGUAGE = 2;
Expand All @@ -63,6 +65,7 @@ public class LanguageSettings extends PreferenceFragmentCompat
private AppLanguagePreferenceDelegate mAppLanguageDelegate =
new AppLanguagePreferenceDelegate();
private PrefChangeRegistrar mPrefChangeRegistrar;
private Profile mProfile;

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand All @@ -79,6 +82,11 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
LanguagesManager.recordImpression(LanguagesManager.LanguageSettingsPageType.PAGE_MAIN);
}

@Override
public void setProfile(Profile profile) {
mProfile = profile;
}

/**
* The detailed language preferences should be shown if the flag to enable them or the app
* language prompt is enabled. If neither flag is enabled, but an override language is set the
Expand Down Expand Up @@ -363,7 +371,7 @@ private void setLanguageListPreferenceClickListener(LanguageItemListPreference l
}

@VisibleForTesting
static PrefService getPrefService() {
return UserPrefs.get(Profile.getLastUsedRegularProfile());
PrefService getPrefService() {
return UserPrefs.get(mProfile);
}
}
Expand Up @@ -187,8 +187,8 @@ public void testEnabledAndDisableOfferToTranslate() {
// Restore this after test.
boolean enabledInDefault = pref.isChecked();
TestThreadUtils.runOnUiThreadBlocking(() -> {
boolean enabled =
LanguageSettings.getPrefService().getBoolean(Pref.OFFER_TRANSLATE_ENABLED);
boolean enabled = mSettingsActivityTestRule.getFragment().getPrefService().getBoolean(
Pref.OFFER_TRANSLATE_ENABLED);
Assert.assertEquals("The state of switch widget is different from local preference of "
+ "'offer to translate'.",
enabledInDefault, enabled);
Expand All @@ -208,7 +208,8 @@ public void testEnabledAndDisableOfferToTranslate() {
Assert.assertEquals("Preference of 'offer to translate' should be toggled when switch "
+ "widget is clicked.",
!enabledInDefault,
LanguageSettings.getPrefService().getBoolean(Pref.OFFER_TRANSLATE_ENABLED));
mSettingsActivityTestRule.getFragment().getPrefService().getBoolean(
Pref.OFFER_TRANSLATE_ENABLED));
});

TestThreadUtils.runOnUiThreadBlocking((Runnable) moreButton::performClick);
Expand All @@ -218,7 +219,7 @@ public void testEnabledAndDisableOfferToTranslate() {

// Reset state.
TestThreadUtils.runOnUiThreadBlocking(() -> {
LanguageSettings.getPrefService().setBoolean(
mSettingsActivityTestRule.getFragment().getPrefService().setBoolean(
Pref.OFFER_TRANSLATE_ENABLED, enabledInDefault);
});
}
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/settings/BUILD.gn
Expand Up @@ -8,6 +8,7 @@ android_library("java") {
sources = [
"android/java/src/org/chromium/chrome/browser/settings/ChromeManagedPreferenceDelegate.java",
"android/java/src/org/chromium/chrome/browser/settings/FaviconLoader.java",
"android/java/src/org/chromium/chrome/browser/settings/ProfileDependentSetting.java",
]
deps = [
"//base:base_java",
Expand Down
@@ -0,0 +1,17 @@
// Copyright 2023 The Chromium Authors
// 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.settings;

import org.chromium.chrome.browser.profiles.Profile;

/**
* Specifies that this settings entry is dependent on the current profile.
*/
public interface ProfileDependentSetting {
/**
* @param profile The currently selected profile.
*/
void setProfile(Profile profile);
}
1 change: 1 addition & 0 deletions chrome/browser/ui/android/night_mode/BUILD.gn
Expand Up @@ -32,6 +32,7 @@ android_library("java") {
"//chrome/browser/flags:java",
"//chrome/browser/preferences:java",
"//chrome/browser/profiles/android:java",
"//chrome/browser/settings:java",
"//components/browser_ui/settings/android:java",
"//components/browser_ui/site_settings/android:java",
"//components/browser_ui/strings/android:browser_ui_strings_grd",
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.chromium.chrome.browser.night_mode.WebContentsDarkModeMessageController;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.settings.ProfileDependentSetting;
import org.chromium.components.browser_ui.settings.CustomDividerFragment;
import org.chromium.components.browser_ui.settings.SettingsUtils;
import org.chromium.ui.UiUtils;
Expand All @@ -28,12 +29,13 @@
* Fragment to manage the theme user settings.
*/
public class ThemeSettingsFragment
extends PreferenceFragmentCompat implements CustomDividerFragment {
extends PreferenceFragmentCompat implements CustomDividerFragment, ProfileDependentSetting {
static final String PREF_UI_THEME_PREF = "ui_theme_pref";

public static final String KEY_THEME_SETTINGS_ENTRY = "theme_settings_entry";

private boolean mWebContentsDarkModeEnabled;
private Profile mProfile;

@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) {
Expand All @@ -43,8 +45,8 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
SharedPreferencesManager sharedPreferencesManager = SharedPreferencesManager.getInstance();
RadioButtonGroupThemePreference radioButtonGroupThemePreference =
(RadioButtonGroupThemePreference) findPreference(PREF_UI_THEME_PREF);
mWebContentsDarkModeEnabled = WebContentsDarkModeController.isGlobalUserSettingsEnabled(
Profile.getLastUsedRegularProfile());
mWebContentsDarkModeEnabled =
WebContentsDarkModeController.isGlobalUserSettingsEnabled(mProfile);
radioButtonGroupThemePreference.initialize(
NightModeUtils.getThemeSetting(), mWebContentsDarkModeEnabled);

Expand All @@ -56,7 +58,7 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
mWebContentsDarkModeEnabled =
radioButtonGroupThemePreference.isDarkenWebsitesEnabled();
WebContentsDarkModeController.setGlobalUserSettings(
Profile.getLastUsedRegularProfile(), mWebContentsDarkModeEnabled);
mProfile, mWebContentsDarkModeEnabled);
}
}
int theme = (int) newValue;
Expand All @@ -76,8 +78,7 @@ && getArguments().containsKey(KEY_THEME_SETTINGS_ENTRY)

if (ChromeFeatureList.isEnabled(
ChromeFeatureList.DARKEN_WEBSITES_CHECKBOX_IN_THEMES_SETTING)) {
WebContentsDarkModeMessageController.notifyEventSettingsOpened(
Profile.getLastUsedRegularProfile());
WebContentsDarkModeMessageController.notifyEventSettingsOpened(mProfile);
}
}

Expand All @@ -98,4 +99,9 @@ public void onActivityCreated(Bundle savedInstanceState) {
public boolean hasDivider() {
return false;
}

@Override
public void setProfile(Profile profile) {
mProfile = profile;
}
}

0 comments on commit 0f10936

Please sign in to comment.