| 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();
|
| + }
|
| +}
|
|
|