File: content_password_manager_driver.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 (234 lines) | stat: -rw-r--r-- 10,580 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright 2014 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_PASSWORD_MANAGER_CONTENT_BROWSER_CONTENT_PASSWORD_MANAGER_DRIVER_H_
#define COMPONENTS_PASSWORD_MANAGER_CONTENT_BROWSER_CONTENT_PASSWORD_MANAGER_DRIVER_H_

#include <map>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/autofill/content/common/mojom/autofill_agent.mojom.h"
#include "components/autofill/content/common/mojom/autofill_driver.mojom.h"
#include "components/autofill/core/common/password_form_generation_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/password_manager/core/browser/password_autofill_manager.h"
#include "components/password_manager/core/browser/password_generation_frame_helper.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/password_manager/core/browser/password_manager_driver.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"

namespace content {
class RenderFrameHost;
}

namespace password_manager {

// There is one ContentPasswordManagerDriver per RenderFrameHost.
// The lifetime is managed by the ContentPasswordManagerDriverFactory.
class ContentPasswordManagerDriver final
    : public PasswordManagerDriver,
      public autofill::mojom::PasswordManagerDriver {
 public:
  ContentPasswordManagerDriver(content::RenderFrameHost* render_frame_host,
                               PasswordManagerClient* client);

  ContentPasswordManagerDriver(const ContentPasswordManagerDriver&) = delete;
  ContentPasswordManagerDriver& operator=(const ContentPasswordManagerDriver&) =
      delete;

  ~ContentPasswordManagerDriver() override;

  // Gets the driver for `render_frame_host`.
  static ContentPasswordManagerDriver* GetForRenderFrameHost(
      content::RenderFrameHost* render_frame_host);

  void BindPendingReceiver(
      mojo::PendingAssociatedReceiver<autofill::mojom::PasswordManagerDriver>
          pending_receiver);
  void DidNavigate();

  // PasswordManagerDriver implementation.
  int GetId() const override;
  void PropagateFillDataOnParsingCompletion(
      const autofill::PasswordFormFillData& form_data) override;
  void InformNoSavedCredentials(
      bool should_show_popup_without_passwords) override;
  void FormEligibleForGenerationFound(
      const autofill::PasswordFormGenerationData& form) override;
  void GeneratedPasswordAccepted(const std::u16string& password) override;
  void GeneratedPasswordAccepted(
      const autofill::FormData& form_data,
      autofill::FieldRendererId generation_element_id,
      const std::u16string& password) override;
  void GeneratedPasswordRejected() override;
  void FocusNextFieldAfterPasswords() override;
  void FillField(
      const std::u16string& value,
      autofill::AutofillSuggestionTriggerSource suggestion_source) override;
  void FillChangePasswordForm(
      autofill::FieldRendererId password_element_id,
      autofill::FieldRendererId new_password_element_id,
      autofill::FieldRendererId confirm_password_element_id,
      const std::u16string& old_password,
      const std::u16string& new_password,
      base::OnceCallback<void(const std::optional<autofill::FormData>&)>
          form_data_callback) override;
  void SubmitFormWithEnter(
      autofill::FieldRendererId field,
      base::OnceCallback<void(bool)> success_callback) override;
  void FillSuggestion(const std::u16string& username,
                      const std::u16string& password,
                      base::OnceCallback<void(bool)> success_callback) override;
  void FillSuggestionById(
      autofill::FieldRendererId username_element_id,
      autofill::FieldRendererId password_element_id,
      const std::u16string& username,
      const std::u16string& password,
      autofill::AutofillSuggestionTriggerSource suggestion_source) override;
  void FillIntoFocusedField(bool is_password,
                            const std::u16string& credential) override;
#if BUILDFLAG(IS_ANDROID)
  void TriggerFormSubmission() override;
#endif
  void PreviewField(autofill::FieldRendererId field_id,
                    const std::u16string& value) override;
  void PreviewSuggestion(const std::u16string& username,
                         const std::u16string& password) override;
  void PreviewSuggestionById(autofill::FieldRendererId username_element_id,
                             autofill::FieldRendererId password_element_id,
                             const std::u16string& username,
                             const std::u16string& password) override;
  void PreviewGenerationSuggestion(const std::u16string& password) override;
  void ClearPreviewedForm() override;
  void SetSuggestionAvailability(autofill::FieldRendererId element_id,
                                 autofill::mojom::AutofillSuggestionAvailability
                                     suggestion_availability) override;
  PasswordGenerationFrameHelper* GetPasswordGenerationHelper() override;
  PasswordManagerInterface* GetPasswordManager() override;
  PasswordAutofillManager* GetPasswordAutofillManager() override;
  void SendLoggingAvailability() override;
  bool IsInPrimaryMainFrame() const override;
  bool CanShowAutofillUi() const override;
  int GetFrameId() const override;
  const GURL& GetLastCommittedURL() const override;
  void AnnotateFieldsWithParsingResult(
      const autofill::ParsingResult& parsing_result) override;
  base::WeakPtr<password_manager::PasswordManagerDriver> AsWeakPtr() override;

  base::WeakPtr<ContentPasswordManagerDriver> AsWeakPtrImpl() {
    return weak_factory_.GetWeakPtr();
  }

  // Notify the renderer that the user wants to trigger password generation.
  void GeneratePassword(autofill::mojom::PasswordGenerationAgent::
                            TriggeredGeneratePasswordCallback callback);

  // Notify the associated `PasswordAutofillManager` that password suggestions
  // for the triggering field can be shown.
  void ShowPasswordSuggestionsForField(
      const autofill::TriggeringField& triggering_field);

  // Returns true if the field is any of the following cases:
  // 1) The field has type="password".
  // 2) The field was type="password" field at some point of time.
  // 3) The field has a variation of the word "password" in name/id attributes.
  // 4) The server predicts the field as new password field.
  bool IsPasswordFieldForPasswordManager(
      autofill::FieldRendererId field_renderer_id,
      std::optional<blink::mojom::FormControlType> form_control_type);

  content::RenderFrameHost* render_frame_host() const {
    return render_frame_host_;
  }

#if defined(UNIT_TEST)
  // Exposed to allow browser tests to hook the driver.
  mojo::AssociatedReceiver<autofill::mojom::PasswordManagerDriver>&
  ReceiverForTesting() {
    return password_manager_receiver_;
  }
#endif

 protected:
  // autofill::mojom::PasswordManagerDriver:
  // Note that these messages received from a potentially compromised renderer.
  // For that reason, any access to form data should be validated via
  // bad_message::CheckChildProcessSecurityPolicy. Further, messages should not
  // be sent from a prerendered page, so we will similarly validate calls to
  // ensure that this is not the case.
  void PasswordFormsParsed(
      const std::vector<autofill::FormData>& forms_data) override;
  void PasswordFormsRendered(
      const std::vector<autofill::FormData>& visible_forms_data) override;
  void PasswordFormSubmitted(const autofill::FormData& form_data) override;
  void InformAboutUserInput(const autofill::FormData& form_data) override;
  void DynamicFormSubmission(autofill::mojom::SubmissionIndicatorEvent
                                 submission_indication_event) override;
  void PasswordFormCleared(const autofill::FormData& form_data) override;
  void RecordSavePasswordProgress(const std::string& log) override;
  void UserModifiedPasswordField() override;
  void UserModifiedNonPasswordField(autofill::FieldRendererId renderer_id,
                                    const std::u16string& value,
                                    bool autocomplete_attribute_has_username,
                                    bool is_likely_otp) override;
  void ShowPasswordSuggestions(
      const autofill::PasswordSuggestionRequest& request) override;
  void CheckSafeBrowsingReputation(const GURL& form_action,
                                   const GURL& frame_url) override;
  void FocusedInputChanged(
      autofill::FieldRendererId focused_field_id,
      autofill::mojom::FocusedFieldType focused_field_type) override;
  void LogFirstFillingResult(autofill::FormRendererId form_renderer_id,
                             int32_t result) override;

 private:
  void LogFilledFieldType();

  const mojo::AssociatedRemote<autofill::mojom::AutofillAgent>&
  GetAutofillAgent();

  const mojo::AssociatedRemote<autofill::mojom::PasswordAutofillAgent>&
  GetPasswordAutofillAgent();

  const mojo::AssociatedRemote<autofill::mojom::PasswordGenerationAgent>&
  GetPasswordGenerationAgent();

  void OnChangePasswordFormFilled(
      base::OnceCallback<void(const std::optional<autofill::FormData>&)>
          form_data_callback,
      const std::optional<autofill::FormData>& raw_form);

  const raw_ptr<content::RenderFrameHost> render_frame_host_;
  const raw_ptr<PasswordManagerClient> client_;
  PasswordGenerationFrameHelper password_generation_helper_;
  PasswordAutofillManager password_autofill_manager_;

  int id_;
  autofill::FieldRendererId last_triggering_field_id_;

  mojo::AssociatedRemote<autofill::mojom::PasswordAutofillAgent>
      password_autofill_agent_;
  const mojo::AssociatedRemote<autofill::mojom::PasswordAutofillAgent>
      password_autofill_agent_unbound_;

  mojo::AssociatedRemote<autofill::mojom::PasswordGenerationAgent>
      password_gen_agent_;

  mojo::AssociatedReceiver<autofill::mojom::PasswordManagerDriver>
      password_manager_receiver_{this};

  base::WeakPtrFactory<ContentPasswordManagerDriver> weak_factory_{this};
};

}  // namespace password_manager

#endif  // COMPONENTS_PASSWORD_MANAGER_CONTENT_BROWSER_CONTENT_PASSWORD_MANAGER_DRIVER_H_