Skip to content

Commit 85aa944

Browse files
author
Steve Block
committed
Fix HTML5Audio to call WebView.isPrivateBrowsingEnabled() on the UI thread
Bug: 5384494 Change-Id: I2587cf0e898f67becb9dbc2067fb1b0a6a37e124
1 parent cbb62bb commit 85aa944

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

core/java/android/webkit/HTML5Audio.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.media.MediaPlayer.OnPreparedListener;
2424
import android.media.MediaPlayer.OnSeekCompleteListener;
2525
import android.os.Handler;
26+
import android.os.Looper;
2627
import android.os.Message;
2728
import android.util.Log;
2829

@@ -33,7 +34,11 @@
3334
import java.util.TimerTask;
3435

3536
/**
36-
* <p>HTML5 support class for Audio.
37+
* HTML5 support class for Audio.
38+
*
39+
* This class runs almost entirely on the WebCore thread. The exception is when
40+
* accessing the WebView object to determine whether private browsing is
41+
* enabled.
3742
*/
3843
class HTML5Audio extends Handler
3944
implements MediaPlayer.OnBufferingUpdateListener,
@@ -49,7 +54,7 @@ class HTML5Audio extends Handler
4954
// The C++ MediaPlayerPrivateAndroid object.
5055
private int mNativePointer;
5156
// The private status of the view that created this player
52-
private boolean mIsPrivate;
57+
private IsPrivateBrowsingEnabledGetter mIsPrivateBrowsingEnabledGetter;
5358

5459
private static int IDLE = 0;
5560
private static int INITIALIZED = 1;
@@ -82,6 +87,35 @@ public void run() {
8287
}
8388
}
8489

90+
// Helper class to determine whether private browsing is enabled in the
91+
// given WebView. Queries the WebView on the UI thread. Calls to get()
92+
// block until the data is available.
93+
private class IsPrivateBrowsingEnabledGetter {
94+
private boolean mIsReady;
95+
private boolean mIsPrivateBrowsingEnabled;
96+
IsPrivateBrowsingEnabledGetter(Looper uiThreadLooper, final WebView webView) {
97+
new Handler(uiThreadLooper).post(new Runnable() {
98+
@Override
99+
public void run() {
100+
synchronized(IsPrivateBrowsingEnabledGetter.this) {
101+
mIsPrivateBrowsingEnabled = webView.isPrivateBrowsingEnabled();
102+
mIsReady = true;
103+
IsPrivateBrowsingEnabledGetter.this.notify();
104+
}
105+
}
106+
});
107+
}
108+
synchronized boolean get() {
109+
while (!mIsReady) {
110+
try {
111+
wait();
112+
} catch (InterruptedException e) {
113+
}
114+
}
115+
return mIsPrivateBrowsingEnabled;
116+
}
117+
};
118+
85119
@Override
86120
public void handleMessage(Message msg) {
87121
switch (msg.what) {
@@ -149,7 +183,8 @@ public HTML5Audio(WebViewCore webViewCore, int nativePtr) {
149183
// Save the native ptr
150184
mNativePointer = nativePtr;
151185
resetMediaPlayer();
152-
mIsPrivate = webViewCore.getWebView().isPrivateBrowsingEnabled();
186+
mIsPrivateBrowsingEnabledGetter = new IsPrivateBrowsingEnabledGetter(
187+
webViewCore.getContext().getMainLooper(), webViewCore.getWebView());
153188
}
154189

155190
private void resetMediaPlayer() {
@@ -177,13 +212,14 @@ private void setDataSource(String url) {
177212
if (mState != IDLE) {
178213
resetMediaPlayer();
179214
}
180-
String cookieValue = CookieManager.getInstance().getCookie(url, mIsPrivate);
215+
String cookieValue = CookieManager.getInstance().getCookie(
216+
url, mIsPrivateBrowsingEnabledGetter.get());
181217
Map<String, String> headers = new HashMap<String, String>();
182218

183219
if (cookieValue != null) {
184220
headers.put(COOKIE, cookieValue);
185221
}
186-
if (mIsPrivate) {
222+
if (mIsPrivateBrowsingEnabledGetter.get()) {
187223
headers.put(HIDE_URL_LOGS, "true");
188224
}
189225

core/java/android/webkit/WebViewCore.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,8 @@ private void sendViewInvalidate(int left, int top, int right, int bottom) {
22252225
mRepaintScheduled = false;
22262226
}
22272227

2228+
// Gets the WebView corresponding to this WebViewCore. Note that the
2229+
// WebView object must only be used on the UI thread.
22282230
/* package */ WebView getWebView() {
22292231
return mWebView;
22302232
}
@@ -2626,8 +2628,7 @@ private void setWebTextViewAutoFillable(int queryId, String preview) {
26262628
}
26272629
}
26282630

2629-
// called by JNI
2630-
private Context getContext() {
2631+
Context getContext() {
26312632
return mContext;
26322633
}
26332634

0 commit comments

Comments
 (0)