Commit 6771a557 authored by sandromaggi's avatar sandromaggi Committed by Commit Bot

[Autofill Assistant] Remove |OnFindElementForX| functions

These can be abstracted and unified.

Bug: b/168107066
Change-Id: I0983ee22dd5980d09adb5a9e162f1c6a9eaa5f5e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2404342
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#806134}
parent d325dff7
......@@ -22,6 +22,25 @@ void RetainElementAndExecuteCallback(
std::move(callback).Run(status);
}
void OnFindElement(
base::OnceCallback<void(const ElementFinder::Result&,
base::OnceCallback<void(const ClientStatus&)>)>
perform,
base::OnceCallback<void(const ClientStatus&)> done,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element_result) {
if (!element_status.ok()) {
VLOG(1) << __func__ << " Failed to find element.";
std::move(done).Run(element_status);
return;
}
std::move(perform).Run(
*element_result,
base::BindOnce(&RetainElementAndExecuteCallback,
std::move(element_result), std::move(done)));
}
void OnScrollIntoViewForClickOrTap(
ActionDelegate* delegate,
ClickType click_type,
......@@ -65,23 +84,6 @@ void PerformClickOrTap(ActionDelegate* delegate,
delegate, click_type, element, std::move(callback)));
}
void OnFindElementForClickOrTap(
ActionDelegate* delegate,
ClickType click_type,
base::OnceCallback<void(const ClientStatus&)> callback,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element) {
if (!element_status.ok()) {
VLOG(1) << __func__ << " Failed to find the element to click or tap.";
std::move(callback).Run(element_status);
return;
}
PerformClickOrTap(delegate, click_type, *element,
base::BindOnce(&RetainElementAndExecuteCallback,
std::move(element), std::move(callback)));
}
void OnClickOrTapForSendKeyboardInput(
ActionDelegate* delegate,
const std::vector<UChar32> codepoints,
......@@ -110,80 +112,45 @@ void PerformSendKeyboardInput(
delay_in_millis, element, std::move(callback)));
}
void OnFindElementForSendKeyboardInput(
ActionDelegate* delegate,
const std::vector<UChar32> codepoints,
int delay_in_millis,
base::OnceCallback<void(const ClientStatus&)> callback,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element) {
if (!element_status.ok()) {
VLOG(1) << __func__
<< " Failed to find the element to send keyboad input to.";
std::move(callback).Run(element_status);
return;
}
PerformSendKeyboardInput(
delegate, codepoints, delay_in_millis, *element,
base::BindOnce(&RetainElementAndExecuteCallback, std::move(element),
std::move(callback)));
}
void OnFindElementForSetFieldValue(
void PerformSetFieldValue(
ActionDelegate* delegate,
const std::string& value,
KeyboardValueFillStrategy fill_strategy,
int key_press_delay_in_millisecond,
base::OnceCallback<void(const ClientStatus&)> callback,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element) {
if (!element_status.ok()) {
VLOG(1) << __func__ << " Failed to find element to set value.";
std::move(callback).Run(element_status);
return;
}
const ElementFinder::Result& element,
base::OnceCallback<void(const ClientStatus&)> callback) {
// TODO(b/158153191): This should reuse the callback chains in the util
// instead of relying on the |WebController| to properly implement it.
// This requires to extract more methods and some internal logic of
// |SetFieldValue|.
delegate->SetFieldValue(
*element, value, fill_strategy, key_press_delay_in_millisecond,
base::BindOnce(&RetainElementAndExecuteCallback, std::move(element),
std::move(callback)));
delegate->SetFieldValue(element, value, fill_strategy,
key_press_delay_in_millisecond, std::move(callback));
}
void OnFindElementForSelectOption(
ActionDelegate* delegate,
const std::string& value,
DropdownSelectStrategy select_strategy,
base::OnceCallback<void(const ClientStatus&)> callback,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element) {
if (!element_status.ok()) {
VLOG(1) << __func__ << " Failed to find element to select option.";
std::move(callback).Run(element_status);
return;
}
} // namespace
delegate->SelectOption(
*element, value, select_strategy,
base::BindOnce(&RetainElementAndExecuteCallback, std::move(element),
std::move(callback)));
void FindElementAndPerform(
ActionDelegate* delegate,
const Selector& selector,
base::OnceCallback<void(const ElementFinder::Result&,
base::OnceCallback<void(const ClientStatus&)>)>
perform,
base::OnceCallback<void(const ClientStatus&)> done) {
DCHECK(!selector.empty());
VLOG(3) << __func__ << " " << selector;
delegate->FindElement(
selector,
base::BindOnce(&OnFindElement, std::move(perform), std::move(done)));
}
} // namespace
void ClickOrTapElement(ActionDelegate* delegate,
const Selector& selector,
ClickType click_type,
base::OnceCallback<void(const ClientStatus&)> callback) {
VLOG(3) << __func__ << " " << selector;
DCHECK(!selector.empty());
delegate->FindElement(selector,
base::BindOnce(&OnFindElementForClickOrTap, delegate,
click_type, std::move(callback)));
FindElementAndPerform(
delegate, selector,
base::BindOnce(&PerformClickOrTap, delegate, click_type),
std::move(callback));
}
void SendKeyboardInput(ActionDelegate* delegate,
......@@ -191,12 +158,10 @@ void SendKeyboardInput(ActionDelegate* delegate,
const std::vector<UChar32> codepoints,
int delay_in_millis,
base::OnceCallback<void(const ClientStatus&)> callback) {
VLOG(3) << __func__ << " " << selector;
DCHECK(!selector.empty());
delegate->FindElement(
selector,
base::BindOnce(&OnFindElementForSendKeyboardInput, delegate, codepoints,
delay_in_millis, std::move(callback)));
FindElementAndPerform(delegate, selector,
base::BindOnce(&PerformSendKeyboardInput, delegate,
codepoints, delay_in_millis),
std::move(callback));
}
void SetFieldValue(ActionDelegate* delegate,
......@@ -205,24 +170,11 @@ void SetFieldValue(ActionDelegate* delegate,
KeyboardValueFillStrategy fill_strategy,
int key_press_delay_in_millisecond,
base::OnceCallback<void(const ClientStatus&)> callback) {
VLOG(3) << __func__ << " " << selector;
DCHECK(!selector.empty());
delegate->FindElement(
selector, base::BindOnce(&OnFindElementForSetFieldValue, delegate, value,
fill_strategy, key_press_delay_in_millisecond,
std::move(callback)));
}
void SelectOption(ActionDelegate* delegate,
const Selector& selector,
const std::string& value,
DropdownSelectStrategy select_strategy,
base::OnceCallback<void(const ClientStatus&)> callback) {
VLOG(3) << __func__ << " " << selector;
DCHECK(!selector.empty());
delegate->FindElement(
selector, base::BindOnce(&OnFindElementForSelectOption, delegate, value,
select_strategy, std::move(callback)));
FindElementAndPerform(
delegate, selector,
base::BindOnce(&PerformSetFieldValue, delegate, value, fill_strategy,
key_press_delay_in_millisecond),
std::move(callback));
}
} // namespace ActionDelegateUtil
......
......@@ -13,6 +13,18 @@
namespace autofill_assistant {
namespace ActionDelegateUtil {
// Finds the element given by the selector. If the resolution fails, it
// immediately executes the |done| callback. If the resolution succeeds, it
// executes the |perform| callback with the element and the |done| callback as
// arguments, while retaining the element.
void FindElementAndPerform(
ActionDelegate* delegate,
const Selector& selector,
base::OnceCallback<void(const ElementFinder::Result&,
base::OnceCallback<void(const ClientStatus&)>)>
perform,
base::OnceCallback<void(const ClientStatus&)> done);
void ClickOrTapElement(ActionDelegate* delegate,
const Selector& selector,
ClickType click_type,
......@@ -31,12 +43,6 @@ void SetFieldValue(ActionDelegate* delegate,
int key_press_delay_in_millisecond,
base::OnceCallback<void(const ClientStatus&)> callback);
void SelectOption(ActionDelegate* delegate,
const Selector& selector,
const std::string& value,
DropdownSelectStrategy select_strategy,
base::OnceCallback<void(const ClientStatus&)> callback);
} // namespace ActionDelegateUtil
} // namespace autofill_assistant
......
......@@ -13,6 +13,18 @@
#include "components/autofill_assistant/browser/client_status.h"
namespace autofill_assistant {
namespace {
void PerformSelectOption(
ActionDelegate* delegate,
const std::string& value,
DropdownSelectStrategy select_strategy,
const ElementFinder::Result& element,
base::OnceCallback<void(const ClientStatus&)> callback) {
delegate->SelectOption(element, value, select_strategy, std::move(callback));
}
} // namespace
SelectOptionAction::SelectOptionAction(ActionDelegate* delegate,
const ActionProto& proto)
......@@ -54,10 +66,12 @@ void SelectOptionAction::OnWaitForElement(ProcessActionCallback callback,
return;
}
ActionDelegateUtil::SelectOption(
delegate_, selector, proto_.select_option().selected_option(),
proto_.select_option().select_strategy(),
base::BindOnce(&::autofill_assistant::SelectOptionAction::OnSelectOption,
ActionDelegateUtil::FindElementAndPerform(
delegate_, selector,
base::BindOnce(&PerformSelectOption, delegate_,
proto_.select_option().selected_option(),
proto_.select_option().select_strategy()),
base::BindOnce(&SelectOptionAction::OnSelectOption,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
......
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