Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 1 | // 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 Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 5 | #include "remoting/test/ftl_services_playground.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 6 | |
Yuwei Huang | 30bac9e | 2019-03-01 21:28:14 | [diff] [blame] | 7 | #include <inttypes.h> |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 8 | #include <string> |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 9 | #include <utility> |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 10 | #include <vector> |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 11 | |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 12 | #include "base/base64.h" |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 13 | #include "base/bind.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 15 | #include "base/command_line.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 17 | #include "base/guid.h" |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 18 | #include "base/logging.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 19 | #include "base/path_service.h" |
Yuwei Huang | 22e0b56 | 2019-03-27 20:54:29 | [diff] [blame] | 20 | #include "base/run_loop.h" |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
Yuwei Huang | 84b92a7c | 2019-04-16 22:25:00 | [diff] [blame] | 22 | #include "remoting/base/grpc_support/grpc_async_unary_request.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 23 | #include "remoting/base/oauth_token_getter_impl.h" |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 24 | #include "remoting/base/protobuf_http_status.h" |
| 25 | #include "remoting/base/url_request_context_getter.h" |
Yuwei Huang | 8dd1b728 | 2019-05-03 02:12:40 | [diff] [blame] | 26 | #include "remoting/proto/ftl/v1/ftl_services.grpc.pb.h" |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 27 | #include "remoting/signaling/ftl_grpc_context.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 28 | #include "remoting/test/cli_util.h" |
Yuwei Huang | 1cf6806 | 2019-04-11 23:28:42 | [diff] [blame] | 29 | #include "remoting/test/test_device_id_provider.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 30 | #include "remoting/test/test_oauth_token_getter.h" |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 31 | #include "remoting/test/test_token_storage.h" |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 32 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
| 33 | #include "services/network/transitional_url_loader_factory_owner.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 34 | |
| 35 | namespace { |
| 36 | |
| 37 | constexpr char kSwitchNameHelp[] = "help"; |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 38 | constexpr char kSwitchNameUsername[] = "username"; |
| 39 | constexpr char kSwitchNameStoragePath[] = "storage-path"; |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 40 | constexpr char kSwitchNameNoAutoSignin[] = "no-auto-signin"; |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 41 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 42 | bool NeedsManualSignin() { |
| 43 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 44 | kSwitchNameNoAutoSignin); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 45 | } |
| 46 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 47 | } // namespace |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 48 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 49 | namespace remoting { |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 50 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 51 | FtlServicesPlayground::FtlServicesPlayground() {} |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 52 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 53 | FtlServicesPlayground::~FtlServicesPlayground() = default; |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 54 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 55 | bool FtlServicesPlayground::ShouldPrintHelp() { |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 56 | return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp); |
| 57 | } |
| 58 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 59 | void FtlServicesPlayground::PrintHelp() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 60 | printf( |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 61 | "Usage: %s [--no-auto-signin] [--auth-code=<auth-code>] " |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 62 | "[--storage-path=<storage-path>] [--username=<[email protected]>]\n", |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 63 | base::CommandLine::ForCurrentProcess() |
| 64 | ->GetProgram() |
| 65 | .MaybeAsASCII() |
| 66 | .c_str()); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 67 | } |
| 68 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 69 | void FtlServicesPlayground::StartAndAuthenticate() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 70 | DCHECK(!storage_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 71 | DCHECK(!token_getter_); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 72 | DCHECK(!executor_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 73 | |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 74 | base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 75 | std::string username = cmd_line->GetSwitchValueASCII(kSwitchNameUsername); |
| 76 | base::FilePath storage_path = |
| 77 | cmd_line->GetSwitchValuePath(kSwitchNameStoragePath); |
| 78 | storage_ = test::TestTokenStorage::OnDisk(username, storage_path); |
| 79 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 80 | token_getter_ = std::make_unique<test::TestOAuthTokenGetter>(storage_.get()); |
| 81 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 82 | base::RunLoop run_loop; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 83 | token_getter_->Initialize( |
| 84 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 85 | weak_factory_.GetWeakPtr(), run_loop.QuitClosure())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 86 | run_loop.Run(); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 87 | |
| 88 | StartLoop(); |
| 89 | } |
| 90 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 91 | void FtlServicesPlayground::StartLoop() { |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 92 | std::vector<test::CommandOption> options{ |
| 93 | {"GetIceServer", base::BindRepeating(&FtlServicesPlayground::GetIceServer, |
| 94 | weak_factory_.GetWeakPtr())}, |
| 95 | {"PullMessages", base::BindRepeating(&FtlServicesPlayground::PullMessages, |
| 96 | weak_factory_.GetWeakPtr())}, |
| 97 | {"ReceiveMessages", |
| 98 | base::BindRepeating(&FtlServicesPlayground::StartReceivingMessages, |
| 99 | weak_factory_.GetWeakPtr())}, |
| 100 | {"SendMessage", base::BindRepeating(&FtlServicesPlayground::SendMessage, |
| 101 | weak_factory_.GetWeakPtr())}}; |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 102 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 103 | if (NeedsManualSignin()) { |
| 104 | options.insert( |
| 105 | options.begin(), |
| 106 | {"SignInGaia", base::BindRepeating(&FtlServicesPlayground::SignInGaia, |
| 107 | weak_factory_.GetWeakPtr())}); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 108 | } |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 109 | |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 110 | auto url_request_context_getter = |
| 111 | base::MakeRefCounted<URLRequestContextGetter>( |
| 112 | base::ThreadTaskRunnerHandle::Get()); |
| 113 | url_loader_factory_owner_ = |
| 114 | std::make_unique<network::TransitionalURLLoaderFactoryOwner>( |
| 115 | url_request_context_getter); |
| 116 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 117 | test::RunCommandOptionsLoop(options); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 118 | } |
| 119 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 120 | void FtlServicesPlayground::ResetServices(base::OnceClosure on_done) { |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 121 | executor_ = std::make_unique<GrpcAuthenticatedExecutor>(token_getter_.get()); |
| 122 | peer_to_peer_stub_ = PeerToPeer::NewStub(FtlGrpcContext::CreateChannel()); |
| 123 | |
| 124 | registration_manager_ = std::make_unique<FtlRegistrationManager>( |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 125 | token_getter_.get(), url_loader_factory_owner_->GetURLLoaderFactory(), |
Yuwei Huang | 1cf6806 | 2019-04-11 23:28:42 | [diff] [blame] | 126 | std::make_unique<test::TestDeviceIdProvider>(storage_.get())); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 127 | |
| 128 | message_subscription_.reset(); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 129 | messaging_client_ = std::make_unique<FtlMessagingClient>( |
Yuwei Huang | fefd7335 | 2020-03-04 23:36:57 | [diff] [blame] | 130 | token_getter_.get(), registration_manager_.get(), &signaling_tracker_); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 131 | message_subscription_ = messaging_client_->RegisterMessageCallback( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 132 | base::BindRepeating(&FtlServicesPlayground::OnMessageReceived, |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 133 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 134 | |
| 135 | if (NeedsManualSignin()) { |
| 136 | std::move(on_done).Run(); |
| 137 | } else { |
| 138 | SignInGaia(std::move(on_done)); |
| 139 | } |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 140 | } |
| 141 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 142 | void FtlServicesPlayground::GetIceServer(base::OnceClosure on_done) { |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 143 | DCHECK(peer_to_peer_stub_); |
| 144 | VLOG(0) << "Running GetIceServer..."; |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 145 | ftl::GetICEServerRequest request; |
| 146 | *request.mutable_header() = FtlGrpcContext::CreateRequestHeader(); |
| 147 | auto grpc_request = CreateGrpcAsyncUnaryRequest( |
Erik Jensen | 1ea0101 | 2020-07-27 21:35:06 | [diff] [blame] | 148 | base::BindOnce(&PeerToPeer::StubInterface::AsyncGetICEServer, |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 149 | base::Unretained(peer_to_peer_stub_.get())), |
Yuwei Huang | 2071aeb | 2019-05-10 21:10:59 | [diff] [blame] | 150 | request, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 151 | base::BindOnce(&FtlServicesPlayground::OnGetIceServerResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 152 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | 2071aeb | 2019-05-10 21:10:59 | [diff] [blame] | 153 | FtlGrpcContext::FillClientContext(grpc_request->context()); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 154 | executor_->ExecuteRpc(std::move(grpc_request)); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 155 | } |
| 156 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 157 | void FtlServicesPlayground::OnGetIceServerResponse( |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 158 | base::OnceClosure on_done, |
Yuwei Huang | 81bbc1a | 2019-03-08 20:46:06 | [diff] [blame] | 159 | const grpc::Status& status, |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 160 | const ftl::GetICEServerResponse& response) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 161 | if (!status.ok()) { |
| 162 | HandleGrpcStatusError(std::move(on_done), status); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | printf("Ice transport policy: %s\n", |
| 167 | response.ice_config().ice_transport_policy().c_str()); |
| 168 | for (const ftl::ICEServerList& server : response.ice_config().ice_servers()) { |
| 169 | printf( |
| 170 | "ICE server:\n" |
| 171 | " hostname=%s\n" |
| 172 | " username=%s\n" |
| 173 | " credential=%s\n" |
| 174 | " max_rate_kbps=%" PRId64 "\n", |
| 175 | server.hostname().c_str(), server.username().c_str(), |
| 176 | server.credential().c_str(), server.max_rate_kbps()); |
| 177 | for (const std::string& url : server.urls()) { |
| 178 | printf(" url=%s\n", url.c_str()); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 179 | } |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 180 | } |
| 181 | std::move(on_done).Run(); |
| 182 | } |
| 183 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 184 | void FtlServicesPlayground::SignInGaia(base::OnceClosure on_done) { |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 185 | DCHECK(registration_manager_); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 186 | VLOG(0) << "Running SignInGaia..."; |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 187 | registration_manager_->SignInGaia( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 188 | base::BindOnce(&FtlServicesPlayground::OnSignInGaiaResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 189 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 190 | } |
| 191 | |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 192 | void FtlServicesPlayground::OnSignInGaiaResponse( |
| 193 | base::OnceClosure on_done, |
| 194 | const ProtobufHttpStatus& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 195 | if (!status.ok()) { |
Yuwei Huang | feddf23 | 2020-07-28 00:27:49 | [diff] [blame^] | 196 | // TODO(yuweih): Clean this up. |
| 197 | HandleGrpcStatusError( |
| 198 | std::move(on_done), |
| 199 | grpc::Status(static_cast<grpc::StatusCode>(status.error_code()), |
| 200 | status.error_message())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 201 | return; |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 202 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 203 | |
| 204 | std::string registration_id_base64; |
| 205 | base::Base64Encode(registration_manager_->GetRegistrationId(), |
| 206 | ®istration_id_base64); |
| 207 | printf("Service signed in. registration_id(base64)=%s\n", |
| 208 | registration_id_base64.c_str()); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 209 | std::move(on_done).Run(); |
| 210 | } |
| 211 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 212 | void FtlServicesPlayground::PullMessages(base::OnceClosure on_done) { |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 213 | DCHECK(messaging_client_); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 214 | VLOG(0) << "Running PullMessages..."; |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 215 | |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 216 | messaging_client_->PullMessages( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 217 | base::BindOnce(&FtlServicesPlayground::OnPullMessagesResponse, |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 218 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 219 | } |
| 220 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 221 | void FtlServicesPlayground::OnPullMessagesResponse(base::OnceClosure on_done, |
| 222 | const grpc::Status& status) { |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 223 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 224 | HandleGrpcStatusError(std::move(on_done), status); |
| 225 | return; |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 226 | } |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 227 | std::move(on_done).Run(); |
| 228 | } |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 229 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 230 | void FtlServicesPlayground::SendMessage(base::OnceClosure on_done) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 231 | DCHECK(messaging_client_); |
| 232 | VLOG(0) << "Running SendMessage..."; |
| 233 | |
| 234 | printf("Receiver ID: "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 235 | std::string receiver_id = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 236 | |
| 237 | printf("Receiver registration ID (base64, optional): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 238 | std::string registration_id_base64 = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 239 | |
| 240 | std::string registration_id; |
| 241 | bool success = base::Base64Decode(registration_id_base64, ®istration_id); |
| 242 | if (!success) { |
| 243 | fprintf(stderr, "Your input can't be base64 decoded.\n"); |
| 244 | std::move(on_done).Run(); |
| 245 | return; |
| 246 | } |
| 247 | DoSendMessage(receiver_id, registration_id, std::move(on_done), true); |
| 248 | } |
| 249 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 250 | void FtlServicesPlayground::DoSendMessage(const std::string& receiver_id, |
| 251 | const std::string& registration_id, |
| 252 | base::OnceClosure on_done, |
| 253 | bool should_keep_running) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 254 | if (!should_keep_running) { |
| 255 | std::move(on_done).Run(); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | printf("Message (enter nothing to quit): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 260 | std::string message = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 261 | |
| 262 | if (message.empty()) { |
| 263 | std::move(on_done).Run(); |
| 264 | return; |
| 265 | } |
| 266 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 267 | auto on_continue = base::BindOnce(&FtlServicesPlayground::DoSendMessage, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 268 | weak_factory_.GetWeakPtr(), receiver_id, |
| 269 | registration_id, std::move(on_done)); |
| 270 | |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 271 | ftl::ChromotingMessage crd_message; |
| 272 | crd_message.mutable_xmpp()->set_stanza(message); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 273 | messaging_client_->SendMessage( |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 274 | receiver_id, registration_id, crd_message, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 275 | base::BindOnce(&FtlServicesPlayground::OnSendMessageResponse, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 276 | weak_factory_.GetWeakPtr(), std::move(on_continue))); |
| 277 | } |
| 278 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 279 | void FtlServicesPlayground::OnSendMessageResponse( |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 280 | base::OnceCallback<void(bool)> on_continue, |
| 281 | const grpc::Status& status) { |
| 282 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 283 | HandleGrpcStatusError(base::BindOnce(std::move(on_continue), false), |
| 284 | status); |
| 285 | return; |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 286 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 287 | |
| 288 | printf("Message successfully sent.\n"); |
| 289 | std::move(on_continue).Run(true); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 290 | } |
| 291 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 292 | void FtlServicesPlayground::StartReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 293 | VLOG(0) << "Running StartReceivingMessages..."; |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 294 | receive_messages_done_callback_ = std::move(on_done); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 295 | messaging_client_->StartReceivingMessages( |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 296 | base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamReady, |
| 297 | weak_factory_.GetWeakPtr()), |
| 298 | base::BindOnce(&FtlServicesPlayground::OnReceiveMessagesStreamClosed, |
| 299 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 300 | } |
| 301 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 302 | void FtlServicesPlayground::StopReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 303 | messaging_client_->StopReceivingMessages(); |
| 304 | std::move(on_done).Run(); |
| 305 | } |
| 306 | |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 307 | void FtlServicesPlayground::OnMessageReceived( |
Yuwei Huang | 4ef50e73 | 2019-05-30 04:30:25 | [diff] [blame] | 308 | const ftl::Id& sender_id, |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 309 | const std::string& sender_registration_id, |
Yuwei Huang | 4c919d14 | 2019-04-05 17:05:49 | [diff] [blame] | 310 | const ftl::ChromotingMessage& message) { |
| 311 | std::string message_text = message.xmpp().stanza(); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 312 | printf( |
| 313 | "Received message:\n" |
| 314 | " Sender ID=%s\n" |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame] | 315 | " Sender Registration ID=%s\n" |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 316 | " Message=%s\n", |
Yuwei Huang | 4ef50e73 | 2019-05-30 04:30:25 | [diff] [blame] | 317 | sender_id.id().c_str(), sender_registration_id.c_str(), |
| 318 | message_text.c_str()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 319 | } |
| 320 | |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 321 | void FtlServicesPlayground::OnReceiveMessagesStreamReady() { |
| 322 | printf("Started receiving messages. Press enter to stop streaming...\n"); |
| 323 | test::WaitForEnterKey(base::BindOnce( |
| 324 | &FtlServicesPlayground::StopReceivingMessages, weak_factory_.GetWeakPtr(), |
| 325 | std::move(receive_messages_done_callback_))); |
| 326 | } |
| 327 | |
| 328 | void FtlServicesPlayground::OnReceiveMessagesStreamClosed( |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 329 | const grpc::Status& status) { |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 330 | base::OnceClosure callback = std::move(receive_messages_done_callback_); |
| 331 | bool is_callback_null = callback.is_null(); |
| 332 | if (is_callback_null) { |
| 333 | callback = base::DoNothing::Once(); |
| 334 | } |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 335 | if (status.error_code() == grpc::StatusCode::CANCELLED) { |
| 336 | printf("ReceiveMessages stream canceled by client.\n"); |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 337 | std::move(callback).Run(); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 338 | return; |
| 339 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 340 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 341 | if (!status.ok()) { |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 342 | HandleGrpcStatusError(std::move(callback), status); |
| 343 | } else { |
| 344 | printf("Stream closed by server.\n"); |
| 345 | std::move(callback).Run(); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 346 | } |
Yuwei Huang | ce344e3 | 2019-04-05 19:31:29 | [diff] [blame] | 347 | |
| 348 | if (is_callback_null) { |
| 349 | // Stream had been started and callback has been passed to wait for the |
| 350 | // enter key. |
| 351 | printf("Please press enter to continue...\n"); |
| 352 | } |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 353 | } |
| 354 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 355 | void FtlServicesPlayground::HandleGrpcStatusError(base::OnceClosure on_done, |
| 356 | const grpc::Status& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 357 | DCHECK(!status.ok()); |
| 358 | if (status.error_code() == grpc::StatusCode::UNAUTHENTICATED) { |
| 359 | if (NeedsManualSignin()) { |
| 360 | printf( |
| 361 | "Request is unauthenticated. You should run SignInGaia first if " |
| 362 | "you haven't done so, otherwise your OAuth token might be expired. \n" |
| 363 | "Request for new OAuth token? [y/N]: "); |
Yuwei Huang | dc86bdf4 | 2019-04-18 19:14:10 | [diff] [blame] | 364 | if (!test::ReadYNBool()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 365 | std::move(on_done).Run(); |
| 366 | return; |
| 367 | } |
| 368 | } |
| 369 | VLOG(0) << "Grpc request failed to authenticate. " |
| 370 | << "Trying to reauthenticate..."; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 371 | token_getter_->ResetWithAuthenticationFlow( |
| 372 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 373 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(), |
| 378 | status.error_message().c_str()); |
| 379 | std::move(on_done).Run(); |
| 380 | } |
| 381 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 382 | } // namespace remoting |