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