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