Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.os.Bundle; |
| 8 import android.os.Build; | 8 |
| 9 import org.chromium.base.ContextUtils; | |
| 10 import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler Factory; | |
| 11 import org.chromium.components.background_task_scheduler.TaskIds; | |
| 12 import org.chromium.components.background_task_scheduler.TaskInfo; | |
| 9 | 13 |
| 10 import java.util.concurrent.TimeUnit; | 14 import java.util.concurrent.TimeUnit; |
| 11 | 15 |
| 12 /** | 16 /** |
| 13 * The background scheduler class is for setting GCM Network Manager tasks. | 17 * Class responsible for scheduling and canceling offline page related backgroun d tasks. |
| 14 */ | 18 */ |
| 15 public abstract class BackgroundScheduler { | 19 public class BackgroundScheduler { |
| 16 private static final long ONE_WEEK_IN_SECONDS = TimeUnit.DAYS.toSeconds(7); | 20 static final long ONE_WEEK_IN_MILLISECONDS = TimeUnit.DAYS.toMillis(7); |
| 17 private static final long FIVE_MINUTES_IN_SECONDS = TimeUnit.MINUTES.toSecon ds(5); | 21 static final long FIVE_MINUTES_IN_MILLISECONDS = TimeUnit.MINUTES.toSeconds( 5); |
| 18 private static final long NO_DELAY = 0; | 22 static final long NO_DELAY = 0; |
| 19 private static final boolean OVERWRITE = true; | 23 private static final boolean OVERWRITE = true; |
| 20 | 24 |
| 21 /** | 25 /** Provides an instance of BackgroundScheduler for given context and curren t API level. */ |
| 22 * Context used by the scheduler to access services. Extracted to a field, t o clean up method | 26 public static BackgroundScheduler getInstance() { |
|
nyquist
2017/05/30 18:45:26
Nit: I know this is just refactored code, and not
| |
| 23 * signatures. | 27 return new BackgroundScheduler(); |
| 24 */ | |
| 25 private Context mContext; | |
| 26 | |
| 27 /** | |
| 28 * Provides an instance of BackgroundScheduler for given context and current API level. | |
| 29 * <p> | |
| 30 * Warning: Don't cache the returned value, as it is bound to {@code context }. Consumers should | |
| 31 * simply get an instance every time. | |
| 32 * @return An instance of BackgroundScheduler. | |
| 33 */ | |
| 34 public static BackgroundScheduler getInstance(Context context) { | |
| 35 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
| 36 return new BackgroundJobScheduler(context); | |
| 37 } else { | |
| 38 return new BackgroundGcmScheduler(context); | |
| 39 } | |
| 40 } | 28 } |
| 41 | 29 |
| 42 protected BackgroundScheduler(Context context) { | 30 /** Cancels a background tasks. */ |
| 43 mContext = context; | 31 public void cancel() { |
| 32 BackgroundTaskSchedulerFactory.getScheduler().cancel( | |
| 33 ContextUtils.getApplicationContext(), TaskIds.OFFLINE_PAGES_BACK GROUND_JOB_ID); | |
| 44 } | 34 } |
| 45 | 35 |
| 46 /** Schedules a GCM Network Manager task for provided triggering conditions. */ | 36 /** Schedules a background task for provided triggering conditions. */ |
| 47 public void schedule(TriggerConditions triggerConditions) { | 37 public void schedule(TriggerConditions triggerConditions) { |
| 48 scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_SECONDS, OVERWRITE ); | 38 scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_MILLISECONDS, OVER WRITE); |
| 49 } | 39 } |
| 50 | 40 |
| 51 /** | 41 /** |
| 52 * If there is no currently scheduled task, then start a GCM Network Manager request | 42 * If there is no currently scheduled task, then start a GCM Network Manager request |
| 53 * for the given Triggering conditions but delayed to run after {@code delay StartSeconds}. | 43 * for the given Triggering conditions but delayed to run after {@code delay StartSeconds}. |
| 54 * Typically, the Request Coordinator will overwrite this task after task pr ocessing | 44 * Typically, the Request Coordinator will overwrite this task after task pr ocessing |
| 55 * and/or queue updates. This is a backup task in case processing is killed by the | 45 * and/or queue updates. This is a backup task in case processing is killed by the |
| 56 * system. | 46 * system. |
| 57 */ | 47 */ |
| 58 public void scheduleBackup(TriggerConditions triggerConditions, long delaySt artSeconds) { | 48 public void scheduleBackup(TriggerConditions triggerConditions, long delaySt artMs) { |
| 59 scheduleImpl(triggerConditions, delayStartSeconds, ONE_WEEK_IN_SECONDS, !OVERWRITE); | 49 scheduleImpl(triggerConditions, delayStartMs, ONE_WEEK_IN_MILLISECONDS, !OVERWRITE); |
| 60 } | |
| 61 | |
| 62 /** Cancel any outstanding GCM Network Manager requests. */ | |
| 63 public abstract void cancel(); | |
| 64 | |
| 65 /** | |
| 66 * For the given Triggering conditions, start a new GCM Network Manager requ est allowed | |
| 67 * to run after {@code delayStartSecs} seconds. | |
| 68 */ | |
| 69 protected abstract void scheduleImpl(TriggerConditions triggerConditions, | |
| 70 long delayStartSeconds, long executionDeadlineSeconds, boolean overw rite); | |
| 71 | |
| 72 /** @return Context used to access OS services. */ | |
| 73 protected Context getContext() { | |
| 74 return mContext; | |
| 75 } | 50 } |
| 76 | 51 |
| 77 /** | 52 /** |
| 78 * If GooglePlayServices upgrades, any outstaning tasks will be lost. | 53 * Method for rescheduling a background task for offline pages in the event of OS upgrade or |
| 79 * Set a reminder to wake up and check the task queue if an upgrade happens. | 54 * GooglePlayServices upgrade. |
| 55 * We use the least restrictive trigger conditions. A wakeup will cause the queue to be | |
| 56 * checked, and the trigger conditions will be replaced by the current trigg er conditions | |
| 57 * needed. | |
| 80 */ | 58 */ |
| 81 public void rescheduleOfflinePagesTasksOnUpgrade() { | 59 public void reschedule() { |
| 82 // We use the least restrictive trigger conditions. A wakeup will cause | |
| 83 // the queue to be checked, and the trigger conditions will be replaced by | |
| 84 // the current trigger conditions needed. | |
| 85 TriggerConditions triggerConditions = new TriggerConditions(false, 0, fa lse); | 60 TriggerConditions triggerConditions = new TriggerConditions(false, 0, fa lse); |
| 86 scheduleBackup(triggerConditions, FIVE_MINUTES_IN_SECONDS); | 61 scheduleBackup(triggerConditions, FIVE_MINUTES_IN_MILLISECONDS); |
| 62 } | |
| 63 | |
| 64 protected void scheduleImpl(TriggerConditions triggerConditions, long delayS tartMs, | |
| 65 long executionDeadlineMs, boolean overwrite) { | |
| 66 Bundle taskExtras = new Bundle(); | |
| 67 TaskExtrasPacker.packTimeInBundle(taskExtras); | |
| 68 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions); | |
| 69 | |
| 70 TaskInfo taskInfo = | |
| 71 TaskInfo.createOneOffTask(TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_I D, | |
| 72 OfflineBackgroundTask.class, delayStartMs, execu tionDeadlineMs) | |
| 73 .setRequiredNetworkType(triggerConditions.requireUnmeter edNetwork() | |
| 74 ? TaskInfo.NETWORK_TYPE_UNMETERED | |
| 75 : TaskInfo.NETWORK_TYPE_ANY) | |
| 76 .setUpdateCurrent(overwrite) | |
| 77 .setIsPersisted(true) | |
| 78 .setExtras(taskExtras) | |
| 79 .setRequiresCharging(triggerConditions.requirePowerConne cted()) | |
| 80 .build(); | |
| 81 | |
| 82 BackgroundTaskSchedulerFactory.getScheduler().schedule( | |
| 83 ContextUtils.getApplicationContext(), taskInfo); | |
| 87 } | 84 } |
| 88 } | 85 } |
| OLD | NEW |