Bug 1351739 - Part 0 - Use INVALID_TAB_ID more. r?sebastian,walkingice draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Thu, 06 Apr 2017 21:30:55 +0200
changeset 569632 354299e9e0e7181a83ece44be3498e88ddc2eefe
parent 569631 b8cdf728d44c5ac5a51c953805fdde11e6078130
child 569633 3b90fd287f5ba2b2b41b5879fda8672564baccb6
push id56240
push usermozilla@buttercookie.de
push dateThu, 27 Apr 2017 18:44:39 +0000
reviewerssebastian, walkingice
bugs1351739
milestone55.0a1
Bug 1351739 - Part 0 - Use INVALID_TAB_ID more. r?sebastian,walkingice -1 is probably not all that mysterious as far as magic numbers go, but still... MozReview-Commit-ID: zK3P6HeWzK
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
mobile/android/base/java/org/mozilla/gecko/Tabs.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -124,16 +124,18 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.TimeUnit;
 
+import static org.mozilla.gecko.Tabs.INVALID_TAB_ID;
+
 public abstract class GeckoApp
     extends GeckoActivity
     implements
     BundleEventListener,
     ContextGetter,
     GeckoAppShell.GeckoInterface,
     ScreenOrientationDelegate,
     GeckoMenu.Callback,
@@ -209,17 +211,17 @@ public abstract class GeckoApp
     private View mFullScreenPluginView;
 
     private final HashMap<String, PowerManager.WakeLock> mWakeLocks = new HashMap<String, PowerManager.WakeLock>();
 
     protected boolean mLastSessionCrashed;
     protected boolean mShouldRestore;
     private boolean mSessionRestoreParsingFinished = false;
 
