Commit 5f13cb27 authored by Lukasz Suder's avatar Lukasz Suder Committed by Commit Bot

[Autofill Assistant] Server Payload is saved between requests.

The Server Payload should not only be passed for GetNextActions, but
also for GetActions.

Now it is saved inside ClientMemory as ScriptExecutor has too short
lifetime (only 1 request/response roundtrip).

Bug: 806868
Change-Id: Id18ab90bfc2c4ea69bd07caab6373c6d0dbf139f
Reviewed-on: https://chromium-review.googlesource.com/c/1298013
Commit-Queue: Lukasz Suder <lsuder@google.com>
Reviewed-by: default avatarStephane Zermatten <szermatt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602373}
parent 97e7182b
...@@ -34,6 +34,7 @@ class MockService : public Service { ...@@ -34,6 +34,7 @@ class MockService : public Service {
void GetActions(const std::string& script_path, void GetActions(const std::string& script_path,
const GURL& ignored_url, const GURL& ignored_url,
const std::map<std::string, std::string>& parameters, const std::map<std::string, std::string>& parameters,
const std::string& server_payload,
ResponseCallback callback) override { ResponseCallback callback) override {
OnGetActions(script_path, parameters, callback); OnGetActions(script_path, parameters, callback);
} }
......
...@@ -105,7 +105,8 @@ bool ProtocolUtils::ParseScripts( ...@@ -105,7 +105,8 @@ bool ProtocolUtils::ParseScripts(
std::string ProtocolUtils::CreateInitialScriptActionsRequest( std::string ProtocolUtils::CreateInitialScriptActionsRequest(
const std::string& script_path, const std::string& script_path,
const GURL& url, const GURL& url,
const std::map<std::string, std::string>& parameters) { const std::map<std::string, std::string>& parameters,
const std::string& server_payload) {
ScriptActionRequestProto request_proto; ScriptActionRequestProto request_proto;
InitialScriptActionsRequestProto* initial_request_proto = InitialScriptActionsRequestProto* initial_request_proto =
request_proto.mutable_initial_request(); request_proto.mutable_initial_request();
...@@ -119,6 +120,10 @@ std::string ProtocolUtils::CreateInitialScriptActionsRequest( ...@@ -119,6 +120,10 @@ std::string ProtocolUtils::CreateInitialScriptActionsRequest(
request_proto.mutable_client_context()->mutable_chrome()->set_chrome_version( request_proto.mutable_client_context()->mutable_chrome()->set_chrome_version(
version_info::GetProductNameAndVersionForUserAgent()); version_info::GetProductNameAndVersionForUserAgent());
if (!server_payload.empty()) {
request_proto.set_server_payload(server_payload);
}
std::string serialized_initial_request_proto; std::string serialized_initial_request_proto;
bool success = bool success =
request_proto.SerializeToString(&serialized_initial_request_proto); request_proto.SerializeToString(&serialized_initial_request_proto);
......
...@@ -43,7 +43,8 @@ class ProtocolUtils { ...@@ -43,7 +43,8 @@ class ProtocolUtils {
static std::string CreateInitialScriptActionsRequest( static std::string CreateInitialScriptActionsRequest(
const std::string& script_path, const std::string& script_path,
const GURL& url, const GURL& url,
const std::map<std::string, std::string>& parameters); const std::map<std::string, std::string>& parameters,
const std::string& server_payload);
// Create request to get next sequence of actions for a script. // Create request to get next sequence of actions for a script.
static std::string CreateNextScriptActionsRequest( static std::string CreateNextScriptActionsRequest(
......
...@@ -75,7 +75,8 @@ TEST(ProtocolUtilsTest, CreateInitialScriptActionsRequest) { ...@@ -75,7 +75,8 @@ TEST(ProtocolUtilsTest, CreateInitialScriptActionsRequest) {
ScriptActionRequestProto request; ScriptActionRequestProto request;
EXPECT_TRUE( EXPECT_TRUE(
request.ParseFromString(ProtocolUtils::CreateInitialScriptActionsRequest( request.ParseFromString(ProtocolUtils::CreateInitialScriptActionsRequest(
"script_path", GURL("http://example.com/"), parameters))); "script_path", GURL("http://example.com/"), parameters,
"server_payload")));
EXPECT_THAT(request.client_context().chrome().chrome_version(), EXPECT_THAT(request.client_context().chrome().chrome_version(),
Not(IsEmpty())); Not(IsEmpty()));
...@@ -88,6 +89,7 @@ TEST(ProtocolUtilsTest, CreateInitialScriptActionsRequest) { ...@@ -88,6 +89,7 @@ TEST(ProtocolUtilsTest, CreateInitialScriptActionsRequest) {
EXPECT_EQ("b", initial.script_parameters(0).value()); EXPECT_EQ("b", initial.script_parameters(0).value());
EXPECT_EQ("c", initial.script_parameters(1).name()); EXPECT_EQ("c", initial.script_parameters(1).name());
EXPECT_EQ("d", initial.script_parameters(1).value()); EXPECT_EQ("d", initial.script_parameters(1).value());
EXPECT_EQ("server_payload", request.server_payload());
} }
TEST(ProtocolUtilsTest, CreateGetScriptsRequest) { TEST(ProtocolUtilsTest, CreateGetScriptsRequest) {
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/credit_card.h"
#include "components/autofill_assistant/browser/batch_element_checker.h" #include "components/autofill_assistant/browser/batch_element_checker.h"
#include "components/autofill_assistant/browser/client_memory.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"
...@@ -29,8 +30,12 @@ constexpr base::TimeDelta kWaitForSelectorDeadline = ...@@ -29,8 +30,12 @@ constexpr base::TimeDelta kWaitForSelectorDeadline =
} // namespace } // namespace
ScriptExecutor::ScriptExecutor(const std::string& script_path, ScriptExecutor::ScriptExecutor(const std::string& script_path,
const std::string& server_payload,
ScriptExecutor::Listener* listener,
ScriptExecutorDelegate* delegate) ScriptExecutorDelegate* delegate)
: script_path_(script_path), : script_path_(script_path),
last_server_payload_(server_payload),
listener_(listener),
delegate_(delegate), delegate_(delegate),
at_end_(CONTINUE), at_end_(CONTINUE),
should_stop_script_(false), should_stop_script_(false),
...@@ -46,7 +51,7 @@ void ScriptExecutor::Run(RunScriptCallback callback) { ...@@ -46,7 +51,7 @@ void ScriptExecutor::Run(RunScriptCallback callback) {
delegate_->GetService()->GetActions( delegate_->GetService()->GetActions(
script_path_, delegate_->GetWebController()->GetUrl(), script_path_, delegate_->GetWebController()->GetUrl(),
delegate_->GetParameters(), delegate_->GetParameters(), last_server_payload_,
base::BindOnce(&ScriptExecutor::OnGetActions, base::BindOnce(&ScriptExecutor::OnGetActions,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
...@@ -201,6 +206,9 @@ void ScriptExecutor::OnGetActions(bool result, const std::string& response) { ...@@ -201,6 +206,9 @@ void ScriptExecutor::OnGetActions(bool result, const std::string& response) {
bool parse_result = bool parse_result =
ProtocolUtils::ParseActions(response, &last_server_payload_, &actions_); ProtocolUtils::ParseActions(response, &last_server_payload_, &actions_);
if (listener_) {
listener_->OnServerPayloadChanged(last_server_payload_);
}
if (!parse_result) { if (!parse_result) {
RunCallback(false); RunCallback(false);
return; return;
......
...@@ -22,8 +22,22 @@ namespace autofill_assistant { ...@@ -22,8 +22,22 @@ namespace autofill_assistant {
// Class to execute an assistant script. // Class to execute an assistant script.
class ScriptExecutor : public ActionDelegate { class ScriptExecutor : public ActionDelegate {
public: public:
// |delegate| should outlive this object and should not be nullptr. // Listens to events on ScriptExecutor.
// TODO(b/806868): Make server_payload a part of callback instead of the
// listener.
class Listener {
public:
virtual ~Listener() = default;
// Called when new server_payload is available.
virtual void OnServerPayloadChanged(const std::string& server_payload) = 0;
};
// |delegate| and |listener| should outlive this object and should not be
// nullptr.
ScriptExecutor(const std::string& script_path, ScriptExecutor(const std::string& script_path,
const std::string& server_payload,
ScriptExecutor::Listener* listener,
ScriptExecutorDelegate* delegate); ScriptExecutorDelegate* delegate);
~ScriptExecutor() override; ~ScriptExecutor() override;
...@@ -105,12 +119,13 @@ class ScriptExecutor : public ActionDelegate { ...@@ -105,12 +119,13 @@ class ScriptExecutor : public ActionDelegate {
void OnProcessedAction(std::unique_ptr<ProcessedActionProto> action); void OnProcessedAction(std::unique_ptr<ProcessedActionProto> action);
std::string script_path_; std::string script_path_;
std::string last_server_payload_;
ScriptExecutor::Listener* const listener_;
ScriptExecutorDelegate* delegate_; ScriptExecutorDelegate* delegate_;
RunScriptCallback callback_; RunScriptCallback callback_;
std::vector<std::unique_ptr<Action>> actions_; std::vector<std::unique_ptr<Action>> actions_;
std::vector<ProcessedActionProto> processed_actions_; std::vector<ProcessedActionProto> processed_actions_;
std::string last_server_payload_;
AtEnd at_end_; AtEnd at_end_;
bool should_stop_script_; bool should_stop_script_;
bool should_clean_contextual_ui_on_finish_; bool should_clean_contextual_ui_on_finish_;
......
...@@ -32,10 +32,12 @@ using ::testing::StrEq; ...@@ -32,10 +32,12 @@ using ::testing::StrEq;
using ::testing::StrictMock; using ::testing::StrictMock;
using ::testing::_; using ::testing::_;
class ScriptExecutorTest : public testing::Test, public ScriptExecutorDelegate { class ScriptExecutorTest : public testing::Test,
public ScriptExecutorDelegate,
public ScriptExecutor::Listener {
public: public:
void SetUp() override { void SetUp() override {
executor_ = std::make_unique<ScriptExecutor>("script path", this); executor_ = std::make_unique<ScriptExecutor>("script path", "", this, this);
url_ = GURL("http://example.com/"); url_ = GURL("http://example.com/");
// In this test, "tell" actions always succeed and "click" actions always // In this test, "tell" actions always succeed and "click" actions always
...@@ -68,6 +70,8 @@ class ScriptExecutorTest : public testing::Test, public ScriptExecutorDelegate { ...@@ -68,6 +70,8 @@ class ScriptExecutorTest : public testing::Test, public ScriptExecutorDelegate {
return nullptr; return nullptr;
} }
void OnServerPayloadChanged(const std::string& server_payload) override {}
content::WebContents* GetWebContents() 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) {
......
...@@ -86,7 +86,8 @@ void ScriptTracker::ExecuteScript(const std::string& script_path, ...@@ -86,7 +86,8 @@ void ScriptTracker::ExecuteScript(const std::string& script_path,
} }
executed_scripts_[script_path] = SCRIPT_STATUS_RUNNING; executed_scripts_[script_path] = SCRIPT_STATUS_RUNNING;
executor_ = std::make_unique<ScriptExecutor>(script_path, delegate_); executor_ = std::make_unique<ScriptExecutor>(
script_path, last_server_payload_, this, delegate_);
ScriptExecutor::RunScriptCallback run_script_callback = base::BindOnce( ScriptExecutor::RunScriptCallback run_script_callback = base::BindOnce(
&ScriptTracker::OnScriptRun, weak_ptr_factory_.GetWeakPtr(), script_path, &ScriptTracker::OnScriptRun, weak_ptr_factory_.GetWeakPtr(), script_path,
std::move(callback)); std::move(callback));
...@@ -190,4 +191,8 @@ void ScriptTracker::ClearAvailableScripts() { ...@@ -190,4 +191,8 @@ void ScriptTracker::ClearAvailableScripts() {
TerminatePendingChecks(); TerminatePendingChecks();
} }
void ScriptTracker::OnServerPayloadChanged(const std::string& server_payload) {
last_server_payload_ = server_payload;
}
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -26,7 +26,7 @@ class ScriptExecutorDelegate; ...@@ -26,7 +26,7 @@ class ScriptExecutorDelegate;
// //
// User of this class is responsible for retrieving and passing scripts to the // User of this class is responsible for retrieving and passing scripts to the
// tracker and letting the tracker know about changes to the DOM. // tracker and letting the tracker know about changes to the DOM.
class ScriptTracker { class ScriptTracker : public ScriptExecutor::Listener {
public: public:
// Listens to changes on the ScriptTracker state. // Listens to changes on the ScriptTracker state.
class Listener { class Listener {
...@@ -44,7 +44,7 @@ class ScriptTracker { ...@@ -44,7 +44,7 @@ class ScriptTracker {
ScriptTracker(ScriptExecutorDelegate* delegate, ScriptTracker(ScriptExecutorDelegate* delegate,
ScriptTracker::Listener* listener); ScriptTracker::Listener* listener);
~ScriptTracker(); ~ScriptTracker() override;
// Updates the set of available |scripts|. This interrupts any pending checks, // Updates the set of available |scripts|. This interrupts any pending checks,
// but don't start a new one.' // but don't start a new one.'
...@@ -84,6 +84,9 @@ class ScriptTracker { ...@@ -84,6 +84,9 @@ class ScriptTracker {
void UpdateRunnableScriptsIfNecessary(); void UpdateRunnableScriptsIfNecessary();
void OnCheckDone(); void OnCheckDone();
// Overrides ScriptExecutor::Listener.
void OnServerPayloadChanged(const std::string& server_payload) override;
// Cleans up any state use by pending checks. Stops running pending checks. // Cleans up any state use by pending checks. Stops running pending checks.
void TerminatePendingChecks(); void TerminatePendingChecks();
...@@ -129,6 +132,8 @@ class ScriptTracker { ...@@ -129,6 +132,8 @@ class ScriptTracker {
// |pending_run_script_callback_| is not nullptr. // |pending_run_script_callback_| is not nullptr.
ScriptExecutor::RunScriptCallback pending_run_script_callback_; ScriptExecutor::RunScriptCallback pending_run_script_callback_;
std::string last_server_payload_;
base::WeakPtrFactory<ScriptTracker> weak_ptr_factory_; base::WeakPtrFactory<ScriptTracker> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ScriptTracker); DISALLOW_COPY_AND_ASSIGN(ScriptTracker);
......
...@@ -96,12 +96,13 @@ void Service::GetScriptsForUrl( ...@@ -96,12 +96,13 @@ void Service::GetScriptsForUrl(
void Service::GetActions(const std::string& script_path, void Service::GetActions(const std::string& script_path,
const GURL& url, const GURL& url,
const std::map<std::string, std::string>& parameters, const std::map<std::string, std::string>& parameters,
const std::string& server_payload,
ResponseCallback callback) { ResponseCallback callback) {
DCHECK(!script_path.empty()); DCHECK(!script_path.empty());
SendRequest(AddLoader(script_action_server_url_, SendRequest(AddLoader(script_action_server_url_,
ProtocolUtils::CreateInitialScriptActionsRequest( ProtocolUtils::CreateInitialScriptActionsRequest(
script_path, url, parameters), script_path, url, parameters, server_payload),
std::move(callback))); std::move(callback)));
} }
......
...@@ -51,6 +51,7 @@ class Service { ...@@ -51,6 +51,7 @@ class Service {
virtual void GetActions(const std::string& script_path, virtual void GetActions(const std::string& script_path,
const GURL& url, const GURL& url,
const std::map<std::string, std::string>& parameters, const std::map<std::string, std::string>& parameters,
const std::string& server_payload,
ResponseCallback callback); ResponseCallback callback);
// Get next sequence of actions according to server payload in previous // Get next sequence of actions according to server payload in previous
......
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