Commit 3d655c7d authored by Stephane Zermatten's avatar Stephane Zermatten Committed by Commit Bot

[Autofill Assistant] Implement the navigate action.

This change introduces support for the action navigate, which tells the
browser to go to a specific URL. The action finishes without waiting for
the new page to load.

Bug: 806868
Change-Id: Ibe354eb627b836e6e1d5ad5ff461a8b392102faa
Reviewed-on: https://chromium-review.googlesource.com/1248783
Commit-Queue: Stephane Zermatten <szermatt@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595061}
parent dfabb6a5
...@@ -22,6 +22,8 @@ jumbo_static_library("browser") { ...@@ -22,6 +22,8 @@ jumbo_static_library("browser") {
"actions/click_action.h", "actions/click_action.h",
"actions/focus_element_action.cc", "actions/focus_element_action.cc",
"actions/focus_element_action.h", "actions/focus_element_action.h",
"actions/navigate_action.cc",
"actions/navigate_action.h",
"actions/reset_action.cc", "actions/reset_action.cc",
"actions/reset_action.h", "actions/reset_action.h",
"actions/select_option_action.cc", "actions/select_option_action.cc",
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "base/callback_forward.h" #include "base/callback_forward.h"
class GURL;
namespace autofill { namespace autofill {
class AutofillProfile; class AutofillProfile;
} }
...@@ -90,6 +92,10 @@ class ActionDelegate { ...@@ -90,6 +92,10 @@ class ActionDelegate {
NodeProto* node_tree_out, NodeProto* node_tree_out,
base::OnceCallback<void(bool)> callback) = 0; base::OnceCallback<void(bool)> callback) = 0;
// Load |url| in the current tab. Returns immediately, before the new page has
// been loaded.
virtual void LoadURL(const GURL& url) = 0;
// Shut down Autofill Assistant at the end of the current script. // Shut down Autofill Assistant at the end of the current script.
virtual void Shutdown() = 0; virtual void Shutdown() = 0;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "components/autofill_assistant/browser/actions/mock_action_delegate.h" #include "components/autofill_assistant/browser/actions/mock_action_delegate.h"
#include "url/gurl.h"
namespace autofill_assistant { namespace autofill_assistant {
MockActionDelegate::MockActionDelegate() = default; MockActionDelegate::MockActionDelegate() = default;
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_MOCK_ACTION_DELEGATE_H_ #ifndef COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_MOCK_ACTION_DELEGATE_H_
#define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_MOCK_ACTION_DELEGATE_H_ #define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_MOCK_ACTION_DELEGATE_H_
#include <string>
#include <vector>
#include "base/callback.h" #include "base/callback.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"
...@@ -95,6 +98,7 @@ class MockActionDelegate : public ActionDelegate { ...@@ -95,6 +98,7 @@ class MockActionDelegate : public ActionDelegate {
void(const std::vector<std::string>& selectors, void(const std::vector<std::string>& selectors,
NodeProto* node_tree_out, NodeProto* node_tree_out,
base::OnceCallback<void(bool)> callback)); base::OnceCallback<void(bool)> callback));
MOCK_METHOD1(LoadURL, void(const GURL& url));
MOCK_METHOD0(Shutdown, void()); MOCK_METHOD0(Shutdown, void());
MOCK_METHOD0(Restart, void()); MOCK_METHOD0(Restart, void());
MOCK_METHOD0(GetClientMemory, ClientMemory*()); MOCK_METHOD0(GetClientMemory, ClientMemory*());
......
// Copyright 2018 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 "components/autofill_assistant/browser/actions/navigate_action.h"
#include <memory>
#include <utility>
#include "base/callback.h"
#include "components/autofill_assistant/browser/actions/action_delegate.h"
#include "url/gurl.h"
namespace autofill_assistant {
NavigateAction::NavigateAction(const ActionProto& proto) : Action(proto) {
DCHECK(proto_.has_navigate());
}
NavigateAction::~NavigateAction() {}
void NavigateAction::ProcessAction(ActionDelegate* delegate,
ProcessActionCallback callback) {
GURL url(proto_.navigate().url());
delegate->LoadURL(url);
processed_action_proto_ = std::make_unique<ProcessedActionProto>();
UpdateProcessedAction(/* status= */ true);
std::move(callback).Run(std::move(processed_action_proto_));
}
} // namespace autofill_assistant
// Copyright 2018 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 COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_NAVIGATE_ACTION_H_
#define COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_NAVIGATE_ACTION_H_
#include <string>
#include "base/macros.h"
#include "components/autofill_assistant/browser/actions/action.h"
namespace autofill_assistant {
// An action to display a message.
class NavigateAction : public Action {
public:
explicit NavigateAction(const ActionProto& proto);
~NavigateAction() override;
// Overrides Action:
void ProcessAction(ActionDelegate* delegate,
ProcessActionCallback callback) override;
private:
DISALLOW_COPY_AND_ASSIGN(NavigateAction);
};
} // namespace autofill_assistant
#endif // COMPONENTS_AUTOFILL_ASSISTANT_BROWSER_ACTIONS_NAVIGATE_ACTION_H_
...@@ -21,6 +21,8 @@ class MockWebController : public WebController { ...@@ -21,6 +21,8 @@ class MockWebController : public WebController {
MOCK_METHOD0(GetUrl, const GURL&()); MOCK_METHOD0(GetUrl, const GURL&());
MOCK_METHOD1(LoadURL, void(const GURL&));
void ClickElement(const std::vector<std::string>& selectors, void ClickElement(const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) override { base::OnceCallback<void(bool)> callback) override {
// Transforming callback into a references allows using RunOnceCallback on // Transforming callback into a references allows using RunOnceCallback on
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "components/autofill_assistant/browser/actions/autofill_action.h" #include "components/autofill_assistant/browser/actions/autofill_action.h"
#include "components/autofill_assistant/browser/actions/click_action.h" #include "components/autofill_assistant/browser/actions/click_action.h"
#include "components/autofill_assistant/browser/actions/focus_element_action.h" #include "components/autofill_assistant/browser/actions/focus_element_action.h"
#include "components/autofill_assistant/browser/actions/navigate_action.h"
#include "components/autofill_assistant/browser/actions/reset_action.h" #include "components/autofill_assistant/browser/actions/reset_action.h"
#include "components/autofill_assistant/browser/actions/select_option_action.h" #include "components/autofill_assistant/browser/actions/select_option_action.h"
#include "components/autofill_assistant/browser/actions/stop_action.h" #include "components/autofill_assistant/browser/actions/stop_action.h"
...@@ -176,6 +177,10 @@ bool ProtocolUtils::ParseActions( ...@@ -176,6 +177,10 @@ bool ProtocolUtils::ParseActions(
actions->emplace_back(std::make_unique<SelectOptionAction>(action)); actions->emplace_back(std::make_unique<SelectOptionAction>(action));
break; break;
} }
case ActionProto::ActionInfoCase::kNavigate: {
actions->emplace_back(std::make_unique<NavigateAction>(action));
break;
}
case ActionProto::ActionInfoCase::kStop: { case ActionProto::ActionInfoCase::kStop: {
actions->emplace_back(std::make_unique<StopAction>(action)); actions->emplace_back(std::make_unique<StopAction>(action));
break; break;
......
...@@ -115,6 +115,10 @@ void ScriptExecutor::BuildNodeTree(const std::vector<std::string>& selectors, ...@@ -115,6 +115,10 @@ void ScriptExecutor::BuildNodeTree(const std::vector<std::string>& selectors,
std::move(callback)); std::move(callback));
} }
void ScriptExecutor::LoadURL(const GURL& url) {
delegate_->GetWebController()->LoadURL(url);
}
void ScriptExecutor::Shutdown() { void ScriptExecutor::Shutdown() {
at_end_ = SHUTDOWN; at_end_ = SHUTDOWN;
} }
......
...@@ -80,6 +80,7 @@ class ScriptExecutor : public ActionDelegate { ...@@ -80,6 +80,7 @@ class ScriptExecutor : public ActionDelegate {
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;
void LoadURL(const GURL& url) override;
void Shutdown() override; void Shutdown() override;
void Restart() override; void Restart() override;
ClientMemory* GetClientMemory() override; ClientMemory* GetClientMemory() override;
......
...@@ -187,6 +187,7 @@ message ActionProto { ...@@ -187,6 +187,7 @@ message ActionProto {
oneof action_info { oneof action_info {
ClickProto click = 5; ClickProto click = 5;
SelectOptionProto select_option = 7; SelectOptionProto select_option = 7;
NavigateProto navigate = 9;
TellProto tell = 11; TellProto tell = 11;
FocusElementProto focus_element = 12; FocusElementProto focus_element = 12;
WaitForDomProto wait_for_dom = 19; WaitForDomProto wait_for_dom = 19;
...@@ -357,6 +358,11 @@ message UploadDomProto { ...@@ -357,6 +358,11 @@ message UploadDomProto {
optional ElementReferenceProto tree_root = 1; optional ElementReferenceProto tree_root = 1;
} }
// Load the given URL in the current tab.
message NavigateProto {
optional string url = 1;
}
// Resets Autofill Assistant: clears any state and server payload. // Resets Autofill Assistant: clears any state and server payload.
message ResetProto {} message ResetProto {}
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "components/autofill_assistant/browser/web_controller.h" #include "components/autofill_assistant/browser/web_controller.h"
#include <utility>
#include "base/callback.h" #include "base/callback.h"
#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"
...@@ -83,6 +85,11 @@ const GURL& WebController::GetUrl() { ...@@ -83,6 +85,11 @@ const GURL& WebController::GetUrl() {
return web_contents_->GetLastCommittedURL(); return web_contents_->GetLastCommittedURL();
} }
void WebController::LoadURL(const GURL& url) {
web_contents_->GetController().LoadURLWithParams(
content::NavigationController::LoadURLParams(url));
}
void WebController::ClickElement(const std::vector<std::string>& selectors, void WebController::ClickElement(const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback) { base::OnceCallback<void(bool)> callback) {
DCHECK(!selectors.empty()); DCHECK(!selectors.empty());
......
...@@ -45,6 +45,10 @@ class WebController { ...@@ -45,6 +45,10 @@ class WebController {
// Returns the last committed URL of the associated |web_contents_|. // Returns the last committed URL of the associated |web_contents_|.
virtual const GURL& GetUrl(); virtual const GURL& GetUrl();
// Load |url| in the current tab. Returns immediately, before the new page has
// been loaded.
virtual void LoadURL(const GURL& url);
// Perform a mouse left button click on the element given by |selectors| and // Perform a mouse left button click on the element given by |selectors| and
// return the result through callback. // return the result through callback.
// CSS selectors in |selectors| are ordered from top frame to the frame // CSS selectors in |selectors| are ordered from top frame to the frame
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
namespace autofill_assistant { namespace autofill_assistant {
const char* kTargetWebsitePath = "/autofill_assistant_target_website.html";
class WebControllerBrowserTest : public content::ContentBrowserTest { class WebControllerBrowserTest : public content::ContentBrowserTest {
public: public:
WebControllerBrowserTest() {} WebControllerBrowserTest() {}
...@@ -26,9 +28,8 @@ class WebControllerBrowserTest : public content::ContentBrowserTest { ...@@ -26,9 +28,8 @@ class WebControllerBrowserTest : public content::ContentBrowserTest {
http_server_->ServeFilesFromSourceDirectory( http_server_->ServeFilesFromSourceDirectory(
"components/test/data/autofill_assistant"); "components/test/data/autofill_assistant");
ASSERT_TRUE(http_server_->Start()); ASSERT_TRUE(http_server_->Start());
ASSERT_TRUE(NavigateToURL( ASSERT_TRUE(
shell(), NavigateToURL(shell(), http_server_->GetURL(kTargetWebsitePath)));
http_server_->GetURL("/autofill_assistant_target_website.html")));
web_controller_ = web_controller_ =
WebController::CreateForWebContents(shell()->web_contents()); WebController::CreateForWebContents(shell()->web_contents());
} }
...@@ -200,9 +201,11 @@ class WebControllerBrowserTest : public content::ContentBrowserTest { ...@@ -200,9 +201,11 @@ class WebControllerBrowserTest : public content::ContentBrowserTest {
std::move(done_callback).Run(); std::move(done_callback).Run();
} }
protected:
std::unique_ptr<WebController> web_controller_;
private: private:
std::unique_ptr<net::EmbeddedTestServer> http_server_; std::unique_ptr<net::EmbeddedTestServer> http_server_;
std::unique_ptr<WebController> web_controller_;
DISALLOW_COPY_AND_ASSIGN(WebControllerBrowserTest); DISALLOW_COPY_AND_ASSIGN(WebControllerBrowserTest);
}; };
...@@ -398,4 +401,11 @@ IN_PROC_BROWSER_TEST_F(WebControllerBrowserTest, GetAndSetFieldValue) { ...@@ -398,4 +401,11 @@ IN_PROC_BROWSER_TEST_F(WebControllerBrowserTest, GetAndSetFieldValue) {
EXPECT_FALSE(SetFieldValue(selectors, "foobar")); EXPECT_FALSE(SetFieldValue(selectors, "foobar"));
} }
IN_PROC_BROWSER_TEST_F(WebControllerBrowserTest, NavigateToUrl) {
EXPECT_EQ(kTargetWebsitePath, web_controller_->GetUrl().path());
web_controller_->LoadURL(GURL(url::kAboutBlankURL));
WaitForLoadStop(shell()->web_contents());
EXPECT_EQ(url::kAboutBlankURL, web_controller_->GetUrl().spec());
}
} // namespace } // namespace
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