File: chrome_autofill_ai_client.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,665 bytes parent folder | download | duplicates (2)
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
// 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/autofill_ai/chrome_autofill_ai_client.h"

#include "base/check_deref.h"
#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/autofill/autofill_entity_data_manager_factory.h"
#include "chrome/browser/autofill/strike_database_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/signin_util.h"
#include "chrome/browser/ui/autofill/autofill_ai/save_or_update_autofill_ai_data_controller.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/data_model/autofill_ai/entity_instance.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_types.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill_ai/core/browser/autofill_ai_client.h"
#include "components/autofill_ai/core/browser/autofill_ai_manager.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_contents.h"

ChromeAutofillAiClient::ChromeAutofillAiClient(
    content::WebContents* web_contents)
    : web_contents_(CHECK_DEREF(web_contents)),
      prediction_improvements_manager_{
          this,
          autofill::StrikeDatabaseFactory::GetForProfile(
              Profile::FromBrowserContext(web_contents->GetBrowserContext())),
      } {
  DCHECK(base::FeatureList::IsEnabled(
      autofill::features::kAutofillAiWithDataSchema));
}

ChromeAutofillAiClient::~ChromeAutofillAiClient() = default;

// static
std::unique_ptr<ChromeAutofillAiClient>
ChromeAutofillAiClient::MaybeCreateForWebContents(
    content::WebContents* web_contents) {
  if (!base::FeatureList::IsEnabled(
          autofill::features::kAutofillAiWithDataSchema)) {
    return nullptr;
  }
  return base::WrapUnique<ChromeAutofillAiClient>(
      new ChromeAutofillAiClient(web_contents));
}

autofill::ContentAutofillClient& ChromeAutofillAiClient::GetAutofillClient() {
  // TODO: crbug.com/371534239 - Make the lifecycle relationships explicit.
  return CHECK_DEREF(
      autofill::ContentAutofillClient::FromWebContents(&*web_contents_));
}

autofill_ai::AutofillAiManager& ChromeAutofillAiClient::GetManager() {
  return prediction_improvements_manager_;
}

autofill::EntityDataManager* ChromeAutofillAiClient::GetEntityDataManager() {
  Profile* profile =
      Profile::FromBrowserContext(web_contents_->GetBrowserContext());
  return profile ? autofill::AutofillEntityDataManagerFactory::GetForProfile(
                       profile)
                 : nullptr;
}

void ChromeAutofillAiClient::ShowSaveOrUpdateBubble(
    autofill::EntityInstance new_entity,
    std::optional<autofill::EntityInstance> old_entity,
    SaveOrUpdatePromptResultCallback prompt_acceptance_callback) {
#if !BUILDFLAG(IS_ANDROID)
  if (auto* controller =
          autofill_ai::SaveOrUpdateAutofillAiDataController::GetOrCreate(
              &*web_contents_, GetAutofillClient().GetAppLocale())) {
    controller->ShowPrompt(std::move(new_entity), std::move(old_entity),
                           std::move(prompt_acceptance_callback));
    return;
  }
#endif  // !BUILDFLAG(IS_ANDROID)
  std::move(prompt_acceptance_callback).Run(SaveOrUpdatePromptResult());
}

autofill::FormStructure* ChromeAutofillAiClient::GetCachedFormStructure(
    const autofill::FormGlobalId& form_id) {
  autofill::ContentAutofillDriver* driver =
      autofill::ContentAutofillDriver::GetForRenderFrameHost(
          web_contents_->GetPrimaryMainFrame());
  if (!driver) {
    return nullptr;
  }
  return driver->GetAutofillManager().FindCachedFormById(form_id);
}

optimization_guide::ModelQualityLogsUploaderService*
ChromeAutofillAiClient::GetMqlsUploadService() {
  Profile* profile =
      Profile::FromBrowserContext(web_contents_->GetBrowserContext());
  OptimizationGuideKeyedService* optimization_guide_keyed_service =
      OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
  if (!optimization_guide_keyed_service) {
    return nullptr;
  }
  return optimization_guide_keyed_service->GetModelQualityLogsUploaderService();
}