| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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.chrome.browser; | |
| 6 | |
| 7 import org.chromium.base.Log; | |
| 8 | |
| 9 import java.util.concurrent.CountDownLatch; | |
| 10 import java.util.concurrent.TimeUnit; | |
| 11 | |
| 12 /** | |
| 13 * Class to wait for a task to complete before releasing control back to the cal
ler. | |
| 14 */ | |
| 15 public class ChromeBackgroundServiceWaiter { | |
| 16 private static final String TAG = "CBSWaiter"; | |
| 17 /** Synchronization object to control the wait. */ | |
| 18 private final CountDownLatch mLatch; | |
| 19 /** How long to wait for before giving up */ | |
| 20 private int mWakelockTimeoutSeconds; | |
| 21 | |
| 22 public ChromeBackgroundServiceWaiter(int wakelockTimeoutSeconds) { | |
| 23 mWakelockTimeoutSeconds = wakelockTimeoutSeconds; | |
| 24 mLatch = new CountDownLatch(1); | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Wait, blocking the current thread until another thread calls onWaitDone. | |
| 29 */ | |
| 30 public void startWaiting() { | |
| 31 try { | |
| 32 boolean waitSucceeded = mLatch.await(mWakelockTimeoutSeconds, TimeUn
it.SECONDS); | |
| 33 if (!waitSucceeded) { | |
| 34 Log.d(TAG, "waiting for latch timed out"); | |
| 35 } | |
| 36 } catch (InterruptedException e) { | |
| 37 Log.d(TAG, "ChromeBackgroundServiceWaiter interrupted while holding
wake lock. " + e); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 | |
| 42 /** | |
| 43 * Called when the wait is complete. | |
| 44 */ | |
| 45 public void onWaitDone() { | |
| 46 // Release the waited thread to return to the caller, and thus release t
he wake lock held on | |
| 47 // behalf of the Owner. | |
| 48 mLatch.countDown(); | |
| 49 } | |
| 50 } | |
| OLD | NEW |