| 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 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 36 {kError, "ERROR"}, | 37 {kError, "ERROR"}, |
| 37 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, | 38 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 } // namespace | 41 } // namespace |
| 41 | 42 |
| 42 It2MeNativeMessagingHost::It2MeNativeMessagingHost( | 43 It2MeNativeMessagingHost::It2MeNativeMessagingHost( |
| 43 scoped_ptr<ChromotingHostContext> context, | 44 scoped_ptr<ChromotingHostContext> context, |
| 44 scoped_ptr<It2MeHostFactory> factory) | 45 scoped_ptr<It2MeHostFactory> factory) |
| 45 : client_(nullptr), | 46 : client_(nullptr), |
| 46 host_context_(context.Pass()), | 47 host_context_(std::move(context)), |
| 47 factory_(factory.Pass()), | 48 factory_(std::move(factory)), |
| 48 weak_factory_(this) { | 49 weak_factory_(this) { |
| 49 weak_ptr_ = weak_factory_.GetWeakPtr(); | 50 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 50 | 51 |
| 51 const ServiceUrls* service_urls = ServiceUrls::GetInstance(); | 52 const ServiceUrls* service_urls = ServiceUrls::GetInstance(); |
| 52 const bool xmpp_server_valid = | 53 const bool xmpp_server_valid = |
| 53 net::ParseHostAndPort(service_urls->xmpp_server_address(), | 54 net::ParseHostAndPort(service_urls->xmpp_server_address(), |
| 54 &xmpp_server_config_.host, | 55 &xmpp_server_config_.host, |
| 55 &xmpp_server_config_.port); | 56 &xmpp_server_config_.port); |
| 56 DCHECK(xmpp_server_valid); | 57 DCHECK(xmpp_server_valid); |
| 57 | 58 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 83 static_cast<base::DictionaryValue*>(message_value.release())); | 84 static_cast<base::DictionaryValue*>(message_value.release())); |
| 84 | 85 |
| 85 // If the client supplies an ID, it will expect it in the response. This | 86 // If the client supplies an ID, it will expect it in the response. This |
| 86 // might be a string or a number, so cope with both. | 87 // might be a string or a number, so cope with both. |
| 87 const base::Value* id; | 88 const base::Value* id; |
| 88 if (message_dict->Get("id", &id)) | 89 if (message_dict->Get("id", &id)) |
| 89 response->Set("id", id->DeepCopy()); | 90 response->Set("id", id->DeepCopy()); |
| 90 | 91 |
| 91 std::string type; | 92 std::string type; |
| 92 if (!message_dict->GetString("type", &type)) { | 93 if (!message_dict->GetString("type", &type)) { |
| 93 SendErrorAndExit(response.Pass(), "'type' not found in request."); | 94 SendErrorAndExit(std::move(response), "'type' not found in request."); |
| 94 return; | 95 return; |
| 95 } | 96 } |
| 96 | 97 |
| 97 response->SetString("type", type + "Response"); | 98 response->SetString("type", type + "Response"); |
| 98 | 99 |
| 99 if (type == "hello") { | 100 if (type == "hello") { |
| 100 ProcessHello(*message_dict, response.Pass()); | 101 ProcessHello(*message_dict, std::move(response)); |
| 101 } else if (type == "connect") { | 102 } else if (type == "connect") { |
| 102 ProcessConnect(*message_dict, response.Pass()); | 103 ProcessConnect(*message_dict, std::move(response)); |
| 103 } else if (type == "disconnect") { | 104 } else if (type == "disconnect") { |
| 104 ProcessDisconnect(*message_dict, response.Pass()); | 105 ProcessDisconnect(*message_dict, std::move(response)); |
| 105 } else { | 106 } else { |
| 106 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); | 107 SendErrorAndExit(std::move(response), "Unsupported request type: " + type); |
| 107 } | 108 } |
| 108 } | 109 } |
| 109 | 110 |
| 110 void It2MeNativeMessagingHost::Start(Client* client) { | 111 void It2MeNativeMessagingHost::Start(Client* client) { |
| 111 DCHECK(task_runner()->BelongsToCurrentThread()); | 112 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 112 client_ = client; | 113 client_ = client; |
| 113 #if !defined(OS_CHROMEOS) | 114 #if !defined(OS_CHROMEOS) |
| 114 log_message_handler_.reset( | 115 log_message_handler_.reset( |
| 115 new LogMessageHandler( | 116 new LogMessageHandler( |
| 116 base::Bind(&It2MeNativeMessagingHost::SendMessageToClient, | 117 base::Bind(&It2MeNativeMessagingHost::SendMessageToClient, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 130 const base::DictionaryValue& message, | 131 const base::DictionaryValue& message, |
| 131 scoped_ptr<base::DictionaryValue> response) const { | 132 scoped_ptr<base::DictionaryValue> response) const { |
| 132 DCHECK(task_runner()->BelongsToCurrentThread()); | 133 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 133 | 134 |
| 134 response->SetString("version", STRINGIZE(VERSION)); | 135 response->SetString("version", STRINGIZE(VERSION)); |
| 135 | 136 |
| 136 // This list will be populated when new features are added. | 137 // This list will be populated when new features are added. |
| 137 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 138 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
| 138 response->Set("supportedFeatures", supported_features_list.release()); | 139 response->Set("supportedFeatures", supported_features_list.release()); |
| 139 | 140 |
| 140 SendMessageToClient(response.Pass()); | 141 SendMessageToClient(std::move(response)); |
| 141 } | 142 } |
| 142 | 143 |
| 143 void It2MeNativeMessagingHost::ProcessConnect( | 144 void It2MeNativeMessagingHost::ProcessConnect( |
| 144 const base::DictionaryValue& message, | 145 const base::DictionaryValue& message, |
| 145 scoped_ptr<base::DictionaryValue> response) { | 146 scoped_ptr<base::DictionaryValue> response) { |
| 146 DCHECK(task_runner()->BelongsToCurrentThread()); | 147 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 147 | 148 |
| 148 if (it2me_host_.get()) { | 149 if (it2me_host_.get()) { |
| 149 SendErrorAndExit(response.Pass(), | 150 SendErrorAndExit(std::move(response), |
| 150 "Connect can be called only when disconnected."); | 151 "Connect can be called only when disconnected."); |
| 151 return; | 152 return; |
| 152 } | 153 } |
| 153 | 154 |
| 154 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; | 155 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; |
| 155 | 156 |
| 156 if (!message.GetString("userName", &xmpp_config.username)) { | 157 if (!message.GetString("userName", &xmpp_config.username)) { |
| 157 SendErrorAndExit(response.Pass(), "'userName' not found in request."); | 158 SendErrorAndExit(std::move(response), "'userName' not found in request."); |
| 158 return; | 159 return; |
| 159 } | 160 } |
| 160 | 161 |
| 161 std::string auth_service_with_token; | 162 std::string auth_service_with_token; |
| 162 if (!message.GetString("authServiceWithToken", &auth_service_with_token)) { | 163 if (!message.GetString("authServiceWithToken", &auth_service_with_token)) { |
| 163 SendErrorAndExit(response.Pass(), | 164 SendErrorAndExit(std::move(response), |
| 164 "'authServiceWithToken' not found in request."); | 165 "'authServiceWithToken' not found in request."); |
| 165 return; | 166 return; |
| 166 } | 167 } |
| 167 | 168 |
| 168 // For backward compatibility the webapp still passes OAuth service as part of | 169 // For backward compatibility the webapp still passes OAuth service as part of |
| 169 // the authServiceWithToken field. But auth service part is always expected to | 170 // the authServiceWithToken field. But auth service part is always expected to |
| 170 // be set to oauth2. | 171 // be set to oauth2. |
| 171 const char kOAuth2ServicePrefix[] = "oauth2:"; | 172 const char kOAuth2ServicePrefix[] = "oauth2:"; |
| 172 if (!base::StartsWith(auth_service_with_token, kOAuth2ServicePrefix, | 173 if (!base::StartsWith(auth_service_with_token, kOAuth2ServicePrefix, |
| 173 base::CompareCase::SENSITIVE)) { | 174 base::CompareCase::SENSITIVE)) { |
| 174 SendErrorAndExit(response.Pass(), "Invalid 'authServiceWithToken': " + | 175 SendErrorAndExit(std::move(response), "Invalid 'authServiceWithToken': " + |
| 175 auth_service_with_token); | 176 auth_service_with_token); |
| 176 return; | 177 return; |
| 177 } | 178 } |
| 178 | 179 |
| 179 xmpp_config.auth_token = | 180 xmpp_config.auth_token = |
| 180 auth_service_with_token.substr(strlen(kOAuth2ServicePrefix)); | 181 auth_service_with_token.substr(strlen(kOAuth2ServicePrefix)); |
| 181 | 182 |
| 182 #if !defined(NDEBUG) | 183 #if !defined(NDEBUG) |
| 183 std::string address; | 184 std::string address; |
| 184 if (!message.GetString("xmppServerAddress", &address)) { | 185 if (!message.GetString("xmppServerAddress", &address)) { |
| 185 SendErrorAndExit(response.Pass(), | 186 SendErrorAndExit(std::move(response), |
| 186 "'xmppServerAddress' not found in request."); | 187 "'xmppServerAddress' not found in request."); |
| 187 return; | 188 return; |
| 188 } | 189 } |
| 189 | 190 |
| 190 if (!net::ParseHostAndPort( | 191 if (!net::ParseHostAndPort(address, &xmpp_server_config_.host, |
| 191 address, &xmpp_server_config_.host, &xmpp_server_config_.port)) { | 192 &xmpp_server_config_.port)) { |
| 192 SendErrorAndExit(response.Pass(), | 193 SendErrorAndExit(std::move(response), |
| 193 "Invalid 'xmppServerAddress': " + address); | 194 "Invalid 'xmppServerAddress': " + address); |
| 194 return; | 195 return; |
| 195 } | 196 } |
| 196 | 197 |
| 197 if (!message.GetBoolean("xmppServerUseTls", &xmpp_server_config_.use_tls)) { | 198 if (!message.GetBoolean("xmppServerUseTls", &xmpp_server_config_.use_tls)) { |
| 198 SendErrorAndExit(response.Pass(), | 199 SendErrorAndExit(std::move(response), |
| 199 "'xmppServerUseTls' not found in request."); | 200 "'xmppServerUseTls' not found in request."); |
| 200 return; | 201 return; |
| 201 } | 202 } |
| 202 | 203 |
| 203 if (!message.GetString("directoryBotJid", &directory_bot_jid_)) { | 204 if (!message.GetString("directoryBotJid", &directory_bot_jid_)) { |
| 204 SendErrorAndExit(response.Pass(), | 205 SendErrorAndExit(std::move(response), |
| 205 "'directoryBotJid' not found in request."); | 206 "'directoryBotJid' not found in request."); |
| 206 return; | 207 return; |
| 207 } | 208 } |
| 208 #endif // !defined(NDEBUG) | 209 #endif // !defined(NDEBUG) |
| 209 | 210 |
| 210 // Create the It2Me host and start connecting. | 211 // Create the It2Me host and start connecting. |
| 211 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), | 212 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), |
| 212 weak_ptr_, | 213 weak_ptr_, |
| 213 xmpp_config, | 214 xmpp_config, |
| 214 directory_bot_jid_); | 215 directory_bot_jid_); |
| 215 it2me_host_->Connect(); | 216 it2me_host_->Connect(); |
| 216 | 217 |
| 217 SendMessageToClient(response.Pass()); | 218 SendMessageToClient(std::move(response)); |
| 218 } | 219 } |
| 219 | 220 |
| 220 void It2MeNativeMessagingHost::ProcessDisconnect( | 221 void It2MeNativeMessagingHost::ProcessDisconnect( |
| 221 const base::DictionaryValue& message, | 222 const base::DictionaryValue& message, |
| 222 scoped_ptr<base::DictionaryValue> response) { | 223 scoped_ptr<base::DictionaryValue> response) { |
| 223 DCHECK(task_runner()->BelongsToCurrentThread()); | 224 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 224 | 225 |
| 225 if (it2me_host_.get()) { | 226 if (it2me_host_.get()) { |
| 226 it2me_host_->Disconnect(); | 227 it2me_host_->Disconnect(); |
| 227 it2me_host_ = nullptr; | 228 it2me_host_ = nullptr; |
| 228 } | 229 } |
| 229 SendMessageToClient(response.Pass()); | 230 SendMessageToClient(std::move(response)); |
| 230 } | 231 } |
| 231 | 232 |
| 232 void It2MeNativeMessagingHost::SendErrorAndExit( | 233 void It2MeNativeMessagingHost::SendErrorAndExit( |
| 233 scoped_ptr<base::DictionaryValue> response, | 234 scoped_ptr<base::DictionaryValue> response, |
| 234 const std::string& description) const { | 235 const std::string& description) const { |
| 235 DCHECK(task_runner()->BelongsToCurrentThread()); | 236 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 236 | 237 |
| 237 LOG(ERROR) << description; | 238 LOG(ERROR) << description; |
| 238 | 239 |
| 239 response->SetString("type", "error"); | 240 response->SetString("type", "error"); |
| 240 response->SetString("description", description); | 241 response->SetString("description", description); |
| 241 SendMessageToClient(response.Pass()); | 242 SendMessageToClient(std::move(response)); |
| 242 | 243 |
| 243 // Trigger a host shutdown by sending an empty message. | 244 // Trigger a host shutdown by sending an empty message. |
| 244 client_->CloseChannel(std::string()); | 245 client_->CloseChannel(std::string()); |
| 245 } | 246 } |
| 246 | 247 |
| 247 void It2MeNativeMessagingHost::OnStateChanged( | 248 void It2MeNativeMessagingHost::OnStateChanged( |
| 248 It2MeHostState state, | 249 It2MeHostState state, |
| 249 const std::string& error_message) { | 250 const std::string& error_message) { |
| 250 DCHECK(task_runner()->BelongsToCurrentThread()); | 251 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 251 | 252 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 276 // "error" message so that errors that occur before the "connect" message | 277 // "error" message so that errors that occur before the "connect" message |
| 277 // is sent can be communicated. | 278 // is sent can be communicated. |
| 278 message->SetString("type", "error"); | 279 message->SetString("type", "error"); |
| 279 message->SetString("description", error_message); | 280 message->SetString("description", error_message); |
| 280 break; | 281 break; |
| 281 | 282 |
| 282 default: | 283 default: |
| 283 ; | 284 ; |
| 284 } | 285 } |
| 285 | 286 |
| 286 SendMessageToClient(message.Pass()); | 287 SendMessageToClient(std::move(message)); |
| 287 } | 288 } |
| 288 | 289 |
| 289 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 290 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
| 290 DCHECK(task_runner()->BelongsToCurrentThread()); | 291 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 291 | 292 |
| 292 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 293 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 293 | 294 |
| 294 message->SetString("type", "natPolicyChanged"); | 295 message->SetString("type", "natPolicyChanged"); |
| 295 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 296 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
| 296 SendMessageToClient(message.Pass()); | 297 SendMessageToClient(std::move(message)); |
| 297 } | 298 } |
| 298 | 299 |
| 299 // Stores the Access Code for the web-app to query. | 300 // Stores the Access Code for the web-app to query. |
| 300 void It2MeNativeMessagingHost::OnStoreAccessCode( | 301 void It2MeNativeMessagingHost::OnStoreAccessCode( |
| 301 const std::string& access_code, | 302 const std::string& access_code, |
| 302 base::TimeDelta access_code_lifetime) { | 303 base::TimeDelta access_code_lifetime) { |
| 303 DCHECK(task_runner()->BelongsToCurrentThread()); | 304 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 304 | 305 |
| 305 access_code_ = access_code; | 306 access_code_ = access_code; |
| 306 access_code_lifetime_ = access_code_lifetime; | 307 access_code_lifetime_ = access_code_lifetime; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 319 return host_context_->ui_task_runner(); | 320 return host_context_->ui_task_runner(); |
| 320 } | 321 } |
| 321 | 322 |
| 322 /* static */ | 323 /* static */ |
| 323 std::string It2MeNativeMessagingHost::HostStateToString( | 324 std::string It2MeNativeMessagingHost::HostStateToString( |
| 324 It2MeHostState host_state) { | 325 It2MeHostState host_state) { |
| 325 return ValueToName(kIt2MeHostStates, host_state); | 326 return ValueToName(kIt2MeHostStates, host_state); |
| 326 } | 327 } |
| 327 | 328 |
| 328 } // namespace remoting | 329 } // namespace remoting |
| OLD | NEW |