Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1181)

Side by Side Diff: chrome/browser/ui/views/payments/editor_view_controller.h

Issue 2715533002: [Payments] Add error messages to credit card editor. (Closed)
Patch Set: tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <tuple>
9 #include <unordered_map> 10 #include <unordered_map>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
15 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h" 16 #include "chrome/browser/ui/views/payments/payment_request_sheet_controller.h"
16 #include "chrome/browser/ui/views/payments/validation_delegate.h" 17 #include "chrome/browser/ui/views/payments/validation_delegate.h"
17 #include "components/autofill/core/browser/field_types.h" 18 #include "components/autofill/core/browser/field_types.h"
18 #include "ui/views/controls/button/vector_icon_button_delegate.h" 19 #include "ui/views/controls/button/vector_icon_button_delegate.h"
19 #include "ui/views/controls/combobox/combobox_listener.h" 20 #include "ui/views/controls/combobox/combobox_listener.h"
20 #include "ui/views/controls/textfield/textfield_controller.h" 21 #include "ui/views/controls/textfield/textfield_controller.h"
21 #include "ui/views/view.h" 22 #include "ui/views/view.h"
22 23
23 namespace ui { 24 namespace ui {
24 class ComboboxModel; 25 class ComboboxModel;
25 } 26 }
26 27
27 namespace views { 28 namespace views {
29 class GridLayout;
30 class Label;
28 class Textfield; 31 class Textfield;
29 class View; 32 class View;
30 } // namespace views 33 } // namespace views
31 34
32 namespace payments { 35 namespace payments {
33 36
34 class PaymentRequest; 37 class PaymentRequest;
35 class PaymentRequestDialogView; 38 class PaymentRequestDialogView;
36 class ValidatingCombobox; 39 class ValidatingCombobox;
37 class ValidatingTextfield; 40 class ValidatingTextfield;
38 41
39 // Field definition for an editor field, used to build the UI. 42 // Field definition for an editor field, used to build the UI.
40 struct EditorField { 43 struct EditorField {
41 enum class LengthHint : int { HINT_LONG, HINT_SHORT }; 44 enum class LengthHint : int { HINT_LONG, HINT_SHORT };
42 enum class ControlType : int { TEXTFIELD, COMBOBOX }; 45 enum class ControlType : int { TEXTFIELD, COMBOBOX };
43 46
44 EditorField(autofill::ServerFieldType type, 47 EditorField(autofill::ServerFieldType type,
45 const base::string16& label, 48 const base::string16& label,
46 LengthHint length_hint, 49 LengthHint length_hint,
47 bool required, 50 bool required,
48 ControlType control_type = ControlType::TEXTFIELD) 51 ControlType control_type = ControlType::TEXTFIELD)
49 : type(type), 52 : type(type),
50 label(label), 53 label(label),
51 length_hint(length_hint), 54 length_hint(length_hint),
52 required(required), 55 required(required),
53 control_type(control_type) {} 56 control_type(control_type) {}
54 57
58 struct Compare {
59 bool operator()(const EditorField& lhs, const EditorField& rhs) const {
60 return std::tie(lhs.type, lhs.label) < std::tie(rhs.type, rhs.label);
61 }
62 };
63
55 // Data type in the field. 64 // Data type in the field.
56 const autofill::ServerFieldType type; 65 const autofill::ServerFieldType type;
57 // Label to be shown alongside the field. 66 // Label to be shown alongside the field.
58 const base::string16 label; 67 const base::string16 label;
59 // Hint about the length of this field's contents. 68 // Hint about the length of this field's contents.
60 LengthHint length_hint; 69 LengthHint length_hint;
61 // Whether the field is required. 70 // Whether the field is required.
62 bool required; 71 bool required;
63 // The control type. 72 // The control type.
64 ControlType control_type; 73 ControlType control_type;
65 }; 74 };
66 75
67 // The PaymentRequestSheetController subtype for the editor screens of the 76 // The PaymentRequestSheetController subtype for the editor screens of the
68 // Payment Request flow. 77 // Payment Request flow.
69 class EditorViewController : public PaymentRequestSheetController, 78 class EditorViewController : public PaymentRequestSheetController,
70 public views::TextfieldController, 79 public views::TextfieldController,
71 public views::ComboboxListener { 80 public views::ComboboxListener {
72 public: 81 public:
73 using TextFieldsMap = 82 using TextFieldsMap =
74 std::unordered_map<ValidatingTextfield*, const EditorField>; 83 std::unordered_map<ValidatingTextfield*, const EditorField>;
75 using ComboboxMap = 84 using ComboboxMap =
76 std::unordered_map<ValidatingCombobox*, const EditorField>; 85 std::unordered_map<ValidatingCombobox*, const EditorField>;
86 using ErrorLabelMap =
87 std::map<const EditorField, views::Label*, EditorField::Compare>;
77 88
78 // Does not take ownership of the arguments, which should outlive this object. 89 // Does not take ownership of the arguments, which should outlive this object.
79 EditorViewController(PaymentRequest* request, 90 EditorViewController(PaymentRequest* request,
80 PaymentRequestDialogView* dialog); 91 PaymentRequestDialogView* dialog);
81 ~EditorViewController() override; 92 ~EditorViewController() override;
82 93
83 // PaymentRequestSheetController: 94 // PaymentRequestSheetController:
84 std::unique_ptr<views::View> CreateView() override; 95 std::unique_ptr<views::View> CreateView() override;
85 std::unique_ptr<views::View> CreateLeadingFooterView() override; 96 std::unique_ptr<views::View> CreateLeadingFooterView() override;
86 97
87 virtual std::unique_ptr<views::View> CreateHeaderView() = 0; 98 virtual std::unique_ptr<views::View> CreateHeaderView() = 0;
88 // Returns the field definitions used to build the UI. 99 // Returns the field definitions used to build the UI.
89 virtual std::vector<EditorField> GetFieldDefinitions() = 0; 100 virtual std::vector<EditorField> GetFieldDefinitions() = 0;
90 // Validates the data entered and attempts to save; returns true on success. 101 // Validates the data entered and attempts to save; returns true on success.
91 virtual bool ValidateModelAndSave() = 0; 102 virtual bool ValidateModelAndSave() = 0;
92 // Creates a ValidationDelegate which knows how to validate for a given 103 // Creates a ValidationDelegate which knows how to validate for a given
93 // |field| definition. 104 // |field| definition. A reference to this |controller| is provided and should
105 // outlive the delegate.
94 virtual std::unique_ptr<ValidationDelegate> CreateValidationDelegate( 106 virtual std::unique_ptr<ValidationDelegate> CreateValidationDelegate(
anthonyvd 2017/02/22 19:32:45 Why not just use |this| in the function body inste
Mathieu 2017/02/22 20:08:59 Doh!
95 const EditorField& field) = 0; 107 const EditorField& field,
108 EditorViewController* controller) = 0;
96 virtual std::unique_ptr<ui::ComboboxModel> GetComboboxModelForType( 109 virtual std::unique_ptr<ui::ComboboxModel> GetComboboxModelForType(
97 const autofill::ServerFieldType& type) = 0; 110 const autofill::ServerFieldType& type) = 0;
98 111
112 // Will display |error_message| alongside the input field represented by
113 // |field|.
114 void DisplayErrorMessageForField(const EditorField& field,
115 const base::string16& error_message);
116
99 const ComboboxMap& comboboxes() const { return comboboxes_; } 117 const ComboboxMap& comboboxes() const { return comboboxes_; }
100 const TextFieldsMap& text_fields() const { return text_fields_; } 118 const TextFieldsMap& text_fields() const { return text_fields_; }
101 119
102 protected: 120 protected:
103 // PaymentRequestSheetController; 121 // PaymentRequestSheetController;
104 std::unique_ptr<views::Button> CreatePrimaryButton() override; 122 std::unique_ptr<views::Button> CreatePrimaryButton() override;
105 123
106 private: 124 private:
107 // PaymentRequestSheetController: 125 // PaymentRequestSheetController:
108 void ButtonPressed(views::Button* sender, const ui::Event& event) override; 126 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
109 127
110 // views::TextfieldController: 128 // views::TextfieldController:
111 void ContentsChanged(views::Textfield* sender, 129 void ContentsChanged(views::Textfield* sender,
112 const base::string16& new_contents) override; 130 const base::string16& new_contents) override;
113 131
114 // views::ComboboxListener: 132 // views::ComboboxListener:
115 void OnPerformAction(views::Combobox* combobox) override; 133 void OnPerformAction(views::Combobox* combobox) override;
116 134
117 // Creates a view for an input field to be added in the editor sheet. |field| 135 // Creates the whole editor view to go within the editor dialog. It
118 // is the field definition, which contains the label and the hint about 136 // encompasses all the input fields created by CreateInputField().
119 // the length of the input field. 137 std::unique_ptr<views::View> CreateEditorView();
120 std::unique_ptr<views::View> CreateInputField(const EditorField& field); 138
139 // Adds some views to |layout|, to represent an input field and its labels.
140 // |field| is the field definition, which contains the label and the hint
141 // about the length of the input field. A placeholder error label is also
142 // added (see implementation).
143 void CreateInputField(views::GridLayout* layout, const EditorField& field);
121 144
122 // Used to remember the association between the input field UI element and the 145 // Used to remember the association between the input field UI element and the
123 // original field definition. The ValidatingTextfield* and ValidatingCombobox* 146 // original field definition. The ValidatingTextfield* and ValidatingCombobox*
124 // are owned by their parent view, this only keeps a reference that is good as 147 // are owned by their parent view, this only keeps a reference that is good as
125 // long as the input field is visible. 148 // long as the input field is visible.
126 TextFieldsMap text_fields_; 149 TextFieldsMap text_fields_;
127 ComboboxMap comboboxes_; 150 ComboboxMap comboboxes_;
151 // Tracks the relationship between a field and its error label.
152 ErrorLabelMap error_labels_;
128 153
129 DISALLOW_COPY_AND_ASSIGN(EditorViewController); 154 DISALLOW_COPY_AND_ASSIGN(EditorViewController);
130 }; 155 };
131 156
132 } // namespace payments 157 } // namespace payments
133 158
134 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_ 159 #endif // CHROME_BROWSER_UI_VIEWS_PAYMENTS_EDITOR_VIEW_CONTROLLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698