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 #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 Loading... | |
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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 PaymentRequestManager* strongSelf = weakSelf; |
378 // Early return if the manager has been deallocated. | 383 // Early return if the manager has been deallocated. |
379 if (!strongSelf) | 384 if (!strongSelf) |
Moe
2017/04/06 15:35:43
Could you please call the method on weakSelf here
macourteau
2017/04/06 18:38:47
Done.
| |
380 return; | 385 return; |
381 [[strongSelf paymentRequestJsManager] | 386 [[strongSelf paymentRequestJsManager] |
382 resolveAbortPromiseWithCompletionHandler:nil]; | 387 resolveAbortPromiseWithCompletionHandler:nil]; |
383 }; | 388 }; |
384 | 389 |
385 ProceduralBlock callback = ^{ | 390 ProceduralBlock callback = ^{ |
386 PaymentRequestManager* strongSelf = weakSelf; | 391 [weakSelf terminateRequestWithErrorMessage:kAbortMessage |
387 // Early return if the manager has been deallocated. | 392 callback:cancellationCallback]; |
388 if (!strongSelf) | |
389 return; | |
390 [strongSelf | |
391 terminateRequestWithErrorMessage:@"The payment request was aborted." | |
392 callback:cancellationCallback]; | |
393 }; | 393 }; |
394 | 394 |
395 [_paymentRequestCoordinator displayErrorWithCallback:callback]; | 395 [_paymentRequestCoordinator displayErrorWithCallback:callback]; |
396 | 396 |
397 return YES; | 397 return YES; |
398 } | 398 } |
399 | 399 |
400 - (BOOL)displayErrorThenCancelRequest { | 400 - (BOOL)displayErrorThenCancelRequest { |
401 // TODO(crbug.com/602666): Check that there is already a pending request. | 401 // TODO(crbug.com/602666): Check that there is already a pending request. |
402 | 402 |
403 [_unblockEventQueueTimer invalidate]; | 403 [_unblockEventQueueTimer invalidate]; |
404 [_paymentResponseTimeoutTimer invalidate]; | 404 [_paymentResponseTimeoutTimer invalidate]; |
405 [_updateEventTimeoutTimer invalidate]; | 405 [_updateEventTimeoutTimer invalidate]; |
406 | 406 |
407 __weak PaymentRequestManager* weakSelf = self; | 407 __weak PaymentRequestManager* weakSelf = self; |
408 ProceduralBlock callback = ^{ | 408 ProceduralBlock callback = ^{ |
409 PaymentRequestManager* strongSelf = weakSelf; | 409 [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 }; | 410 }; |
417 | 411 |
418 [_paymentRequestCoordinator displayErrorWithCallback:callback]; | 412 [_paymentRequestCoordinator displayErrorWithCallback:callback]; |
419 | 413 |
420 return YES; | 414 return YES; |
421 } | 415 } |
422 | 416 |
423 - (BOOL)doResponseComplete { | 417 - (BOOL)doResponseComplete { |
424 base::DictionaryValue command; | 418 base::DictionaryValue command; |
425 command.SetString("result", "unknown"); | 419 command.SetString("result", "unknown"); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
528 [self webState]->GetNavigationManager()->GetLastCommittedItem(); | 522 [self webState]->GetNavigationManager()->GetLastCommittedItem(); |
529 return navigationItem && | 523 return navigationItem && |
530 navigationItem->GetSSL().security_style == | 524 navigationItem->GetSSL().security_style == |
531 web::SECURITY_STYLE_AUTHENTICATED; | 525 web::SECURITY_STYLE_AUTHENTICATED; |
532 } | 526 } |
533 | 527 |
534 #pragma mark - PaymentRequestCoordinatorDelegate methods | 528 #pragma mark - PaymentRequestCoordinatorDelegate methods |
535 | 529 |
536 - (void)paymentRequestCoordinatorDidCancel: | 530 - (void)paymentRequestCoordinatorDidCancel: |
537 (PaymentRequestCoordinator*)coordinator { | 531 (PaymentRequestCoordinator*)coordinator { |
538 [self terminateRequestWithErrorMessage:@"The payment request was canceled." | 532 [self terminateRequestWithErrorMessage:kCancelMessage callback:nil]; |
539 callback:nil]; | 533 } |
534 | |
535 - (void)paymentRequestCoordinatorDidSelectSettings: | |
536 (PaymentRequestCoordinator*)coordinator { | |
537 ProceduralBlockWithBool callback = ^(BOOL) { | |
538 UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow]; | |
539 DCHECK(mainWindow); | |
540 GenericChromeCommand* command = | |
541 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_AUTOFILL_SETTINGS]; | |
542 [mainWindow chromeExecuteCommand:command]; | |
543 }; | |
544 | |
545 [self terminateRequestWithErrorMessage:kCancelMessage callback:callback]; | |
540 } | 546 } |
541 | 547 |
542 - (void)paymentRequestCoordinator:(PaymentRequestCoordinator*)coordinator | 548 - (void)paymentRequestCoordinator:(PaymentRequestCoordinator*)coordinator |
543 didConfirmWithPaymentResponse:(web::PaymentResponse)paymentResponse { | 549 didConfirmWithPaymentResponse:(web::PaymentResponse)paymentResponse { |
544 [_paymentRequestJsManager | 550 [_paymentRequestJsManager |
545 resolveRequestPromiseWithPaymentResponse:paymentResponse | 551 resolveRequestPromiseWithPaymentResponse:paymentResponse |
546 completionHandler:nil]; | 552 completionHandler:nil]; |
547 [self setUnblockEventQueueTimer]; | 553 [self setUnblockEventQueueTimer]; |
548 [self setPaymentResponseTimeoutTimer]; | 554 [self setPaymentResponseTimeoutTimer]; |
549 } | 555 } |
(...skipping 19 matching lines...) Expand all Loading... | |
569 - (void)webState:(web::WebState*)webState | 575 - (void)webState:(web::WebState*)webState |
570 didCommitNavigationWithDetails: | 576 didCommitNavigationWithDetails: |
571 (const web::LoadCommittedDetails&)load_details { | 577 (const web::LoadCommittedDetails&)load_details { |
572 [self dismissUI]; | 578 [self dismissUI]; |
573 _isScriptInjected = NO; | 579 _isScriptInjected = NO; |
574 [self enableCurrentWebState]; | 580 [self enableCurrentWebState]; |
575 [self initializeWebViewForPaymentRequest]; | 581 [self initializeWebViewForPaymentRequest]; |
576 } | 582 } |
577 | 583 |
578 @end | 584 @end |
OLD | NEW |