From 90f4f1e6eab83f9cf855b30067f3cb0afb12ca96 Mon Sep 17 00:00:00 2001 From: Deep Date: Sun, 11 Sep 2022 19:32:58 -0400 Subject: [PATCH] Initial native work Add enableFilter native function ADd native function to check for the filter list --- android/brave_java_sources.gni | 1 + .../shields/BraveCookieConsentNotices.java | 80 +++++++++++++++++++ browser/android/BUILD.gn | 2 + .../android/brave_cookie_consent_notices.cc | 55 +++++++++++++ .../android/brave_cookie_consent_notices.h | 35 ++++++++ build/android/BUILD.gn | 1 + 6 files changed, 174 insertions(+) create mode 100644 android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java create mode 100644 browser/android/brave_cookie_consent_notices.cc create mode 100644 browser/android/brave_cookie_consent_notices.h diff --git a/android/brave_java_sources.gni b/android/brave_java_sources.gni index 6f8ed262393..bd6e514a32f 100644 --- a/android/brave_java_sources.gni +++ b/android/brave_java_sources.gni @@ -300,6 +300,7 @@ brave_java_sources = [ "../../brave/android/java/org/chromium/chrome/browser/settings/developer/BraveRewardsDebugPreferences.java", "../../brave/android/java/org/chromium/chrome/browser/settings/themes/BraveRadioButtonGroupCustomHomepageThemePreference.java", "../../brave/android/java/org/chromium/chrome/browser/share/BraveShareDelegateImpl.java", + "../../brave/android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java", "../../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", diff --git a/android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java b/android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java new file mode 100644 index 00000000000..4bcafd59b6a --- /dev/null +++ b/android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java @@ -0,0 +1,80 @@ +/* Copyright (c) 2019 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.CalledByNative; +import org.chromium.base.annotations.JNINamespace; +import org.chromium.base.annotations.NativeMethods; +import org.chromium.chrome.browser.profiles.Profile; + +import java.util.ArrayList; +import java.util.List; + +@JNINamespace("chrome::android") +public class BraveCookieConsentNotices { + private long mNativeBraveCookieConsentNotices; + private static BraveCookieConsentNotices sInstance; + private static final Object lock = new Object(); + + public static BraveCookieConsentNotices getInstance() { + synchronized(lock) { + if(sInstance == null) { + sInstance = new BraveCookieConsentNotices(); + } + } + return sInstance; + } + + private BraveCookieConsentNotices() { + mNativeBraveCookieConsentNotices = 0; + init(); + } + + private void init() { + if (mNativeBraveCookieConsentNotices == 0) { + BraveCookieConsentNoticesJni.get().init(this); + } + } + + public void enableFilter() { + synchronized (lock) { + BraveCookieConsentNoticesJni.get().enableFilter(mNativeBraveCookieConsentNotices); + } + } + + public boolean isFilterListAvailable() { + synchronized (lock) { + return BraveCookieConsentNoticesJni.get().isFilterListAvailable( + mNativeBraveCookieConsentNotices); + } + } + + @Override + protected void finalize() { + destroy(); + } + + private void destroy() { + if (mNativeBraveCookieConsentNotices == 0) { + return; + } + BraveCookieConsentNoticesJni.get().destroy(mNativeBraveCookieConsentNotices); + } + + @CalledByNative + private void setNativePtr(long nativePtr) { + assert mNativeBraveCookieConsentNotices == 0; + mNativeBraveCookieConsentNotices = nativePtr; + } + + @NativeMethods + interface Natives { + void init(BraveCookieConsentNotices self); + void enableFilter(long nativeBraveCookieConsentNotices); + boolean isFilterListAvailable(long nativeBraveCookieConsentNotices); + void destroy(long nativeBraveCookieConsentNotices); + } +} diff --git a/browser/android/BUILD.gn b/browser/android/BUILD.gn index 41989276a84..0e32db96baa 100644 --- a/browser/android/BUILD.gn +++ b/browser/android/BUILD.gn @@ -6,6 +6,8 @@ source_set("android_browser_process") { sources = [ "//brave/browser/brave_ads/android/brave_ads_native_helper.cc", + "brave_cookie_consent_notices.cc", + "brave_cookie_consent_notices.h", "brave_feature_util.cc", "brave_relaunch_utils.cc", "brave_shields_content_settings.cc", diff --git a/browser/android/brave_cookie_consent_notices.cc b/browser/android/brave_cookie_consent_notices.cc new file mode 100644 index 00000000000..dbd1542b2e6 --- /dev/null +++ b/browser/android/brave_cookie_consent_notices.cc @@ -0,0 +1,55 @@ +/* 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/. */ + +#include "brave/browser/android/brave_cookie_consent_notices.h" + +#include + +#include "base/android/jni_android.h" +#include "base/android/jni_string.h" +#include "brave/build/android/jni_headers/BraveCookieConsentNotices_jni.h" +#include "brave/browser/brave_browser_process.h" +#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h" +#include "brave/components/brave_shields/browser/ad_block_service.h" +#include "brave/components/brave_shields/common/brave_shield_constants.h" +#include "url/gurl.h" + +namespace chrome { +namespace android { + +BraveCookieConsentNotices::BraveCookieConsentNotices( + JNIEnv* env, + const base::android::JavaRef& obj) + : jobj_(base::android::ScopedJavaGlobalRef(obj)) { + Java_BraveCookieConsentNotices_setNativePtr(env, obj, + reinterpret_cast(this)); +} + +BraveCookieConsentNotices::~BraveCookieConsentNotices() {} + +void BraveCookieConsentNotices::Destroy(JNIEnv* env) { + delete this; +} + +void BraveCookieConsentNotices::EnableFilter(JNIEnv* env) { + g_brave_browser_process->ad_block_service() + ->regional_service_manager() + ->EnableFilterList(brave_shields::kCookieListUuid, true); +} + +bool BraveCookieConsentNotices::IsFilterListAvailable(JNIEnv* env) { + return g_brave_browser_process->ad_block_service() + ->regional_service_manager() + ->IsFilterListAvailable(brave_shields::kCookieListUuid); +} + +static void JNI_BraveCookieConsentNotices_Init( + JNIEnv* env, + const base::android::JavaParamRef& jcaller) { + new BraveCookieConsentNotices(env, jcaller); +} + +} // namespace android +} // namespace chrome diff --git a/browser/android/brave_cookie_consent_notices.h b/browser/android/brave_cookie_consent_notices.h new file mode 100644 index 00000000000..5b27edf3c02 --- /dev/null +++ b/browser/android/brave_cookie_consent_notices.h @@ -0,0 +1,35 @@ +/* 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/. */ + +#ifndef BRAVE_BROWSER_ANDROID_BRAVE_COOKIE_CONSENT_NOTICES_H_ +#define BRAVE_BROWSER_ANDROID_BRAVE_COOKIE_CONSENT_NOTICES_H_ + +#include +#include +#include "base/android/scoped_java_ref.h" + +namespace chrome { +namespace android { + +class BraveCookieConsentNotices { + public: + BraveCookieConsentNotices(JNIEnv* env, + const base::android::JavaRef& obj); + ~BraveCookieConsentNotices(); + + void EnableFilter(JNIEnv* env); + + bool IsFilterListAvailable(JNIEnv* env); + + void Destroy(JNIEnv* env); + + private: + base::android::ScopedJavaGlobalRef jobj_; +}; + +} // namespace android +} // namespace chrome + +#endif // BRAVE_BROWSER_ANDROID_BRAVE_COOKIE_CONSENT_NOTICES_H_ diff --git a/build/android/BUILD.gn b/build/android/BUILD.gn index da39d1b7913..95f4ea38dce 100755 --- a/build/android/BUILD.gn +++ b/build/android/BUILD.gn @@ -198,6 +198,7 @@ generate_jni("jni_headers") { "//brave/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java", "//brave/android/java/org/chromium/chrome/browser/preferences/website/BraveShieldsContentSettings.java", "//brave/android/java/org/chromium/chrome/browser/settings/developer/BraveQAPreferences.java", + "//brave/android/java/org/chromium/chrome/browser/shields/BraveCookieConsentNotices.java", "//brave/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java", "//brave/android/java/org/chromium/chrome/browser/sync/BraveSyncDevices.java", "//brave/android/java/org/chromium/chrome/browser/vpn/BraveVpnNativeWorker.java",