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/host/it2me/it2me_native_messaging_host.h" | 5 #include "remoting/host/it2me/it2me_native_messaging_host.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/callback_helpers.h" | |
12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringize_macros.h" | 16 #include "base/strings/stringize_macros.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
19 #include "remoting/base/auth_token_util.h" | 20 #include "remoting/base/auth_token_util.h" |
20 #include "remoting/host/chromoting_host_context.h" | 21 #include "remoting/host/chromoting_host_context.h" |
21 #include "remoting/host/host_exit_codes.h" | 22 #include "remoting/host/host_exit_codes.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 bool xmpp_server_valid = | 55 bool xmpp_server_valid = |
55 net::ParseHostAndPort(service_urls->xmpp_server_address(), | 56 net::ParseHostAndPort(service_urls->xmpp_server_address(), |
56 &xmpp_server_config_.host, | 57 &xmpp_server_config_.host, |
57 &xmpp_server_config_.port); | 58 &xmpp_server_config_.port); |
58 DCHECK(xmpp_server_valid); | 59 DCHECK(xmpp_server_valid); |
59 | 60 |
60 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); | 61 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); |
61 directory_bot_jid_ = service_urls->directory_bot_jid(); | 62 directory_bot_jid_ = service_urls->directory_bot_jid(); |
62 } | 63 } |
63 | 64 |
64 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {} | 65 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
66 DCHECK(task_runner()->BelongsToCurrentThread()); | |
67 } | |
65 | 68 |
66 void It2MeNativeMessagingHost::SetSendMessageCallback( | 69 void It2MeNativeMessagingHost::Start( |
67 const SendMessageCallback& send_message) { | 70 base::PlatformFile input, |
68 send_message_ = send_message; | 71 base::PlatformFile output, |
72 const base::Closure& quit_closure) { | |
73 DCHECK(task_runner()->BelongsToCurrentThread()); | |
74 | |
75 // Set up the native messaging channel. | |
76 channel_.reset( | |
77 new NativeMessagingChannel( | |
78 base::Bind(&It2MeNativeMessagingHost::ProcessMessage, weak_ptr_), | |
79 input, | |
80 output)); | |
81 | |
82 quit_closure_ = quit_closure; | |
83 channel_->Start(base::Bind(&It2MeNativeMessagingHost::ShutDown, weak_ptr_)); | |
69 } | 84 } |
70 | 85 |
71 void It2MeNativeMessagingHost::ProcessMessage( | 86 void It2MeNativeMessagingHost::ProcessMessage( |
72 scoped_ptr<base::DictionaryValue> message) { | 87 scoped_ptr<base::DictionaryValue> message) { |
88 DCHECK(task_runner()->BelongsToCurrentThread()); | |
89 | |
73 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 90 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
74 | 91 |
75 // If the client supplies an ID, it will expect it in the response. This | 92 // If the client supplies an ID, it will expect it in the response. This |
76 // might be a string or a number, so cope with both. | 93 // might be a string or a number, so cope with both. |
77 const base::Value* id; | 94 const base::Value* id; |
78 if (message->Get("id", &id)) | 95 if (message->Get("id", &id)) |
79 response->Set("id", id->DeepCopy()); | 96 response->Set("id", id->DeepCopy()); |
80 | 97 |
81 std::string type; | 98 std::string type; |
82 if (!message->GetString("type", &type)) { | 99 if (!message->GetString("type", &type)) { |
(...skipping 10 matching lines...) Expand all Loading... | |
93 } else if (type == "disconnect") { | 110 } else if (type == "disconnect") { |
94 ProcessDisconnect(*message, response.Pass()); | 111 ProcessDisconnect(*message, response.Pass()); |
95 } else { | 112 } else { |
96 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); | 113 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); |
97 } | 114 } |
98 } | 115 } |
99 | 116 |
100 void It2MeNativeMessagingHost::ProcessHello( | 117 void It2MeNativeMessagingHost::ProcessHello( |
101 const base::DictionaryValue& message, | 118 const base::DictionaryValue& message, |
102 scoped_ptr<base::DictionaryValue> response) { | 119 scoped_ptr<base::DictionaryValue> response) { |
120 DCHECK(task_runner()->BelongsToCurrentThread()); | |
121 | |
103 response->SetString("version", STRINGIZE(VERSION)); | 122 response->SetString("version", STRINGIZE(VERSION)); |
104 | 123 |
105 // This list will be populated when new features are added. | 124 // This list will be populated when new features are added. |
106 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 125 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
107 response->Set("supportedFeatures", supported_features_list.release()); | 126 response->Set("supportedFeatures", supported_features_list.release()); |
108 | 127 |
109 send_message_.Run(response.Pass()); | 128 channel_->SendMessage(response.Pass()); |
110 } | 129 } |
111 | 130 |
112 void It2MeNativeMessagingHost::ProcessConnect( | 131 void It2MeNativeMessagingHost::ProcessConnect( |
113 const base::DictionaryValue& message, | 132 const base::DictionaryValue& message, |
114 scoped_ptr<base::DictionaryValue> response) { | 133 scoped_ptr<base::DictionaryValue> response) { |
134 DCHECK(task_runner()->BelongsToCurrentThread()); | |
135 | |
115 if (it2me_host_.get()) { | 136 if (it2me_host_.get()) { |
116 SendErrorAndExit(response.Pass(), | 137 SendErrorAndExit(response.Pass(), |
117 "Connect can be called only when disconnected."); | 138 "Connect can be called only when disconnected."); |
118 return; | 139 return; |
119 } | 140 } |
120 | 141 |
121 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; | 142 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; |
122 | 143 |
123 if (!message.GetString("userName", &xmpp_config.username)) { | 144 if (!message.GetString("userName", &xmpp_config.username)) { |
124 SendErrorAndExit(response.Pass(), "'userName' not found in request."); | 145 SendErrorAndExit(response.Pass(), "'userName' not found in request."); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 #endif // !defined(NDEBUG) | 192 #endif // !defined(NDEBUG) |
172 | 193 |
173 // Create the It2Me host and start connecting. | 194 // Create the It2Me host and start connecting. |
174 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 195 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
175 host_context_->ui_task_runner(), | 196 host_context_->ui_task_runner(), |
176 weak_ptr_, | 197 weak_ptr_, |
177 xmpp_config, | 198 xmpp_config, |
178 directory_bot_jid_); | 199 directory_bot_jid_); |
179 it2me_host_->Connect(); | 200 it2me_host_->Connect(); |
180 | 201 |
181 send_message_.Run(response.Pass()); | 202 channel_->SendMessage(response.Pass()); |
182 } | 203 } |
183 | 204 |
184 void It2MeNativeMessagingHost::ProcessDisconnect( | 205 void It2MeNativeMessagingHost::ProcessDisconnect( |
185 const base::DictionaryValue& message, | 206 const base::DictionaryValue& message, |
186 scoped_ptr<base::DictionaryValue> response) { | 207 scoped_ptr<base::DictionaryValue> response) { |
208 DCHECK(task_runner()->BelongsToCurrentThread()); | |
209 | |
187 if (it2me_host_.get()) { | 210 if (it2me_host_.get()) { |
188 it2me_host_->Disconnect(); | 211 it2me_host_->Disconnect(); |
189 it2me_host_ = NULL; | 212 it2me_host_ = NULL; |
190 } | 213 } |
191 send_message_.Run(response.Pass()); | 214 channel_->SendMessage(response.Pass()); |
192 } | 215 } |
193 | 216 |
194 void It2MeNativeMessagingHost::SendErrorAndExit( | 217 void It2MeNativeMessagingHost::SendErrorAndExit( |
195 scoped_ptr<base::DictionaryValue> response, | 218 scoped_ptr<base::DictionaryValue> response, |
196 const std::string& description) { | 219 const std::string& description) { |
220 DCHECK(task_runner()->BelongsToCurrentThread()); | |
221 | |
197 LOG(ERROR) << description; | 222 LOG(ERROR) << description; |
198 | 223 |
199 response->SetString("type", "error"); | 224 response->SetString("type", "error"); |
200 response->SetString("description", description); | 225 response->SetString("description", description); |
201 send_message_.Run(response.Pass()); | 226 channel_->SendMessage(response.Pass()); |
202 | 227 |
203 // Trigger a host shutdown by sending a NULL message. | 228 // Trigger a host shutdown by sending a NULL message. |
204 send_message_.Run(scoped_ptr<base::DictionaryValue>()); | 229 channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); |
205 } | 230 } |
206 | 231 |
207 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 232 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
208 DCHECK(task_runner()->BelongsToCurrentThread()); | 233 DCHECK(task_runner()->BelongsToCurrentThread()); |
209 | 234 |
210 state_ = state; | 235 state_ = state; |
211 | 236 |
212 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 237 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
213 | 238 |
214 message->SetString("type", "hostStateChanged"); | 239 message->SetString("type", "hostStateChanged"); |
(...skipping 12 matching lines...) Expand all Loading... | |
227 break; | 252 break; |
228 | 253 |
229 case kDisconnected: | 254 case kDisconnected: |
230 client_username_.clear(); | 255 client_username_.clear(); |
231 break; | 256 break; |
232 | 257 |
233 default: | 258 default: |
234 ; | 259 ; |
235 } | 260 } |
236 | 261 |
237 send_message_.Run(message.Pass()); | 262 channel_->SendMessage(message.Pass()); |
238 } | 263 } |
239 | 264 |
240 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 265 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
241 DCHECK(task_runner()->BelongsToCurrentThread()); | 266 DCHECK(task_runner()->BelongsToCurrentThread()); |
242 | 267 |
243 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 268 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
244 | 269 |
245 message->SetString("type", "natPolicyChanged"); | 270 message->SetString("type", "natPolicyChanged"); |
246 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 271 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
247 send_message_.Run(message.Pass()); | 272 channel_->SendMessage(message.Pass()); |
248 } | 273 } |
249 | 274 |
250 // Stores the Access Code for the web-app to query. | 275 // Stores the Access Code for the web-app to query. |
251 void It2MeNativeMessagingHost::OnStoreAccessCode( | 276 void It2MeNativeMessagingHost::OnStoreAccessCode( |
252 const std::string& access_code, | 277 const std::string& access_code, |
253 base::TimeDelta access_code_lifetime) { | 278 base::TimeDelta access_code_lifetime) { |
254 DCHECK(task_runner()->BelongsToCurrentThread()); | 279 DCHECK(task_runner()->BelongsToCurrentThread()); |
255 | 280 |
256 access_code_ = access_code; | 281 access_code_ = access_code; |
257 access_code_lifetime_ = access_code_lifetime; | 282 access_code_lifetime_ = access_code_lifetime; |
258 } | 283 } |
259 | 284 |
260 // Stores the client user's name for the web-app to query. | 285 // Stores the client user's name for the web-app to query. |
261 void It2MeNativeMessagingHost::OnClientAuthenticated( | 286 void It2MeNativeMessagingHost::OnClientAuthenticated( |
262 const std::string& client_username) { | 287 const std::string& client_username) { |
263 DCHECK(task_runner()->BelongsToCurrentThread()); | 288 DCHECK(task_runner()->BelongsToCurrentThread()); |
264 | 289 |
265 client_username_ = client_username; | 290 client_username_ = client_username; |
266 } | 291 } |
267 | 292 |
268 void It2MeNativeMessagingHost::ShutDown() { | 293 void It2MeNativeMessagingHost::ShutDown() { |
Sergey Ulanov
2013/12/12 19:10:28
Do you really need this method? I think you can ju
weitao
2013/12/12 23:33:48
Done.
| |
294 DCHECK(task_runner()->BelongsToCurrentThread()); | |
295 | |
269 if (it2me_host_.get()) { | 296 if (it2me_host_.get()) { |
270 it2me_host_->Disconnect(); | 297 it2me_host_->Disconnect(); |
271 it2me_host_ = NULL; | 298 it2me_host_ = NULL; |
272 } | 299 } |
273 host_context_.reset(); | 300 |
301 if (!quit_closure_.is_null()) | |
302 base::ResetAndReturn(&quit_closure_).Run(); | |
274 } | 303 } |
275 | 304 |
276 scoped_refptr<AutoThreadTaskRunner> It2MeNativeMessagingHost::task_runner() { | 305 scoped_refptr<AutoThreadTaskRunner> It2MeNativeMessagingHost::task_runner() { |
277 return host_context_->ui_task_runner(); | 306 return host_context_->ui_task_runner(); |
278 } | 307 } |
279 | 308 |
280 /* static */ | 309 /* static */ |
281 std::string It2MeNativeMessagingHost::HostStateToString( | 310 std::string It2MeNativeMessagingHost::HostStateToString( |
282 It2MeHostState host_state) { | 311 It2MeHostState host_state) { |
283 return ValueToName(kIt2MeHostStates, host_state); | 312 return ValueToName(kIt2MeHostStates, host_state); |
284 } | 313 } |
285 | 314 |
286 } // namespace remoting | 315 } // namespace remoting |
OLD | NEW |