OLD | NEW |
(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.content.browser.androidoverlay; |
| 6 |
| 7 import android.os.Handler; |
| 8 import android.view.Surface; |
| 9 |
| 10 import java.util.concurrent.Semaphore; |
| 11 |
| 12 /** |
| 13 * DialogOverlayCore::Host implementation that transfers messages to the thread
on which it was |
| 14 * constructed. Due to threading concerns, waitForCleanup is not forwarded. |
| 15 */ |
| 16 class ThreadHoppingHost implements DialogOverlayCore.Host { |
| 17 // Handler for the host we're proxying to. Typically Browser::UI. |
| 18 private Handler mHandler; |
| 19 |
| 20 // Host impl that we're proxying to. |
| 21 private final DialogOverlayCore.Host mHost; |
| 22 |
| 23 // Semaphore to keep track of whether cleanup has started yet. |
| 24 private final Semaphore mSemaphore = new Semaphore(0); |
| 25 |
| 26 // We will forward to |host| on whatever thread we're constructed on. |
| 27 public ThreadHoppingHost(DialogOverlayCore.Host host) { |
| 28 mHandler = new Handler(); |
| 29 mHost = host; |
| 30 } |
| 31 |
| 32 @Override |
| 33 public void onSurfaceReady(final Surface surface) { |
| 34 mHandler.post(new Runnable() { |
| 35 @Override |
| 36 public void run() { |
| 37 mHost.onSurfaceReady(surface); |
| 38 } |
| 39 }); |
| 40 } |
| 41 |
| 42 @Override |
| 43 public void onOverlayDestroyed() { |
| 44 mHandler.post(new Runnable() { |
| 45 @Override |
| 46 public void run() { |
| 47 mHost.onOverlayDestroyed(); |
| 48 } |
| 49 }); |
| 50 } |
| 51 |
| 52 // We don't forward via to |mHandler|, since it should be asynchronous. Els
e, the |mHandler| |
| 53 // thread would block. Instead, we wait here and somebody must call onClean
up() to let us know |
| 54 // that cleanup has started, and that we may return. |
| 55 @Override |
| 56 public void waitForCleanup() { |
| 57 while (true) { |
| 58 try { |
| 59 mSemaphore.acquire(); |
| 60 break; |
| 61 } catch (InterruptedException e) { |
| 62 } |
| 63 } |
| 64 } |
| 65 |
| 66 // Notify us that cleanup has started. This is called on |mHandler|'s threa
d. |
| 67 public void onCleanup() { |
| 68 mSemaphore.release(1); |
| 69 } |
| 70 } |
OLD | NEW |