Add-custom-tab-intents-privacy-option.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. From: csagan5 <32685696+csagan5@users.noreply.github.com>
  2. Date: Wed, 29 Aug 2018 11:03:44 +0200
  3. Subject: Add custom tab intents privacy option
  4. Add custom tab intents privacy option and force
  5. open external links in incognito flag.
  6. Flags are mutually exclusive.
  7. See also: https://github.com/bromite/bromite/issues/1474
  8. ---
  9. .../java/res/xml/privacy_preferences.xml | 10 ++++++++
  10. .../browser/LaunchIntentDispatcher.java | 22 +++++++++++++++++
  11. .../IncognitoCustomTabIntentDataProvider.java | 6 +++++
  12. .../privacy/settings/PrivacySettings.java | 24 +++++++++++++++++++
  13. .../chrome/browser/tab/TabAssociatedApp.java | 6 ++++-
  14. .../strings/android_chrome_strings.grd | 15 ++++++++++++
  15. 6 files changed, 82 insertions(+), 1 deletion(-)
  16. diff --git a/chrome/android/java/res/xml/privacy_preferences.xml b/chrome/android/java/res/xml/privacy_preferences.xml
  17. --- a/chrome/android/java/res/xml/privacy_preferences.xml
  18. +++ b/chrome/android/java/res/xml/privacy_preferences.xml
  19. @@ -65,6 +65,16 @@
  20. android:fragment="org.chromium.chrome.browser.privacy.settings.DoNotTrackSettings"
  21. android:key="do_not_track"
  22. android:title="@string/do_not_track_title"/>
  23. + <org.chromium.components.browser_ui.settings.ChromeSwitchPreference
  24. + android:key="allow_custom_tab_intents"
  25. + android:title="@string/allow_custom_tab_intents_title"
  26. + android:summary="@string/allow_custom_tab_intents_summary"
  27. + android:defaultValue="false" />
  28. + <org.chromium.components.browser_ui.settings.ChromeSwitchPreference
  29. + android:key="open_external_links_incognito"
  30. + android:title="@string/open_external_links_incognito_title"
  31. + android:summary="@string/open_external_links_incognito_summary"
  32. + android:defaultValue="false" />
  33. <Preference
  34. android:key="privacy_sandbox"
  35. android:title="@string/prefs_privacy_sandbox"
  36. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
  37. --- a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
  38. +++ b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
  39. @@ -46,6 +46,7 @@ import org.chromium.chrome.browser.notifications.NotificationPlatformBridge;
  40. import org.chromium.chrome.browser.partnercustomizations.PartnerBrowserCustomizations;
  41. import org.chromium.chrome.browser.searchwidget.SearchActivity;
  42. import org.chromium.chrome.browser.tab.Tab;
  43. +import org.chromium.chrome.browser.tab.TabLaunchType;
  44. import org.chromium.chrome.browser.translate.TranslateIntentHandler;
  45. import org.chromium.chrome.browser.util.AndroidTaskUtils;
  46. import org.chromium.chrome.browser.vr.VrModuleProvider;
  47. @@ -59,6 +60,8 @@ import java.lang.annotation.Retention;
  48. import java.lang.annotation.RetentionPolicy;
  49. import java.util.Set;
  50. +import org.chromium.chrome.browser.privacy.settings.PrivacySettings;
  51. +
  52. /**
  53. * Dispatches incoming intents to the appropriate activity based on the current configuration and
  54. * Intent fired.
  55. @@ -279,6 +282,9 @@ public class LaunchIntentDispatcher implements IntentHandler.IntentHandlerDelega
  56. */
  57. public static boolean isCustomTabIntent(Intent intent) {
  58. if (intent == null) return false;
  59. + if (!ContextUtils.getAppSharedPreferences()
  60. + .getBoolean(PrivacySettings.PREF_ALLOW_CUSTOM_TAB_INTENTS, false))
  61. + return false;
  62. if (CustomTabsIntent.shouldAlwaysUseBrowserUI(intent)
  63. || !intent.hasExtra(CustomTabsIntent.EXTRA_SESSION)) {
  64. return false;
  65. @@ -298,6 +304,10 @@ public class LaunchIntentDispatcher implements IntentHandler.IntentHandlerDelega
  66. newIntent.setData(uri);
  67. newIntent.setClassName(context, CustomTabActivity.class.getName());
  68. + if (ContextUtils.getAppSharedPreferences()
  69. + .getBoolean(PrivacySettings.PREF_OPEN_EXTERNAL_LINKS_INCOGNITO, false))
  70. + newIntent.putExtra(IntentHandler.EXTRA_OPEN_NEW_INCOGNITO_TAB, true);
  71. +
  72. // Since configureIntentForResizableCustomTab() might change the componenet/class
  73. // associated with the passed intent, it needs to be called after #setClassName(context,
  74. // CustomTabActivity.class.getName());
  75. @@ -417,6 +427,18 @@ public class LaunchIntentDispatcher implements IntentHandler.IntentHandlerDelega
  76. if (Intent.ACTION_VIEW.equals(newIntent.getAction())
  77. && !IntentHandler.wasIntentSenderChrome(newIntent)) {
  78. +
  79. + if (ContextUtils.getAppSharedPreferences().getBoolean(
  80. + PrivacySettings.PREF_OPEN_EXTERNAL_LINKS_INCOGNITO, false)) {
  81. + Context applicationContext = ContextUtils.getApplicationContext();
  82. + newIntent = IntentHandler.createTrustedOpenNewTabIntent(applicationContext,
  83. + /*incognito*/true);
  84. + newIntent.setData(mIntent.getData());
  85. + newIntent.setPackage(applicationContext.getPackageName());
  86. + IntentHandler.setTabLaunchType(newIntent, TabLaunchType.FROM_EXTERNAL_APP);
  87. + newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  88. + }
  89. +
  90. long time = SystemClock.elapsedRealtime();
  91. if (!chromeTabbedTaskExists()) {
  92. newIntent.putExtra(IntentHandler.EXTRA_STARTED_TABBED_CHROME_TASK, true);
  93. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/IncognitoCustomTabIntentDataProvider.java b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/IncognitoCustomTabIntentDataProvider.java
  94. --- a/chrome/android/java/src/org/chromium/chrome/browser/customtabs/IncognitoCustomTabIntentDataProvider.java
  95. +++ b/chrome/android/java/src/org/chromium/chrome/browser/customtabs/IncognitoCustomTabIntentDataProvider.java
  96. @@ -36,6 +36,9 @@ import org.chromium.components.browser_ui.widget.TintedDrawable;
  97. import java.util.ArrayList;
  98. import java.util.List;
  99. +import org.chromium.base.ContextUtils;
  100. +import org.chromium.chrome.browser.privacy.settings.PrivacySettings;
  101. +
  102. /**
  103. * A model class that parses the incoming intent for incognito Custom Tabs specific customization
  104. * data.
  105. @@ -109,6 +112,9 @@ public class IncognitoCustomTabIntentDataProvider extends BrowserServicesIntentD
  106. }
  107. private static boolean isIntentFromThirdPartyAllowed() {
  108. + if (ContextUtils.getAppSharedPreferences()
  109. + .getBoolean(PrivacySettings.PREF_OPEN_EXTERNAL_LINKS_INCOGNITO, false))
  110. + return true;
  111. return CachedFeatureFlags.isEnabled(
  112. ChromeFeatureList.CCT_INCOGNITO_AVAILABLE_TO_THIRD_PARTY);
  113. }
  114. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
  115. --- a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
  116. +++ b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
  117. @@ -94,6 +94,9 @@ public class PrivacySettings
  118. private ManagedPreferenceDelegate mManagedPreferenceDelegate;
  119. + private ChromeSwitchPreference allowCustomTabIntentsPref;
  120. + private ChromeSwitchPreference openExternalLinksPref;
  121. +
  122. @Override
  123. public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
  124. PrivacyPreferencesManagerImpl privacyPrefManager =
  125. @@ -232,6 +235,9 @@ public class PrivacySettings
  126. incognitoReauthPreference.setChecked(lastPrefValue);
  127. }
  128. + public static final String PREF_ALLOW_CUSTOM_TAB_INTENTS = "allow_custom_tab_intents";
  129. + public static final String PREF_OPEN_EXTERNAL_LINKS_INCOGNITO = "open_external_links_incognito";
  130. +
  131. @Override
  132. public boolean onPreferenceChange(Preference preference, Object newValue) {
  133. String key = preference.getKey();
  134. @@ -266,6 +272,14 @@ public class PrivacySettings
  135. } else if (PREF_INCOGNITO_TAB_HISTORY_ENABLED.equals(key)) {
  136. UserPrefs.get(Profile.getLastUsedRegularProfile())
  137. .setBoolean(Pref.INCOGNITO_TAB_HISTORY_ENABLED, (boolean) newValue);
  138. + } else if (PREF_ALLOW_CUSTOM_TAB_INTENTS.equals(key)) {
  139. + SharedPreferences.Editor sharedPreferencesEditor = ContextUtils.getAppSharedPreferences().edit();
  140. + sharedPreferencesEditor.putBoolean(PREF_ALLOW_CUSTOM_TAB_INTENTS, (boolean)newValue);
  141. + sharedPreferencesEditor.apply();
  142. + } else if (PREF_OPEN_EXTERNAL_LINKS_INCOGNITO.equals(key)) {
  143. + SharedPreferences.Editor sharedPreferencesEditor = ContextUtils.getAppSharedPreferences().edit();
  144. + sharedPreferencesEditor.putBoolean(PREF_OPEN_EXTERNAL_LINKS_INCOGNITO, (boolean)newValue);
  145. + sharedPreferencesEditor.apply();
  146. }
  147. return true;
  148. @@ -301,6 +315,16 @@ public class PrivacySettings
  149. canMakePaymentPref.setChecked(prefService.getBoolean(Pref.CAN_MAKE_PAYMENT_ENABLED));
  150. }
  151. + allowCustomTabIntentsPref =
  152. + (ChromeSwitchPreference) findPreference(PREF_ALLOW_CUSTOM_TAB_INTENTS);
  153. + allowCustomTabIntentsPref.setOnPreferenceChangeListener(this);
  154. + allowCustomTabIntentsPref.setManagedPreferenceDelegate(mManagedPreferenceDelegate);
  155. +
  156. + openExternalLinksPref =
  157. + (ChromeSwitchPreference) findPreference(PREF_OPEN_EXTERNAL_LINKS_INCOGNITO);
  158. + openExternalLinksPref.setOnPreferenceChangeListener(this);
  159. + openExternalLinksPref.setManagedPreferenceDelegate(mManagedPreferenceDelegate);
  160. +
  161. Preference doNotTrackPref = findPreference(PREF_DO_NOT_TRACK);
  162. if (doNotTrackPref != null) {
  163. doNotTrackPref.setSummary(prefService.getBoolean(Pref.ENABLE_DO_NOT_TRACK)
  164. diff --git a/chrome/browser/tab/java/src/org/chromium/chrome/browser/tab/TabAssociatedApp.java b/chrome/browser/tab/java/src/org/chromium/chrome/browser/tab/TabAssociatedApp.java
  165. --- a/chrome/browser/tab/java/src/org/chromium/chrome/browser/tab/TabAssociatedApp.java
  166. +++ b/chrome/browser/tab/java/src/org/chromium/chrome/browser/tab/TabAssociatedApp.java
  167. @@ -84,7 +84,11 @@ public final class TabAssociatedApp extends TabWebContentsUserData implements Im
  168. public static boolean isOpenedFromExternalApp(Tab tab) {
  169. TabAssociatedApp app = get(tab);
  170. if (app == null) return false;
  171. -
  172. + if (ContextUtils.getAppSharedPreferences()
  173. + .getBoolean("open_external_links_incognito", false) &&
  174. + tab.isIncognito() &&
  175. + tab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP)
  176. + return true;
  177. String packageName = ContextUtils.getApplicationContext().getPackageName();
  178. return tab.getLaunchType() == TabLaunchType.FROM_EXTERNAL_APP
  179. && !TextUtils.equals(app.getAppId(), packageName);
  180. diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd
  181. --- a/chrome/browser/ui/android/strings/android_chrome_strings.grd
  182. +++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd
  183. @@ -4609,6 +4609,21 @@ To change this setting, <ph name="BEGIN_LINK">&lt;resetlink&gt;</ph>reset sync<p
  184. <message name="IDS_NEAR_OOM_REDUCTION_DECLINE" desc="The text of the button letting the user decline the browser's intervention, so that the page can be reloaded.">
  185. Show original
  186. </message>
  187. + <!-- Allow custom tab intents -->
  188. + <message name="IDS_ALLOW_CUSTOM_TAB_INTENTS_TITLE" desc="Text for 'Allow custom tab intents' settings-privacy option.">
  189. + Allow custom tab intents
  190. + </message>
  191. + <message name="IDS_ALLOW_CUSTOM_TAB_INTENTS_SUMMARY" desc="Summary text for 'Allow custom tab intents' settings-privacy option.">
  192. + Allow applications to open custom tab intents, similar to webview.
  193. + </message>
  194. +
  195. + <!-- Open External Links in Incognito -->
  196. + <message name="IDS_OPEN_EXTERNAL_LINKS_INCOGNITO_TITLE" desc="Text for 'Open external links in incognito' settings-privacy option.">
  197. + Open external links in incognito
  198. + </message>
  199. + <message name="IDS_OPEN_EXTERNAL_LINKS_INCOGNITO_SUMMARY" desc="Summary text for 'Open external links in incognito' settings-privacy option.">
  200. + Force the opening of all external links in incognito mode
  201. + </message>
  202. <!-- Autofill Assistant preferences -->
  203. <!-- TODO(b/168178344): Move to Assistant settings strings section below. -->