File: autofill_client_provider.cc

package info (click to toggle)
chromium 141.0.7390.65-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,236 kB
  • sloc: cpp: 35,264,825; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,623; asm: 950,788; xml: 751,717; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (147 lines) | stat: -rw-r--r-- 5,781 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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 "base/android/jni_string.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/autofill/android/android_autofill_availability_status.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "components/android_autofill/browser/android_autofill_client.h"
#include "components/prefs/android/pref_service_android.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "chrome/browser/autofill/android/jni_headers/AutofillClientProviderUtils_jni.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace autofill {
namespace {

#if BUILDFLAG(IS_ANDROID)
void RecordAvailabilityStatus(AndroidAutofillAvailabilityStatus availability) {
  base::UmaHistogramEnumeration("Autofill.AndroidAutofillAvailabilityStatus",
                                availability);
}

// Counts how often the Chrome pref is reset because an platform autofill
// isn't allowed or doesn't fulfill all preconditions.
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);
}

// Retrieves the group for a synthetic trial. The group depends on whether the
// app package is in a server-provided allowlist for a11y compatibility filling.
std::string GetTrialGroupForPackage() {
  JNIEnv* env = base::android::AttachCurrentThread();
  return base::android::ConvertJavaStringToUTF8(
      env, Java_AutofillClientProviderUtils_getTrialGroupForPackage(env));
}

// Sets a ahread pref that allows to learn whether deep-links into Chrome's
// settings are available to use.
void SetSharedPrefForDeepLink() {
  Java_AutofillClientProviderUtils_setAutofillOptionsDeepLinkPref(
      base::android::AttachCurrentThread(),

      base::FeatureList::IsEnabled(
          autofill::features::kAutofillDeepLinkAutofillOptions));
}

// Sets a shared pref that allows external apps to use a ContentResolver to
// figure out whether Chrome is using platform autofill over the default.
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  // BUILDFLAG(IS_ANDROID)

bool UsesVirtualViewStructureForAutofill(PrefService& prefs) {
#if BUILDFLAG(IS_ANDROID)
  const AndroidAutofillAvailabilityStatus availability =
      GetAndroidAutofillAvailabilityStatus(prefs);
  RecordAvailabilityStatus(availability);
  return availability == AndroidAutofillAvailabilityStatus::kAvailable;
#else
  return false;
#endif  // BUILDFLAG(IS_ANDROID)
}

}  // namespace

AutofillClientProvider::AutofillClientProvider(PrefService* prefs)
    : uses_platform_autofill_(
          UsesVirtualViewStructureForAutofill(CHECK_DEREF(prefs))) {
#if BUILDFLAG(IS_ANDROID)
  DelayRegisteringFieldTrialForA11yDeprecation();
  RecordWhetherAndroidPrefResets(*prefs, uses_platform_autofill_);
  // Ensure the pref is reset if platform autofill is restricted.
  prefs->SetBoolean(prefs::kAutofillUsingVirtualViewStructure,
                    uses_platform_autofill_);
  SetSharedPrefForSettingsContentProvider(uses_platform_autofill_);
  SetSharedPrefForDeepLink();
#endif  // BUILDFLAG(IS_ANDROID)
}

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 {
    ChromeAutofillClient::CreateForWebContents(web_contents);
  }
}

#if BUILDFLAG(IS_ANDROID)
void AutofillClientProvider::RegisterSyntheticFieldTrialForPackage(
    const std::string& package) {
  ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial(
      "SyntheticAutofillViaA11yDeprecated", package,
      variations::SyntheticTrialAnnotationMode::kCurrentLog);
}

void AutofillClientProvider::DelayRegisteringFieldTrialForA11yDeprecation() {
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::TaskPriority::LOWEST},
      base::BindOnce(&GetTrialGroupForPackage),
      base::BindOnce(
          &AutofillClientProvider::RegisterSyntheticFieldTrialForPackage,
          weak_ptr_factory_.GetWeakPtr()));
}
#endif  // BUILDFLAG(IS_ANDROID)

}  // namespace autofill