Bug 842883 - (Part 2.5) Clean up Tabs.handleMessage to avoid calling getTab() all the time. r=bnicholson
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 24 Feb 2013 20:51:05 -0800
changeset 122878 0145cda4b702340c255999e659e5bade8f5e4d3d
parent 122877 1e3df7da1d11cbe6f52bf9447b8847dc46f5f870
child 122879 efdd184d9efd6ab0afce4c82056e67431fb2ca78
push id1387
push userphilringnalda@gmail.com
push dateTue, 26 Feb 2013 22:32:56 +0000
treeherderfx-team@ad4cc4e97774 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs842883
milestone22.0a1
Bug 842883 - (Part 2.5) Clean up Tabs.handleMessage to avoid calling getTab() all the time. r=bnicholson
mobile/android/base/Tabs.java
--- a/mobile/android/base/Tabs.java
+++ b/mobile/android/base/Tabs.java
@@ -54,26 +54,26 @@ public class Tabs implements GeckoEventL
     private static final int SCORE_THRESHOLD = 30;
 
     private static AtomicInteger sTabId = new AtomicInteger(0);
 
     private GeckoApp mActivity;
     private ContentObserver mContentObserver;
 
     private Tabs() {
+        registerEventListener("Session:RestoreEnd");
         registerEventListener("SessionHistory:New");
         registerEventListener("SessionHistory:Back");
         registerEventListener("SessionHistory:Forward");
         registerEventListener("SessionHistory:Goto");
         registerEventListener("SessionHistory:Purge");
         registerEventListener("Tab:Added");
         registerEventListener("Tab:Close");
         registerEventListener("Tab:Select");
         registerEventListener("Content:LocationChange");
-        registerEventListener("Session:RestoreEnd");
         registerEventListener("DOMTitleChanged");
         registerEventListener("DOMLinkAdded");
         registerEventListener("DOMWindowClose");
     }
 
     public void attachToActivity(GeckoApp activity) {
         mActivity = activity;
         mAccountManager = AccountManager.get(mActivity);
@@ -270,73 +270,70 @@ public class Tabs implements GeckoEventL
     public static Tabs getInstance() {
        return Tabs.TabsInstanceHolder.INSTANCE;
     }
 
     // GeckoEventListener implementation
 
     public void handleMessage(String event, JSONObject message) {
         try {
-            if (event.startsWith("SessionHistory:")) {
-                Tab tab = getTab(message.getInt("tabID"));
-                if (tab != null) {
-                    event = event.substring("SessionHistory:".length());
-                    tab.handleSessionHistoryMessage(event, message);
-                }
-            } else if (event.equals("Tab:Added")) {
+            if (event.equals("Session:RestoreEnd")) {
+                notifyListeners(null, TabEvents.RESTORED);
+                return;
+            }
+
+            // All other events handled below should contain a tabID property
+            int id = message.getInt("tabID");
+            Tab tab = getTab(id);
+
+            // "Tab:Added" is a special case because tab will be null if the tab was just added
+            if (event.equals("Tab:Added")) {
                 String url = message.isNull("uri") ? null : message.getString("uri");
-                int id = message.getInt("tabID");
-                Tab tab = null;
 
                 if (message.getBoolean("stub")) {
-                    if (mTabs.containsKey(id)) {
-                        tab = mTabs.get(id);
-                        tab.updateURL(url);
-                    } else {
+                    if (tab == null) {
                         // Tab was already closed; abort
                         return;
                     }
+                    tab.updateURL(url);
                 } else {
                     tab = addTab(id, url, message.getBoolean("external"),
                                           message.getInt("parentId"),
                                           message.getString("title"),
                                           message.getBoolean("isPrivate"));
                 }
 
                 if (message.getBoolean("selected"))
-                    selectTab(tab.getId());
+                    selectTab(id);
                 if (message.getBoolean("delayLoad"))
                     tab.setState(Tab.STATE_DELAYED);
                 if (message.getBoolean("desktopMode"))
                     tab.setDesktopMode(true);
+                return;
+            }
+
+            // Tab was already closed; abort
+            if (tab == null)
+                return;
+
+            if (event.startsWith("SessionHistory:")) {
+                event = event.substring("SessionHistory:".length());
+                tab.handleSessionHistoryMessage(event, message);
             } else if (event.equals("Tab:Close")) {
-                Tab tab = getTab(message.getInt("tabID"));
                 closeTab(tab);
             } else if (event.equals("Tab:Select")) {
-                selectTab(message.getInt("tabID"));
+                selectTab(tab.getId());
             } else if (event.equals("Content:LocationChange")) {
-                Tab tab = getTab(message.getInt("tabID"));
-                if (tab != null) {
-                    tab.handleLocationChange(message);
-                }
-            } else if (event.equals("Session:RestoreEnd")) {
-                notifyListeners(null, TabEvents.RESTORED);
+                tab.handleLocationChange(message);
             } else if (event.equals("DOMTitleChanged")) {
-                Tab tab = getTab(message.getInt("tabID"));
-                if (tab != null) {
-                    tab.updateTitle(message.getString("title"));
-                }
+                tab.updateTitle(message.getString("title"));
             } else if (event.equals("DOMLinkAdded")) {
-                Tab tab = getTab(message.getInt("tabID"));
-                if (tab != null) {
-                    tab.updateFaviconURL(message.getString("href"), message.getInt("size"));
-                    notifyListeners(tab, TabEvents.LINK_ADDED);
-                }
+                tab.updateFaviconURL(message.getString("href"), message.getInt("size"));
+                notifyListeners(tab, TabEvents.LINK_ADDED);
             } else if (event.equals("DOMWindowClose")) {
-                Tab tab = getTab(message.getInt("tabID"));
                 closeTab(tab);
             }
         } catch (Exception e) { 
             Log.w(LOGTAG, "handleMessage threw for " + event, e);
         }
     }
 
     public void refreshThumbnails() {