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

Side by Side Diff: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskGcmTaskService.java

Issue 2779753002: [Android] Adding scheduling through GcmNetworkManager (Closed)
Patch Set: Adding junit tests to buildbots Created 3 years, 8 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
(Empty)
1 // Copyright 2017 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.components.background_task_scheduler;
6
7 import com.google.android.gms.gcm.GcmNetworkManager;
8 import com.google.android.gms.gcm.GcmTaskService;
9 import com.google.android.gms.gcm.TaskParams;
10
11 import org.chromium.base.ContextUtils;
12 import org.chromium.base.Log;
13 import org.chromium.base.ThreadUtils;
14
15 import java.util.concurrent.CountDownLatch;
16 import java.util.concurrent.TimeUnit;
17
18 /** Delegates calls out to various tasks that need to run in the background. */
19 public class BackgroundTaskGcmTaskService extends GcmTaskService {
20 private static final String TAG = "BkgrdTaskGcmTS";
21
22 /** Class that waits for the processing to be done. */
23 private static class Waiter {
24 // Wakelock is only held for 3 minutes by default for GcmTaskService.
25 private static final long MAX_TIMEOUT_SECONDS = 179;
26 private final CountDownLatch mLatch;
27 private long mWaiterTimeoutSeconds;
28 private boolean mIsRescheduleNeeded;
29 private boolean mHasTaskTimedOut;
30
31 public Waiter(long waiterTimeoutSeconds) {
32 mLatch = new CountDownLatch(1);
33 mWaiterTimeoutSeconds = Math.min(waiterTimeoutSeconds, MAX_TIMEOUT_S ECONDS);
34 }
35
36 /** Start waiting for the processing to finish. */
37 public void startWaiting() {
38 try {
39 mHasTaskTimedOut = !mLatch.await(mWaiterTimeoutSeconds, TimeUnit .SECONDS);
40 } catch (InterruptedException e) {
41 Log.d(TAG, "Waiter interrupted while waiting.");
42 }
43 }
44
45 /** Called to finish waiting. */
46 public void onWaitDone(boolean needsRescheduling) {
47 mIsRescheduleNeeded = needsRescheduling;
48 mLatch.countDown();
49 }
50
51 /** @return Whether last task timed out. */
52 public boolean hasTaskTimedOut() {
53 return mHasTaskTimedOut;
54 }
55
56 /** @return Whether task needs to be rescheduled. */
57 public boolean isRescheduleNeeded() {
58 return mIsRescheduleNeeded;
59 }
60 }
61
62 private static class TaskFinishedCallbackGcmTaskService
63 implements BackgroundTask.TaskFinishedCallback {
64 private final Waiter mWaiter;
65
66 public TaskFinishedCallbackGcmTaskService(Waiter waiter) {
67 mWaiter = waiter;
68 }
69
70 @Override
71 public void taskFinished(final boolean needsReschedule) {
72 // TODO(fgorski): Does this need to happen on UI thread?
fgorski 2017/04/11 19:42:10 Tommy?
nyquist 2017/04/18 21:58:28 This will set Waiter#mIsRescheduleNeeded, which is
fgorski 2017/04/20 05:54:15 I'll keep it consistent with conceptual presumptio
73 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
74 @Override
75 public void run() {
76 mWaiter.onWaitDone(needsReschedule);
77 }
78 });
79 }
80 }
81
82 @Override
83 public int onRunTask(TaskParams params) {
84 ThreadUtils.assertOnUiThread();
85 BackgroundTask backgroundTask =
86 BackgroundTaskSchedulerGcmNetworkManager.getBackgroundTaskFromTa skParams(params);
87 if (backgroundTask == null) {
88 Log.w(TAG, "Failed to start task. Could not instantiate class.");
89 return GcmNetworkManager.RESULT_FAILURE;
90 }
91
92 TaskParameters taskParams =
93 BackgroundTaskSchedulerGcmNetworkManager.getTaskParametersFromTa skParams(params);
94 Waiter waiter = new Waiter(Waiter.MAX_TIMEOUT_SECONDS);
95 boolean taskNeedsBackgroundProcessing =
96 backgroundTask.onStartTask(ContextUtils.getApplicationContext(), taskParams,
97 new TaskFinishedCallbackGcmTaskService(waiter));
98
99 if (taskNeedsBackgroundProcessing) {
nyquist 2017/04/18 21:58:28 I did try looking at making this less indented by:
fgorski 2017/04/20 05:54:15 I split the big if into 3 statements. Let me know
100 waiter.startWaiting();
nyquist 2017/04/18 21:58:28 I can't see from the documentation at https://deve
fgorski 2017/04/20 05:54:15 I'll test it and will get back to you. Perhaps my
nyquist 2017/04/25 20:44:15 Did you figure out which thread this runs on?
fgorski 2017/04/26 17:22:35 Done. Running on blocking ui thread now.
101 if (waiter.isRescheduleNeeded()
102 || (waiter.hasTaskTimedOut()
103 && backgroundTask.onStopTask(
104 ContextUtils.getApplicationContext(), taskParams))) {
105 return GcmNetworkManager.RESULT_RESCHEDULE;
106 }
107 }
108
109 return GcmNetworkManager.RESULT_SUCCESS;
110 }
111
112 @Override
113 public void onInitializeTasks() {}
114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698