LeOSium_webview/mulch/patches/0001-Vanadium/0088-Toggle-for-navigating-...

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
---
.../shared_preferences/SharedPrefsUtils.java | 5 ++-
.../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 ++++++++++
.../strings/android_chrome_ext_strings.grdp | 6 ++++
7 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/base/android/java/src/org/chromium/base/shared_preferences/SharedPrefsUtils.java b/base/android/java/src/org/chromium/base/shared_preferences/SharedPrefsUtils.java
index 84784f7ec174c..0bacc94f580dc 100644
--- a/base/android/java/src/org/chromium/base/shared_preferences/SharedPrefsUtils.java
+++ b/base/android/java/src/org/chromium/base/shared_preferences/SharedPrefsUtils.java
@@ -122,6 +122,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() {
@@ -132,7 +134,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/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 e269a2bb14ba4..0d8061367155e 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
@@ -2459,6 +2459,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 a10ca4f2c2def..f06f00dcfbe3d 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
@@ -411,6 +411,9 @@ public class LaunchIntentDispatcher {
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 57aae64057cfb..70cfc9c2aff61 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.base.shared_preferences.SharedPrefsUtils.SharedPrefsExt;
+import org.chromium.chrome.browser.document.ChromeLauncherActivity;
import org.chromium.chrome.browser.preferences.Pref;
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 98d01e921f9b8..da4a5e6b1eba2 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 getListener(@NonNull Profile profile) {
return (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;
};
@@ -88,6 +92,13 @@ final class PrivacySettingsExt {
closeTabsOnExitPref.setOrder(PRIVACY_PREFERENCES_ORDER);
closeTabsOnExitPref.setOnPreferenceChangeListener(getListener(profile));
}
+
+ ChromeSwitchPreference openLinksInIncognitoPref =
+ (ChromeSwitchPreference) prefFragment.findPreference(PREF_OPEN_LINKS_IN_INCOGNITO);
+ if (openLinksInIncognitoPref != null) {
+ openLinksInIncognitoPref.setOrder(PRIVACY_PREFERENCES_ORDER);
+ openLinksInIncognitoPref.setOnPreferenceChangeListener(getListener(profile));
+ }
}
static void updatePreferences(@NonNull PreferenceFragmentCompat prefFragment, @NonNull Profile profile) {
@@ -104,5 +115,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/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>