OLD | NEW |
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/jni/chromoting_jni_instance.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "remoting/client/audio_player.h" | 9 #include "remoting/client/audio_player.h" |
10 #include "remoting/client/jni/chromoting_jni.h" | 10 #include "remoting/client/jni/chromoting_jni.h" |
11 #include "remoting/protocol/libjingle_transport_factory.h" | 11 #include "remoting/protocol/libjingle_transport_factory.h" |
12 | 12 |
13 namespace { | |
14 // TODO(solb) Move into location shared with client plugin. | 13 // TODO(solb) Move into location shared with client plugin. |
15 const char* const CHAT_SERVER = "talk.google.com"; | 14 const char* const CHAT_SERVER = "talk.google.com"; |
16 const int CHAT_PORT = 5222; | 15 const int CHAT_PORT = 5222; |
17 const bool CHAT_USE_TLS = true; | 16 const bool CHAT_USE_TLS = true; |
18 const char* const CHAT_AUTH_METHOD = "oauth2"; | |
19 } // namespace | |
20 | 17 |
21 namespace remoting { | 18 namespace remoting { |
22 | 19 |
23 ChromotingJniInstance::ChromotingJniInstance(const char* username, | 20 ChromotingJniInstance::ChromotingJniInstance(const char* username, |
24 const char* auth_token, | 21 const char* auth_token, |
25 const char* host_jid, | 22 const char* host_jid, |
26 const char* host_id, | 23 const char* host_id, |
27 const char* host_pubkey) { | 24 const char* host_pubkey) { |
28 DCHECK(ChromotingJni::GetInstance()-> | 25 DCHECK(ChromotingJni::GetInstance()-> |
29 ui_task_runner()->BelongsToCurrentThread()); | 26 ui_task_runner()->BelongsToCurrentThread()); |
30 | 27 |
31 username_ = username; | 28 username_ = username; |
32 auth_token_ = auth_token; | 29 auth_token_ = auth_token; |
33 host_jid_ = host_jid; | 30 host_jid_ = host_jid; |
34 host_id_ = host_id; | 31 host_id_ = host_id; |
35 host_pubkey_ = host_pubkey; | 32 host_pubkey_ = host_pubkey; |
36 | 33 |
37 ChromotingJni::GetInstance()->display_task_runner()->PostTask( | 34 ChromotingJni::GetInstance()->display_task_runner()->PostTask( |
38 FROM_HERE, | 35 FROM_HERE, |
39 base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread, | 36 base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread, |
40 this)); | 37 this)); |
41 } | 38 } |
42 | 39 |
43 ChromotingJniInstance::~ChromotingJniInstance() {} | 40 ChromotingJniInstance::~ChromotingJniInstance() {} |
44 | 41 |
45 void ChromotingJniInstance::Cleanup() { | 42 void ChromotingJniInstance::Cleanup() { |
46 if (!ChromotingJni::GetInstance()-> | 43 if (!ChromotingJni::GetInstance()-> |
47 network_task_runner()->BelongsToCurrentThread()) { | 44 display_task_runner()->BelongsToCurrentThread()) { |
48 ChromotingJni::GetInstance()->network_task_runner()->PostTask( | 45 ChromotingJni::GetInstance()->display_task_runner()->PostTask( |
49 FROM_HERE, | 46 FROM_HERE, |
50 base::Bind(&ChromotingJniInstance::Cleanup, this)); | 47 base::Bind(&ChromotingJniInstance::Cleanup, this)); |
51 return; | 48 return; |
52 } | 49 } |
53 | 50 |
54 username_ = ""; | 51 // This must be destroyed on the display thread before the producer is gone. |
55 auth_token_ = ""; | 52 view_.reset(); |
56 host_jid_ = ""; | |
57 host_id_ = ""; | |
58 host_pubkey_ = ""; | |
59 | 53 |
60 // |client_| must be torn down before |signaling_|. | 54 // The weak pointers must be invalidated on the same thread they were used. |
61 pin_callback_.Reset(); | 55 view_weak_factory_->InvalidateWeakPtrs(); |
62 client_.reset(); | 56 |
63 connection_.reset(); | 57 ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE, |
64 client_context_.reset(); | 58 base::Bind(&ChromotingJniInstance::DisconnectFromHostOnNetworkThread, |
65 client_config_.reset(); | 59 this)); |
66 signaling_.reset(); | |
67 signaling_config_.reset(); | |
68 network_settings_.reset(); | |
69 } | 60 } |
70 | 61 |
71 void ChromotingJniInstance::ProvideSecret(const char* pin) { | 62 void ChromotingJniInstance::ProvideSecret(const char* pin) { |
72 DCHECK(ChromotingJni::GetInstance()-> | 63 DCHECK(ChromotingJni::GetInstance()-> |
73 ui_task_runner()->BelongsToCurrentThread()); | 64 ui_task_runner()->BelongsToCurrentThread()); |
74 DCHECK(!pin_callback_.is_null()); | 65 DCHECK(!pin_callback_.is_null()); |
75 | 66 |
76 // We invoke the string constructor to ensure |pin| gets copied *before* the | 67 // We invoke the string constructor to ensure |pin| gets copied *before* the |
77 // asynchronous run, since Java might want it back as soon as we return. | 68 // asynchronous run, since Java might want it back as soon as we return. |
78 ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE, | 69 ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE, |
79 base::Bind(pin_callback_, pin)); | 70 base::Bind(pin_callback_, pin)); |
80 } | 71 } |
81 | 72 |
| 73 void ChromotingJniInstance::RedrawDesktop() { |
| 74 if (!ChromotingJni::GetInstance()-> |
| 75 display_task_runner()->BelongsToCurrentThread()) { |
| 76 ChromotingJni::GetInstance()->display_task_runner()->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&ChromotingJniInstance::RedrawDesktop, |
| 79 this)); |
| 80 return; |
| 81 } |
| 82 |
| 83 ChromotingJni::GetInstance()->RedrawCanvas(); |
| 84 } |
| 85 |
82 void ChromotingJniInstance::OnConnectionState( | 86 void ChromotingJniInstance::OnConnectionState( |
83 protocol::ConnectionToHost::State state, | 87 protocol::ConnectionToHost::State state, |
84 protocol::ErrorCode error) { | 88 protocol::ErrorCode error) { |
85 if (!ChromotingJni::GetInstance()-> | 89 if (!ChromotingJni::GetInstance()-> |
86 ui_task_runner()->BelongsToCurrentThread()) { | 90 ui_task_runner()->BelongsToCurrentThread()) { |
87 ChromotingJni::GetInstance()-> | 91 ChromotingJni::GetInstance()-> |
88 ui_task_runner()->PostTask( | 92 ui_task_runner()->PostTask( |
89 FROM_HERE, | 93 FROM_HERE, |
90 base::Bind(&ChromotingJniInstance::OnConnectionState, | 94 base::Bind(&ChromotingJniInstance::OnConnectionState, |
91 this, | 95 this, |
(...skipping 29 matching lines...) Expand all Loading... |
121 scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> | 125 scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> |
122 ChromotingJniInstance::GetTokenFetcher(const std::string& host_public_key) { | 126 ChromotingJniInstance::GetTokenFetcher(const std::string& host_public_key) { |
123 // Return null to indicate that third-party authentication is unsupported. | 127 // Return null to indicate that third-party authentication is unsupported. |
124 return scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>(); | 128 return scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>(); |
125 } | 129 } |
126 | 130 |
127 void ChromotingJniInstance::ConnectToHostOnDisplayThread() { | 131 void ChromotingJniInstance::ConnectToHostOnDisplayThread() { |
128 DCHECK(ChromotingJni::GetInstance()-> | 132 DCHECK(ChromotingJni::GetInstance()-> |
129 display_task_runner()->BelongsToCurrentThread()); | 133 display_task_runner()->BelongsToCurrentThread()); |
130 | 134 |
131 if (!frame_consumer_.get()) { | 135 frame_consumer_ = new FrameConsumerProxy( |
132 frame_consumer_ = new FrameConsumerProxy( | 136 ChromotingJni::GetInstance()->display_task_runner()); |
133 ChromotingJni::GetInstance()->display_task_runner()); | 137 view_.reset(new JniFrameConsumer()); |
134 // TODO(solb) Instantiate some FrameConsumer implementation and attach it. | 138 view_weak_factory_.reset(new base::WeakPtrFactory<JniFrameConsumer>( |
135 } | 139 view_.get())); |
| 140 frame_consumer_->Attach(view_weak_factory_->GetWeakPtr()); |
136 | 141 |
137 ChromotingJni::GetInstance()->network_task_runner()->PostTask( | 142 ChromotingJni::GetInstance()->network_task_runner()->PostTask( |
138 FROM_HERE, | 143 FROM_HERE, |
139 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, | 144 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, |
140 this)); | 145 this)); |
141 } | 146 } |
142 | 147 |
143 void ChromotingJniInstance::ConnectToHostOnNetworkThread() { | 148 void ChromotingJniInstance::ConnectToHostOnNetworkThread() { |
144 DCHECK(ChromotingJni::GetInstance()-> | 149 DCHECK(ChromotingJni::GetInstance()-> |
145 network_task_runner()->BelongsToCurrentThread()); | 150 network_task_runner()->BelongsToCurrentThread()); |
146 | 151 |
147 client_config_.reset(new ClientConfig()); | 152 client_config_.reset(new ClientConfig()); |
148 client_config_->host_jid = host_jid_; | 153 client_config_->host_jid = host_jid_; |
149 client_config_->host_public_key = host_pubkey_; | 154 client_config_->host_public_key = host_pubkey_; |
150 | 155 |
151 client_config_->fetch_secret_callback = base::Bind( | 156 client_config_->fetch_secret_callback = base::Bind( |
152 &ChromotingJniInstance::FetchSecret, | 157 &ChromotingJniInstance::FetchSecret, |
153 this); | 158 this); |
154 client_config_->authentication_tag = host_id_; | 159 client_config_->authentication_tag = host_id_; |
155 | 160 |
156 // TODO(solb) Move these hardcoded values elsewhere: | |
157 client_config_->authentication_methods.push_back( | 161 client_config_->authentication_methods.push_back( |
158 protocol::AuthenticationMethod::FromString("spake2_hmac")); | 162 protocol::AuthenticationMethod::FromString("spake2_hmac")); |
159 client_config_->authentication_methods.push_back( | 163 client_config_->authentication_methods.push_back( |
160 protocol::AuthenticationMethod::FromString("spake2_plain")); | 164 protocol::AuthenticationMethod::FromString("spake2_plain")); |
161 | 165 |
162 client_context_.reset(new ClientContext( | 166 client_context_.reset(new ClientContext( |
163 ChromotingJni::GetInstance()->network_task_runner().get())); | 167 ChromotingJni::GetInstance()->network_task_runner().get())); |
164 client_context_->Start(); | 168 client_context_->Start(); |
165 | 169 |
166 connection_.reset(new protocol::ConnectionToHost(true)); | 170 connection_.reset(new protocol::ConnectionToHost(true)); |
167 | 171 |
168 client_.reset(new ChromotingClient(*client_config_, | 172 client_.reset(new ChromotingClient(*client_config_, |
169 client_context_.get(), | 173 client_context_.get(), |
170 connection_.get(), | 174 connection_.get(), |
171 this, | 175 this, |
172 frame_consumer_, | 176 frame_consumer_, |
173 scoped_ptr<AudioPlayer>())); | 177 scoped_ptr<AudioPlayer>())); |
174 | 178 |
| 179 view_->set_frame_producer(client_->GetFrameProducer()); |
| 180 |
175 signaling_config_.reset(new XmppSignalStrategy::XmppServerConfig()); | 181 signaling_config_.reset(new XmppSignalStrategy::XmppServerConfig()); |
176 signaling_config_->host = CHAT_SERVER; | 182 signaling_config_->host = CHAT_SERVER; |
177 signaling_config_->port = CHAT_PORT; | 183 signaling_config_->port = CHAT_PORT; |
178 signaling_config_->use_tls = CHAT_USE_TLS; | 184 signaling_config_->use_tls = CHAT_USE_TLS; |
179 | 185 |
180 signaling_.reset(new XmppSignalStrategy( | 186 signaling_.reset(new XmppSignalStrategy( |
181 ChromotingJni::GetInstance()->url_requester(), | 187 ChromotingJni::GetInstance()->url_requester(), |
182 username_, | 188 username_, |
183 auth_token_, | 189 auth_token_, |
184 CHAT_AUTH_METHOD, | 190 "oauth2", |
185 *signaling_config_)); | 191 *signaling_config_)); |
186 | 192 |
187 network_settings_.reset(new NetworkSettings( | 193 network_settings_.reset(new NetworkSettings( |
188 NetworkSettings::NAT_TRAVERSAL_OUTGOING)); | 194 NetworkSettings::NAT_TRAVERSAL_OUTGOING)); |
189 scoped_ptr<protocol::TransportFactory> fact( | 195 scoped_ptr<protocol::TransportFactory> fact( |
190 protocol::LibjingleTransportFactory::Create( | 196 protocol::LibjingleTransportFactory::Create( |
191 *network_settings_, | 197 *network_settings_, |
192 ChromotingJni::GetInstance()->url_requester())); | 198 ChromotingJni::GetInstance()->url_requester())); |
193 | 199 |
194 client_->Start(signaling_.get(), fact.Pass()); | 200 client_->Start(signaling_.get(), fact.Pass()); |
195 } | 201 } |
196 | 202 |
| 203 void ChromotingJniInstance::DisconnectFromHostOnNetworkThread() { |
| 204 DCHECK(ChromotingJni::GetInstance()-> |
| 205 network_task_runner()->BelongsToCurrentThread()); |
| 206 |
| 207 username_ = ""; |
| 208 auth_token_ = ""; |
| 209 host_jid_ = ""; |
| 210 host_id_ = ""; |
| 211 host_pubkey_ = ""; |
| 212 |
| 213 // |client_| must be torn down before |signaling_|. |
| 214 connection_.reset(); |
| 215 client_.reset(); |
| 216 } |
| 217 |
197 void ChromotingJniInstance::FetchSecret( | 218 void ChromotingJniInstance::FetchSecret( |
198 bool pairable, | 219 bool pairable, |
199 const protocol::SecretFetchedCallback& callback) { | 220 const protocol::SecretFetchedCallback& callback) { |
200 if (!ChromotingJni::GetInstance()-> | 221 if (!ChromotingJni::GetInstance()-> |
201 ui_task_runner()->BelongsToCurrentThread()) { | 222 ui_task_runner()->BelongsToCurrentThread()) { |
202 ChromotingJni::GetInstance()->ui_task_runner()->PostTask( | 223 ChromotingJni::GetInstance()->ui_task_runner()->PostTask( |
203 FROM_HERE, | 224 FROM_HERE, |
204 base::Bind(&ChromotingJniInstance::FetchSecret, | 225 base::Bind(&ChromotingJniInstance::FetchSecret, |
205 this, | 226 this, |
206 pairable, | 227 pairable, |
207 callback)); | 228 callback)); |
208 return; | 229 return; |
209 } | 230 } |
210 | 231 |
211 pin_callback_ = callback; | 232 pin_callback_ = callback; |
212 ChromotingJni::GetInstance()->DisplayAuthenticationPrompt(); | 233 ChromotingJni::GetInstance()->DisplayAuthenticationPrompt(); |
213 } | 234 } |
214 | 235 |
215 } // namespace remoting | 236 } // namespace remoting |
OLD | NEW |