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

Side by Side Diff: remoting/client/chromoting_session.cc

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

Powered by Google App Engine
This is Rietveld 408576698