Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java |
| index 38f5eeb4207d97f925e5173bfe6e18c0af568290..2404e26195384aade3ac5c985f61b7a7877f3bac 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java |
| @@ -4,48 +4,38 @@ |
| package org.chromium.chrome.browser.offlinepages; |
| -import android.content.Context; |
| -import android.os.Build; |
| +import android.os.Bundle; |
| + |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.components.background_task_scheduler.BackgroundTaskSchedulerFactory; |
| +import org.chromium.components.background_task_scheduler.TaskIds; |
| +import org.chromium.components.background_task_scheduler.TaskInfo; |
| import java.util.concurrent.TimeUnit; |
| /** |
| - * The background scheduler class is for setting GCM Network Manager tasks. |
| + * Class responsible for scheduling and canceling offline page related background tasks. |
| */ |
| -public abstract class BackgroundScheduler { |
| - private static final long ONE_WEEK_IN_SECONDS = TimeUnit.DAYS.toSeconds(7); |
| - private static final long FIVE_MINUTES_IN_SECONDS = TimeUnit.MINUTES.toSeconds(5); |
| - private static final long NO_DELAY = 0; |
| +public class BackgroundScheduler { |
| + static final long ONE_WEEK_IN_MILLISECONDS = TimeUnit.DAYS.toMillis(7); |
| + static final long FIVE_MINUTES_IN_MILLISECONDS = TimeUnit.MINUTES.toSeconds(5); |
| + static final long NO_DELAY = 0; |
| private static final boolean OVERWRITE = true; |
| - /** |
| - * Context used by the scheduler to access services. Extracted to a field, to clean up method |
| - * signatures. |
| - */ |
| - private Context mContext; |
| - |
| - /** |
| - * Provides an instance of BackgroundScheduler for given context and current API level. |
| - * <p> |
| - * Warning: Don't cache the returned value, as it is bound to {@code context}. Consumers should |
| - * simply get an instance every time. |
| - * @return An instance of BackgroundScheduler. |
| - */ |
| - public static BackgroundScheduler getInstance(Context context) { |
| - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| - return new BackgroundJobScheduler(context); |
| - } else { |
| - return new BackgroundGcmScheduler(context); |
| - } |
| + /** Provides an instance of BackgroundScheduler for given context and current API level. */ |
| + public static BackgroundScheduler getInstance() { |
|
nyquist
2017/05/30 18:45:26
Nit: I know this is just refactored code, and not
|
| + return new BackgroundScheduler(); |
| } |
| - protected BackgroundScheduler(Context context) { |
| - mContext = context; |
| + /** Cancels a background tasks. */ |
| + public void cancel() { |
| + BackgroundTaskSchedulerFactory.getScheduler().cancel( |
| + ContextUtils.getApplicationContext(), TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_ID); |
| } |
| - /** Schedules a GCM Network Manager task for provided triggering conditions. */ |
| + /** Schedules a background task for provided triggering conditions. */ |
| public void schedule(TriggerConditions triggerConditions) { |
| - scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_SECONDS, OVERWRITE); |
| + scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_MILLISECONDS, OVERWRITE); |
| } |
| /** |
| @@ -55,34 +45,41 @@ public abstract class BackgroundScheduler { |
| * and/or queue updates. This is a backup task in case processing is killed by the |
| * system. |
| */ |
| - public void scheduleBackup(TriggerConditions triggerConditions, long delayStartSeconds) { |
| - scheduleImpl(triggerConditions, delayStartSeconds, ONE_WEEK_IN_SECONDS, !OVERWRITE); |
| + public void scheduleBackup(TriggerConditions triggerConditions, long delayStartMs) { |
| + scheduleImpl(triggerConditions, delayStartMs, ONE_WEEK_IN_MILLISECONDS, !OVERWRITE); |
| } |
| - /** Cancel any outstanding GCM Network Manager requests. */ |
| - public abstract void cancel(); |
| - |
| /** |
| - * For the given Triggering conditions, start a new GCM Network Manager request allowed |
| - * to run after {@code delayStartSecs} seconds. |
| + * Method for rescheduling a background task for offline pages in the event of OS upgrade or |
| + * GooglePlayServices upgrade. |
| + * We use the least restrictive trigger conditions. A wakeup will cause the queue to be |
| + * checked, and the trigger conditions will be replaced by the current trigger conditions |
| + * needed. |
| */ |
| - protected abstract void scheduleImpl(TriggerConditions triggerConditions, |
| - long delayStartSeconds, long executionDeadlineSeconds, boolean overwrite); |
| - |
| - /** @return Context used to access OS services. */ |
| - protected Context getContext() { |
| - return mContext; |
| + public void reschedule() { |
| + TriggerConditions triggerConditions = new TriggerConditions(false, 0, false); |
| + scheduleBackup(triggerConditions, FIVE_MINUTES_IN_MILLISECONDS); |
| } |
| - /** |
| - * If GooglePlayServices upgrades, any outstaning tasks will be lost. |
| - * Set a reminder to wake up and check the task queue if an upgrade happens. |
| - */ |
| - public void rescheduleOfflinePagesTasksOnUpgrade() { |
| - // We use the least restrictive trigger conditions. A wakeup will cause |
| - // the queue to be checked, and the trigger conditions will be replaced by |
| - // the current trigger conditions needed. |
| - TriggerConditions triggerConditions = new TriggerConditions(false, 0, false); |
| - scheduleBackup(triggerConditions, FIVE_MINUTES_IN_SECONDS); |
| + protected void scheduleImpl(TriggerConditions triggerConditions, long delayStartMs, |
| + long executionDeadlineMs, boolean overwrite) { |
| + Bundle taskExtras = new Bundle(); |
| + TaskExtrasPacker.packTimeInBundle(taskExtras); |
| + TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerConditions); |
| + |
| + TaskInfo taskInfo = |
| + TaskInfo.createOneOffTask(TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_ID, |
| + OfflineBackgroundTask.class, delayStartMs, executionDeadlineMs) |
| + .setRequiredNetworkType(triggerConditions.requireUnmeteredNetwork() |
| + ? TaskInfo.NETWORK_TYPE_UNMETERED |
| + : TaskInfo.NETWORK_TYPE_ANY) |
| + .setUpdateCurrent(overwrite) |
| + .setIsPersisted(true) |
| + .setExtras(taskExtras) |
| + .setRequiresCharging(triggerConditions.requirePowerConnected()) |
| + .build(); |
| + |
| + BackgroundTaskSchedulerFactory.getScheduler().schedule( |
| + ContextUtils.getApplicationContext(), taskInfo); |
| } |
| } |