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

Side by Side Diff: ios/chrome/browser/payments/payment_request_manager.mm

Issue 2804853002: Cancels payment request when Settings is tapped, and opens the Autofill settings page. (Closed)
Patch Set: Remove more strongSelf checks. Created 3 years, 8 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 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 #import "ios/chrome/browser/payments/payment_request_manager.h" 5 #import "ios/chrome/browser/payments/payment_request_manager.h"
6 6
7 #include "base/ios/block_types.h" 7 #include "base/ios/block_types.h"
8 #include "base/ios/ios_util.h" 8 #include "base/ios/ios_util.h"
9 #import "base/mac/bind_objc_block.h" 9 #import "base/mac/bind_objc_block.h"
10 #include "base/mac/foundation_util.h" 10 #include "base/mac/foundation_util.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
13 #import "base/values.h" 13 #import "base/values.h"
14 #include "components/autofill/core/browser/autofill_manager.h" 14 #include "components/autofill/core/browser/autofill_manager.h"
15 #include "components/autofill/core/browser/personal_data_manager.h" 15 #include "components/autofill/core/browser/personal_data_manager.h"
16 #include "components/autofill/ios/browser/autofill_driver_ios.h" 16 #include "components/autofill/ios/browser/autofill_driver_ios.h"
17 #include "components/payments/core/payment_address.h" 17 #include "components/payments/core/payment_address.h"
18 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h" 18 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h"
19 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 19 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
20 #import "ios/chrome/browser/payments/js_payment_request_manager.h" 20 #import "ios/chrome/browser/payments/js_payment_request_manager.h"
21 #include "ios/chrome/browser/payments/payment_request.h" 21 #include "ios/chrome/browser/payments/payment_request.h"
22 #import "ios/chrome/browser/payments/payment_request_coordinator.h" 22 #import "ios/chrome/browser/payments/payment_request_coordinator.h"
23 #include "ios/chrome/browser/procedural_block_types.h" 23 #include "ios/chrome/browser/procedural_block_types.h"
24 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h"
25 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h"
26 #import "ios/chrome/browser/ui/commands/ios_command_ids.h"
24 #include "ios/web/public/favicon_status.h" 27 #include "ios/web/public/favicon_status.h"
25 #include "ios/web/public/navigation_item.h" 28 #include "ios/web/public/navigation_item.h"
26 #include "ios/web/public/navigation_manager.h" 29 #include "ios/web/public/navigation_manager.h"
27 #include "ios/web/public/payments/payment_request.h" 30 #include "ios/web/public/payments/payment_request.h"
28 #include "ios/web/public/ssl_status.h" 31 #include "ios/web/public/ssl_status.h"
29 #import "ios/web/public/url_scheme_util.h" 32 #import "ios/web/public/url_scheme_util.h"
30 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h" 33 #import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
31 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h" 34 #import "ios/web/public/web_state/ui/crw_web_view_proxy.h"
32 #include "ios/web/public/web_state/url_verification_constants.h" 35 #include "ios/web/public/web_state/url_verification_constants.h"
33 #include "ios/web/public/web_state/web_state.h" 36 #include "ios/web/public/web_state/web_state.h"
34 #import "ios/web/public/web_state/web_state_observer_bridge.h" 37 #import "ios/web/public/web_state/web_state_observer_bridge.h"
35 38
36 #if !defined(__has_feature) || !__has_feature(objc_arc) 39 #if !defined(__has_feature) || !__has_feature(objc_arc)
37 #error "This file requires ARC support." 40 #error "This file requires ARC support."
38 #endif 41 #endif
39 42
40 namespace { 43 namespace {
41 // Command prefix for injected JavaScript. 44 // Command prefix for injected JavaScript.
42 const std::string kCommandPrefix = "paymentRequest"; 45 const std::string kCommandPrefix = "paymentRequest";
43 46
44 // Time interval between attempts to unblock the webview's JS event queue. 47 // Time interval between attempts to unblock the webview's JS event queue.
45 const NSTimeInterval kNoopInterval = 0.1; 48 const NSTimeInterval kNoopInterval = 0.1;
46 49
47 // Time interval before closing the UI if the page has not yet called 50 // Time interval before closing the UI if the page has not yet called
48 // PaymentResponse.complete(). 51 // PaymentResponse.complete().
49 const NSTimeInterval kTimeoutInterval = 60.0; 52 const NSTimeInterval kTimeoutInterval = 60.0;
50 53
54 NSString* kAbortMessage = @"The payment request was aborted.";
55 NSString* kCancelMessage = @"The payment request was canceled.";
56
51 } // namespace 57 } // namespace
52 58
53 @interface PaymentRequestManager ()<CRWWebStateObserver, 59 @interface PaymentRequestManager ()<CRWWebStateObserver,
54 PaymentRequestCoordinatorDelegate> { 60 PaymentRequestCoordinatorDelegate> {
55 // View controller used to present the PaymentRequest view controller. 61 // View controller used to present the PaymentRequest view controller.
56 __weak UIViewController* _baseViewController; 62 __weak UIViewController* _baseViewController;
57 63
58 // PersonalDataManager used to manage user credit cards and addresses. 64 // PersonalDataManager used to manage user credit cards and addresses.
59 autofill::PersonalDataManager* _personalDataManager; 65 autofill::PersonalDataManager* _personalDataManager;
60 66
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 if (changing) { 224 if (changing) {
219 if (!enabled) { 225 if (!enabled) {
220 [self dismissUI]; 226 [self dismissUI];
221 } 227 }
222 _enabled = enabled; 228 _enabled = enabled;
223 [self enableCurrentWebState]; 229 [self enableCurrentWebState];
224 } 230 }
225 } 231 }
226 232
227 - (void)cancelRequest { 233 - (void)cancelRequest {
228 [self terminateRequestWithErrorMessage:@"The payment request was canceled." 234 [self terminateRequestWithErrorMessage:kCancelMessage callback:nil];
229 callback:nil];
230 } 235 }
231 236
232 - (void)terminateRequestWithErrorMessage:(NSString*)errorMessage 237 - (void)terminateRequestWithErrorMessage:(NSString*)errorMessage
233 callback:(ProceduralBlockWithBool)callback { 238 callback:(ProceduralBlockWithBool)callback {
234 [self dismissUI]; 239 [self dismissUI];
235 [_paymentRequestJsManager rejectRequestPromiseWithErrorMessage:errorMessage 240 [_paymentRequestJsManager rejectRequestPromiseWithErrorMessage:errorMessage
236 completionHandler:callback]; 241 completionHandler:callback];
237 } 242 }
238 243
239 - (void)close { 244 - (void)close {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 - (BOOL)handleRequestAbort:(const base::DictionaryValue&)message { 372 - (BOOL)handleRequestAbort:(const base::DictionaryValue&)message {
368 // TODO(crbug.com/602666): Check that there is already a pending request. 373 // TODO(crbug.com/602666): Check that there is already a pending request.
369 374
370 [_unblockEventQueueTimer invalidate]; 375 [_unblockEventQueueTimer invalidate];
371 [_paymentResponseTimeoutTimer invalidate]; 376 [_paymentResponseTimeoutTimer invalidate];
372 [_updateEventTimeoutTimer invalidate]; 377 [_updateEventTimeoutTimer invalidate];
373 378
374 __weak PaymentRequestManager* weakSelf = self; 379 __weak PaymentRequestManager* weakSelf = self;
375 380
376 ProceduralBlockWithBool cancellationCallback = ^(BOOL) { 381 ProceduralBlockWithBool cancellationCallback = ^(BOOL) {
377 PaymentRequestManager* strongSelf = weakSelf; 382 [[weakSelf paymentRequestJsManager]
378 // Early return if the manager has been deallocated.
379 if (!strongSelf)
380 return;
381 [[strongSelf paymentRequestJsManager]
382 resolveAbortPromiseWithCompletionHandler:nil]; 383 resolveAbortPromiseWithCompletionHandler:nil];
383 }; 384 };
384 385
385 ProceduralBlock callback = ^{ 386 ProceduralBlock callback = ^{
386 PaymentRequestManager* strongSelf = weakSelf; 387 [weakSelf terminateRequestWithErrorMessage:kAbortMessage
387 // Early return if the manager has been deallocated. 388 callback:cancellationCallback];
388 if (!strongSelf)
389 return;
390 [strongSelf
391 terminateRequestWithErrorMessage:@"The payment request was aborted."
392 callback:cancellationCallback];
393 }; 389 };
394 390
395 [_paymentRequestCoordinator displayErrorWithCallback:callback]; 391 [_paymentRequestCoordinator displayErrorWithCallback:callback];
396 392
397 return YES; 393 return YES;
398 } 394 }
399 395
400 - (BOOL)displayErrorThenCancelRequest { 396 - (BOOL)displayErrorThenCancelRequest {
401 // TODO(crbug.com/602666): Check that there is already a pending request. 397 // TODO(crbug.com/602666): Check that there is already a pending request.
402 398
403 [_unblockEventQueueTimer invalidate]; 399 [_unblockEventQueueTimer invalidate];
404 [_paymentResponseTimeoutTimer invalidate]; 400 [_paymentResponseTimeoutTimer invalidate];
405 [_updateEventTimeoutTimer invalidate]; 401 [_updateEventTimeoutTimer invalidate];
406 402
407 __weak PaymentRequestManager* weakSelf = self; 403 __weak PaymentRequestManager* weakSelf = self;
408 ProceduralBlock callback = ^{ 404 ProceduralBlock callback = ^{
409 PaymentRequestManager* strongSelf = weakSelf; 405 [weakSelf terminateRequestWithErrorMessage:kCancelMessage callback:nil];
410 // Early return if the manager has been deallocated.
411 if (!strongSelf)
412 return;
413 [strongSelf
414 terminateRequestWithErrorMessage:@"The payment request was canceled."
415 callback:nil];
416 }; 406 };
417 407
418 [_paymentRequestCoordinator displayErrorWithCallback:callback]; 408 [_paymentRequestCoordinator displayErrorWithCallback:callback];
419 409
420 return YES; 410 return YES;
421 } 411 }
422 412
423 - (BOOL)doResponseComplete { 413 - (BOOL)doResponseComplete {
424 base::DictionaryValue command; 414 base::DictionaryValue command;
425 command.SetString("result", "unknown"); 415 command.SetString("result", "unknown");
426 return [self handleResponseComplete:command]; 416 return [self handleResponseComplete:command];
427 } 417 }
428 418
429 - (BOOL)handleResponseComplete:(const base::DictionaryValue&)message { 419 - (BOOL)handleResponseComplete:(const base::DictionaryValue&)message {
430 // TODO(crbug.com/602666): Check that there *is* a pending response here. 420 // TODO(crbug.com/602666): Check that there *is* a pending response here.
431 421
432 [_unblockEventQueueTimer invalidate]; 422 [_unblockEventQueueTimer invalidate];
433 [_paymentResponseTimeoutTimer invalidate]; 423 [_paymentResponseTimeoutTimer invalidate];
434 [_updateEventTimeoutTimer invalidate]; 424 [_updateEventTimeoutTimer invalidate];
435 425
436 std::string result; 426 std::string result;
437 if (!message.GetString("result", &result)) { 427 if (!message.GetString("result", &result)) {
438 DLOG(ERROR) << "JS message parameter 'result' is missing"; 428 DLOG(ERROR) << "JS message parameter 'result' is missing";
439 return NO; 429 return NO;
440 } 430 }
441 431
442 __weak PaymentRequestManager* weakSelf = self; 432 __weak PaymentRequestManager* weakSelf = self;
443 ProceduralBlock callback = ^{ 433 ProceduralBlock callback = ^{
444 PaymentRequestManager* strongSelf = weakSelf; 434 [weakSelf dismissUI];
445 // Early return if the manager has been deallocated. 435 [weakSelf.paymentRequestJsManager
446 if (!strongSelf)
447 return;
448 [strongSelf dismissUI];
449 [strongSelf.paymentRequestJsManager
450 resolveResponsePromiseWithCompletionHandler:nil]; 436 resolveResponsePromiseWithCompletionHandler:nil];
451 }; 437 };
452 438
453 // Display UI indicating failure if the value of |result| is "fail". 439 // Display UI indicating failure if the value of |result| is "fail".
454 if (result == "fail") { 440 if (result == "fail") {
455 [_paymentRequestCoordinator displayErrorWithCallback:callback]; 441 [_paymentRequestCoordinator displayErrorWithCallback:callback];
456 } else { 442 } else {
457 callback(); 443 callback();
458 } 444 }
459 445
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
528 [self webState]->GetNavigationManager()->GetLastCommittedItem(); 514 [self webState]->GetNavigationManager()->GetLastCommittedItem();
529 return navigationItem && 515 return navigationItem &&
530 navigationItem->GetSSL().security_style == 516 navigationItem->GetSSL().security_style ==
531 web::SECURITY_STYLE_AUTHENTICATED; 517 web::SECURITY_STYLE_AUTHENTICATED;
532 } 518 }
533 519
534 #pragma mark - PaymentRequestCoordinatorDelegate methods 520 #pragma mark - PaymentRequestCoordinatorDelegate methods
535 521
536 - (void)paymentRequestCoordinatorDidCancel: 522 - (void)paymentRequestCoordinatorDidCancel:
537 (PaymentRequestCoordinator*)coordinator { 523 (PaymentRequestCoordinator*)coordinator {
538 [self terminateRequestWithErrorMessage:@"The payment request was canceled." 524 [self terminateRequestWithErrorMessage:kCancelMessage callback:nil];
539 callback:nil]; 525 }
526
527 - (void)paymentRequestCoordinatorDidSelectSettings:
528 (PaymentRequestCoordinator*)coordinator {
529 ProceduralBlockWithBool callback = ^(BOOL) {
530 UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
531 DCHECK(mainWindow);
532 GenericChromeCommand* command =
533 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_AUTOFILL_SETTINGS];
534 [mainWindow chromeExecuteCommand:command];
535 };
536
537 [self terminateRequestWithErrorMessage:kCancelMessage callback:callback];
540 } 538 }
541 539
542 - (void)paymentRequestCoordinator:(PaymentRequestCoordinator*)coordinator 540 - (void)paymentRequestCoordinator:(PaymentRequestCoordinator*)coordinator
543 didConfirmWithPaymentResponse:(web::PaymentResponse)paymentResponse { 541 didConfirmWithPaymentResponse:(web::PaymentResponse)paymentResponse {
544 [_paymentRequestJsManager 542 [_paymentRequestJsManager
545 resolveRequestPromiseWithPaymentResponse:paymentResponse 543 resolveRequestPromiseWithPaymentResponse:paymentResponse
546 completionHandler:nil]; 544 completionHandler:nil];
547 [self setUnblockEventQueueTimer]; 545 [self setUnblockEventQueueTimer];
548 [self setPaymentResponseTimeoutTimer]; 546 [self setPaymentResponseTimeoutTimer];
549 } 547 }
(...skipping 19 matching lines...) Expand all
569 - (void)webState:(web::WebState*)webState 567 - (void)webState:(web::WebState*)webState
570 didCommitNavigationWithDetails: 568 didCommitNavigationWithDetails:
571 (const web::LoadCommittedDetails&)load_details { 569 (const web::LoadCommittedDetails&)load_details {
572 [self dismissUI]; 570 [self dismissUI];
573 _isScriptInjected = NO; 571 _isScriptInjected = NO;
574 [self enableCurrentWebState]; 572 [self enableCurrentWebState];
575 [self initializeWebViewForPaymentRequest]; 573 [self initializeWebViewForPaymentRequest];
576 } 574 }
577 575
578 @end 576 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698