From 4a7d276bc05003f98a5444daa192065df067a8ba Mon Sep 17 00:00:00 2001 From: Matthew Finkel Date: Mon, 27 May 2019 15:55:33 +0000 Subject: [PATCH] Bug 24920 - Only create Private tabs if browser.privatebrowsing.autostart is true --- .../java/org/mozilla/gecko/BrowserApp.java | 9 ++++++ .../base/java/org/mozilla/gecko/GeckoApp.java | 31 +++++++++++++++++++ .../base/java/org/mozilla/gecko/Tabs.java | 7 ++++- .../org/mozilla/gecko/tabs/TabsPanel.java | 4 +++ mobile/android/chrome/content/browser.js | 7 ++++- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java index 47e9e9c14fd06..d99baf371b9fd 100644 --- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java +++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java @@ -3315,6 +3315,15 @@ public class BrowserApp extends GeckoApp share.setActionProvider(provider); + // Change visibility of new_tab. This may be called before + // mOnlyPrivateTabs is set in GeckoApp::onCreate(). If the PrefHelper + // callback was not already called, then the visibility will be correctly set + // in the callback. + MenuItem newTabMenuItem = mMenu.findItem(R.id.new_tab); + if (newTabMenuItem != null) { + newTabMenuItem.setVisible(mOnlyPrivateTabs == false); + } + return true; } diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java index 7c8aea696c3aa..fcea7e79fb213 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java @@ -167,6 +167,8 @@ public abstract class GeckoApp extends GeckoActivity */ public static final String PREFS_IS_FIRST_RUN = "telemetry-isFirstRun"; + public static final String PREFS_PRIVATEBROWSING_AUTOSTART = "browser.privatebrowsing.autostart"; + public static final String SAVED_STATE_IN_BACKGROUND = "inBackground"; public static final String SAVED_STATE_PRIVATE_SESSION = "privateSession"; /** @@ -354,6 +356,8 @@ public abstract class GeckoApp extends GeckoActivity private boolean mPrivateBrowsingSessionOutdated; private static final int MAX_PRIVATE_TABS_UPDATE_WAIT_MSEC = 500; + protected boolean mOnlyPrivateTabs = false; + private volatile HealthRecorder mHealthRecorder; private volatile Locale mLastLocale; @@ -1154,6 +1158,28 @@ public abstract class GeckoApp extends GeckoActivity "ToggleChrome:Show", null); + PrefsHelper.getPref(PREFS_PRIVATEBROWSING_AUTOSTART, + new PrefsHelper.PrefHandlerBase() { + @Override public void prefValue(String pref, boolean value) { + if (pref != PREFS_PRIVATEBROWSING_AUTOSTART) { + return; + } + + mOnlyPrivateTabs = value; + + // Change visibility here in case mMenu is initialized. If it is not initialized, + // then the visibility is set in BrowserApp::onCreateOptionsMenu(). + if (mMenu != null) { + MenuItem newTabMenuItem = mMenu.findItem(R.id.new_tab); + if (newTabMenuItem != null) { + newTabMenuItem.setVisible(mOnlyPrivateTabs == false); + } + } + + Tabs.getInstance().setOnlyPrivateTabs(mOnlyPrivateTabs); + } + }); + Tabs.getInstance().attachToContext(this, mLayerView, getAppEventDispatcher()); Tabs.registerOnTabsChangedListener(this); @@ -1192,6 +1218,7 @@ public abstract class GeckoApp extends GeckoActivity } // If we are doing a restore, read the session data so we can send it to Gecko later. + GeckoBundle restoreMessage = null; if (!mIsRestoringActivity && mShouldRestore) { final boolean isExternalURL = invokedWithExternalURL(getIntentURI(new SafeIntent(getIntent()))); @@ -2285,6 +2312,10 @@ public abstract class GeckoApp extends GeckoActivity } } + public boolean isOnlyPrivateTabs() { + return mOnlyPrivateTabs; + } + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) protected void finishAndShutdown(final boolean restart) { ThreadUtils.assertOnUiThread(); diff --git a/mobile/android/base/java/org/mozilla/gecko/Tabs.java b/mobile/android/base/java/org/mozilla/gecko/Tabs.java index 068d8cb7ca289..30c75b68aad8e 100644 --- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java +++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java @@ -106,6 +106,7 @@ public class Tabs implements BundleEventListener { private ContentObserver mBookmarksContentObserver; private PersistTabsRunnable mPersistTabsRunnable; private int mPrivateClearColor; + private boolean mOnlyPrivateTabs = true; // Close all tabs including normal and private tabs. @RobocopTarget @@ -211,6 +212,10 @@ public class Tabs implements BundleEventListener { } } + public synchronized void setOnlyPrivateTabs(boolean onlyPrivateTabs) { + mOnlyPrivateTabs = onlyPrivateTabs; + } + public void detachFromContext() { mGeckoView = null; } @@ -1026,7 +1031,7 @@ public class Tabs implements BundleEventListener { // delayLoad implies background tab boolean background = delayLoad || (flags & LOADURL_BACKGROUND) != 0; - boolean isPrivate = (flags & LOADURL_PRIVATE) != 0 || (intent != null && intent.getBooleanExtra(PRIVATE_TAB_INTENT_EXTRA, false)); + boolean isPrivate = (flags & LOADURL_PRIVATE) != 0 || (intent != null && intent.getBooleanExtra(PRIVATE_TAB_INTENT_EXTRA, false)) || mOnlyPrivateTabs; boolean userEntered = (flags & LOADURL_USER_ENTERED) != 0; boolean desktopMode = (flags & LOADURL_DESKTOP) != 0; boolean external = (flags & LOADURL_EXTERNAL) != 0; diff --git a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java index 08c107ebb62d8..fc27ac03c9293 100644 --- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java +++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsPanel.java @@ -167,6 +167,10 @@ public class TabsPanel extends LinearLayout final View tabNormal = mTabWidget.addTab(R.drawable.tabs_normal, R.string.tabs_normal); mNormalTabsPanel = tabNormal instanceof ThemedImageButton ? ((ThemedImageButton) tabNormal) : null; + if (mActivity.isOnlyPrivateTabs()) { + tabNormal.setVisibility(View.GONE); + } + final View tabPrivate = mTabWidget.addTab(R.drawable.tabs_private, R.string.tabs_private); mPrivateTabsPanel = tabPrivate instanceof ThemedImageButton ? ((ThemedImageButton) tabPrivate) : null; if (mPrivateTabsPanel != null) { diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index 580a8ed2d98cd..49b3f527567be 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -4410,7 +4410,12 @@ Tab.prototype = { // Must be called after appendChild so the docShell has been created. this.setActive(false); - let isPrivate = "isPrivate" in aParams && aParams.isPrivate; + // Create a Private tab if it was explicitly requested or if the pref + // |browser.privatebrowsing.autostart| is true. If the pref is true, then all tabs are created + // as private tabs. The first clause covers new tabs requested by a user. The preference check + // is important because extensions directly open tabs (usually not private tabs) and bypass the + // private tab checks. + let isPrivate = (("isPrivate" in aParams) && aParams.isPrivate) || Services.prefs.getBoolPref("browser.privatebrowsing.autostart"); if (isPrivate) { attrs.privateBrowsingId = 1; } -- GitLab