Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: remoting/client/jni/chromoting_jni_instance.cc

Issue 2753963002: Refactoring and rewriting the chromoting jni instance to be chromoting session. (Closed)
Patch Set: Updating based on feedback. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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
5 #include "remoting/client/jni/chromoting_jni_instance.h"
6
7 #include <android/log.h>
8 #include <stdint.h>
9
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/format_macros.h"
13 #include "base/logging.h"
14 #include "base/memory/ptr_util.h"
15 #include "jingle/glue/thread_wrapper.h"
16 #include "net/socket/client_socket_factory.h"
17 #include "remoting/base/chromium_url_request.h"
18 #include "remoting/base/chromoting_event.h"
19 #include "remoting/client/audio_player_android.h"
20 #include "remoting/client/chromoting_client_runtime.h"
21 #include "remoting/client/client_telemetry_logger.h"
22 #include "remoting/client/jni/android_keymap.h"
23 #include "remoting/client/jni/jni_client.h"
24 #include "remoting/client/jni/jni_pairing_secret_fetcher.h"
25 #include "remoting/protocol/chromium_port_allocator_factory.h"
26 #include "remoting/protocol/chromium_socket_factory.h"
27 #include "remoting/protocol/client_authentication_config.h"
28 #include "remoting/protocol/frame_consumer.h"
29 #include "remoting/protocol/host_stub.h"
30 #include "remoting/protocol/network_settings.h"
31 #include "remoting/protocol/performance_tracker.h"
32 #include "remoting/protocol/transport_context.h"
33 #include "remoting/protocol/video_renderer.h"
34 #include "remoting/signaling/server_log_entry.h"
35 #include "ui/events/keycodes/dom/keycode_converter.h"
36
37 namespace remoting {
38
39 namespace {
40
41 // TODO(solb) Move into location shared with client plugin.
42 const char* const kXmppServer = "talk.google.com";
43 const int kXmppPort = 5222;
44 const bool kXmppUseTls = true;
45
46 // Interval at which to log performance statistics, if enabled.
47 const int kPerfStatsIntervalMs = 60000;
48
49 } // namespace
50
51 ChromotingJniInstance::ChromotingJniInstance(
52 base::WeakPtr<JniClient> jni_client,
53 base::WeakPtr<JniPairingSecretFetcher> secret_fetcher,
54 std::unique_ptr<protocol::CursorShapeStub> cursor_shape_stub,
55 std::unique_ptr<protocol::VideoRenderer> video_renderer,
56 const ConnectToHostInfo& info)
57 : jni_client_(jni_client),
58 secret_fetcher_(secret_fetcher),
59 connection_info_(info),
60 cursor_shape_stub_(std::move(cursor_shape_stub)),
61 video_renderer_(std::move(video_renderer)),
62 capabilities_(info.capabilities),
63 weak_factory_(this) {
64 runtime_ = ChromotingClientRuntime::GetInstance();
65 DCHECK(runtime_->ui_task_runner()->BelongsToCurrentThread());
66 weak_ptr_ = weak_factory_.GetWeakPtr();
67
68 // Initialize XMPP config.
69 xmpp_config_.host = kXmppServer;
70 xmpp_config_.port = kXmppPort;
71 xmpp_config_.use_tls = kXmppUseTls;
72 xmpp_config_.username = info.username;
73 xmpp_config_.auth_token = info.auth_token;
74
75 client_auth_config_.host_id = info.host_id;
76 client_auth_config_.pairing_client_id = info.pairing_id;
77 client_auth_config_.pairing_secret = info.pairing_secret;
78 client_auth_config_.fetch_secret_callback =
79 base::Bind(&JniPairingSecretFetcher::FetchSecret, secret_fetcher);
80 client_auth_config_.fetch_third_party_token_callback = base::Bind(
81 &ChromotingJniInstance::FetchThirdPartyToken, GetWeakPtr(),
82 info.host_pubkey);
83 }
84
85 ChromotingJniInstance::~ChromotingJniInstance() {
86 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
87 if (client_) {
88 ReleaseResources();
89 }
90 }
91
92 void ChromotingJniInstance::Connect() {
93 if (runtime_->network_task_runner()->BelongsToCurrentThread()) {
94 ConnectToHostOnNetworkThread();
95 } else {
96 runtime_->network_task_runner()->PostTask(
97 FROM_HERE,
98 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread,
99 GetWeakPtr()));
100 }
101 }
102
103 void ChromotingJniInstance::Disconnect() {
104 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
105 runtime_->network_task_runner()->PostTask(
106 FROM_HERE,
107 base::Bind(&ChromotingJniInstance::Disconnect, GetWeakPtr()));
108 return;
109 }
110
111 stats_logging_enabled_ = false;
112
113 // User disconnection will not trigger OnConnectionState(Closed, OK).
114 // Remote disconnection will trigger OnConnectionState(...) and later trigger
115 // Disconnect().
116 if (connected_) {
117 logger_->LogSessionStateChange(
118 ChromotingEvent::SessionState::CLOSED,
119 ChromotingEvent::ConnectionError::NONE);
120 connected_ = false;
121 }
122
123 ReleaseResources();
124 }
125
126 void ChromotingJniInstance::FetchThirdPartyToken(
127 const std::string& host_public_key,
128 const std::string& token_url,
129 const std::string& scope,
130 const protocol::ThirdPartyTokenFetchedCallback& token_fetched_callback) {
131 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
132 DCHECK(third_party_token_fetched_callback_.is_null());
133
134 __android_log_print(ANDROID_LOG_INFO,
135 "ThirdPartyAuth",
136 "Fetching Third Party Token from user.");
137
138 third_party_token_fetched_callback_ = token_fetched_callback;
139 runtime_->ui_task_runner()->PostTask(
140 FROM_HERE, base::Bind(&JniClient::FetchThirdPartyToken, jni_client_,
141 token_url, host_public_key, scope));
142 }
143
144 void ChromotingJniInstance::HandleOnThirdPartyTokenFetched(
145 const std::string& token,
146 const std::string& shared_secret) {
147 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
148
149 __android_log_print(
150 ANDROID_LOG_INFO, "ThirdPartyAuth", "Third Party Token Fetched.");
151
152 if (!third_party_token_fetched_callback_.is_null()) {
153 base::ResetAndReturn(&third_party_token_fetched_callback_)
154 .Run(token, shared_secret);
155 } else {
156 __android_log_print(
157 ANDROID_LOG_WARN,
158 "ThirdPartyAuth",
159 "Ignored OnThirdPartyTokenFetched() without a pending fetch.");
160 }
161 }
162
163 void ChromotingJniInstance::ProvideSecret(const std::string& pin,
164 bool create_pairing,
165 const std::string& device_name) {
166 DCHECK(runtime_->ui_task_runner()->BelongsToCurrentThread());
167
168 create_pairing_ = create_pairing;
169
170 if (create_pairing)
171 SetDeviceName(device_name);
172
173 runtime_->network_task_runner()->PostTask(
174 FROM_HERE, base::Bind(&JniPairingSecretFetcher::ProvideSecret,
175 secret_fetcher_, pin));
176 }
177
178 void ChromotingJniInstance::SendMouseEvent(
179 int x, int y,
180 protocol::MouseEvent_MouseButton button,
181 bool button_down) {
182 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
183 runtime_->network_task_runner()->PostTask(
184 FROM_HERE, base::Bind(&ChromotingJniInstance::SendMouseEvent,
185 GetWeakPtr(), x, y, button, button_down));
186 return;
187 }
188
189 protocol::MouseEvent event;
190 event.set_x(x);
191 event.set_y(y);
192 event.set_button(button);
193 if (button != protocol::MouseEvent::BUTTON_UNDEFINED)
194 event.set_button_down(button_down);
195
196 client_->input_stub()->InjectMouseEvent(event);
197 }
198
199 void ChromotingJniInstance::SendMouseWheelEvent(int delta_x, int delta_y) {
200 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
201 runtime_->network_task_runner()->PostTask(
202 FROM_HERE, base::Bind(&ChromotingJniInstance::SendMouseWheelEvent,
203 GetWeakPtr(), delta_x, delta_y));
204 return;
205 }
206
207 protocol::MouseEvent event;
208 event.set_wheel_delta_x(delta_x);
209 event.set_wheel_delta_y(delta_y);
210 client_->input_stub()->InjectMouseEvent(event);
211 }
212
213 bool ChromotingJniInstance::SendKeyEvent(int scan_code,
214 int key_code,
215 bool key_down) {
216 // For software keyboards |scan_code| is set to 0, in which case the
217 // |key_code| is used instead.
218 uint32_t usb_key_code =
219 scan_code ? ui::KeycodeConverter::NativeKeycodeToUsbKeycode(scan_code)
220 : AndroidKeycodeToUsbKeycode(key_code);
221 if (!usb_key_code) {
222 LOG(WARNING) << "Ignoring unknown key code: " << key_code
223 << " scan code: " << scan_code;
224 return false;
225 }
226
227 SendKeyEventInternal(usb_key_code, key_down);
228 return true;
229 }
230
231 void ChromotingJniInstance::SendTextEvent(const std::string& text) {
232 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
233 runtime_->network_task_runner()->PostTask(
234 FROM_HERE,
235 base::Bind(&ChromotingJniInstance::SendTextEvent, GetWeakPtr(), text));
236 return;
237 }
238
239 protocol::TextEvent event;
240 event.set_text(text);
241 client_->input_stub()->InjectTextEvent(event);
242 }
243
244 void ChromotingJniInstance::SendTouchEvent(
245 const protocol::TouchEvent& touch_event) {
246 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
247 runtime_->network_task_runner()->PostTask(
248 FROM_HERE, base::Bind(&ChromotingJniInstance::SendTouchEvent,
249 GetWeakPtr(), touch_event));
250 return;
251 }
252
253 client_->input_stub()->InjectTouchEvent(touch_event);
254 }
255
256 void ChromotingJniInstance::EnableVideoChannel(bool enable) {
257 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
258 runtime_->network_task_runner()->PostTask(
259 FROM_HERE, base::Bind(&ChromotingJniInstance::EnableVideoChannel,
260 GetWeakPtr(), enable));
261 return;
262 }
263
264 protocol::VideoControl video_control;
265 video_control.set_enable(enable);
266 client_->host_stub()->ControlVideo(video_control);
267 }
268
269 void ChromotingJniInstance::SendClientMessage(const std::string& type,
270 const std::string& data) {
271 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
272 runtime_->network_task_runner()->PostTask(
273 FROM_HERE, base::Bind(&ChromotingJniInstance::SendClientMessage,
274 GetWeakPtr(), type, data));
275 return;
276 }
277
278 protocol::ExtensionMessage extension_message;
279 extension_message.set_type(type);
280 extension_message.set_data(data);
281 client_->host_stub()->DeliverClientMessage(extension_message);
282 }
283
284 void ChromotingJniInstance::OnConnectionState(
285 protocol::ConnectionToHost::State state,
286 protocol::ErrorCode error) {
287 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
288
289 // This code assumes no intermediate connection state between CONNECTED and
290 // CLOSED/FAILED.
291 connected_ = state == protocol::ConnectionToHost::CONNECTED;
292 EnableStatsLogging(connected_);
293
294 logger_->LogSessionStateChange(
295 ClientTelemetryLogger::TranslateState(state),
296 ClientTelemetryLogger::TranslateError(error));
297
298 if (create_pairing_ && state == protocol::ConnectionToHost::CONNECTED) {
299 protocol::PairingRequest request;
300 DCHECK(!device_name_.empty());
301 request.set_client_name(device_name_);
302 client_->host_stub()->RequestPairing(request);
303 }
304
305 runtime_->ui_task_runner()->PostTask(
306 FROM_HERE,
307 base::Bind(&JniClient::OnConnectionState, jni_client_, state, error));
308 }
309
310 void ChromotingJniInstance::OnConnectionReady(bool ready) {
311 // We ignore this message, since OnConnectionState tells us the same thing.
312 }
313
314 void ChromotingJniInstance::OnRouteChanged(
315 const std::string& channel_name,
316 const protocol::TransportRoute& route) {
317 std::string message = "Channel " + channel_name + " using " +
318 protocol::TransportRoute::GetTypeString(route.type) + " connection.";
319 __android_log_print(ANDROID_LOG_INFO, "route", "%s", message.c_str());
320 }
321
322 void ChromotingJniInstance::SetCapabilities(const std::string& capabilities) {
323 runtime_->ui_task_runner()->PostTask(
324 FROM_HERE,
325 base::Bind(&JniClient::SetCapabilities, jni_client_, capabilities));
326 }
327
328 void ChromotingJniInstance::SetPairingResponse(
329 const protocol::PairingResponse& response) {
330 runtime_->ui_task_runner()->PostTask(
331 FROM_HERE, base::Bind(&JniClient::CommitPairingCredentials, jni_client_,
332 client_auth_config_.host_id, response.client_id(),
333 response.shared_secret()));
334 }
335
336 void ChromotingJniInstance::DeliverHostMessage(
337 const protocol::ExtensionMessage& message) {
338 runtime_->ui_task_runner()->PostTask(
339 FROM_HERE, base::Bind(&JniClient::HandleExtensionMessage, jni_client_,
340 message.type(), message.data()));
341 }
342
343 void ChromotingJniInstance::SetDesktopSize(const webrtc::DesktopSize& size,
344 const webrtc::DesktopVector& dpi) {
345 // JniFrameConsumer get size from the frames and it doesn't use DPI, so this
346 // call can be ignored.
347 }
348
349 protocol::ClipboardStub* ChromotingJniInstance::GetClipboardStub() {
350 return this;
351 }
352
353 protocol::CursorShapeStub* ChromotingJniInstance::GetCursorShapeStub() {
354 return cursor_shape_stub_.get();
355 }
356
357 void ChromotingJniInstance::InjectClipboardEvent(
358 const protocol::ClipboardEvent& event) {
359 NOTIMPLEMENTED();
360 }
361
362 base::WeakPtr<ChromotingJniInstance> ChromotingJniInstance::GetWeakPtr() {
363 return weak_ptr_;
364 }
365
366 void ChromotingJniInstance::ConnectToHostOnNetworkThread() {
367 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
368
369 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
370
371 client_context_.reset(new ClientContext(runtime_->network_task_runner()));
372 client_context_->Start();
373
374 perf_tracker_.reset(new protocol::PerformanceTracker());
375
376 video_renderer_->Initialize(*client_context_,
377 perf_tracker_.get());
378
379 if (!audio_player_) {
380 audio_player_.reset(new AudioPlayerAndroid());
381 }
382
383 logger_.reset(new ClientTelemetryLogger(runtime_->log_writer(),
384 ChromotingEvent::Mode::ME2ME));
385 logger_->SetHostInfo(
386 connection_info_.host_version,
387 ChromotingEvent::ParseOsFromString(connection_info_.host_os),
388 connection_info_.host_os_version);
389
390 client_.reset(new ChromotingClient(client_context_.get(), this,
391 video_renderer_.get(),
392 audio_player_->GetWeakPtr()));
393
394 signaling_.reset(
395 new XmppSignalStrategy(net::ClientSocketFactory::GetDefaultFactory(),
396 runtime_->url_requester(), xmpp_config_));
397
398 scoped_refptr<protocol::TransportContext> transport_context =
399 new protocol::TransportContext(
400 signaling_.get(),
401 base::MakeUnique<protocol::ChromiumPortAllocatorFactory>(),
402 base::MakeUnique<ChromiumUrlRequestFactory>(
403 runtime_->url_requester()),
404 protocol::NetworkSettings(
405 protocol::NetworkSettings::NAT_TRAVERSAL_FULL),
406 protocol::TransportRole::CLIENT);
407
408 #if defined(ENABLE_WEBRTC_REMOTING_CLIENT)
409 if (connection_info_.flags.find("useWebrtc") != std::string::npos) {
410 VLOG(0) << "Attempting to connect using WebRTC.";
411 std::unique_ptr<protocol::CandidateSessionConfig> protocol_config =
412 protocol::CandidateSessionConfig::CreateEmpty();
413 protocol_config->set_webrtc_supported(true);
414 protocol_config->set_ice_supported(false);
415 client_->set_protocol_config(std::move(protocol_config));
416 }
417 #endif // defined(ENABLE_WEBRTC_REMOTING_CLIENT)
418 client_->Start(signaling_.get(), client_auth_config_, transport_context,
419 connection_info_.host_jid, capabilities_);
420 }
421
422 void ChromotingJniInstance::SetDeviceName(const std::string& device_name) {
423 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
424 runtime_->network_task_runner()->PostTask(
425 FROM_HERE, base::Bind(&ChromotingJniInstance::SetDeviceName,
426 GetWeakPtr(), device_name));
427 return;
428 }
429
430 device_name_ = device_name;
431 }
432
433 void ChromotingJniInstance::SendKeyEventInternal(int usb_key_code,
434 bool key_down) {
435 if (!runtime_->network_task_runner()->BelongsToCurrentThread()) {
436 runtime_->network_task_runner()->PostTask(
437 FROM_HERE, base::Bind(&ChromotingJniInstance::SendKeyEventInternal,
438 GetWeakPtr(), usb_key_code, key_down));
439 return;
440 }
441
442 protocol::KeyEvent event;
443 event.set_usb_keycode(usb_key_code);
444 event.set_pressed(key_down);
445 client_->input_stub()->InjectKeyEvent(event);
446 }
447
448 void ChromotingJniInstance::EnableStatsLogging(bool enabled) {
449 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
450
451 if (enabled && !stats_logging_enabled_) {
452 runtime_->network_task_runner()->PostDelayedTask(
453 FROM_HERE,
454 base::Bind(&ChromotingJniInstance::LogPerfStats, GetWeakPtr()),
455 base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs));
456 }
457 stats_logging_enabled_ = enabled;
458 }
459
460 void ChromotingJniInstance::LogPerfStats() {
461 DCHECK(runtime_->network_task_runner()->BelongsToCurrentThread());
462
463 if (!stats_logging_enabled_)
464 return;
465
466 __android_log_print(
467 ANDROID_LOG_INFO, "stats",
468 "Bandwidth:%.0f FrameRate:%.1f;"
469 " (Avg, Max) Capture:%.1f, %" PRId64 " Encode:%.1f, %" PRId64
470 " Decode:%.1f, %" PRId64 " Render:%.1f, %" PRId64 " RTL:%.0f, %" PRId64,
471 perf_tracker_->video_bandwidth(), perf_tracker_->video_frame_rate(),
472 perf_tracker_->video_capture_ms().Average(),
473 perf_tracker_->video_capture_ms().Max(),
474 perf_tracker_->video_encode_ms().Average(),
475 perf_tracker_->video_encode_ms().Max(),
476 perf_tracker_->video_decode_ms().Average(),
477 perf_tracker_->video_decode_ms().Max(),
478 perf_tracker_->video_paint_ms().Average(),
479 perf_tracker_->video_paint_ms().Max(),
480 perf_tracker_->round_trip_ms().Average(),
481 perf_tracker_->round_trip_ms().Max());
482
483 logger_->LogStatistics(perf_tracker_.get());
484
485 runtime_->network_task_runner()->PostDelayedTask(
486 FROM_HERE, base::Bind(&ChromotingJniInstance::LogPerfStats, GetWeakPtr()),
487 base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs));
488 }
489
490 void ChromotingJniInstance::ReleaseResources() {
491 logger_.reset();
492
493 // |client_| must be torn down before |signaling_|.
494 client_.reset();
495 audio_player_.reset();
496 video_renderer_.reset();
497 signaling_.reset();
498 perf_tracker_.reset();
499 client_context_.reset();
500 cursor_shape_stub_.reset();
501
502 weak_factory_.InvalidateWeakPtrs();
503 }
504
505 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/client/jni/chromoting_jni_instance.h ('k') | remoting/client/jni/connect_to_host_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698