| 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 11 matching lines...) Expand all Loading... |
| 33 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, | 34 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, |
| 34 {kConnected, "CONNECTED"}, | 35 {kConnected, "CONNECTED"}, |
| 35 {kDisconnecting, "DISCONNECTING"}, | 36 {kDisconnecting, "DISCONNECTING"}, |
| 36 {kError, "ERROR"}, | 37 {kError, "ERROR"}, |
| 37 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; | 38 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, }; |
| 38 | 39 |
| 39 } // namespace | 40 } // namespace |
| 40 | 41 |
| 41 It2MeNativeMessagingHost::It2MeNativeMessagingHost( | 42 It2MeNativeMessagingHost::It2MeNativeMessagingHost( |
| 42 scoped_refptr<AutoThreadTaskRunner> task_runner, | 43 scoped_refptr<AutoThreadTaskRunner> task_runner, |
| 44 scoped_ptr<NativeMessagingChannel> channel, |
| 43 scoped_ptr<It2MeHostFactory> factory) | 45 scoped_ptr<It2MeHostFactory> factory) |
| 44 : factory_(factory.Pass()), weak_factory_(this) { | 46 : channel_(channel.Pass()), |
| 47 factory_(factory.Pass()), |
| 48 weak_factory_(this) { |
| 45 weak_ptr_ = weak_factory_.GetWeakPtr(); | 49 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 46 | 50 |
| 47 // Initialize the host context to manage the threads for the it2me host. | 51 // Initialize the host context to manage the threads for the it2me host. |
| 48 // The native messaging host, rather than the It2MeHost object, owns and | 52 // The native messaging host, rather than the It2MeHost object, owns and |
| 49 // maintains the lifetime of the host context. | 53 // maintains the lifetime of the host context. |
| 50 | 54 |
| 51 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); | 55 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); |
| 52 | 56 |
| 53 ServiceUrls* service_urls = ServiceUrls::GetInstance(); | 57 ServiceUrls* service_urls = ServiceUrls::GetInstance(); |
| 54 bool xmpp_server_valid = | 58 bool xmpp_server_valid = |
| 55 net::ParseHostAndPort(service_urls->xmpp_server_address(), | 59 net::ParseHostAndPort(service_urls->xmpp_server_address(), |
| 56 &xmpp_server_config_.host, | 60 &xmpp_server_config_.host, |
| 57 &xmpp_server_config_.port); | 61 &xmpp_server_config_.port); |
| 58 DCHECK(xmpp_server_valid); | 62 DCHECK(xmpp_server_valid); |
| 59 | 63 |
| 60 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); | 64 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); |
| 61 directory_bot_jid_ = service_urls->directory_bot_jid(); | 65 directory_bot_jid_ = service_urls->directory_bot_jid(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {} | 68 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
| 69 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 65 | 70 |
| 66 void It2MeNativeMessagingHost::SetSendMessageCallback( | 71 if (it2me_host_.get()) { |
| 67 const SendMessageCallback& send_message) { | 72 it2me_host_->Disconnect(); |
| 68 send_message_ = send_message; | 73 it2me_host_ = NULL; |
| 74 } |
| 75 } |
| 76 |
| 77 void It2MeNativeMessagingHost::Start(const base::Closure& quit_closure) { |
| 78 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 79 |
| 80 channel_->Start( |
| 81 base::Bind(&It2MeNativeMessagingHost::ProcessMessage, weak_ptr_), |
| 82 quit_closure); |
| 69 } | 83 } |
| 70 | 84 |
| 71 void It2MeNativeMessagingHost::ProcessMessage( | 85 void It2MeNativeMessagingHost::ProcessMessage( |
| 72 scoped_ptr<base::DictionaryValue> message) { | 86 scoped_ptr<base::DictionaryValue> message) { |
| 87 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 88 |
| 73 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 89 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 74 | 90 |
| 75 // If the client supplies an ID, it will expect it in the response. This | 91 // 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. | 92 // might be a string or a number, so cope with both. |
| 77 const base::Value* id; | 93 const base::Value* id; |
| 78 if (message->Get("id", &id)) | 94 if (message->Get("id", &id)) |
| 79 response->Set("id", id->DeepCopy()); | 95 response->Set("id", id->DeepCopy()); |
| 80 | 96 |
| 81 std::string type; | 97 std::string type; |
| 82 if (!message->GetString("type", &type)) { | 98 if (!message->GetString("type", &type)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 93 } else if (type == "disconnect") { | 109 } else if (type == "disconnect") { |
| 94 ProcessDisconnect(*message, response.Pass()); | 110 ProcessDisconnect(*message, response.Pass()); |
| 95 } else { | 111 } else { |
| 96 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); | 112 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); |
| 97 } | 113 } |
| 98 } | 114 } |
| 99 | 115 |
| 100 void It2MeNativeMessagingHost::ProcessHello( | 116 void It2MeNativeMessagingHost::ProcessHello( |
| 101 const base::DictionaryValue& message, | 117 const base::DictionaryValue& message, |
| 102 scoped_ptr<base::DictionaryValue> response) { | 118 scoped_ptr<base::DictionaryValue> response) { |
| 119 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 120 |
| 103 response->SetString("version", STRINGIZE(VERSION)); | 121 response->SetString("version", STRINGIZE(VERSION)); |
| 104 | 122 |
| 105 // This list will be populated when new features are added. | 123 // This list will be populated when new features are added. |
| 106 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 124 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
| 107 response->Set("supportedFeatures", supported_features_list.release()); | 125 response->Set("supportedFeatures", supported_features_list.release()); |
| 108 | 126 |
| 109 send_message_.Run(response.Pass()); | 127 channel_->SendMessage(response.Pass()); |
| 110 } | 128 } |
| 111 | 129 |
| 112 void It2MeNativeMessagingHost::ProcessConnect( | 130 void It2MeNativeMessagingHost::ProcessConnect( |
| 113 const base::DictionaryValue& message, | 131 const base::DictionaryValue& message, |
| 114 scoped_ptr<base::DictionaryValue> response) { | 132 scoped_ptr<base::DictionaryValue> response) { |
| 133 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 134 |
| 115 if (it2me_host_.get()) { | 135 if (it2me_host_.get()) { |
| 116 SendErrorAndExit(response.Pass(), | 136 SendErrorAndExit(response.Pass(), |
| 117 "Connect can be called only when disconnected."); | 137 "Connect can be called only when disconnected."); |
| 118 return; | 138 return; |
| 119 } | 139 } |
| 120 | 140 |
| 121 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; | 141 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; |
| 122 | 142 |
| 123 if (!message.GetString("userName", &xmpp_config.username)) { | 143 if (!message.GetString("userName", &xmpp_config.username)) { |
| 124 SendErrorAndExit(response.Pass(), "'userName' not found in request."); | 144 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) | 191 #endif // !defined(NDEBUG) |
| 172 | 192 |
| 173 // Create the It2Me host and start connecting. | 193 // Create the It2Me host and start connecting. |
| 174 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 194 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
| 175 host_context_->ui_task_runner(), | 195 host_context_->ui_task_runner(), |
| 176 weak_ptr_, | 196 weak_ptr_, |
| 177 xmpp_config, | 197 xmpp_config, |
| 178 directory_bot_jid_); | 198 directory_bot_jid_); |
| 179 it2me_host_->Connect(); | 199 it2me_host_->Connect(); |
| 180 | 200 |
| 181 send_message_.Run(response.Pass()); | 201 channel_->SendMessage(response.Pass()); |
| 182 } | 202 } |
| 183 | 203 |
| 184 void It2MeNativeMessagingHost::ProcessDisconnect( | 204 void It2MeNativeMessagingHost::ProcessDisconnect( |
| 185 const base::DictionaryValue& message, | 205 const base::DictionaryValue& message, |
| 186 scoped_ptr<base::DictionaryValue> response) { | 206 scoped_ptr<base::DictionaryValue> response) { |
| 207 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 208 |
| 187 if (it2me_host_.get()) { | 209 if (it2me_host_.get()) { |
| 188 it2me_host_->Disconnect(); | 210 it2me_host_->Disconnect(); |
| 189 it2me_host_ = NULL; | 211 it2me_host_ = NULL; |
| 190 } | 212 } |
| 191 send_message_.Run(response.Pass()); | 213 channel_->SendMessage(response.Pass()); |
| 192 } | 214 } |
| 193 | 215 |
| 194 void It2MeNativeMessagingHost::SendErrorAndExit( | 216 void It2MeNativeMessagingHost::SendErrorAndExit( |
| 195 scoped_ptr<base::DictionaryValue> response, | 217 scoped_ptr<base::DictionaryValue> response, |
| 196 const std::string& description) { | 218 const std::string& description) { |
| 219 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 220 |
| 197 LOG(ERROR) << description; | 221 LOG(ERROR) << description; |
| 198 | 222 |
| 199 response->SetString("type", "error"); | 223 response->SetString("type", "error"); |
| 200 response->SetString("description", description); | 224 response->SetString("description", description); |
| 201 send_message_.Run(response.Pass()); | 225 channel_->SendMessage(response.Pass()); |
| 202 | 226 |
| 203 // Trigger a host shutdown by sending a NULL message. | 227 // Trigger a host shutdown by sending a NULL message. |
| 204 send_message_.Run(scoped_ptr<base::DictionaryValue>()); | 228 channel_->SendMessage(scoped_ptr<base::DictionaryValue>()); |
| 205 } | 229 } |
| 206 | 230 |
| 207 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 231 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
| 208 DCHECK(task_runner()->BelongsToCurrentThread()); | 232 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 209 | 233 |
| 210 state_ = state; | 234 state_ = state; |
| 211 | 235 |
| 212 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 236 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 213 | 237 |
| 214 message->SetString("type", "hostStateChanged"); | 238 message->SetString("type", "hostStateChanged"); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 227 break; | 251 break; |
| 228 | 252 |
| 229 case kDisconnected: | 253 case kDisconnected: |
| 230 client_username_.clear(); | 254 client_username_.clear(); |
| 231 break; | 255 break; |
| 232 | 256 |
| 233 default: | 257 default: |
| 234 ; | 258 ; |
| 235 } | 259 } |
| 236 | 260 |
| 237 send_message_.Run(message.Pass()); | 261 channel_->SendMessage(message.Pass()); |
| 238 } | 262 } |
| 239 | 263 |
| 240 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 264 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
| 241 DCHECK(task_runner()->BelongsToCurrentThread()); | 265 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 242 | 266 |
| 243 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 267 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 244 | 268 |
| 245 message->SetString("type", "natPolicyChanged"); | 269 message->SetString("type", "natPolicyChanged"); |
| 246 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 270 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
| 247 send_message_.Run(message.Pass()); | 271 channel_->SendMessage(message.Pass()); |
| 248 } | 272 } |
| 249 | 273 |
| 250 // Stores the Access Code for the web-app to query. | 274 // Stores the Access Code for the web-app to query. |
| 251 void It2MeNativeMessagingHost::OnStoreAccessCode( | 275 void It2MeNativeMessagingHost::OnStoreAccessCode( |
| 252 const std::string& access_code, | 276 const std::string& access_code, |
| 253 base::TimeDelta access_code_lifetime) { | 277 base::TimeDelta access_code_lifetime) { |
| 254 DCHECK(task_runner()->BelongsToCurrentThread()); | 278 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 255 | 279 |
| 256 access_code_ = access_code; | 280 access_code_ = access_code; |
| 257 access_code_lifetime_ = access_code_lifetime; | 281 access_code_lifetime_ = access_code_lifetime; |
| 258 } | 282 } |
| 259 | 283 |
| 260 // Stores the client user's name for the web-app to query. | 284 // Stores the client user's name for the web-app to query. |
| 261 void It2MeNativeMessagingHost::OnClientAuthenticated( | 285 void It2MeNativeMessagingHost::OnClientAuthenticated( |
| 262 const std::string& client_username) { | 286 const std::string& client_username) { |
| 263 DCHECK(task_runner()->BelongsToCurrentThread()); | 287 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 264 | 288 |
| 265 client_username_ = client_username; | 289 client_username_ = client_username; |
| 266 } | 290 } |
| 267 | 291 |
| 268 void It2MeNativeMessagingHost::ShutDown() { | |
| 269 if (it2me_host_.get()) { | |
| 270 it2me_host_->Disconnect(); | |
| 271 it2me_host_ = NULL; | |
| 272 } | |
| 273 host_context_.reset(); | |
| 274 } | |
| 275 | |
| 276 scoped_refptr<AutoThreadTaskRunner> It2MeNativeMessagingHost::task_runner() { | 292 scoped_refptr<AutoThreadTaskRunner> It2MeNativeMessagingHost::task_runner() { |
| 277 return host_context_->ui_task_runner(); | 293 return host_context_->ui_task_runner(); |
| 278 } | 294 } |
| 279 | 295 |
| 280 /* static */ | 296 /* static */ |
| 281 std::string It2MeNativeMessagingHost::HostStateToString( | 297 std::string It2MeNativeMessagingHost::HostStateToString( |
| 282 It2MeHostState host_state) { | 298 It2MeHostState host_state) { |
| 283 return ValueToName(kIt2MeHostStates, host_state); | 299 return ValueToName(kIt2MeHostStates, host_state); |
| 284 } | 300 } |
| 285 | 301 |
| 286 } // namespace remoting | 302 } // namespace remoting |
| OLD | NEW |