-    protected int lastSelectedTabId = -1;
+    protected int lastSelectedTabId = INVALID_TAB_ID;
 
     private boolean foregrounded = false;
 
     private static final class LastSessionParser extends SessionParser {
         private JSONArray tabs;
         private JSONObject windowObject;
         private boolean isExternalURL;
 
@@ -237,17 +239,17 @@ public abstract class GeckoApp
             tabIdMap = new SparseIntArray();
         }
 
         public boolean allTabsSkipped() {
             return tabsWereSkipped && !tabsWereProcessed;
         }
 
         public int getNewTabId(int oldTabId) {
-            return tabIdMap.get(oldTabId, -1);
+            return tabIdMap.get(oldTabId, INVALID_TAB_ID);
         }
 
         @Override
         public void onTabRead(final SessionTab sessionTab) {
             if (sessionTab.isAboutHomeWithoutHistory()) {
                 // This is a tab pointing to about:home with no history. We won't restore
                 // this tab. If we end up restoring no tabs then the browser will decide
                 // whether it needs to open about:home or a different 'homepage'. If we'd
@@ -289,17 +291,17 @@ public abstract class GeckoApp
             ThreadUtils.postToUiThread(new Runnable() {
                 @Override
                 public void run() {
                     tab.updateTitle(sessionTab.getTitle());
                 }
             });
 
             try {
-                int oldTabId = tabObject.optInt("tabId", -1);
+                int oldTabId = tabObject.optInt("tabId", INVALID_TAB_ID);
                 int newTabId = tab.getId();
                 tabObject.put("tabId", newTabId);
                 if  (oldTabId >= 0) {
                     tabIdMap.put(oldTabId, newTabId);
                 }
             } catch (JSONException e) {
                 Log.e(LOGTAG, "JSON error", e);
             }
@@ -2197,46 +2199,46 @@ public abstract class GeckoApp
         if (!TextUtils.isEmpty(uri)) {
             passedUri = uri;
         } else {
             passedUri = null;
         }
 
         if (ACTION_LOAD.equals(action)) {
             Tabs.getInstance().loadUrl(intent.getDataString());
-            lastSelectedTabId = -1;
+            lastSelectedTabId = INVALID_TAB_ID;
         } else if (Intent.ACTION_VIEW.equals(action)) {
             processActionViewIntent(new Runnable() {
                 @Override
                 public void run() {
                     final String url = intent.getDataString();
                     int flags = Tabs.LOADURL_NEW_TAB | Tabs.LOADURL_USER_ENTERED | Tabs.LOADURL_EXTERNAL;
                     if (isFirstTab) {
                         flags |= Tabs.LOADURL_FIRST_AFTER_ACTIVITY_UNHIDDEN;
                     }
                     Tabs.getInstance().loadUrlWithIntentExtras(url, intent, flags);
                 }
             });
-            lastSelectedTabId = -1;
+            lastSelectedTabId = INVALID_TAB_ID;
         } else if (ACTION_HOMESCREEN_SHORTCUT.equals(action)) {
             mLayerView.loadUri(uri, GeckoView.LOAD_SWITCH_TAB);
         } else if (Intent.ACTION_SEARCH.equals(action)) {
             mLayerView.loadUri(uri, GeckoView.LOAD_NEW_TAB);
         } else if (NotificationHelper.HELPER_BROADCAST_ACTION.equals(action)) {
             NotificationHelper.getInstance(getApplicationContext()).handleNotificationIntent(intent);
         } else if (ACTION_LAUNCH_SETTINGS.equals(action)) {
             // Check if launched from data reporting notification.
             Intent settingsIntent = new Intent(GeckoApp.this, GeckoPreferences.class);
             // Copy extras.
             settingsIntent.putExtras(intent.getUnsafe());
             startActivity(settingsIntent);
         } else if (ACTION_SWITCH_TAB.equals(action)) {
-            final int tabId = intent.getIntExtra("TabId", -1);
+            final int tabId = intent.getIntExtra("TabId", INVALID_TAB_ID);
             Tabs.getInstance().selectTab(tabId);
-            lastSelectedTabId = -1;
+            lastSelectedTabId = INVALID_TAB_ID;
         }
 
         recordStartupActionTelemetry(passedUri, action);
     }
 
     /**
      * Handles getting a URI from an intent in a way that is backwards-
      * compatible with our previous implementations.
--- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java
@@ -216,17 +216,17 @@ public class Tabs implements BundleEvent
     }
 
     public int isOpen(String url) {
         for (Tab tab : mOrder) {
             if (tab.getURL().equals(url)) {
                 return tab.getId();
             }
         }
-        return -1;
+        return INVALID_TAB_ID;
     }
 
     // Must be synchronized to avoid racing on mBookmarksContentObserver.
     private void lazyRegisterBookmarkObserver() {
         if (mBookmarksContentObserver == null) {
             mBookmarksContentObserver = new ContentObserver(null) {
                 @Override
                 public void onChange(boolean selfChange) {
@@ -384,17 +384,17 @@ public class Tabs implements BundleEvent
 
     public boolean isSelectedTabId(int tabId) {
         final Tab selected = mSelectedTab;
         return selected != null && selected.getId() == tabId;
     }
 
     @RobocopTarget
     public synchronized Tab getTab(int id) {
-        if (id == -1)
+        if (id == INVALID_TAB_ID)
             return null;
 
         if (mTabs.size() == 0)
             return null;
 
         if (!mTabs.containsKey(id))
            return null;
 
@@ -547,17 +547,17 @@ public class Tabs implements BundleEvent
                         notifyListeners(tab, TabEvents.LOCATION_CHANGE, tab.getURL());
                     }
                 }
             });
             return;
         }
 
         // All other events handled below should contain a tabID property
-        final int id = message.getInt("tabID", -1);
+        final int id = message.getInt("tabID", INVALID_TAB_ID);
         Tab tab = getTab(id);
 
         // "Tab:Added" is a special case because tab will be null if the tab was just added
         if ("Tab:Added".equals(event)) {
             String url = message.getString("uri");
 
             if (message.getBoolean("cancelEditMode")) {
                 final Tab oldTab = getSelectedTab();
@@ -686,17 +686,17 @@ public class Tabs implements BundleEvent
             if (status.equals("resume")) {
                 notifyListeners(tab, TabEvents.MEDIA_PLAYING_RESUME);
             } else {
                 tab.setIsMediaPlaying(status.equals("start"));
                 notifyListeners(tab, TabEvents.MEDIA_PLAYING_CHANGE);
             }
 
         } else if ("Tab:SetParentId".equals(event)) {
-            tab.setParentId(message.getInt("parentID", -1));
+            tab.setParentId(message.getInt("parentID", INVALID_TAB_ID));
         }
     }
 
     public void refreshThumbnails() {
         final BrowserDB db = BrowserDB.from(mAppContext);
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
@@ -926,43 +926,43 @@ public class Tabs implements BundleEvent
      *
      * @param url   URL of page to load
      * @param flags flags used to load tab
      *
      * @return      the Tab if a new one was created; null otherwise
      */
     @RobocopTarget
     public Tab loadUrl(String url, int flags) {
-        return loadUrl(url, null, -1, null, flags);
+        return loadUrl(url, null, INVALID_TAB_ID, null, flags);
     }
 
     public Tab loadUrlWithIntentExtras(final String url, final SafeIntent intent, final int flags) {
         // We can't directly create a listener to tell when the user taps on the "What's new"
         // notification, so we use this intent handling as a signal that they tapped the notification.
         if (intent.getBooleanExtra(WhatsNewReceiver.EXTRA_WHATSNEW_NOTIFICATION, false)) {
             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.NOTIFICATION,
                     WhatsNewReceiver.EXTRA_WHATSNEW_NOTIFICATION);
         }
 
         // Note: we don't get the URL from the intent so the calling
         // method has the opportunity to change the URL if applicable.
-        return loadUrl(url, null, -1, intent, flags);
+        return loadUrl(url, null, INVALID_TAB_ID, intent, flags);
     }
 
     public Tab loadUrl(final String url, final String searchEngine, final int parentId, final int flags) {
         return loadUrl(url, searchEngine, parentId, null, flags);
     }
 
     /**
      * Loads a tab with the given URL.
      *
      * @param url          URL of page to load, or search term used if searchEngine is given
      * @param searchEngine if given, the search engine with this name is used
      *                     to search for the url string; if null, the URL is loaded directly
-     * @param parentId     ID of this tab's parent, or -1 if it has no parent
+     * @param parentId     ID of this tab's parent, or INVALID_TAB_ID (-1) if it has no parent
      * @param intent       an intent whose extras are used to modify the request
      * @param flags        flags used to load tab
      *
      * @return             the Tab if a new one was created; null otherwise
      */
     public Tab loadUrl(final String url, final String searchEngine, final int parentId,
                    final SafeIntent intent, final int flags) {
         final GeckoBundle data = new GeckoBundle();
@@ -1093,17 +1093,17 @@ public class Tabs implements BundleEvent
                 selectTab(tab.getId());
                 return;
             }
         }
 
         // getSelectedTab() can return null if no tab has been created yet
         // (i.e., we're restoring a session after a crash). In these cases,
         // don't mark any tabs as a parent.
-        int parentId = -1;
+        int parentId = INVALID_TAB_ID;
         int flags = LOADURL_NEW_TAB;
 
         final Tab selectedTab = getSelectedTab();
         if (selectedTab != null) {
             parentId = selectedTab.getId();
             if (selectedTab.isPrivate()) {
                 flags = flags | LOADURL_PRIVATE;
             }