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.Log; |
| 11 import org.chromium.chrome.browser.profiles.Profile; |
| 12 |
| 13 import java.util.concurrent.CountDownLatch; |
| 14 |
| 15 /** |
| 16 * Handles servicing of background offlining requests coming via the GCMNetworkM
anager. |
| 17 */ |
| 18 public class BackgroundTask implements BackgroundSchedulerBridge.ProcessingDoneC
allback { |
| 19 private static final String TAG = "BackgroundTask"; |
| 20 /** |
| 21 * Triggers processing of background offlining requests. This is called whe
n |
| 22 * system conditions are appropriate for background offlining, typically fro
m the |
| 23 * GcmTaskService onRunTask() method. In response, we will start the |
| 24 * task processing by passing the call along to the C++ RequestCoordinator. |
| 25 * |
| 26 * @returns true for success |
| 27 */ |
| 28 public boolean processBackgroundReqeusts(Context context, Bundle bundle) { |
| 29 mProcessingDoneLatch = new CountDownLatch(1); |
| 30 |
| 31 // TODO(petewil): Decode the TriggerConditions from the bundle. |
| 32 |
| 33 // TODO(petewil): We only have a context. Is this a reasonable way to g
et a profile? |
| 34 Profile profile = Profile.getLastUsedProfile(); |
| 35 if (profile.isOffTheRecord()) { |
| 36 profile = profile.getOriginalProfile(); |
| 37 } |
| 38 |
| 39 // Pass the activation on to the bridge to the C++ RequestCoordinator. |
| 40 BackgroundSchedulerBridge bridge = |
| 41 BackgroundSchedulerBridge.getForProfile(profile, context); |
| 42 bridge.startProcessing(context, this); |
| 43 |
| 44 // Gather UMA data to measure how often the user's machine is amenable t
o background |
| 45 // loading when we wake to do a task. |
| 46 OfflinePageUtils.recordWakeupUMA(context); |
| 47 |
| 48 // Wait for callback async completion before returning. This is importa
nt so |
| 49 // we don't start the next GCM task until we are done with this one. It
also |
| 50 // rate limits our pre-render calls, and holds the wake lock for us. |
| 51 try { |
| 52 // TODO(petewil): Add a timeout here in case pre-rendering hangs. |
| 53 mProcessingDoneLatch.await(); |
| 54 } catch (InterruptedException e) { |
| 55 Log.w(TAG, "IncomingTask interrupted, returning to GcmNetworkManager
"); |
| 56 return false; |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
| 61 /** |
| 62 * Callback function which indicates completion of background work. |
| 63 * @param result - TODO(petewil): What is this for again? What does true me
an? |
| 64 */ |
| 65 public void onProcessingDone(boolean result) { |
| 66 mProcessingDoneLatch.countDown(); |
| 67 } |
| 68 |
| 69 private CountDownLatch mProcessingDoneLatch; |
| 70 } |
OLD | NEW |