Bug 1053889 - Handle null devices returned from Chromecast code. r=mfinkle a=sylvestre
authorWes Johnston <wjohnston@mozilla.com>
Mon, 25 Aug 2014 16:19:29 -0700
changeset 217695 b09553fc5f332a911acf132d9c9d23582ee5f24f
parent 217694 46c3498c3bcd3386a9011bb554f69cd3726df266
child 217696 181adde439462478c976cba134e4c2a9ef9f6d77
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, sylvestre
bugs1053889
milestone33.0a2
Bug 1053889 - Handle null devices returned from Chromecast code. r=mfinkle a=sylvestre
mobile/android/base/ChromeCast.java
mobile/android/base/MediaPlayerManager.java
--- a/mobile/android/base/ChromeCast.java
+++ b/mobile/android/base/ChromeCast.java
@@ -145,16 +145,20 @@ class ChromeCast implements GeckoMediaPl
     }
 
     // This dumps everything we can find about the device into JSON. This will hopefully make it
     // easier to filter out duplicate devices from different sources in js.
     public JSONObject toJSON() {
         final JSONObject obj = new JSONObject();
         try {
             final CastDevice device = CastDevice.getFromBundle(route.getExtras());
+            if (device == null) {
+                return null;
+            }
+
             obj.put("uuid", route.getId());
             obj.put("version", device.getDeviceVersion());
             obj.put("friendlyName", device.getFriendlyName());
             obj.put("location", device.getIpAddress().toString());
             obj.put("modelName", device.getModelName());
             // For now we just assume all of these are Google devices
             obj.put("manufacturer", "Google Inc.");
         } catch(JSONException ex) {
--- a/mobile/android/base/MediaPlayerManager.java
+++ b/mobile/android/base/MediaPlayerManager.java
@@ -16,16 +16,18 @@ import org.json.JSONException;
 import android.content.Context;
 import android.support.v7.media.MediaControlIntent;
 import android.support.v7.media.MediaRouteSelector;
 import android.support.v7.media.MediaRouter;
 import android.support.v7.media.MediaRouter.RouteInfo;
 import android.util.Log;
 
 import java.util.HashMap;
+import java.util.Map;
+import java.util.Iterator;
 
 /* Wraper for different MediaRouter types supproted by Android. i.e. Chromecast, Miracast, etc. */
 interface GeckoMediaPlayer {
     public JSONObject toJSON();
     public void load(String title, String url, String type, EventCallback callback);
     public void play(EventCallback callback);
     public void pause(EventCallback callback);
     public void stop(EventCallback callback);
@@ -51,17 +53,17 @@ class MediaPlayerManager implements Nati
     private static void debug(String msg) {
         if (SHOW_DEBUG) {
             Log.d(LOGTAG, msg);
         }
     }
 
     private final Context context;
     private final MediaRouter mediaRouter;
-    private final HashMap<String, GeckoMediaPlayer> displays = new HashMap<String, GeckoMediaPlayer>();
+    private final Map<String, GeckoMediaPlayer> displays = new HashMap<String, GeckoMediaPlayer>();
     private static MediaPlayerManager instance;
 
     @JNITarget
     public static void init(Context context) {
         if (instance != null) {
             debug("MediaPlayerManager initialized twice");
             return;
         }
@@ -109,19 +111,27 @@ class MediaPlayerManager implements Nati
     // GeckoEventListener implementation
     @Override
     public void handleMessage(String event, final NativeJSObject message, final EventCallback callback) {
         debug(event);
 
         if ("MediaPlayer:Get".equals(event)) {
             final JSONObject result = new JSONObject();
             final JSONArray disps = new JSONArray();
-            for (GeckoMediaPlayer disp : displays.values()) {
+
+            final Iterator<GeckoMediaPlayer> items = displays.values().iterator();
+            while (items.hasNext()) {
+                GeckoMediaPlayer disp = items.next();
                 try {
-                    disps.put(disp.toJSON());
+                    JSONObject json = disp.toJSON();
+                    if (json == null) {
+                        items.remove();
+                    } else {
+                        disps.put(json);
+                    }
                 } catch(Exception ex) {
                     // This may happen if the device isn't a real Chromecast,
                     // for example Firefly casting devices.
                     Log.e(LOGTAG, "Couldn't create JSON for display", ex);
                 }
             }
 
             try {