Creates a separate mojo ConnectionErrorHandler for Privacy Hub on Android

This commit is contained in:
Serg
2023-07-05 10:23:12 -04:00
parent 77ba6b5120
commit c91cd387e1
4 changed files with 77 additions and 6 deletions
@@ -110,6 +110,7 @@ import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.fullscreen.BrowserControlsManager;
import org.chromium.chrome.browser.informers.BraveAndroidSyncDisabledInformer;
import org.chromium.chrome.browser.informers.BraveSyncAccountDeletedInformer;
import org.chromium.chrome.browser.misc_metrics.PrivacyHubMetricsConnectionErrorHandler;
import org.chromium.chrome.browser.misc_metrics.PrivacyHubMetricsFactory;
import org.chromium.chrome.browser.notifications.BraveNotificationWarningDialog;
import org.chromium.chrome.browser.notifications.BravePermissionUtils;
@@ -204,7 +205,9 @@ public abstract class BraveActivity extends ChromeActivity
implements BrowsingDataBridge.OnClearBrowsingDataListener, BraveVpnObserver,
OnBraveSetDefaultBrowserListener, ConnectionErrorHandler, PrefObserver,
BraveSafeBrowsingApiHandler.BraveSafeBrowsingApiHandlerDelegate,
BraveNewsConnectionErrorHandler.BraveNewsConnectionErrorHandlerDelegate {
BraveNewsConnectionErrorHandler.BraveNewsConnectionErrorHandlerDelegate,
PrivacyHubMetricsConnectionErrorHandler
.PrivacyHubMetricsConnectionErrorHandlerDelegate {
public static final String BRAVE_SEND_URL = "brave://wallet/send";
public static final String BRAVE_SWAP_URL = "brave://wallet/swap";
public static final String BRAVE_REWARDS_SETTINGS_URL = "brave://rewards/";
@@ -269,6 +272,7 @@ public abstract class BraveActivity extends ChromeActivity
private NotificationPermissionController mNotificationPermissionController;
private BraveNewsController mBraveNewsController;
private BraveNewsConnectionErrorHandler mBraveNewsConnectionErrorHandler;
private PrivacyHubMetricsConnectionErrorHandler mPrivacyHubMetricsConnectionErrorHandler;
/**
* Serves as a general exception for failed attempts to get BraveActivity.
@@ -427,6 +431,7 @@ public abstract class BraveActivity extends ChromeActivity
super.onDestroyInternal();
cleanUpBraveNewsController();
cleanUpWalletNativeServices();
cleanUpPrivacyHubMetrics();
}
public WalletModel getWalletModel() {
@@ -1990,12 +1995,19 @@ public abstract class BraveActivity extends ChromeActivity
mAssetRatioService = AssetRatioServiceFactory.getInstance().getAssetRatioService(this);
}
private void initPrivacyHubMetrics() {
@Override
public void initPrivacyHubMetrics() {
if (mPrivacyHubMetrics != null) {
return;
}
if (mPrivacyHubMetricsConnectionErrorHandler == null) {
mPrivacyHubMetricsConnectionErrorHandler =
PrivacyHubMetricsConnectionErrorHandler.getInstance();
mPrivacyHubMetricsConnectionErrorHandler.setDelegate(this);
}
mPrivacyHubMetrics = PrivacyHubMetricsFactory.getInstance().getMetricsService(this);
mPrivacyHubMetrics = PrivacyHubMetricsFactory.getInstance().getMetricsService(
mPrivacyHubMetricsConnectionErrorHandler);
mPrivacyHubMetrics.recordEnabledStatus(
OnboardingPrefManager.getInstance().isBraveStatsEnabled());
}
@@ -2030,7 +2042,6 @@ public abstract class BraveActivity extends ChromeActivity
if (mTxService != null) mTxService.close();
if (mEthTxManagerProxy != null) mEthTxManagerProxy.close();
if (mSolanaTxManagerProxy != null) mSolanaTxManagerProxy.close();
if (mPrivacyHubMetrics != null) mPrivacyHubMetrics.close();
if (mBraveWalletService != null) mBraveWalletService.close();
mKeyringService = null;
mBlockchainRegistry = null;
@@ -2039,10 +2050,15 @@ public abstract class BraveActivity extends ChromeActivity
mEthTxManagerProxy = null;
mSolanaTxManagerProxy = null;
mAssetRatioService = null;
mPrivacyHubMetrics = null;
mBraveWalletService = null;
}
@Override
public void cleanUpPrivacyHubMetrics() {
if (mPrivacyHubMetrics != null) mPrivacyHubMetrics.close();
mPrivacyHubMetrics = null;
}
@NonNull
private BraveToolbarLayoutImpl getBraveToolbarLayout() {
BraveToolbarLayoutImpl layout = findViewById(R.id.toolbar);
@@ -13,7 +13,7 @@ import org.chromium.mojo.system.MojoException;
*/
public class BraveNewsConnectionErrorHandler implements ConnectionErrorHandler {
/**
*This is a delegate that is implement in the object where the connection is created
*This is a delegate that is implemented in the object where the connection is created
*/
public interface BraveNewsConnectionErrorHandlerDelegate {
default void initBraveNewsController() {}
@@ -0,0 +1,54 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */
package org.chromium.chrome.browser.misc_metrics;
import org.chromium.mojo.bindings.ConnectionErrorHandler;
import org.chromium.mojo.system.MojoException;
/**
* This is a handler for mojo connection failure for Privacy Hub Metrics
*/
public class PrivacyHubMetricsConnectionErrorHandler implements ConnectionErrorHandler {
/**
*This is a delegate that is implemented in the object where the connection is created
*/
public interface PrivacyHubMetricsConnectionErrorHandlerDelegate {
default void initPrivacyHubMetrics() {}
default void cleanUpPrivacyHubMetrics() {}
}
private PrivacyHubMetricsConnectionErrorHandlerDelegate
mPrivacyHubMetricsConnectionErrorHandlerDelegate;
private static final Object sLock = new Object();
private static PrivacyHubMetricsConnectionErrorHandler sInstance;
public static PrivacyHubMetricsConnectionErrorHandler getInstance() {
synchronized (sLock) {
if (sInstance == null) {
sInstance = new PrivacyHubMetricsConnectionErrorHandler();
}
}
return sInstance;
}
public void setDelegate(PrivacyHubMetricsConnectionErrorHandlerDelegate
privacyHubMetricsConnectionErrorHandlerDelegate) {
mPrivacyHubMetricsConnectionErrorHandlerDelegate =
privacyHubMetricsConnectionErrorHandlerDelegate;
assert mPrivacyHubMetricsConnectionErrorHandlerDelegate
!= null : "mPrivacyHubMetricsConnectionErrorHandlerDelegate has to be initialized";
}
@Override
public void onConnectionError(MojoException e) {
if (mPrivacyHubMetricsConnectionErrorHandlerDelegate == null) {
return;
}
mPrivacyHubMetricsConnectionErrorHandlerDelegate.cleanUpPrivacyHubMetrics();
mPrivacyHubMetricsConnectionErrorHandlerDelegate.initPrivacyHubMetrics();
}
}