Commit ca91c61f authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Implement CWVCreditCardNameFixer to help with name fix flows

For credit card saves where the name is missing, a fix flow may be
offered to the user to add one.

Bug: 1066690
Change-Id: Ifdfcbd016a8ba79635c9aa592b5a12192b8b8cb8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2132943
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755956}
parent 185700d3
...@@ -90,6 +90,7 @@ if (ios_web_view_enable_autofill) { ...@@ -90,6 +90,7 @@ if (ios_web_view_enable_autofill) {
"public/cwv_autofill_suggestion.h", "public/cwv_autofill_suggestion.h",
"public/cwv_credit_card.h", "public/cwv_credit_card.h",
"public/cwv_credit_card_expiration_fixer.h", "public/cwv_credit_card_expiration_fixer.h",
"public/cwv_credit_card_name_fixer.h",
"public/cwv_credit_card_saver.h", "public/cwv_credit_card_saver.h",
"public/cwv_credit_card_verifier.h", "public/cwv_credit_card_verifier.h",
"public/cwv_password.h", "public/cwv_password.h",
...@@ -256,6 +257,8 @@ if (ios_web_view_enable_autofill) { ...@@ -256,6 +257,8 @@ if (ios_web_view_enable_autofill) {
"internal/autofill/cwv_credit_card_expiration_fixer.mm", "internal/autofill/cwv_credit_card_expiration_fixer.mm",
"internal/autofill/cwv_credit_card_expiration_fixer_internal.h", "internal/autofill/cwv_credit_card_expiration_fixer_internal.h",
"internal/autofill/cwv_credit_card_internal.h", "internal/autofill/cwv_credit_card_internal.h",
"internal/autofill/cwv_credit_card_name_fixer.mm",
"internal/autofill/cwv_credit_card_name_fixer_internal.h",
"internal/autofill/cwv_credit_card_saver.mm", "internal/autofill/cwv_credit_card_saver.mm",
"internal/autofill/cwv_credit_card_saver_internal.h", "internal/autofill/cwv_credit_card_saver_internal.h",
"internal/autofill/cwv_credit_card_verifier.mm", "internal/autofill/cwv_credit_card_verifier.mm",
...@@ -419,6 +422,7 @@ test("ios_web_view_unittests") { ...@@ -419,6 +422,7 @@ test("ios_web_view_unittests") {
"internal/autofill/cwv_autofill_profile_unittest.mm", "internal/autofill/cwv_autofill_profile_unittest.mm",
"internal/autofill/cwv_autofill_suggestion_unittest.mm", "internal/autofill/cwv_autofill_suggestion_unittest.mm",
"internal/autofill/cwv_credit_card_expiration_fixer_unittest.mm", "internal/autofill/cwv_credit_card_expiration_fixer_unittest.mm",
"internal/autofill/cwv_credit_card_name_fixer_unittest.mm",
"internal/autofill/cwv_credit_card_saver_unittest.mm", "internal/autofill/cwv_credit_card_saver_unittest.mm",
"internal/autofill/cwv_credit_card_unittest.mm", "internal/autofill/cwv_credit_card_unittest.mm",
"internal/autofill/cwv_credit_card_verifier_unittest.mm", "internal/autofill/cwv_credit_card_verifier_unittest.mm",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web_view/internal/autofill/cwv_credit_card_name_fixer_internal.h"
#include <memory>
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/ui/payments/card_name_fix_flow_controller_impl.h"
#include "components/autofill/core/browser/ui/payments/card_name_fix_flow_view.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
// Empty implementation of |autofill::CardNameFixFlowView|.
class WebViewCardNameFixFlowView : public autofill::CardNameFixFlowView {
public:
void Show() override {
// No op.
}
void ControllerGone() override {
// No op.
}
};
} // namespace ios_web_view
@implementation CWVCreditCardNameFixer {
std::unique_ptr<autofill::CardNameFixFlowControllerImpl> _controller;
std::unique_ptr<ios_web_view::WebViewCardNameFixFlowView> _view;
}
- (instancetype)initWithName:(NSString*)name
callback:(base::OnceCallback<void(const base::string16&)>)
callback {
self = [super init];
if (self) {
_view = std::make_unique<ios_web_view::WebViewCardNameFixFlowView>();
_controller = std::make_unique<autofill::CardNameFixFlowControllerImpl>();
_controller->Show(_view.get(), base::SysNSStringToUTF16(name),
std::move(callback));
}
return self;
}
- (void)dealloc {
_controller->OnConfirmNameDialogClosed();
}
#pragma mark - Public
- (NSString*)inferredCardHolderName {
return base::SysUTF16ToNSString(_controller->GetInferredCardholderName());
}
- (NSString*)cancelButtonLabel {
return base::SysUTF16ToNSString(_controller->GetCancelButtonLabel());
}
- (NSString*)inferredNameTooltipText {
return base::SysUTF16ToNSString(_controller->GetInferredNameTooltipText());
}
- (NSString*)inputLabel {
return base::SysUTF16ToNSString(_controller->GetInputLabel());
}
- (NSString*)inputPlaceholderText {
return base::SysUTF16ToNSString(_controller->GetInputLabel());
}
- (NSString*)saveButtonLabel {
return base::SysUTF16ToNSString(_controller->GetSaveButtonLabel());
}
- (NSString*)titleText {
return base::SysUTF16ToNSString(_controller->GetTitleText());
}
- (void)acceptWithName:(NSString*)name {
_controller->OnNameAccepted(base::SysNSStringToUTF16(name));
}
- (void)cancel {
_controller->OnDismissed();
}
@end
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_CREDIT_CARD_NAME_FIXER_INTERNAL_H_
#define IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_CREDIT_CARD_NAME_FIXER_INTERNAL_H_
#import "ios/web_view/public/cwv_credit_card_name_fixer.h"
#include "base/callback.h"
#include "base/strings/string16.h"
@interface CWVCreditCardNameFixer ()
// Initialize with a suggested |name| and a |callback| to be invoked with the
// chosen name.
- (instancetype)initWithName:(NSString*)name
callback:(base::OnceCallback<void(const base::string16&)>)
callback NS_DESIGNATED_INITIALIZER;
@end
#endif // IOS_WEB_VIEW_INTERNAL_AUTOFILL_CWV_CREDIT_CARD_NAME_FIXER_INTERNAL_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web_view/internal/autofill/cwv_credit_card_name_fixer_internal.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/strings/sys_string_conversions.h"
#include "ios/web_view/test/test_with_locale_and_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace ios_web_view {
class CWVCreditCardNameFixerTest : public TestWithLocaleAndResources {
public:
void AcceptNameCallback(const base::string16& name) {
accepted_name_ = base::SysUTF16ToNSString(name);
}
NSString* accepted_name_;
};
// Tests CWVCreditCardNameFixer properties.
TEST_F(CWVCreditCardNameFixerTest, Properties) {
NSString* name = @"John Doe";
CWVCreditCardNameFixer* fixer =
[[CWVCreditCardNameFixer alloc] initWithName:name
callback:base::DoNothing()];
EXPECT_NSEQ(name, fixer.inferredCardHolderName);
EXPECT_TRUE(fixer.inferredCardHolderName);
EXPECT_TRUE(fixer.cancelButtonLabel);
EXPECT_TRUE(fixer.inferredNameTooltipText);
EXPECT_TRUE(fixer.inputLabel);
EXPECT_TRUE(fixer.inputPlaceholderText);
EXPECT_TRUE(fixer.saveButtonLabel);
EXPECT_TRUE(fixer.titleText);
}
// Tests CWVCreditCardNameFixer properly accepts the chosen name.
TEST_F(CWVCreditCardNameFixerTest, AcceptName) {
NSString* inferred_name = @"John Doe";
NSString* accepted_name = @"Jane Doe";
base::OnceCallback<void(const base::string16&)> callback = base::BindOnce(
&CWVCreditCardNameFixerTest::AcceptNameCallback, base::Unretained(this));
CWVCreditCardNameFixer* fixer =
[[CWVCreditCardNameFixer alloc] initWithName:inferred_name
callback:std::move(callback)];
[fixer acceptWithName:accepted_name];
EXPECT_NSEQ(accepted_name, accepted_name_);
}
} // namespace ios_web_view
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_VIEW_PUBLIC_CWV_CREDIT_CARD_NAME_FIXER_H_
#define IOS_WEB_VIEW_PUBLIC_CWV_CREDIT_CARD_NAME_FIXER_H_
#import <Foundation/Foundation.h>
#import "cwv_export.h"
NS_ASSUME_NONNULL_BEGIN
// Interface for responding to name fix requests when saving a credit card. This
// can happen if a card was entered without a name. Depending on the user's
// decision, either call |acceptWithName:| or |cancel|, not both.
CWV_EXPORT
@interface CWVCreditCardNameFixer : NSObject
// The suggested card holder name, for example from the user's Google Account.
// Can be empty if there's no name to suggest.
@property(nonatomic, readonly, copy) NSString* inferredCardHolderName;
// Label for cancel button.
@property(nonatomic, readonly, copy) NSString* cancelButtonLabel;
// Extra explanatory text for |inferredCardHolderName|.
@property(nonatomic, readonly, copy) NSString* inferredNameTooltipText;
// Label for the input control.
@property(nonatomic, readonly, copy) NSString* inputLabel;
// Placeholder text for use in the input control.
@property(nonatomic, readonly, copy) NSString* inputPlaceholderText;
// Label for the save button.
@property(nonatomic, readonly, copy) NSString* saveButtonLabel;
// Title for the navigation bar.
@property(nonatomic, readonly, copy) NSString* titleText;
- (instancetype)init NS_UNAVAILABLE;
// Accepts |name| as the name to associate with the card.
- (void)acceptWithName:(NSString*)name;
// Cancels the name fix. The card will not be saved.
- (void)cancel;
@end
NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_PUBLIC_CWV_CREDIT_CARD_NAME_FIXER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment