| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" | 15 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" |
| 16 #include "chrome/browser/ui/views/payments/validating_textfield.h" | 16 #include "chrome/browser/ui/views/payments/validation_delegate.h" |
| 17 #include "components/autofill/core/browser/field_types.h" | 17 #include "components/autofill/core/browser/field_types.h" |
| 18 #include "ui/views/controls/button/vector_icon_button_delegate.h" | 18 #include "ui/views/controls/button/vector_icon_button_delegate.h" |
| 19 #include "ui/views/controls/combobox/combobox_listener.h" |
| 19 #include "ui/views/controls/textfield/textfield_controller.h" | 20 #include "ui/views/controls/textfield/textfield_controller.h" |
| 20 | 21 |
| 22 namespace ui { |
| 23 class ComboboxModel; |
| 24 } |
| 25 |
| 21 namespace views { | 26 namespace views { |
| 22 class Textfield; | 27 class Textfield; |
| 23 class View; | 28 class View; |
| 24 } // namespace views | 29 } // namespace views |
| 25 | 30 |
| 26 namespace payments { | 31 namespace payments { |
| 27 | 32 |
| 28 class PaymentRequest; | 33 class PaymentRequest; |
| 29 class PaymentRequestDialogView; | 34 class PaymentRequestDialogView; |
| 35 class ValidatingCombobox; |
| 36 class ValidatingTextfield; |
| 30 | 37 |
| 31 // Field definition for an editor field, used to build the UI. | 38 // Field definition for an editor field, used to build the UI. |
| 32 struct EditorField { | 39 struct EditorField { |
| 33 enum class LengthHint : int { HINT_LONG, HINT_SHORT }; | 40 enum class LengthHint : int { HINT_LONG, HINT_SHORT }; |
| 41 enum class ControlType : int { TEXTFIELD, COMBOBOX }; |
| 34 | 42 |
| 35 EditorField(autofill::ServerFieldType type, | 43 EditorField(autofill::ServerFieldType type, |
| 36 const base::string16& label, | 44 const base::string16& label, |
| 37 LengthHint length_hint, | 45 LengthHint length_hint, |
| 38 bool required) | 46 bool required, |
| 47 ControlType control_type = ControlType::TEXTFIELD) |
| 39 : type(type), | 48 : type(type), |
| 40 label(label), | 49 label(label), |
| 41 length_hint(length_hint), | 50 length_hint(length_hint), |
| 42 required(required) {} | 51 required(required), |
| 52 control_type(control_type) {} |
| 43 | 53 |
| 44 // Data type in the field. | 54 // Data type in the field. |
| 45 const autofill::ServerFieldType type; | 55 const autofill::ServerFieldType type; |
| 46 // Label to be shown alongside the field. | 56 // Label to be shown alongside the field. |
| 47 const base::string16 label; | 57 const base::string16 label; |
| 48 // Hint about the length of this field's contents. | 58 // Hint about the length of this field's contents. |
| 49 LengthHint length_hint; | 59 LengthHint length_hint; |
| 50 // Whether the field is required. | 60 // Whether the field is required. |
| 51 bool required; | 61 bool required; |
| 62 // The control type. |
| 63 ControlType control_type; |
| 52 }; | 64 }; |
| 53 | 65 |
| 54 // The PaymentRequestSheetController subtype for the editor screens of the | 66 // The PaymentRequestSheetController subtype for the editor screens of the |
| 55 // Payment Request flow. | 67 // Payment Request flow. |
| 56 class EditorViewController : public PaymentRequestSheetController, | 68 class EditorViewController : public PaymentRequestSheetController, |
| 57 public views::TextfieldController { | 69 public views::TextfieldController, |
| 70 public views::ComboboxListener { |
| 58 public: | 71 public: |
| 59 using TextFieldsMap = | 72 using TextFieldsMap = |
| 60 std::unordered_map<ValidatingTextfield*, const EditorField>; | 73 std::unordered_map<ValidatingTextfield*, const EditorField>; |
| 74 using ComboboxMap = |
| 75 std::unordered_map<ValidatingCombobox*, const EditorField>; |
| 61 | 76 |
| 62 // Does not take ownership of the arguments, which should outlive this object. | 77 // Does not take ownership of the arguments, which should outlive this object. |
| 63 EditorViewController(PaymentRequest* request, | 78 EditorViewController(PaymentRequest* request, |
| 64 PaymentRequestDialogView* dialog); | 79 PaymentRequestDialogView* dialog); |
| 65 ~EditorViewController() override; | 80 ~EditorViewController() override; |
| 66 | 81 |
| 67 // PaymentRequestSheetController: | 82 // PaymentRequestSheetController: |
| 68 std::unique_ptr<views::View> CreateView() override; | 83 std::unique_ptr<views::View> CreateView() override; |
| 69 | 84 |
| 70 // Returns the field definitions used to build the UI. | 85 // Returns the field definitions used to build the UI. |
| 71 virtual std::vector<EditorField> GetFieldDefinitions() = 0; | 86 virtual std::vector<EditorField> GetFieldDefinitions() = 0; |
| 72 // Validates the data entered and attempts to save; returns true on success. | 87 // Validates the data entered and attempts to save; returns true on success. |
| 73 virtual bool ValidateModelAndSave() = 0; | 88 virtual bool ValidateModelAndSave() = 0; |
| 74 // Creates a ValidatingTextfield::Delegate which knows how to validate for a | 89 // Creates a ValidationDelegate which knows how to validate for a given |
| 75 // given |field| definition. | 90 // |field| definition. |
| 76 virtual std::unique_ptr<ValidatingTextfield::Delegate> | 91 virtual std::unique_ptr<ValidationDelegate> CreateValidationDelegate( |
| 77 CreateValidationDelegate(const EditorField& field) = 0; | 92 const EditorField& field) = 0; |
| 93 virtual std::unique_ptr<ui::ComboboxModel> GetComboboxModelForType( |
| 94 const autofill::ServerFieldType& type) = 0; |
| 78 | 95 |
| 96 const ComboboxMap& comboboxes() const { return comboboxes_; } |
| 79 const TextFieldsMap& text_fields() const { return text_fields_; } | 97 const TextFieldsMap& text_fields() const { return text_fields_; } |
| 80 | 98 |
| 81 protected: | 99 protected: |
| 82 // PaymentRequestSheetController; | 100 // PaymentRequestSheetController; |
| 83 std::unique_ptr<views::Button> CreatePrimaryButton() override; | 101 std::unique_ptr<views::Button> CreatePrimaryButton() override; |
| 84 | 102 |
| 85 private: | 103 private: |
| 86 // PaymentRequestSheetController: | 104 // PaymentRequestSheetController: |
| 87 void ButtonPressed(views::Button* sender, const ui::Event& event) override; | 105 void ButtonPressed(views::Button* sender, const ui::Event& event) override; |
| 88 | 106 |
| 89 // views::TextfieldController | 107 // views::TextfieldController: |
| 90 void ContentsChanged(views::Textfield* sender, | 108 void ContentsChanged(views::Textfield* sender, |
| 91 const base::string16& new_contents) override; | 109 const base::string16& new_contents) override; |
| 92 | 110 |
| 111 // views::ComboboxListener: |
| 112 void OnPerformAction(views::Combobox* combobox) override; |
| 113 |
| 93 // Creates a view for an input field to be added in the editor sheet. |field| | 114 // Creates a view for an input field to be added in the editor sheet. |field| |
| 94 // is the field definition, which contains the label and the hint about | 115 // is the field definition, which contains the label and the hint about |
| 95 // the length of the input field. | 116 // the length of the input field. |
| 96 std::unique_ptr<views::View> CreateInputField(const EditorField& field); | 117 std::unique_ptr<views::View> CreateInputField(const EditorField& field); |
| 97 | 118 |
| 98 // Used to remember the association between the input field UI element and the | 119 // Used to remember the association between the input field UI element and the |
| 99 // original field definition. The ValidatingTextfield* are owned by their | 120 // original field definition. The ValidatingTextfield* and ValidatingCombobox* |
| 100 // parent view, this only keeps a reference that is good as long as the | 121 // are owned by their parent view, this only keeps a reference that is good as |
| 101 // textfield is visible. | 122 // long as the input field is visible. |
| 102 TextFieldsMap text_fields_; | 123 TextFieldsMap text_fields_; |
| 124 ComboboxMap comboboxes_; |
| 103 | 125 |
| 104 DISALLOW_COPY_AND_ASSIGN(EditorViewController); | 126 DISALLOW_COPY_AND_ASSIGN(EditorViewController); |
| 105 }; | 127 }; |
| 106 | 128 |
| 107 } // namespace payments | 129 } // namespace payments |
| 108 | 130 |
| 109 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ | 131 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ |
| OLD | NEW |