| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 5 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" | 11 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" |
| 12 #include "chrome/browser/ui/views/payments/cvc_unmask_view_controller.h" |
| 12 #include "chrome/browser/ui/views/payments/order_summary_view_controller.h" | 13 #include "chrome/browser/ui/views/payments/order_summary_view_controller.h" |
| 13 #include "chrome/browser/ui/views/payments/payment_method_view_controller.h" | 14 #include "chrome/browser/ui/views/payments/payment_method_view_controller.h" |
| 14 #include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h" | 15 #include "chrome/browser/ui/views/payments/payment_sheet_view_controller.h" |
| 15 #include "chrome/browser/ui/views/payments/profile_list_view_controller.h" | 16 #include "chrome/browser/ui/views/payments/profile_list_view_controller.h" |
| 16 #include "chrome/browser/ui/views/payments/shipping_address_editor_view_controll
er.h" | 17 #include "chrome/browser/ui/views/payments/shipping_address_editor_view_controll
er.h" |
| 17 #include "chrome/browser/ui/views/payments/shipping_option_view_controller.h" | 18 #include "chrome/browser/ui/views/payments/shipping_option_view_controller.h" |
| 18 #include "components/constrained_window/constrained_window_views.h" | 19 #include "components/constrained_window/constrained_window_views.h" |
| 19 #include "components/payments/content/payment_request.h" | 20 #include "components/payments/content/payment_request.h" |
| 20 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 21 #include "ui/views/layout/fill_layout.h" | 22 #include "ui/views/layout/fill_layout.h" |
| 22 | 23 |
| 23 namespace chrome { | 24 namespace chrome { |
| 24 | 25 |
| 25 payments::PaymentRequestDialog* CreatePaymentRequestDialog( | 26 payments::PaymentRequestDialog* CreatePaymentRequestDialog( |
| 26 payments::PaymentRequest* request) { | 27 payments::PaymentRequest* request) { |
| 27 return new payments::PaymentRequestDialogView(request, | 28 return new payments::PaymentRequestDialogView(request, |
| 28 /* no observer */ nullptr); | 29 /* no observer */ nullptr); |
| 29 } | 30 } |
| 30 | 31 |
| 31 } // namespace chrome | 32 } // namespace chrome |
| 32 | 33 |
| 33 namespace payments { | 34 namespace payments { |
| 35 |
| 36 class CvcUnmaskUIDelegate |
| 37 : public autofill::payments::FullCardRequest::UIDelegate, |
| 38 public CvcUnmaskViewController::Observer { |
| 39 public: |
| 40 explicit CvcUnmaskUIDelegate(PaymentRequestDialogView* dialog, |
| 41 PaymentRequestSpec* spec, |
| 42 PaymentRequestState* state) |
| 43 : dialog_(dialog), spec_(spec), state_(state), weak_ptr_factory_(this) {} |
| 44 |
| 45 ~CvcUnmaskUIDelegate() override { OnClosed(); } |
| 46 |
| 47 // autofill::payments::FullCardRequest::UIDelegate: |
| 48 void ShowUnmaskPrompt( |
| 49 const autofill::CreditCard& card, |
| 50 autofill::AutofillClient::UnmaskCardReason reason, |
| 51 base::WeakPtr<autofill::CardUnmaskDelegate> delegate) override { |
| 52 unmask_delegate_ = delegate; |
| 53 std::unique_ptr<CvcUnmaskViewController> view_controller = |
| 54 base::MakeUnique<CvcUnmaskViewController>( |
| 55 spec_, state_, dialog_, card.TypeAndLastFourDigits(), card.type()); |
| 56 |
| 57 view_controller->AddObserver(this); |
| 58 dialog_->ShowCvcUnmaskSheet(std::move(view_controller)); |
| 59 } |
| 60 |
| 61 void OnUnmaskVerificationResult( |
| 62 autofill::AutofillClient::PaymentsRpcResult result) override { |
| 63 // TODO(anthonyvd): Check result and display UI. |
| 64 } |
| 65 |
| 66 // CvcUnmaskViewController::Observer |
| 67 void OnCvcConfirmed(const base::string16& cvc) override { |
| 68 if (unmask_delegate_) { |
| 69 autofill::CardUnmaskDelegate::UnmaskResponse response; |
| 70 response.cvc = cvc; |
| 71 unmask_delegate_->OnUnmaskResponse(response); |
| 72 } |
| 73 } |
| 74 |
| 75 void OnClosed() override { |
| 76 // TODO(anthonyvd): The dialog was closed, notify the full card request. |
| 77 } |
| 78 |
| 79 base::WeakPtr<CvcUnmaskUIDelegate> AsWeakPtr() { |
| 80 return weak_ptr_factory_.GetWeakPtr(); |
| 81 } |
| 82 |
| 83 private: |
| 84 PaymentRequestDialogView* dialog_; |
| 85 PaymentRequestSpec* spec_; |
| 86 PaymentRequestState* state_; |
| 87 |
| 88 base::WeakPtr<autofill::CardUnmaskDelegate> unmask_delegate_; |
| 89 |
| 90 base::WeakPtrFactory<CvcUnmaskUIDelegate> weak_ptr_factory_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(CvcUnmaskUIDelegate); |
| 93 }; |
| 94 |
| 34 namespace { | 95 namespace { |
| 35 | 96 |
| 36 // This function creates an instance of a PaymentRequestSheetController | 97 // This function creates an instance of a PaymentRequestSheetController |
| 37 // subclass of concrete type |Controller|, passing it non-owned pointers to | 98 // subclass of concrete type |Controller|, passing it non-owned pointers to |
| 38 // |dialog| and the |request| that initiated that dialog. |map| should be owned | 99 // |dialog| and the |request| that initiated that dialog. |map| should be owned |
| 39 // by |dialog|. | 100 // by |dialog|. |
| 40 std::unique_ptr<views::View> CreateViewAndInstallController( | 101 std::unique_ptr<views::View> CreateViewAndInstallController( |
| 41 std::unique_ptr<PaymentRequestSheetController> controller, | 102 std::unique_ptr<PaymentRequestSheetController> controller, |
| 42 payments::ControllerMap* map) { | 103 payments::ControllerMap* map) { |
| 43 std::unique_ptr<views::View> view = controller->CreateView(); | 104 std::unique_ptr<views::View> view = controller->CreateView(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 210 } |
| 150 | 211 |
| 151 void PaymentRequestDialogView::ShowShippingOptionSheet() { | 212 void PaymentRequestDialogView::ShowShippingOptionSheet() { |
| 152 view_stack_.Push(CreateViewAndInstallController( | 213 view_stack_.Push(CreateViewAndInstallController( |
| 153 base::MakeUnique<ShippingOptionViewController>( | 214 base::MakeUnique<ShippingOptionViewController>( |
| 154 request_->spec(), request_->state(), this), | 215 request_->spec(), request_->state(), this), |
| 155 &controller_map_), | 216 &controller_map_), |
| 156 /* animate = */ true); | 217 /* animate = */ true); |
| 157 } | 218 } |
| 158 | 219 |
| 220 void PaymentRequestDialogView::ShowCvcUnmaskSheet( |
| 221 std::unique_ptr<CvcUnmaskViewController> view_controller) { |
| 222 view_stack_.Push(CreateViewAndInstallController(std::move(view_controller), |
| 223 &controller_map_), |
| 224 /* animate = */ true); |
| 225 } |
| 226 |
| 159 void PaymentRequestDialogView::ShowCreditCardEditor() { | 227 void PaymentRequestDialogView::ShowCreditCardEditor() { |
| 160 view_stack_.Push(CreateViewAndInstallController( | 228 view_stack_.Push(CreateViewAndInstallController( |
| 161 base::MakeUnique<CreditCardEditorViewController>( | 229 base::MakeUnique<CreditCardEditorViewController>( |
| 162 request_->spec(), request_->state(), this), | 230 request_->spec(), request_->state(), this), |
| 163 &controller_map_), | 231 &controller_map_), |
| 164 /* animate = */ true); | 232 /* animate = */ true); |
| 165 if (observer_for_testing_) | 233 if (observer_for_testing_) |
| 166 observer_for_testing_->OnCreditCardEditorOpened(); | 234 observer_for_testing_->OnCreditCardEditorOpened(); |
| 167 } | 235 } |
| 168 | 236 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 184 void PaymentRequestDialogView::ShowDialog() { | 252 void PaymentRequestDialogView::ShowDialog() { |
| 185 constrained_window::ShowWebModalDialogViews(this, request_->web_contents()); | 253 constrained_window::ShowWebModalDialogViews(this, request_->web_contents()); |
| 186 } | 254 } |
| 187 | 255 |
| 188 void PaymentRequestDialogView::CloseDialog() { | 256 void PaymentRequestDialogView::CloseDialog() { |
| 189 // This calls PaymentRequestDialogView::Cancel() before closing. | 257 // This calls PaymentRequestDialogView::Cancel() before closing. |
| 190 // ViewHierarchyChanged() also gets called after Cancel(). | 258 // ViewHierarchyChanged() also gets called after Cancel(). |
| 191 GetWidget()->Close(); | 259 GetWidget()->Close(); |
| 192 } | 260 } |
| 193 | 261 |
| 262 base::WeakPtr<autofill::payments::FullCardRequest::UIDelegate> |
| 263 PaymentRequestDialogView::GetFullCardRequestUIDelegate() { |
| 264 cvc_unmask_ui_delegate_ = base::MakeUnique<CvcUnmaskUIDelegate>( |
| 265 this, request_->spec(), request_->state()); |
| 266 return cvc_unmask_ui_delegate_->AsWeakPtr(); |
| 267 } |
| 268 |
| 194 void PaymentRequestDialogView::ShowInitialPaymentSheet() { | 269 void PaymentRequestDialogView::ShowInitialPaymentSheet() { |
| 195 view_stack_.Push(CreateViewAndInstallController( | 270 view_stack_.Push(CreateViewAndInstallController( |
| 196 base::MakeUnique<PaymentSheetViewController>( | 271 base::MakeUnique<PaymentSheetViewController>( |
| 197 request_->spec(), request_->state(), this), | 272 request_->spec(), request_->state(), this), |
| 198 &controller_map_), | 273 &controller_map_), |
| 199 /* animate = */ false); | 274 /* animate = */ false); |
| 200 if (observer_for_testing_) | 275 if (observer_for_testing_) |
| 201 observer_for_testing_->OnDialogOpened(); | 276 observer_for_testing_->OnDialogOpened(); |
| 202 } | 277 } |
| 203 | 278 |
| 204 gfx::Size PaymentRequestDialogView::GetPreferredSize() const { | 279 gfx::Size PaymentRequestDialogView::GetPreferredSize() const { |
| 205 return gfx::Size(450, 450); | 280 return gfx::Size(450, 450); |
| 206 } | 281 } |
| 207 | 282 |
| 208 void PaymentRequestDialogView::ViewHierarchyChanged( | 283 void PaymentRequestDialogView::ViewHierarchyChanged( |
| 209 const ViewHierarchyChangedDetails& details) { | 284 const ViewHierarchyChangedDetails& details) { |
| 210 if (being_closed_) | 285 if (being_closed_) |
| 211 return; | 286 return; |
| 212 | 287 |
| 213 // When a view that is associated with a controller is removed from this | 288 // When a view that is associated with a controller is removed from this |
| 214 // view's descendants, dispose of the controller. | 289 // view's descendants, dispose of the controller. |
| 215 if (!details.is_add && | 290 if (!details.is_add && |
| 216 controller_map_.find(details.child) != controller_map_.end()) { | 291 controller_map_.find(details.child) != controller_map_.end()) { |
| 217 DCHECK(!details.move_view); | 292 DCHECK(!details.move_view); |
| 218 controller_map_.erase(details.child); | 293 controller_map_.erase(details.child); |
| 219 } | 294 } |
| 220 } | 295 } |
| 221 | 296 |
| 222 } // namespace payments | 297 } // namespace payments |
| OLD | NEW |