File: auth_factor_store.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (121 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_AUTH_PANEL_IMPL_AUTH_FACTOR_STORE_H_
#define CHROMEOS_ASH_COMPONENTS_AUTH_PANEL_IMPL_AUTH_FACTOR_STORE_H_

#include <optional>
#include <string>

#include "base/callback_list.h"
#include "chromeos/ash/components/auth_panel/impl/auth_panel.h"
#include "chromeos/ash/components/auth_panel/impl/auth_panel_event_dispatcher.h"
#include "chromeos/ash/components/auth_panel/public/shared_types.h"
#include "chromeos/ash/components/osauth/public/common_types.h"

namespace ash {

class AuthHubConnector;
class AuthPanel;
class ImeController;

// This class encapsulates the UI state of `AuthPanel`.
class AuthFactorStore {
 public:
  struct State {
    enum class AuthenticationStage {
      kIdle,
      kAuthenticating,
      kAuthenticated,
      kMaxValue = kAuthenticated,
    };

    struct AuthTextfieldState {
      bool is_password_visible_ = false;
      bool is_read_only = false;
      std::string password_;
    };

    struct PasswordViewState {
      bool is_display_password_button_visible_ = true;
      bool is_factor_enabled_ = true;
      bool is_capslock_on_ = false;
      bool is_password_textfield_focused_ = true;
      AuthFactorState factor_state_;
      AuthTextfieldState auth_textfield_state_;

      explicit PasswordViewState(bool is_capslock_on);
      ~PasswordViewState();
    };

    State();
    ~State();

    void InitializePasswordViewState(bool is_capslock_on);
    void OnAshAuthFactorStateChanged(AshAuthFactor factor,
                                     AuthFactorState state);

    AuthenticationStage authentication_stage_ = AuthenticationStage::kIdle;
    std::optional<PasswordViewState> password_view_state_;
  };

  using OnStateUpdatedCallback =
      base::RepeatingCallback<void(const State& state)>;
  using OnStateUpdatedCallbackList =
      base::RepeatingCallbackList<void(const State& state)>;

  AuthFactorStore(ImeController* ime_controller,
                  AuthHubConnector* connector,
                  std::optional<AshAuthFactor> password_type,
                  AuthHub* auth_hub);
  ~AuthFactorStore();

  base::CallbackListSubscription Subscribe(OnStateUpdatedCallback callback);

  void OnUserAction(const AuthPanelEventDispatcher::UserAction& action);
  void OnFactorStateChanged(AshAuthFactor factor, AuthFactorState state);
  void OnAuthVerdict(AshAuthFactor factor,
                     AuthPanelEventDispatcher::AuthVerdict verdict);
 private:
  friend class AuthPanel::TestApi;

  void NotifyStateChanged();

  void SubmitPassword(const std::string& password);

  State state_;

  OnStateUpdatedCallbackList state_update_callbacks_;

  std::optional<AshAuthFactor> password_type_;

  raw_ptr<AuthHubConnector> auth_hub_connector_;

  auth_panel::SubmitPasswordCallback submit_password_callback_;

  raw_ptr<AuthHub> auth_hub_;
};

class AuthFactorStoreFactory {
 public:
  explicit AuthFactorStoreFactory(AuthHub* auth_hub) : auth_hub_(auth_hub) {}

  std::unique_ptr<AuthFactorStore> CreateAuthFactorStore(
      ImeController* ime_controller,
      AuthHubConnector* connector,
      std::optional<AshAuthFactor> password_type) {
    return std::make_unique<AuthFactorStore>(ime_controller, connector,
                                             password_type, auth_hub_);
  }

 private:
  // AuthHub is a long-lived, singleton object. It's created early in Ash's
  // lifecycle and destroyed late, after message loop stops. It is therefore
  // guaranteed to outlive `AuthFactorStoreFactory` and `AuthFactorStore`.
  raw_ptr<AuthHub> auth_hub_;
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_AUTH_PANEL_IMPL_AUTH_FACTOR_STORE_H_