OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 CHROME_BROWSER_CHROMEOS_UI_REQUEST_PIN_VIEW_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_UI_REQUEST_PIN_VIEW_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "ui/base/ui_base_types.h" |
| 16 #include "ui/views/controls/label.h" |
| 17 #include "ui/views/controls/textfield/textfield.h" |
| 18 #include "ui/views/controls/textfield/textfield_controller.h" |
| 19 #include "ui/views/view.h" |
| 20 #include "ui/views/window/dialog_delegate.h" |
| 21 |
| 22 namespace views { |
| 23 class Label; |
| 24 } |
| 25 |
| 26 namespace chromeos { |
| 27 |
| 28 class PassphraseTextfield; |
| 29 |
| 30 // A dialog box for requesting PIN code. Instances of this class are managed by |
| 31 // PinDialogManager. |
| 32 class RequestPinView : public views::DialogDelegateView, |
| 33 public views::TextfieldController { |
| 34 public: |
| 35 enum RequestPinCodeType { PIN, PUK, UNCHANGED }; |
| 36 |
| 37 enum RequestPinErrorType { |
| 38 NONE, |
| 39 INVALID_PIN, |
| 40 INVALID_PUK, |
| 41 MAX_ATTEMPTS_EXCEEDED, |
| 42 UNKNOWN_ERROR |
| 43 }; |
| 44 |
| 45 class Delegate { |
| 46 public: |
| 47 // Notification when user closes the PIN dialog. |
| 48 virtual void OnPinDialogClosed() = 0; |
| 49 }; |
| 50 |
| 51 // Used to send the PIN/PUK entered by the user in the textfield to the |
| 52 // extension that asked for the code. |
| 53 using RequestPinCallback = base::Callback<void(const base::string16&)>; |
| 54 |
| 55 // Creates the view to be embeded in the dialog that requests the PIN/PUK. |
| 56 // |extension_name| - the name of the extension making the request. Displayed |
| 57 // in the title and in the header of the view. |
| 58 // |code_type| - the type of code requested, PIN or PUK. UNCHANGED is not |
| 59 // accepted here. |
| 60 // |attempts_left| - the number of attempts user has to try the code. When |
| 61 // zero the textfield is disabled and user cannot provide any input. When |
| 62 // -1 the user is allowed to provide the input and no information about |
| 63 // the attepts left is displayed in the view. |
| 64 // |callback| - used to send the value of the PIN/PUK the user entered. |
| 65 // |delegate| - used to notify that dialog was closed. Cannot be null. |
| 66 RequestPinView(const std::string& extension_name, |
| 67 RequestPinCodeType code_type, |
| 68 int attempts_left, |
| 69 const RequestPinCallback& callback, |
| 70 Delegate* delegate); |
| 71 ~RequestPinView() override; |
| 72 |
| 73 // views::TextfieldController |
| 74 void ContentsChanged(views::Textfield* sender, |
| 75 const base::string16& new_contents) override; |
| 76 |
| 77 // views::DialogDelegateView |
| 78 bool Cancel() override; |
| 79 bool Accept() override; |
| 80 base::string16 GetWindowTitle() const override; |
| 81 views::View* GetInitiallyFocusedView() override; |
| 82 bool IsDialogButtonEnabled(ui::DialogButton button) const override; |
| 83 |
| 84 // Returns whether the view is locked while waiting the extension to process |
| 85 // the user input data. |
| 86 bool IsLocked(); |
| 87 |
| 88 // Set the new callback to be used when user will provide the input. The old |
| 89 // callback must be used and reset to null at this point. |
| 90 void SetCallback(const RequestPinCallback& callback); |
| 91 |
| 92 // |code_type| - specifies whether the user is asked to enter PIN or PUK. If |
| 93 // UNCHANGED value is provided, the dialog displays the same value that |
| 94 // was last set. |
| 95 // |error_type| - the error template to be displayed in red in the dialog. If |
| 96 // NONE, no error is displayed. |
| 97 // |attempts_left| - included in the view as the number of attepts user can |
| 98 // have to enter correct code. |
| 99 // |accept_input| - specifies whether the textfield is enabled. If disabled |
| 100 // the user is unable to provide input. |
| 101 void SetDialogParameters(RequestPinCodeType code_type, |
| 102 RequestPinErrorType error_type, |
| 103 int attempts_left, |
| 104 bool accept_input); |
| 105 |
| 106 // Set the name of extension that is using this view. The name is included in |
| 107 // the header text displayed by the view. |
| 108 void SetExtensionName(const std::string& extension_name); |
| 109 |
| 110 views::Textfield* textfield_for_testing() { return textfield_; } |
| 111 views::Label* error_label_for_testing() { return error_label_; } |
| 112 |
| 113 private: |
| 114 // This initializes the view, with all the UI components. |
| 115 void Init(); |
| 116 void SetAcceptInput(bool accept_input); |
| 117 void SetErrorMessage(RequestPinErrorType error_type, int attempts_left); |
| 118 // Updates the header text |header_label_| based on values from |
| 119 // |window_title_| and |code_type_|. |
| 120 void UpdateHeaderText(); |
| 121 |
| 122 // Used to send the input when the view is not locked. If user closes the |
| 123 // view, the provided input is empty. The |callback_| must be reset to null |
| 124 // after being used, allowing to check that it was used when a new callback is |
| 125 // set. |
| 126 RequestPinCallback callback_; |
| 127 |
| 128 // Owned by the caller. |
| 129 Delegate* delegate_; |
| 130 |
| 131 base::string16 window_title_; |
| 132 views::Label* header_label_ = nullptr; |
| 133 base::string16 code_type_; |
| 134 views::Textfield* textfield_ = nullptr; |
| 135 views::Label* error_label_ = nullptr; |
| 136 |
| 137 base::WeakPtrFactory<RequestPinView> weak_ptr_factory_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(RequestPinView); |
| 140 }; |
| 141 |
| 142 } // namespace chromeos |
| 143 |
| 144 #endif // CHROME_BROWSER_CHROMEOS_UI_REQUEST_PIN_VIEW_H_ |
OLD | NEW |