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/json/json_reader.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/message_loop/message_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/base/net_util.h" | 19 #include "net/base/net_util.h" |
19 #include "net/url_request/url_fetcher.h" | 20 #include "net/url_request/url_fetcher.h" |
20 #include "remoting/base/auth_token_util.h" | 21 #include "remoting/base/auth_token_util.h" |
21 #include "remoting/base/service_urls.h" | 22 #include "remoting/base/service_urls.h" |
22 #include "remoting/host/chromoting_host_context.h" | 23 #include "remoting/host/chromoting_host_context.h" |
23 #include "remoting/host/host_exit_codes.h" | 24 #include "remoting/host/host_exit_codes.h" |
(...skipping 10 matching lines...) Expand all Loading... |
34 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, | 35 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, |
35 {kConnected, "CONNECTED"}, | 36 {kConnected, "CONNECTED"}, |
36 {kDisconnecting, "DISCONNECTING"}, | 37 {kDisconnecting, "DISCONNECTING"}, |
37 {kError, "ERROR"}, | 38 {kError, "ERROR"}, |
38 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; | 39 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; |
39 | 40 |
40 } // namespace | 41 } // namespace |
41 | 42 |
42 It2MeNativeMessagingHost::It2MeNativeMessagingHost( | 43 It2MeNativeMessagingHost::It2MeNativeMessagingHost( |
43 scoped_refptr<AutoThreadTaskRunner> task_runner, | 44 scoped_refptr<AutoThreadTaskRunner> task_runner, |
| 45 scoped_ptr<extensions::NativeMessagingChannel> channel, |
44 scoped_ptr<It2MeHostFactory> factory) | 46 scoped_ptr<It2MeHostFactory> factory) |
45 : client_(NULL), | 47 : channel_(channel.Pass()), |
46 factory_(factory.Pass()), | 48 factory_(factory.Pass()), |
47 weak_factory_(this) { | 49 weak_factory_(this) { |
48 weak_ptr_ = weak_factory_.GetWeakPtr(); | 50 weak_ptr_ = weak_factory_.GetWeakPtr(); |
49 | 51 |
50 // 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. |
51 // The native messaging host, rather than the It2MeHost object, owns and | 53 // The native messaging host, rather than the It2MeHost object, owns and |
52 // maintains the lifetime of the host context. | 54 // maintains the lifetime of the host context. |
53 | 55 |
54 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); | 56 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); |
55 | 57 |
(...skipping 10 matching lines...) Expand all Loading... |
66 | 68 |
67 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { | 69 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
68 DCHECK(task_runner()->BelongsToCurrentThread()); | 70 DCHECK(task_runner()->BelongsToCurrentThread()); |
69 | 71 |
70 if (it2me_host_.get()) { | 72 if (it2me_host_.get()) { |
71 it2me_host_->Disconnect(); | 73 it2me_host_->Disconnect(); |
72 it2me_host_ = NULL; | 74 it2me_host_ = NULL; |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 void It2MeNativeMessagingHost::OnMessage(const std::string& message) { | 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) { |
77 DCHECK(task_runner()->BelongsToCurrentThread()); | 88 DCHECK(task_runner()->BelongsToCurrentThread()); |
78 | 89 |
79 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 90 if (!message->IsType(base::Value::TYPE_DICTIONARY)) { |
80 scoped_ptr<base::Value> message_value(base::JSONReader::Read(message)); | |
81 if (!message_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
82 LOG(ERROR) << "Received a message that's not a dictionary."; | 91 LOG(ERROR) << "Received a message that's not a dictionary."; |
83 client_->CloseChannel(std::string()); | 92 channel_->SendMessage(nullptr); |
84 return; | 93 return; |
85 } | 94 } |
86 | 95 |
87 scoped_ptr<base::DictionaryValue> message_dict( | 96 scoped_ptr<base::DictionaryValue> message_dict( |
88 static_cast<base::DictionaryValue*>(message_value.release())); | 97 static_cast<base::DictionaryValue*>(message.release())); |
| 98 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
89 | 99 |
90 // If the client supplies an ID, it will expect it in the response. This | 100 // If the client supplies an ID, it will expect it in the response. This |
91 // might be a string or a number, so cope with both. | 101 // might be a string or a number, so cope with both. |
92 const base::Value* id; | 102 const base::Value* id; |
93 if (message_dict->Get("id", &id)) | 103 if (message_dict->Get("id", &id)) |
94 response->Set("id", id->DeepCopy()); | 104 response->Set("id", id->DeepCopy()); |
95 | 105 |
96 std::string type; | 106 std::string type; |
97 if (!message_dict->GetString("type", &type)) { | 107 if (!message_dict->GetString("type", &type)) { |
98 SendErrorAndExit(response.Pass(), "'type' not found in request."); | 108 SendErrorAndExit(response.Pass(), "'type' not found in request."); |
99 return; | 109 return; |
100 } | 110 } |
101 | 111 |
102 response->SetString("type", type + "Response"); | 112 response->SetString("type", type + "Response"); |
103 | 113 |
104 if (type == "hello") { | 114 if (type == "hello") { |
105 ProcessHello(*message_dict, response.Pass()); | 115 ProcessHello(*message_dict, response.Pass()); |
106 } else if (type == "connect") { | 116 } else if (type == "connect") { |
107 ProcessConnect(*message_dict, response.Pass()); | 117 ProcessConnect(*message_dict, response.Pass()); |
108 } else if (type == "disconnect") { | 118 } else if (type == "disconnect") { |
109 ProcessDisconnect(*message_dict, response.Pass()); | 119 ProcessDisconnect(*message_dict, response.Pass()); |
110 } else { | 120 } else { |
111 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); | 121 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); |
112 } | 122 } |
113 } | 123 } |
114 | 124 |
115 void It2MeNativeMessagingHost::Start(Client* client) { | 125 void It2MeNativeMessagingHost::OnDisconnect() { |
116 DCHECK(task_runner()->BelongsToCurrentThread()); | 126 if (!quit_closure_.is_null()) |
117 client_ = client; | 127 base::ResetAndReturn(&quit_closure_).Run(); |
118 } | |
119 | |
120 void It2MeNativeMessagingHost::SendMessageToClient( | |
121 scoped_ptr<base::DictionaryValue> message) const { | |
122 DCHECK(task_runner()->BelongsToCurrentThread()); | |
123 std::string message_json; | |
124 base::JSONWriter::Write(message.get(), &message_json); | |
125 client_->PostMessageFromNativeHost(message_json); | |
126 } | 128 } |
127 | 129 |
128 void It2MeNativeMessagingHost::ProcessHello( | 130 void It2MeNativeMessagingHost::ProcessHello( |
129 const base::DictionaryValue& message, | 131 const base::DictionaryValue& message, |
130 scoped_ptr<base::DictionaryValue> response) const { | 132 scoped_ptr<base::DictionaryValue> response) const { |
131 DCHECK(task_runner()->BelongsToCurrentThread()); | 133 DCHECK(task_runner()->BelongsToCurrentThread()); |
132 | 134 |
133 response->SetString("version", STRINGIZE(VERSION)); | 135 response->SetString("version", STRINGIZE(VERSION)); |
134 | 136 |
135 // This list will be populated when new features are added. | 137 // This list will be populated when new features are added. |
136 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 138 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
137 response->Set("supportedFeatures", supported_features_list.release()); | 139 response->Set("supportedFeatures", supported_features_list.release()); |
138 | 140 |
139 SendMessageToClient(response.Pass()); | 141 channel_->SendMessage(response.Pass()); |
140 } | 142 } |
141 | 143 |
142 void It2MeNativeMessagingHost::ProcessConnect( | 144 void It2MeNativeMessagingHost::ProcessConnect( |
143 const base::DictionaryValue& message, | 145 const base::DictionaryValue& message, |
144 scoped_ptr<base::DictionaryValue> response) { | 146 scoped_ptr<base::DictionaryValue> response) { |
145 DCHECK(task_runner()->BelongsToCurrentThread()); | 147 DCHECK(task_runner()->BelongsToCurrentThread()); |
146 | 148 |
147 if (it2me_host_.get()) { | 149 if (it2me_host_.get()) { |
148 SendErrorAndExit(response.Pass(), | 150 SendErrorAndExit(response.Pass(), |
149 "Connect can be called only when disconnected."); | 151 "Connect can be called only when disconnected."); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 #endif // !defined(NDEBUG) | 205 #endif // !defined(NDEBUG) |
204 | 206 |
205 // Create the It2Me host and start connecting. | 207 // Create the It2Me host and start connecting. |
206 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 208 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
207 host_context_->ui_task_runner(), | 209 host_context_->ui_task_runner(), |
208 weak_ptr_, | 210 weak_ptr_, |
209 xmpp_config, | 211 xmpp_config, |
210 directory_bot_jid_); | 212 directory_bot_jid_); |
211 it2me_host_->Connect(); | 213 it2me_host_->Connect(); |
212 | 214 |
213 SendMessageToClient(response.Pass()); | 215 channel_->SendMessage(response.Pass()); |
214 } | 216 } |
215 | 217 |
216 void It2MeNativeMessagingHost::ProcessDisconnect( | 218 void It2MeNativeMessagingHost::ProcessDisconnect( |
217 const base::DictionaryValue& message, | 219 const base::DictionaryValue& message, |
218 scoped_ptr<base::DictionaryValue> response) { | 220 scoped_ptr<base::DictionaryValue> response) { |
219 DCHECK(task_runner()->BelongsToCurrentThread()); | 221 DCHECK(task_runner()->BelongsToCurrentThread()); |
220 | 222 |
221 if (it2me_host_.get()) { | 223 if (it2me_host_.get()) { |
222 it2me_host_->Disconnect(); | 224 it2me_host_->Disconnect(); |
223 it2me_host_ = NULL; | 225 it2me_host_ = NULL; |
224 } | 226 } |
225 SendMessageToClient(response.Pass()); | 227 channel_->SendMessage(response.Pass()); |
226 } | 228 } |
227 | 229 |
228 void It2MeNativeMessagingHost::SendErrorAndExit( | 230 void It2MeNativeMessagingHost::SendErrorAndExit( |
229 scoped_ptr<base::DictionaryValue> response, | 231 scoped_ptr<base::DictionaryValue> response, |
230 const std::string& description) const { | 232 const std::string& description) const { |
231 DCHECK(task_runner()->BelongsToCurrentThread()); | 233 DCHECK(task_runner()->BelongsToCurrentThread()); |
232 | 234 |
233 LOG(ERROR) << description; | 235 LOG(ERROR) << description; |
234 | 236 |
235 response->SetString("type", "error"); | 237 response->SetString("type", "error"); |
236 response->SetString("description", description); | 238 response->SetString("description", description); |
237 SendMessageToClient(response.Pass()); | 239 channel_->SendMessage(response.Pass()); |
238 | 240 |
239 // Trigger a host shutdown by sending an empty message. | 241 // Trigger a host shutdown by sending a NULL message. |
240 client_->CloseChannel(std::string()); | 242 channel_->SendMessage(nullptr); |
241 } | 243 } |
242 | 244 |
243 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 245 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
244 DCHECK(task_runner()->BelongsToCurrentThread()); | 246 DCHECK(task_runner()->BelongsToCurrentThread()); |
245 | 247 |
246 state_ = state; | 248 state_ = state; |
247 | 249 |
248 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 250 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
249 | 251 |
250 message->SetString("type", "hostStateChanged"); | 252 message->SetString("type", "hostStateChanged"); |
(...skipping 12 matching lines...) Expand all Loading... |
263 break; | 265 break; |
264 | 266 |
265 case kDisconnected: | 267 case kDisconnected: |
266 client_username_.clear(); | 268 client_username_.clear(); |
267 break; | 269 break; |
268 | 270 |
269 default: | 271 default: |
270 ; | 272 ; |
271 } | 273 } |
272 | 274 |
273 SendMessageToClient(message.Pass()); | 275 channel_->SendMessage(message.Pass()); |
274 } | 276 } |
275 | 277 |
276 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 278 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
277 DCHECK(task_runner()->BelongsToCurrentThread()); | 279 DCHECK(task_runner()->BelongsToCurrentThread()); |
278 | 280 |
279 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 281 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
280 | 282 |
281 message->SetString("type", "natPolicyChanged"); | 283 message->SetString("type", "natPolicyChanged"); |
282 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 284 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
283 SendMessageToClient(message.Pass()); | 285 channel_->SendMessage(message.Pass()); |
284 } | 286 } |
285 | 287 |
286 // Stores the Access Code for the web-app to query. | 288 // Stores the Access Code for the web-app to query. |
287 void It2MeNativeMessagingHost::OnStoreAccessCode( | 289 void It2MeNativeMessagingHost::OnStoreAccessCode( |
288 const std::string& access_code, | 290 const std::string& access_code, |
289 base::TimeDelta access_code_lifetime) { | 291 base::TimeDelta access_code_lifetime) { |
290 DCHECK(task_runner()->BelongsToCurrentThread()); | 292 DCHECK(task_runner()->BelongsToCurrentThread()); |
291 | 293 |
292 access_code_ = access_code; | 294 access_code_ = access_code; |
293 access_code_lifetime_ = access_code_lifetime; | 295 access_code_lifetime_ = access_code_lifetime; |
294 } | 296 } |
295 | 297 |
296 // Stores the client user's name for the web-app to query. | 298 // Stores the client user's name for the web-app to query. |
297 void It2MeNativeMessagingHost::OnClientAuthenticated( | 299 void It2MeNativeMessagingHost::OnClientAuthenticated( |
298 const std::string& client_username) { | 300 const std::string& client_username) { |
299 DCHECK(task_runner()->BelongsToCurrentThread()); | 301 DCHECK(task_runner()->BelongsToCurrentThread()); |
300 | 302 |
301 client_username_ = client_username; | 303 client_username_ = client_username; |
302 } | 304 } |
303 | 305 |
304 scoped_refptr<base::SingleThreadTaskRunner> | 306 scoped_refptr<AutoThreadTaskRunner> |
305 It2MeNativeMessagingHost::task_runner() const { | 307 It2MeNativeMessagingHost::task_runner() const { |
306 return host_context_->ui_task_runner(); | 308 return host_context_->ui_task_runner(); |
307 } | 309 } |
308 | 310 |
309 /* static */ | 311 /* static */ |
310 std::string It2MeNativeMessagingHost::HostStateToString( | 312 std::string It2MeNativeMessagingHost::HostStateToString( |
311 It2MeHostState host_state) { | 313 It2MeHostState host_state) { |
312 return ValueToName(kIt2MeHostStates, host_state); | 314 return ValueToName(kIt2MeHostStates, host_state); |
313 } | 315 } |
314 | 316 |
315 } // namespace remoting | 317 } // namespace remoting |
316 | 318 |
OLD | NEW |