1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ASH_IN_SESSION_AUTH_AUTH_DIALOG_CONTENTS_VIEW_H_
6 #define ASH_IN_SESSION_AUTH_AUTH_DIALOG_CONTENTS_VIEW_H_
7 
8 #include <string>
9 
10 #include "ash/login/ui/login_palette.h"
11 #include "ash/public/cpp/login_types.h"
12 #include "ui/views/view.h"
13 
14 namespace views {
15 class BoxLayout;
16 class Label;
17 class LabelButton;
18 class MdTextButton;
19 }  // namespace views
20 
21 namespace ash {
22 
23 class AnimatedRoundedImageView;
24 class LoginPasswordView;
25 class LoginPinView;
26 class LoginPinInputView;
27 
28 // Contains the debug views that allows the developer to interact with the
29 // AuthDialogController.
30 class AuthDialogContentsView : public views::View {
31  public:
32   // Flags which describe the set of currently visible auth methods.
33   enum AuthMethods {
34     kAuthNone = 0,              // No auth methods.
35     kAuthPassword = 1 << 0,     // Display password.
36     kAuthPin = 1 << 1,          // Display PIN keyboard.
37     kAuthFingerprint = 1 << 2,  // Use fingerprint to unlock.
38   };
39 
40   // Extra control parameters to be passed when setting the auth methods.
41   struct AuthMethodsMetadata {
42     // User's pin length to use for autosubmit.
43     size_t autosubmit_pin_length = 0;
44   };
45 
46   AuthDialogContentsView(uint32_t auth_methods,
47                          const std::string& origin_name,
48                          const AuthMethodsMetadata& auth_metadata,
49                          const UserAvatar& avatar);
50   AuthDialogContentsView(const AuthDialogContentsView&) = delete;
51   AuthDialogContentsView& operator=(const AuthDialogContentsView&) = delete;
52   ~AuthDialogContentsView() override;
53 
54   // views::Views:
55   void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
56   void RequestFocus() override;
57 
auth_methods()58   uint32_t auth_methods() const { return auth_methods_; }
59 
60  private:
61   class TitleLabel;
62   class FingerprintView;
63 
64   // views::View:
65   void AddedToWidget() override;
66 
67   // Add a view for user avatar.
68   void AddAvatarView(const UserAvatar& avatar);
69 
70   // Add a view for dialog title.
71   void AddTitleView();
72 
73   // Add a view that shows which website/app we are authenticating for.
74   void AddOriginNameView();
75 
76   // Add a view for entering PIN (if autosubmit is off).
77   void AddPinTextInputView();
78 
79   // Add a PIN pad view.
80   void AddPinPadView();
81 
82   // Add a PIN input view that automatically submits PIN.
83   void AddPinDigitInputView();
84 
85   // Initializes password input field functionality.
86   void InitPasswordView();
87 
88   // Add a vertical spacing view.
89   void AddVerticalSpacing(int height);
90 
91   // Add a view for action buttons.
92   void AddActionButtonsView();
93 
94   // Called when the user taps a digit on the PIN pad.
95   void OnInsertDigitFromPinPad(int digit);
96 
97   // Called when the user taps backspace on the PIN pad.
98   void OnBackspaceFromPinPad();
99 
100   // Called when either:
101   // 1. the user inserts or deletes a character in
102   // |pin_text_input_view_| or |pin_digit_input_view_| without using the PIN
103   // pad, or
104   // 2. contents of |pin_text_input_view_| or |pin_digit_input_view_| are
105   // cleared by a Reset() call.
106   void OnPinTextChanged(bool is_empty);
107 
108   // Called when the user submits password or PIN.
109   void OnAuthSubmit(const base::string16& password);
110 
111   // Called when PIN authentication of the user completes.
112   void OnPinAuthComplete(base::Optional<bool> success);
113 
114   // Called when fingerprint authentication completes.
115   void OnFingerprintAuthComplete(bool success,
116                                  FingerprintState fingerprint_state);
117 
118   // Called when the cancel button is pressed.
119   void OnCancelButtonPressed(const ui::Event& event);
120 
121   // Called when the "Need help?" button is pressed.
122   void OnNeedHelpButtonPressed(const ui::Event& event);
123 
124   // Debug container which holds the entire debug UI.
125   views::View* container_ = nullptr;
126 
127   // Layout for |container_|.
128   views::BoxLayout* main_layout_ = nullptr;
129 
130   // User avatar to indicate this is an OS dialog.
131   AnimatedRoundedImageView* avatar_view_ = nullptr;
132 
133   // Title of the auth dialog, also used to show PIN auth error message..
134   TitleLabel* title_ = nullptr;
135 
136   // Prompt message to the user.
137   views::Label* origin_name_view_ = nullptr;
138 
139   // Whether PIN can be auto submitted.
140   bool pin_autosubmit_on_ = false;
141 
142   // Number of PIN attempts so far.
143   int pin_attempts_ = 0;
144 
145   // Text input field for PIN if PIN cannot be auto submitted.
146   LoginPasswordView* pin_text_input_view_ = nullptr;
147 
148   // PIN input view that's shown if PIN can be auto submitted.
149   LoginPinInputView* pin_digit_input_view_ = nullptr;
150 
151   // PIN pad view.
152   LoginPinView* pin_pad_view_ = nullptr;
153 
154   FingerprintView* fingerprint_view_ = nullptr;
155 
156   // A button to cancel authentication and close the dialog.
157   views::MdTextButton* cancel_button_ = nullptr;
158 
159   // A button to show a help center article.
160   views::LabelButton* help_button_ = nullptr;
161 
162   // Flags of auth methods that should be visible.
163   uint32_t auth_methods_ = 0u;
164 
165   const std::string origin_name_;
166 
167   // Extra parameters to control the UI.
168   AuthMethodsMetadata auth_metadata_;
169 
170   LoginPalette palette_ = CreateInSessionAuthPalette();
171 
172   // Container which holds action buttons.
173   views::View* action_view_container_ = nullptr;
174 
175   base::WeakPtrFactory<AuthDialogContentsView> weak_factory_{this};
176 };
177 
178 }  // namespace ash
179 
180 #endif  // ASH_IN_SESSION_AUTH_AUTH_DIALOG_CONTENTS_VIEW_H_
181