mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 993195 - Add EventCallback parameter to handleMessage. r=jchen
This commit is contained in:
parent
7157447196
commit
3a0c2ffebe
@ -1632,7 +1632,7 @@ abstract public class BrowserApp extends GeckoApp
|
||||
message.put("type", BrowserHealthRecorder.EVENT_SEARCH);
|
||||
message.put("location", where);
|
||||
message.put("identifier", identifier);
|
||||
GeckoAppShell.getEventDispatcher().dispatchEvent(message);
|
||||
GeckoAppShell.getEventDispatcher().dispatchEvent(message, null);
|
||||
} catch (Exception e) {
|
||||
Log.w(LOGTAG, "Error recording search.", e);
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ package org.mozilla.gecko;
|
||||
|
||||
import org.mozilla.gecko.GeckoAppShell;
|
||||
import org.mozilla.gecko.GeckoEvent;
|
||||
import org.mozilla.gecko.util.EventCallback;
|
||||
import org.mozilla.gecko.util.GeckoEventListener;
|
||||
import org.mozilla.gecko.util.NativeEventListener;
|
||||
import org.mozilla.gecko.util.NativeJSContainer;
|
||||
@ -15,7 +16,6 @@ import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -24,6 +24,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
public final class EventDispatcher {
|
||||
private static final String LOGTAG = "GeckoEventDispatcher";
|
||||
private static final String GUID = "__guid__";
|
||||
private static final String STATUS_CANCEL = "cancel";
|
||||
private static final String STATUS_ERROR = "error";
|
||||
private static final String STATUS_SUCCESS = "success";
|
||||
|
||||
@ -138,6 +139,7 @@ public final class EventDispatcher {
|
||||
}
|
||||
|
||||
public void dispatchEvent(final NativeJSContainer message) {
|
||||
EventCallback callback = null;
|
||||
try {
|
||||
// First try native listeners.
|
||||
final String type = message.getString("type");
|
||||
@ -146,12 +148,18 @@ public final class EventDispatcher {
|
||||
synchronized (mGeckoThreadNativeListeners) {
|
||||
listeners = mGeckoThreadNativeListeners.get(type);
|
||||
}
|
||||
|
||||
final String guid = message.optString(GUID, null);
|
||||
if (guid != null) {
|
||||
callback = new GeckoEventCallback(guid, type);
|
||||
}
|
||||
|
||||
if (listeners != null) {
|
||||
if (listeners.size() == 0) {
|
||||
Log.w(LOGTAG, "No listeners for " + type);
|
||||
}
|
||||
for (final NativeEventListener listener : listeners) {
|
||||
listener.handleMessage(type, message);
|
||||
listener.handleMessage(type, message, callback);
|
||||
}
|
||||
// If we found native listeners, we assume we don't have any JSON listeners
|
||||
// and return early. This assumption is checked when registering listeners.
|
||||
@ -162,7 +170,7 @@ public final class EventDispatcher {
|
||||
}
|
||||
try {
|
||||
// If we didn't find native listeners, try JSON listeners.
|
||||
dispatchEvent(new JSONObject(message.toString()));
|
||||
dispatchEvent(new JSONObject(message.toString()), callback);
|
||||
} catch (final JSONException e) {
|
||||
Log.e(LOGTAG, "Cannot parse JSON");
|
||||
} catch (final UnsupportedOperationException e) {
|
||||
@ -170,7 +178,7 @@ public final class EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
public void dispatchEvent(final JSONObject message) {
|
||||
public void dispatchEvent(final JSONObject message, final EventCallback callback) {
|
||||
// {
|
||||
// "type": "value",
|
||||
// "event_specific": "value",
|
||||
@ -184,6 +192,12 @@ public final class EventDispatcher {
|
||||
}
|
||||
if (listeners == null || listeners.size() == 0) {
|
||||
Log.w(LOGTAG, "No listeners for " + type);
|
||||
|
||||
// If there are no listeners, cancel the callback to prevent Gecko-side observers
|
||||
// from being leaked.
|
||||
if (callback != null) {
|
||||
callback.sendCancel();
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (final GeckoEventListener listener : listeners) {
|
||||
@ -194,23 +208,70 @@ public final class EventDispatcher {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void sendResponse(JSONObject message, Object response) {
|
||||
sendResponseHelper(STATUS_SUCCESS, message, response);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static void sendError(JSONObject message, Object response) {
|
||||
sendResponseHelper(STATUS_ERROR, message, response);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static void sendResponseHelper(String status, JSONObject message, Object response) {
|
||||
try {
|
||||
final JSONObject wrapper = new JSONObject();
|
||||
wrapper.put(GUID, message.getString(GUID));
|
||||
wrapper.put("status", status);
|
||||
wrapper.put("response", response);
|
||||
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(message.getString("type") + ":Response", wrapper.toString()));
|
||||
} catch (JSONException e) {
|
||||
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(
|
||||
message.getString("type") + ":Response", wrapper.toString()));
|
||||
} catch (final JSONException e) {
|
||||
Log.e(LOGTAG, "Unable to send response", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static class GeckoEventCallback implements EventCallback {
|
||||
private final String guid;
|
||||
private final String type;
|
||||
private boolean sent;
|
||||
|
||||
public GeckoEventCallback(final String guid, final String type) {
|
||||
this.guid = guid;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void sendSuccess(final Object response) {
|
||||
sendResponse(STATUS_SUCCESS, response);
|
||||
}
|
||||
|
||||
public void sendError(final Object response) {
|
||||
sendResponse(STATUS_ERROR, response);
|
||||
}
|
||||
|
||||
public void sendCancel() {
|
||||
sendResponse(STATUS_CANCEL, null);
|
||||
}
|
||||
|
||||
private void sendResponse(final String status, final Object response) {
|
||||
if (sent) {
|
||||
throw new IllegalStateException("Callback has already been executed for type=" +
|
||||
type + ", guid=" + guid);
|
||||
}
|
||||
|
||||
sent = true;
|
||||
|
||||
try {
|
||||
final JSONObject wrapper = new JSONObject();
|
||||
wrapper.put(GUID, guid);
|
||||
wrapper.put("status", status);
|
||||
wrapper.put("response", response);
|
||||
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(type + ":Response",
|
||||
wrapper.toString()));
|
||||
} catch (final JSONException e) {
|
||||
Log.e(LOGTAG, "Unable to send response for: " + type, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,7 @@ gujar.sources += [
|
||||
'util/ActivityResultHandler.java',
|
||||
'util/ActivityResultHandlerMap.java',
|
||||
'util/Clipboard.java',
|
||||
'util/EventCallback.java',
|
||||
'util/FileUtils.java',
|
||||
'util/FloatUtils.java',
|
||||
'util/GamepadUtils.java',
|
||||
|
31
mobile/android/base/util/EventCallback.java
Normal file
31
mobile/android/base/util/EventCallback.java
Normal file
@ -0,0 +1,31 @@
|
||||
package org.mozilla.gecko.util;
|
||||
|
||||
/**
|
||||
* Callback interface for Gecko requests.
|
||||
*
|
||||
* For each instance of EventCallback, exactly one of sendResponse, sendError, or sendCancel
|
||||
* must be called to prevent observer leaks. If more than one send* method is called, or if a
|
||||
* single send method is called multiple times, an {@link IllegalStateException} will be thrown.
|
||||
*/
|
||||
public interface EventCallback {
|
||||
/**
|
||||
* Sends a success response with the given data.
|
||||
*
|
||||
* @param response The response data to send to Gecko. Can be any of the types accepted by
|
||||
* JSONObject#put(String, Object).
|
||||
*/
|
||||
public void sendSuccess(Object response);
|
||||
|
||||
/**
|
||||
* Sends an error response with the given data.
|
||||
*
|
||||
* @param response The response data to send to Gecko. Can be any of the types accepted by
|
||||
* JSONObject#put(String, Object).
|
||||
*/
|
||||
public void sendError(Object response);
|
||||
|
||||
/**
|
||||
* Cancels the request, preventing any Gecko-side callbacks from being executed.
|
||||
*/
|
||||
public void sendCancel();
|
||||
}
|
@ -9,5 +9,15 @@ import org.mozilla.gecko.mozglue.RobocopTarget;
|
||||
|
||||
@RobocopTarget
|
||||
public interface NativeEventListener {
|
||||
void handleMessage(String event, NativeJSObject message);
|
||||
/**
|
||||
* Handles a message sent from Gecko.
|
||||
*
|
||||
* @param event The name of the event being sent.
|
||||
* @param message The message data.
|
||||
* @param callback The callback interface for this message. A callback is provided only if the
|
||||
* originating sendMessageToJava call included a callback argument; otherwise,
|
||||
* callback will be null. All listeners for a given event are given the same
|
||||
* callback object, and exactly one listener must handle the callback.
|
||||
*/
|
||||
void handleMessage(String event, NativeJSObject message, EventCallback callback);
|
||||
}
|
||||
|
@ -26,6 +26,11 @@ function sendMessageToJava(aMessage, aCallback) {
|
||||
|
||||
Services.obs.removeObserver(obs, aMessage.type + ":Response", false);
|
||||
|
||||
if (data.status === "cancel") {
|
||||
// No Java-side listeners handled our callback.
|
||||
return;
|
||||
}
|
||||
|
||||
aCallback(data.status === "success" ? data.response : null,
|
||||
data.status === "error" ? data.response : null);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user