Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "chrome/browser/ui/autofill/autofill_dialog_models.h" | |
| 7 #include "chrome/browser/ui/autofill/card_unmask_prompt_controller.h" | 8 #include "chrome/browser/ui/autofill/card_unmask_prompt_controller.h" |
| 8 #include "chrome/browser/ui/autofill/card_unmask_prompt_view.h" | 9 #include "chrome/browser/ui/autofill/card_unmask_prompt_view.h" |
| 9 #include "chrome/grit/generated_resources.h" | 10 #include "chrome/grit/generated_resources.h" |
| 10 #include "components/constrained_window/constrained_window_views.h" | 11 #include "components/constrained_window/constrained_window_views.h" |
| 11 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
| 15 #include "ui/views/controls/combobox/combobox.h" | |
| 16 #include "ui/views/controls/combobox/combobox_listener.h" | |
| 14 #include "ui/views/controls/image_view.h" | 17 #include "ui/views/controls/image_view.h" |
| 15 #include "ui/views/controls/label.h" | 18 #include "ui/views/controls/label.h" |
| 16 #include "ui/views/controls/textfield/textfield.h" | 19 #include "ui/views/controls/textfield/textfield.h" |
| 17 #include "ui/views/controls/textfield/textfield_controller.h" | 20 #include "ui/views/controls/textfield/textfield_controller.h" |
| 18 #include "ui/views/layout/box_layout.h" | 21 #include "ui/views/layout/box_layout.h" |
| 19 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
| 20 #include "ui/views/window/dialog_client_view.h" | 23 #include "ui/views/window/dialog_client_view.h" |
| 21 #include "ui/views/window/dialog_delegate.h" | 24 #include "ui/views/window/dialog_delegate.h" |
| 22 | 25 |
| 23 namespace autofill { | 26 namespace autofill { |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 class CardUnmaskPromptViews : public CardUnmaskPromptView, | 30 class CardUnmaskPromptViews : public CardUnmaskPromptView, |
| 31 views::ComboboxListener, | |
| 28 views::DialogDelegateView, | 32 views::DialogDelegateView, |
| 29 views::TextfieldController { | 33 views::TextfieldController { |
| 30 public: | 34 public: |
| 31 explicit CardUnmaskPromptViews(CardUnmaskPromptController* controller) | 35 explicit CardUnmaskPromptViews(CardUnmaskPromptController* controller) |
| 32 : controller_(controller), cvc_input_(nullptr), message_label_(nullptr) {} | 36 : controller_(controller), |
| 37 cvc_input_(nullptr), | |
| 38 month_input_(nullptr), | |
| 39 year_input_(nullptr), | |
| 40 message_label_(nullptr) {} | |
| 33 | 41 |
| 34 ~CardUnmaskPromptViews() override { | 42 ~CardUnmaskPromptViews() override { |
| 35 if (controller_) | 43 if (controller_) |
| 36 controller_->OnUnmaskDialogClosed(); | 44 controller_->OnUnmaskDialogClosed(); |
| 37 } | 45 } |
| 38 | 46 |
| 39 void Show() { | 47 void Show() { |
| 40 constrained_window::ShowWebModalDialogViews(this, | 48 constrained_window::ShowWebModalDialogViews(this, |
| 41 controller_->GetWebContents()); | 49 controller_->GetWebContents()); |
| 42 } | 50 } |
| 43 | 51 |
| 44 // CardUnmaskPromptView | 52 // CardUnmaskPromptView |
| 45 void ControllerGone() override { | 53 void ControllerGone() override { |
| 46 controller_ = nullptr; | 54 controller_ = nullptr; |
| 47 ClosePrompt(); | 55 ClosePrompt(); |
| 48 } | 56 } |
| 49 | 57 |
| 50 void DisableAndWaitForVerification() override { | 58 void DisableAndWaitForVerification() override { |
| 51 cvc_input_->SetEnabled(false); | 59 SetInputsEnabled(false); |
| 52 message_label_->SetText(base::ASCIIToUTF16("Verifying...")); | 60 message_label_->SetText(base::ASCIIToUTF16("Verifying...")); |
| 53 message_label_->SetVisible(true); | 61 message_label_->SetVisible(true); |
| 54 GetDialogClientView()->UpdateDialogButtons(); | 62 GetDialogClientView()->UpdateDialogButtons(); |
| 55 Layout(); | 63 Layout(); |
| 56 } | 64 } |
| 57 | 65 |
| 58 void GotVerificationResult(bool success) override { | 66 void GotVerificationResult(bool success) override { |
| 59 if (success) { | 67 if (success) { |
| 60 message_label_->SetText(base::ASCIIToUTF16("Success!")); | 68 message_label_->SetText(base::ASCIIToUTF16("Success!")); |
| 61 base::MessageLoop::current()->PostDelayedTask( | 69 base::MessageLoop::current()->PostDelayedTask( |
| 62 FROM_HERE, base::Bind(&CardUnmaskPromptViews::ClosePrompt, | 70 FROM_HERE, base::Bind(&CardUnmaskPromptViews::ClosePrompt, |
| 63 base::Unretained(this)), | 71 base::Unretained(this)), |
| 64 base::TimeDelta::FromSeconds(1)); | 72 base::TimeDelta::FromSeconds(1)); |
| 65 } else { | 73 } else { |
| 66 cvc_input_->SetEnabled(true); | 74 SetInputsEnabled(true); |
| 67 message_label_->SetText(base::ASCIIToUTF16("Verification error.")); | 75 message_label_->SetText(base::ASCIIToUTF16("Verification error.")); |
| 68 GetDialogClientView()->UpdateDialogButtons(); | 76 GetDialogClientView()->UpdateDialogButtons(); |
| 69 } | 77 } |
| 70 Layout(); | 78 Layout(); |
| 71 } | 79 } |
| 72 | 80 |
| 81 void SetInputsEnabled(bool enabled) { | |
| 82 cvc_input_->SetEnabled(enabled); | |
| 83 | |
| 84 if (month_input_) | |
| 85 month_input_->SetEnabled(enabled); | |
| 86 if (year_input_) | |
| 87 year_input_->SetEnabled(enabled); | |
| 88 } | |
| 89 | |
| 73 // views::DialogDelegateView | 90 // views::DialogDelegateView |
| 74 View* GetContentsView() override { | 91 View* GetContentsView() override { |
| 75 InitIfNecessary(); | 92 InitIfNecessary(); |
| 76 return this; | 93 return this; |
| 77 } | 94 } |
| 78 | 95 |
| 79 // views::View | 96 // views::View |
| 80 gfx::Size GetPreferredSize() const override { | 97 gfx::Size GetPreferredSize() const override { |
| 81 // Must hardcode a width so the label knows where to wrap. TODO(estade): | 98 // Must hardcode a width so the label knows where to wrap. TODO(estade): |
| 82 // This can lead to a weird looking dialog if we end up getting allocated | 99 // This can lead to a weird looking dialog if we end up getting allocated |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 107 | 124 |
| 108 bool ShouldDefaultButtonBeBlue() const override { return true; } | 125 bool ShouldDefaultButtonBeBlue() const override { return true; } |
| 109 | 126 |
| 110 bool IsDialogButtonEnabled(ui::DialogButton button) const override { | 127 bool IsDialogButtonEnabled(ui::DialogButton button) const override { |
| 111 if (button == ui::DIALOG_BUTTON_CANCEL) | 128 if (button == ui::DIALOG_BUTTON_CANCEL) |
| 112 return true; | 129 return true; |
| 113 | 130 |
| 114 DCHECK_EQ(ui::DIALOG_BUTTON_OK, button); | 131 DCHECK_EQ(ui::DIALOG_BUTTON_OK, button); |
| 115 | 132 |
| 116 return cvc_input_->enabled() && | 133 return cvc_input_->enabled() && |
| 117 controller_->InputTextIsValid(cvc_input_->text()); | 134 controller_->InputTextIsValid(cvc_input_->text()) && |
| 135 (!month_input_ || | |
| 136 month_input_->selected_index() != | |
| 137 month_combobox_model_.GetDefaultIndex()) && | |
| 138 (year_input_ || | |
|
brettw
2015/01/27 00:12:37
Did you mean !year_input_ here?
Evan Stade
2015/01/27 00:14:42
yes good catch.
| |
| 139 year_input_->selected_index() != | |
| 140 year_combobox_model_.GetDefaultIndex()); | |
| 118 } | 141 } |
| 119 | 142 |
| 120 views::View* GetInitiallyFocusedView() override { return cvc_input_; } | 143 views::View* GetInitiallyFocusedView() override { return cvc_input_; } |
| 121 | 144 |
| 122 bool Cancel() override { | 145 bool Cancel() override { |
| 123 return true; | 146 return true; |
| 124 } | 147 } |
| 125 | 148 |
| 126 bool Accept() override { | 149 bool Accept() override { |
| 127 if (!controller_) | 150 if (!controller_) |
| 128 return true; | 151 return true; |
| 129 | 152 |
| 130 controller_->OnUnmaskResponse(cvc_input_->text()); | 153 controller_->OnUnmaskResponse( |
| 154 cvc_input_->text(), | |
| 155 month_input_ | |
| 156 ? month_combobox_model_.GetItemAt(month_input_->selected_index()) | |
| 157 : base::string16(), | |
| 158 year_input_ | |
| 159 ? year_combobox_model_.GetItemAt(year_input_->selected_index()) | |
| 160 : base::string16()); | |
| 131 return false; | 161 return false; |
| 132 } | 162 } |
| 133 | 163 |
| 134 // views::TextfieldController | 164 // views::TextfieldController |
| 135 void ContentsChanged(views::Textfield* sender, | 165 void ContentsChanged(views::Textfield* sender, |
| 136 const base::string16& new_contents) override { | 166 const base::string16& new_contents) override { |
| 137 GetDialogClientView()->UpdateDialogButtons(); | 167 GetDialogClientView()->UpdateDialogButtons(); |
| 138 } | 168 } |
| 139 | 169 |
| 170 // views::ComboboxListener | |
| 171 void OnPerformAction(views::Combobox* combobox) override { | |
| 172 GetDialogClientView()->UpdateDialogButtons(); | |
| 173 } | |
| 174 | |
| 140 private: | 175 private: |
| 141 void InitIfNecessary() { | 176 void InitIfNecessary() { |
| 142 if (has_children()) | 177 if (has_children()) |
| 143 return; | 178 return; |
| 144 | 179 |
| 145 SetLayoutManager( | 180 SetLayoutManager( |
| 146 new views::BoxLayout(views::BoxLayout::kVertical, 19, 0, 5)); | 181 new views::BoxLayout(views::BoxLayout::kVertical, 19, 0, 5)); |
| 147 views::Label* instructions = | 182 views::Label* instructions = |
| 148 new views::Label(controller_->GetInstructionsMessage()); | 183 new views::Label(controller_->GetInstructionsMessage()); |
| 149 | 184 |
| 150 instructions->SetMultiLine(true); | 185 instructions->SetMultiLine(true); |
| 151 instructions->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 186 instructions->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 152 AddChildView(instructions); | 187 AddChildView(instructions); |
| 153 | 188 |
| 154 views::View* cvc_container = new views::View(); | 189 views::View* input_row = new views::View(); |
| 155 cvc_container->SetLayoutManager( | 190 input_row->SetLayoutManager( |
| 156 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 5)); | 191 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 5)); |
| 157 AddChildView(cvc_container); | 192 AddChildView(input_row); |
| 193 | |
| 194 if (controller_->ShouldRequestExpirationDate()) { | |
| 195 month_input_ = new views::Combobox(&month_combobox_model_); | |
| 196 month_input_->set_listener(this); | |
| 197 input_row->AddChildView(month_input_); | |
| 198 year_input_ = new views::Combobox(&year_combobox_model_); | |
| 199 year_input_->set_listener(this); | |
| 200 input_row->AddChildView(year_input_); | |
| 201 } | |
| 158 | 202 |
| 159 cvc_input_ = new views::Textfield(); | 203 cvc_input_ = new views::Textfield(); |
| 160 cvc_input_->set_controller(this); | 204 cvc_input_->set_controller(this); |
| 161 cvc_input_->set_placeholder_text( | 205 cvc_input_->set_placeholder_text( |
| 162 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC)); | 206 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PLACEHOLDER_CVC)); |
| 163 cvc_input_->set_default_width_in_chars(10); | 207 cvc_input_->set_default_width_in_chars(10); |
| 164 cvc_container->AddChildView(cvc_input_); | 208 input_row->AddChildView(cvc_input_); |
| 165 | 209 |
| 166 views::ImageView* cvc_image = new views::ImageView(); | 210 views::ImageView* cvc_image = new views::ImageView(); |
| 167 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 211 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 168 | 212 |
| 169 cvc_image->SetImage(rb.GetImageSkiaNamed(controller_->GetCvcImageRid())); | 213 cvc_image->SetImage(rb.GetImageSkiaNamed(controller_->GetCvcImageRid())); |
| 170 | 214 |
| 171 cvc_container->AddChildView(cvc_image); | 215 input_row->AddChildView(cvc_image); |
| 172 | 216 |
| 173 message_label_ = new views::Label(); | 217 message_label_ = new views::Label(); |
| 174 cvc_container->AddChildView(message_label_); | 218 input_row->AddChildView(message_label_); |
| 175 message_label_->SetVisible(false); | 219 message_label_->SetVisible(false); |
| 176 } | 220 } |
| 177 | 221 |
| 178 void ClosePrompt() { GetWidget()->Close(); } | 222 void ClosePrompt() { GetWidget()->Close(); } |
| 179 | 223 |
| 180 CardUnmaskPromptController* controller_; | 224 CardUnmaskPromptController* controller_; |
| 181 | 225 |
| 182 views::Textfield* cvc_input_; | 226 views::Textfield* cvc_input_; |
| 183 | 227 |
| 228 // These will be null when expiration date is not required. | |
| 229 views::Combobox* month_input_; | |
| 230 views::Combobox* year_input_; | |
| 231 | |
| 232 MonthComboboxModel month_combobox_model_; | |
| 233 YearComboboxModel year_combobox_model_; | |
| 234 | |
| 184 // TODO(estade): this is a temporary standin in place of some spinner UI | 235 // TODO(estade): this is a temporary standin in place of some spinner UI |
| 185 // as well as a better error message. | 236 // as well as a better error message. |
| 186 views::Label* message_label_; | 237 views::Label* message_label_; |
| 187 | 238 |
| 188 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViews); | 239 DISALLOW_COPY_AND_ASSIGN(CardUnmaskPromptViews); |
| 189 }; | 240 }; |
| 190 | 241 |
| 191 } // namespace | 242 } // namespace |
| 192 | 243 |
| 193 // static | 244 // static |
| 194 CardUnmaskPromptView* CardUnmaskPromptView::CreateAndShow( | 245 CardUnmaskPromptView* CardUnmaskPromptView::CreateAndShow( |
| 195 CardUnmaskPromptController* controller) { | 246 CardUnmaskPromptController* controller) { |
| 196 CardUnmaskPromptViews* view = new CardUnmaskPromptViews(controller); | 247 CardUnmaskPromptViews* view = new CardUnmaskPromptViews(controller); |
| 197 view->Show(); | 248 view->Show(); |
| 198 return view; | 249 return view; |
| 199 } | 250 } |
| 200 | 251 |
| 201 } // namespace autofill | 252 } // namespace autofill |
| OLD | NEW |