| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.offlinepages; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.os.Bundle; | |
| 9 | |
| 10 import org.chromium.base.ApplicationStatus; | |
| 11 import org.chromium.base.Callback; | |
| 12 import org.chromium.base.Log; | |
| 13 import org.chromium.base.SysUtils; | |
| 14 import org.chromium.chrome.browser.ChromeBackgroundServiceWaiter; | |
| 15 import org.chromium.chrome.browser.offlinepages.interfaces.BackgroundSchedulerPr
ocessor; | |
| 16 | |
| 17 /** | |
| 18 * Handles servicing of background offlining requests coming via the GcmNetworkM
anager. | |
| 19 */ | |
| 20 public class BackgroundOfflinerTask { | |
| 21 private static final String TAG = "BGOfflinerTask"; | |
| 22 private static final long DEFER_START_SECONDS = 5 * 60; | |
| 23 | |
| 24 private final BackgroundSchedulerProcessor mBridge; | |
| 25 | |
| 26 public BackgroundOfflinerTask(BackgroundSchedulerProcessor bridge) { | |
| 27 mBridge = bridge; | |
| 28 } | |
| 29 | |
| 30 /** | |
| 31 * Triggers processing of background offlining requests. This is called whe
n | |
| 32 * system conditions are appropriate for background offlining, typically fro
m the | |
| 33 * GcmTaskService onRunTask() method. In response, we will start the | |
| 34 * task processing by passing the call along to the C++ RequestCoordinator. | |
| 35 * Also starts UMA collection. | |
| 36 * | |
| 37 * @returns true for success | |
| 38 */ | |
| 39 public void startBackgroundRequests( | |
| 40 Context context, Bundle bundle, final ChromeBackgroundServiceWaiter
waiter) { | |
| 41 // Set up backup scheduled task in case processing is killed before Requ
estCoordinator | |
| 42 // has a chance to reschedule base on remaining work. | |
| 43 BackgroundScheduler.getInstance(context).scheduleBackup( | |
| 44 TaskExtrasPacker.unpackTriggerConditionsFromBundle(bundle), DEFE
R_START_SECONDS); | |
| 45 // Complete the wait if background request processing was not started. | |
| 46 // If background processing was started, completion is going to be handl
ed by callback. | |
| 47 if (!startBackgroundRequestsImpl(mBridge, context, bundle, createCallbac
k(waiter))) { | |
| 48 waiter.onWaitDone(); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * Triggers processing of background offlining requests. This is called whe
n | |
| 54 * system conditions are appropriate for background offlining, typically fro
m the | |
| 55 * GcmTaskService onRunTask() method. In response, we will start the | |
| 56 * task processing by passing the call along to the C++ RequestCoordinator. | |
| 57 * Also starts UMA collection. | |
| 58 * | |
| 59 * @returns Whether processing will be carried out and completion will be in
dicated through a | |
| 60 * callback. | |
| 61 */ | |
| 62 static boolean startBackgroundRequestsImpl(BackgroundSchedulerProcessor brid
ge, Context context, | |
| 63 Bundle taskExtras, Callback<Boolean> callback) { | |
| 64 TriggerConditions triggerConditions = | |
| 65 TaskExtrasPacker.unpackTriggerConditionsFromBundle(taskExtras); | |
| 66 DeviceConditions currentConditions = DeviceConditions.getCurrentConditio
ns(context); | |
| 67 if (!currentConditions.isPowerConnected() | |
| 68 && currentConditions.getBatteryPercentage() | |
| 69 < triggerConditions.getMinimumBatteryPercentage()) { | |
| 70 Log.d(TAG, "Battery percentage is lower than minimum to start proces
sing"); | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 if (SysUtils.isLowEndDevice() && ApplicationStatus.hasVisibleActivities(
)) { | |
| 75 Log.d(TAG, "Application visible on low-end device so deferring backg
round processing"); | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 // 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. | |
| 81 long taskScheduledTimeMillis = TaskExtrasPacker.unpackTimeFromBundle(tas
kExtras); | |
| 82 OfflinePageUtils.recordWakeupUMA(context, taskScheduledTimeMillis); | |
| 83 | |
| 84 return bridge.startScheduledProcessing(currentConditions, callback); | |
| 85 } | |
| 86 | |
| 87 private Callback<Boolean> createCallback(final ChromeBackgroundServiceWaiter
waiter) { | |
| 88 return new Callback<Boolean>() { | |
| 89 /** Callback releasing the wakelock once background work concludes.
*/ | |
| 90 @Override | |
| 91 public void onResult(Boolean result) { | |
| 92 Log.d(TAG, "onResult"); | |
| 93 // Release the wake lock. | |
| 94 waiter.onWaitDone(); | |
| 95 } | |
| 96 }; | |
| 97 } | |
| 98 } | |
| OLD | NEW |