blob: 18d5371e29cf258bf5e33b24496d4465ef9b1629 [file] [log] [blame]
Yuwei Huang5a84f9532019-02-20 18:29:011// Copyright 2019 The Chromium Authors. All rights reserved.
2// 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 Huang575c32ee2019-02-27 04:41:5613#include "base/bind.h"
Yuwei Huangbdc4a972019-03-01 17:43:0614#include "base/bind_helpers.h"
Yuwei Huang5a84f9532019-02-20 18:29:0115#include "base/command_line.h"
Yuwei Huangbdc4a972019-03-01 17:43:0616#include "base/files/file_path.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"
Yuwei Huangb8e1f2d2019-03-22 19:40:2621#include "base/task/post_task.h"
Yuwei Huangbdc4a972019-03-01 17:43:0622#include "remoting/base/oauth_token_getter_impl.h"
Yuwei Huange5b94452019-03-23 00:26:0523#include "remoting/signaling/ftl_device_id_provider.h"
Yuwei Huang82c01892019-03-28 20:39:4024#include "remoting/signaling/ftl_grpc_context.h"
Yuwei Huang3f1fcd362019-03-11 20:27:1725#include "remoting/signaling/ftl_services.grpc.pb.h"
Yuwei Huang82c01892019-03-28 20:39:4026#include "remoting/signaling/grpc_support/grpc_async_unary_request.h"
Yuwei Huang41ddec52019-04-02 02:08:1927#include "remoting/test/cli_util.h"
28#include "remoting/test/test_oauth_token_getter.h"
Yuwei Huang946cebd42019-03-04 22:14:2029#include "remoting/test/test_token_storage.h"
Yuwei Huangbdc4a972019-03-01 17:43:0630
31namespace {
32
33constexpr char kSwitchNameHelp[] = "help";
Yuwei Huang946cebd42019-03-04 22:14:2034constexpr char kSwitchNameUsername[] = "username";
35constexpr char kSwitchNameStoragePath[] = "storage-path";
Yuwei Huangfce958772019-03-30 00:18:1136constexpr char kSwitchNameNoAutoSignin[] = "no-auto-signin";
Yuwei Huangbdc4a972019-03-01 17:43:0637
Yuwei Huangfce958772019-03-30 00:18:1138bool NeedsManualSignin() {
39 return base::CommandLine::ForCurrentProcess()->HasSwitch(
40 kSwitchNameNoAutoSignin);
Yuwei Huangb8e1f2d2019-03-22 19:40:2641}
42
Yuwei Huangbdc4a972019-03-01 17:43:0643} // namespace
Yuwei Huang5a84f9532019-02-20 18:29:0144
Yuwei Huang575c32ee2019-02-27 04:41:5645namespace remoting {
Yuwei Huang5a84f9532019-02-20 18:29:0146
Yuwei Huang2687b47f2019-04-01 20:57:0847FtlServicesPlayground::FtlServicesPlayground() : weak_factory_(this) {}
Yuwei Huang575c32ee2019-02-27 04:41:5648
Yuwei Huang2687b47f2019-04-01 20:57:0849FtlServicesPlayground::~FtlServicesPlayground() = default;
Yuwei Huang575c32ee2019-02-27 04:41:5650
Yuwei Huang2687b47f2019-04-01 20:57:0851bool FtlServicesPlayground::ShouldPrintHelp() {
Yuwei Huangbdc4a972019-03-01 17:43:0652 return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp);
53}
54
Yuwei Huang2687b47f2019-04-01 20:57:0855void FtlServicesPlayground::PrintHelp() {
Yuwei Huang946cebd42019-03-04 22:14:2056 printf(
Yuwei Huang41ddec52019-04-02 02:08:1957 "Usage: %s [--no-auto-signin] [--auth-code=<auth-code>] "
Yuwei Huangfce958772019-03-30 00:18:1158 "[--storage-path=<storage-path>] [--username=<[email protected]>]\n",
Yuwei Huang946cebd42019-03-04 22:14:2059 base::CommandLine::ForCurrentProcess()
60 ->GetProgram()
61 .MaybeAsASCII()
62 .c_str());
Yuwei Huangbdc4a972019-03-01 17:43:0663}
64
Yuwei Huang2687b47f2019-04-01 20:57:0865void FtlServicesPlayground::StartAndAuthenticate() {
Yuwei Huang946cebd42019-03-04 22:14:2066 DCHECK(!storage_);
Yuwei Huangbdc4a972019-03-01 17:43:0667 DCHECK(!token_getter_);
Yuwei Huang82c01892019-03-28 20:39:4068 DCHECK(!executor_);
Yuwei Huangbdc4a972019-03-01 17:43:0669
Yuwei Huang946cebd42019-03-04 22:14:2070 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
71 std::string username = cmd_line->GetSwitchValueASCII(kSwitchNameUsername);
72 base::FilePath storage_path =
73 cmd_line->GetSwitchValuePath(kSwitchNameStoragePath);
74 storage_ = test::TestTokenStorage::OnDisk(username, storage_path);
75
Yuwei Huang41ddec52019-04-02 02:08:1976 token_getter_ = std::make_unique<test::TestOAuthTokenGetter>(storage_.get());
77
Yuwei Huangfce958772019-03-30 00:18:1178 base::RunLoop run_loop;
Yuwei Huang41ddec52019-04-02 02:08:1979 token_getter_->Initialize(
80 base::BindOnce(&FtlServicesPlayground::ResetServices,
81 weak_factory_.GetWeakPtr(), run_loop.QuitClosure()));
Yuwei Huangfce958772019-03-30 00:18:1182 run_loop.Run();
Yuwei Huangaae881f2019-03-01 23:10:2783
84 StartLoop();
85}
86
Yuwei Huang2687b47f2019-04-01 20:57:0887void FtlServicesPlayground::StartLoop() {
Yuwei Huang41ddec52019-04-02 02:08:1988 std::vector<test::CommandOption> options{
89 {"GetIceServer", base::BindRepeating(&FtlServicesPlayground::GetIceServer,
90 weak_factory_.GetWeakPtr())},
91 {"PullMessages", base::BindRepeating(&FtlServicesPlayground::PullMessages,
92 weak_factory_.GetWeakPtr())},
93 {"ReceiveMessages",
94 base::BindRepeating(&FtlServicesPlayground::StartReceivingMessages,
95 weak_factory_.GetWeakPtr())},
96 {"SendMessage", base::BindRepeating(&FtlServicesPlayground::SendMessage,
97 weak_factory_.GetWeakPtr())}};
Yuwei Huangfce958772019-03-30 00:18:1198
Yuwei Huang41ddec52019-04-02 02:08:1999 if (NeedsManualSignin()) {
100 options.insert(
101 options.begin(),
102 {"SignInGaia", base::BindRepeating(&FtlServicesPlayground::SignInGaia,
103 weak_factory_.GetWeakPtr())});
Yuwei Huangaae881f2019-03-01 23:10:27104 }
Yuwei Huang41ddec52019-04-02 02:08:19105
106 test::RunCommandOptionsLoop(options);
Yuwei Huangbdc4a972019-03-01 17:43:06107}
108
Yuwei Huang2687b47f2019-04-01 20:57:08109void FtlServicesPlayground::ResetServices(base::OnceClosure on_done) {
Yuwei Huang82c01892019-03-28 20:39:40110 executor_ = std::make_unique<GrpcAuthenticatedExecutor>(token_getter_.get());
111 peer_to_peer_stub_ = PeerToPeer::NewStub(FtlGrpcContext::CreateChannel());
112
113 registration_manager_ = std::make_unique<FtlRegistrationManager>(
114 token_getter_.get(),
115 std::make_unique<FtlDeviceIdProvider>(storage_.get()));
Yuwei Huang968994cb2019-03-21 00:55:27116
117 message_subscription_.reset();
Yuwei Huang82c01892019-03-28 20:39:40118 messaging_client_ = std::make_unique<FtlMessagingClient>(
119 token_getter_.get(), registration_manager_.get());
Yuwei Huang968994cb2019-03-21 00:55:27120 message_subscription_ = messaging_client_->RegisterMessageCallback(
Yuwei Huang2687b47f2019-04-01 20:57:08121 base::BindRepeating(&FtlServicesPlayground::OnMessageReceived,
Yuwei Huang968994cb2019-03-21 00:55:27122 weak_factory_.GetWeakPtr()));
Yuwei Huangfce958772019-03-30 00:18:11123
124 if (NeedsManualSignin()) {
125 std::move(on_done).Run();
126 } else {
127 SignInGaia(std::move(on_done));
128 }
Yuwei Huang3f1fcd362019-03-11 20:27:17129}
130
Yuwei Huang2687b47f2019-04-01 20:57:08131void FtlServicesPlayground::GetIceServer(base::OnceClosure on_done) {
Yuwei Huang3f1fcd362019-03-11 20:27:17132 DCHECK(peer_to_peer_stub_);
133 VLOG(0) << "Running GetIceServer...";
Yuwei Huang82c01892019-03-28 20:39:40134 ftl::GetICEServerRequest request;
135 *request.mutable_header() = FtlGrpcContext::CreateRequestHeader();
136 auto grpc_request = CreateGrpcAsyncUnaryRequest(
Yuwei Huang3f1fcd362019-03-11 20:27:17137 base::BindOnce(&PeerToPeer::Stub::AsyncGetICEServer,
138 base::Unretained(peer_to_peer_stub_.get())),
Yuwei Huang82c01892019-03-28 20:39:40139 FtlGrpcContext::CreateClientContext(), request,
Yuwei Huang2687b47f2019-04-01 20:57:08140 base::BindOnce(&FtlServicesPlayground::OnGetIceServerResponse,
Yuwei Huang946cebd42019-03-04 22:14:20141 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huang82c01892019-03-28 20:39:40142 executor_->ExecuteRpc(std::move(grpc_request));
Yuwei Huang575c32ee2019-02-27 04:41:56143}
144
Yuwei Huang2687b47f2019-04-01 20:57:08145void FtlServicesPlayground::OnGetIceServerResponse(
Yuwei Huang575c32ee2019-02-27 04:41:56146 base::OnceClosure on_done,
Yuwei Huang81bbc1a2019-03-08 20:46:06147 const grpc::Status& status,
Yuwei Huang575c32ee2019-02-27 04:41:56148 const ftl::GetICEServerResponse& response) {
Yuwei Huangfce958772019-03-30 00:18:11149 if (!status.ok()) {
150 HandleGrpcStatusError(std::move(on_done), status);
151 return;
152 }
153
154 printf("Ice transport policy: %s\n",
155 response.ice_config().ice_transport_policy().c_str());
156 for (const ftl::ICEServerList& server : response.ice_config().ice_servers()) {
157 printf(
158 "ICE server:\n"
159 " hostname=%s\n"
160 " username=%s\n"
161 " credential=%s\n"
162 " max_rate_kbps=%" PRId64 "\n",
163 server.hostname().c_str(), server.username().c_str(),
164 server.credential().c_str(), server.max_rate_kbps());
165 for (const std::string& url : server.urls()) {
166 printf(" url=%s\n", url.c_str());
Yuwei Huang575c32ee2019-02-27 04:41:56167 }
Yuwei Huangaae881f2019-03-01 23:10:27168 }
169 std::move(on_done).Run();
170}
171
Yuwei Huang2687b47f2019-04-01 20:57:08172void FtlServicesPlayground::SignInGaia(base::OnceClosure on_done) {
Yuwei Huange5b94452019-03-23 00:26:05173 DCHECK(registration_manager_);
Yuwei Huangaae881f2019-03-01 23:10:27174 VLOG(0) << "Running SignInGaia...";
Yuwei Huange5b94452019-03-23 00:26:05175 registration_manager_->SignInGaia(
Yuwei Huang2687b47f2019-04-01 20:57:08176 base::BindOnce(&FtlServicesPlayground::OnSignInGaiaResponse,
Yuwei Huang946cebd42019-03-04 22:14:20177 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huangaae881f2019-03-01 23:10:27178}
179
Yuwei Huang2687b47f2019-04-01 20:57:08180void FtlServicesPlayground::OnSignInGaiaResponse(base::OnceClosure on_done,
181 const grpc::Status& status) {
Yuwei Huangfce958772019-03-30 00:18:11182 if (!status.ok()) {
183 HandleGrpcStatusError(std::move(on_done), status);
184 return;
Yuwei Huang98655b92019-03-04 23:41:41185 }
Yuwei Huangfce958772019-03-30 00:18:11186
187 std::string registration_id_base64;
188 base::Base64Encode(registration_manager_->GetRegistrationId(),
189 &registration_id_base64);
190 printf("Service signed in. registration_id(base64)=%s\n",
191 registration_id_base64.c_str());
Yuwei Huang98655b92019-03-04 23:41:41192 std::move(on_done).Run();
193}
194
Yuwei Huang2687b47f2019-04-01 20:57:08195void FtlServicesPlayground::PullMessages(base::OnceClosure on_done) {
Yuwei Huang968994cb2019-03-21 00:55:27196 DCHECK(messaging_client_);
Yuwei Huang98655b92019-03-04 23:41:41197 VLOG(0) << "Running PullMessages...";
Yuwei Huang3f1fcd362019-03-11 20:27:17198
Yuwei Huang968994cb2019-03-21 00:55:27199 messaging_client_->PullMessages(
Yuwei Huang2687b47f2019-04-01 20:57:08200 base::BindOnce(&FtlServicesPlayground::OnPullMessagesResponse,
Yuwei Huang98655b92019-03-04 23:41:41201 weak_factory_.GetWeakPtr(), std::move(on_done)));
202}
203
Yuwei Huang2687b47f2019-04-01 20:57:08204void FtlServicesPlayground::OnPullMessagesResponse(base::OnceClosure on_done,
205 const grpc::Status& status) {
Yuwei Huang98655b92019-03-04 23:41:41206 if (!status.ok()) {
Yuwei Huangfce958772019-03-30 00:18:11207 HandleGrpcStatusError(std::move(on_done), status);
208 return;
Yuwei Huang5a84f9532019-02-20 18:29:01209 }
Yuwei Huang575c32ee2019-02-27 04:41:56210 std::move(on_done).Run();
211}
Yuwei Huang5a84f9532019-02-20 18:29:01212
Yuwei Huang2687b47f2019-04-01 20:57:08213void FtlServicesPlayground::SendMessage(base::OnceClosure on_done) {
Yuwei Huang4de60092019-03-22 21:19:44214 DCHECK(messaging_client_);
215 VLOG(0) << "Running SendMessage...";
216
217 printf("Receiver ID: ");
Yuwei Huang41ddec52019-04-02 02:08:19218 std::string receiver_id = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44219
220 printf("Receiver registration ID (base64, optional): ");
Yuwei Huang41ddec52019-04-02 02:08:19221 std::string registration_id_base64 = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44222
223 std::string registration_id;
224 bool success = base::Base64Decode(registration_id_base64, &registration_id);
225 if (!success) {
226 fprintf(stderr, "Your input can't be base64 decoded.\n");
227 std::move(on_done).Run();
228 return;
229 }
230 DoSendMessage(receiver_id, registration_id, std::move(on_done), true);
231}
232
Yuwei Huang2687b47f2019-04-01 20:57:08233void FtlServicesPlayground::DoSendMessage(const std::string& receiver_id,
234 const std::string& registration_id,
235 base::OnceClosure on_done,
236 bool should_keep_running) {
Yuwei Huang4de60092019-03-22 21:19:44237 if (!should_keep_running) {
238 std::move(on_done).Run();
239 return;
240 }
241
242 printf("Message (enter nothing to quit): ");
Yuwei Huang41ddec52019-04-02 02:08:19243 std::string message = test::ReadString();
Yuwei Huang4de60092019-03-22 21:19:44244
245 if (message.empty()) {
246 std::move(on_done).Run();
247 return;
248 }
249
Yuwei Huang2687b47f2019-04-01 20:57:08250 auto on_continue = base::BindOnce(&FtlServicesPlayground::DoSendMessage,
Yuwei Huang4de60092019-03-22 21:19:44251 weak_factory_.GetWeakPtr(), receiver_id,
252 registration_id, std::move(on_done));
253
Yuwei Huang4c919d142019-04-05 17:05:49254 ftl::ChromotingMessage crd_message;
255 crd_message.mutable_xmpp()->set_stanza(message);
Yuwei Huang4de60092019-03-22 21:19:44256 messaging_client_->SendMessage(
Yuwei Huang4c919d142019-04-05 17:05:49257 receiver_id, registration_id, crd_message,
Yuwei Huang2687b47f2019-04-01 20:57:08258 base::BindOnce(&FtlServicesPlayground::OnSendMessageResponse,
Yuwei Huang4de60092019-03-22 21:19:44259 weak_factory_.GetWeakPtr(), std::move(on_continue)));
260}
261
Yuwei Huang2687b47f2019-04-01 20:57:08262void FtlServicesPlayground::OnSendMessageResponse(
Yuwei Huang4de60092019-03-22 21:19:44263 base::OnceCallback<void(bool)> on_continue,
264 const grpc::Status& status) {
265 if (!status.ok()) {
Yuwei Huangfce958772019-03-30 00:18:11266 HandleGrpcStatusError(base::BindOnce(std::move(on_continue), false),
267 status);
268 return;
Yuwei Huang4de60092019-03-22 21:19:44269 }
Yuwei Huangfce958772019-03-30 00:18:11270
271 printf("Message successfully sent.\n");
272 std::move(on_continue).Run(true);
Yuwei Huang4de60092019-03-22 21:19:44273}
274
Yuwei Huang2687b47f2019-04-01 20:57:08275void FtlServicesPlayground::StartReceivingMessages(base::OnceClosure on_done) {
Yuwei Huangb8e1f2d2019-03-22 19:40:26276 VLOG(0) << "Running StartReceivingMessages...";
277 messaging_client_->StartReceivingMessages(
Yuwei Huang2687b47f2019-04-01 20:57:08278 base::BindOnce(&FtlServicesPlayground::OnStartReceivingMessagesDone,
Yuwei Huangb8e1f2d2019-03-22 19:40:26279 weak_factory_.GetWeakPtr(), std::move(on_done)));
280}
281
Yuwei Huang2687b47f2019-04-01 20:57:08282void FtlServicesPlayground::StopReceivingMessages(base::OnceClosure on_done) {
Yuwei Huangb8e1f2d2019-03-22 19:40:26283 messaging_client_->StopReceivingMessages();
284 std::move(on_done).Run();
285}
286
Yuwei Huangc9bb46f2019-04-04 02:19:00287void FtlServicesPlayground::OnMessageReceived(
288 const std::string& sender_id,
289 const std::string& sender_registration_id,
Yuwei Huang4c919d142019-04-05 17:05:49290 const ftl::ChromotingMessage& message) {
291 std::string message_text = message.xmpp().stanza();
Yuwei Huang968994cb2019-03-21 00:55:27292 printf(
293 "Received message:\n"
294 " Sender ID=%s\n"
Yuwei Huangc9bb46f2019-04-04 02:19:00295 " Sender Registration ID=%s\n"
Yuwei Huang968994cb2019-03-21 00:55:27296 " Message=%s\n",
Yuwei Huang4c919d142019-04-05 17:05:49297 sender_id.c_str(), sender_registration_id.c_str(), message_text.c_str());
Yuwei Huang968994cb2019-03-21 00:55:27298}
299
Yuwei Huang2687b47f2019-04-01 20:57:08300void FtlServicesPlayground::OnStartReceivingMessagesDone(
Yuwei Huangb8e1f2d2019-03-22 19:40:26301 base::OnceClosure on_done,
302 const grpc::Status& status) {
303 if (status.error_code() == grpc::StatusCode::CANCELLED) {
304 printf("ReceiveMessages stream canceled by client.\n");
305 std::move(on_done).Run();
306 return;
307 }
Yuwei Huangfce958772019-03-30 00:18:11308
Yuwei Huangb8e1f2d2019-03-22 19:40:26309 if (!status.ok()) {
Yuwei Huangfce958772019-03-30 00:18:11310 HandleGrpcStatusError(std::move(on_done), status);
Yuwei Huangb8e1f2d2019-03-22 19:40:26311 return;
312 }
313 printf("Started receiving messages. Press enter to stop streaming...\n");
Yuwei Huang41ddec52019-04-02 02:08:19314 test::WaitForEnterKey(
315 base::BindOnce(&FtlServicesPlayground::StopReceivingMessages,
316 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huangb8e1f2d2019-03-22 19:40:26317}
318
Yuwei Huang2687b47f2019-04-01 20:57:08319void FtlServicesPlayground::HandleGrpcStatusError(base::OnceClosure on_done,
320 const grpc::Status& status) {
Yuwei Huangfce958772019-03-30 00:18:11321 DCHECK(!status.ok());
322 if (status.error_code() == grpc::StatusCode::UNAUTHENTICATED) {
323 if (NeedsManualSignin()) {
324 printf(
325 "Request is unauthenticated. You should run SignInGaia first if "
326 "you haven't done so, otherwise your OAuth token might be expired. \n"
327 "Request for new OAuth token? [y/N]: ");
Yuwei Huang41ddec52019-04-02 02:08:19328 std::string result = test::ReadString();
Yuwei Huangfce958772019-03-30 00:18:11329 if (result != "y" && result != "Y") {
330 std::move(on_done).Run();
331 return;
332 }
333 }
334 VLOG(0) << "Grpc request failed to authenticate. "
335 << "Trying to reauthenticate...";
Yuwei Huang41ddec52019-04-02 02:08:19336 token_getter_->ResetWithAuthenticationFlow(
337 base::BindOnce(&FtlServicesPlayground::ResetServices,
338 weak_factory_.GetWeakPtr(), std::move(on_done)));
Yuwei Huangfce958772019-03-30 00:18:11339 return;
340 }
341
342 fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(),
343 status.error_message().c_str());
344 std::move(on_done).Run();
345}
346
Yuwei Huang575c32ee2019-02-27 04:41:56347} // namespace remoting