Commit 263ace9b authored by sandromaggi's avatar sandromaggi Committed by Commit Bot

[Autofill Assistant] Refactor |GetElementRect| to take Element

This refactors |WebController::GetElementRect| to take an
|ElementFinder::Result| instead of a |Selector|.

Bug: b/168107066
Change-Id: I0cbe7b12c95a92dac14c992017e41e3b0301f5e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2505650
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#822637}
parent 092d3da0
...@@ -72,34 +72,6 @@ void FindElementAndPerformImpl( ...@@ -72,34 +72,6 @@ void FindElementAndPerformImpl(
base::BindOnce(&OnFindElement, std::move(perform), std::move(done))); base::BindOnce(&OnFindElement, std::move(perform), std::move(done)));
} }
template <typename R>
void RetainElementAndExecuteGetCallback(
std::unique_ptr<ElementFinder::Result> element,
base::OnceCallback<void(const ClientStatus&, const R&)> callback,
const ClientStatus& status,
const R& result) {
DCHECK(element != nullptr);
std::move(callback).Run(status, result);
}
template <typename R>
void TakeElementAndGetPropertyImpl(
ElementActionGetCallback<R> perform_and_get,
base::OnceCallback<void(const ClientStatus&, const R&)> 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, R());
return;
}
std::move(perform_and_get)
.Run(*element_result,
base::BindOnce(&RetainElementAndExecuteGetCallback<R>,
std::move(element_result), std::move(done)));
}
} // namespace } // namespace
void PerformAll(std::unique_ptr<ElementActionVector> perform_actions, void PerformAll(std::unique_ptr<ElementActionVector> perform_actions,
...@@ -125,16 +97,6 @@ void TakeElementAndPerform(ElementActionCallback perform, ...@@ -125,16 +97,6 @@ void TakeElementAndPerform(ElementActionCallback perform,
std::move(element)); std::move(element));
} }
void TakeElementAndGetProperty(
ElementActionGetCallback<std::string> perform_and_get,
base::OnceCallback<void(const ClientStatus&, const std::string&)> done,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element) {
TakeElementAndGetPropertyImpl<std::string>(std::move(perform_and_get),
std::move(done), element_status,
std::move(element));
}
void ClickOrTapElement(const ActionDelegate* delegate, void ClickOrTapElement(const ActionDelegate* delegate,
const Selector& selector, const Selector& selector,
ClickType click_type, ClickType click_type,
......
...@@ -12,6 +12,19 @@ ...@@ -12,6 +12,19 @@
namespace autofill_assistant { namespace autofill_assistant {
namespace action_delegate_util { namespace action_delegate_util {
namespace {
template <typename R>
void RetainElementAndExecuteGetCallback(
std::unique_ptr<ElementFinder::Result> element,
base::OnceCallback<void(const ClientStatus&, const R&)> callback,
const ClientStatus& status,
const R& result) {
DCHECK(element != nullptr);
std::move(callback).Run(status, result);
}
} // namespace
using ElementActionCallback = using ElementActionCallback =
base::OnceCallback<void(const ElementFinder::Result&, base::OnceCallback<void(const ElementFinder::Result&,
...@@ -45,11 +58,23 @@ void TakeElementAndPerform(ElementActionCallback perform, ...@@ -45,11 +58,23 @@ void TakeElementAndPerform(ElementActionCallback perform,
// element and the |done| callback as arguments, while retaining the element. // element and the |done| callback as arguments, while retaining the element.
// If the initial status is not ok, execute the |done| callback with the default // If the initial status is not ok, execute the |done| callback with the default
// value immediately. // value immediately.
template <typename R>
void TakeElementAndGetProperty( void TakeElementAndGetProperty(
ElementActionGetCallback<std::string> perform_and_get, ElementActionGetCallback<R> perform_and_get,
base::OnceCallback<void(const ClientStatus&, const std::string&)> done, base::OnceCallback<void(const ClientStatus&, const R&)> done,
const ClientStatus& element_status, const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element); std::unique_ptr<ElementFinder::Result> element_result) {
if (!element_status.ok()) {
VLOG(1) << __func__ << " Failed to find element.";
std::move(done).Run(element_status, R());
return;
}
std::move(perform_and_get)
.Run(*element_result,
base::BindOnce(&RetainElementAndExecuteGetCallback<R>,
std::move(element_result), std::move(done)));
}
void PerformAll(std::unique_ptr<ElementActionVector> perform_actions, void PerformAll(std::unique_ptr<ElementActionVector> perform_actions,
const ElementFinder::Result& element, const ElementFinder::Result& element,
......
...@@ -215,7 +215,7 @@ TEST_F(ActionDelegateUtilTest, TakeElementAndGetProperty) { ...@@ -215,7 +215,7 @@ TEST_F(ActionDelegateUtilTest, TakeElementAndGetProperty) {
.WillOnce(RunOnceCallback<1>(OkClientStatus(), "value")); .WillOnce(RunOnceCallback<1>(OkClientStatus(), "value"));
EXPECT_CALL(*this, MockDoneGet(EqualsStatus(OkClientStatus()), "value")); EXPECT_CALL(*this, MockDoneGet(EqualsStatus(OkClientStatus()), "value"));
TakeElementAndGetProperty( TakeElementAndGetProperty<std::string>(
base::BindOnce(&ActionDelegateUtilTest::MockGetAction, base::BindOnce(&ActionDelegateUtilTest::MockGetAction,
base::Unretained(this)), base::Unretained(this)),
base::BindOnce(&ActionDelegateUtilTest::MockDoneGet, base::BindOnce(&ActionDelegateUtilTest::MockDoneGet,
...@@ -233,7 +233,7 @@ TEST_F(ActionDelegateUtilTest, TakeElementAndGetPropertyWithFailedStatus) { ...@@ -233,7 +233,7 @@ TEST_F(ActionDelegateUtilTest, TakeElementAndGetPropertyWithFailedStatus) {
MockDoneGet(EqualsStatus(ClientStatus(ELEMENT_RESOLUTION_FAILED)), MockDoneGet(EqualsStatus(ClientStatus(ELEMENT_RESOLUTION_FAILED)),
std::string())); std::string()));
TakeElementAndGetProperty( TakeElementAndGetProperty<std::string>(
base::BindOnce(&ActionDelegateUtilTest::MockGetAction, base::BindOnce(&ActionDelegateUtilTest::MockGetAction,
base::Unretained(this)), base::Unretained(this)),
base::BindOnce(&ActionDelegateUtilTest::MockDoneGet, base::BindOnce(&ActionDelegateUtilTest::MockDoneGet,
......
...@@ -131,7 +131,7 @@ void GetElementStatusAction::OnWaitForElement( ...@@ -131,7 +131,7 @@ void GetElementStatusAction::OnWaitForElement(
delegate_->FindElement( delegate_->FindElement(
selector_, selector_,
base::BindOnce( base::BindOnce(
&action_delegate_util::TakeElementAndGetProperty, &action_delegate_util::TakeElementAndGetProperty<std::string>,
base::BindOnce(&ActionDelegate::GetStringAttribute, base::BindOnce(&ActionDelegate::GetStringAttribute,
delegate_->GetWeakPtr(), attribute_list), delegate_->GetWeakPtr(), attribute_list),
base::BindOnce(&GetElementStatusAction::OnGetStringAttribute, base::BindOnce(&GetElementStatusAction::OnGetStringAttribute,
......
...@@ -47,11 +47,13 @@ void UploadDomAction::OnWaitForElement(const Selector& selector, ...@@ -47,11 +47,13 @@ void UploadDomAction::OnWaitForElement(const Selector& selector,
} }
delegate_->FindElement( delegate_->FindElement(
selector, base::BindOnce(&action_delegate_util::TakeElementAndGetProperty, selector,
base::BindOnce(&ActionDelegate::GetOuterHtml, base::BindOnce(
delegate_->GetWeakPtr()), &action_delegate_util::TakeElementAndGetProperty<std::string>,
base::BindOnce(&UploadDomAction::OnGetOuterHtml, base::BindOnce(&ActionDelegate::GetOuterHtml,
weak_ptr_factory_.GetWeakPtr()))); delegate_->GetWeakPtr()),
base::BindOnce(&UploadDomAction::OnGetOuterHtml,
weak_ptr_factory_.GetWeakPtr())));
} }
void UploadDomAction::OnGetOuterHtml(const ClientStatus& status, void UploadDomAction::OnGetOuterHtml(const ClientStatus& status,
......
...@@ -65,15 +65,16 @@ void BatchElementChecker::Run(WebController* web_controller) { ...@@ -65,15 +65,16 @@ void BatchElementChecker::Run(WebController* web_controller) {
for (auto& entry : get_field_value_callbacks_) { for (auto& entry : get_field_value_callbacks_) {
web_controller->FindElement( web_controller->FindElement(
entry.first, /* strict= */ true, entry.first, /* strict= */ true,
base::BindOnce(&action_delegate_util::TakeElementAndGetProperty, base::BindOnce(
base::BindOnce(&WebController::GetFieldValue, &action_delegate_util::TakeElementAndGetProperty<std::string>,
web_controller->GetWeakPtr()), base::BindOnce(&WebController::GetFieldValue,
base::BindOnce(&BatchElementChecker::OnFieldValueChecked, web_controller->GetWeakPtr()),
weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&BatchElementChecker::OnFieldValueChecked,
// Guaranteed to exist for the lifetime of weak_ptr_factory_.GetWeakPtr(),
// this instance, because the map isn't // Guaranteed to exist for the lifetime of
// modified after Run has been called. // this instance, because the map isn't
base::Unretained(&entry.second)))); // modified after Run has been called.
base::Unretained(&entry.second))));
} }
// The extra +1 of pending_check_count and this check happening last // The extra +1 of pending_check_count and this check happening last
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#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 "base/time/time.h" #include "base/time/time.h"
#include "components/autofill_assistant/browser/actions/action_delegate_util.h"
#include "components/autofill_assistant/browser/script_executor_delegate.h" #include "components/autofill_assistant/browser/script_executor_delegate.h"
#include "components/autofill_assistant/browser/web/web_controller.h" #include "components/autofill_assistant/browser/web/web_controller.h"
...@@ -128,10 +129,15 @@ void ElementArea::Update() { ...@@ -128,10 +129,15 @@ void ElementArea::Update() {
for (auto& rectangle : rectangles_) { for (auto& rectangle : rectangles_) {
for (auto& position : rectangle.positions) { for (auto& position : rectangle.positions) {
delegate_->GetWebController()->GetElementRect( delegate_->GetWebController()->FindElement(
position.selector, position.selector, /* strict= */ true,
base::BindOnce(&ElementArea::OnGetElementRect, base::BindOnce(
weak_ptr_factory_.GetWeakPtr(), position.selector)); &action_delegate_util::TakeElementAndGetProperty<RectF>,
base::BindOnce(&WebController::GetElementRect,
delegate_->GetWebController()->GetWeakPtr()),
base::BindOnce(&ElementArea::OnGetElementRect,
weak_ptr_factory_.GetWeakPtr(),
position.selector)));
} }
} }
} }
......
...@@ -107,12 +107,12 @@ class MockWebController : public WebController { ...@@ -107,12 +107,12 @@ class MockWebController : public WebController {
callback)); callback));
void GetElementRect( void GetElementRect(
const Selector& selector, const ElementFinder::Result& element,
ElementRectGetter::ElementRectCallback callback) override { ElementRectGetter::ElementRectCallback callback) override {
OnGetElementRect(selector, callback); OnGetElementRect(element, callback);
} }
MOCK_METHOD2(OnGetElementRect, MOCK_METHOD2(OnGetElementRect,
void(const Selector& selector, void(const ElementFinder::Result& element,
ElementRectGetter::ElementRectCallback& callback)); ElementRectGetter::ElementRectCallback& callback));
void WaitForWindowHeightChange( void WaitForWindowHeightChange(
......
...@@ -1317,28 +1317,15 @@ void WebController::OnGetVisualViewport( ...@@ -1317,28 +1317,15 @@ void WebController::OnGetVisualViewport(
} }
void WebController::GetElementRect( void WebController::GetElementRect(
const Selector& selector, const ElementFinder::Result& element,
ElementRectGetter::ElementRectCallback callback) { ElementRectGetter::ElementRectCallback callback) {
FindElement(
selector, /* strict_mode= */ true,
base::BindOnce(&WebController::OnFindElementForRect,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void WebController::OnFindElementForRect(
ElementRectGetter::ElementRectCallback callback,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element_result) {
if (!element_status.ok()) {
std::move(callback).Run(element_status, RectF());
return;
}
std::unique_ptr<ElementRectGetter> getter = std::unique_ptr<ElementRectGetter> getter =
std::make_unique<ElementRectGetter>(devtools_client_.get()); std::make_unique<ElementRectGetter>(devtools_client_.get());
auto* ptr = getter.get(); auto* ptr = getter.get();
pending_workers_.emplace_back(std::move(getter)); pending_workers_.emplace_back(std::move(getter));
ptr->Start( ptr->Start(
std::move(element_result), // TODO(b/172041811): Ownership of element.
std::make_unique<ElementFinder::Result>(element),
base::BindOnce(&WebController::OnGetElementRect, base::BindOnce(&WebController::OnGetElementRect,
weak_ptr_factory_.GetWeakPtr(), ptr, std::move(callback))); weak_ptr_factory_.GetWeakPtr(), ptr, std::move(callback)));
} }
......
...@@ -215,14 +215,14 @@ class WebController { ...@@ -215,14 +215,14 @@ class WebController {
virtual void GetVisualViewport( virtual void GetVisualViewport(
base::OnceCallback<void(const ClientStatus&, const RectF&)> callback); base::OnceCallback<void(const ClientStatus&, const RectF&)> callback);
// Gets the position of the element identified by the selector. // Gets the position of the |element|.
// //
// If unsuccessful, the callback gets the failure status with an empty rect. // If unsuccessful, the callback gets the failure status with an empty rect.
// //
// If successful, the callback gets a success status with a set of // If successful, the callback gets a success status with a set of
// (left, top, right, bottom) coordinates rect, expressed in absolute CSS // (left, top, right, bottom) coordinates rect, expressed in absolute CSS
// coordinates. // coordinates.
virtual void GetElementRect(const Selector& selector, virtual void GetElementRect(const ElementFinder::Result& element,
ElementRectGetter::ElementRectCallback callback); ElementRectGetter::ElementRectCallback callback);
// Calls the callback once the main document window has been resized. // Calls the callback once the main document window has been resized.
...@@ -378,9 +378,6 @@ class WebController { ...@@ -378,9 +378,6 @@ class WebController {
size_t index, size_t index,
int delay_in_milli, int delay_in_milli,
base::OnceCallback<void(const ClientStatus&)> callback); base::OnceCallback<void(const ClientStatus&)> callback);
void OnFindElementForRect(ElementRectGetter::ElementRectCallback callback,
const ClientStatus& status,
std::unique_ptr<ElementFinder::Result> result);
void OnGetElementRect(ElementRectGetter* getter_to_release, void OnGetElementRect(ElementRectGetter* getter_to_release,
ElementRectGetter::ElementRectCallback callback, ElementRectGetter::ElementRectCallback callback,
const ClientStatus& rect_status, const ClientStatus& rect_status,
......
...@@ -800,15 +800,39 @@ class WebControllerBrowserTest : public content::ContentBrowserTest, ...@@ -800,15 +800,39 @@ class WebControllerBrowserTest : public content::ContentBrowserTest,
ClientStatus GetElementRect(const Selector& selector, RectF* rect_output) { ClientStatus GetElementRect(const Selector& selector, RectF* rect_output) {
base::RunLoop run_loop; base::RunLoop run_loop;
ClientStatus result; ClientStatus result;
web_controller_->GetElementRect(
selector, base::BindOnce(&WebControllerBrowserTest::OnGetElementRect, web_controller_->FindElement(
base::Unretained(this), run_loop.QuitClosure(), selector, /* strict= */ true,
&result, rect_output)); base::BindOnce(&WebControllerBrowserTest::GetElementRectElementCallback,
base::Unretained(this), run_loop.QuitClosure(), &result,
rect_output));
run_loop.Run(); run_loop.Run();
return result; return result;
} }
void OnGetElementRect(base::OnceClosure done_callback, void GetElementRectElementCallback(
base::OnceClosure done_callback,
ClientStatus* result_output,
RectF* rect_output,
const ClientStatus& element_status,
std::unique_ptr<ElementFinder::Result> element_result) {
if (!element_status.ok()) {
*result_output = element_status;
std::move(done_callback).Run();
return;
}
ASSERT_TRUE(element_result != nullptr);
web_controller_->GetElementRect(
*element_result,
base::BindOnce(&WebControllerBrowserTest::OnGetElementRect,
base::Unretained(this), std::move(element_result),
std::move(done_callback), result_output, rect_output));
}
void OnGetElementRect(std::unique_ptr<ElementFinder::Result> element,
base::OnceClosure done_callback,
ClientStatus* result_output, ClientStatus* result_output,
RectF* rect_output, RectF* rect_output,
const ClientStatus& rect_status, const ClientStatus& rect_status,
......
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