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..83f0a77dc3f4ba6565a92240d7e0930b20eb60cb |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundTask.java |
@@ -0,0 +1,70 @@ |
+// 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.chrome.browser.profiles.Profile; |
+ |
+import java.util.concurrent.CountDownLatch; |
+ |
+/** |
+ * Handles servicing of background offlining requests coming via the GCMNetworkManager. |
dougarnett
2016/05/20 22:17:32
nit: GcmNetworkManager is the actual capitalizatio
Pete Williamson
2016/05/23 19:36:31
Done.
|
+ */ |
+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 processBackgroundReqeusts(Context context, Bundle bundle) { |
dougarnett
2016/05/20 22:17:32
spelling of Requests
Pete Williamson
2016/05/23 19:36:31
Done.
|
+ mProcessingDoneLatch = new CountDownLatch(1); |
+ |
+ // TODO(petewil): Decode the TriggerConditions from the bundle. |
+ |
+ // TODO(petewil): We only have a context. Is this a reasonable way to get a profile? |
+ Profile profile = Profile.getLastUsedProfile(); |
+ if (profile.isOffTheRecord()) { |
+ profile = profile.getOriginalProfile(); |
+ } |
+ |
+ // Make sure the bridge has a pointer to the proper profile and context. |
+ BackgroundSchedulerBridge.getForProfile(profile, context); |
+ // 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/20 21:52:31
Based on the call flow, this will block the UI thr
dougarnett
2016/05/20 22:30:18
Nice catch!
We might want a thread check enteri
Pete Williamson
2016/05/23 19:36:31
The intent is for this to block the incoming threa
Pete Williamson
2016/05/23 19:36:31
Check addded.
dougarnett
2016/05/24 16:27:58
Added comment to the runOnUiThread call to make th
dewittj
2016/05/24 16:56:19
Can you point me to where this promise is made? I
|
+ } 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; |
+} |