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