From 6a612f687346079db288689f5d72f5c74de0b718 Mon Sep 17 00:00:00 2001 From: Serg Date: Thu, 5 Jan 2023 13:25:08 -0500 Subject: [PATCH] Enables Safe Browsing flag on Android by default --- .../chrome/browser/app/BraveActivity.java | 30 ++++++++++++------- .../settings/BravePrivacySettingsTest.java | 3 +- browser/android/safe_browsing/features.cc | 2 +- .../BraveSafeBrowsingApiHandler.java | 8 ++++- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/app/BraveActivity.java b/android/java/org/chromium/chrome/browser/app/BraveActivity.java index 293041c97be9..cd8a4bf4b831 100644 --- a/android/java/org/chromium/chrome/browser/app/BraveActivity.java +++ b/android/java/org/chromium/chrome/browser/app/BraveActivity.java @@ -197,6 +197,7 @@ import org.chromium.components.safe_browsing.SafeBrowsingApiBridge; import org.chromium.components.search_engines.TemplateUrl; import org.chromium.components.user_prefs.UserPrefs; +import org.chromium.content_public.browser.UiThreadTaskTraits; import org.chromium.content_public.browser.WebContents; import org.chromium.mojo.bindings.ConnectionErrorHandler; import org.chromium.mojo.system.MojoException; @@ -270,6 +271,7 @@ public abstract class BraveActivity extends C private boolean isProcessingPendingDappsTxRequest; private int mLastTabId; private boolean mNativeInitialized; + private boolean mSafeBrowsingFlagEnabled; private NewTabPageManager mNewTabPageManager; private NotificationPermissionController mNotificationPermissionController; private BraveNewsController mBraveNewsController; @@ -307,15 +309,13 @@ public void onResumeWithNative() { } updateWalletBadgeVisibility(); } - boolean safeBrowsingFlagEnabled = + + // We can store a state of that flag as a browser has to be restarted + // when the flag state is changed in any case + mSafeBrowsingFlagEnabled = ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_ANDROID_SAFE_BROWSING); - boolean safeBrowsingPrefEnabled = - SafeBrowsingBridge.getSafeBrowsingState() != SafeBrowsingState.NO_SAFE_BROWSING; - if (safeBrowsingFlagEnabled && safeBrowsingPrefEnabled) { - executeInitSafeBrowsing(0); - } else if (!safeBrowsingFlagEnabled && safeBrowsingPrefEnabled) { - SafeBrowsingBridge.setSafeBrowsingState(SafeBrowsingState.NO_SAFE_BROWSING); - } + + executeInitSafeBrowsing(0); } @Override @@ -830,6 +830,11 @@ public void turnSafeBrowsingOff() { SafeBrowsingBridge.setSafeBrowsingState(SafeBrowsingState.NO_SAFE_BROWSING); } + @Override + public boolean isSafeBrowsingEnabled() { + return mSafeBrowsingFlagEnabled; + } + @Override public Activity getActivity() { return this; @@ -1995,8 +2000,13 @@ public void addOrEditBookmark(final Tab tabToBookmark) { // BraveSafeBrowsingApiHandler.SAFE_BROWSING_INIT_INTERVAL_MS, // as upstream does, to keep the GmsCore process alive. private void executeInitSafeBrowsing(long delay) { - PostTask.postDelayedTask(TaskTraits.USER_VISIBLE_MAY_BLOCK, () -> { - BraveSafeBrowsingApiHandler.getInstance().initSafeBrowsing(); + // SafeBrowsingBridge.getSafeBrowsingState() has to be executed on a main thread + PostTask.postDelayedTask(UiThreadTaskTraits.DEFAULT, () -> { + if (SafeBrowsingBridge.getSafeBrowsingState() != SafeBrowsingState.NO_SAFE_BROWSING) { + // initSafeBrowsing could be executed on a background thread + PostTask.postTask(TaskTraits.USER_VISIBLE_MAY_BLOCK, + () -> { BraveSafeBrowsingApiHandler.getInstance().initSafeBrowsing(); }); + } executeInitSafeBrowsing(BraveSafeBrowsingApiHandler.SAFE_BROWSING_INIT_INTERVAL_MS); }, delay); } diff --git a/android/javatests/org/chromium/chrome/browser/privacy/settings/BravePrivacySettingsTest.java b/android/javatests/org/chromium/chrome/browser/privacy/settings/BravePrivacySettingsTest.java index 29160fc6123b..fdc733fdb219 100644 --- a/android/javatests/org/chromium/chrome/browser/privacy/settings/BravePrivacySettingsTest.java +++ b/android/javatests/org/chromium/chrome/browser/privacy/settings/BravePrivacySettingsTest.java @@ -72,8 +72,7 @@ public void testParentItems() { checkPreferenceExists(PREF_CLEAR_BROWSING_DATA); checkPreferenceExists(PREF_DO_NOT_TRACK); checkPreferenceExists(PREF_HTTPS_FIRST_MODE); - // TODO : Need to add back the test once we have fix ready for safe browsing - // checkPreferenceExists(PREF_SAFE_BROWSING); + checkPreferenceExists(PREF_SAFE_BROWSING); checkPreferenceExists(PREF_SECURE_DNS); checkPreferenceExists(PREF_INCOGNITO_LOCK); checkPreferenceExists(PREF_PHONE_AS_A_SECURITY_KEY); diff --git a/browser/android/safe_browsing/features.cc b/browser/android/safe_browsing/features.cc index 30a37a2a9157..2d4b2d33118a 100644 --- a/browser/android/safe_browsing/features.cc +++ b/browser/android/safe_browsing/features.cc @@ -12,7 +12,7 @@ namespace features { BASE_FEATURE(kBraveAndroidSafeBrowsing, "BraveAndroidSafeBrowsing", - base::FEATURE_DISABLED_BY_DEFAULT); + base::FEATURE_ENABLED_BY_DEFAULT); } // namespace features } // namespace safe_browsing diff --git a/components/safe_browsing/android/java/src/org/chromium/components/safe_browsing/BraveSafeBrowsingApiHandler.java b/components/safe_browsing/android/java/src/org/chromium/components/safe_browsing/BraveSafeBrowsingApiHandler.java index 1aa7db475d6b..2a0f309d3f38 100644 --- a/components/safe_browsing/android/java/src/org/chromium/components/safe_browsing/BraveSafeBrowsingApiHandler.java +++ b/components/safe_browsing/android/java/src/org/chromium/components/safe_browsing/BraveSafeBrowsingApiHandler.java @@ -31,6 +31,9 @@ public class BraveSafeBrowsingApiHandler implements SafeBrowsingApiHandler { public interface BraveSafeBrowsingApiHandlerDelegate { default void turnSafeBrowsingOff() {} + default boolean isSafeBrowsingEnabled() { + return true; + } Activity getActivity(); } @@ -72,7 +75,10 @@ public boolean init(Observer observer) { @Override public void startUriLookup(final long callbackId, String uri, int[] threatsOfInterest) { - if (mBraveSafeBrowsingApiHandlerDelegate == null) { + if (mBraveSafeBrowsingApiHandlerDelegate == null + || !mBraveSafeBrowsingApiHandlerDelegate.isSafeBrowsingEnabled()) { + mObserver.onUrlCheckDone( + callbackId, SafeBrowsingResult.TIMEOUT, "{}", DEFAULT_CHECK_DELTA); return; } mTriesCount++;