#include "chrome/browser/ui/autofill/autofill_client_provider.h"
#include "base/check_deref.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "content/public/browser/web_contents.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "chrome/browser/autofill/android/android_autofill_availability_status.h"
#include "components/android_autofill/browser/android_autofill_client.h"
#include "components/prefs/android/pref_service_android.h"
#include "chrome/browser/autofill/android/jni_headers/AutofillClientProviderUtils_jni.h"
#endif
#include "arkweb/build/features/features.h"
#if BUILDFLAG(ARKWEB_AUTOFILL)
#include "cef/ohos_cef_ext/libcef/browser/autofill/oh_autofill_client.h"
#endif
namespace autofill {
namespace {
#if BUILDFLAG(IS_ANDROID)
void RecordAvailabilityStatus(AndroidAutofillAvailabilityStatus availability) {
base::UmaHistogramEnumeration("Autofill.AndroidAutofillAvailabilityStatus",
availability);
}
void RecordWhetherAndroidPrefResets(PrefService& prefs,
bool uses_platform_autofill) {
const bool will_reset_pref =
prefs.GetBoolean(prefs::kAutofillUsingVirtualViewStructure) &&
!uses_platform_autofill;
base::UmaHistogramBoolean("Autofill.ResetAutofillPrefToChrome",
will_reset_pref);
}
void SetSharedPrefForDeepLink() {
Java_AutofillClientProviderUtils_setAutofillOptionsDeepLinkPref(
base::android::AttachCurrentThread(),
base::FeatureList::IsEnabled(
autofill::features::kAutofillDeepLinkAutofillOptions));
}
void SetSharedPrefForSettingsContentProvider(bool uses_platform_autofill) {
if (base::FeatureList::IsEnabled(
autofill::features::kAutofillThirdPartyModeContentProvider)) {
Java_AutofillClientProviderUtils_setThirdPartyModePref(
base::android::AttachCurrentThread(), uses_platform_autofill);
} else {
Java_AutofillClientProviderUtils_unsetThirdPartyModePref(
base::android::AttachCurrentThread());
}
}
AndroidAutofillAvailabilityStatus GetAndroidAutofillAvailabilityStatus(
PrefService& prefs) {
return static_cast<AndroidAutofillAvailabilityStatus>(
Java_AutofillClientProviderUtils_getAndroidAutofillFrameworkAvailability(
base::android::AttachCurrentThread(), &prefs));
}
#endif
bool UsesVirtualViewStructureForAutofill(PrefService& prefs) {
#if BUILDFLAG(IS_ANDROID)
const AndroidAutofillAvailabilityStatus availability =
GetAndroidAutofillAvailabilityStatus(prefs);
RecordAvailabilityStatus(availability);
return availability == AndroidAutofillAvailabilityStatus::kAvailable;
#else
return false;
#endif
}
}
AutofillClientProvider::AutofillClientProvider(PrefService* prefs)
: uses_platform_autofill_(
UsesVirtualViewStructureForAutofill(CHECK_DEREF(prefs))) {
#if BUILDFLAG(IS_ANDROID)
RecordWhetherAndroidPrefResets(*prefs, uses_platform_autofill_);
prefs->SetBoolean(prefs::kAutofillUsingVirtualViewStructure,
uses_platform_autofill_);
SetSharedPrefForSettingsContentProvider(uses_platform_autofill_);
SetSharedPrefForDeepLink();
#endif
}
AutofillClientProvider::~AutofillClientProvider() = default;
void AutofillClientProvider::CreateClientForWebContents(
content::WebContents* web_contents) {
if (uses_platform_autofill()) {
#if BUILDFLAG(IS_ANDROID)
android_autofill::AndroidAutofillClient::CreateForWebContents(web_contents);
#else
NOTREACHED();
#endif
} else {
#if BUILDFLAG(ARKWEB_AUTOFILL)
autofill::OhAutofillClient::CreateForWebContents(web_contents);
#else
ChromeAutofillClient::CreateForWebContents(web_contents);
#endif
}
}
}
#if BUILDFLAG(IS_ANDROID)
DEFINE_JNI(AutofillClientProviderUtils)
#endif