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

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