Commit 24d0ad17 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Move a SharedPref from SnippetsLauncher to ChromePreferenceKeys

Register it in ChromePreferenceKeys and use SharedPreferencesManager
consistently instead of SharedPreferences directly.

Bug: 1022108
Change-Id: Idd37d5d5624167e836cd49c2887e748f0d966566
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2001118
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#732439}
parent 5c7cba20
......@@ -18,6 +18,8 @@ import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.ChromeBackgroundService;
import org.chromium.chrome.browser.externalauth.ExternalAuthUtils;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
/**
* The {@link SnippetsLauncher} singleton is created and owned by the C++ browser.
......@@ -34,9 +36,6 @@ public class SnippetsLauncher {
// The amount of "flex" to add around the fetching periods, as a ratio of the period.
private static final double FLEX_FACTOR = 0.1;
@VisibleForTesting
public static final String PREF_IS_SCHEDULED = "ntp_snippets.is_scheduled";
// The instance of SnippetsLauncher currently owned by a C++ SnippetsLauncherAndroid, if any.
// If it is non-null then the browser is running.
private static SnippetsLauncher sInstance;
......@@ -124,10 +123,8 @@ public class SnippetsLauncher {
Log.i(TAG, "Scheduling: " + periodWifiSeconds + " " + periodFallbackSeconds);
boolean isScheduled = periodWifiSeconds != 0 || periodFallbackSeconds != 0;
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(PREF_IS_SCHEDULED, isScheduled)
.apply();
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.NTP_SNIPPETS_IS_SCHEDULED, isScheduled);
// Google Play Services may not be up to date, if the application was not installed through
// the Play Store. In this case, scheduling the task will fail silently.
......@@ -140,7 +137,8 @@ public class SnippetsLauncher {
// Disable GCM for the remainder of this session.
mGCMEnabled = false;
ContextUtils.getAppSharedPreferences().edit().remove(PREF_IS_SCHEDULED).apply();
SharedPreferencesManager.getInstance().removeKey(
ChromePreferenceKeys.NTP_SNIPPETS_IS_SCHEDULED);
// Return false so that the failure will be logged.
return false;
}
......@@ -164,7 +162,8 @@ public class SnippetsLauncher {
public static boolean shouldNotifyOnBrowserUpgraded() {
// If there was no schedule previously, we do not need to react to upgrades.
return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IS_SCHEDULED, false);
return SharedPreferencesManager.getInstance().readBoolean(
ChromePreferenceKeys.NTP_SNIPPETS_IS_SCHEDULED, false);
}
}
......@@ -22,13 +22,14 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.chromium.base.ContextUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.task.PostTask;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.chrome.browser.background_sync.BackgroundSyncBackgroundTaskScheduler;
import org.chromium.chrome.browser.ntp.snippets.SnippetsLauncher;
import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler;
import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory;
......@@ -193,10 +194,8 @@ public class ChromeBackgroundServiceTest {
@Feature({"NTPSnippets"})
public void testNTPSnippetsRescheduleWithPrefWhenInstanceExists() {
// Set the pref indicating that fetching was scheduled before.
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true)
.apply();
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.NTP_SNIPPETS_IS_SCHEDULED, true);
startOnInitializeTasksAndVerify(
/*shouldStart=*/false, /*shouldCallOnBrowserUpgraded=*/true);
......@@ -208,10 +207,8 @@ public class ChromeBackgroundServiceTest {
public void testNTPSnippetsRescheduleAndLaunchBrowserWithPrefWhenInstanceDoesNotExist() {
deleteSnippetsLauncherInstance();
// Set the pref indicating that fetching was scheduled before.
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(SnippetsLauncher.PREF_IS_SCHEDULED, true)
.apply();
SharedPreferencesManager.getInstance().writeBoolean(
ChromePreferenceKeys.NTP_SNIPPETS_IS_SCHEDULED, true);
startOnInitializeTasksAndVerify(/*shouldStart=*/true, /*shouldCallOnBrowserUpgraded=*/true);
}
......
......@@ -425,6 +425,8 @@ public final class ChromePreferenceKeys {
public static final String LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE =
"LocaleManager_WAS_IN_SPECIAL_LOCALE";
public static final String NTP_SNIPPETS_IS_SCHEDULED = "ntp_snippets.is_scheduled";
/**
* Key to cache whether offline indicator v2 (persistent offline indicator) is enabled.
*/
......@@ -743,6 +745,7 @@ public final class ChromePreferenceKeys {
LOCALE_MANAGER_PROMO_SHOWN,
LOCALE_MANAGER_SEARCH_ENGINE_PROMO_SHOW_STATE,
LOCALE_MANAGER_WAS_IN_SPECIAL_LOCALE,
NTP_SNIPPETS_IS_SCHEDULED,
OFFLINE_INDICATOR_V2_ENABLED,
PAYMENTS_PAYMENT_COMPLETE_ONCE,
PRIVACY_ALLOW_PRERENDER_OLD,
......
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