Skip to content

Commit

Permalink
Bug 1046880 - Catch invalid name exceptions in EventDispatcher; r=rne…
Browse files Browse the repository at this point in the history
…wman
  • Loading branch information
Jim Chen committed Aug 6, 2014
1 parent a359b31 commit d844f2c
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions mobile/android/base/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.mozilla.gecko.util.GeckoEventListener;
import org.mozilla.gecko.util.NativeEventListener;
import org.mozilla.gecko.util.NativeJSContainer;
import org.mozilla.gecko.util.NativeJSObject;

import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -141,7 +142,11 @@ public void unregisterGeckoThreadListener(final GeckoEventListener listener,

public void dispatchEvent(final NativeJSContainer message) {
// First try native listeners.
final String type = message.getString("type");
final String type = message.optString("type", null);
if (type == null) {
Log.e(LOGTAG, "JSON message must have a type property");
return;
}

final List<NativeEventListener> listeners;
synchronized (mGeckoThreadNativeListeners) {
Expand All @@ -158,8 +163,12 @@ public void dispatchEvent(final NativeJSContainer message) {
if (listeners.size() == 0) {
Log.w(LOGTAG, "No listeners for " + type);
}
for (final NativeEventListener listener : listeners) {
listener.handleMessage(type, message, callback);
try {
for (final NativeEventListener listener : listeners) {
listener.handleMessage(type, message, callback);
}
} catch (final NativeJSObject.InvalidPropertyException e) {
Log.e(LOGTAG, "Exception occurred while handling " + type, e);
}
// If we found native listeners, we assume we don't have any JSON listeners
// and return early. This assumption is checked when registering listeners.
Expand All @@ -170,9 +179,9 @@ public void dispatchEvent(final NativeJSContainer message) {
// If we didn't find native listeners, try JSON listeners.
dispatchEvent(new JSONObject(message.toString()), callback);
} catch (final JSONException e) {
Log.e(LOGTAG, "Cannot parse JSON");
Log.e(LOGTAG, "Cannot parse JSON", e);
} catch (final UnsupportedOperationException e) {
Log.e(LOGTAG, "Cannot convert message to JSON");
Log.e(LOGTAG, "Cannot convert message to JSON", e);
}
}

Expand Down

0 comments on commit d844f2c

Please sign in to comment.