Skip to content

Commit

Permalink
Merge pull request #15063 from brave/cookie_consent_dialog_android
Browse files Browse the repository at this point in the history
Block cookie consent dialog android
  • Loading branch information
deeppandya authored Sep 22, 2022
2 parents 69635e8 + e9090b7 commit 52e5e49
Show file tree
Hide file tree
Showing 39 changed files with 637 additions and 12 deletions.
17 changes: 17 additions & 0 deletions android/brave_java_resources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ brave_java_resources = [
"java/res/drawable-hdpi/ic_ad_free_videos.png",
"java/res/drawable-hdpi/ic_brave_onboarding.png",
"java/res/drawable-hdpi/ic_chrome.png",
"java/res/drawable-hdpi/ic_cookie.png",
"java/res/drawable-hdpi/ic_cookie_background.png",
"java/res/drawable-hdpi/ic_cross_modal.png",
"java/res/drawable-hdpi/ic_delete_white_24dp.png",
"java/res/drawable-hdpi/ic_expand_less_black_24dp.png",
Expand Down Expand Up @@ -138,6 +140,8 @@ brave_java_resources = [
"java/res/drawable-mdpi/ic_ad_free_videos.png",
"java/res/drawable-mdpi/ic_brave_onboarding.png",
"java/res/drawable-mdpi/ic_chrome.png",
"java/res/drawable-mdpi/ic_cookie.png",
"java/res/drawable-mdpi/ic_cookie_background.png",
"java/res/drawable-mdpi/ic_cross_modal.png",
"java/res/drawable-mdpi/ic_delete_white_24dp.png",
"java/res/drawable-mdpi/ic_expand_less_black_24dp.png",
Expand Down Expand Up @@ -179,6 +183,11 @@ brave_java_resources = [
"java/res/drawable-mdpi/verified_creator_large.png",
"java/res/drawable-mdpi/verified_disclosure.png",
"java/res/drawable-mdpi/yandex.png",
"java/res/drawable-night-hdpi/ic_cookie_background.png",
"java/res/drawable-night-mdpi/ic_cookie_background.png",
"java/res/drawable-night-xhdpi/ic_cookie_background.png",
"java/res/drawable-night-xxhdpi/ic_cookie_background.png",
"java/res/drawable-night-xxxhdpi/ic_cookie_background.png",
"java/res/drawable-nodpi/dylan_malval_sea_min.webp",
"java/res/drawable-nodpi/eth.png",
"java/res/drawable-nodpi/fee_icon.png",
Expand Down Expand Up @@ -226,6 +235,8 @@ brave_java_resources = [
"java/res/drawable-xhdpi/ic_ad_free_videos.png",
"java/res/drawable-xhdpi/ic_brave_onboarding.png",
"java/res/drawable-xhdpi/ic_chrome.png",
"java/res/drawable-xhdpi/ic_cookie.png",
"java/res/drawable-xhdpi/ic_cookie_background.png",
"java/res/drawable-xhdpi/ic_cross_modal.png",
"java/res/drawable-xhdpi/ic_delete_white_24dp.png",
"java/res/drawable-xhdpi/ic_expand_less_black_24dp.png",
Expand Down Expand Up @@ -308,6 +319,8 @@ brave_java_resources = [
"java/res/drawable-xxhdpi/ic_ad_free_videos.png",
"java/res/drawable-xxhdpi/ic_brave_onboarding.png",
"java/res/drawable-xxhdpi/ic_chrome.png",
"java/res/drawable-xxhdpi/ic_cookie.png",
"java/res/drawable-xxhdpi/ic_cookie_background.png",
"java/res/drawable-xxhdpi/ic_cross_modal.png",
"java/res/drawable-xxhdpi/ic_delete_white_24dp.png",
"java/res/drawable-xxhdpi/ic_expand_less_black_24dp.png",
Expand Down Expand Up @@ -389,6 +402,8 @@ brave_java_resources = [
"java/res/drawable-xxxhdpi/ic_ad_free_videos.png",
"java/res/drawable-xxxhdpi/ic_brave_onboarding.png",
"java/res/drawable-xxxhdpi/ic_chrome.png",
"java/res/drawable-xxxhdpi/ic_cookie.png",
"java/res/drawable-xxxhdpi/ic_cookie_background.png",
"java/res/drawable-xxxhdpi/ic_cross_modal.png",
"java/res/drawable-xxxhdpi/ic_delete_white_24dp.png",
"java/res/drawable-xxxhdpi/ic_expand_less_black_24dp.png",
Expand Down Expand Up @@ -482,6 +497,7 @@ brave_java_resources = [
"java/res/drawable/brave_sync_warning.xml",
"java/res/drawable/card_bg.xml",
"java/res/drawable/circular_progress.xml",
"java/res/drawable/cookie_consent_tooltip_background.xml",
"java/res/drawable/crypto_wallet_blue_button.xml",
"java/res/drawable/crypto_wallet_hollow_button.xml",
"java/res/drawable/default_dot.xml",
Expand Down Expand Up @@ -712,6 +728,7 @@ brave_java_resources = [
"java/res/layout/activity_welcome_onboarding.xml",
"java/res/layout/application_item_layout.xml",
"java/res/layout/approve_tx_bottom_sheet.xml",
"java/res/layout/block_cookie_consent_notices_tooltip.xml",
"java/res/layout/bottom_toolbar.xml",
"java/res/layout/bottom_toolbar_browsing.xml",
"java/res/layout/bottom_toolbar_menu_button.xml",
Expand Down
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ brave_java_sources = [
"../../brave/android/java/org/chromium/chrome/browser/shields/BraveShieldsHandler.java",
"../../brave/android/java/org/chromium/chrome/browser/shields/BraveShieldsMenuObserver.java",
"../../brave/android/java/org/chromium/chrome/browser/shields/BraveShieldsUtils.java",
"../../brave/android/java/org/chromium/chrome/browser/shields/CookieListOptInServiceFactory.java",
"../../brave/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java",
"../../brave/android/java/org/chromium/chrome/browser/site_settings/BraveSiteSettingsDelegate.java",
"../../brave/android/java/org/chromium/chrome/browser/site_settings/BraveWalletEthereumConnectedSites.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ public final class BravePreferenceKeys {
"org.chromium.chrome.browser.Brave_Biometrics_For_Wallet_Encrypted";
public static final String BRAVE_AD_FREE_CALLOUT_DIALOG = "brave_ad_free_callout_dialog";
public static final String BRAVE_OPENED_YOUTUBE = "brave_opened_youtube";
public static final String SHOULD_SHOW_COOKIE_CONSENT_NOTICE =
"should_show_cookie_consent_notice";
public static final String LOADED_SITE_COUNT = "loaded_site_count";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@

import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.brave_shields.mojom.CookieListOptInPageAndroidHandler;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveConfig;
import org.chromium.chrome.browser.metrics.UmaSessionStats;
import org.chromium.chrome.browser.preferences.BravePref;
import org.chromium.chrome.browser.preferences.BravePrefServiceBridge;
import org.chromium.chrome.browser.preferences.Pref;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.chrome.browser.preferences.website.BraveShieldsContentSettings;
import org.chromium.chrome.browser.privacy.settings.PrivacyPreferencesManagerImpl;
import org.chromium.chrome.browser.privacy.settings.PrivacySettings;
Expand All @@ -29,14 +31,17 @@
import org.chromium.chrome.browser.settings.BravePreferenceDialogFragment;
import org.chromium.chrome.browser.settings.BraveWebrtcPolicyPreference;
import org.chromium.chrome.browser.settings.ChromeManagedPreferenceDelegate;
import org.chromium.chrome.browser.shields.CookieListOptInServiceFactory;
import org.chromium.components.browser_ui.settings.ChromeBaseCheckBoxPreference;
import org.chromium.components.browser_ui.settings.ChromeBasePreference;
import org.chromium.components.browser_ui.settings.ChromeSwitchPreference;
import org.chromium.components.browser_ui.settings.SettingsUtils;
import org.chromium.components.prefs.PrefService;
import org.chromium.components.user_prefs.UserPrefs;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;

public class BravePrivacySettings extends PrivacySettings {
public class BravePrivacySettings extends PrivacySettings implements ConnectionErrorHandler {
// Chromium Prefs
private static final String PREF_CAN_MAKE_PAYMENT = "can_make_payment";
private static final String PREF_NETWORK_PREDICTIONS = "preload_pages";
Expand Down Expand Up @@ -64,6 +69,7 @@ public class BravePrivacySettings extends PrivacySettings {
private static final String PREF_HTTPSE = "httpse";
private static final String PREF_DE_AMP = "de_amp";
private static final String PREF_IPFS_GATEWAY = "ipfs_gateway";
private static final String PREF_BLOCK_COOKIE_CONSENT_NOTICES = "block_cookie_consent_notices";
private static final String PREF_AD_BLOCK = "ad_block";
private static final String PREF_BLOCK_SCRIPTS = "scripts_block";
public static final String PREF_FINGERPRINTING_PROTECTION = "fingerprinting_protection";
Expand Down Expand Up @@ -105,11 +111,11 @@ public class BravePrivacySettings extends PrivacySettings {
PREF_OTHER_PRIVACY_SETTINGS_SECTION, // other section
PREF_WEBRTC_POLICY, PREF_SAFE_BROWSING, PREF_INCOGNITO_LOCK, PREF_CAN_MAKE_PAYMENT,
PREF_UNSTOPPABLE_DOMAINS, PREF_ETH_NAMED_SERVICE, PREF_IPFS_GATEWAY, PREF_SECURE_DNS,
PREF_DO_NOT_TRACK, PREF_PHONE_AS_A_SECURITY_KEY, PREF_CLOSE_TABS_ON_EXIT, PREF_SEND_P3A,
PREF_SEND_CRASH_REPORTS, PREF_BRAVE_STATS_USAGE_PING,
PREF_SHOW_AUTOCOMPLETE_IN_ADDRESS_BAR, PREF_SEARCH_SUGGESTIONS,
PREF_AUTOCOMPLETE_TOP_SITES, PREF_AUTOCOMPLETE_BRAVE_SUGGESTED_SITES, PREF_USAGE_STATS,
PREF_PRIVACY_SANDBOX};
PREF_BLOCK_COOKIE_CONSENT_NOTICES, PREF_DO_NOT_TRACK, PREF_PHONE_AS_A_SECURITY_KEY,
PREF_CLOSE_TABS_ON_EXIT, PREF_SEND_P3A, PREF_SEND_CRASH_REPORTS,
PREF_BRAVE_STATS_USAGE_PING, PREF_SHOW_AUTOCOMPLETE_IN_ADDRESS_BAR,
PREF_SEARCH_SUGGESTIONS, PREF_AUTOCOMPLETE_TOP_SITES,
PREF_AUTOCOMPLETE_BRAVE_SUGGESTED_SITES, PREF_USAGE_STATS, PREF_PRIVACY_SANDBOX};

private final int STRICT = 0;
private final int STANDARD = 1;
Expand Down Expand Up @@ -137,6 +143,7 @@ public class BravePrivacySettings extends PrivacySettings {
private ChromeSwitchPreference mSendCrashReports;
private ChromeSwitchPreference mBraveStatsUsagePing;
private ChromeSwitchPreference mIpfsGatewayPref;
private ChromeSwitchPreference mBlockCookieConsentNoticesPref;
private PreferenceCategory mSocialBlockingCategory;
private ChromeSwitchPreference mSocialBlockingGoogle;
private ChromeSwitchPreference mHttpsEverywhere;
Expand All @@ -147,6 +154,31 @@ public class BravePrivacySettings extends PrivacySettings {
private ChromeSwitchPreference mClearBrowsingDataOnExit;
private Preference mUstoppableDomains;
private ChromeSwitchPreference mFingerprntLanguagePref;
private CookieListOptInPageAndroidHandler mCookieListOptInPageAndroidHandler;

@Override
public void onConnectionError(MojoException e) {
mCookieListOptInPageAndroidHandler = null;
initCookieListOptInPageAndroidHandler();
}

private void initCookieListOptInPageAndroidHandler() {
if (mCookieListOptInPageAndroidHandler != null) {
return;
}

mCookieListOptInPageAndroidHandler =
CookieListOptInServiceFactory.getInstance().getCookieListOptInPageAndroidHandler(
this);
}

@Override
public void onDestroy() {
if (mCookieListOptInPageAndroidHandler != null) {
mCookieListOptInPageAndroidHandler.close();
}
super.onDestroy();
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Expand All @@ -157,6 +189,8 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

SettingsUtils.addPreferencesFromResource(this, R.xml.brave_privacy_preferences);

initCookieListOptInPageAndroidHandler();

mHttpsePref = (ChromeSwitchPreference) findPreference(PREF_HTTPSE);
mHttpsePref.setOnPreferenceChangeListener(this);

Expand All @@ -178,6 +212,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
mIpfsGatewayPref = (ChromeSwitchPreference) findPreference(PREF_IPFS_GATEWAY);
mIpfsGatewayPref.setOnPreferenceChangeListener(this);

mBlockCookieConsentNoticesPref =
(ChromeSwitchPreference) findPreference(PREF_BLOCK_COOKIE_CONSENT_NOTICES);
mBlockCookieConsentNoticesPref.setOnPreferenceChangeListener(this);

mBlockCrosssiteCookies =
(BraveDialogPreference) findPreference(PREF_BLOCK_CROSS_SITE_COOKIES);
mBlockCrosssiteCookies.setOnPreferenceChangeListener(this);
Expand Down Expand Up @@ -245,6 +283,14 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
removePreferenceIfPresent(PREF_PRIVACY_SANDBOX);
removePreferenceIfPresent(PREF_SAFE_BROWSING);

if (mCookieListOptInPageAndroidHandler != null) {
mCookieListOptInPageAndroidHandler.shouldShowDialog(shouldShowDialog -> {
if (!shouldShowDialog) {
removePreferenceIfPresent(PREF_BLOCK_COOKIE_CONSENT_NOTICES);
}
});
}

updateBravePreferences();
}

Expand Down Expand Up @@ -295,6 +341,11 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {

} else if (PREF_IPFS_GATEWAY.equals(key)) {
BravePrefServiceBridge.getInstance().setIpfsGatewayEnabled((boolean) newValue);
} else if (PREF_BLOCK_COOKIE_CONSENT_NOTICES.equals(key)) {
if (mCookieListOptInPageAndroidHandler != null) {
mCookieListOptInPageAndroidHandler.enableFilter((boolean) newValue);
}

} else if (PREF_FINGERPRINTING_PROTECTION.equals(key)) {
if (newValue instanceof String
&& String.valueOf(newValue).equals(
Expand Down Expand Up @@ -522,6 +573,10 @@ private void updateBravePreferences() {
mAutocompleteBraveSuggestedSites.setEnabled(autocompleteEnabled);
mFingerprntLanguagePref.setChecked(UserPrefs.get(Profile.getLastUsedRegularProfile())
.getBoolean(BravePref.REDUCE_LANGUAGE_ENABLED));
if (mCookieListOptInPageAndroidHandler != null) {
mCookieListOptInPageAndroidHandler.isFilterListEnabled(
isEnabled -> { mBlockCookieConsentNoticesPref.setChecked(isEnabled); });
}
}

private void removePreferenceIfPresent(String key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* Copyright (c) 2022 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.chromium.chrome.browser.shields;

import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.brave_shields.mojom.CookieListOptInPageAndroidHandler;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.bindings.Interface;
import org.chromium.mojo.bindings.Interface.Proxy.Handler;
import org.chromium.mojo.system.MessagePipeHandle;
import org.chromium.mojo.system.impl.CoreImpl;

@JNINamespace("chrome::android")
public class CookieListOptInServiceFactory {
private static final Object lock = new Object();
private static CookieListOptInServiceFactory instance;

public static CookieListOptInServiceFactory getInstance() {
synchronized (lock) {
if (instance == null) {
instance = new CookieListOptInServiceFactory();
}
}
return instance;
}

private CookieListOptInServiceFactory() {}

public CookieListOptInPageAndroidHandler getCookieListOptInPageAndroidHandler(
ConnectionErrorHandler connectionErrorHandler) {
Profile profile = Utils.getProfile(false); // Always use regular profile
int nativeHandle =
CookieListOptInServiceFactoryJni.get().getInterfaceToCookieListOptInService(
profile);
MessagePipeHandle handle = wrapNativeHandle(nativeHandle);
CookieListOptInPageAndroidHandler cookieListOptInPageAndroidHandler =
CookieListOptInPageAndroidHandler.MANAGER.attachProxy(handle, 0);
Handler handler = ((Interface.Proxy) cookieListOptInPageAndroidHandler).getProxyHandler();
handler.setErrorHandler(connectionErrorHandler);

return cookieListOptInPageAndroidHandler;
}

private MessagePipeHandle wrapNativeHandle(int nativeHandle) {
return CoreImpl.getInstance().acquireNativeHandle(nativeHandle).toMessagePipeHandle();
}

@NativeMethods
interface Natives {
int getInterfaceToCookieListOptInService(Profile profile);
}
}
Loading

0 comments on commit 52e5e49

Please sign in to comment.