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" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, | 35 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, |
36 {kConnected, "CONNECTED"}, | 36 {kConnected, "CONNECTED"}, |
37 {kDisconnecting, "DISCONNECTING"}, | 37 {kDisconnecting, "DISCONNECTING"}, |
38 {kError, "ERROR"}, | 38 {kError, "ERROR"}, |
39 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; | 39 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 It2MeNativeMessagingHost::It2MeNativeMessagingHost( | 43 It2MeNativeMessagingHost::It2MeNativeMessagingHost( |
44 scoped_refptr<AutoThreadTaskRunner> task_runner, | 44 scoped_refptr<AutoThreadTaskRunner> task_runner, |
45 scoped_ptr<NativeMessagingChannel> channel, | 45 scoped_ptr<extensions::NativeMessagingChannel> channel, |
46 scoped_ptr<It2MeHostFactory> factory) | 46 scoped_ptr<It2MeHostFactory> factory) |
47 : channel_(channel.Pass()), | 47 : channel_(channel.Pass()), |
48 factory_(factory.Pass()), | 48 factory_(factory.Pass()), |
49 weak_factory_(this) { | 49 weak_factory_(this) { |
50 weak_ptr_ = weak_factory_.GetWeakPtr(); | 50 weak_ptr_ = weak_factory_.GetWeakPtr(); |
51 | 51 |
52 // Initialize the host context to manage the threads for the it2me host. | 52 // Initialize the host context to manage the threads for the it2me host. |
53 // The native messaging host, rather than the It2MeHost object, owns and | 53 // The native messaging host, rather than the It2MeHost object, owns and |
54 // maintains the lifetime of the host context. | 54 // maintains the lifetime of the host context. |
55 | 55 |
(...skipping 12 matching lines...) Expand all Loading... | |
68 | 68 |
69 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { | 69 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
70 DCHECK(task_runner()->BelongsToCurrentThread()); | 70 DCHECK(task_runner()->BelongsToCurrentThread()); |
71 | 71 |
72 if (it2me_host_.get()) { | 72 if (it2me_host_.get()) { |
73 it2me_host_->Disconnect(); | 73 it2me_host_->Disconnect(); |
74 it2me_host_ = NULL; | 74 it2me_host_ = NULL; |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 void It2MeNativeMessagingHost::Start(const base::Closure& quit_closure) const { | 78 void It2MeNativeMessagingHost::Start(const base::Closure& quit_closure) { |
79 DCHECK(task_runner()->BelongsToCurrentThread()); | |
80 DCHECK(!quit_closure.is_null()); | |
81 | |
82 quit_closure_ = quit_closure; | |
83 | |
84 channel_->Start(this); | |
85 } | |
86 | |
87 void It2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) { | |
79 DCHECK(task_runner()->BelongsToCurrentThread()); | 88 DCHECK(task_runner()->BelongsToCurrentThread()); |
80 | 89 |
81 channel_->Start( | 90 scoped_ptr<base::DictionaryValue> message_dict( |
82 base::Bind(&It2MeNativeMessagingHost::ProcessMessage, weak_ptr_), | 91 static_cast<base::DictionaryValue*>(message.release())); |
83 quit_closure); | |
84 } | |
85 | |
86 void It2MeNativeMessagingHost::ProcessMessage( | |
87 scoped_ptr<base::DictionaryValue> message) { | |
88 DCHECK(task_runner()->BelongsToCurrentThread()); | |
89 | |
90 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 92 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
91 | 93 |
92 // If the client supplies an ID, it will expect it in the response. This | 94 // If the client supplies an ID, it will expect it in the response. This |
93 // might be a string or a number, so cope with both. | 95 // might be a string or a number, so cope with both. |
94 const base::Value* id; | 96 const base::Value* id; |
95 if (message->Get("id", &id)) | 97 if (message_dict->Get("id", &id)) |
96 response->Set("id", id->DeepCopy()); | 98 response->Set("id", id->DeepCopy()); |
97 | 99 |
98 std::string type; | 100 std::string type; |
99 if (!message->GetString("type", &type)) { | 101 if (!message_dict->GetString("type", &type)) { |
100 SendErrorAndExit(response.Pass(), "'type' not found in request."); | 102 SendErrorAndExit(response.Pass(), "'type' not found in request."); |
101 return; | 103 return; |
102 } | 104 } |
103 | 105 |
104 response->SetString("type", type + "Response"); | 106 response->SetString("type", type + "Response"); |
105 | 107 |
106 if (type == "hello") { | 108 if (type == "hello") { |
107 ProcessHello(*message, response.Pass()); | 109 ProcessHello(*message_dict, response.Pass()); |
108 } else if (type == "connect") { | 110 } else if (type == "connect") { |
109 ProcessConnect(*message, response.Pass()); | 111 ProcessConnect(*message_dict, response.Pass()); |
110 } else if (type == "disconnect") { | 112 } else if (type == "disconnect") { |
111 ProcessDisconnect(*message, response.Pass()); | 113 ProcessDisconnect(*message_dict, response.Pass()); |
112 } else { | 114 } else { |
113 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); | 115 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); |
114 } | 116 } |
115 } | 117 } |
116 | 118 |
119 void It2MeNativeMessagingHost::OnDisconnect() { | |
120 if (!quit_closure_.is_null()) { | |
Sergey Ulanov
2014/09/18 18:08:35
nit: remove {} for consistency with other single-l
kelvinp
2014/09/18 19:03:50
Done.
| |
121 base::ResetAndReturn(&quit_closure_).Run(); | |
122 } | |
123 } | |
124 | |
117 void It2MeNativeMessagingHost::ProcessHello( | 125 void It2MeNativeMessagingHost::ProcessHello( |
118 const base::DictionaryValue& message, | 126 const base::DictionaryValue& message, |
119 scoped_ptr<base::DictionaryValue> response) const { | 127 scoped_ptr<base::DictionaryValue> response) const { |
120 DCHECK(task_runner()->BelongsToCurrentThread()); | 128 DCHECK(task_runner()->BelongsToCurrentThread()); |
121 | 129 |
122 response->SetString("version", STRINGIZE(VERSION)); | 130 response->SetString("version", STRINGIZE(VERSION)); |
123 | 131 |
124 // This list will be populated when new features are added. | 132 // This list will be populated when new features are added. |
125 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 133 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
126 response->Set("supportedFeatures", supported_features_list.release()); | 134 response->Set("supportedFeatures", supported_features_list.release()); |
127 | 135 |
128 channel_->SendMessage(response.Pass()); | 136 channel_->SendMessage(response.PassAs<base::Value>()); |
129 } | 137 } |
130 | 138 |
131 void It2MeNativeMessagingHost::ProcessConnect( | 139 void It2MeNativeMessagingHost::ProcessConnect( |
132 const base::DictionaryValue& message, | 140 const base::DictionaryValue& message, |
133 scoped_ptr<base::DictionaryValue> response) { | 141 scoped_ptr<base::DictionaryValue> response) { |
134 DCHECK(task_runner()->BelongsToCurrentThread()); | 142 DCHECK(task_runner()->BelongsToCurrentThread()); |
135 | 143 |
136 if (it2me_host_.get()) { | 144 if (it2me_host_.get()) { |
137 SendErrorAndExit(response.Pass(), | 145 SendErrorAndExit(response.Pass(), |
138 "Connect can be called only when disconnected."); | 146 "Connect can be called only when disconnected."); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 #endif // !defined(NDEBUG) | 200 #endif // !defined(NDEBUG) |
193 | 201 |
194 // Create the It2Me host and start connecting. | 202 // Create the It2Me host and start connecting. |
195 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 203 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
196 host_context_->ui_task_runner(), | 204 host_context_->ui_task_runner(), |
197 weak_ptr_, | 205 weak_ptr_, |
198 xmpp_config, | 206 xmpp_config, |
199 directory_bot_jid_); | 207 directory_bot_jid_); |
200 it2me_host_->Connect(); | 208 it2me_host_->Connect(); |
201 | 209 |
202 channel_->SendMessage(response.Pass()); | 210 channel_->SendMessage(response.PassAs<base::Value>()); |
203 } | 211 } |
204 | 212 |
205 void It2MeNativeMessagingHost::ProcessDisconnect( | 213 void It2MeNativeMessagingHost::ProcessDisconnect( |
206 const base::DictionaryValue& message, | 214 const base::DictionaryValue& message, |
207 scoped_ptr<base::DictionaryValue> response) { | 215 scoped_ptr<base::DictionaryValue> response) { |
208 DCHECK(task_runner()->BelongsToCurrentThread()); | 216 DCHECK(task_runner()->BelongsToCurrentThread()); |
209 | 217 |
210 if (it2me_host_.get()) { | 218 if (it2me_host_.get()) { |
211 it2me_host_->Disconnect(); | 219 it2me_host_->Disconnect(); |
212 it2me_host_ = NULL; | 220 it2me_host_ = NULL; |
213 } | 221 } |
214 channel_->SendMessage(response.Pass()); | 222 channel_->SendMessage(response.PassAs<base::Value>()); |
215 } | 223 } |
216 | 224 |
217 void It2MeNativeMessagingHost::SendErrorAndExit( | 225 void It2MeNativeMessagingHost::SendErrorAndExit( |
218 scoped_ptr<base::DictionaryValue> response, | 226 scoped_ptr<base::DictionaryValue> response, |
219 const std::string& description) const { | 227 const std::string& description) const { |
220 DCHECK(task_runner()->BelongsToCurrentThread()); | 228 DCHECK(task_runner()->BelongsToCurrentThread()); |
221 | 229 |
222 LOG(ERROR) << description; | 230 LOG(ERROR) << description; |
223 | 231 |
224 response->SetString("type", "error"); | 232 response->SetString("type", "error"); |
225 response->SetString("description", description); | 233 response->SetString("description", description); |
226 channel_->SendMessage(response.Pass()); | 234 channel_->SendMessage(response.PassAs<base::Value>()); |
227 | 235 |
228 // Trigger a host shutdown by sending a NULL message. | 236 // Trigger a host shutdown by sending a NULL message. |
229 channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); | 237 channel_->SendMessage(scoped_ptr<base::Value>()); |
230 } | 238 } |
231 | 239 |
232 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 240 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
233 DCHECK(task_runner()->BelongsToCurrentThread()); | 241 DCHECK(task_runner()->BelongsToCurrentThread()); |
234 | 242 |
235 state_ = state; | 243 state_ = state; |
236 | 244 |
237 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 245 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
238 | 246 |
239 message->SetString("type", "hostStateChanged"); | 247 message->SetString("type", "hostStateChanged"); |
(...skipping 12 matching lines...) Expand all Loading... | |
252 break; | 260 break; |
253 | 261 |
254 case kDisconnected: | 262 case kDisconnected: |
255 client_username_.clear(); | 263 client_username_.clear(); |
256 break; | 264 break; |
257 | 265 |
258 default: | 266 default: |
259 ; | 267 ; |
260 } | 268 } |
261 | 269 |
262 channel_->SendMessage(message.Pass()); | 270 channel_->SendMessage(message.PassAs<base::Value>()); |
263 } | 271 } |
264 | 272 |
265 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 273 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
266 DCHECK(task_runner()->BelongsToCurrentThread()); | 274 DCHECK(task_runner()->BelongsToCurrentThread()); |
267 | 275 |
268 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 276 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
269 | 277 |
270 message->SetString("type", "natPolicyChanged"); | 278 message->SetString("type", "natPolicyChanged"); |
271 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 279 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
272 channel_->SendMessage(message.Pass()); | 280 channel_->SendMessage(message.PassAs<base::Value>()); |
273 } | 281 } |
274 | 282 |
275 // Stores the Access Code for the web-app to query. | 283 // Stores the Access Code for the web-app to query. |
276 void It2MeNativeMessagingHost::OnStoreAccessCode( | 284 void It2MeNativeMessagingHost::OnStoreAccessCode( |
277 const std::string& access_code, | 285 const std::string& access_code, |
278 base::TimeDelta access_code_lifetime) { | 286 base::TimeDelta access_code_lifetime) { |
279 DCHECK(task_runner()->BelongsToCurrentThread()); | 287 DCHECK(task_runner()->BelongsToCurrentThread()); |
280 | 288 |
281 access_code_ = access_code; | 289 access_code_ = access_code; |
282 access_code_lifetime_ = access_code_lifetime; | 290 access_code_lifetime_ = access_code_lifetime; |
(...skipping 13 matching lines...) Expand all Loading... | |
296 } | 304 } |
297 | 305 |
298 /* static */ | 306 /* static */ |
299 std::string It2MeNativeMessagingHost::HostStateToString( | 307 std::string It2MeNativeMessagingHost::HostStateToString( |
300 It2MeHostState host_state) { | 308 It2MeHostState host_state) { |
301 return ValueToName(kIt2MeHostStates, host_state); | 309 return ValueToName(kIt2MeHostStates, host_state); |
302 } | 310 } |
303 | 311 |
304 } // namespace remoting | 312 } // namespace remoting |
305 | 313 |
OLD | NEW |