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 : client_(NULL), |
| 48 factory_(factory.Pass()), | 46 factory_(factory.Pass()), |
| 49 weak_factory_(this) { | 47 weak_factory_(this) { |
| 50 weak_ptr_ = weak_factory_.GetWeakPtr(); | 48 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 51 | 49 |
| 52 // Initialize the host context to manage the threads for the it2me host. | 50 // Initialize the host context to manage the threads for the it2me host. |
| 53 // The native messaging host, rather than the It2MeHost object, owns and | 51 // The native messaging host, rather than the It2MeHost object, owns and |
| 54 // maintains the lifetime of the host context. | 52 // maintains the lifetime of the host context. |
| 55 | 53 |
| 56 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); | 54 host_context_.reset(ChromotingHostContext::Create(task_runner).release()); |
| 57 | 55 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 68 | 66 |
| 69 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { | 67 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { |
| 70 DCHECK(task_runner()->BelongsToCurrentThread()); | 68 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 71 | 69 |
| 72 if (it2me_host_.get()) { | 70 if (it2me_host_.get()) { |
| 73 it2me_host_->Disconnect(); | 71 it2me_host_->Disconnect(); |
| 74 it2me_host_ = NULL; | 72 it2me_host_ = NULL; |
| 75 } | 73 } |
| 76 } | 74 } |
| 77 | 75 |
| 78 void It2MeNativeMessagingHost::Start(const base::Closure& quit_closure) { | 76 void It2MeNativeMessagingHost::OnMessage(const std::string& message) { |
| 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) { | |
| 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*>(base::JSONReader::Read(message))); |
|
Sergey Ulanov
2014/10/02 23:09:59
Verify that the message is a dictionary before cas
| |
| 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)) |
| 98 response->Set("id", id->DeepCopy()); | 87 response->Set("id", id->DeepCopy()); |
| 99 | 88 |
| 100 std::string type; | 89 std::string type; |
| 101 if (!message_dict->GetString("type", &type)) { | 90 if (!message_dict->GetString("type", &type)) { |
| 102 SendErrorAndExit(response.Pass(), "'type' not found in request."); | 91 SendErrorAndExit(response.Pass(), "'type' not found in request."); |
| 103 return; | 92 return; |
| 104 } | 93 } |
| 105 | 94 |
| 106 response->SetString("type", type + "Response"); | 95 response->SetString("type", type + "Response"); |
| 107 | 96 |
| 108 if (type == "hello") { | 97 if (type == "hello") { |
| 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::Start(Client* client) { |
| 120 if (!quit_closure_.is_null()) | 109 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 121 base::ResetAndReturn(&quit_closure_).Run(); | 110 client_ = client; |
| 111 } | |
| 112 | |
| 113 void It2MeNativeMessagingHost::SendMessageToClient( | |
| 114 scoped_ptr<base::DictionaryValue> message) const { | |
| 115 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 116 std::string message_json; | |
| 117 base::JSONWriter::Write(message.get(), &message_json); | |
| 118 client_->PostMessageFromNativeHost(message_json); | |
| 122 } | 119 } |
| 123 | 120 |
| 124 void It2MeNativeMessagingHost::ProcessHello( | 121 void It2MeNativeMessagingHost::ProcessHello( |
| 125 const base::DictionaryValue& message, | 122 const base::DictionaryValue& message, |
| 126 scoped_ptr<base::DictionaryValue> response) const { | 123 scoped_ptr<base::DictionaryValue> response) const { |
| 127 DCHECK(task_runner()->BelongsToCurrentThread()); | 124 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 128 | 125 |
| 129 response->SetString("version", STRINGIZE(VERSION)); | 126 response->SetString("version", STRINGIZE(VERSION)); |
| 130 | 127 |
| 131 // This list will be populated when new features are added. | 128 // This list will be populated when new features are added. |
| 132 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); | 129 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); |
| 133 response->Set("supportedFeatures", supported_features_list.release()); | 130 response->Set("supportedFeatures", supported_features_list.release()); |
| 134 | 131 |
| 135 channel_->SendMessage(response.PassAs<base::Value>()); | 132 SendMessageToClient(response.Pass()); |
| 136 } | 133 } |
| 137 | 134 |
| 138 void It2MeNativeMessagingHost::ProcessConnect( | 135 void It2MeNativeMessagingHost::ProcessConnect( |
| 139 const base::DictionaryValue& message, | 136 const base::DictionaryValue& message, |
| 140 scoped_ptr<base::DictionaryValue> response) { | 137 scoped_ptr<base::DictionaryValue> response) { |
| 141 DCHECK(task_runner()->BelongsToCurrentThread()); | 138 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 142 | 139 |
| 143 if (it2me_host_.get()) { | 140 if (it2me_host_.get()) { |
| 144 SendErrorAndExit(response.Pass(), | 141 SendErrorAndExit(response.Pass(), |
| 145 "Connect can be called only when disconnected."); | 142 "Connect can be called only when disconnected."); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 #endif // !defined(NDEBUG) | 196 #endif // !defined(NDEBUG) |
| 200 | 197 |
| 201 // Create the It2Me host and start connecting. | 198 // Create the It2Me host and start connecting. |
| 202 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), | 199 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), |
| 203 host_context_->ui_task_runner(), | 200 host_context_->ui_task_runner(), |
| 204 weak_ptr_, | 201 weak_ptr_, |
| 205 xmpp_config, | 202 xmpp_config, |
| 206 directory_bot_jid_); | 203 directory_bot_jid_); |
| 207 it2me_host_->Connect(); | 204 it2me_host_->Connect(); |
| 208 | 205 |
| 209 channel_->SendMessage(response.PassAs<base::Value>()); | 206 SendMessageToClient(response.Pass()); |
| 210 } | 207 } |
| 211 | 208 |
| 212 void It2MeNativeMessagingHost::ProcessDisconnect( | 209 void It2MeNativeMessagingHost::ProcessDisconnect( |
| 213 const base::DictionaryValue& message, | 210 const base::DictionaryValue& message, |
| 214 scoped_ptr<base::DictionaryValue> response) { | 211 scoped_ptr<base::DictionaryValue> response) { |
| 215 DCHECK(task_runner()->BelongsToCurrentThread()); | 212 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 216 | 213 |
| 217 if (it2me_host_.get()) { | 214 if (it2me_host_.get()) { |
| 218 it2me_host_->Disconnect(); | 215 it2me_host_->Disconnect(); |
| 219 it2me_host_ = NULL; | 216 it2me_host_ = NULL; |
| 220 } | 217 } |
| 221 channel_->SendMessage(response.PassAs<base::Value>()); | 218 SendMessageToClient(response.Pass()); |
| 222 } | 219 } |
| 223 | 220 |
| 224 void It2MeNativeMessagingHost::SendErrorAndExit( | 221 void It2MeNativeMessagingHost::SendErrorAndExit( |
| 225 scoped_ptr<base::DictionaryValue> response, | 222 scoped_ptr<base::DictionaryValue> response, |
| 226 const std::string& description) const { | 223 const std::string& description) const { |
| 227 DCHECK(task_runner()->BelongsToCurrentThread()); | 224 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 228 | 225 |
| 229 LOG(ERROR) << description; | 226 LOG(ERROR) << description; |
| 230 | 227 |
| 231 response->SetString("type", "error"); | 228 response->SetString("type", "error"); |
| 232 response->SetString("description", description); | 229 response->SetString("description", description); |
| 233 channel_->SendMessage(response.PassAs<base::Value>()); | 230 SendMessageToClient(response.Pass()); |
| 234 | 231 |
| 235 // Trigger a host shutdown by sending a NULL message. | 232 // Trigger a host shutdown by sending an empty message. |
| 236 channel_->SendMessage(scoped_ptr<base::Value>()); | 233 client_->CloseChannel(std::string()); |
| 237 } | 234 } |
| 238 | 235 |
| 239 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { | 236 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { |
| 240 DCHECK(task_runner()->BelongsToCurrentThread()); | 237 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 241 | 238 |
| 242 state_ = state; | 239 state_ = state; |
| 243 | 240 |
| 244 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 241 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 245 | 242 |
| 246 message->SetString("type", "hostStateChanged"); | 243 message->SetString("type", "hostStateChanged"); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 259 break; | 256 break; |
| 260 | 257 |
| 261 case kDisconnected: | 258 case kDisconnected: |
| 262 client_username_.clear(); | 259 client_username_.clear(); |
| 263 break; | 260 break; |
| 264 | 261 |
| 265 default: | 262 default: |
| 266 ; | 263 ; |
| 267 } | 264 } |
| 268 | 265 |
| 269 channel_->SendMessage(message.PassAs<base::Value>()); | 266 SendMessageToClient(message.Pass()); |
| 270 } | 267 } |
| 271 | 268 |
| 272 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | 269 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
| 273 DCHECK(task_runner()->BelongsToCurrentThread()); | 270 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 274 | 271 |
| 275 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); | 272 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); |
| 276 | 273 |
| 277 message->SetString("type", "natPolicyChanged"); | 274 message->SetString("type", "natPolicyChanged"); |
| 278 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); | 275 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); |
| 279 channel_->SendMessage(message.PassAs<base::Value>()); | 276 SendMessageToClient(message.Pass()); |
| 280 } | 277 } |
| 281 | 278 |
| 282 // Stores the Access Code for the web-app to query. | 279 // Stores the Access Code for the web-app to query. |
| 283 void It2MeNativeMessagingHost::OnStoreAccessCode( | 280 void It2MeNativeMessagingHost::OnStoreAccessCode( |
| 284 const std::string& access_code, | 281 const std::string& access_code, |
| 285 base::TimeDelta access_code_lifetime) { | 282 base::TimeDelta access_code_lifetime) { |
| 286 DCHECK(task_runner()->BelongsToCurrentThread()); | 283 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 287 | 284 |
| 288 access_code_ = access_code; | 285 access_code_ = access_code; |
| 289 access_code_lifetime_ = access_code_lifetime; | 286 access_code_lifetime_ = access_code_lifetime; |
| 290 } | 287 } |
| 291 | 288 |
| 292 // Stores the client user's name for the web-app to query. | 289 // Stores the client user's name for the web-app to query. |
| 293 void It2MeNativeMessagingHost::OnClientAuthenticated( | 290 void It2MeNativeMessagingHost::OnClientAuthenticated( |
| 294 const std::string& client_username) { | 291 const std::string& client_username) { |
| 295 DCHECK(task_runner()->BelongsToCurrentThread()); | 292 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 296 | 293 |
| 297 client_username_ = client_username; | 294 client_username_ = client_username; |
| 298 } | 295 } |
| 299 | 296 |
| 300 scoped_refptr<AutoThreadTaskRunner> | 297 scoped_refptr<base::SingleThreadTaskRunner> |
| 301 It2MeNativeMessagingHost::task_runner() const { | 298 It2MeNativeMessagingHost::task_runner() const { |
| 302 return host_context_->ui_task_runner(); | 299 return host_context_->ui_task_runner(); |
| 303 } | 300 } |
| 304 | 301 |
| 305 /* static */ | 302 /* static */ |
| 306 std::string It2MeNativeMessagingHost::HostStateToString( | 303 std::string It2MeNativeMessagingHost::HostStateToString( |
| 307 It2MeHostState host_state) { | 304 It2MeHostState host_state) { |
| 308 return ValueToName(kIt2MeHostStates, host_state); | 305 return ValueToName(kIt2MeHostStates, host_state); |
| 309 } | 306 } |
| 310 | 307 |
| 311 } // namespace remoting | 308 } // namespace remoting |
| 312 | 309 |
| OLD | NEW |