OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser.androidoverlay; | 5 package org.chromium.content.browser.androidoverlay; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.os.Handler; | |
9 import android.os.HandlerThread; | |
8 | 10 |
9 import org.chromium.media.mojom.AndroidOverlay; | 11 import org.chromium.media.mojom.AndroidOverlay; |
10 import org.chromium.media.mojom.AndroidOverlayClient; | 12 import org.chromium.media.mojom.AndroidOverlayClient; |
11 import org.chromium.media.mojom.AndroidOverlayConfig; | 13 import org.chromium.media.mojom.AndroidOverlayConfig; |
12 import org.chromium.media.mojom.AndroidOverlayProvider; | 14 import org.chromium.media.mojom.AndroidOverlayProvider; |
13 import org.chromium.mojo.bindings.InterfaceRequest; | 15 import org.chromium.mojo.bindings.InterfaceRequest; |
14 import org.chromium.mojo.system.MojoException; | 16 import org.chromium.mojo.system.MojoException; |
15 import org.chromium.services.service_manager.InterfaceFactory; | 17 import org.chromium.services.service_manager.InterfaceFactory; |
16 | 18 |
17 /** | 19 /** |
18 * Default impl of AndroidOverlayProvider. Creates AndroidOverlayImpls. | 20 * Default impl of AndroidOverlayProvider. Creates AndroidOverlayImpls. We're a singleton, in the |
21 * sense that all provider clients talk to the same instance in the browser. | |
19 */ | 22 */ |
20 public class AndroidOverlayProviderImpl implements AndroidOverlayProvider { | 23 public class AndroidOverlayProviderImpl implements AndroidOverlayProvider { |
21 private static final String TAG = "AndroidOverlayProvider"; | 24 private static final String TAG = "AndroidOverlayProvider"; |
22 | 25 |
26 // We maintain a thread with a Looper for the AndroidOverlays to use, since Dialog requires one. | |
27 // We don't want this to be the native thread that's used to create them (th e browser UI thread) | |
28 // since we don't want to block that waiting for sync callbacks from Android , such as | |
29 // surfaceDestroyed. Instead, we run all AndroidOverlays on one shared over lay-ui thread. | |
boliu
2017/03/30 22:42:02
maybe just call this the "overlay" thread everywhe
liberato (no reviews please)
2017/04/04 17:49:29
Done.
| |
30 private HandlerThread mOverlayUiThread; | |
31 private Handler mHandler; | |
32 | |
33 // Number of AndroidOverlays that have been created but not released. | |
34 private int mNumOverlays; | |
35 | |
36 // Runnable that notifies us that a client has been released. | |
37 private Runnable mNotifyReleasedRunnable = new Runnable() { | |
38 @Override | |
39 public void run() { | |
40 notifyReleased(); | |
41 } | |
42 }; | |
43 | |
44 // Maximum number of concurrent overlays that we allow. | |
45 private static final int MAX_OVERLAYS = 1; | |
boliu
2017/03/30 22:42:02
nit: constants at the top
liberato (no reviews please)
2017/04/04 17:49:29
Done.
| |
46 | |
23 /** | 47 /** |
24 * Create an overlay matching |config| for |client|, and bind it to |request |. Remember that | 48 * Create an overlay matching |config| for |client|, and bind it to |request |. Remember that |
25 * potentially many providers are created. | 49 * potentially many providers are created. |
26 */ | 50 */ |
27 public void createOverlay(InterfaceRequest<AndroidOverlay> request, AndroidO verlayClient client, | 51 public void createOverlay(InterfaceRequest<AndroidOverlay> request, AndroidO verlayClient client, |
boliu
2017/03/30 22:42:02
is this @Override?
liberato (no reviews please)
2017/04/04 17:49:29
Done.
| |
28 AndroidOverlayConfig config) { | 52 AndroidOverlayConfig config) { |
29 client.onDestroyed(); | 53 // Limit the number of concurrent surfaces. |
54 if (mNumOverlays >= MAX_OVERLAYS) { | |
55 client.onDestroyed(); | |
56 return; | |
57 } | |
58 | |
59 startThreadIfNeeded(); | |
60 mNumOverlays++; | |
61 | |
62 DialogOverlayOperations ops = | |
63 new DialogOverlayOperationsImpl(mHandler, mNotifyReleasedRunnabl e); | |
64 | |
65 DialogOverlayImpl impl = new DialogOverlayImpl(client, config, ops); | |
66 DialogOverlayImpl.MANAGER.bind(impl, request); | |
30 } | 67 } |
31 | 68 |
69 /** | |
70 * Make sure that mOverlayUiThread and mHandler are ready for use, if needed . | |
71 */ | |
72 private void startThreadIfNeeded() { | |
73 if (mOverlayUiThread != null) return; | |
74 | |
75 mOverlayUiThread = new HandlerThread("AndroidOverlayThread"); | |
76 mOverlayUiThread.start(); | |
77 mHandler = new Handler(mOverlayUiThread.getLooper()); | |
78 } | |
79 | |
80 /** | |
81 * Called by AndroidOverlays when they no longer need the thread via |mNotif yReleasedRunnable|. | |
82 */ | |
83 public void notifyReleased() { | |
boliu
2017/03/30 22:42:02
only called by runnable, right? so can be private
liberato (no reviews please)
2017/04/04 17:49:29
Done.
| |
84 assert mNumOverlays > 0; | |
85 mNumOverlays--; | |
86 | |
87 // We don't stop the looper thread here, else android can get mad when i t tries to send | |
88 // a message from the dialog on this thread. AndroidOverlay might have to notify us | |
89 // separately to tell us when it's done with the thread, if we don't wan t to wait until | |
90 // then to start creating a new SV. | |
91 // Instead, we just avoid shutting down the thread at all for now. | |
92 } | |
93 | |
94 // Remember that we can't tell which client disconnected. | |
32 @Override | 95 @Override |
33 public void close() {} | 96 public void close() {} |
34 | 97 |
98 // Remember that we can't tell which client disconnected. | |
35 @Override | 99 @Override |
36 public void onConnectionError(MojoException e) {} | 100 public void onConnectionError(MojoException e) {} |
37 | 101 |
38 /** | 102 /** |
39 * Mojo factory. | 103 * Mojo factory. |
40 */ | 104 */ |
41 public static class Factory implements InterfaceFactory<AndroidOverlayProvid er> { | 105 public static class Factory implements InterfaceFactory<AndroidOverlayProvid er> { |
106 private static AndroidOverlayProviderImpl sImpl; | |
42 public Factory(Context context) {} | 107 public Factory(Context context) {} |
43 | 108 |
44 @Override | 109 @Override |
45 public AndroidOverlayProvider createImpl() { | 110 public AndroidOverlayProvider createImpl() { |
46 return new AndroidOverlayProviderImpl(); | 111 if (sImpl == null) sImpl = new AndroidOverlayProviderImpl(); |
112 return sImpl; | |
47 } | 113 } |
48 } | 114 } |
49 } | 115 } |
OLD | NEW |