From 01228fcff613e2f30773130dd31acd3ba7d62bac Mon Sep 17 00:00:00 2001 From: Steve Block Date: Fri, 19 Feb 2010 12:59:50 +0000 Subject: [PATCH] Makes sure GeolocationPermissions is fully robust to calls being made before the message handler is initialized Bug: 2315829 Change-Id: I6c0d1adf7049c5c89228e43e34934702664c8691 --- .../webkit/GeolocationPermissions.java | 87 ++++++------------- 1 file changed, 26 insertions(+), 61 deletions(-) diff --git a/core/java/android/webkit/GeolocationPermissions.java b/core/java/android/webkit/GeolocationPermissions.java index 817fb3c2116e0..4565b756ed460 100755 --- a/core/java/android/webkit/GeolocationPermissions.java +++ b/core/java/android/webkit/GeolocationPermissions.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.Vector; /** @@ -61,11 +62,8 @@ public final class GeolocationPermissions { private Handler mHandler; private Handler mUIHandler; - // Members used to transfer the origins and permissions between threads. - private Set mOrigins; - private boolean mAllowed; - private Set mOriginsToClear; - private Set mOriginsToAllow; + // A queue to store messages until the handler is ready. + private Vector mQueuedMessages; // Message ids static final int GET_ORIGINS = 0; @@ -134,21 +132,21 @@ public final class GeolocationPermissions { // Runs on the WebKit thread. switch (msg.what) { case GET_ORIGINS: { - getOriginsImpl(); + Set origins = nativeGetOrigins(); ValueCallback callback = (ValueCallback) msg.obj; Map values = new HashMap(); values.put(CALLBACK, callback); - values.put(ORIGINS, mOrigins); + values.put(ORIGINS, origins); postUIMessage(Message.obtain(null, RETURN_ORIGINS, values)); } break; case GET_ALLOWED: { Map values = (Map) msg.obj; String origin = (String) values.get(ORIGIN); ValueCallback callback = (ValueCallback) values.get(CALLBACK); - getAllowedImpl(origin); + boolean allowed = nativeGetAllowed(origin); Map retValues = new HashMap(); retValues.put(CALLBACK, callback); - retValues.put(ALLOWED, new Boolean(mAllowed)); + retValues.put(ALLOWED, new Boolean(allowed)); postUIMessage(Message.obtain(null, RETURN_ALLOWED, retValues)); } break; case CLEAR: @@ -164,15 +162,12 @@ public final class GeolocationPermissions { } }; - if (mOriginsToClear != null) { - for (String origin : mOriginsToClear) { - nativeClear(origin); - } - } - if (mOriginsToAllow != null) { - for (String origin : mOriginsToAllow) { - nativeAllow(origin); + // Handle the queued messages + if (mQueuedMessages != null) { + while (!mQueuedMessages.isEmpty()) { + mHandler.sendMessage(mQueuedMessages.remove(0)); } + mQueuedMessages = null; } } } @@ -181,8 +176,14 @@ public final class GeolocationPermissions { * Utility function to send a message to our handler. */ private synchronized void postMessage(Message msg) { - assert(mHandler != null); - mHandler.sendMessage(msg); + if (mHandler == null) { + if (mQueuedMessages == null) { + mQueuedMessages = new Vector(); + } + mQueuedMessages.add(msg); + } else { + mHandler.sendMessage(msg); + } } /** @@ -207,22 +208,14 @@ public final class GeolocationPermissions { public void getOrigins(ValueCallback > callback) { if (callback != null) { if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { - getOriginsImpl(); - callback.onReceiveValue(mOrigins); + Set origins = nativeGetOrigins(); + callback.onReceiveValue(origins); } else { postMessage(Message.obtain(null, GET_ORIGINS, callback)); } } } - /** - * Helper method to get the set of origins. - */ - private void getOriginsImpl() { - // Called on the WebKit thread. - mOrigins = nativeGetOrigins(); - } - /** * Gets the permission state for the specified origin. * @@ -238,8 +231,8 @@ public final class GeolocationPermissions { return; } if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { - getAllowedImpl(origin); - callback.onReceiveValue(new Boolean(mAllowed)); + boolean allowed = nativeGetAllowed(origin); + callback.onReceiveValue(new Boolean(allowed)); } else { Map values = new HashMap(); values.put(ORIGIN, origin); @@ -248,14 +241,6 @@ public final class GeolocationPermissions { } } - /** - * Helper method to get the permission state for the specified origin. - */ - private void getAllowedImpl(String origin) { - // Called on the WebKit thread. - mAllowed = nativeGetAllowed(origin); - } - /** * Clears the permission state for the specified origin. This method may be * called before the WebKit thread has intialized the message handler. @@ -263,17 +248,7 @@ public final class GeolocationPermissions { */ public void clear(String origin) { // Called on the UI thread. - if (mHandler == null) { - if (mOriginsToClear == null) { - mOriginsToClear = new HashSet(); - } - mOriginsToClear.add(origin); - if (mOriginsToAllow != null) { - mOriginsToAllow.remove(origin); - } - } else { - postMessage(Message.obtain(null, CLEAR, origin)); - } + postMessage(Message.obtain(null, CLEAR, origin)); } /** @@ -283,17 +258,7 @@ public final class GeolocationPermissions { */ public void allow(String origin) { // Called on the UI thread. - if (mHandler == null) { - if (mOriginsToAllow == null) { - mOriginsToAllow = new HashSet(); - } - mOriginsToAllow.add(origin); - if (mOriginsToClear != null) { - mOriginsToClear.remove(origin); - } - } else { - postMessage(Message.obtain(null, ALLOW, origin)); - } + postMessage(Message.obtain(null, ALLOW, origin)); } /**