blob: efd14254323ed1584370f5112dae2963df1f9fd0 [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2019 The Chromium Authors
Yuwei Huang5a84f9532019-02-20 18:29:012// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yuwei Huang2687b47f2019-04-01 20:57:085#include "remoting/test/ftl_services_playground.h"
Yuwei Huangbdc4a972019-03-01 17:43:066
Yuwei Huang30bac9e2019-03-01 21:28:147#include <inttypes.h>
Yuwei Huangaae881f2019-03-01 23:10:278#include <string>
Yuwei Huangbdc4a972019-03-01 17:43:069#include <utility>
Yuwei Huang98655b92019-03-04 23:41:4110#include <vector>
Yuwei Huangbdc4a972019-03-01 17:43:0611
Yuwei Huangaae881f2019-03-01 23:10:2712#include "base/base64.h"
Yuwei Huang5a84f9532019-02-20 18:29:0113#include "base/command_line.h"
Yuwei Huangbdc4a972019-03-01 17:43:0614#include "base/files/file_path.h"
Avi Drissman135261e2023-01-11 22:43:1515#include "base/functional/bind.h"
16#include "base/functional/callback_helpers.h"
Yuwei Huangaae881f2019-03-01 23:10:2717#include "base/guid.h"
Yuwei Huang5a84f9532019-02-20 18:29:0118#include "base/logging.h"
Yuwei Huangbdc4a972019-03-01 17:43:0619#include "base/path_service.h"
Yuwei Huang22e0b562019-03-27 20:54:2920#include "base/run_loop.h"
Sean Mahere672a662023-01-09 21:42:2821#include "base/task/single_thread_task_runner.h"
Yuwei Huangbdc4a972019-03-01 17:43:0622#include "remoting/base/oauth_token_getter_impl.h"
Yuwei Huangfeddf232020-07-28 00:27:4923#include "remoting/base/protobuf_http_status.h"
24#include "remoting/base/url_request_context_getter.h"
Yuwei Huang76ce10d2020-07-30 19:59:3225#include "remoting/proto/ftl/v1/ftl_messages.pb.h"
Yuwei Huang41ddec52019-04-02 02:08:1926#include "remoting/test/cli_util.h"
Yuwei Huang1cf68062019-04-11 23:28:4227#include "remoting/test/test_device_id_provider.h"
Yuwei Huang41ddec52019-04-02 02:08:1928#include "remoting/test/test_oauth_token_getter.h"
Yuwei Huang946cebd42019-03-04 22:14:2029#include "remoting/test/test_token_storage.h"
Yuwei Huangfeddf232020-07-28 00:27:4930#include "services/network/public/cpp/shared_url_loader_factory.h"
31#include "services/network/transitional_url_loader_factory_owner.h"
Yuwei Huangbdc4a972019-03-01 17:43:0632
33namespace {
34
35constexpr char kSwitchNameHelp[] = "help";
Yuwei Huang946cebd42019-03-04 22:14:2036constexpr char kSwitchNameUsername[] = "username";
37constexpr char kSwitchNameStoragePath[] = "storage-path";
Yuwei Huangfce958772019-03-30 00:18:1138constexpr char kSwitchNameNoAutoSignin[] = "no-auto-signin";
Yuwei Huangbdc4a972019-03-01 17:43:0639
Yuwei Huangfce958772019-03-30 00:18:1140bool NeedsManualSignin() {
41 return base::CommandLine::ForCurrentProcess()->HasSwitch(
42 kSwitchNameNoAutoSignin);
Yuwei Huangb8e1f2d2019-03-22 19:40:2643}
44
Yuwei Huangbdc4a972019-03-01 17:43:0645} // namespace
Yuwei Huang5a84f9532019-02-20 18:29:0146
Yuwei Huang575c32ee2019-02-27 04:41:5647namespace remoting {
Yuwei Huang5a84f9532019-02-20 18:29:0148
Jeremy Roman7c5cfabd2019-08-12 15:45:2749FtlServicesPlayground::FtlServicesPlayground() {}
Yuwei Huang575c32ee2019-02-27 04:41:5650
Yuwei Huang2687b47f2019-04-01 20:57:0851FtlServicesPlayground::~FtlServicesPlayground() = default;
Yuwei Huang575c32ee2019-02-27 04:41:5652
Yuwei Huang2687b47f2019-04-01 20:57:0853bool FtlServicesPlayground::ShouldPrintHelp() {
Yuwei Huangbdc4a972019-03-01 17:43:0654 return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp);
55}
56
Yuwei Huang2687b47f2019-04-01 20:57:0857void FtlServicesPlayground::PrintHelp() {
Yuwei Huang946cebd42019-03-04 22:14:2058 printf(
Yuwei Huang41ddec52019-04-02 02:08:1959 "Usage: %s [--no-auto-signin] [--auth-code=<auth-code>] "
Yuwei Huangfce958772019-03-30 00:18:1160 "[--storage-path=<storage-path>] [--username=<[email protected]>]\n",
Yuwei Huang946cebd42019-03-04 22:14:2061 base::CommandLine::ForCurrentProcess()
62 ->GetProgram()
63 .MaybeAsASCII()
64 .c_str());
Yuwei Huangbdc4a972019-03-01 17:43:0665}
66
Yuwei Huang2687b47f2019-04-01 20:57:0867void FtlServicesPlayground::StartAndAuthenticate() {
Yuwei Huang946cebd42019-03-04 22:14:2068 DCHECK(!storage_);
Yuwei Huangbdc4a972019-03-01 17:43:0669 DCHECK(!token_getter_);
Yuwei Huangbdc4a972019-03-01 17:43:0670
Yuwei Huang946cebd42019-03-04 22:14:2071 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
72 std::string username = cmd_line->GetSwitchValueASCII(kSwitchNameUsername);
73 base::FilePath storage_path =
74 cmd_line->GetSwitchValuePath(kSwitchNameStoragePath);
75 storage_ = test::TestTokenStorage::OnDisk(username, storage_path);
76
Yuwei Huang41ddec52019-04-02 02:08:1977 token_getter_ = std::make_unique<test::TestOAuthTokenGetter>(storage_.get());
78
Yuwei Huangfce958772019-03-30 00:18:1179 base::RunLoop run_loop;
Yuwei Huang41ddec52019-04-02 02:08:1980 token_getter_->Initialize(
81 base::BindOnce(&FtlServicesPlayground::ResetServices,
82 weak_factory_.GetWeakPtr(), run_loop.QuitClosure()));
Yuwei Huangfce958772019-03-30 00:18:1183 run_loop.Run();
Yuwei Huangaae881f2019-03-01 23:10:2784
85 StartLoop();
86}
87
Yuwei Huang2687b47f2019-04-01 20:57:0888void FtlServicesPlayground::StartLoop() {
Yuwei Huang41ddec52019-04-02 02:08:1989 std::vector<test::CommandOption> options{
Yuwei Huang41ddec52019-04-02 02:08:1990 {"ReceiveMessages",
91 base::BindRepeating(&FtlServicesPlayground::StartReceivingMessages,
92 weak_factory_.GetWeakPtr())},
93 {"SendMessage", base::BindRepeating(&FtlServicesPlayground::SendMessage,
94 weak_factory_.GetWeakPtr())}};
Yuwei Huangfce958772019-03-30 00:18:1195
Yuwei Huang41ddec52019-04-02 02:08:1996 if (NeedsManualSignin()) {
97 options.insert(
98 options.begin(),
99 {"SignInGaia", base::BindRepeating(&FtlServicesPlayground::SignInGaia,
100 weak_factory_.GetWeakPtr())});
Yuwei Huangaae881f2019-03-01 23:10:27101 }
Yuwei Huang41ddec52019-04-02 02:08:19102
Yuwei Huangfeddf232020-07-28 00:27:49103 auto url_request_context_getter =
104 base::MakeRefCounted<URLRequestContextGetter>(
Sean Maher5b9af51f2022-11-21 15:32:47105 base::SingleThreadTaskRunner::GetCurrentDefault());
Yuwei Huangfeddf232020-07-28 00:27:49106 url_loader_factory_owner_ =
107 std::make_unique<network::TransitionalURLLoaderFactoryOwner>(
108 url_request_context_getter);
109
Yuwei Huang41ddec52019-04-02 02:08:19110 test::RunCommandOptionsLoop(options);
Yuwei Huangbdc4a972019-03-01 17:43:06111}
112
Yuwei Huang2687b47f2019-04-01 20:57:08113void FtlServicesPlayground::ResetServices(base::OnceClosure on_done) {
Yuwei Huang82c01892019-03-28 20:39:40114 registration_manager_ = std::make_unique<FtlRegistrationManager>(
Yuwei Huangfeddf232020-07-28 00:27:49115 token_getter_.get(), url_loader_factory_owner_->GetURLLoaderFactory(),
Yuwei Huang1cf68062019-04-11 23:28:42116 std::make_unique<test::TestDeviceIdProvider>(storage_.get()));
Yuwei Huang968994cb2019-03-21 00:55:27117
Peter Kasting7ba9440c2020-11-22 01:49:02118 message_subscription_ = {};
Yuwei Huang82c01892019-03-28 20:39:40119 messaging_client_ = std::make_unique<FtlMessagingClient>(
Yuwei Huang76ce10d2020-07-30 19:59:32120 token_getter_.get(), url_loader_factory_owner_->GetURLLoaderFactory(),
Yuwei Huangfebdea62020-09-16 21:55:58121 registration_manager_.get());
Yuwei Huang968994cb2019-03-21 00:55:27122 message_subscription_ = messaging_client_->RegisterMessageCallback(
Yuwei Huang2687b47f2019-04-01 20:57:08123 base::BindRepeating(&FtlServicesPlayground::OnMessageReceived,
Yuwei Huang968994cb2019-03-21 00:55:27124 weak_factory_.GetWeakPtr()));
Yuwei Huangfce958772019-03-30 00:18:11125
126 if (NeedsManualSignin()) {
127 std::move(on_done).Run();
128 } else {
129 SignInGaia(std::move(on_done));
130 }
Yuwei Huang3f1fcd362019-03-11 20:27:17131}
132
Yuwei Huang2687b47f2019-04-01 20:57:08133void FtlServicesPlayground::SignInGaia(base::OnceClosure on_done) {
Yuwei Huange5b94452019-03-23 00:26:05134 DCHECK(registration_manager_);
Yuwei Huangaae881f2019-03-01 23:10:27135 VLOG(0) << "Running SignInGaia...";
Yuwei Huange5b94452019-03-23 00:26:05136 registration_manager_->SignInGaia(
Yuwei Huang2687b47f2019-04-01 20:57:08137 base::BindOnce(&FtlServicesPlayground::OnSignInGaiaResponse,
Yuwei Huang946cebd42019-03-04 22:14:20138 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huangaae881f2019-03-01 23:10:27139}
140
Yuwei Huangfeddf232020-07-28 00:27:49141void FtlServicesPlayground::OnSignInGaiaResponse(
142 base::OnceClosure on_done,
143 const ProtobufHttpStatus& status) {
Yuwei Huangfce958772019-03-30 00:18:11144 if (!status.ok()) {
Yuwei Huang76ce10d2020-07-30 19:59:32145 HandleStatusError(std::move(on_done), status);
Yuwei Huangfce958772019-03-30 00:18:11146 return;
Yuwei Huang98655b92019-03-04 23:41:41147 }
Yuwei Huangfce958772019-03-30 00:18:11148
149 std::string registration_id_base64;
150 base::Base64Encode(registration_manager_->GetRegistrationId(),
151 &registration_id_base64);
152 printf("Service signed in. registration_id(base64)=%s\n",
153 registration_id_base64.c_str());
Yuwei Huang98655b92019-03-04 23:41:41154 std::move(on_done).Run();
155}
156
Yuwei Huang2687b47f2019-04-01 20:57:08157void FtlServicesPlayground::SendMessage(base::OnceClosure on_done) {
Yuwei Huang4de60092019-03-22 21:19:44158 DCHECK(messaging_client_);
159 VLOG(0) << "Running SendMessage...";
160
161 printf("Receiver ID: ");
Yuwei Huang41ddec52019-04-02 02:08:19162 std::string receiver_id = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44163
164 printf("Receiver registration ID (base64, optional): ");
Yuwei Huang41ddec52019-04-02 02:08:19165 std::string registration_id_base64 = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44166
167 std::string registration_id;
168 bool success = base::Base64Decode(registration_id_base64, &registration_id);
169 if (!success) {
170 fprintf(stderr, "Your input can't be base64 decoded.\n");
171 std::move(on_done).Run();
172 return;
173 }
174 DoSendMessage(receiver_id, registration_id, std::move(on_done), true);
175}
176
Yuwei Huang2687b47f2019-04-01 20:57:08177void FtlServicesPlayground::DoSendMessage(const std::string& receiver_id,
178 const std::string& registration_id,
179 base::OnceClosure on_done,
180 bool should_keep_running) {
Yuwei Huang4de60092019-03-22 21:19:44181 if (!should_keep_running) {
182 std::move(on_done).Run();
183 return;
184 }
185
186 printf("Message (enter nothing to quit): ");
Yuwei Huang41ddec52019-04-02 02:08:19187 std::string message = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44188
189 if (message.empty()) {
190 std::move(on_done).Run();
191 return;
192 }
193
Yuwei Huang2687b47f2019-04-01 20:57:08194 auto on_continue = base::BindOnce(&FtlServicesPlayground::DoSendMessage,
Yuwei Huang4de60092019-03-22 21:19:44195 weak_factory_.GetWeakPtr(), receiver_id,
196 registration_id, std::move(on_done));
197
Yuwei Huang4c919d142019-04-05 17:05:49198 ftl::ChromotingMessage crd_message;
199 crd_message.mutable_xmpp()->set_stanza(message);
Yuwei Huang4de60092019-03-22 21:19:44200 messaging_client_->SendMessage(
Yuwei Huang4c919d142019-04-05 17:05:49201 receiver_id, registration_id, crd_message,
Yuwei Huang2687b47f2019-04-01 20:57:08202 base::BindOnce(&FtlServicesPlayground::OnSendMessageResponse,
Yuwei Huang4de60092019-03-22 21:19:44203 weak_factory_.GetWeakPtr(), std::move(on_continue)));
204}
205
Yuwei Huang2687b47f2019-04-01 20:57:08206void FtlServicesPlayground::OnSendMessageResponse(
Yuwei Huang4de60092019-03-22 21:19:44207 base::OnceCallback<void(bool)> on_continue,
Yuwei Huang76ce10d2020-07-30 19:59:32208 const ProtobufHttpStatus& status) {
Yuwei Huang4de60092019-03-22 21:19:44209 if (!status.ok()) {
Yuwei Huang76ce10d2020-07-30 19:59:32210 HandleStatusError(base::BindOnce(std::move(on_continue), false), status);
Yuwei Huangfce958772019-03-30 00:18:11211 return;
Yuwei Huang4de60092019-03-22 21:19:44212 }
Yuwei Huangfce958772019-03-30 00:18:11213
214 printf("Message successfully sent.\n");
215 std::move(on_continue).Run(true);
Yuwei Huang4de60092019-03-22 21:19:44216}
217
Yuwei Huang2687b47f2019-04-01 20:57:08218void FtlServicesPlayground::StartReceivingMessages(base::OnceClosure on_done) {
Yuwei Huangb8e1f2d2019-03-22 19:40:26219 VLOG(0) << "Running StartReceivingMessages...";
Yuwei Huangce344e32019-04-05 19:31:29220 receive_messages_done_callback_ = std::move(on_done);
Yuwei Huangb8e1f2d2019-03-22 19:40:26221 messaging_client_->StartReceivingMessages(
Yuwei Huangce344e32019-04-05 19:31:29222 base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamReady,
223 weak_factory_.GetWeakPtr()),
224 base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamClosed,
225 weak_factory_.GetWeakPtr()));
Yuwei Huangb8e1f2d2019-03-22 19:40:26226}
227
Yuwei Huang2687b47f2019-04-01 20:57:08228void FtlServicesPlayground::StopReceivingMessages(base::OnceClosure on_done) {
Yuwei Huangb8e1f2d2019-03-22 19:40:26229 messaging_client_->StopReceivingMessages();
230 std::move(on_done).Run();
231}
232
Yuwei Huangc9bb46f2019-04-04 02:19:00233void FtlServicesPlayground::OnMessageReceived(
Yuwei Huang4ef50e732019-05-30 04:30:25234 const ftl::Id& sender_id,
Yuwei Huangc9bb46f2019-04-04 02:19:00235 const std::string& sender_registration_id,
Yuwei Huang4c919d142019-04-05 17:05:49236 const ftl::ChromotingMessage& message) {
237 std::string message_text = message.xmpp().stanza();
Yuwei Huang968994cb2019-03-21 00:55:27238 printf(
239 "Received message:\n"
240 " Sender ID=%s\n"
Yuwei Huangc9bb46f2019-04-04 02:19:00241 " Sender Registration ID=%s\n"
Yuwei Huang968994cb2019-03-21 00:55:27242 " Message=%s\n",
Yuwei Huang4ef50e732019-05-30 04:30:25243 sender_id.id().c_str(), sender_registration_id.c_str(),
244 message_text.c_str());
Yuwei Huang968994cb2019-03-21 00:55:27245}
246
Yuwei Huangce344e32019-04-05 19:31:29247void FtlServicesPlayground::OnReceiveMessagesStreamReady() {
248 printf("Started receiving messages. Press enter to stop streaming...\n");
249 test::WaitForEnterKey(base::BindOnce(
250 &FtlServicesPlayground::StopReceivingMessages, weak_factory_.GetWeakPtr(),
251 std::move(receive_messages_done_callback_)));
252}
253
254void FtlServicesPlayground::OnReceiveMessagesStreamClosed(
Yuwei Huang76ce10d2020-07-30 19:59:32255 const ProtobufHttpStatus& status) {
Yuwei Huangce344e32019-04-05 19:31:29256 base::OnceClosure callback = std::move(receive_messages_done_callback_);
257 bool is_callback_null = callback.is_null();
258 if (is_callback_null) {
Daniel Chenge0fc4632021-10-01 00:37:41259 callback = base::DoNothing();
Yuwei Huangce344e32019-04-05 19:31:29260 }
Yuwei Huang76ce10d2020-07-30 19:59:32261 if (status.error_code() == ProtobufHttpStatus::Code::CANCELLED) {
Yuwei Huangb8e1f2d2019-03-22 19:40:26262 printf("ReceiveMessages stream canceled by client.\n");
Yuwei Huangce344e32019-04-05 19:31:29263 std::move(callback).Run();
Yuwei Huangb8e1f2d2019-03-22 19:40:26264 return;
265 }
Yuwei Huangfce958772019-03-30 00:18:11266
Yuwei Huangb8e1f2d2019-03-22 19:40:26267 if (!status.ok()) {
Yuwei Huang76ce10d2020-07-30 19:59:32268 HandleStatusError(std::move(callback), status);
Yuwei Huangce344e32019-04-05 19:31:29269 } else {
270 printf("Stream closed by server.\n");
271 std::move(callback).Run();
Yuwei Huangb8e1f2d2019-03-22 19:40:26272 }
Yuwei Huangce344e32019-04-05 19:31:29273
274 if (is_callback_null) {
275 // Stream had been started and callback has been passed to wait for the
276 // enter key.
277 printf("Please press enter to continue...\n");
278 }
Yuwei Huangb8e1f2d2019-03-22 19:40:26279}
280
Yuwei Huang76ce10d2020-07-30 19:59:32281void FtlServicesPlayground::HandleStatusError(
282 base::OnceClosure on_done,
283 const ProtobufHttpStatus& status) {
Yuwei Huangfce958772019-03-30 00:18:11284 DCHECK(!status.ok());
Yuwei Huang76ce10d2020-07-30 19:59:32285 if (status.error_code() == ProtobufHttpStatus::Code::UNAUTHENTICATED) {
Yuwei Huangfce958772019-03-30 00:18:11286 if (NeedsManualSignin()) {
287 printf(
288 "Request is unauthenticated. You should run SignInGaia first if "
289 "you haven't done so, otherwise your OAuth token might be expired. \n"
290 "Request for new OAuth token? [y/N]: ");
Yuwei Huangdc86bdf42019-04-18 19:14:10291 if (!test::ReadYNBool()) {
Yuwei Huangfce958772019-03-30 00:18:11292 std::move(on_done).Run();
293 return;
294 }
295 }
Yuwei Huang76ce10d2020-07-30 19:59:32296 VLOG(0) << "Request failed to authenticate. "
Yuwei Huangfce958772019-03-30 00:18:11297 << "Trying to reauthenticate...";
Yuwei Huang41ddec52019-04-02 02:08:19298 token_getter_->ResetWithAuthenticationFlow(
299 base::BindOnce(&FtlServicesPlayground::ResetServices,
300 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huangfce958772019-03-30 00:18:11301 return;
302 }
303
304 fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(),
305 status.error_message().c_str());
306 std::move(on_done).Run();
307}
308
Yuwei Huang575c32ee2019-02-27 04:41:56309} // namespace remoting