Commit ca7902e6 authored by Tommy Martino's avatar Tommy Martino Committed by Commit Bot

[MF Android] Adds Credit Card Controller

This CL adds the CreditCardAccessoryController class, the class in C++
land responsible for communicating with the PersonalDataManager and
propagating the results to the Manual Filling controller (and, thus,
to the Java frontend).

Change-Id: Icffa6d2f9d8c140de31e18d92f747a090645a37f
Bug: 902425
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1599927
Commit-Queue: Tommy Martino <tmartino@chromium.org>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660625}
parent 5dad9e84
......@@ -2610,6 +2610,9 @@ jumbo_split_static_library("browser") {
"autofill/android/phone_number_util_android.cc",
"autofill/autofill_keyboard_accessory_adapter.cc",
"autofill/autofill_keyboard_accessory_adapter.h",
"autofill/credit_card_accessory_controller.h",
"autofill/credit_card_accessory_controller_impl.cc",
"autofill/credit_card_accessory_controller_impl.h",
"autofill/manual_filling_controller.h",
"autofill/manual_filling_controller_impl.cc",
"autofill/manual_filling_controller_impl.h",
......
// Copyright 2019 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 CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_H_
#define CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_H_
#include "chrome/browser/autofill/accessory_controller.h"
namespace autofill {
// Interface for credit card-specific keyboard accessory controller between the
// ManualFillingController and Autofill backend logic.
class CreditCardAccessoryController : public AccessoryController {
public:
CreditCardAccessoryController() = default;
~CreditCardAccessoryController() override = default;
// Fetches suggestions and propagates to the frontend.
virtual void RefreshSuggestionsForField() = 0;
};
} // namespace autofill
#endif // CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_H_
// Copyright 2019 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.
#include "chrome/browser/autofill/credit_card_accessory_controller_impl.h"
#include <iterator>
#include <vector>
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/manual_filling_controller.h"
#include "chrome/browser/autofill/manual_filling_utils.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace autofill {
namespace {
base::string16 GetTitle(bool unused) {
return l10n_util::GetStringUTF16(IDS_MANUAL_FILLING_CREDIT_CARD_SHEET_TITLE);
}
void AddField(const base::string16& data, UserInfo* user_info) {
user_info->add_field(UserInfo::Field(data, data,
/*is_password=*/false,
/*selectable=*/true));
}
UserInfo Translate(const CreditCard* data) {
DCHECK(data);
UserInfo user_info;
AddField(data->ObfuscatedLastFourDigits(), &user_info);
if (data->HasValidExpirationDate()) {
// TOOD(crbug.com/902425): Pass expiration date as grouped values
AddField(data->ExpirationMonthAsString(), &user_info);
AddField(data->Expiration4DigitYearAsString(), &user_info);
}
if (data->HasNameOnCard()) {
AddField(data->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL), &user_info);
}
return user_info;
}
} // namespace
CreditCardAccessoryControllerImpl::CreditCardAccessoryControllerImpl(
autofill::PersonalDataManager* personal_data_manager,
content::WebContents* web_contents)
: personal_data_manager_(personal_data_manager),
web_contents_(web_contents) {}
CreditCardAccessoryControllerImpl::~CreditCardAccessoryControllerImpl() =
default;
void CreditCardAccessoryControllerImpl::RefreshSuggestionsForField() {
const std::vector<CreditCard*> suggestions = GetSuggestions();
std::vector<UserInfo> info_to_add;
std::transform(suggestions.begin(), suggestions.end(),
std::back_inserter(info_to_add), &Translate);
// TODO(crbug.com/902425): Add "Manage payment methods" footer command
std::vector<FooterCommand> footer_commands;
bool has_suggestions = !info_to_add.empty();
GetManualFillingController()->RefreshSuggestionsForField(
/*is_fillable=*/true,
autofill::CreateAccessorySheetData(GetTitle(has_suggestions),
std::move(info_to_add),
std::move(footer_commands)));
}
void CreditCardAccessoryControllerImpl::SetManualFillingControllerForTesting(
base::WeakPtr<ManualFillingController> controller) {
mf_controller_ = controller;
}
const std::vector<CreditCard*>
CreditCardAccessoryControllerImpl::GetSuggestions() {
DCHECK(personal_data_manager_);
return personal_data_manager_->GetCreditCardsToSuggest(
/*include_server_cards=*/true);
}
base::WeakPtr<ManualFillingController>
CreditCardAccessoryControllerImpl::GetManualFillingController() {
if (!mf_controller_)
mf_controller_ = ManualFillingController::GetOrCreate(web_contents_);
DCHECK(mf_controller_);
return mf_controller_;
}
} // namespace autofill
// Copyright 2019 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 CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_IMPL_H_
#define CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_IMPL_H_
#include "chrome/browser/autofill/credit_card_accessory_controller.h"
#include "base/memory/weak_ptr.h"
#include "components/autofill/core/browser/personal_data_manager.h"
namespace content {
class WebContents;
}
class ManualFillingController;
namespace autofill {
class CreditCardAccessoryControllerImpl : public CreditCardAccessoryController {
public:
CreditCardAccessoryControllerImpl(PersonalDataManager* personal_data_manager,
content::WebContents* web_contents);
~CreditCardAccessoryControllerImpl() override;
// AccessoryController:
// TODO(crbug.com/902425): Implement filling logic.
void OnFillingTriggered(const UserInfo::Field& selection) override {}
// CreditCardAccessoryController:
void RefreshSuggestionsForField() override;
void SetManualFillingControllerForTesting(
base::WeakPtr<ManualFillingController> controller);
private:
const std::vector<CreditCard*> GetSuggestions();
base::WeakPtr<ManualFillingController> GetManualFillingController();
const PersonalDataManager* personal_data_manager_;
content::WebContents* web_contents_;
base::WeakPtr<ManualFillingController> mf_controller_;
};
} // namespace autofill
#endif // CHROME_BROWSER_AUTOFILL_CREDIT_CARD_ACCESSORY_CONTROLLER_IMPL_H_
// Copyright 2019 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.
#include "chrome/browser/autofill/credit_card_accessory_controller_impl.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/autofill/mock_manual_filling_controller.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::SaveArg;
namespace autofill {
class CreditCardAccessoryControllerTest : public testing::Test {
public:
CreditCardAccessoryControllerTest()
: controller_(&data_manager_, /*web_contents=*/nullptr) {}
void SetUp() override {
controller_.SetManualFillingControllerForTesting(
mock_mf_controller_.AsWeakPtr());
data_manager_.SetPrefService(profile_.GetPrefs());
}
void TearDown() override {
data_manager_.SetPrefService(nullptr);
data_manager_.ClearCreditCards();
}
protected:
content::TestBrowserThreadBundle
test_browser_thread_bundle_; // for |profile_|
CreditCardAccessoryControllerImpl controller_;
testing::StrictMock<MockManualFillingController> mock_mf_controller_;
autofill::TestPersonalDataManager data_manager_;
TestingProfile profile_;
};
TEST_F(CreditCardAccessoryControllerTest, RefreshSuggestionsForField) {
autofill::CreditCard card;
card.SetNumber(base::ASCIIToUTF16("4111111111111111"));
card.SetExpirationMonth(04);
card.SetExpirationYear(39);
card.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL,
base::ASCIIToUTF16("Kirby Puckett"));
data_manager_.AddCreditCard(card);
autofill::AccessorySheetData result(autofill::FallbackSheetType::PASSWORD,
base::string16());
EXPECT_CALL(mock_mf_controller_, RefreshSuggestionsForField(
/*is_fillable=*/true, _))
.WillOnce(SaveArg<1>(&result));
controller_.RefreshSuggestionsForField();
ASSERT_EQ(1u, result.user_info_list().size());
auto info = result.user_info_list().at(0);
ASSERT_EQ(4u, info.fields().size());
base::string16 expected_cc_string =
autofill::internal::GetObfuscatedStringForCardDigits(
base::ASCIIToUTF16("1111"));
EXPECT_EQ(expected_cc_string, info.fields().at(0).display_text());
EXPECT_EQ(base::ASCIIToUTF16("04"), info.fields().at(1).display_text());
EXPECT_EQ(base::ASCIIToUTF16("2039"), info.fields().at(2).display_text());
EXPECT_EQ(base::ASCIIToUTF16("Kirby Puckett"),
info.fields().at(3).display_text());
}
} // namespace autofill
......@@ -3306,6 +3306,7 @@ test("unit_tests") {
"../browser/autofill/autofill_credit_card_filling_infobar_delegate_mobile_unittest.cc",
"../browser/autofill/autofill_keyboard_accessory_adapter_unittest.cc",
"../browser/autofill/autofill_save_card_infobar_delegate_mobile_unittest.cc",
"../browser/autofill/credit_card_accessory_controller_impl_unittest.cc",
"../browser/autofill/manual_filling_controller_impl_unittest.cc",
"../browser/media/android/cdm/media_drm_origin_id_manager_unittest.cc",
"../browser/metrics/android_metrics_provider_unittest.cc",
......
......@@ -251,4 +251,11 @@
</else>
</if>
<!-- Manual filling (keyboard accessory) strings -->
<if expr="is_android">
<message name="IDS_MANUAL_FILLING_CREDIT_CARD_SHEET_TITLE" desc="Title shown at the top of a sheet where users can select indiviual data pieces (e.g., the cardholder name) related to their stored payment methods (e.g., credit cards) to fill that data piece into the focused form field. Sentence-cased.">
Payment methods
</message>
</if>
</grit-part>
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