File: autofill_ai_manager.h

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 (127 lines) | stat: -rw-r--r-- 5,434 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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.

#ifndef COMPONENTS_AUTOFILL_AI_CORE_BROWSER_AUTOFILL_AI_MANAGER_H_
#define COMPONENTS_AUTOFILL_AI_CORE_BROWSER_AUTOFILL_AI_MANAGER_H_

#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/uuid.h"
#include "components/autofill/core/browser/data_model/autofill_ai/entity_instance.h"
#include "components/autofill/core/browser/integrators/autofill_ai/autofill_ai_delegate.h"
#include "components/autofill/core/browser/strike_databases/autofill_ai/autofill_ai_save_strike_database_by_attribute.h"
#include "components/autofill/core/browser/strike_databases/autofill_ai/autofill_ai_save_strike_database_by_host.h"
#include "components/autofill/core/browser/strike_databases/autofill_ai/autofill_ai_update_strike_database.h"
#include "components/autofill/core/common/aliases.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/autofill_ai/core/browser/autofill_ai_client.h"
#include "components/autofill_ai/core/browser/metrics/autofill_ai_logger.h"

namespace autofill {
class FormData;
class FormStructure;
class LogManager;
class StrikeDatabase;
}  // namespace autofill

namespace base {
class Uuid;
}  // namespace base

namespace autofill_ai {

// The class for embedder-independent, tab-specific Autofill AI logic. This
// class is an interface.
class AutofillAiManager : public autofill::AutofillAiDelegate {
 public:
  AutofillAiManager(AutofillAiClient* client,
                    autofill::StrikeDatabase* strike_database);
  AutofillAiManager(const AutofillAiManager&) = delete;
  AutofillAiManager& operator=(const AutofillAiManager&) = delete;
  ~AutofillAiManager() override;

  // autofill::AutofillAiDelegate:
  std::vector<autofill::Suggestion> GetSuggestions(
      autofill::FormGlobalId form_global_id,
      autofill::FieldGlobalId field_global_id) override;
  bool OnFormSubmitted(const autofill::FormStructure& form,
                       ukm::SourceId ukm_source_id) override;
  bool ShouldDisplayIph(autofill::FormGlobalId form,
                        autofill::FieldGlobalId field) const override;
  void OnSuggestionsShown(const autofill::FormStructure& form,
                          const autofill::AutofillField& field,
                          ukm::SourceId ukm_source_id) override;
  void OnFormSeen(const autofill::FormStructure& form) override;
  void OnDidFillSuggestion(
      const base::Uuid& guid,
      const autofill::FormStructure& form,
      const autofill::AutofillField& trigger_field,
      base::span<const autofill::AutofillField* const> filled_fields,
      ukm::SourceId ukm_source_id) override;
  void OnEditedAutofilledField(const autofill::FormStructure& form,
                               const autofill::AutofillField& field,
                               ukm::SourceId ukm_source_id) override;

  base::WeakPtr<AutofillAiManager> GetWeakPtr();

 private:
  friend class AutofillAiManagerTestApi;

  // Strike database related methods:
  void AddStrikeForSaveAttempt(const GURL& url,
                               const autofill::EntityInstance& entity);
  void AddStrikeForUpdateAttempt(const base::Uuid& entity_uuid);
  void ClearStrikesForSave(const GURL& url,
                           const autofill::EntityInstance& entity);
  void ClearStrikesForUpdate(const base::Uuid& entity_uuid);
  bool IsSaveBlockedByStrikeDatabase(
      const GURL& url,
      const autofill::EntityInstance& entity) const;
  bool IsUpdateBlockedByStrikeDatabase(const base::Uuid& entity_uuid) const;

  // Attempts to display an import bubble for `form` if Autofill AI is
  // interested in the form. Returns whether an import bubble will be shown.
  bool MaybeImportForm(const autofill::FormStructure& form);

  // Updates the `EntityDataManager` and the save strike database depending on
  // the prompt `result`.
  void HandleSavePromptResult(
      const GURL& form_url,
      const autofill::EntityInstance& entity,
      AutofillAiClient::SaveOrUpdatePromptResult result);
  // Updates the `EntityDataManager` and the update strike database depending on
  // the prompt `result`.
  void HandleUpdatePromptResult(
      const base::Uuid& entity_uuid,
      AutofillAiClient::SaveOrUpdatePromptResult result);

  autofill::LogManager* GetCurrentLogManager();

  // A raw reference to the client, which owns `this` and therefore outlives
  // it.
  const raw_ref<AutofillAiClient> client_;

  // Logger that records various Autofill AI metrics.
  AutofillAiLogger logger_{&*client_};

  // A strike database for save prompts keyed by (entity_type_name, host).
  std::unique_ptr<autofill::AutofillAiSaveStrikeDatabaseByHost>
      save_strike_db_by_host_;

  // A strike database for save prompts keyed by (entity_type_name,
  // attribute_type_name_1, attribute_value_1, ...).
  std::unique_ptr<autofill::AutofillAiSaveStrikeDatabaseByAttribute>
      save_strike_db_by_attribute_;

  // A strike database for update prompts keyed by the guid of the entity that
  // is to be updated.
  std::unique_ptr<autofill::AutofillAiUpdateStrikeDatabase> update_strike_db_;

  base::WeakPtrFactory<AutofillAiManager> weak_ptr_factory_{this};
};

}  // namespace autofill_ai

#endif  // COMPONENTS_AUTOFILL_AI_CORE_BROWSER_AUTOFILL_AI_MANAGER_H_