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
3 // found in the LICENSE file.
5 #include "remoting/host/it2me/it2me_native_messaging_host.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/json/json_reader.h"
13 #include "base/json/json_writer.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringize_macros.h"
16 #include "base/threading/thread.h"
17 #include "base/values.h"
18 #include "media/base/media.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "remoting/base/auth_token_util.h"
22 #include "remoting/base/service_urls.h"
23 #include "remoting/host/chromoting_host_context.h"
24 #include "remoting/host/host_exit_codes.h"
25 #include "remoting/protocol/name_value_map.h"
31 const remoting::protocol::NameMapElement
<It2MeHostState
> kIt2MeHostStates
[] = {
32 {kDisconnected
, "DISCONNECTED"},
33 {kStarting
, "STARTING"},
34 {kRequestedAccessCode
, "REQUESTED_ACCESS_CODE"},
35 {kReceivedAccessCode
, "RECEIVED_ACCESS_CODE"},
36 {kConnected
, "CONNECTED"},
37 {kDisconnecting
, "DISCONNECTING"},
39 {kInvalidDomainError
, "INVALID_DOMAIN_ERROR"},
44 It2MeNativeMessagingHost::It2MeNativeMessagingHost(
45 scoped_ptr
<ChromotingHostContext
> context
,
46 scoped_ptr
<It2MeHostFactory
> factory
)
48 host_context_(context
.Pass()),
49 factory_(factory
.Pass()),
51 weak_ptr_
= weak_factory_
.GetWeakPtr();
53 // Ensures runtime specific CPU features are initialized.
54 media::InitializeCPUSpecificMediaFeatures();
56 const ServiceUrls
* service_urls
= ServiceUrls::GetInstance();
57 const bool xmpp_server_valid
=
58 net::ParseHostAndPort(service_urls
->xmpp_server_address(),
59 &xmpp_server_config_
.host
,
60 &xmpp_server_config_
.port
);
61 DCHECK(xmpp_server_valid
);
63 xmpp_server_config_
.use_tls
= service_urls
->xmpp_server_use_tls();
64 directory_bot_jid_
= service_urls
->directory_bot_jid();
67 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {
68 DCHECK(task_runner()->BelongsToCurrentThread());
70 if (it2me_host_
.get()) {
71 it2me_host_
->Disconnect();
76 void It2MeNativeMessagingHost::OnMessage(const std::string
& message
) {
77 DCHECK(task_runner()->BelongsToCurrentThread());
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
)) {
82 LOG(ERROR
) << "Received a message that's not a dictionary.";
83 client_
->CloseChannel(std::string());
87 scoped_ptr
<base::DictionaryValue
> message_dict(
88 static_cast<base::DictionaryValue
*>(message_value
.release()));
90 // If the client supplies an ID, it will expect it in the response. This
91 // might be a string or a number, so cope with both.
92 const base::Value
* id
;
93 if (message_dict
->Get("id", &id
))
94 response
->Set("id", id
->DeepCopy());
97 if (!message_dict
->GetString("type", &type
)) {
98 SendErrorAndExit(response
.Pass(), "'type' not found in request.");
102 response
->SetString("type", type
+ "Response");
104 if (type
== "hello") {
105 ProcessHello(*message_dict
, response
.Pass());
106 } else if (type
== "connect") {
107 ProcessConnect(*message_dict
, response
.Pass());
108 } else if (type
== "disconnect") {
109 ProcessDisconnect(*message_dict
, response
.Pass());
111 SendErrorAndExit(response
.Pass(), "Unsupported request type: " + type
);
115 void It2MeNativeMessagingHost::Start(Client
* client
) {
116 DCHECK(task_runner()->BelongsToCurrentThread());
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 void It2MeNativeMessagingHost::ProcessHello(
129 const base::DictionaryValue
& message
,
130 scoped_ptr
<base::DictionaryValue
> response
) const {
131 DCHECK(task_runner()->BelongsToCurrentThread());
133 response
->SetString("version", STRINGIZE(VERSION
));
135 // This list will be populated when new features are added.
136 scoped_ptr
<base::ListValue
> supported_features_list(new base::ListValue());
137 response
->Set("supportedFeatures", supported_features_list
.release());
139 SendMessageToClient(response
.Pass());
142 void It2MeNativeMessagingHost::ProcessConnect(
143 const base::DictionaryValue
& message
,
144 scoped_ptr
<base::DictionaryValue
> response
) {
145 DCHECK(task_runner()->BelongsToCurrentThread());
147 if (it2me_host_
.get()) {
148 SendErrorAndExit(response
.Pass(),
149 "Connect can be called only when disconnected.");
153 XmppSignalStrategy::XmppServerConfig xmpp_config
= xmpp_server_config_
;
155 if (!message
.GetString("userName", &xmpp_config
.username
)) {
156 SendErrorAndExit(response
.Pass(), "'userName' not found in request.");
160 std::string auth_service_with_token
;
161 if (!message
.GetString("authServiceWithToken", &auth_service_with_token
)) {
162 SendErrorAndExit(response
.Pass(),
163 "'authServiceWithToken' not found in request.");
167 ParseAuthTokenWithService(auth_service_with_token
,
168 &xmpp_config
.auth_token
,
169 &xmpp_config
.auth_service
);
170 if (xmpp_config
.auth_token
.empty()) {
173 "Invalid 'authServiceWithToken': " + auth_service_with_token
);
179 if (!message
.GetString("xmppServerAddress", &address
)) {
180 SendErrorAndExit(response
.Pass(),
181 "'xmppServerAddress' not found in request.");
185 if (!net::ParseHostAndPort(
186 address
, &xmpp_server_config_
.host
, &xmpp_server_config_
.port
)) {
187 SendErrorAndExit(response
.Pass(),
188 "Invalid 'xmppServerAddress': " + address
);
192 if (!message
.GetBoolean("xmppServerUseTls", &xmpp_server_config_
.use_tls
)) {
193 SendErrorAndExit(response
.Pass(),
194 "'xmppServerUseTls' not found in request.");
198 if (!message
.GetString("directoryBotJid", &directory_bot_jid_
)) {
199 SendErrorAndExit(response
.Pass(),
200 "'directoryBotJid' not found in request.");
203 #endif // !defined(NDEBUG)
205 // Create the It2Me host and start connecting.
206 it2me_host_
= factory_
->CreateIt2MeHost(host_context_
->Copy(),
210 it2me_host_
->Connect();
212 SendMessageToClient(response
.Pass());
215 void It2MeNativeMessagingHost::ProcessDisconnect(
216 const base::DictionaryValue
& message
,
217 scoped_ptr
<base::DictionaryValue
> response
) {
218 DCHECK(task_runner()->BelongsToCurrentThread());
220 if (it2me_host_
.get()) {
221 it2me_host_
->Disconnect();
224 SendMessageToClient(response
.Pass());
227 void It2MeNativeMessagingHost::SendErrorAndExit(
228 scoped_ptr
<base::DictionaryValue
> response
,
229 const std::string
& description
) const {
230 DCHECK(task_runner()->BelongsToCurrentThread());
232 LOG(ERROR
) << description
;
234 response
->SetString("type", "error");
235 response
->SetString("description", description
);
236 SendMessageToClient(response
.Pass());
238 // Trigger a host shutdown by sending an empty message.
239 client_
->CloseChannel(std::string());
242 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state
) {
243 DCHECK(task_runner()->BelongsToCurrentThread());
247 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue());
249 message
->SetString("type", "hostStateChanged");
250 message
->SetString("state",
251 It2MeNativeMessagingHost::HostStateToString(state
));
254 case kReceivedAccessCode
:
255 message
->SetString("accessCode", access_code_
);
256 message
->SetInteger("accessCodeLifetime",
257 access_code_lifetime_
.InSeconds());
261 message
->SetString("client", client_username_
);
265 client_username_
.clear();
272 SendMessageToClient(message
.Pass());
275 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled
) {
276 DCHECK(task_runner()->BelongsToCurrentThread());
278 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue());
280 message
->SetString("type", "natPolicyChanged");
281 message
->SetBoolean("natTraversalEnabled", nat_traversal_enabled
);
282 SendMessageToClient(message
.Pass());
285 // Stores the Access Code for the web-app to query.
286 void It2MeNativeMessagingHost::OnStoreAccessCode(
287 const std::string
& access_code
,
288 base::TimeDelta access_code_lifetime
) {
289 DCHECK(task_runner()->BelongsToCurrentThread());
291 access_code_
= access_code
;
292 access_code_lifetime_
= access_code_lifetime
;
295 // Stores the client user's name for the web-app to query.
296 void It2MeNativeMessagingHost::OnClientAuthenticated(
297 const std::string
& client_username
) {
298 DCHECK(task_runner()->BelongsToCurrentThread());
300 client_username_
= client_username
;
303 scoped_refptr
<base::SingleThreadTaskRunner
>
304 It2MeNativeMessagingHost::task_runner() const {
305 return host_context_
->ui_task_runner();
309 std::string
It2MeNativeMessagingHost::HostStateToString(
310 It2MeHostState host_state
) {
311 return ValueToName(kIt2MeHostStates
, host_state
);
314 } // namespace remoting