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

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

Issue 2697493002: [Offline pages] Implementation of OfflineBackgroundTask using background_task_scheduler (Closed)
Patch Set: Created 3 years, 10 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.content.Context;
8 import android.os.Bundle; 8 import android.os.Bundle;
9 import android.os.PersistableBundle;
9 10
10 import org.chromium.base.ApplicationStatus; 11 import org.chromium.base.ApplicationStatus;
11 import org.chromium.base.Callback; 12 import org.chromium.base.Callback;
12 import org.chromium.base.Log; 13 import org.chromium.base.Log;
13 import org.chromium.base.SysUtils; 14 import org.chromium.base.SysUtils;
14 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter; 15 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter;
15 import org.chromium.chrome.browser.offlinepages.interfaces.BackgroundSchedulerPr ocessor; 16 import org.chromium.chrome.browser.offlinepages.interfaces.BackgroundSchedulerPr ocessor;
16 17
17 /** 18 /**
18 * Handles servicing of background offlining requests coming via the GcmNetworkM anager. 19 * Handles servicing of background offlining requests coming via the GcmNetworkM anager.
(...skipping 12 matching lines...) Expand all
31 * Triggers processing of background offlining requests. This is called whe n 32 * Triggers processing of background offlining requests. This is called whe n
32 * system conditions are appropriate for background offlining, typically fro m the 33 * system conditions are appropriate for background offlining, typically fro m the
33 * GcmTaskService onRunTask() method. In response, we will start the 34 * GcmTaskService onRunTask() method. In response, we will start the
34 * task processing by passing the call along to the C++ RequestCoordinator. 35 * task processing by passing the call along to the C++ RequestCoordinator.
35 * Also starts UMA collection. 36 * Also starts UMA collection.
36 * 37 *
37 * @returns true for success 38 * @returns true for success
38 */ 39 */
39 public void startBackgroundRequests( 40 public void startBackgroundRequests(
40 Context context, Bundle bundle, final ChromeBackgroundServiceWaiter waiter) { 41 Context context, Bundle bundle, final ChromeBackgroundServiceWaiter waiter) {
42 TriggerConditions triggerConditions =
43 TaskExtrasPacker.unpackTriggerConditionsFromBundle(bundle);
44 // Set up backup scheduled task in case processing is killed before Requ estCoordinator
45 // has a chance to reschedule base on remaining work.
46 BackgroundScheduler.getInstance(context).scheduleBackup(
47 triggerConditions, DEFER_START_SECONDS);
48 long taskScheduledTimeMillis = TaskExtrasPacker.unpackTimeFromBundle(bun dle);
41 // Complete the wait if background request processing was not started. 49 // Complete the wait if background request processing was not started.
42 // If background processing was started, completion is going to be handl ed by callback. 50 // If background processing was started, completion is going to be handl ed by callback.
43 if (!startBackgroundRequestsImpl(context, bundle, waiter)) { 51 if (!startBackgroundRequestsImpl(
52 context, triggerConditions, taskScheduledTimeMillis, createC allback(waiter))) {
44 waiter.onWaitDone(); 53 waiter.onWaitDone();
45 } 54 }
46 } 55 }
47 56
57 /** TODO(fgorski): add documentation
Pete Williamson 2017/02/21 18:43:43 Let's add the doc now instead of later. Why do we
58 * When result is true we are going to be doing work...
59 */
60 public boolean startBackgroundRequests(
61 Context context, PersistableBundle bundle, Callback<Boolean> callbac k) {
62 // TODO(fgorski): I may not need the backup job... which is handled by t he BGJobScheduler
63 // already
64 TriggerConditions conditions = TaskExtrasPacker.unpackTriggerConditionsF romBundle(bundle);
65 long taskScheduledTimeMillis = TaskExtrasPacker.unpackTimeFromBundle(bun dle);
66 return startBackgroundRequestsImpl(context, conditions, taskScheduledTim eMillis, callback);
67 }
68
48 /** 69 /**
49 * Triggers processing of background offlining requests. This is called whe n 70 * Triggers processing of background offlining requests. This is called whe n
50 * system conditions are appropriate for background offlining, typically fro m the 71 * system conditions are appropriate for background offlining, typically fro m the
51 * GcmTaskService onRunTask() method. In response, we will start the 72 * GcmTaskService onRunTask() method. In response, we will start the
52 * task processing by passing the call along to the C++ RequestCoordinator. 73 * task processing by passing the call along to the C++ RequestCoordinator.
53 * Also starts UMA collection. 74 * Also starts UMA collection.
54 * 75 *
55 * @returns true for success 76 * @returns Whether processing will be carried out and completion will be in dicated through a
77 * callback.
56 */ 78 */
57 private boolean startBackgroundRequestsImpl( 79 private boolean startBackgroundRequestsImpl(Context context,
58 Context context, Bundle bundle, final ChromeBackgroundServiceWaiter waiter) { 80 TriggerConditions triggerConditions, long taskScheduledTimeMillis,
59 // Set up backup scheduled task in case processing is killed before Requ estCoordinator 81 Callback<Boolean> callback) {
60 // has a chance to reschedule base on remaining work.
61 TriggerConditions previousTriggerConditions =
62 TaskExtrasPacker.unpackTriggerConditionsFromBundle(bundle);
63 BackgroundScheduler.getInstance(context).scheduleBackup(
64 previousTriggerConditions, DEFER_START_SECONDS);
65
66 DeviceConditions currentConditions = OfflinePageUtils.getDeviceCondition s(context); 82 DeviceConditions currentConditions = OfflinePageUtils.getDeviceCondition s(context);
67 if (!currentConditions.isPowerConnected() 83 if (!currentConditions.isPowerConnected()
68 && currentConditions.getBatteryPercentage() 84 && currentConditions.getBatteryPercentage()
69 < previousTriggerConditions.getMinimumBatteryPercentage( )) { 85 < triggerConditions.getMinimumBatteryPercentage()) {
70 Log.d(TAG, "Battery percentage is lower than minimum to start proces sing"); 86 Log.d(TAG, "Battery percentage is lower than minimum to start proces sing");
71 return false; 87 return false;
72 } 88 }
73 89
74 if (SysUtils.isLowEndDevice() && ApplicationStatus.hasVisibleActivities( )) { 90 if (SysUtils.isLowEndDevice() && ApplicationStatus.hasVisibleActivities( )) {
75 Log.d(TAG, "Application visible on low-end device so deferring backg round processing"); 91 Log.d(TAG, "Application visible on low-end device so deferring backg round processing");
76 return false; 92 return false;
77 } 93 }
78 94
79 // Gather UMA data to measure how often the user's machine is amenable t o background 95 // Gather UMA data to measure how often the user's machine is amenable t o background
80 // loading when we wake to do a task. 96 // loading when we wake to do a task.
81 long taskScheduledTimeMillis = TaskExtrasPacker.unpackTimeFromBundle(bun dle);
82 OfflinePageUtils.recordWakeupUMA(context, taskScheduledTimeMillis); 97 OfflinePageUtils.recordWakeupUMA(context, taskScheduledTimeMillis);
83 98
84 return mBridge.startScheduledProcessing(currentConditions, createCallbac k(waiter)); 99 return mBridge.startScheduledProcessing(currentConditions, callback);
85 } 100 }
86 101
87 private Callback<Boolean> createCallback(final ChromeBackgroundServiceWaiter waiter) { 102 private Callback<Boolean> createCallback(final ChromeBackgroundServiceWaiter waiter) {
88 return new Callback<Boolean>() { 103 return new Callback<Boolean>() {
89 /** Callback releasing the wakelock once background work concludes. */ 104 /** Callback releasing the wakelock once background work concludes. */
90 @Override 105 @Override
91 public void onResult(Boolean result) { 106 public void onResult(Boolean result) {
92 Log.d(TAG, "onProcessingDone"); 107 Log.d(TAG, "onProcessingDone");
93 // Release the wake lock. 108 // Release the wake lock.
94 waiter.onWaitDone(); 109 waiter.onWaitDone();
95 } 110 }
96 }; 111 };
97 } 112 }
98 } 113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698