Commit b9174f8e authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Removed dep from lite_service on service_impl

This makes it easier to control the amount of data we send in the RPC.
It's also cleaner this way, and furthermore, this is in preparation for
a new RPC, which lite_service will communicate with, but service_impl
will not.

Bug: b/171178550
Change-Id: Idbab2402ede4f4473575baf4254ed0cc13d0c7e0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2485254
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Auto-Submit: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Reviewed-by: default avatarMarian Fechete <marianfe@google.com>
Cr-Commit-Position: refs/heads/master@{#818965}
parent a35f730e
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "components/autofill_assistant/browser/service/api_key_fetcher.h" #include "components/autofill_assistant/browser/service/api_key_fetcher.h"
#include "components/autofill_assistant/browser/service/lite_service.h" #include "components/autofill_assistant/browser/service/lite_service.h"
#include "components/autofill_assistant/browser/service/server_url_fetcher.h" #include "components/autofill_assistant/browser/service/server_url_fetcher.h"
#include "components/autofill_assistant/browser/service/service_impl.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
namespace autofill_assistant { namespace autofill_assistant {
...@@ -48,17 +47,15 @@ jlong JNI_AutofillAssistantLiteService_CreateLiteService( ...@@ -48,17 +47,15 @@ jlong JNI_AutofillAssistantLiteService_CreateLiteService(
} }
ServerUrlFetcher url_fetcher{ServerUrlFetcher::GetDefaultServerUrl()}; ServerUrlFetcher url_fetcher{ServerUrlFetcher::GetDefaultServerUrl()};
auto request_sender = std::make_unique<ServiceRequestSender>(
web_contents->GetBrowserContext(), /* access_token_fetcher = */ nullptr,
std::make_unique<NativeURLLoaderFactory>(),
ApiKeyFetcher().GetAPIKey(chrome::GetChannel()),
/* auth_enabled = */ false, /* disable_auth_if_no_access_token = */ true);
return reinterpret_cast<jlong>(new LiteService( return reinterpret_cast<jlong>(new LiteService(
std::make_unique<ServiceImpl>(std::move(request_sender), std::make_unique<ServiceRequestSender>(
url_fetcher.GetSupportsScriptEndpoint(), web_contents->GetBrowserContext(),
url_fetcher.GetNextActionsEndpoint(), /* access_token_fetcher = */ nullptr,
std::make_unique<EmptyClientContext>()), std::make_unique<NativeURLLoaderFactory>(),
ApiKeyFetcher().GetAPIKey(chrome::GetChannel()),
/* auth_enabled = */ false,
/* disable_auth_if_no_access_token = */ true),
url_fetcher.GetNextActionsEndpoint(),
base::android::ConvertJavaStringToUTF8(env, jtrigger_script_path), base::android::ConvertJavaStringToUTF8(env, jtrigger_script_path),
base::BindOnce(&OnFinished, base::android::ScopedJavaGlobalRef<jobject>( base::BindOnce(&OnFinished, base::android::ScopedJavaGlobalRef<jobject>(
java_lite_service)), java_lite_service)),
......
...@@ -12,17 +12,20 @@ ...@@ -12,17 +12,20 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "components/autofill_assistant/browser/protocol_utils.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
namespace autofill_assistant { namespace autofill_assistant {
LiteService::LiteService( LiteService::LiteService(
std::unique_ptr<Service> service_impl, std::unique_ptr<ServiceRequestSender> request_sender,
const GURL& get_actions_server_url,
const std::string& trigger_script_path, const std::string& trigger_script_path,
base::OnceCallback<void(Metrics::LiteScriptFinishedState)> base::OnceCallback<void(Metrics::LiteScriptFinishedState)>
notify_finished_callback, notify_finished_callback,
base::RepeatingCallback<void(bool)> notify_script_running_callback) base::RepeatingCallback<void(bool)> notify_script_running_callback)
: service_impl_(std::move(service_impl)), : request_sender_(std::move(request_sender)),
get_actions_server_url_(get_actions_server_url),
trigger_script_path_(trigger_script_path), trigger_script_path_(trigger_script_path),
notify_finished_callback_(std::move(notify_finished_callback)), notify_finished_callback_(std::move(notify_finished_callback)),
notify_script_running_callback_( notify_script_running_callback_(
...@@ -70,10 +73,12 @@ void LiteService::GetActions(const std::string& script_path, ...@@ -70,10 +73,12 @@ void LiteService::GetActions(const std::string& script_path,
// Note: trigger context and payloads should not be sent to the backend for // Note: trigger context and payloads should not be sent to the backend for
// privacy reasons. // privacy reasons.
service_impl_->GetActions( request_sender_->SendRequest(
trigger_script_path_, GURL(), TriggerContextImpl(), get_actions_server_url_,
/* global_payload = */ std::string(), ProtocolUtils::CreateInitialScriptActionsRequest(
/* script_payload = */ std::string(), trigger_script_path_, GURL(), /* global_payload = */ std::string(),
/* script_payload = */ std::string(), EmptyClientContext().AsProto(),
/* script_parameters = */ {}),
base::BindOnce(&LiteService::OnGetActions, weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&LiteService::OnGetActions, weak_ptr_factory_.GetWeakPtr(),
std::move(callback))); std::move(callback)));
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "components/autofill_assistant/browser/metrics.h" #include "components/autofill_assistant/browser/metrics.h"
#include "components/autofill_assistant/browser/service/service_impl.h" #include "components/autofill_assistant/browser/service/service_impl.h"
#include "components/autofill_assistant/browser/service/service_request_sender.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -25,7 +26,8 @@ namespace autofill_assistant { ...@@ -25,7 +26,8 @@ namespace autofill_assistant {
class LiteService : public Service { class LiteService : public Service {
public: public:
explicit LiteService( explicit LiteService(
std::unique_ptr<Service> service_impl, std::unique_ptr<ServiceRequestSender> request_sender,
const GURL& get_actions_server_url,
const std::string& trigger_script_path, const std::string& trigger_script_path,
base::OnceCallback<void(Metrics::LiteScriptFinishedState)> base::OnceCallback<void(Metrics::LiteScriptFinishedState)>
notify_finished_callback, notify_finished_callback,
...@@ -78,8 +80,12 @@ class LiteService : public Service { ...@@ -78,8 +80,12 @@ class LiteService : public Service {
void StopWithoutErrorMessage(ResponseCallback callback, void StopWithoutErrorMessage(ResponseCallback callback,
Metrics::LiteScriptFinishedState state); Metrics::LiteScriptFinishedState state);
// The actual service that communicates with the backend. // The request sender that communicates with the backend.
std::unique_ptr<Service> service_impl_; std::unique_ptr<ServiceRequestSender> request_sender_;
// The GURL of the GetActions/GetNextActions rpc handler.
GURL get_actions_server_url_;
// The script path to fetch actions from. // The script path to fetch actions from.
std::string trigger_script_path_; std::string trigger_script_path_;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "components/autofill_assistant/browser/service/lite_service.h" #include "components/autofill_assistant/browser/service/lite_service.h"
#include "components/autofill_assistant/browser/service.pb.h" #include "components/autofill_assistant/browser/service.pb.h"
#include "components/autofill_assistant/browser/service/mock_service.h" #include "components/autofill_assistant/browser/service/mock_service.h"
#include "components/autofill_assistant/browser/service/mock_service_request_sender.h"
#include "components/autofill_assistant/browser/service/service.h" #include "components/autofill_assistant/browser/service/service.h"
#include "components/autofill_assistant/browser/test_util.h" #include "components/autofill_assistant/browser/test_util.h"
#include "components/autofill_assistant/browser/trigger_context.h" #include "components/autofill_assistant/browser/trigger_context.h"
...@@ -27,31 +28,31 @@ namespace autofill_assistant { ...@@ -27,31 +28,31 @@ namespace autofill_assistant {
namespace { namespace {
const char kFakeScriptPath[] = "localhost/chrome/test"; const char kFakeScriptPath[] = "localhost/chrome/test";
const char kFakeUrl[] = "https://www.example.com"; const char kFakeUrl[] = "https://www.example.com";
const char kGetActionsServerUrl[] =
"https://www.fake.backend.com/action_server";
} // namespace } // namespace
using ::testing::_; using ::testing::_;
using ::testing::AtMost; using ::testing::AtMost;
using ::testing::Eq; using ::testing::Eq;
using ::testing::NiceMock;
class LiteServiceTest : public testing::Test { class LiteServiceTest : public testing::Test {
protected: protected:
LiteServiceTest() { LiteServiceTest() {
auto service_impl = std::make_unique<MockService>(); auto mock_request_sender =
mock_native_service_ = service_impl.get(); std::make_unique<NiceMock<MockServiceRequestSender>>();
mock_request_sender_ = mock_request_sender.get();
lite_service_ = std::make_unique<LiteService>( lite_service_ = std::make_unique<LiteService>(
std::move(service_impl), kFakeScriptPath, mock_finished_callback_.Get(), std::move(mock_request_sender), GURL(kGetActionsServerUrl),
kFakeScriptPath, mock_finished_callback_.Get(),
mock_script_running_callback_.Get()); mock_script_running_callback_.Get());
EXPECT_CALL(*mock_native_service_, OnGetScriptsForUrl).Times(0);
EXPECT_CALL(*mock_native_service_, OnGetNextActions).Times(0);
EXPECT_CALL(*mock_native_service_, EXPECT_CALL(*mock_request_sender_,
OnGetActions(kFakeScriptPath, _, _, _, _, _)) OnSendRequest(GURL(kGetActionsServerUrl), _, _))
.Times(AtMost(1)) .Times(AtMost(1))
.WillOnce([&](const std::string& script_path, const GURL& url, .WillOnce([&](const GURL& url, const std::string& request_body,
const TriggerContext& trigger_context,
const std::string& global_payload,
const std::string& script_payload,
Service::ResponseCallback& callback) { Service::ResponseCallback& callback) {
std::string serialized_response; std::string serialized_response;
get_actions_response_.SerializeToString(&serialized_response); get_actions_response_.SerializeToString(&serialized_response);
...@@ -78,13 +79,13 @@ class LiteServiceTest : public testing::Test { ...@@ -78,13 +79,13 @@ class LiteServiceTest : public testing::Test {
EXPECT_CALL(mock_finished_callback_, Run(state)); EXPECT_CALL(mock_finished_callback_, Run(state));
} }
NiceMock<MockServiceRequestSender>* mock_request_sender_;
base::MockCallback<base::OnceCallback<void(Metrics::LiteScriptFinishedState)>> base::MockCallback<base::OnceCallback<void(Metrics::LiteScriptFinishedState)>>
mock_finished_callback_; mock_finished_callback_;
base::MockCallback<base::RepeatingCallback<void(bool)>> base::MockCallback<base::RepeatingCallback<void(bool)>>
mock_script_running_callback_; mock_script_running_callback_;
base::MockCallback<base::OnceCallback<void(int, const std::string&)>> base::MockCallback<base::OnceCallback<void(int, const std::string&)>>
mock_response_callback_; mock_response_callback_;
MockService* mock_native_service_ = nullptr;
std::unique_ptr<LiteService> lite_service_; std::unique_ptr<LiteService> lite_service_;
ActionsResponseProto get_actions_response_; ActionsResponseProto get_actions_response_;
}; };
...@@ -98,9 +99,10 @@ TEST_F(LiteServiceTest, RunsNotificationOnDelete) { ...@@ -98,9 +99,10 @@ TEST_F(LiteServiceTest, RunsNotificationOnDelete) {
notification_callback, notification_callback,
Run(Metrics::LiteScriptFinishedState::LITE_SCRIPT_SERVICE_DELETED)); Run(Metrics::LiteScriptFinishedState::LITE_SCRIPT_SERVICE_DELETED));
{ {
LiteService lite_service(std::make_unique<MockService>(), kFakeScriptPath, LiteService lite_service(
notification_callback.Get(), std::make_unique<NiceMock<MockServiceRequestSender>>(),
script_running_callback.Get()); GURL(kGetActionsServerUrl), kFakeScriptPath,
notification_callback.Get(), script_running_callback.Get());
} }
} }
...@@ -125,20 +127,20 @@ TEST_F(LiteServiceTest, GetActionsOnlySendsScriptPath) { ...@@ -125,20 +127,20 @@ TEST_F(LiteServiceTest, GetActionsOnlySendsScriptPath) {
TriggerContextImpl non_empty_context; TriggerContextImpl non_empty_context;
non_empty_context.SetCallerAccountHash("something"); non_empty_context.SetCallerAccountHash("something");
EXPECT_CALL(*mock_native_service_, EXPECT_CALL(*mock_request_sender_,
OnGetActions(kFakeScriptPath, _, _, _, _, _)) OnSendRequest(GURL(kGetActionsServerUrl), _, _))
.Times(1) .Times(1)
.WillOnce([&](const std::string& script_path, const GURL& url, .WillOnce([&](const GURL& url, const std::string& request_body,
const TriggerContext& trigger_context,
const std::string& global_payload,
const std::string& script_payload,
Service::ResponseCallback& callback) { Service::ResponseCallback& callback) {
EXPECT_THAT(script_path, Eq(kFakeScriptPath)); ScriptActionRequestProto rpc_proto;
ASSERT_TRUE(rpc_proto.ParseFromString(request_body));
EXPECT_THAT(rpc_proto.initial_request().query().script_path(0),
Eq(kFakeScriptPath));
// All other information has been cleared. // All other information has been cleared.
EXPECT_THAT(url, Eq(GURL())); EXPECT_THAT(rpc_proto.global_payload(), Eq(""));
EXPECT_THAT(trigger_context.get_caller_account_hash(), Eq("")); EXPECT_THAT(rpc_proto.script_payload(), Eq(""));
EXPECT_THAT(global_payload, Eq("")); EXPECT_THAT(rpc_proto.client_context(), Eq(ClientContextProto()));
EXPECT_THAT(script_payload, Eq(""));
}); });
lite_service_->GetActions(kFakeScriptPath, GURL(kFakeUrl), non_empty_context, lite_service_->GetActions(kFakeScriptPath, GURL(kFakeUrl), non_empty_context,
...@@ -157,13 +159,10 @@ TEST_F(LiteServiceTest, GetActionsOnlyFetchesFromScriptPath) { ...@@ -157,13 +159,10 @@ TEST_F(LiteServiceTest, GetActionsOnlyFetchesFromScriptPath) {
TEST_F(LiteServiceTest, StopsOnGetActionsFailed) { TEST_F(LiteServiceTest, StopsOnGetActionsFailed) {
ExpectStopWithFinishedState( ExpectStopWithFinishedState(
Metrics::LiteScriptFinishedState::LITE_SCRIPT_GET_ACTIONS_FAILED); Metrics::LiteScriptFinishedState::LITE_SCRIPT_GET_ACTIONS_FAILED);
EXPECT_CALL(*mock_native_service_, EXPECT_CALL(*mock_request_sender_,
OnGetActions(kFakeScriptPath, _, _, _, _, _)) OnSendRequest(GURL(kGetActionsServerUrl), _, _))
.Times(1) .Times(1)
.WillOnce([&](const std::string& script_path, const GURL& url, .WillOnce([&](const GURL& url, const std::string& request_body,
const TriggerContext& trigger_context,
const std::string& global_payload,
const std::string& script_payload,
Service::ResponseCallback& callback) { Service::ResponseCallback& callback) {
std::move(callback).Run(net::HTTP_UNAUTHORIZED, std::string()); std::move(callback).Run(net::HTTP_UNAUTHORIZED, std::string());
}); });
...@@ -177,13 +176,10 @@ TEST_F(LiteServiceTest, StopsOnGetActionsFailed) { ...@@ -177,13 +176,10 @@ TEST_F(LiteServiceTest, StopsOnGetActionsFailed) {
TEST_F(LiteServiceTest, StopsOnGetActionsParsingError) { TEST_F(LiteServiceTest, StopsOnGetActionsParsingError) {
ExpectStopWithFinishedState( ExpectStopWithFinishedState(
Metrics::LiteScriptFinishedState::LITE_SCRIPT_GET_ACTIONS_PARSE_ERROR); Metrics::LiteScriptFinishedState::LITE_SCRIPT_GET_ACTIONS_PARSE_ERROR);
EXPECT_CALL(*mock_native_service_, EXPECT_CALL(*mock_request_sender_,
OnGetActions(kFakeScriptPath, _, _, _, _, _)) OnSendRequest(GURL(kGetActionsServerUrl), _, _))
.Times(1) .Times(1)
.WillOnce([&](const std::string& script_path, const GURL& url, .WillOnce([&](const GURL& url, const std::string& request_body,
const TriggerContext& trigger_context,
const std::string& global_payload,
const std::string& script_payload,
Service::ResponseCallback& callback) { Service::ResponseCallback& callback) {
std::move(callback).Run(net::HTTP_OK, std::string("invalid proto")); std::move(callback).Run(net::HTTP_OK, std::string("invalid proto"));
}); });
......
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