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

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

Issue 1864213002: Convert //remoting to use std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Mac IWYU Created 4 years, 8 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 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 23 matching lines...) Expand all
34 {kRequestedAccessCode, "REQUESTED_ACCESS_CODE"}, 34 {kRequestedAccessCode, "REQUESTED_ACCESS_CODE"},
35 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"}, 35 {kReceivedAccessCode, "RECEIVED_ACCESS_CODE"},
36 {kConnected, "CONNECTED"}, 36 {kConnected, "CONNECTED"},
37 {kError, "ERROR"}, 37 {kError, "ERROR"},
38 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"}, 38 {kInvalidDomainError, "INVALID_DOMAIN_ERROR"},
39 }; 39 };
40 40
41 } // namespace 41 } // namespace
42 42
43 It2MeNativeMessagingHost::It2MeNativeMessagingHost( 43 It2MeNativeMessagingHost::It2MeNativeMessagingHost(
44 scoped_ptr<ChromotingHostContext> context, 44 std::unique_ptr<ChromotingHostContext> context,
45 scoped_ptr<It2MeHostFactory> factory) 45 std::unique_ptr<It2MeHostFactory> factory)
46 : client_(nullptr), 46 : client_(nullptr),
47 host_context_(std::move(context)), 47 host_context_(std::move(context)),
48 factory_(std::move(factory)), 48 factory_(std::move(factory)),
49 weak_factory_(this) { 49 weak_factory_(this) {
50 weak_ptr_ = weak_factory_.GetWeakPtr(); 50 weak_ptr_ = weak_factory_.GetWeakPtr();
51 51
52 const ServiceUrls* service_urls = ServiceUrls::GetInstance(); 52 const ServiceUrls* service_urls = ServiceUrls::GetInstance();
53 const bool xmpp_server_valid = 53 const bool xmpp_server_valid =
54 net::ParseHostAndPort(service_urls->xmpp_server_address(), 54 net::ParseHostAndPort(service_urls->xmpp_server_address(),
55 &xmpp_server_config_.host, 55 &xmpp_server_config_.host,
56 &xmpp_server_config_.port); 56 &xmpp_server_config_.port);
57 DCHECK(xmpp_server_valid); 57 DCHECK(xmpp_server_valid);
58 58
59 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); 59 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls();
60 directory_bot_jid_ = service_urls->directory_bot_jid(); 60 directory_bot_jid_ = service_urls->directory_bot_jid();
61 } 61 }
62 62
63 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() { 63 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {
64 DCHECK(task_runner()->BelongsToCurrentThread()); 64 DCHECK(task_runner()->BelongsToCurrentThread());
65 65
66 if (it2me_host_.get()) { 66 if (it2me_host_.get()) {
67 it2me_host_->Disconnect(); 67 it2me_host_->Disconnect();
68 it2me_host_ = nullptr; 68 it2me_host_ = nullptr;
69 } 69 }
70 } 70 }
71 71
72 void It2MeNativeMessagingHost::OnMessage(const std::string& message) { 72 void It2MeNativeMessagingHost::OnMessage(const std::string& message) {
73 DCHECK(task_runner()->BelongsToCurrentThread()); 73 DCHECK(task_runner()->BelongsToCurrentThread());
74 74
75 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue()); 75 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue());
76 scoped_ptr<base::Value> message_value = base::JSONReader::Read(message); 76 std::unique_ptr<base::Value> message_value = base::JSONReader::Read(message);
77 if (!message_value->IsType(base::Value::TYPE_DICTIONARY)) { 77 if (!message_value->IsType(base::Value::TYPE_DICTIONARY)) {
78 LOG(ERROR) << "Received a message that's not a dictionary."; 78 LOG(ERROR) << "Received a message that's not a dictionary.";
79 client_->CloseChannel(std::string()); 79 client_->CloseChannel(std::string());
80 return; 80 return;
81 } 81 }
82 82
83 scoped_ptr<base::DictionaryValue> message_dict( 83 std::unique_ptr<base::DictionaryValue> message_dict(
84 static_cast<base::DictionaryValue*>(message_value.release())); 84 static_cast<base::DictionaryValue*>(message_value.release()));
85 85
86 // 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
87 // might be a string or a number, so cope with both. 87 // might be a string or a number, so cope with both.
88 const base::Value* id; 88 const base::Value* id;
89 if (message_dict->Get("id", &id)) 89 if (message_dict->Get("id", &id))
90 response->Set("id", id->DeepCopy()); 90 response->Set("id", id->DeepCopy());
91 91
92 std::string type; 92 std::string type;
93 if (!message_dict->GetString("type", &type)) { 93 if (!message_dict->GetString("type", &type)) {
(...skipping 19 matching lines...) Expand all
113 client_ = client; 113 client_ = client;
114 #if !defined(OS_CHROMEOS) 114 #if !defined(OS_CHROMEOS)
115 log_message_handler_.reset( 115 log_message_handler_.reset(
116 new LogMessageHandler( 116 new LogMessageHandler(
117 base::Bind(&It2MeNativeMessagingHost::SendMessageToClient, 117 base::Bind(&It2MeNativeMessagingHost::SendMessageToClient,
118 base::Unretained(this)))); 118 base::Unretained(this))));
119 #endif // !defined(OS_CHROMEOS) 119 #endif // !defined(OS_CHROMEOS)
120 } 120 }
121 121
122 void It2MeNativeMessagingHost::SendMessageToClient( 122 void It2MeNativeMessagingHost::SendMessageToClient(
123 scoped_ptr<base::Value> message) const { 123 std::unique_ptr<base::Value> message) const {
124 DCHECK(task_runner()->BelongsToCurrentThread()); 124 DCHECK(task_runner()->BelongsToCurrentThread());
125 std::string message_json; 125 std::string message_json;
126 base::JSONWriter::Write(*message, &message_json); 126 base::JSONWriter::Write(*message, &message_json);
127 client_->PostMessageFromNativeHost(message_json); 127 client_->PostMessageFromNativeHost(message_json);
128 } 128 }
129 129
130 void It2MeNativeMessagingHost::ProcessHello( 130 void It2MeNativeMessagingHost::ProcessHello(
131 const base::DictionaryValue& message, 131 const base::DictionaryValue& message,
132 scoped_ptr<base::DictionaryValue> response) const { 132 std::unique_ptr<base::DictionaryValue> response) const {
133 DCHECK(task_runner()->BelongsToCurrentThread()); 133 DCHECK(task_runner()->BelongsToCurrentThread());
134 134
135 response->SetString("version", STRINGIZE(VERSION)); 135 response->SetString("version", STRINGIZE(VERSION));
136 136
137 // This list will be populated when new features are added. 137 // This list will be populated when new features are added.
138 scoped_ptr<base::ListValue> supported_features_list(new base::ListValue()); 138 std::unique_ptr<base::ListValue> supported_features_list(
139 new base::ListValue());
139 response->Set("supportedFeatures", supported_features_list.release()); 140 response->Set("supportedFeatures", supported_features_list.release());
140 141
141 SendMessageToClient(std::move(response)); 142 SendMessageToClient(std::move(response));
142 } 143 }
143 144
144 void It2MeNativeMessagingHost::ProcessConnect( 145 void It2MeNativeMessagingHost::ProcessConnect(
145 const base::DictionaryValue& message, 146 const base::DictionaryValue& message,
146 scoped_ptr<base::DictionaryValue> response) { 147 std::unique_ptr<base::DictionaryValue> response) {
147 DCHECK(task_runner()->BelongsToCurrentThread()); 148 DCHECK(task_runner()->BelongsToCurrentThread());
148 149
149 if (it2me_host_.get()) { 150 if (it2me_host_.get()) {
150 SendErrorAndExit(std::move(response), 151 SendErrorAndExit(std::move(response),
151 "Connect can be called only when disconnected."); 152 "Connect can be called only when disconnected.");
152 return; 153 return;
153 } 154 }
154 155
155 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; 156 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_;
156 157
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 weak_ptr_, 214 weak_ptr_,
214 xmpp_config, 215 xmpp_config,
215 directory_bot_jid_); 216 directory_bot_jid_);
216 it2me_host_->Connect(); 217 it2me_host_->Connect();
217 218
218 SendMessageToClient(std::move(response)); 219 SendMessageToClient(std::move(response));
219 } 220 }
220 221
221 void It2MeNativeMessagingHost::ProcessDisconnect( 222 void It2MeNativeMessagingHost::ProcessDisconnect(
222 const base::DictionaryValue& message, 223 const base::DictionaryValue& message,
223 scoped_ptr<base::DictionaryValue> response) { 224 std::unique_ptr<base::DictionaryValue> response) {
224 DCHECK(task_runner()->BelongsToCurrentThread()); 225 DCHECK(task_runner()->BelongsToCurrentThread());
225 226
226 if (it2me_host_.get()) { 227 if (it2me_host_.get()) {
227 it2me_host_->Disconnect(); 228 it2me_host_->Disconnect();
228 it2me_host_ = nullptr; 229 it2me_host_ = nullptr;
229 } 230 }
230 SendMessageToClient(std::move(response)); 231 SendMessageToClient(std::move(response));
231 } 232 }
232 233
233 void It2MeNativeMessagingHost::SendErrorAndExit( 234 void It2MeNativeMessagingHost::SendErrorAndExit(
234 scoped_ptr<base::DictionaryValue> response, 235 std::unique_ptr<base::DictionaryValue> response,
235 const std::string& description) const { 236 const std::string& description) const {
236 DCHECK(task_runner()->BelongsToCurrentThread()); 237 DCHECK(task_runner()->BelongsToCurrentThread());
237 238
238 LOG(ERROR) << description; 239 LOG(ERROR) << description;
239 240
240 response->SetString("type", "error"); 241 response->SetString("type", "error");
241 response->SetString("description", description); 242 response->SetString("description", description);
242 SendMessageToClient(std::move(response)); 243 SendMessageToClient(std::move(response));
243 244
244 // Trigger a host shutdown by sending an empty message. 245 // Trigger a host shutdown by sending an empty message.
245 client_->CloseChannel(std::string()); 246 client_->CloseChannel(std::string());
246 } 247 }
247 248
248 void It2MeNativeMessagingHost::OnStateChanged( 249 void It2MeNativeMessagingHost::OnStateChanged(
249 It2MeHostState state, 250 It2MeHostState state,
250 const std::string& error_message) { 251 const std::string& error_message) {
251 DCHECK(task_runner()->BelongsToCurrentThread()); 252 DCHECK(task_runner()->BelongsToCurrentThread());
252 253
253 state_ = state; 254 state_ = state;
254 255
255 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 256 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue());
256 257
257 message->SetString("type", "hostStateChanged"); 258 message->SetString("type", "hostStateChanged");
258 message->SetString("state", HostStateToString(state)); 259 message->SetString("state", HostStateToString(state));
259 260
260 switch (state_) { 261 switch (state_) {
261 case kReceivedAccessCode: 262 case kReceivedAccessCode:
262 message->SetString("accessCode", access_code_); 263 message->SetString("accessCode", access_code_);
263 message->SetInteger("accessCodeLifetime", 264 message->SetInteger("accessCodeLifetime",
264 access_code_lifetime_.InSeconds()); 265 access_code_lifetime_.InSeconds());
265 break; 266 break;
(...skipping 17 matching lines...) Expand all
283 default: 284 default:
284 break; 285 break;
285 } 286 }
286 287
287 SendMessageToClient(std::move(message)); 288 SendMessageToClient(std::move(message));
288 } 289 }
289 290
290 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) { 291 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled) {
291 DCHECK(task_runner()->BelongsToCurrentThread()); 292 DCHECK(task_runner()->BelongsToCurrentThread());
292 293
293 scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue()); 294 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue());
294 295
295 message->SetString("type", "natPolicyChanged"); 296 message->SetString("type", "natPolicyChanged");
296 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled); 297 message->SetBoolean("natTraversalEnabled", nat_traversal_enabled);
297 SendMessageToClient(std::move(message)); 298 SendMessageToClient(std::move(message));
298 } 299 }
299 300
300 // Stores the Access Code for the web-app to query. 301 // Stores the Access Code for the web-app to query.
301 void It2MeNativeMessagingHost::OnStoreAccessCode( 302 void It2MeNativeMessagingHost::OnStoreAccessCode(
302 const std::string& access_code, 303 const std::string& access_code,
303 base::TimeDelta access_code_lifetime) { 304 base::TimeDelta access_code_lifetime) {
(...skipping 16 matching lines...) Expand all
320 return host_context_->ui_task_runner(); 321 return host_context_->ui_task_runner();
321 } 322 }
322 323
323 /* static */ 324 /* static */
324 std::string It2MeNativeMessagingHost::HostStateToString( 325 std::string It2MeNativeMessagingHost::HostStateToString(
325 It2MeHostState host_state) { 326 It2MeHostState host_state) {
326 return ValueToName(kIt2MeHostStates, host_state); 327 return ValueToName(kIt2MeHostStates, host_state);
327 } 328 }
328 329
329 } // namespace remoting 330 } // namespace remoting
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