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 | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 22 | #include "remoting/base/oauth_token_getter_impl.h" |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 23 | #include "remoting/signaling/ftl_device_id_provider.h" |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 24 | #include "remoting/signaling/ftl_grpc_context.h" |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 25 | #include "remoting/signaling/ftl_services.grpc.pb.h" |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 26 | #include "remoting/signaling/grpc_support/grpc_async_unary_request.h" |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 27 | #include "remoting/test/cli_util.h" |
| 28 | #include "remoting/test/test_oauth_token_getter.h" |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 29 | #include "remoting/test/test_token_storage.h" |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | constexpr char kSwitchNameHelp[] = "help"; |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 34 | constexpr char kSwitchNameUsername[] = "username"; |
| 35 | constexpr char kSwitchNameStoragePath[] = "storage-path"; |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 36 | constexpr char kSwitchNameNoAutoSignin[] = "no-auto-signin"; |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 37 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 38 | bool NeedsManualSignin() { |
| 39 | return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 40 | kSwitchNameNoAutoSignin); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 41 | } |
| 42 | |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 43 | } // namespace |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 44 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 45 | namespace remoting { |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 46 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 47 | FtlServicesPlayground::FtlServicesPlayground() : weak_factory_(this) {} |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 48 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 49 | FtlServicesPlayground::~FtlServicesPlayground() = default; |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 50 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 51 | bool FtlServicesPlayground::ShouldPrintHelp() { |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 52 | return base::CommandLine::ForCurrentProcess()->HasSwitch(kSwitchNameHelp); |
| 53 | } |
| 54 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 55 | void FtlServicesPlayground::PrintHelp() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 56 | printf( |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 57 | "Usage: %s [--no-auto-signin] [--auth-code=<auth-code>] " |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 58 | "[--storage-path=<storage-path>] [--username=<[email protected]>]\n", |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 59 | base::CommandLine::ForCurrentProcess() |
| 60 | ->GetProgram() |
| 61 | .MaybeAsASCII() |
| 62 | .c_str()); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 63 | } |
| 64 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 65 | void FtlServicesPlayground::StartAndAuthenticate() { |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 66 | DCHECK(!storage_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 67 | DCHECK(!token_getter_); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 68 | DCHECK(!executor_); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 69 | |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 70 | 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 Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 76 | token_getter_ = std::make_unique<test::TestOAuthTokenGetter>(storage_.get()); |
| 77 | |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 78 | base::RunLoop run_loop; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 79 | token_getter_->Initialize( |
| 80 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 81 | weak_factory_.GetWeakPtr(), run_loop.QuitClosure())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 82 | run_loop.Run(); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 83 | |
| 84 | StartLoop(); |
| 85 | } |
| 86 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 87 | void FtlServicesPlayground::StartLoop() { |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 88 | 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 Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 98 | |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 99 | if (NeedsManualSignin()) { |
| 100 | options.insert( |
| 101 | options.begin(), |
| 102 | {"SignInGaia", base::BindRepeating(&FtlServicesPlayground::SignInGaia, |
| 103 | weak_factory_.GetWeakPtr())}); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 104 | } |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 105 | |
| 106 | test::RunCommandOptionsLoop(options); |
Yuwei Huang | bdc4a97 | 2019-03-01 17:43:06 | [diff] [blame] | 107 | } |
| 108 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 109 | void FtlServicesPlayground::ResetServices(base::OnceClosure on_done) { |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 110 | 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 Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 116 | |
| 117 | message_subscription_.reset(); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 118 | messaging_client_ = std::make_unique<FtlMessagingClient>( |
| 119 | token_getter_.get(), registration_manager_.get()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 120 | message_subscription_ = messaging_client_->RegisterMessageCallback( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 121 | base::BindRepeating(&FtlServicesPlayground::OnMessageReceived, |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 122 | weak_factory_.GetWeakPtr())); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 123 | |
| 124 | if (NeedsManualSignin()) { |
| 125 | std::move(on_done).Run(); |
| 126 | } else { |
| 127 | SignInGaia(std::move(on_done)); |
| 128 | } |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 129 | } |
| 130 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 131 | void FtlServicesPlayground::GetIceServer(base::OnceClosure on_done) { |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 132 | DCHECK(peer_to_peer_stub_); |
| 133 | VLOG(0) << "Running GetIceServer..."; |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 134 | ftl::GetICEServerRequest request; |
| 135 | *request.mutable_header() = FtlGrpcContext::CreateRequestHeader(); |
| 136 | auto grpc_request = CreateGrpcAsyncUnaryRequest( |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 137 | base::BindOnce(&PeerToPeer::Stub::AsyncGetICEServer, |
| 138 | base::Unretained(peer_to_peer_stub_.get())), |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 139 | FtlGrpcContext::CreateClientContext(), request, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 140 | base::BindOnce(&FtlServicesPlayground::OnGetIceServerResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 141 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | 82c0189 | 2019-03-28 20:39:40 | [diff] [blame] | 142 | executor_->ExecuteRpc(std::move(grpc_request)); |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 143 | } |
| 144 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 145 | void FtlServicesPlayground::OnGetIceServerResponse( |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 146 | base::OnceClosure on_done, |
Yuwei Huang | 81bbc1a | 2019-03-08 20:46:06 | [diff] [blame] | 147 | const grpc::Status& status, |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 148 | const ftl::GetICEServerResponse& response) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 149 | 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 Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 167 | } |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 168 | } |
| 169 | std::move(on_done).Run(); |
| 170 | } |
| 171 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 172 | void FtlServicesPlayground::SignInGaia(base::OnceClosure on_done) { |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 173 | DCHECK(registration_manager_); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 174 | VLOG(0) << "Running SignInGaia..."; |
Yuwei Huang | e5b9445 | 2019-03-23 00:26:05 | [diff] [blame] | 175 | registration_manager_->SignInGaia( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 176 | base::BindOnce(&FtlServicesPlayground::OnSignInGaiaResponse, |
Yuwei Huang | 946cebd4 | 2019-03-04 22:14:20 | [diff] [blame] | 177 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | aae881f | 2019-03-01 23:10:27 | [diff] [blame] | 178 | } |
| 179 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 180 | void FtlServicesPlayground::OnSignInGaiaResponse(base::OnceClosure on_done, |
| 181 | const grpc::Status& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 182 | if (!status.ok()) { |
| 183 | HandleGrpcStatusError(std::move(on_done), status); |
| 184 | return; |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 185 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 186 | |
| 187 | std::string registration_id_base64; |
| 188 | base::Base64Encode(registration_manager_->GetRegistrationId(), |
| 189 | ®istration_id_base64); |
| 190 | printf("Service signed in. registration_id(base64)=%s\n", |
| 191 | registration_id_base64.c_str()); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 192 | std::move(on_done).Run(); |
| 193 | } |
| 194 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 195 | void FtlServicesPlayground::PullMessages(base::OnceClosure on_done) { |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 196 | DCHECK(messaging_client_); |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 197 | VLOG(0) << "Running PullMessages..."; |
Yuwei Huang | 3f1fcd36 | 2019-03-11 20:27:17 | [diff] [blame] | 198 | |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 199 | messaging_client_->PullMessages( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 200 | base::BindOnce(&FtlServicesPlayground::OnPullMessagesResponse, |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 201 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 202 | } |
| 203 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 204 | void FtlServicesPlayground::OnPullMessagesResponse(base::OnceClosure on_done, |
| 205 | const grpc::Status& status) { |
Yuwei Huang | 98655b9 | 2019-03-04 23:41:41 | [diff] [blame] | 206 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 207 | HandleGrpcStatusError(std::move(on_done), status); |
| 208 | return; |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 209 | } |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 210 | std::move(on_done).Run(); |
| 211 | } |
Yuwei Huang | 5a84f953 | 2019-02-20 18:29:01 | [diff] [blame] | 212 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 213 | void FtlServicesPlayground::SendMessage(base::OnceClosure on_done) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 214 | DCHECK(messaging_client_); |
| 215 | VLOG(0) << "Running SendMessage..."; |
| 216 | |
| 217 | printf("Receiver ID: "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 218 | std::string receiver_id = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 219 | |
| 220 | printf("Receiver registration ID (base64, optional): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 221 | std::string registration_id_base64 = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 222 | |
| 223 | std::string registration_id; |
| 224 | bool success = base::Base64Decode(registration_id_base64, ®istration_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 Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 233 | void FtlServicesPlayground::DoSendMessage(const std::string& receiver_id, |
| 234 | const std::string& registration_id, |
| 235 | base::OnceClosure on_done, |
| 236 | bool should_keep_running) { |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 237 | if (!should_keep_running) { |
| 238 | std::move(on_done).Run(); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | printf("Message (enter nothing to quit): "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 243 | std::string message = test::ReadString(); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 244 | |
| 245 | if (message.empty()) { |
| 246 | std::move(on_done).Run(); |
| 247 | return; |
| 248 | } |
| 249 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 250 | auto on_continue = base::BindOnce(&FtlServicesPlayground::DoSendMessage, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 251 | weak_factory_.GetWeakPtr(), receiver_id, |
| 252 | registration_id, std::move(on_done)); |
| 253 | |
| 254 | messaging_client_->SendMessage( |
| 255 | receiver_id, registration_id, message, |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 256 | base::BindOnce(&FtlServicesPlayground::OnSendMessageResponse, |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 257 | weak_factory_.GetWeakPtr(), std::move(on_continue))); |
| 258 | } |
| 259 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 260 | void FtlServicesPlayground::OnSendMessageResponse( |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 261 | base::OnceCallback<void(bool)> on_continue, |
| 262 | const grpc::Status& status) { |
| 263 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 264 | HandleGrpcStatusError(base::BindOnce(std::move(on_continue), false), |
| 265 | status); |
| 266 | return; |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 267 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 268 | |
| 269 | printf("Message successfully sent.\n"); |
| 270 | std::move(on_continue).Run(true); |
Yuwei Huang | 4de6009 | 2019-03-22 21:19:44 | [diff] [blame] | 271 | } |
| 272 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 273 | void FtlServicesPlayground::StartReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 274 | VLOG(0) << "Running StartReceivingMessages..."; |
| 275 | messaging_client_->StartReceivingMessages( |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 276 | base::BindOnce(&FtlServicesPlayground::OnStartReceivingMessagesDone, |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 277 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
| 278 | } |
| 279 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 280 | void FtlServicesPlayground::StopReceivingMessages(base::OnceClosure on_done) { |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 281 | messaging_client_->StopReceivingMessages(); |
| 282 | std::move(on_done).Run(); |
| 283 | } |
| 284 | |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame^] | 285 | void FtlServicesPlayground::OnMessageReceived( |
| 286 | const std::string& sender_id, |
| 287 | const std::string& sender_registration_id, |
| 288 | const std::string& message) { |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 289 | printf( |
| 290 | "Received message:\n" |
| 291 | " Sender ID=%s\n" |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame^] | 292 | " Sender Registration ID=%s\n" |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 293 | " Message=%s\n", |
Yuwei Huang | c9bb46f | 2019-04-04 02:19:00 | [diff] [blame^] | 294 | sender_id.c_str(), sender_registration_id.c_str(), message.c_str()); |
Yuwei Huang | 968994cb | 2019-03-21 00:55:27 | [diff] [blame] | 295 | } |
| 296 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 297 | void FtlServicesPlayground::OnStartReceivingMessagesDone( |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 298 | base::OnceClosure on_done, |
| 299 | const grpc::Status& status) { |
| 300 | if (status.error_code() == grpc::StatusCode::CANCELLED) { |
| 301 | printf("ReceiveMessages stream canceled by client.\n"); |
| 302 | std::move(on_done).Run(); |
| 303 | return; |
| 304 | } |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 305 | |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 306 | if (!status.ok()) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 307 | HandleGrpcStatusError(std::move(on_done), status); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | printf("Started receiving messages. Press enter to stop streaming...\n"); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 311 | test::WaitForEnterKey( |
| 312 | base::BindOnce(&FtlServicesPlayground::StopReceivingMessages, |
| 313 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | b8e1f2d | 2019-03-22 19:40:26 | [diff] [blame] | 314 | } |
| 315 | |
Yuwei Huang | 2687b47f | 2019-04-01 20:57:08 | [diff] [blame] | 316 | void FtlServicesPlayground::HandleGrpcStatusError(base::OnceClosure on_done, |
| 317 | const grpc::Status& status) { |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 318 | DCHECK(!status.ok()); |
| 319 | if (status.error_code() == grpc::StatusCode::UNAUTHENTICATED) { |
| 320 | if (NeedsManualSignin()) { |
| 321 | printf( |
| 322 | "Request is unauthenticated. You should run SignInGaia first if " |
| 323 | "you haven't done so, otherwise your OAuth token might be expired. \n" |
| 324 | "Request for new OAuth token? [y/N]: "); |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 325 | std::string result = test::ReadString(); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 326 | if (result != "y" && result != "Y") { |
| 327 | std::move(on_done).Run(); |
| 328 | return; |
| 329 | } |
| 330 | } |
| 331 | VLOG(0) << "Grpc request failed to authenticate. " |
| 332 | << "Trying to reauthenticate..."; |
Yuwei Huang | 41ddec5 | 2019-04-02 02:08:19 | [diff] [blame] | 333 | token_getter_->ResetWithAuthenticationFlow( |
| 334 | base::BindOnce(&FtlServicesPlayground::ResetServices, |
| 335 | weak_factory_.GetWeakPtr(), std::move(on_done))); |
Yuwei Huang | fce95877 | 2019-03-30 00:18:11 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
| 339 | fprintf(stderr, "RPC failed. Code=%d, Message=%s\n", status.error_code(), |
| 340 | status.error_message().c_str()); |
| 341 | std::move(on_done).Run(); |
| 342 | } |
| 343 | |
Yuwei Huang | 575c32ee | 2019-02-27 04:41:56 | [diff] [blame] | 344 | } // namespace remoting |