diff options
author | Shimeng (Simon) Wang <swang@google.com> | 2010-09-13 10:23:02 -0700 |
---|---|---|
committer | Shimeng (Simon) Wang <swang@google.com> | 2010-09-13 10:47:58 -0700 |
commit | dc9f2b6668b408d451cf63527ef2ce074f678ae0 (patch) | |
tree | e3901fdbec3a82b8431728d4536d55465f2a2789 /core/java/android | |
parent | 3d7606aa607b24817e37c264f2141ed7b2d50be0 (diff) | |
download | frameworks_base-dc9f2b6668b408d451cf63527ef2ce074f678ae0.zip frameworks_base-dc9f2b6668b408d451cf63527ef2ce074f678ae0.tar.gz frameworks_base-dc9f2b6668b408d451cf63527ef2ce074f678ae0.tar.bz2 |
DO NOT MERGE
Use a static variable and methods for the current WebView.
sJavaBridge may not exist by the time onWindowFocusChanged is called.
To avoid an NPE, just static methods that modify a global field.
JWebCoreJavaBridge is a global object anyway so using a static field
will be fine. To avoid any garbage collection issues, store the
WebView in a WeakReference.
Bug: 2908023
Change-Id: I05e9261f2c3d13c10c73c9b34f3aeea1d12a08a6
Diffstat (limited to 'core/java/android')
-rw-r--r-- | core/java/android/webkit/JWebCoreJavaBridge.java | 22 | ||||
-rw-r--r-- | core/java/android/webkit/WebView.java | 4 |
2 files changed, 15 insertions, 11 deletions
diff --git a/core/java/android/webkit/JWebCoreJavaBridge.java b/core/java/android/webkit/JWebCoreJavaBridge.java index e766693..1f8d53f 100644 --- a/core/java/android/webkit/JWebCoreJavaBridge.java +++ b/core/java/android/webkit/JWebCoreJavaBridge.java @@ -20,6 +20,8 @@ import android.os.Handler; import android.os.Message; import android.util.Log; +import java.lang.ref.WeakReference; +import java.util.HashMap; import java.util.Set; final class JWebCoreJavaBridge extends Handler { @@ -44,7 +46,8 @@ final class JWebCoreJavaBridge extends Handler { // keep track of the main WebView attached to the current window so that we // can get the proper Context. - private WebView mCurrentMainWebView; + private static WeakReference<WebView> sCurrentMainWebView = + new WeakReference<WebView>(null); /* package */ static final int REFRESH_PLUGINS = 100; @@ -62,20 +65,20 @@ final class JWebCoreJavaBridge extends Handler { nativeFinalize(); } - synchronized void setActiveWebView(WebView webview) { - if (mCurrentMainWebView != null) { + static synchronized void setActiveWebView(WebView webview) { + if (sCurrentMainWebView.get() != null) { // it is possible if there is a sub-WebView. Do nothing. return; } - mCurrentMainWebView = webview; + sCurrentMainWebView = new WeakReference<WebView>(webview); } - synchronized void removeActiveWebView(WebView webview) { - if (mCurrentMainWebView != webview) { + static synchronized void removeActiveWebView(WebView webview) { + if (sCurrentMainWebView.get() != webview) { // it is possible if there is a sub-WebView. Do nothing. return; } - mCurrentMainWebView = null; + sCurrentMainWebView.clear(); } /** @@ -256,11 +259,12 @@ final class JWebCoreJavaBridge extends Handler { synchronized private String getSignedPublicKey(int index, String challenge, String url) { - if (mCurrentMainWebView != null) { + WebView current = sCurrentMainWebView.get(); + if (current != null) { // generateKeyPair expects organizations which we don't have. Ignore // url. return CertTool.getSignedPublicKey( - mCurrentMainWebView.getContext(), index, challenge); + current.getContext(), index, challenge); } else { Log.e(LOGTAG, "There is no active WebView for getSignedPublicKey"); return ""; diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 4bb11bb..456e0d9 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -4502,9 +4502,9 @@ public class WebView extends AbsoluteLayout public void onWindowFocusChanged(boolean hasWindowFocus) { setActive(hasWindowFocus); if (hasWindowFocus) { - BrowserFrame.sJavaBridge.setActiveWebView(this); + JWebCoreJavaBridge.setActiveWebView(this); } else { - BrowserFrame.sJavaBridge.removeActiveWebView(this); + JWebCoreJavaBridge.removeActiveWebView(this); } super.onWindowFocusChanged(hasWindowFocus); } |