[M73]Keep the Settings>Passwords menu item enabled when saving is disabled by policy

The current behavior disables the Passwords menu item when saving passwords
is disabled by policy, however users should still be able to view previously
saved passwords.

Linking to the Google Password Manager will be disabled if saving passwords
is managed.

Bug: 925036
Change-Id: Ie94bf3ea0ce6703aa9f1fd67ed26ad513c780d87
Reviewed-on: https://chromium-review.googlesource.com/c/1433241
Reviewed-by: Theresa <twellington@chromium.org>
Commit-Queue: Ioana Pandele <ioanap@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#626057}(cherry picked from commit 13ad2f4290d8dfedec27901f8c0734f8ebd3bc6b)
Reviewed-on: https://chromium-review.googlesource.com/c/1447716
Reviewed-by: Ioana Pandele <ioanap@chromium.org>
Cr-Commit-Position: refs/branch-heads/3683@{#89}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java
index 1a255a71..66b5165 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/MainPreferences.java
@@ -111,12 +111,11 @@
             getPreferenceScreen().removePreference(findPreference(PREF_SYNC_AND_SERVICES));
         }
 
-        setManagedPreferenceDelegateForPreference(PREF_SEARCH_ENGINE);
-        setManagedPreferenceDelegateForPreference(PREF_SAVED_PASSWORDS);
-        setManagedPreferenceDelegateForPreference(PREF_DATA_REDUCTION);
-
         updatePasswordsPreference();
 
+        setManagedPreferenceDelegateForPreference(PREF_SEARCH_ENGINE);
+        setManagedPreferenceDelegateForPreference(PREF_DATA_REDUCTION);
+
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             // If we are on Android O+ the Notifications preference should lead to the Android
             // Settings notifications page, not to Chrome's notifications settings page.
@@ -307,9 +306,6 @@
         return new ManagedPreferenceDelegate() {
             @Override
             public boolean isPreferenceControlledByPolicy(Preference preference) {
-                if (PREF_SAVED_PASSWORDS.equals(preference.getKey())) {
-                    return PrefServiceBridge.getInstance().isRememberPasswordsManaged();
-                }
                 if (PREF_DATA_REDUCTION.equals(preference.getKey())) {
                     return DataReductionProxySettings.getInstance().isDataReductionProxyManaged();
                 }
@@ -321,11 +317,6 @@
 
             @Override
             public boolean isPreferenceClickDisabledByPolicy(Preference preference) {
-                if (PREF_SAVED_PASSWORDS.equals(preference.getKey())) {
-                    PrefServiceBridge prefs = PrefServiceBridge.getInstance();
-                    return prefs.isRememberPasswordsManaged()
-                            && !prefs.isRememberPasswordsEnabled();
-                }
                 if (PREF_DATA_REDUCTION.equals(preference.getKey())) {
                     DataReductionProxySettings settings = DataReductionProxySettings.getInstance();
                     return settings.isDataReductionProxyManaged()
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/PreferencesLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/PreferencesLauncher.java
index 2aba1536..b8cd5bf 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/PreferencesLauncher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/PreferencesLauncher.java
@@ -133,12 +133,13 @@
             Activity activity, @ManagePasswordsReferrer int referrer) {
         RecordHistogram.recordEnumeratedHistogram("PasswordManager.ManagePasswordsReferrer",
                 referrer, ManagePasswordsReferrer.MAX_VALUE + 1);
-
         if (isSyncingPasswordsWithoutCustomPassphrase()) {
             RecordHistogram.recordEnumeratedHistogram(
                     "PasswordManager.ManagePasswordsReferrerSignedInAndSyncing", referrer,
                     ManagePasswordsReferrer.MAX_VALUE + 1);
-            if (tryShowingTheGooglePasswordManager(activity)) return;
+            if (!PrefServiceBridge.getInstance().isRememberPasswordsManaged()) {
+                if (tryShowingTheGooglePasswordManager(activity)) return;
+            }
         }
 
         launchSettingsPage(activity, SavePasswordsPreferences.class);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferences.java
index 5928d3d..abdba9d 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferences.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferences.java
@@ -23,6 +23,7 @@
 import android.view.MenuItem;
 
 import org.chromium.base.ApiCompatibilityUtils;
+import org.chromium.base.StrictModeContext;
 import org.chromium.base.VisibleForTesting;
 import org.chromium.base.metrics.RecordHistogram;
 import org.chromium.chrome.R;
@@ -409,7 +410,10 @@
         });
         mSavePasswordsSwitch.setManagedPreferenceDelegate(
                 preference -> PrefServiceBridge.getInstance().isRememberPasswordsManaged());
-        getPreferenceScreen().addPreference(mSavePasswordsSwitch);
+
+        try (StrictModeContext ctx = StrictModeContext.allowDiskReads()) {
+            getPreferenceScreen().addPreference(mSavePasswordsSwitch);
+        }
 
         // Note: setting the switch state before the preference is added to the screen results in
         // some odd behavior where the switch state doesn't always match the internal enabled state
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java
index e0f2786..e3bab92 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java
@@ -4,6 +4,12 @@
 
 package org.chromium.chrome.browser.preferences;
 
+import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.action.ViewActions.click;
+import static android.support.test.espresso.assertion.ViewAssertions.matches;
+import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
+
 import android.app.Activity;
 import android.app.Instrumentation;
 import android.content.Context;
@@ -26,6 +32,7 @@
 import org.chromium.base.test.util.Feature;
 import org.chromium.base.test.util.FlakyTest;
 import org.chromium.base.test.util.RetryOnFailure;
+import org.chromium.chrome.R;
 import org.chromium.chrome.browser.accessibility.FontSizePrefs;
 import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
 import org.chromium.chrome.browser.preferences.website.ContentSettingValues;
@@ -366,6 +373,32 @@
         assertFontSizePrefs(true, fontSmallerThanThreshold);
     }
 
+    @Test
+    @SmallTest
+    @Policies.Add({ @Policies.Item(key = "PasswordManagerEnabled", string = "false") })
+    public void testSavePasswordsPreferences_ManagedAndDisabled() throws ExecutionException {
+        ThreadUtils.runOnUiThreadBlocking(() -> {
+            try {
+                ChromeBrowserInitializer.getInstance().handleSynchronousStartup();
+            } catch (ProcessInitException e) {
+                Assert.fail("Unable to initialize process: " + e);
+            }
+        });
+
+        CriteriaHelper.pollUiThread(new Criteria() {
+            @Override
+            public boolean isSatisfied() {
+                return PrefServiceBridge.getInstance().isRememberPasswordsManaged();
+            }
+        });
+
+        PreferencesTest.startPreferences(
+                InstrumentationRegistry.getInstrumentation(), MainPreferences.class.getName());
+
+        onView(withText(R.string.prefs_saved_passwords_title)).perform(click());
+        onView(withText(R.string.prefs_saved_passwords)).check(matches(isDisplayed()));
+    }
+
     private void assertFontSizePrefs(final boolean expectedForceEnableZoom,
             final float expectedFontScale) {
         final Context targetContext = InstrumentationRegistry.getTargetContext();