Bug 1346004 - Part 1 - Keep track of a tab type on the Java tab object. r?sebastian draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 11 Mar 2017 20:04:07 +0100
changeset 502489 c2231bc42ec48cf4645d1cf2a8fb50ef4f1b810d
parent 502488 269b0d125bd9880f9f523eb33e6f970cd0bceca6
child 502490 355a2c876b8affd1b09ac97a40905a325b04096b
push id50298
push usermozilla@buttercookie.de
push dateTue, 21 Mar 2017 21:08:11 +0000
reviewerssebastian
bugs1346004
milestone55.0a1
Bug 1346004 - Part 1 - Keep track of a tab type on the Java tab object. r?sebastian As long as the tabs are opened in the same Gecko browser window, splitting the Java UI tabs list into multiple parts breaks too many assumptions, so the easier solution is to allow setting a type attribute on each tab object, which will allow filtering of them later on. MozReview-Commit-ID: 1PbxMkWTK47
mobile/android/base/java/org/mozilla/gecko/PrivateTab.java
mobile/android/base/java/org/mozilla/gecko/Tab.java
mobile/android/base/java/org/mozilla/gecko/Tabs.java
mobile/android/chrome/content/browser.js
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testFilterOpenTab.java
--- a/mobile/android/base/java/org/mozilla/gecko/PrivateTab.java
+++ b/mobile/android/base/java/org/mozilla/gecko/PrivateTab.java
@@ -6,18 +6,18 @@
 package org.mozilla.gecko;
 
 import android.content.Context;
 
 import org.json.JSONObject;
 import org.mozilla.gecko.db.BrowserDB;
 
 public class PrivateTab extends Tab {
-    public PrivateTab(Context context, int id, String url, boolean external, int parentId, String title) {
-        super(context, id, url, external, parentId, title);
+    public PrivateTab(Context context, int id, String url, boolean external, int parentId, String title, TabType type) {
+        super(context, id, url, external, parentId, title, type);
     }
 
     @Override
     protected void saveThumbnailToDB(final BrowserDB db) {}
 
     @Override
     public void setMetadata(JSONObject metadata) {}
 
--- a/mobile/android/base/java/org/mozilla/gecko/Tab.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tab.java
@@ -38,16 +38,17 @@ import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
 
 public class Tab {
     private static final String LOGTAG = "GeckoTab";
 
     private static Pattern sColorPattern;
     private final int mId;
+    private TabType mType;
     private final BrowserDB mDB;
     private long mLastUsed;
     private String mUrl;
     private String mBaseDomain;
     private String mUserRequested; // The original url requested. May be typed by the user or sent by an extneral app for example.
     private String mTitle;
     private Bitmap mFavicon;
     private String mFaviconUrl;
@@ -113,20 +114,21 @@ public class Tab {
 
     public enum ErrorType {
         CERT_ERROR,  // Pages with certificate problems
         BLOCKED,     // Pages blocked for phishing or malware warnings
         NET_ERROR,   // All other types of error
         NONE         // Non error pages
     }
 
-    public Tab(Context context, int id, String url, boolean external, int parentId, String title) {
+    public Tab(Context context, int id, String url, boolean external, int parentId, String title, TabType type) {
         mAppContext = context.getApplicationContext();
         mDB = BrowserDB.from(context);
         mId = id;
+        mType = type;
         mUrl = url;
         mBaseDomain = "";
         mUserRequested = "";
         mExternal = external;
         mParentId = parentId;
         mTitle = title == null ? "" : title;
         mSiteIdentity = new SiteIdentity();
         mContentType = "";
@@ -760,16 +762,26 @@ public class Tab {
     public boolean getDesktopMode() {
         return mDesktopMode;
     }
 
     public boolean isPrivate() {
         return false;
     }
 
+    public TabType getType() {
+        return mType;
+    }
+
+    public enum TabType {
+        BROWSING,
+        CUSTOMTAB,
+        WEBAPP
+    }
+
     /**
      * Sets the tab load progress to the given percentage.
      *
      * @param progressPercentage Percentage to set progress to (0-100)
      */
     void setLoadProgress(int progressPercentage) {
         mLoadProgress = progressPercentage;
     }
--- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java
@@ -37,16 +37,18 @@ import android.database.sqlite.SQLiteExc
 import android.graphics.Color;
 import android.net.Uri;
 import android.os.Handler;
 import android.provider.Browser;
 import android.support.annotation.UiThread;
 import android.support.v4.content.ContextCompat;
 import android.util.Log;
 
+import static org.mozilla.gecko.Tab.TabType;
+
 public class Tabs implements BundleEventListener {
     private static final String LOGTAG = "GeckoTabs";
 
     // mOrder and mTabs are always of the same cardinality, and contain the same values.
     private volatile CopyOnWriteArrayList<Tab> mOrder = new CopyOnWriteArrayList<Tab>();
 
     // A cache that maps a tab ID to an mOrder tab position.  All access should be synchronized.
     private final TabPositionCache tabPositionCache = new TabPositionCache();
@@ -225,19 +227,19 @@ public class Tabs implements BundleEvent
             };
 
             // It's safe to use the db here since we aren't doing any I/O.
             final GeckoProfile profile = GeckoProfile.get(mAppContext);
             BrowserDB.from(profile).registerBookmarkObserver(getContentResolver(), mBookmarksContentObserver);
         }
     }
 
-    private Tab addTab(int id, String url, boolean external, int parentId, String title, boolean isPrivate, int tabIndex) {
-        final Tab tab = isPrivate ? new PrivateTab(mAppContext, id, url, external, parentId, title) :
-                                    new Tab(mAppContext, id, url, external, parentId, title);
+    private Tab addTab(int id, String url, boolean external, int parentId, String title, boolean isPrivate, int tabIndex, TabType type) {
+        final Tab tab = isPrivate ? new PrivateTab(mAppContext, id, url, external, parentId, title, type) :
+                                    new Tab(mAppContext, id, url, external, parentId, title, type);
         synchronized (this) {
             lazyRegisterBookmarkObserver();
             mTabs.put(id, tab);
 
             if (tabIndex > -1) {
                 mOrder.add(tabIndex, tab);
                 if (tabPositionCache.mOrderPosition >= tabIndex) {
                     tabPositionCache.mTabId = INVALID_TAB_ID;
@@ -530,17 +532,18 @@ public class Tabs implements BundleEvent
                     // Tab was already closed; abort
                     return;
                 }
             } else {
                 tab = addTab(id, url, message.getBoolean("external"),
                                       message.getInt("parentId"),
                                       message.getString("title"),
                                       message.getBoolean("isPrivate"),
-                                      message.getInt("tabIndex"));
+                                      message.getInt("tabIndex"),
+                                      TabType.valueOf(message.getString("tabType")));
                 // If we added the tab as a stub, we should have already
                 // selected it, so ignore this flag for stubbed tabs.
                 if (message.getBoolean("selected"))
                     selectTab(id);
             }
 
             if (message.getBoolean("delayLoad"))
                 tab.setState(Tab.STATE_DELAYED);
@@ -865,27 +868,27 @@ public class Tabs implements BundleEvent
         }
 
         return null;
     }
 
     /**
      * Loads a tab with the given URL in the currently selected tab.
      *
-     * @param url URL of page to load, or search term used if searchEngine is given
+     * @param url URL of page to load
      */
     @RobocopTarget
     public Tab loadUrl(String url) {
         return loadUrl(url, LOADURL_NONE);
     }
 
     /**
      * Loads a tab with the given URL.
      *
-     * @param url   URL of page to load, or search term used if searchEngine is given
+     * @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);
     }
@@ -929,16 +932,17 @@ public class Tabs implements BundleEvent
         boolean background = delayLoad || (flags & LOADURL_BACKGROUND) != 0;
 
         boolean isPrivate = (flags & LOADURL_PRIVATE) != 0;
         boolean userEntered = (flags & LOADURL_USER_ENTERED) != 0;
         boolean desktopMode = (flags & LOADURL_DESKTOP) != 0;
         boolean external = (flags & LOADURL_EXTERNAL) != 0;
         final boolean isFirstShownAfterActivityUnhidden = (flags & LOADURL_FIRST_AFTER_ACTIVITY_UNHIDDEN) != 0;
         final boolean customTab = (flags & LOADURL_CUSTOMTAB) != 0;
+        final TabType type = customTab ? TabType.CUSTOMTAB : TabType.BROWSING;
 
         data.putString("url", url);
         data.putString("engine", searchEngine);
         data.putInt("parentId", parentId);
         data.putBoolean("userEntered", userEntered);
         data.putBoolean("isPrivate", isPrivate);
         data.putBoolean("pinned", (flags & LOADURL_PINNED) != 0);
         data.putBoolean("desktopMode", desktopMode);
@@ -986,17 +990,17 @@ public class Tabs implements BundleEvent
             // The URL is updated for the tab once Gecko responds with the
             // Tab:Added data. We can preliminarily set the tab's URL as
             // long as it's a valid URI.
             String tabUrl = (url != null && Uri.parse(url).getScheme() != null) ? url : null;
 
             // Add the new tab to the end of the tab order.
             final int tabIndex = -1;
 
-            tabToSelect = addTab(tabId, tabUrl, external, parentId, url, isPrivate, tabIndex);
+            tabToSelect = addTab(tabId, tabUrl, external, parentId, url, isPrivate, tabIndex, type);
             tabToSelect.setDesktopMode(desktopMode);
             tabToSelect.setApplicationId(applicationId);
             if (isFirstShownAfterActivityUnhidden) {
                 // We just opened Firefox so we want to show
                 // the toolbar but not animate it to avoid jank.
                 tabToSelect.setShouldShowToolbarWithoutAnimationOnFirstSelection(true);
             }
         }
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -3573,16 +3573,17 @@ Tab.prototype = {
 
       this.desktopMode = ("desktopMode" in aParams) ? aParams.desktopMode : false;
       this._parentId = ("parentId" in aParams && typeof aParams.parentId == "number")
                       ? aParams.parentId : -1;
 
       let message = {
         type: "Tab:Added",
         tabID: this.id,
+        tabType: "BROWSING",
         uri: truncate(uri, MAX_URI_LENGTH),
         parentId: this.parentId,
         tabIndex: ("tabIndex" in aParams) ? aParams.tabIndex : -1,
         external: ("external" in aParams) ? aParams.external : false,
         selected: ("selected" in aParams || aParams.cancelEditMode === true)
                   ? aParams.selected !== false || aParams.cancelEditMode === true : true,
         cancelEditMode: aParams.cancelEditMode === true,
         title: truncate(title, MAX_TITLE_LENGTH),
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testFilterOpenTab.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testFilterOpenTab.java
@@ -12,16 +12,18 @@ import org.mozilla.gecko.PrivateTab;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.db.BrowserContract;
 import org.mozilla.gecko.db.TabsProvider;
 
 import android.content.ContentProvider;
 import android.content.Context;
 import android.database.Cursor;
 
+import static org.mozilla.gecko.Tab.TabType;
+
 /**
  * Tests that local tabs are filtered prior to upload.
  * - create a set of tabs and persists them through TabsAccessor.
  * - verifies that tabs are filtered by querying.
  */
 public class testFilterOpenTab extends ContentProviderTest {
     private static final String[] TABS_PROJECTION_COLUMNS = new String[] {
                                                                 BrowserContract.Tabs.TITLE,
@@ -49,21 +51,21 @@ public class testFilterOpenTab extends C
         return mProvider.query(BrowserContract.Tabs.CONTENT_URI,
                                TABS_PROJECTION_COLUMNS,
                                LOCAL_TABS_SELECTION,
                                null,
                                null);
     }
 
     private Tab createTab(int id, String url, boolean external, int parentId, String title) {
-        return new Tab((Context) getActivity(), id, url, external, parentId, title);
+        return new Tab((Context) getActivity(), id, url, external, parentId, title, TabType.BROWSING);
     }
 
     private Tab createPrivateTab(int id, String url, boolean external, int parentId, String title) {
-        return new PrivateTab((Context) getActivity(), id, url, external, parentId, title);
+        return new PrivateTab((Context) getActivity(), id, url, external, parentId, title, TabType.BROWSING);
     }
 
     @Override
     public void setUp() throws Exception {
         super.setUp(sTabProviderCallable, BrowserContract.TABS_AUTHORITY, "tabs.db");
         mTests.add(new TestInsertLocalTabs());
     }