196 lines
10 KiB
Diff
196 lines
10 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: fgei <fgei@gmail.com>
|
|
Date: Sun, 19 Jun 2022 06:39:48 +0000
|
|
Subject: [PATCH] Toggle for navigating external URL in incognito
|
|
|
|
---
|
|
.../java/res/xml/privacy_preferences_ext.xml | 5 +++
|
|
.../chrome/browser/ChromeTabbedActivity.java | 2 ++
|
|
.../browser/LaunchIntentDispatcher.java | 3 ++
|
|
.../chrome/browser/TabPreferencesUtils.java | 32 +++++++++++++++++++
|
|
.../privacy/settings/PrivacySettingsExt.java | 17 ++++++++++
|
|
.../browser/preferences/SharedPrefsUtils.java | 5 ++-
|
|
.../strings/android_chrome_ext_strings.grdp | 6 ++++
|
|
7 files changed, 69 insertions(+), 1 deletion(-)
|
|
|
|
diff --git a/chrome/android/java/res/xml/privacy_preferences_ext.xml b/chrome/android/java/res/xml/privacy_preferences_ext.xml
|
|
index ea1dd6c3f529c..4f6b5a3ae75a5 100644
|
|
--- a/chrome/android/java/res/xml/privacy_preferences_ext.xml
|
|
+++ b/chrome/android/java/res/xml/privacy_preferences_ext.xml
|
|
@@ -15,5 +15,10 @@ that can be found in the LICENSE file.
|
|
android:title="@string/close_tabs_on_exit_title"
|
|
android:summary="@string/close_tabs_on_exit_summary"
|
|
android:persistent="false"/>
|
|
+ <org.chromium.components.browser_ui.settings.ChromeSwitchPreference
|
|
+ android:key="open_links_in_incognito"
|
|
+ android:title="@string/open_links_in_incognito_title"
|
|
+ android:summary="@string/open_links_in_incognito_summary"
|
|
+ android:persistent="false"/>
|
|
</PreferenceScreen>
|
|
|
|
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
|
|
index f595aa4e1b231..2950cc2c8b81b 100644
|
|
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
|
|
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
|
|
@@ -2373,6 +2373,8 @@ public class ChromeTabbedActivity extends ChromeActivity<ChromeActivityComponent
|
|
// - we decided to close the tab, but it was opened by an external app, so we will go
|
|
// exit Chrome on top of closing the tab
|
|
final boolean minimizeApp =
|
|
+ (currentTab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP &&
|
|
+ TabPreferencesUtils.shouldOpenLinksInIncognito()) ||
|
|
!shouldCloseTab || TabAssociatedApp.isOpenedFromExternalApp(currentTab);
|
|
|
|
BackPressManager.record(BackPressHandler.Type.MINIMIZE_APP_AND_CLOSE_TAB);
|
|
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
|
|
index aa44a679bee3c..2f755a8aee5c7 100644
|
|
--- a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
|
|
+++ b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
|
|
@@ -453,6 +453,9 @@ public class LaunchIntentDispatcher implements IntentHandler.IntentHandlerDelega
|
|
|
|
if (Intent.ACTION_VIEW.equals(newIntent.getAction())
|
|
&& !IntentHandler.wasIntentSenderChrome(newIntent)) {
|
|
+ if (TabPreferencesUtils.shouldOpenLinksInIncognito()) {
|
|
+ newIntent = TabPreferencesUtils.createIncognitoIntent(mActivity, newIntent);
|
|
+ }
|
|
if (!chromeTabbedTaskExists()) {
|
|
newIntent.putExtra(IntentHandler.EXTRA_STARTED_TABBED_CHROME_TASK, true);
|
|
}
|
|
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/TabPreferencesUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/TabPreferencesUtils.java
|
|
index e004200c3102c..91fee9ee6bef3 100644
|
|
--- a/chrome/android/java/src/org/chromium/chrome/browser/TabPreferencesUtils.java
|
|
+++ b/chrome/android/java/src/org/chromium/chrome/browser/TabPreferencesUtils.java
|
|
@@ -4,10 +4,19 @@
|
|
|
|
package org.chromium.chrome.browser;
|
|
|
|
+import android.content.Context;
|
|
+import android.provider.Browser;
|
|
+import android.content.Intent;
|
|
+
|
|
+import androidx.annotation.NonNull;
|
|
+
|
|
+import org.chromium.base.IntentUtils;
|
|
import org.chromium.base.ThreadUtils;
|
|
+import org.chromium.chrome.browser.document.ChromeLauncherActivity;
|
|
import org.chromium.chrome.browser.preferences.Pref;
|
|
import org.chromium.chrome.browser.preferences.SharedPrefsUtils.SharedPrefsExt;
|
|
import org.chromium.chrome.browser.profiles.Profile;
|
|
+import org.chromium.chrome.browser.tab.TabLaunchType;
|
|
import org.chromium.components.prefs.PrefService;
|
|
import org.chromium.components.user_prefs.UserPrefs;
|
|
|
|
@@ -21,4 +30,27 @@ public final class TabPreferencesUtils {
|
|
return SharedPrefsExt.CLOSE_TABS_ON_EXIT.get();
|
|
}
|
|
|
|
+ public static boolean shouldOpenLinksInIncognito() {
|
|
+ return SharedPrefsExt.OPEN_LINKS_IN_INCOGNITO.get();
|
|
+ }
|
|
+
|
|
+ static Intent createIncognitoIntent(Context context, @NonNull Intent intent) {
|
|
+ Intent newIntent = new Intent();
|
|
+ newIntent.setAction(intent.getAction());
|
|
+ newIntent.setData(intent.getData());
|
|
+ newIntent.putExtras(intent);
|
|
+ newIntent.setClass(context, ChromeLauncherActivity.class);
|
|
+ newIntent.putExtra(Browser.EXTRA_CREATE_NEW_TAB, true);
|
|
+ IntentHandler.setTabLaunchType(newIntent, TabLaunchType.FROM_EXTERNAL_APP);
|
|
+ newIntent = appendNeededIncognitoExtras(context, newIntent);
|
|
+ return newIntent;
|
|
+ }
|
|
+
|
|
+ public static Intent appendNeededIncognitoExtras(Context context, @NonNull Intent intent) {
|
|
+ intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName());
|
|
+ intent.putExtra(IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, true);
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
+ IntentUtils.addTrustedIntentExtras(intent);
|
|
+ return intent;
|
|
+ }
|
|
}
|
|
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java
|
|
index e702d36ea82a7..e75b5cd5a534a 100644
|
|
--- a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java
|
|
+++ b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java
|
|
@@ -27,6 +27,8 @@ final class PrivacySettingsExt {
|
|
|
|
private static final String PREF_SEARCH_SUGGESTIONS = "search_suggestions";
|
|
private static final String PREF_CLOSE_TABS_ON_EXIT = SharedPrefsExt.CLOSE_TABS_ON_EXIT.getKey();
|
|
+ private static final String PREF_OPEN_LINKS_IN_INCOGNITO =
|
|
+ SharedPrefsExt.OPEN_LINKS_IN_INCOGNITO.getKey();
|
|
|
|
private static final Preference.OnPreferenceChangeListener LISTENER =
|
|
(pref, val) -> {
|
|
@@ -39,6 +41,8 @@ final class PrivacySettingsExt {
|
|
prefService.setBoolean(Pref.SEARCH_SUGGEST_ENABLED, (boolean) val);
|
|
} else if (PREF_CLOSE_TABS_ON_EXIT.equals(key)) {
|
|
SharedPrefsExt.CLOSE_TABS_ON_EXIT.put((boolean) val);
|
|
+ } else if (PREF_OPEN_LINKS_IN_INCOGNITO.equals(key)) {
|
|
+ SharedPrefsExt.OPEN_LINKS_IN_INCOGNITO.put((boolean) val);
|
|
}
|
|
return true;
|
|
};
|
|
@@ -83,6 +87,13 @@ final class PrivacySettingsExt {
|
|
closeTabsOnExitPref.setOrder(PRIVACY_PREFERENCES_ORDER);
|
|
closeTabsOnExitPref.setOnPreferenceChangeListener(LISTENER);
|
|
}
|
|
+
|
|
+ ChromeSwitchPreference openLinksInIncognitoPref =
|
|
+ (ChromeSwitchPreference) prefFragment.findPreference(PREF_OPEN_LINKS_IN_INCOGNITO);
|
|
+ if (openLinksInIncognitoPref != null) {
|
|
+ openLinksInIncognitoPref.setOrder(PRIVACY_PREFERENCES_ORDER);
|
|
+ openLinksInIncognitoPref.setOnPreferenceChangeListener(LISTENER);
|
|
+ }
|
|
}
|
|
|
|
static void updatePreferences(@NonNull PreferenceFragmentCompat prefFragment) {
|
|
@@ -99,5 +110,11 @@ final class PrivacySettingsExt {
|
|
SettingsExtUtils.safelyUpdateSwitchPreference(/* switchPref */ closeTabsOnExitPref,
|
|
/* newSummary*/ null,
|
|
/* newCheckedValue*/ SharedPrefsExt.CLOSE_TABS_ON_EXIT.get());
|
|
+
|
|
+ ChromeSwitchPreference openLinksInIncognitoPref =
|
|
+ (ChromeSwitchPreference) prefFragment.findPreference(PREF_OPEN_LINKS_IN_INCOGNITO);
|
|
+ SettingsExtUtils.safelyUpdateSwitchPreference(/* switchPref */ openLinksInIncognitoPref,
|
|
+ /* newSummary*/ null,
|
|
+ /* newCheckedValue*/ SharedPrefsExt.OPEN_LINKS_IN_INCOGNITO.get());
|
|
}
|
|
}
|
|
diff --git a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
|
index 8fc7dde1db697..c2ab10e14c29a 100644
|
|
--- a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
|
+++ b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
|
@@ -138,6 +138,8 @@ public final class SharedPrefsUtils {
|
|
public static class SharedPrefsExt {
|
|
public static final BoolSharedPref CLOSE_TABS_ON_EXIT =
|
|
new BoolSharedPref("close_tabs_on_exit", false);
|
|
+ public static final BoolSharedPref OPEN_LINKS_IN_INCOGNITO =
|
|
+ new BoolSharedPref("open_links_in_incognito", false);
|
|
}
|
|
|
|
static SharedPreferencesManager getSharedPrefManager() {
|
|
@@ -148,7 +150,8 @@ public final class SharedPrefsUtils {
|
|
static boolean isKeyInUse(String key) {
|
|
// clang-format off
|
|
return Arrays.asList(
|
|
- SharedPrefsExt.CLOSE_TABS_ON_EXIT.getKey()
|
|
+ SharedPrefsExt.CLOSE_TABS_ON_EXIT.getKey(),
|
|
+ SharedPrefsExt.OPEN_LINKS_IN_INCOGNITO.getKey()
|
|
).contains(key);
|
|
// clang-format on
|
|
}
|
|
diff --git a/chrome/browser/ui/android/strings/android_chrome_ext_strings.grdp b/chrome/browser/ui/android/strings/android_chrome_ext_strings.grdp
|
|
index a1018b43620cd..52b9ca2042bee 100644
|
|
--- a/chrome/browser/ui/android/strings/android_chrome_ext_strings.grdp
|
|
+++ b/chrome/browser/ui/android/strings/android_chrome_ext_strings.grdp
|
|
@@ -9,4 +9,10 @@
|
|
<message name="IDS_CLOSE_TABS_ON_EXIT_SUMMARY" desc="Summary of closing tabs on exit menu item">
|
|
Don't persist tabs between browsing sessions
|
|
</message>
|
|
+ <message name="IDS_OPEN_LINKS_IN_INCOGNITO_TITLE" desc="Title of opening external links in incognito tabs menu item">
|
|
+ Open external links in incognito
|
|
+ </message>
|
|
+ <message name="IDS_OPEN_LINKS_IN_INCOGNITO_SUMMARY" desc="Summary of opening external links in incognito tabs menu item">
|
|
+ Open links navigated by external apps in incognito tabs
|
|
+ </message>
|
|
</grit-part>
|