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

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: Fix compile error 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 : 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
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 if (!message->IsType(base::Value::TYPE_DICTIONARY)) { 79 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
91 LOG(ERROR) << "Received a message that's not a dictionary."; 80 scoped_ptr<base::Value> message_value(
92 channel_->SendMessage(nullptr); 81 make_scoped_ptr(base::JSONReader::Read(message)));
Sergey Ulanov 2014/10/03 19:22:10 don't need make_scoped_ptr() here. scoped_ptr<> ca
kelvinp 2014/10/06 04:37:26 Done.
82 if (message_value->IsType(base::Value::TYPE_DICTIONARY)) {
83 SendErrorAndExit(response.Pass(),
84 "Received a message that's not a dictionary.");
93 return; 85 return;
94 } 86 }
95 87
96 scoped_ptr<base::DictionaryValue> message_dict( 88 scoped_ptr<base::DictionaryValue> message_dict(
97 static_cast<base::DictionaryValue*>(message.release())); 89 static_cast<base::DictionaryValue*>(message_value.get()));
Sergey Ulanov 2014/10/03 19:22:10 You need release() here instead of get(). With get
kelvinp 2014/10/06 04:37:26 Done.
98 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
99 90
100 // 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
101 // might be a string or a number, so cope with both. 92 // might be a string or a number, so cope with both.
102 const base::Value* id; 93 const base::Value* id;
103 if (message_dict->Get("id", &id)) 94 if (message_dict->Get("id", &id))
104 response->Set("id", id->DeepCopy()); 95 response->Set("id", id->DeepCopy());
105 96
106 std::string type; 97 std::string type;
107 if (!message_dict->GetString("type", &type)) { 98 if (!message_dict->GetString("type", &type)) {
108 SendErrorAndExit(response.Pass(), "'type' not found in request."); 99 SendErrorAndExit(response.Pass(), "'type' not found in request.");
109 return; 100 return;
110 } 101 }
111 102
112 response->SetString("type", type + "Response"); 103 response->SetString("type", type + "Response");
113 104
114 if (type == "hello") { 105 if (type == "hello") {
115 ProcessHello(*message_dict, response.Pass()); 106 ProcessHello(*message_dict, response.Pass());
116 } else if (type == "connect") { 107 } else if (type == "connect") {
117 ProcessConnect(*message_dict, response.Pass()); 108 ProcessConnect(*message_dict, response.Pass());
118 } else if (type == "disconnect") { 109 } else if (type == "disconnect") {
119 ProcessDisconnect(*message_dict, response.Pass()); 110 ProcessDisconnect(*message_dict, response.Pass());
120 } else { 111 } else {
121 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type); 112 SendErrorAndExit(response.Pass(), "Unsupported request type: " + type);
122 } 113 }
123 } 114 }
124 115
125 void It2MeNativeMessagingHost::OnDisconnect() { 116 void It2MeNativeMessagingHost::Start(Client* client) {
126 if (!quit_closure_.is_null()) 117 DCHECK(task_runner()->BelongsToCurrentThread());
127 base::ResetAndReturn(&quit_closure_).Run(); 118 client_ = client;
119 }
120
121 void It2MeNativeMessagingHost::SendMessageToClient(
122 scoped_ptr<base::DictionaryValue> message) const {
123 DCHECK(task_runner()->BelongsToCurrentThread());
124 std::string message_json;
125 base::JSONWriter::Write(message.get(), &message_json);
126 client_->PostMessageFromNativeHost(message_json);
128 } 127 }
129 128
130 void It2MeNativeMessagingHost::ProcessHello( 129 void It2MeNativeMessagingHost::ProcessHello(
131 const base::DictionaryValue& message, 130 const base::DictionaryValue& message,
132 scoped_ptr<base::DictionaryValue> response) const { 131 scoped_ptr<base::DictionaryValue> response) const {
133 DCHECK(task_runner()->BelongsToCurrentThread()); 132 DCHECK(task_runner()->BelongsToCurrentThread());
134 133
135 response->SetString("version", STRINGIZE(VERSION)); 134 response->SetString("version", STRINGIZE(VERSION));
136 135
137 // This list will be populated when new features are added. 136 // This list will be populated when new features are added.
138 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); 137 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue());
139 response->Set("supportedFeatures", supported_features_list.release()); 138 response->Set("supportedFeatures", supported_features_list.release());
140 139
141 channel_->SendMessage(response.Pass()); 140 SendMessageToClient(response.Pass());
142 } 141 }
143 142
144 void It2MeNativeMessagingHost::ProcessConnect( 143 void It2MeNativeMessagingHost::ProcessConnect(
145 const base::DictionaryValue& message, 144 const base::DictionaryValue& message,
146 scoped_ptr<base::DictionaryValue> response) { 145 scoped_ptr<base::DictionaryValue> response) {
147 DCHECK(task_runner()->BelongsToCurrentThread()); 146 DCHECK(task_runner()->BelongsToCurrentThread());
148 147
149 if (it2me_host_.get()) { 148 if (it2me_host_.get()) {
150 SendErrorAndExit(response.Pass(), 149 SendErrorAndExit(response.Pass(),
151 "Connect can be called only when disconnected."); 150 "Connect can be called only when disconnected.");
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 #endif // !defined(NDEBUG) 204 #endif // !defined(NDEBUG)
206 205
207 // Create the It2Me host and start connecting. 206 // Create the It2Me host and start connecting.
208 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(), 207 it2me_host_ = factory_->CreateIt2MeHost(host_context_.get(),
209 host_context_->ui_task_runner(), 208 host_context_->ui_task_runner(),
210 weak_ptr_, 209 weak_ptr_,
211 xmpp_config, 210 xmpp_config,
212 directory_bot_jid_); 211 directory_bot_jid_);
213 it2me_host_->Connect(); 212 it2me_host_->Connect();
214 213
215 channel_->SendMessage(response.Pass()); 214 SendMessageToClient(response.Pass());
216 } 215 }
217 216
218 void It2MeNativeMessagingHost::ProcessDisconnect( 217 void It2MeNativeMessagingHost::ProcessDisconnect(
219 const base::DictionaryValue& message, 218 const base::DictionaryValue& message,
220 scoped_ptr<base::DictionaryValue> response) { 219 scoped_ptr<base::DictionaryValue> response) {
221 DCHECK(task_runner()->BelongsToCurrentThread()); 220 DCHECK(task_runner()->BelongsToCurrentThread());
222 221
223 if (it2me_host_.get()) { 222 if (it2me_host_.get()) {
224 it2me_host_->Disconnect(); 223 it2me_host_->Disconnect();
225 it2me_host_ = NULL; 224 it2me_host_ = NULL;
226 } 225 }
227 channel_->SendMessage(response.Pass()); 226 SendMessageToClient(response.Pass());
228 } 227 }
229 228
230 void It2MeNativeMessagingHost::SendErrorAndExit( 229 void It2MeNativeMessagingHost::SendErrorAndExit(
231 scoped_ptr<base::DictionaryValue> response, 230 scoped_ptr<base::DictionaryValue> response,
232 const std::string& description) const { 231 const std::string& description) const {
233 DCHECK(task_runner()->BelongsToCurrentThread()); 232 DCHECK(task_runner()->BelongsToCurrentThread());
234 233
235 LOG(ERROR) << description; 234 LOG(ERROR) << description;
236 235
237 response->SetString("type", "error"); 236 response->SetString("type", "error");
238 response->SetString("description", description); 237 response->SetString("description", description);
239 channel_->SendMessage(response.Pass()); 238 SendMessageToClient(response.Pass());
240 239
241 // Trigger a host shutdown by sending a NULL message. 240 // Trigger a host shutdown by sending an empty message.
242 channel_->SendMessage(nullptr); 241 client_->CloseChannel(std::string());
243 } 242 }
244 243
245 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) { 244 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state) {
246 DCHECK(task_runner()->BelongsToCurrentThread()); 245 DCHECK(task_runner()->BelongsToCurrentThread());
247 246
248 state_ = state; 247 state_ = state;
249 248
250 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 249 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
251 250
252 message->SetString("type", "hostStateChanged"); 251 message->SetString("type", "hostStateChanged");
(...skipping 12 matching lines...) Expand all
265 break; 264 break;
266 265
267 case kDisconnected: 266 case kDisconnected:
268 client_username_.clear(); 267 client_username_.clear();
269 break; 268 break;
270 269
271 default: 270 default:
272 ; 271 ;
273 } 272 }
274 273
275 channel_->SendMessage(message.Pass()); 274 SendMessageToClient(message.Pass());
276 } 275 }
277 276
278 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { 277 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) {
279 DCHECK(task_runner()->BelongsToCurrentThread()); 278 DCHECK(task_runner()->BelongsToCurrentThread());
280 279
281 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 280 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
282 281
283 message->SetString("type", "natPolicyChanged"); 282 message->SetString("type", "natPolicyChanged");
284 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); 283 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled);
285 channel_->SendMessage(message.Pass()); 284 SendMessageToClient(message.Pass());
286 } 285 }
287 286
288 // Stores the Access Code for the web-app to query. 287 // Stores the Access Code for the web-app to query.
289 void It2MeNativeMessagingHost::OnStoreAccessCode( 288 void It2MeNativeMessagingHost::OnStoreAccessCode(
290 const std::string& access_code, 289 const std::string& access_code,
291 base::TimeDelta access_code_lifetime) { 290 base::TimeDelta access_code_lifetime) {
292 DCHECK(task_runner()->BelongsToCurrentThread()); 291 DCHECK(task_runner()->BelongsToCurrentThread());
293 292
294 access_code_ = access_code; 293 access_code_ = access_code;
295 access_code_lifetime_ = access_code_lifetime; 294 access_code_lifetime_ = access_code_lifetime;
296 } 295 }
297 296
298 // Stores the client user's name for the web-app to query. 297 // Stores the client user's name for the web-app to query.
299 void It2MeNativeMessagingHost::OnClientAuthenticated( 298 void It2MeNativeMessagingHost::OnClientAuthenticated(
300 const std::string& client_username) { 299 const std::string& client_username) {
301 DCHECK(task_runner()->BelongsToCurrentThread()); 300 DCHECK(task_runner()->BelongsToCurrentThread());
302 301
303 client_username_ = client_username; 302 client_username_ = client_username;
304 } 303 }
305 304
306 scoped_refptr<AutoThreadTaskRunner> 305 scoped_refptr<base::SingleThreadTaskRunner>
307 It2MeNativeMessagingHost::task_runner() const { 306 It2MeNativeMessagingHost::task_runner() const {
308 return host_context_->ui_task_runner(); 307 return host_context_->ui_task_runner();
309 } 308 }
310 309
311 /* static */ 310 /* static */
312 std::string It2MeNativeMessagingHost::HostStateToString( 311 std::string It2MeNativeMessagingHost::HostStateToString(
313 It2MeHostState host_state) { 312 It2MeHostState host_state) {
314 return ValueToName(kIt2MeHostStates, host_state); 313 return ValueToName(kIt2MeHostStates, host_state);
315 } 314 }
316 315
317 } // namespace remoting 316 } // namespace remoting
318 317
OLDNEW
« no previous file with comments | « remoting/host/it2me/it2me_native_messaging_host.h ('k') | remoting/host/it2me/it2me_native_messaging_host_main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698