Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundTask.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundTask.java b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundTask.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2d95a4f9c1a5bff9da94b7425c9d6bb5d7bc9e95 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundTask.java |
| @@ -0,0 +1,64 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.offlinepages; |
| + |
| +import android.content.Context; |
| +import android.os.Bundle; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.base.ThreadUtils; |
| + |
| +import java.util.concurrent.CountDownLatch; |
| + |
| +/** |
| + * Handles servicing of background offlining requests coming via the GcmNetworkManager. |
| + */ |
| +public class BackgroundTask implements BackgroundSchedulerBridge.ProcessingDoneCallback { |
| + private static final String TAG = "BackgroundTask"; |
| + /** |
| + * Triggers processing of background offlining requests. This is called when |
| + * system conditions are appropriate for background offlining, typically from the |
| + * GcmTaskService onRunTask() method. In response, we will start the |
| + * task processing by passing the call along to the C++ RequestCoordinator. |
| + * |
| + * @returns true for success |
| + */ |
| + public boolean processBackgroundRequests(Context context, Bundle bundle) { |
| + mProcessingDoneLatch = new CountDownLatch(1); |
| + // GCM Network Manager promises to call us on a fresh thread, not the UI thread. |
| + assert (!ThreadUtils.runningOnUiThread()); |
|
dougarnett
2016/05/24 16:27:58
Will assert as ChromeBackgroundService is currentl
dewittj
2016/05/24 16:56:19
also nit: this should be the first line of the fun
Pete Williamson
2016/05/24 18:49:31
Noted, I'll discuss the resolution with you (possi
Pete Williamson
2016/05/24 18:49:31
Done.
|
| + |
| + // TODO(petewil): Decode the TriggerConditions from the bundle. |
| + |
| + // Pass the activation on to the bridge to the C++ RequestCoordinator. |
| + BackgroundSchedulerBridge.startProcessing(context, this); |
| + |
| + // Gather UMA data to measure how often the user's machine is amenable to background |
| + // loading when we wake to do a task. |
| + OfflinePageUtils.recordWakeupUMA(context); |
| + |
| + // Wait for callback async completion before returning. This is important so |
| + // we don't start the next GCM task until we are done with this one. It also |
| + // rate limits our pre-render calls, and holds the wake lock for us. |
| + try { |
| + // TODO(petewil): Add a timeout here in case pre-rendering hangs. |
| + mProcessingDoneLatch.await(); |
|
dewittj
2016/05/24 16:56:19
why not just use the two-argument await with a tim
Pete Williamson
2016/05/24 18:49:31
Done.
|
| + } catch (InterruptedException e) { |
| + Log.w(TAG, "IncomingTask interrupted, returning to GcmNetworkManager"); |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + /** |
| + * Callback function which indicates completion of background work. |
| + * @param result - TODO(petewil): What is this for again? What does true mean? |
| + */ |
| + public void onProcessingDone(boolean result) { |
| + mProcessingDoneLatch.countDown(); |
| + } |
| + |
| + private CountDownLatch mProcessingDoneLatch; |
| +} |