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()) |
| 121 base::ResetAndReturn(&quit_closure_).Run(); |
| 122 } |
| 123 |
117 void It2MeNativeMessagingHost::ProcessHello( | 124 void It2MeNativeMessagingHost::ProcessHello( |
118 const base::DictionaryValue& message, | 125 const base::DictionaryValue& message, |
119 scoped_ptr<base::DictionaryValue> response) const { | 126 scoped_ptr<base::DictionaryValue> response) const { |
120 DCHECK(task_runner()->BelongsToCurrentThread()); | 127 DCHECK(task_runner()->BelongsToCurrentThread()); |
121 | 128 |
122 response->SetString("version", STRINGIZE(VERSION)); | 129 response->SetString("version", STRINGIZE(VERSION)); |
123 | 130 |
124 // This list will be populated when new features are added. | 131 // This list will be populated when new features are added. |
125 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 132 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
126 response->Set("supportedFeatures", supported_features_list.release()); | 133 response->Set("supportedFeatures", supported_features_list.release()); |
127 | 134 |
128 channel_->SendMessage(response.Pass()); | 135 channel_->SendMessage(response.PassAs<base::Value>()); |
129 } | 136 } |
130 | 137 |
131 void It2MeNativeMessagingHost::ProcessConnect( | 138 void It2MeNativeMessagingHost::ProcessConnect( |
132 const base::DictionaryValue& message, | 139 const base::DictionaryValue& message, |
133 scoped_ptr<base::DictionaryValue> response) { | 140 scoped_ptr<base::DictionaryValue> response) { |
134 DCHECK(task_runner()->BelongsToCurrentThread()); | 141 DCHECK(task_runner()->BelongsToCurrentThread()); |
135 | 142 |
136 if (it2me_host_.get()) { | 143 if (it2me_host_.get()) { |
137 SendErrorAndExit(response.Pass(), | 144 SendErrorAndExit(response.Pass(), |
138 "Connect can be called only when disconnected."); | 145 "Connect can be called only when disconnected."); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 #endif // !defined(NDEBUG) | 199 #endif // !defined(NDEBUG) |
193 | 200 |
194 // Create the It2Me host and start connecting. | 201 // Create the It2Me host and start connecting. |
195 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 202 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
196 host_context_->ui_task_runner(), | 203 host_context_->ui_task_runner(), |
197 weak_ptr_, | 204 weak_ptr_, |
198 xmpp_config, | 205 xmpp_config, |
199 directory_bot_jid_); | 206 directory_bot_jid_); |
200 it2me_host_->Connect(); | 207 it2me_host_->Connect(); |
201 | 208 |
202 channel_->SendMessage(response.Pass()); | 209 channel_->SendMessage(response.PassAs<base::Value>()); |
203 } | 210 } |
204 | 211 |
205 void It2MeNativeMessagingHost::ProcessDisconnect( | 212 void It2MeNativeMessagingHost::ProcessDisconnect( |
206 const base::DictionaryValue& message, | 213 const base::DictionaryValue& message, |
207 scoped_ptr<base::DictionaryValue> response) { | 214 scoped_ptr<base::DictionaryValue> response) { |
208 DCHECK(task_runner()->BelongsToCurrentThread()); | 215 DCHECK(task_runner()->BelongsToCurrentThread()); |
209 | 216 |
210 if (it2me_host_.get()) { | 217 if (it2me_host_.get()) { |
211 it2me_host_->Disconnect(); | 218 it2me_host_->Disconnect(); |
212 it2me_host_ = NULL; | 219 it2me_host_ = NULL; |
213 } | 220 } |
214 channel_->SendMessage(response.Pass()); | 221 channel_->SendMessage(response.PassAs<base::Value>()); |
215 } | 222 } |
216 | 223 |
217 void It2MeNativeMessagingHost::SendErrorAndExit( | 224 void It2MeNativeMessagingHost::SendErrorAndExit( |
218 scoped_ptr<base::DictionaryValue> response, | 225 scoped_ptr<base::DictionaryValue> response, |
219 const std::string& description) const { | 226 const std::string& description) const { |
220 DCHECK(task_runner()->BelongsToCurrentThread()); | 227 DCHECK(task_runner()->BelongsToCurrentThread()); |
221 | 228 |
222 LOG(ERROR) << description; | 229 LOG(ERROR) << description; |
223 | 230 |
224 response->SetString("type", "error"); | 231 response->SetString("type", "error"); |
225 response->SetString("description", description); | 232 response->SetString("description", description); |
226 channel_->SendMessage(response.Pass()); | 233 channel_->SendMessage(response.PassAs<base::Value>()); |
227 | 234 |
228 // Trigger a host shutdown by sending a NULL message. | 235 // Trigger a host shutdown by sending a NULL message. |
229 channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); | 236 channel_->SendMessage(scoped_ptr<base::Value>()); |
230 } | 237 } |
231 | 238 |
232 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 239 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
233 DCHECK(task_runner()->BelongsToCurrentThread()); | 240 DCHECK(task_runner()->BelongsToCurrentThread()); |
234 | 241 |
235 state_ = state; | 242 state_ = state; |
236 | 243 |
237 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 244 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
238 | 245 |
239 message->SetString("type", "hostStateChanged"); | 246 message->SetString("type", "hostStateChanged"); |
(...skipping 12 matching lines...) Expand all Loading... |
252 break; | 259 break; |
253 | 260 |
254 case kDisconnected: | 261 case kDisconnected: |
255 client_username_.clear(); | 262 client_username_.clear(); |
256 break; | 263 break; |
257 | 264 |
258 default: | 265 default: |
259 ; | 266 ; |
260 } | 267 } |
261 | 268 |
262 channel_->SendMessage(message.Pass()); | 269 channel_->SendMessage(message.PassAs<base::Value>()); |
263 } | 270 } |
264 | 271 |
265 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 272 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
266 DCHECK(task_runner()->BelongsToCurrentThread()); | 273 DCHECK(task_runner()->BelongsToCurrentThread()); |
267 | 274 |
268 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 275 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
269 | 276 |
270 message->SetString("type", "natPolicyChanged"); | 277 message->SetString("type", "natPolicyChanged"); |
271 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 278 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
272 channel_->SendMessage(message.Pass()); | 279 channel_->SendMessage(message.PassAs<base::Value>()); |
273 } | 280 } |
274 | 281 |
275 // Stores the Access Code for the web-app to query. | 282 // Stores the Access Code for the web-app to query. |
276 void It2MeNativeMessagingHost::OnStoreAccessCode( | 283 void It2MeNativeMessagingHost::OnStoreAccessCode( |
277 const std::string& access_code, | 284 const std::string& access_code, |
278 base::TimeDelta access_code_lifetime) { | 285 base::TimeDelta access_code_lifetime) { |
279 DCHECK(task_runner()->BelongsToCurrentThread()); | 286 DCHECK(task_runner()->BelongsToCurrentThread()); |
280 | 287 |
281 access_code_ = access_code; | 288 access_code_ = access_code; |
282 access_code_lifetime_ = access_code_lifetime; | 289 access_code_lifetime_ = access_code_lifetime; |
(...skipping 13 matching lines...) Expand all Loading... |
296 } | 303 } |
297 | 304 |
298 /* static */ | 305 /* static */ |
299 std::string It2MeNativeMessagingHost::HostStateToString( | 306 std::string It2MeNativeMessagingHost::HostStateToString( |
300 It2MeHostState host_state) { | 307 It2MeHostState host_state) { |
301 return ValueToName(kIt2MeHostStates, host_state); | 308 return ValueToName(kIt2MeHostStates, host_state); |
302 } | 309 } |
303 | 310 |
304 } // namespace remoting | 311 } // namespace remoting |
305 | 312 |
OLD | NEW |