Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: remoting/host/it2me/it2me_native_messaging_host.cc

Issue 591463003: Remote Assistance on Chrome OS Part III - NativeMessageHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@native_messaging
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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::Send(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()); 77 OnMessageFromClient(message.Pass());
81
82 quit_closure_ = quit_closure;
83
84 channel_->Start(this);
85 } 78 }
86 79
87 void It2MeNativeMessagingHost::OnMessage(scoped_ptr<base::Value> message) { 80 void It2MeNativeMessagingHost::set_client(base::WeakPtr<Client> client) {
81 client_ = client;
82 }
83
84 void It2MeNativeMessagingHost::OnMessageFromClient(
85 scoped_ptr<base::Value> message) {
88 DCHECK(task_runner()->BelongsToCurrentThread()); 86 DCHECK(task_runner()->BelongsToCurrentThread());
89 87
90 scoped_ptr<base::DictionaryValue> message_dict( 88 scoped_ptr<base::DictionaryValue> message_dict(
91 static_cast<base::DictionaryValue*>(message.release())); 89 static_cast<base::DictionaryValue*>(message.release()));
92 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); 90 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
93 91
94 // If the client supplies an ID, it will expect it in the response. This 92 // 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. 93 // might be a string or a number, so cope with both.
96 const base::Value* id; 94 const base::Value* id;
97 if (message_dict->Get("id", &id)) 95 if (message_dict->Get("id", &id))
(...skipping 11 matching lines...) Expand all
109 ProcessHello(*message_dict, response.Pass()); 107 ProcessHello(*message_dict, response.Pass());
110 } else if (type == "connect") { 108 } else if (type == "connect") {
111 ProcessConnect(*message_dict, response.Pass()); 109 ProcessConnect(*message_dict, response.Pass());
112 } else if (type == "disconnect") { 110 } else if (type == "disconnect") {
113 ProcessDisconnect(*message_dict, response.Pass()); 111 ProcessDisconnect(*message_dict, response.Pass());
114 } else { 112 } else {
115 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); 113 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type);
116 } 114 }
117 } 115 }
118 116
119 void It2MeNativeMessagingHost::OnDisconnect() { 117 void It2MeNativeMessagingHost::SendMessageToClient(
120 if (!quit_closure_.is_null()) 118 scoped_ptr<base::DictionaryValue> message) const {
121 base::ResetAndReturn(&quit_closure_).Run(); 119 std::string message_json;
120 base::JSONWriter::Write(message.get(), &message_json);
121 client_->PostMessageFromNativeHost(message_json);
122 } 122 }
123 123
124 void It2MeNativeMessagingHost::ProcessHello( 124 void It2MeNativeMessagingHost::ProcessHello(
125 const base::DictionaryValue& message, 125 const base::DictionaryValue& message,
126 scoped_ptr<base::DictionaryValue> response) const { 126 scoped_ptr<base::DictionaryValue> response) const {
127 DCHECK(task_runner()->BelongsToCurrentThread()); 127 DCHECK(task_runner()->BelongsToCurrentThread());
128 128
129 response->SetString("version", STRINGIZE(VERSION)); 129 response->SetString("version", STRINGIZE(VERSION));
130 130
131 // This list will be populated when new features are added. 131 // This list will be populated when new features are added.
132 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); 132 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue());
133 response->Set("supportedFeatures", supported_features_list.release()); 133 response->Set("supportedFeatures", supported_features_list.release());
134 134
135 channel_->SendMessage(response.PassAs<base::Value>()); 135 SendMessageToClient(response.Pass());
136 } 136 }
137 137
138 void It2MeNativeMessagingHost::ProcessConnect( 138 void It2MeNativeMessagingHost::ProcessConnect(
139 const base::DictionaryValue& message, 139 const base::DictionaryValue& message,
140 scoped_ptr<base::DictionaryValue> response) { 140 scoped_ptr<base::DictionaryValue> response) {
141 DCHECK(task_runner()->BelongsToCurrentThread()); 141 DCHECK(task_runner()->BelongsToCurrentThread());
142 142
143 if (it2me_host_.get()) { 143 if (it2me_host_.get()) {
144 SendErrorAndExit(response.Pass(), 144 SendErrorAndExit(response.Pass(),
145 "Connect can be called only when disconnected."); 145 "Connect can be called only when disconnected.");
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 #endif // !defined(NDEBUG) 199 #endif // !defined(NDEBUG)
200 200
201 // Create the It2Me host and start connecting. 201 // Create the It2Me host and start connecting.
202 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), 202 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(),
203 host_context_->ui_task_runner(), 203 host_context_->ui_task_runner(),
204 weak_ptr_, 204 weak_ptr_,
205 xmpp_config, 205 xmpp_config,
206 directory_bot_jid_); 206 directory_bot_jid_);
207 it2me_host_->Connect(); 207 it2me_host_->Connect();
208 208
209 channel_->SendMessage(response.PassAs<base::Value>()); 209 SendMessageToClient(response.Pass());
210 } 210 }
211 211
212 void It2MeNativeMessagingHost::ProcessDisconnect( 212 void It2MeNativeMessagingHost::ProcessDisconnect(
213 const base::DictionaryValue& message, 213 const base::DictionaryValue& message,
214 scoped_ptr<base::DictionaryValue> response) { 214 scoped_ptr<base::DictionaryValue> response) {
215 DCHECK(task_runner()->BelongsToCurrentThread()); 215 DCHECK(task_runner()->BelongsToCurrentThread());
216 216
217 if (it2me_host_.get()) { 217 if (it2me_host_.get()) {
218 it2me_host_->Disconnect(); 218 it2me_host_->Disconnect();
219 it2me_host_ = NULL; 219 it2me_host_ = NULL;
220 } 220 }
221 channel_->SendMessage(response.PassAs<base::Value>()); 221 SendMessageToClient(response.Pass());
222 } 222 }
223 223
224 void It2MeNativeMessagingHost::SendErrorAndExit( 224 void It2MeNativeMessagingHost::SendErrorAndExit(
225 scoped_ptr<base::DictionaryValue> response, 225 scoped_ptr<base::DictionaryValue> response,
226 const std::string& description) const { 226 const std::string& description) const {
227 DCHECK(task_runner()->BelongsToCurrentThread()); 227 DCHECK(task_runner()->BelongsToCurrentThread());
228 228
229 LOG(ERROR) << description; 229 LOG(ERROR) << description;
230 230
231 response->SetString("type", "error"); 231 response->SetString("type", "error");
232 response->SetString("description", description); 232 response->SetString("description", description);
233 channel_->SendMessage(response.PassAs<base::Value>()); 233 SendMessageToClient(response.Pass());
234 234
235 // Trigger a host shutdown by sending a NULL message. 235 // Trigger a host shutdown by sending a NULL message.
236 channel_->SendMessage(scoped_ptr<base::Value>()); 236 client_->CloseChannel("");
237 } 237 }
238 238
239 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { 239 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) {
240 DCHECK(task_runner()->BelongsToCurrentThread()); 240 DCHECK(task_runner()->BelongsToCurrentThread());
241 241
242 state_ = state; 242 state_ = state;
243 243
244 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 244 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
245 245
246 message->SetString("type", "hostStateChanged"); 246 message->SetString("type", "hostStateChanged");
(...skipping 12 matching lines...) Expand all
259 break; 259 break;
260 260
261 case kDisconnected: 261 case kDisconnected:
262 client_username_.clear(); 262 client_username_.clear();
263 break; 263 break;
264 264
265 default: 265 default:
266 ; 266 ;
267 } 267 }
268 268
269 channel_->SendMessage(message.PassAs<base::Value>()); 269 SendMessageToClient(message.Pass());
270 } 270 }
271 271
272 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { 272 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) {
273 DCHECK(task_runner()->BelongsToCurrentThread()); 273 DCHECK(task_runner()->BelongsToCurrentThread());
274 274
275 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 275 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
276 276
277 message->SetString("type", "natPolicyChanged"); 277 message->SetString("type", "natPolicyChanged");
278 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); 278 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled);
279 channel_->SendMessage(message.PassAs<base::Value>()); 279 SendMessageToClient(message.Pass());
280 } 280 }
281 281
282 // Stores the Access Code for the web-app to query. 282 // Stores the Access Code for the web-app to query.
283 void It2MeNativeMessagingHost::OnStoreAccessCode( 283 void It2MeNativeMessagingHost::OnStoreAccessCode(
284 const std::string& access_code, 284 const std::string& access_code,
285 base::TimeDelta access_code_lifetime) { 285 base::TimeDelta access_code_lifetime) {
286 DCHECK(task_runner()->BelongsToCurrentThread()); 286 DCHECK(task_runner()->BelongsToCurrentThread());
287 287
288 access_code_ = access_code; 288 access_code_ = access_code;
289 access_code_lifetime_ = access_code_lifetime; 289 access_code_lifetime_ = access_code_lifetime;
(...skipping 13 matching lines...) Expand all
303 } 303 }
304 304
305 /* static */ 305 /* static */
306 std::string It2MeNativeMessagingHost::HostStateToString( 306 std::string It2MeNativeMessagingHost::HostStateToString(
307 It2MeHostState host_state) { 307 It2MeHostState host_state) {
308 return ValueToName(kIt2MeHostStates, host_state); 308 return ValueToName(kIt2MeHostStates, host_state);
309 } 309 }
310 310
311 } // namespace remoting 311 } // namespace remoting
312 312
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698