Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundScheduler.java

Issue 2830843002: [Offline pages] Updates to background scheduling to use BTS (Closed)
Patch Set: Fixing the crash on NCN not being initialized Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 private static class LazyHolder {
22 * Context used by the scheduler to access services. Extracted to a field, t o clean up method 26 static final BackgroundScheduler INSTANCE = new BackgroundScheduler();
23 * signatures.
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 } 27 }
41 28
42 protected BackgroundScheduler(Context context) { 29 /** Provides an instance of BackgroundScheduler for given context and curren t API level. */
43 mContext = context; 30 public static BackgroundScheduler getInstance() {
31 return LazyHolder.INSTANCE;
44 } 32 }
45 33
46 /** Schedules a GCM Network Manager task for provided triggering conditions. */ 34 /** Cancels a background tasks. */
35 public void cancel() {
36 BackgroundTaskSchedulerFactory.getScheduler().cancel(
37 ContextUtils.getApplicationContext(), TaskIds.OFFLINE_PAGES_BACK GROUND_JOB_ID);
38 }
39
40 /** Schedules a background task for provided triggering conditions. */
47 public void schedule(TriggerConditions triggerConditions) { 41 public void schedule(TriggerConditions triggerConditions) {
48 scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_SECONDS, OVERWRITE ); 42 scheduleImpl(triggerConditions, NO_DELAY, ONE_WEEK_IN_MILLISECONDS, OVER WRITE);
49 } 43 }
50 44
51 /** 45 /**
52 * If there is no currently scheduled task, then start a GCM Network Manager request 46 * 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}. 47 * 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 48 * 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 49 * and/or queue updates. This is a backup task in case processing is killed by the
56 * system. 50 * system.
57 */ 51 */
58 public void scheduleBackup(TriggerConditions triggerConditions, long delaySt artSeconds) { 52 public void scheduleBackup(TriggerConditions triggerConditions, long delaySt artMs) {
59 scheduleImpl(triggerConditions, delayStartSeconds, ONE_WEEK_IN_SECONDS, !OVERWRITE); 53 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 } 54 }
76 55
77 /** 56 /**
78 * If GooglePlayServices upgrades, any outstaning tasks will be lost. 57 * 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. 58 * GooglePlayServices upgrade.
59 * We use the least restrictive trigger conditions. A wakeup will cause the queue to be
60 * checked, and the trigger conditions will be replaced by the current trigg er conditions
61 * needed.
80 */ 62 */
81 public void rescheduleOfflinePagesTasksOnUpgrade() { 63 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); 64 TriggerConditions triggerConditions = new TriggerConditions(false, 0, fa lse);
86 scheduleBackup(triggerConditions, FIVE_MINUTES_IN_SECONDS); 65 scheduleBackup(triggerConditions, FIVE_MINUTES_IN_MILLISECONDS);
66 }
67
68 protected void scheduleImpl(TriggerConditions triggerConditions, long delayS tartMs,
69 long executionDeadlineMs, boolean overwrite) {
70 Bundle taskExtras = new Bundle();
71 TaskExtrasPacker.packTimeInBundle(taskExtras);
72 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions);
73
74 TaskInfo taskInfo =
75 TaskInfo.createOneOffTask(TaskIds.OFFLINE_PAGES_BACKGROUND_JOB_I D,
76 OfflineBackgroundTask.class, delayStartMs, execu tionDeadlineMs)
77 .setRequiredNetworkType(triggerConditions.requireUnmeter edNetwork()
78 ? TaskInfo.NETWORK_TYPE_UNMETERED
79 : TaskInfo.NETWORK_TYPE_ANY)
80 .setUpdateCurrent(overwrite)
81 .setIsPersisted(true)
82 .setExtras(taskExtras)
83 .setRequiresCharging(triggerConditions.requirePowerConne cted())
84 .build();
85
86 BackgroundTaskSchedulerFactory.getScheduler().schedule(
87 ContextUtils.getApplicationContext(), taskInfo);
87 } 88 }
88 } 89 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698