Commit 259427f1 authored by gogerald's avatar gogerald Committed by Commit Bot

[Autofill Assistant] Implement autofill card form

src/chrome/test:browser_tests will be added later on Desktop
since it is not available on Android (crbug.com/611756).

Bug: 806868
Change-Id: I1f0896057d3a3608ff72e1205c4c4f372ea56c71
Reviewed-on: https://chromium-review.googlesource.com/1239440Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Ganggui Tang <gogerald@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595585}
parent 57fb5ba9
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "chrome/browser/android/chrome_feature_list.h" #include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/channel_info.h" #include "chrome/common/channel_info.h"
#include "components/autofill_assistant/browser/controller.h" #include "components/autofill_assistant/browser/controller.h"
#include "components/variations/variations_associated_data.h" #include "components/variations/variations_associated_data.h"
...@@ -145,6 +147,11 @@ std::string UiControllerAndroid::GetApiKey() { ...@@ -145,6 +147,11 @@ std::string UiControllerAndroid::GetApiKey() {
return api_key; return api_key;
} }
autofill::PersonalDataManager* UiControllerAndroid::GetPersonalDataManager() {
return autofill::PersonalDataManagerFactory::GetForProfile(
ProfileManager::GetLastUsedProfile());
}
std::string UiControllerAndroid::GetServerUrl() { std::string UiControllerAndroid::GetServerUrl() {
return variations::GetVariationParamValueByFeature( return variations::GetVariationParamValueByFeature(
chrome::android::kAutofillAssistant, "url"); chrome::android::kAutofillAssistant, "url");
......
...@@ -39,6 +39,7 @@ class UiControllerAndroid : public UiController, public Client { ...@@ -39,6 +39,7 @@ class UiControllerAndroid : public UiController, public Client {
// Overrides Client: // Overrides Client:
std::string GetApiKey() override; std::string GetApiKey() override;
autofill::PersonalDataManager* GetPersonalDataManager() override;
std::string GetServerUrl() override; std::string GetServerUrl() override;
UiController* GetUiController() override; UiController* GetUiController() override;
......
...@@ -148,7 +148,7 @@ class PersonalDataManager : public KeyedService, ...@@ -148,7 +148,7 @@ class PersonalDataManager : public KeyedService,
// Returns the profile with the specified |guid|, or nullptr if there is no // Returns the profile with the specified |guid|, or nullptr if there is no
// profile with the specified |guid|. Both web and auxiliary profiles may // profile with the specified |guid|. Both web and auxiliary profiles may
// be returned. // be returned.
AutofillProfile* GetProfileByGUID(const std::string& guid); virtual AutofillProfile* GetProfileByGUID(const std::string& guid);
// Returns the profile with the specified |guid| from the given |profiles|, or // Returns the profile with the specified |guid| from the given |profiles|, or
// nullptr if there is no profile with the specified |guid|. // nullptr if there is no profile with the specified |guid|.
......
...@@ -13,8 +13,13 @@ ...@@ -13,8 +13,13 @@
class GURL; class GURL;
namespace autofill { namespace autofill {
class AutofillProfile; class CreditCard;
} class PersonalDataManager;
} // namespace autofill
namespace content {
class WebContents;
} // namespace content
namespace autofill_assistant { namespace autofill_assistant {
class ClientMemory; class ClientMemory;
...@@ -54,9 +59,10 @@ class ActionDelegate { ...@@ -54,9 +59,10 @@ class ActionDelegate {
virtual void ChooseCard( virtual void ChooseCard(
base::OnceCallback<void(const std::string&)> callback) = 0; base::OnceCallback<void(const std::string&)> callback) = 0;
// Fill the card form given by |selectors| with the given card |guid| in // Fill the card form given by |selectors| with the given |card| and its
// personal data manager. // |cvc|. Return result asynchronously through |callback|.
virtual void FillCardForm(const std::string& guid, virtual void FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) = 0; base::OnceCallback<void(bool)> callback) = 0;
...@@ -82,10 +88,6 @@ class ActionDelegate { ...@@ -82,10 +88,6 @@ class ActionDelegate {
const std::string& value, const std::string& value,
base::OnceCallback<void(bool)> callback) = 0; base::OnceCallback<void(bool)> callback) = 0;
// Get the AutofillProfile with ID |guid|, or nullptr if it doesn't exist.
virtual const autofill::AutofillProfile* GetAutofillProfile(
const std::string& guid) = 0;
// Given an element |selectors| on the page as the root element, build a node // Given an element |selectors| on the page as the root element, build a node
// tree using the output parameter |node_tree_out| as a starting node. // tree using the output parameter |node_tree_out| as a starting node.
virtual void BuildNodeTree(const std::vector<std::string>& selectors, virtual void BuildNodeTree(const std::vector<std::string>& selectors,
...@@ -106,6 +108,12 @@ class ActionDelegate { ...@@ -106,6 +108,12 @@ class ActionDelegate {
// Return the current ClientMemory. // Return the current ClientMemory.
virtual ClientMemory* GetClientMemory() = 0; virtual ClientMemory* GetClientMemory() = 0;
// Get current personal data manager.
virtual autofill::PersonalDataManager* GetPersonalDataManager() = 0;
// Get associated web contents.
virtual content::WebContents* GetWebContents() = 0;
protected: protected:
ActionDelegate() = default; ActionDelegate() = default;
}; };
......
...@@ -10,13 +10,83 @@ ...@@ -10,13 +10,83 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/content_autofill_driver_factory.h"
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/payments/full_card_request.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill_assistant/browser/actions/action_delegate.h" #include "components/autofill_assistant/browser/actions/action_delegate.h"
#include "components/autofill_assistant/browser/client_memory.h" #include "components/autofill_assistant/browser/client_memory.h"
#include "content/public/browser/web_contents.h"
namespace autofill_assistant { namespace autofill_assistant {
namespace {
// Self-deleting requester of full card details, including full PAN and the CVC
// number.
class SelfDeleteFullCardRequester
: public autofill::payments::FullCardRequest::ResultDelegate {
public:
SelfDeleteFullCardRequester() : weak_ptr_factory_(this) {}
using GetFullCardCallback =
base::OnceCallback<void(std::unique_ptr<autofill::CreditCard>,
const base::string16& cvc)>;
void GetFullCard(content::WebContents* web_contents,
autofill::CreditCard* card,
GetFullCardCallback callback) {
DCHECK(card);
callback_ = std::move(callback);
autofill::ContentAutofillDriverFactory* factory =
autofill::ContentAutofillDriverFactory::FromWebContents(web_contents);
if (!factory) {
OnFullCardRequestFailed();
return;
}
autofill::ContentAutofillDriver* driver =
factory->DriverForFrame(web_contents->GetMainFrame());
if (!driver) {
OnFullCardRequestFailed();
return;
}
driver->autofill_manager()->GetOrCreateFullCardRequest()->GetFullCard(
*card, autofill::AutofillClient::UNMASK_FOR_AUTOFILL,
weak_ptr_factory_.GetWeakPtr(),
driver->autofill_manager()->GetAsFullCardRequestUIDelegate());
}
private:
~SelfDeleteFullCardRequester() override {}
// payments::FullCardRequest::ResultDelegate:
void OnFullCardRequestSucceeded(
const autofill::payments::FullCardRequest& /* full_card_request */,
const autofill::CreditCard& card,
const base::string16& cvc) override {
std::move(callback_).Run(std::make_unique<autofill::CreditCard>(card), cvc);
delete this;
}
// payments::FullCardRequest::ResultDelegate:
void OnFullCardRequestFailed() override {
// Failed might because of cancel, so return nullptr to notice caller.
std::move(callback_).Run(nullptr, base::string16());
delete this;
}
GetFullCardCallback callback_;
base::WeakPtrFactory<SelfDeleteFullCardRequester> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SelfDeleteFullCardRequester);
};
} // namespace
AutofillAction::AutofillAction(const ActionProto& proto) AutofillAction::AutofillAction(const ActionProto& proto)
: Action(proto), pending_set_field_value_(0), weak_ptr_factory_(this) { : Action(proto), pending_set_field_value_(0), weak_ptr_factory_(this) {
if (proto.has_use_address()) { if (proto.has_use_address()) {
...@@ -129,10 +199,15 @@ void AutofillAction::FillFormWithData(const std::string& guid, ...@@ -129,10 +199,15 @@ void AutofillAction::FillFormWithData(const std::string& guid,
ActionDelegate* delegate) { ActionDelegate* delegate) {
DCHECK(!selectors_.empty()); DCHECK(!selectors_.empty());
if (is_autofill_card_) { if (is_autofill_card_) {
delegate->FillCardForm( autofill::CreditCard* card =
guid, selectors_, delegate->GetPersonalDataManager()->GetCreditCardByGUID(guid);
base::BindOnce(&AutofillAction::OnFormFilled, DCHECK(card);
weak_ptr_factory_.GetWeakPtr(), guid, delegate)); // TODO(crbug.com/806868): Consider refactoring SelfDeleteFullCardRequester
// so as to unit test it.
(new SelfDeleteFullCardRequester())
->GetFullCard(delegate->GetWebContents(), card,
base::BindOnce(&AutofillAction::OnGetFullCard,
weak_ptr_factory_.GetWeakPtr(), delegate));
return; return;
} }
...@@ -142,6 +217,24 @@ void AutofillAction::FillFormWithData(const std::string& guid, ...@@ -142,6 +217,24 @@ void AutofillAction::FillFormWithData(const std::string& guid,
weak_ptr_factory_.GetWeakPtr(), guid, delegate)); weak_ptr_factory_.GetWeakPtr(), guid, delegate));
} }
void AutofillAction::OnGetFullCard(ActionDelegate* delegate,
std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc) {
if (!card) {
// TODO(crbug.com/806868): The failure might because of cancel, then ask to
// choose a card again.
UpdateProcessedAction(false);
std::move(process_action_callback_).Run(std::move(processed_action_proto_));
return;
}
std::string guid = card->guid();
delegate->FillCardForm(
std::move(card), cvc, selectors_,
base::BindOnce(&AutofillAction::OnFormFilled,
weak_ptr_factory_.GetWeakPtr(), guid, delegate));
}
void AutofillAction::OnFormFilled(const std::string& guid, void AutofillAction::OnFormFilled(const std::string& guid,
ActionDelegate* delegate, ActionDelegate* delegate,
bool successful) { bool successful) {
...@@ -208,7 +301,8 @@ void AutofillAction::OnGetRequiredFieldValue( ...@@ -208,7 +301,8 @@ void AutofillAction::OnGetRequiredFieldValue(
} }
} }
const autofill::AutofillProfile* profile = delegate->GetAutofillProfile(guid); const autofill::AutofillProfile* profile =
delegate->GetPersonalDataManager()->GetProfileByGUID(guid);
DCHECK(profile); DCHECK(profile);
// We process all fields with an empty value in order to perform the fallback // We process all fields with an empty value in order to perform the fallback
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
namespace autofill { namespace autofill {
class AutofillProfile; class AutofillProfile;
class CreditCard;
} }
namespace autofill_assistant { namespace autofill_assistant {
...@@ -40,6 +41,11 @@ class AutofillAction : public Action { ...@@ -40,6 +41,11 @@ class AutofillAction : public Action {
// or not through |callback|. // or not through |callback|.
void FillFormWithData(const std::string& guid, ActionDelegate* delegate); void FillFormWithData(const std::string& guid, ActionDelegate* delegate);
// Called after getting full credit card with its cvc.
void OnGetFullCard(ActionDelegate* delegate,
std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc);
// Called when the form has been filled. // Called when the form has been filled.
void OnFormFilled(const std::string& guid, void OnFormFilled(const std::string& guid,
ActionDelegate* delegate, ActionDelegate* delegate,
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/guid.h" #include "base/guid.h"
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h" #include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill_assistant/browser/actions/mock_action_delegate.h" #include "components/autofill_assistant/browser/actions/mock_action_delegate.h"
#include "components/autofill_assistant/browser/mock_client_memory.h" #include "components/autofill_assistant/browser/mock_client_memory.h"
#include "components/autofill_assistant/browser/mock_run_once_callback.h" #include "components/autofill_assistant/browser/mock_run_once_callback.h"
...@@ -23,6 +24,41 @@ using ::testing::InSequence; ...@@ -23,6 +24,41 @@ using ::testing::InSequence;
using ::testing::Return; using ::testing::Return;
using ::testing::StrNe; using ::testing::StrNe;
class MockPersonalDataManager : public autofill::PersonalDataManager {
public:
MockPersonalDataManager() : PersonalDataManager("en-US") {}
~MockPersonalDataManager() override{};
// PersonalDataManager:
std::string SaveImportedProfile(
const autofill::AutofillProfile& profile) override {
std::vector<autofill::AutofillProfile> profiles;
std::string merged_guid =
MergeProfile(profile, &profiles_, "en-US", &profiles);
if (merged_guid == profile.guid())
profiles_.push_back(std::make_unique<autofill::AutofillProfile>(profile));
return merged_guid;
}
autofill::AutofillProfile* GetProfileByGUID(
const std::string& guid) override {
autofill::AutofillProfile* result = nullptr;
for (const auto& profile : profiles_) {
if (profile->guid() != guid)
continue;
result = profile.get();
break;
}
return result;
}
private:
std::vector<std::unique_ptr<autofill::AutofillProfile>> profiles_;
DISALLOW_COPY_AND_ASSIGN(MockPersonalDataManager);
};
// A callback that expects to be called immediately. // A callback that expects to be called immediately.
// //
// This relies on mocked methods calling their callbacks immediately (which is // This relies on mocked methods calling their callbacks immediately (which is
...@@ -59,12 +95,14 @@ class AutofillActionTest : public testing::Test { ...@@ -59,12 +95,14 @@ class AutofillActionTest : public testing::Test {
autofill::test::kEmptyOrigin); autofill::test::kEmptyOrigin);
autofill::test::SetProfileInfo(&profile, kFirstName, "", kLastName, kEmail, autofill::test::SetProfileInfo(&profile, kFirstName, "", kLastName, kEmail,
"", "", "", "", "", "", "", ""); "", "", "", "", "", "", "", "");
autofill_profile_ = std::move(profile); autofill_profile_guid_ = profile.guid();
personal_data_manager_ = std::make_unique<MockPersonalDataManager>();
personal_data_manager_->SaveImportedProfile(profile);
ON_CALL(mock_action_delegate_, GetClientMemory) ON_CALL(mock_action_delegate_, GetClientMemory)
.WillByDefault(Return(&mock_client_memory_)); .WillByDefault(Return(&mock_client_memory_));
ON_CALL(mock_action_delegate_, GetAutofillProfile(autofill_profile_.guid())) ON_CALL(mock_action_delegate_, GetPersonalDataManager)
.WillByDefault(Return(&autofill_profile_)); .WillByDefault(Return(personal_data_manager_.get()));
} }
protected: protected:
...@@ -102,7 +140,8 @@ class AutofillActionTest : public testing::Test { ...@@ -102,7 +140,8 @@ class AutofillActionTest : public testing::Test {
MockActionDelegate mock_action_delegate_; MockActionDelegate mock_action_delegate_;
MockClientMemory mock_client_memory_; MockClientMemory mock_client_memory_;
autofill::AutofillProfile autofill_profile_; std::string autofill_profile_guid_;
std::unique_ptr<autofill::PersonalDataManager> personal_data_manager_;
}; };
TEST_F(AutofillActionTest, FillManually) { TEST_F(AutofillActionTest, FillManually) {
...@@ -128,41 +167,6 @@ TEST_F(AutofillActionTest, FillManually) { ...@@ -128,41 +167,6 @@ TEST_F(AutofillActionTest, FillManually) {
EXPECT_FALSE(ProcessAction(action_proto)); EXPECT_FALSE(ProcessAction(action_proto));
} }
TEST_F(AutofillActionTest, FillCardFormFails) {
InSequence seq;
ActionProto action_proto = CreateUseCardAction();
// Return a fake selected card.
EXPECT_CALL(mock_client_memory_, selected_card())
.WillOnce(Return(autofill_profile_.guid()));
// Autofill fails.
EXPECT_CALL(mock_action_delegate_,
OnFillCardForm(autofill_profile_.guid(), _, _))
.WillOnce(RunOnceCallback<2>(false));
EXPECT_FALSE(ProcessAction(action_proto));
}
TEST_F(AutofillActionTest, FillCardFormSucceeds) {
InSequence seq;
ActionProto action_proto = CreateUseCardAction();
// Return a fake selected card.
EXPECT_CALL(mock_client_memory_, selected_card())
.WillOnce(Return(autofill_profile_.guid()));
// Autofill succeeds.
EXPECT_CALL(
mock_action_delegate_,
OnFillCardForm(autofill_profile_.guid(), ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true));
EXPECT_TRUE(ProcessAction(action_proto));
}
TEST_F(AutofillActionTest, ValidationSucceeds) { TEST_F(AutofillActionTest, ValidationSucceeds) {
InSequence seq; InSequence seq;
...@@ -178,12 +182,12 @@ TEST_F(AutofillActionTest, ValidationSucceeds) { ...@@ -178,12 +182,12 @@ TEST_F(AutofillActionTest, ValidationSucceeds) {
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_.guid())); .WillOnce(Return(autofill_profile_guid_));
// Autofill succeeds. // Autofill succeeds.
EXPECT_CALL(mock_action_delegate_, EXPECT_CALL(
OnFillAddressForm(autofill_profile_.guid(), mock_action_delegate_,
ElementsAre(kFakeSelector), _)) OnFillAddressForm(autofill_profile_guid_, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
// Validation succeeds. // Validation succeeds.
...@@ -213,12 +217,12 @@ TEST_F(AutofillActionTest, FallbackFails) { ...@@ -213,12 +217,12 @@ TEST_F(AutofillActionTest, FallbackFails) {
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_.guid())); .WillOnce(Return(autofill_profile_guid_));
// Autofill succeeds. // Autofill succeeds.
EXPECT_CALL(mock_action_delegate_, EXPECT_CALL(
OnFillAddressForm(autofill_profile_.guid(), mock_action_delegate_,
ElementsAre(kFakeSelector), _)) OnFillAddressForm(autofill_profile_guid_, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
// Validation fails when getting FIRST_NAME. // Validation fails when getting FIRST_NAME.
...@@ -254,12 +258,12 @@ TEST_F(AutofillActionTest, FallbackSucceeds) { ...@@ -254,12 +258,12 @@ TEST_F(AutofillActionTest, FallbackSucceeds) {
// Return a fake selected address. // Return a fake selected address.
EXPECT_CALL(mock_client_memory_, selected_address(kAddressName)) EXPECT_CALL(mock_client_memory_, selected_address(kAddressName))
.WillOnce(Return(autofill_profile_.guid())); .WillOnce(Return(autofill_profile_guid_));
// Autofill succeeds. // Autofill succeeds.
EXPECT_CALL(mock_action_delegate_, EXPECT_CALL(
OnFillAddressForm(autofill_profile_.guid(), mock_action_delegate_,
ElementsAre(kFakeSelector), _)) OnFillAddressForm(autofill_profile_guid_, ElementsAre(kFakeSelector), _))
.WillOnce(RunOnceCallback<2>(true)); .WillOnce(RunOnceCallback<2>(true));
// Validation fails when getting FIRST_NAME. // Validation fails when getting FIRST_NAME.
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill_assistant/browser/actions/action_delegate.h" #include "components/autofill_assistant/browser/actions/action_delegate.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
...@@ -54,10 +55,11 @@ class MockActionDelegate : public ActionDelegate { ...@@ -54,10 +55,11 @@ class MockActionDelegate : public ActionDelegate {
MOCK_METHOD1(OnChooseCard, MOCK_METHOD1(OnChooseCard,
void(base::OnceCallback<void(const std::string&)>& callback)); void(base::OnceCallback<void(const std::string&)>& callback));
void FillCardForm(const std::string& guid, void FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) override { base::OnceCallback<void(bool)> callback) override {
OnFillCardForm(guid, selectors, callback); OnFillCardForm(card->guid(), selectors, callback);
} }
MOCK_METHOD3(OnFillCardForm, MOCK_METHOD3(OnFillCardForm,
...@@ -92,8 +94,6 @@ class MockActionDelegate : public ActionDelegate { ...@@ -92,8 +94,6 @@ class MockActionDelegate : public ActionDelegate {
void(const std::vector<std::string>& selectors, void(const std::vector<std::string>& selectors,
const std::string& value, const std::string& value,
base::OnceCallback<void(bool)>& callback)); base::OnceCallback<void(bool)>& callback));
MOCK_METHOD1(GetAutofillProfile,
const autofill::AutofillProfile*(const std::string& guid));
MOCK_METHOD3(BuildNodeTree, MOCK_METHOD3(BuildNodeTree,
void(const std::vector<std::string>& selectors, void(const std::vector<std::string>& selectors,
NodeProto* node_tree_out, NodeProto* node_tree_out,
...@@ -102,6 +102,8 @@ class MockActionDelegate : public ActionDelegate { ...@@ -102,6 +102,8 @@ class MockActionDelegate : public ActionDelegate {
MOCK_METHOD0(Shutdown, void()); MOCK_METHOD0(Shutdown, void());
MOCK_METHOD0(Restart, void()); MOCK_METHOD0(Restart, void());
MOCK_METHOD0(GetClientMemory, ClientMemory*()); MOCK_METHOD0(GetClientMemory, ClientMemory*());
MOCK_METHOD0(GetPersonalDataManager, autofill::PersonalDataManager*());
MOCK_METHOD0(GetWebContents, content::WebContents*());
}; };
} // namespace autofill_assistant } // namespace autofill_assistant
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#include <string> #include <string>
namespace autofill {
class PersonalDataManager;
} // namespace autofill
namespace autofill_assistant { namespace autofill_assistant {
class UiController; class UiController;
...@@ -19,6 +23,9 @@ class Client { ...@@ -19,6 +23,9 @@ class Client {
// Returns the API key to be used for requests to the backend. // Returns the API key to be used for requests to the backend.
virtual std::string GetApiKey() = 0; virtual std::string GetApiKey() = 0;
// Returns the current active personal data manager.
virtual autofill::PersonalDataManager* GetPersonalDataManager() = 0;
// Returns the server URL to be used for requests to the backend. // Returns the server URL to be used for requests to the backend.
virtual std::string GetServerUrl() = 0; virtual std::string GetServerUrl() = 0;
......
...@@ -49,6 +49,14 @@ const std::map<std::string, std::string>& Controller::GetParameters() { ...@@ -49,6 +49,14 @@ const std::map<std::string, std::string>& Controller::GetParameters() {
return *parameters_; return *parameters_;
} }
autofill::PersonalDataManager* Controller::GetPersonalDataManager() {
return client_->GetPersonalDataManager();
}
content::WebContents* Controller::GetWebContents() {
return web_contents();
}
Controller::Controller( Controller::Controller(
content::WebContents* web_contents, content::WebContents* web_contents,
std::unique_ptr<Client> client, std::unique_ptr<Client> client,
......
...@@ -48,6 +48,8 @@ class Controller : public ScriptExecutorDelegate, ...@@ -48,6 +48,8 @@ class Controller : public ScriptExecutorDelegate,
WebController* GetWebController() override; WebController* GetWebController() override;
ClientMemory* GetClientMemory() override; ClientMemory* GetClientMemory() override;
const std::map<std::string, std::string>& GetParameters() override; const std::map<std::string, std::string>& GetParameters() override;
autofill::PersonalDataManager* GetPersonalDataManager() override;
content::WebContents* GetWebContents() override;
private: private:
friend ControllerTest; friend ControllerTest;
......
...@@ -40,6 +40,9 @@ class FakeClient : public Client { ...@@ -40,6 +40,9 @@ class FakeClient : public Client {
// Implements Client // Implements Client
std::string GetApiKey() override { return ""; } std::string GetApiKey() override { return ""; }
autofill::PersonalDataManager* GetPersonalDataManager() override {
return nullptr;
}
std::string GetServerUrl() override { return ""; } std::string GetServerUrl() override { return ""; }
UiController* GetUiController() override { return ui_controller_.get(); } UiController* GetUiController() override { return ui_controller_.get(); }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/credit_card.h"
#include "components/autofill_assistant/browser/protocol_utils.h" #include "components/autofill_assistant/browser/protocol_utils.h"
#include "components/autofill_assistant/browser/service.h" #include "components/autofill_assistant/browser/service.h"
#include "components/autofill_assistant/browser/ui_controller.h" #include "components/autofill_assistant/browser/ui_controller.h"
...@@ -70,10 +70,11 @@ void ScriptExecutor::ChooseCard( ...@@ -70,10 +70,11 @@ void ScriptExecutor::ChooseCard(
delegate_->GetUiController()->ChooseCard(std::move(callback)); delegate_->GetUiController()->ChooseCard(std::move(callback));
} }
void ScriptExecutor::FillCardForm(const std::string& guid, void ScriptExecutor::FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
delegate_->GetWebController()->FillCardForm(guid, selectors, delegate_->GetWebController()->FillCardForm(std::move(card), cvc, selectors,
std::move(callback)); std::move(callback));
} }
...@@ -102,12 +103,6 @@ void ScriptExecutor::SetFieldValue(const std::vector<std::string>& selectors, ...@@ -102,12 +103,6 @@ void ScriptExecutor::SetFieldValue(const std::vector<std::string>& selectors,
std::move(callback)); std::move(callback));
} }
const autofill::AutofillProfile* ScriptExecutor::GetAutofillProfile(
const std::string& guid) {
// TODO(crbug.com/806868): Implement GetAutofillProfile.
return nullptr;
}
void ScriptExecutor::BuildNodeTree(const std::vector<std::string>& selectors, void ScriptExecutor::BuildNodeTree(const std::vector<std::string>& selectors,
NodeProto* node_tree_out, NodeProto* node_tree_out,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
...@@ -131,6 +126,14 @@ ClientMemory* ScriptExecutor::GetClientMemory() { ...@@ -131,6 +126,14 @@ ClientMemory* ScriptExecutor::GetClientMemory() {
return delegate_->GetClientMemory(); return delegate_->GetClientMemory();
} }
autofill::PersonalDataManager* ScriptExecutor::GetPersonalDataManager() {
return delegate_->GetPersonalDataManager();
}
content::WebContents* ScriptExecutor::GetWebContents() {
return delegate_->GetWebContents();
}
void ScriptExecutor::OnGetActions(bool result, const std::string& response) { void ScriptExecutor::OnGetActions(bool result, const std::string& response) {
if (!result) { if (!result) {
RunCallback(false); RunCallback(false);
......
...@@ -61,7 +61,8 @@ class ScriptExecutor : public ActionDelegate { ...@@ -61,7 +61,8 @@ class ScriptExecutor : public ActionDelegate {
base::OnceCallback<void(bool)> callback) override; base::OnceCallback<void(bool)> callback) override;
void ChooseCard( void ChooseCard(
base::OnceCallback<void(const std::string&)> callback) override; base::OnceCallback<void(const std::string&)> callback) override;
void FillCardForm(const std::string& guid, void FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) override; base::OnceCallback<void(bool)> callback) override;
void SelectOption(const std::vector<std::string>& selectors, void SelectOption(const std::vector<std::string>& selectors,
...@@ -75,8 +76,6 @@ class ScriptExecutor : public ActionDelegate { ...@@ -75,8 +76,6 @@ class ScriptExecutor : public ActionDelegate {
void SetFieldValue(const std::vector<std::string>& selectors, void SetFieldValue(const std::vector<std::string>& selectors,
const std::string& value, const std::string& value,
base::OnceCallback<void(bool)> callback) override; base::OnceCallback<void(bool)> callback) override;
const autofill::AutofillProfile* GetAutofillProfile(
const std::string& guid) override;
void BuildNodeTree(const std::vector<std::string>& selectors, void BuildNodeTree(const std::vector<std::string>& selectors,
NodeProto* node_tree_out, NodeProto* node_tree_out,
base::OnceCallback<void(bool)> callback) override; base::OnceCallback<void(bool)> callback) override;
...@@ -84,6 +83,8 @@ class ScriptExecutor : public ActionDelegate { ...@@ -84,6 +83,8 @@ class ScriptExecutor : public ActionDelegate {
void Shutdown() override; void Shutdown() override;
void Restart() override; void Restart() override;
ClientMemory* GetClientMemory() override; ClientMemory* GetClientMemory() override;
autofill::PersonalDataManager* GetPersonalDataManager() override;
content::WebContents* GetWebContents() override;
private: private:
void OnGetActions(bool result, const std::string& response); void OnGetActions(bool result, const std::string& response);
......
...@@ -8,6 +8,14 @@ ...@@ -8,6 +8,14 @@
#include <map> #include <map>
#include <string> #include <string>
namespace autofill {
class PersonalDataManager;
} // namespace autofill
namespace content {
class WebContents;
} // namespace content
namespace autofill_assistant { namespace autofill_assistant {
class Service; class Service;
...@@ -27,6 +35,10 @@ class ScriptExecutorDelegate { ...@@ -27,6 +35,10 @@ class ScriptExecutorDelegate {
virtual const std::map<std::string, std::string>& GetParameters() = 0; virtual const std::map<std::string, std::string>& GetParameters() = 0;
virtual autofill::PersonalDataManager* GetPersonalDataManager() = 0;
virtual content::WebContents* GetWebContents() = 0;
protected: protected:
virtual ~ScriptExecutorDelegate() {} virtual ~ScriptExecutorDelegate() {}
}; };
......
...@@ -59,6 +59,12 @@ class ScriptExecutorTest : public testing::Test, public ScriptExecutorDelegate { ...@@ -59,6 +59,12 @@ class ScriptExecutorTest : public testing::Test, public ScriptExecutorDelegate {
return parameters_; return parameters_;
} }
autofill::PersonalDataManager* GetPersonalDataManager() override {
return nullptr;
}
content::WebContents* GetWebContents() override { return nullptr; }
std::string Serialize(const google::protobuf::MessageLite& message) { std::string Serialize(const google::protobuf::MessageLite& message) {
std::string output; std::string output;
message.SerializeToString(&output); message.SerializeToString(&output);
......
...@@ -61,6 +61,12 @@ class ScriptTrackerTest : public testing::Test, ...@@ -61,6 +61,12 @@ class ScriptTrackerTest : public testing::Test,
return parameters_; return parameters_;
} }
autofill::PersonalDataManager* GetPersonalDataManager() override {
return nullptr;
}
content::WebContents* GetWebContents() override { return nullptr; }
// Overrides ScriptTracker::Listener // Overrides ScriptTracker::Listener
void OnRunnableScriptsChanged( void OnRunnableScriptsChanged(
const std::vector<ScriptHandle>& runnable_scripts) override { const std::vector<ScriptHandle>& runnable_scripts) override {
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "base/logging.h" #include "base/logging.h"
#include "components/autofill/content/browser/content_autofill_driver.h" #include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/autofill_manager.h" #include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/form_data.h" #include "components/autofill/core/common/form_data.h"
#include "components/autofill_assistant/browser/service.pb.h" #include "components/autofill_assistant/browser/service.pb.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
...@@ -81,6 +83,10 @@ WebController::WebController(content::WebContents* web_contents, ...@@ -81,6 +83,10 @@ WebController::WebController(content::WebContents* web_contents,
WebController::~WebController() {} WebController::~WebController() {}
WebController::FillFormInputData::FillFormInputData() {}
WebController::FillFormInputData::~FillFormInputData() {}
const GURL& WebController::GetUrl() { const GURL& WebController::GetUrl() {
return web_contents_->GetLastCommittedURL(); return web_contents_->GetLastCommittedURL();
} }
...@@ -450,14 +456,17 @@ void WebController::OnFocusElement( ...@@ -450,14 +456,17 @@ void WebController::OnFocusElement(
void WebController::FillAddressForm(const std::string& guid, void WebController::FillAddressForm(const std::string& guid,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
auto data_to_autofill = std::make_unique<FillFormInputData>();
data_to_autofill->autofill_data_guid = guid;
FindElement(selectors, FindElement(selectors,
base::BindOnce(&WebController::OnFindElementForFillingForm, base::BindOnce(&WebController::OnFindElementForFillingForm,
weak_ptr_factory_.GetWeakPtr(), guid, selectors, weak_ptr_factory_.GetWeakPtr(),
std::move(data_to_autofill), selectors,
std::move(callback))); std::move(callback)));
} }
void WebController::OnFindElementForFillingForm( void WebController::OnFindElementForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
std::unique_ptr<FindElementResult> element_result) { std::unique_ptr<FindElementResult> element_result) {
...@@ -469,13 +478,13 @@ void WebController::OnFindElementForFillingForm( ...@@ -469,13 +478,13 @@ void WebController::OnFindElementForFillingForm(
ClickObject(element_result->object_id, ClickObject(element_result->object_id,
base::BindOnce(&WebController::OnClickObjectForFillingForm, base::BindOnce(&WebController::OnClickObjectForFillingForm,
weak_ptr_factory_.GetWeakPtr(), autofill_data_guid, weak_ptr_factory_.GetWeakPtr(),
selectors, std::move(callback), std::move(data_to_autofill), selectors,
std::move(element_result))); std::move(callback), std::move(element_result)));
} }
void WebController::OnClickObjectForFillingForm( void WebController::OnClickObjectForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
std::unique_ptr<FindElementResult> element_result, std::unique_ptr<FindElementResult> element_result,
...@@ -496,13 +505,13 @@ void WebController::OnClickObjectForFillingForm( ...@@ -496,13 +505,13 @@ void WebController::OnClickObjectForFillingForm(
driver->GetAutofillAgent()->GetElementFormAndFieldData( driver->GetAutofillAgent()->GetElementFormAndFieldData(
element_selectors, element_selectors,
base::BindOnce(&WebController::OnGetFormAndFieldDataForFillingForm, base::BindOnce(&WebController::OnGetFormAndFieldDataForFillingForm,
weak_ptr_factory_.GetWeakPtr(), autofill_data_guid, weak_ptr_factory_.GetWeakPtr(),
std::move(callback), std::move(data_to_autofill), std::move(callback),
element_result->container_frame_host)); element_result->container_frame_host));
} }
void WebController::OnGetFormAndFieldDataForFillingForm( void WebController::OnGetFormAndFieldDataForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
content::RenderFrameHost* container_frame_host, content::RenderFrameHost* container_frame_host,
const autofill::FormData& form_data, const autofill::FormData& form_data,
...@@ -520,16 +529,31 @@ void WebController::OnGetFormAndFieldDataForFillingForm( ...@@ -520,16 +529,31 @@ void WebController::OnGetFormAndFieldDataForFillingForm(
OnResult(false, std::move(callback)); OnResult(false, std::move(callback));
return; return;
} }
driver->autofill_manager()->FillProfileForm(autofill_data_guid, form_data,
form_field); if (data_to_autofill->card) {
driver->autofill_manager()->FillCreditCardForm(
autofill::kNoQueryId, form_data, form_field, *data_to_autofill->card,
data_to_autofill->cvc);
} else {
driver->autofill_manager()->FillProfileForm(
data_to_autofill->autofill_data_guid, form_data, form_field);
}
OnResult(true, std::move(callback)); OnResult(true, std::move(callback));
} }
void WebController::FillCardForm(const std::string& guid, void WebController::FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
// TODO(crbug.com/806868): Implement fill card form operation. auto data_to_autofill = std::make_unique<FillFormInputData>();
std::move(callback).Run(true); data_to_autofill->card = std::move(card);
data_to_autofill->cvc = cvc;
FindElement(selectors,
base::BindOnce(&WebController::OnFindElementForFillingForm,
weak_ptr_factory_.GetWeakPtr(),
std::move(data_to_autofill), selectors,
std::move(callback)));
} }
void WebController::SelectOption(const std::vector<std::string>& selectors, void WebController::SelectOption(const std::vector<std::string>& selectors,
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
#include "components/autofill_assistant/browser/devtools/devtools/domains/types_runtime.h" #include "components/autofill_assistant/browser/devtools/devtools/domains/types_runtime.h"
#include "components/autofill_assistant/browser/devtools/devtools_client.h" #include "components/autofill_assistant/browser/devtools/devtools_client.h"
namespace autofill {
class CreditCard;
} // namespace autofill
namespace content { namespace content {
class WebContents; class WebContents;
class RenderFrameHost; class RenderFrameHost;
...@@ -66,9 +70,10 @@ class WebController { ...@@ -66,9 +70,10 @@ class WebController {
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback); base::OnceCallback<void(bool)> callback);
// Fill the card form given by |selectors| with the given card |guid| in // Fill the card form given by |selectors| with the given |card| and its
// personal data manager. // |cvc|.
virtual void FillCardForm(const std::string& guid, virtual void FillCardForm(std::unique_ptr<autofill::CreditCard> card,
const base::string16& cvc,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback); base::OnceCallback<void(bool)> callback);
...@@ -122,6 +127,18 @@ class WebController { ...@@ -122,6 +127,18 @@ class WebController {
using FindElementCallback = using FindElementCallback =
base::OnceCallback<void(std::unique_ptr<FindElementResult>)>; base::OnceCallback<void(std::unique_ptr<FindElementResult>)>;
struct FillFormInputData {
FillFormInputData();
~FillFormInputData();
// Data for filling address form.
std::string autofill_data_guid;
// Data for filling card form.
std::unique_ptr<autofill::CreditCard> card;
base::string16 cvc;
};
void OnFindElementForClick(base::OnceCallback<void(bool)> callback, void OnFindElementForClick(base::OnceCallback<void(bool)> callback,
std::unique_ptr<FindElementResult> result); std::unique_ptr<FindElementResult> result);
void ClickObject(const std::string& object_id, void ClickObject(const std::string& object_id,
...@@ -178,18 +195,18 @@ class WebController { ...@@ -178,18 +195,18 @@ class WebController {
void OnResult(const std::string& result, void OnResult(const std::string& result,
base::OnceCallback<void(const std::string&)> callback); base::OnceCallback<void(const std::string&)> callback);
void OnFindElementForFillingForm( void OnFindElementForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
std::unique_ptr<FindElementResult> element_result); std::unique_ptr<FindElementResult> element_result);
void OnClickObjectForFillingForm( void OnClickObjectForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
const std::vector<std::string>& selectors, const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
std::unique_ptr<FindElementResult> element_result, std::unique_ptr<FindElementResult> element_result,
bool click_result); bool click_result);
void OnGetFormAndFieldDataForFillingForm( void OnGetFormAndFieldDataForFillingForm(
const std::string& autofill_data_guid, std::unique_ptr<FillFormInputData> data_to_autofill,
base::OnceCallback<void(bool)> callback, base::OnceCallback<void(bool)> callback,
content::RenderFrameHost* container_frame_host, content::RenderFrameHost* container_frame_host,
const autofill::FormData& form_data, const autofill::FormData& form_data,
......
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