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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundServiceWaiter.java

Issue 2066593003: Add a general purpose facility for letting background tasks wait until (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: logical merge fix Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundServiceWaiter.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundServiceWaiter.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundServiceWaiter.java
new file mode 100644
index 0000000000000000000000000000000000000000..5e1544f45e7abfdb65a09e1a3a551aad0db0c935
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundServiceWaiter.java
@@ -0,0 +1,50 @@
+// 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;
+
+import org.chromium.base.Log;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Class to wait for a task to complete before releasing control back to the caller.
+ */
+public class ChromeBackgroundServiceWaiter {
+ private static final String TAG = "CBSWaiter";
+ /** Synchronization object to control the wait. */
+ private final CountDownLatch mLatch;
+ /** How long to wait for before giving up */
+ private int mWakelockTimeoutSeconds;
+
+ public ChromeBackgroundServiceWaiter(int wakelockTimeoutSeconds) {
+ mWakelockTimeoutSeconds = wakelockTimeoutSeconds;
+ mLatch = new CountDownLatch(1);
+ }
+
+ /**
+ * Wait, blocking the current thread until another thread calls onWaitDone.
+ */
+ public void startWaiting() {
+ try {
+ boolean waitSucceeded = mLatch.await(mWakelockTimeoutSeconds, TimeUnit.SECONDS);
+ if (!waitSucceeded) {
+ Log.d(TAG, "waiting for latch timed out");
+ }
+ } catch (InterruptedException e) {
+ Log.d(TAG, "ChromeBackgroundServiceWaiter interrupted while holding wake lock. " + e);
+ }
+ }
+
+
+ /**
+ * Called when the wait is complete.
+ */
+ public void onWaitDone() {
+ // Release the waited thread to return to the caller, and thus release the wake lock held on
+ // behalf of the Owner.
+ mLatch.countDown();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698