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/callback_helpers.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringize_macros.h"
17 #include "base/threading/thread.h"
18 #include "base/values.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_fetcher.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"}, };
43 It2MeNativeMessagingHost::It2MeNativeMessagingHost(
44 scoped_refptr
<AutoThreadTaskRunner
> task_runner
,
45 scoped_ptr
<NativeMessagingChannel
> channel
,
46 scoped_ptr
<It2MeHostFactory
> factory
)
47 : channel_(channel
.Pass()),
48 factory_(factory
.Pass()),
50 weak_ptr_
= weak_factory_
.GetWeakPtr();
52 // Initialize the host context to manage the threads for the it2me host.
53 // The native messaging host, rather than the It2MeHost object, owns and
54 // maintains the lifetime of the host context.
56 host_context_
.reset(ChromotingHostContext::Create(task_runner
).release());
58 const ServiceUrls
* service_urls
= ServiceUrls::GetInstance();
59 const bool xmpp_server_valid
=
60 net::ParseHostAndPort(service_urls
->xmpp_server_address(),
61 &xmpp_server_config_
.host
,
62 &xmpp_server_config_
.port
);
63 DCHECK(xmpp_server_valid
);
65 xmpp_server_config_
.use_tls
= service_urls
->xmpp_server_use_tls();
66 directory_bot_jid_
= service_urls
->directory_bot_jid();
69 It2MeNativeMessagingHost::~It2MeNativeMessagingHost() {
70 DCHECK(task_runner()->BelongsToCurrentThread());
72 if (it2me_host_
.get()) {
73 it2me_host_
->Disconnect();
78 void It2MeNativeMessagingHost::Start(const base::Closure
& quit_closure
) const {
79 DCHECK(task_runner()->BelongsToCurrentThread());
82 base::Bind(&It2MeNativeMessagingHost::ProcessMessage
, weak_ptr_
),
86 void It2MeNativeMessagingHost::ProcessMessage(
87 scoped_ptr
<base::DictionaryValue
> message
) {
88 DCHECK(task_runner()->BelongsToCurrentThread());
90 scoped_ptr
<base::DictionaryValue
> response(new base::DictionaryValue());
92 // If the client supplies an ID, it will expect it in the response. This
93 // might be a string or a number, so cope with both.
94 const base::Value
* id
;
95 if (message
->Get("id", &id
))
96 response
->Set("id", id
->DeepCopy());
99 if (!message
->GetString("type", &type
)) {
100 SendErrorAndExit(response
.Pass(), "'type' not found in request.");
104 response
->SetString("type", type
+ "Response");
106 if (type
== "hello") {
107 ProcessHello(*message
, response
.Pass());
108 } else if (type
== "connect") {
109 ProcessConnect(*message
, response
.Pass());
110 } else if (type
== "disconnect") {
111 ProcessDisconnect(*message
, response
.Pass());
113 SendErrorAndExit(response
.Pass(), "Unsupported request type: " + type
);
117 void It2MeNativeMessagingHost::ProcessHello(
118 const base::DictionaryValue
& message
,
119 scoped_ptr
<base::DictionaryValue
> response
) const {
120 DCHECK(task_runner()->BelongsToCurrentThread());
122 response
->SetString("version", STRINGIZE(VERSION
));
124 // This list will be populated when new features are added.
125 scoped_ptr
<base::ListValue
> supported_features_list(new base::ListValue());
126 response
->Set("supportedFeatures", supported_features_list
.release());
128 channel_
->SendMessage(response
.Pass());
131 void It2MeNativeMessagingHost::ProcessConnect(
132 const base::DictionaryValue
& message
,
133 scoped_ptr
<base::DictionaryValue
> response
) {
134 DCHECK(task_runner()->BelongsToCurrentThread());
136 if (it2me_host_
.get()) {
137 SendErrorAndExit(response
.Pass(),
138 "Connect can be called only when disconnected.");
142 XmppSignalStrategy::XmppServerConfig xmpp_config
= xmpp_server_config_
;
144 if (!message
.GetString("userName", &xmpp_config
.username
)) {
145 SendErrorAndExit(response
.Pass(), "'userName' not found in request.");
149 std::string auth_service_with_token
;
150 if (!message
.GetString("authServiceWithToken", &auth_service_with_token
)) {
151 SendErrorAndExit(response
.Pass(),
152 "'authServiceWithToken' not found in request.");
156 ParseAuthTokenWithService(auth_service_with_token
,
157 &xmpp_config
.auth_token
,
158 &xmpp_config
.auth_service
);
159 if (xmpp_config
.auth_token
.empty()) {
162 "Invalid 'authServiceWithToken': " + auth_service_with_token
);
168 if (!message
.GetString("xmppServerAddress", &address
)) {
169 SendErrorAndExit(response
.Pass(),
170 "'xmppServerAddress' not found in request.");
174 if (!net::ParseHostAndPort(
175 address
, &xmpp_server_config_
.host
, &xmpp_server_config_
.port
)) {
176 SendErrorAndExit(response
.Pass(),
177 "Invalid 'xmppServerAddress': " + address
);
181 if (!message
.GetBoolean("xmppServerUseTls", &xmpp_server_config_
.use_tls
)) {
182 SendErrorAndExit(response
.Pass(),
183 "'xmppServerUseTls' not found in request.");
187 if (!message
.GetString("directoryBotJid", &directory_bot_jid_
)) {
188 SendErrorAndExit(response
.Pass(),
189 "'directoryBotJid' not found in request.");
192 #endif // !defined(NDEBUG)
194 // Create the It2Me host and start connecting.
195 it2me_host_
= factory_
->CreateIt2MeHost(host_context_
.get(),
196 host_context_
->ui_task_runner(),
200 it2me_host_
->Connect();
202 channel_
->SendMessage(response
.Pass());
205 void It2MeNativeMessagingHost::ProcessDisconnect(
206 const base::DictionaryValue
& message
,
207 scoped_ptr
<base::DictionaryValue
> response
) {
208 DCHECK(task_runner()->BelongsToCurrentThread());
210 if (it2me_host_
.get()) {
211 it2me_host_
->Disconnect();
214 channel_
->SendMessage(response
.Pass());
217 void It2MeNativeMessagingHost::SendErrorAndExit(
218 scoped_ptr
<base::DictionaryValue
> response
,
219 const std::string
& description
) const {
220 DCHECK(task_runner()->BelongsToCurrentThread());
222 LOG(ERROR
) << description
;
224 response
->SetString("type", "error");
225 response
->SetString("description", description
);
226 channel_
->SendMessage(response
.Pass());
228 // Trigger a host shutdown by sending a NULL message.
229 channel_
->SendMessage(scoped_ptr
<base::DictionaryValue
>());
232 void It2MeNativeMessagingHost::OnStateChanged(It2MeHostState state
) {
233 DCHECK(task_runner()->BelongsToCurrentThread());
237 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue());
239 message
->SetString("type", "hostStateChanged");
240 message
->SetString("state",
241 It2MeNativeMessagingHost::HostStateToString(state
));
244 case kReceivedAccessCode
:
245 message
->SetString("accessCode", access_code_
);
246 message
->SetInteger("accessCodeLifetime",
247 access_code_lifetime_
.InSeconds());
251 message
->SetString("client", client_username_
);
255 client_username_
.clear();
262 channel_
->SendMessage(message
.Pass());
265 void It2MeNativeMessagingHost::OnNatPolicyChanged(bool nat_traversal_enabled
) {
266 DCHECK(task_runner()->BelongsToCurrentThread());
268 scoped_ptr
<base::DictionaryValue
> message(new base::DictionaryValue());
270 message
->SetString("type", "natPolicyChanged");
271 message
->SetBoolean("natTraversalEnabled", nat_traversal_enabled
);
272 channel_
->SendMessage(message
.Pass());
275 // Stores the Access Code for the web-app to query.
276 void It2MeNativeMessagingHost::OnStoreAccessCode(
277 const std::string
& access_code
,
278 base::TimeDelta access_code_lifetime
) {
279 DCHECK(task_runner()->BelongsToCurrentThread());
281 access_code_
= access_code
;
282 access_code_lifetime_
= access_code_lifetime
;
285 // Stores the client user's name for the web-app to query.
286 void It2MeNativeMessagingHost::OnClientAuthenticated(
287 const std::string
& client_username
) {
288 DCHECK(task_runner()->BelongsToCurrentThread());
290 client_username_
= client_username
;
293 scoped_refptr
<AutoThreadTaskRunner
>
294 It2MeNativeMessagingHost::task_runner() const {
295 return host_context_
->ui_task_runner();
299 std::string
It2MeNativeMessagingHost::HostStateToString(
300 It2MeHostState host_state
) {
301 return ValueToName(kIt2MeHostStates
, host_state
);
304 } // namespace remoting