Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
| 6 | 6 |
| 7 #include "ios/chrome/browser/ui/payments/payment_request_mediator.h" | 7 #include "ios/chrome/browser/ui/payments/payment_request_mediator.h" |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/autofill/core/browser/autofill_data_util.h" | 11 #include "components/autofill/core/browser/autofill_data_util.h" |
| 12 #include "components/autofill/core/browser/autofill_profile.h" | 12 #include "components/autofill/core/browser/autofill_profile.h" |
| 13 #include "components/autofill/core/browser/credit_card.h" | 13 #include "components/autofill/core/browser/credit_card.h" |
| 14 #include "components/autofill/core/browser/field_types.h" | 14 #include "components/autofill/core/browser/field_types.h" |
| 15 #include "components/payments/core/currency_formatter.h" | 15 #include "components/payments/core/currency_formatter.h" |
| 16 #include "components/payments/core/payment_prefs.h" | |
| 16 #include "components/payments/core/strings_util.h" | 17 #include "components/payments/core/strings_util.h" |
| 17 #include "components/signin/core/browser/signin_manager.h" | 18 #include "components/prefs/pref_service.h" |
| 18 #include "components/strings/grit/components_strings.h" | 19 #include "components/strings/grit/components_strings.h" |
| 19 #include "ios/chrome/browser/payments/payment_request.h" | 20 #include "ios/chrome/browser/payments/payment_request.h" |
| 20 #include "ios/chrome/browser/payments/payment_request_util.h" | 21 #include "ios/chrome/browser/payments/payment_request_util.h" |
| 21 #include "ios/chrome/browser/signin/signin_manager_factory.h" | |
| 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h" | 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h" |
| 23 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h" | 23 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_footer_item .h" |
| 24 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 24 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 25 #import "ios/chrome/browser/ui/payments/cells/autofill_profile_item.h" | 25 #import "ios/chrome/browser/ui/payments/cells/autofill_profile_item.h" |
| 26 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h" | 26 #import "ios/chrome/browser/ui/payments/cells/payment_method_item.h" |
| 27 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h" | 27 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h" |
| 28 #import "ios/chrome/browser/ui/payments/cells/price_item.h" | 28 #import "ios/chrome/browser/ui/payments/cells/price_item.h" |
| 29 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 29 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 43 using ::payments::GetShippingOptionSectionString; | 43 using ::payments::GetShippingOptionSectionString; |
| 44 using ::payment_request_util::GetEmailLabelFromAutofillProfile; | 44 using ::payment_request_util::GetEmailLabelFromAutofillProfile; |
| 45 using ::payment_request_util::GetNameLabelFromAutofillProfile; | 45 using ::payment_request_util::GetNameLabelFromAutofillProfile; |
| 46 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; | 46 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; |
| 47 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; | 47 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; |
| 48 using ::payment_request_util::GetShippingSectionTitle; | 48 using ::payment_request_util::GetShippingSectionTitle; |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 @interface PaymentRequestMediator () | 51 @interface PaymentRequestMediator () |
| 52 | 52 |
| 53 @property(nonatomic, assign) ios::ChromeBrowserState* browserState; | |
| 54 | |
| 55 // The PaymentRequest object owning an instance of web::PaymentRequest as | 53 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 56 // provided by the page invoking the Payment Request API. This is a weak | 54 // provided by the page invoking the Payment Request API. This is a weak |
| 57 // pointer and should outlive this class. | 55 // pointer and should outlive this class. |
| 58 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 56 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
| 59 | 57 |
| 60 @end | 58 @end |
| 61 | 59 |
| 62 @implementation PaymentRequestMediator | 60 @implementation PaymentRequestMediator |
| 63 | 61 |
| 64 @synthesize totalValueChanged = _totalValueChanged; | 62 @synthesize totalValueChanged = _totalValueChanged; |
| 65 @synthesize browserState = _browserState; | |
| 66 @synthesize paymentRequest = _paymentRequest; | 63 @synthesize paymentRequest = _paymentRequest; |
| 67 | 64 |
| 68 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | 65 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
| 69 paymentRequest:(PaymentRequest*)paymentRequest { | |
| 70 DCHECK(browserState); | |
| 71 self = [super init]; | 66 self = [super init]; |
| 72 if (self) { | 67 if (self) { |
| 73 _browserState = browserState; | |
| 74 _paymentRequest = paymentRequest; | 68 _paymentRequest = paymentRequest; |
| 75 } | 69 } |
| 76 return self; | 70 return self; |
| 77 } | 71 } |
| 78 | 72 |
| 79 #pragma mark - PaymentRequestViewControllerDataSource | 73 #pragma mark - PaymentRequestViewControllerDataSource |
| 80 | 74 |
| 81 - (BOOL)canPay { | 75 - (BOOL)canPay { |
| 82 return self.paymentRequest->selected_credit_card() != nullptr && | 76 return self.paymentRequest->selected_credit_card() != nullptr && |
| 83 (self.paymentRequest->selected_shipping_option() != nullptr || | 77 (self.paymentRequest->selected_shipping_option() != nullptr || |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 uppercaseStringWithLocale:[NSLocale currentLocale]]; | 245 uppercaseStringWithLocale:[NSLocale currentLocale]]; |
| 252 } else { | 246 } else { |
| 253 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator; | 247 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 254 } | 248 } |
| 255 return item; | 249 return item; |
| 256 } | 250 } |
| 257 | 251 |
| 258 - (CollectionViewFooterItem*)footerItem { | 252 - (CollectionViewFooterItem*)footerItem { |
| 259 CollectionViewFooterItem* item = [[CollectionViewFooterItem alloc] init]; | 253 CollectionViewFooterItem* item = [[CollectionViewFooterItem alloc] init]; |
| 260 | 254 |
| 261 // TODO(crbug.com/602666): Find out if the first payment has completed. | 255 // If no transaction has been completed so far, choose which string to display |
| 262 BOOL firstPaymentCompleted = YES; | 256 // as a function of the profile's signed in state. Otherwise, always show the |
| 263 if (!firstPaymentCompleted) { | 257 // same string. |
| 258 bool firstTransactionCompleted = | |
|
macourteau
2017/07/05 14:10:36
optional nit (personal preference): const bool
Moe
2017/07/05 16:01:47
Done.
| |
| 259 _paymentRequest->GetPrefService()->GetBoolean( | |
| 260 payments::kPaymentsFirstTransactionCompleted); | |
| 261 if (firstTransactionCompleted) { | |
| 264 item.text = l10n_util::GetNSString(IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS); | 262 item.text = l10n_util::GetNSString(IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS); |
| 265 } else if ([[self authenticatedAccountName] length]) { | |
| 266 const base::string16 accountName = | |
| 267 base::SysNSStringToUTF16([self authenticatedAccountName]); | |
| 268 const std::string formattedString = l10n_util::GetStringFUTF8( | |
| 269 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN, accountName); | |
| 270 item.text = base::SysUTF8ToNSString(formattedString); | |
| 271 } else { | 263 } else { |
| 272 item.text = l10n_util::GetNSString( | 264 const std::string email = _paymentRequest->GetAuthenticatedEmail(); |
| 273 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT); | 265 if (!email.empty()) { |
| 266 const std::string formattedString = l10n_util::GetStringFUTF8( | |
| 267 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_IN, | |
| 268 base::UTF8ToUTF16(email)); | |
| 269 item.text = base::SysUTF8ToNSString(formattedString); | |
| 270 } else { | |
| 271 item.text = l10n_util::GetNSString( | |
| 272 IDS_PAYMENTS_CARD_AND_ADDRESS_SETTINGS_SIGNED_OUT); | |
| 273 } | |
| 274 } | 274 } |
| 275 item.linkURL = GURL(kSettingsURL); | 275 item.linkURL = GURL(kSettingsURL); |
| 276 return item; | 276 return item; |
| 277 } | 277 } |
| 278 | 278 |
| 279 #pragma mark - Helper methods | |
| 280 | |
| 281 // Returns the authenticated account name, or nil if user is not authenticated. | |
| 282 - (NSString*)authenticatedAccountName { | |
| 283 const SigninManager* signinManager = | |
| 284 ios::SigninManagerFactory::GetForBrowserStateIfExists(self.browserState); | |
| 285 if (signinManager && signinManager->IsAuthenticated()) { | |
| 286 return base::SysUTF8ToNSString( | |
| 287 signinManager->GetAuthenticatedAccountInfo().email); | |
| 288 } | |
| 289 return nil; | |
| 290 } | |
| 291 | |
| 292 @end | 279 @end |
| OLD | NEW |