File: password_generation_popup_controller_impl.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; 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-- 8,752 bytes parent folder | download | duplicates (6)
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 CHROME_BROWSER_UI_PASSWORDS_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_
#define CHROME_BROWSER_UI_PASSWORDS_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_

#include <stddef.h>

#include <memory>
#include <string>
#include <vector>

#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "build/build_config.h"
#include "chrome/browser/ui/autofill/popup_controller_common.h"
#include "chrome/browser/ui/passwords/password_generation_popup_controller.h"
#include "components/autofill/core/browser/ui/popup_open_enums.h"
#include "components/autofill/core/common/signatures.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/password_manager/core/browser/password_form.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/native_widget_types.h"
#include "components/zoom/zoom_observer.h"

namespace content {
class WebContents;
}  // namespace content

namespace input {
struct NativeWebKeyboardEvent;
}  // namespace input

namespace password_manager {
class PasswordManagerDriver;
}  // namespace password_manager

namespace autofill {
class FormData;
namespace password_generation {
enum class PasswordGenerationType;
struct PasswordGenerationUIData;
}  // namespace password_generation
}  // namespace autofill

class PasswordGenerationPopupObserver;
class PasswordGenerationPopupView;

// This class controls a PasswordGenerationPopupView. It is responsible for
// determining the location of the popup, handling keypress events while the
// popup is active, and notifying both the renderer and the password manager
// if the password is accepted.
class PasswordGenerationPopupControllerImpl
    : public PasswordGenerationPopupController,
      public content::WebContentsObserver,
      public zoom::ZoomObserver {
 public:
  // Create a controller or return |previous| if it is suitable. Will hide
  // |previous| if it is not returned. |bounds| is the bounds of the element
  // that we are showing the dropdown for in screen space. |ui_data| contains
  // parameters for generation a passwords. If not NULL, |observer| will be
  // notified of changes of the popup state.
  static base::WeakPtr<PasswordGenerationPopupControllerImpl> GetOrCreate(
      base::WeakPtr<PasswordGenerationPopupControllerImpl> previous,
      const gfx::RectF& bounds,
      const autofill::password_generation::PasswordGenerationUIData& ui_data,
      const base::WeakPtr<password_manager::PasswordManagerDriver>& driver,
      PasswordGenerationPopupObserver* observer,
      content::WebContents* web_contents,
      content::RenderFrameHost* frame);

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

  ~PasswordGenerationPopupControllerImpl() override;

  // Generate the password string and store it in `current_generated_password_`.
  void GeneratePasswordValue(
      autofill::password_generation::PasswordGenerationType generation_type);

  // Create a PasswordGenerationPopupView if one doesn't already exist.
  void Show(GenerationUIState state);

  // Update the value of the generated password to be displayed in the UI (e.g.
  // upon editing the generated password).
  void UpdateGeneratedPassword(std::u16string new_password);

  // Hides the popup, since its position is no longer valid.
  void FrameWasScrolled();

  // Hides the popup, since the generation element for which it was shown
  // is no longer focused.
  void GenerationElementLostFocus();

  // The generated password counts as rejected either if the user ignores the
  // popup and types a password, or if the generated password is deleted.
  // In both cases the popups should be hidden. In the latter case, a new popup
  // might be shown offering another password if generation is offered
  // automatically on that field.
  void GeneratedPasswordRejected();

  base::WeakPtr<PasswordGenerationPopupControllerImpl> GetWeakPtr();

  // content::WebContentsObserver overrides
  void WebContentsDestroyed() override;
  void PrimaryPageChanged(content::Page& page) override;

  // Returns true if the popup is visible, or false otherwise.
  bool IsVisible() const;

  // ZoomObserver:
  void OnZoomControllerDestroyed(
      zoom::ZoomController* zoom_controller) override;
  void OnZoomChanged(
      const zoom::ZoomController::ZoomChangedEventData& data) override;

#if defined(UNIT_TEST)
  PasswordGenerationPopupView* view() const { return view_; }
  void SetViewForTesting(PasswordGenerationPopupView* view) { view_ = view; }
  void SelectAcceptButtonForTesting() {
    SelectElement(PasswordGenerationPopupElement::kAcceptButton);
  }
  void SelectCancelButtonForTesting() {
    SelectElement(PasswordGenerationPopupElement::kCancelButton);
  }
#endif

 protected:
  PasswordGenerationPopupControllerImpl(
      const gfx::RectF& bounds,
      const autofill::password_generation::PasswordGenerationUIData& ui_data,
      const base::WeakPtr<password_manager::PasswordManagerDriver>& driver,
      PasswordGenerationPopupObserver* observer,
      content::WebContents* web_contents,
      content::RenderFrameHost* frame);

 private:
  class KeyPressRegistrator;

  // Defines different elements of the popup that can be selected.
  enum class PasswordGenerationPopupElement {
    kNone = 0,
    kAcceptButton = 1,
    kCancelButton = 2,
  };

  // AutofillPopupViewDelegate implementation:
  void Hide(autofill::SuggestionHidingReason) override;
  void ViewDestroyed() override;
  gfx::NativeView container_view() const override;
  content::WebContents* GetWebContents() const override;
  const gfx::RectF& element_bounds() const override;
  autofill::PopupAnchorType anchor_type() const override;
  base::i18n::TextDirection GetElementTextDirection() const override;

  // PasswordGenerationPopupController implementation:
  void PasswordAccepted() override;
  void PasswordRejected() override;
  std::u16string GetPrimaryAccountEmail() override;
  GenerationUIState state() const override;
  bool accept_button_selected() const override;
  bool cancel_button_selected() const override;
  const std::u16string& password() const override;
  std::u16string SuggestedText() const override;
  const std::u16string& HelpText() const override;

  void HideImpl();

  bool HandleKeyPressEvent(const input::NativeWebKeyboardEvent& event);

  // Whether the elements of popup are selectable (true in generation state).
  bool IsSelectable() const;
  // Sets currently selected popup element.
  void SelectElement(PasswordGenerationPopupElement element);
  // Accepts currently selected element. No-op if no element is selected.
  bool PossiblyAcceptSelectedElement();

  // Handle to the popup. May be NULL if popup isn't showing.
  raw_ptr<PasswordGenerationPopupView> view_;

  const autofill::FormData form_data_;

  base::WeakPtr<password_manager::PasswordManagerDriver> const driver_;

  // May be NULL.
  const raw_ptr<PasswordGenerationPopupObserver> observer_;

  // Signature of the form for which password generation is triggered.
  const autofill::FormSignature form_signature_;

  // Signature of the field for which password generation is triggered.
  const autofill::FieldSignature field_signature_;

  // Renderer ID of the generation element.
  const autofill::FieldRendererId generation_element_id_;

  // Maximum length of the password to be generated. 0 represents an unbound
  // maximum length.
  const uint32_t max_length_;

  // Contains common popup data.
  const autofill::PopupControllerCommon controller_common_;

  // Help text in the footer.
  std::u16string help_text_;

  // The current password that is considered generated. This is the password to
  // be displayed in the user generation dialog.
  std::u16string current_generated_password_;

  // Currently selected / highlighted element of the popup.
  PasswordGenerationPopupElement selected_element_ =
      PasswordGenerationPopupElement::kNone;

  // The state of the generation popup.
  GenerationUIState state_;

  std::unique_ptr<KeyPressRegistrator> key_press_handler_manager_;

  base::ScopedObservation<zoom::ZoomController, zoom::ZoomObserver>
      zoom_observation_{this};

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

#endif  // CHROME_BROWSER_UI_PASSWORDS_PASSWORD_GENERATION_POPUP_CONTROLLER_IMPL_H_