1 // Copyright 2014 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/host_signaling_manager.h"
8 #include "base/time/time.h"
9 #include "net/socket/client_socket_factory.h"
10 #include "remoting/base/auto_thread_task_runner.h"
11 #include "remoting/base/logging.h"
12 #include "remoting/base/url_request_context_getter.h"
13 #include "remoting/host/chromoting_host_context.h"
14 #include "remoting/host/dns_blackhole_checker.h"
15 #include "remoting/host/heartbeat_sender.h"
16 #include "remoting/host/signaling_connector.h"
17 #include "remoting/signaling/xmpp_signal_strategy.h"
21 HostSignalingManager::HostSignalingManager(
22 scoped_ptr
<base::WeakPtrFactory
<Listener
>> weak_factory_for_listener
,
23 const scoped_refptr
<AutoThreadTaskRunner
>& network_task_runner
,
24 scoped_ptr
<SignalStrategy
> signal_strategy
,
25 scoped_ptr
<SignalingConnector
> signaling_connector
,
26 scoped_ptr
<HeartbeatSender
> heartbeat_sender
)
27 : weak_factory_for_listener_(weak_factory_for_listener
.Pass()),
28 network_task_runner_(network_task_runner
),
29 signal_strategy_(signal_strategy
.Pass()),
30 signaling_connector_(signaling_connector
.Pass()),
31 heartbeat_sender_(heartbeat_sender
.Pass()) {
34 scoped_ptr
<HostSignalingManager
> HostSignalingManager::Create(
36 const scoped_refptr
<AutoThreadTaskRunner
>& network_task_runner
,
37 const scoped_refptr
<net::URLRequestContextGetter
>&
38 url_request_context_getter
,
39 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
40 const std::string
& talkgadget_prefix_policy
,
41 const std::string
& host_id
,
42 const scoped_refptr
<const RsaKeyPair
>& host_key_pair
,
43 const std::string
& directory_bot_jid
,
44 scoped_ptr
<OAuthTokenGetter::OAuthCredentials
> oauth_credentials
) {
45 DCHECK(network_task_runner
->BelongsToCurrentThread());
47 scoped_ptr
<base::WeakPtrFactory
<Listener
>> weak_factory(
48 new base::WeakPtrFactory
<Listener
>(listener
));
50 scoped_ptr
<XmppSignalStrategy
> signal_strategy(
51 new XmppSignalStrategy(net::ClientSocketFactory::GetDefaultFactory(),
52 url_request_context_getter
, xmpp_server_config
));
54 scoped_ptr
<DnsBlackholeChecker
> dns_blackhole_checker(new DnsBlackholeChecker(
55 url_request_context_getter
, talkgadget_prefix_policy
));
57 scoped_ptr
<SignalingConnector
> signaling_connector(new SignalingConnector(
58 signal_strategy
.get(), dns_blackhole_checker
.Pass(),
59 base::Bind(&Listener::OnAuthFailed
, weak_factory
->GetWeakPtr())));
61 if (!oauth_credentials
->refresh_token
.empty()) {
62 scoped_ptr
<OAuthTokenGetter
> oauth_token_getter(new OAuthTokenGetter(
63 oauth_credentials
.Pass(), url_request_context_getter
, false));
65 signaling_connector
->EnableOAuth(oauth_token_getter
.Pass());
68 scoped_ptr
<HeartbeatSender
> heartbeat_sender(new HeartbeatSender(
69 base::Bind(&Listener::OnHeartbeatSuccessful
, weak_factory
->GetWeakPtr()),
70 base::Bind(&Listener::OnUnknownHostIdError
, weak_factory
->GetWeakPtr()),
71 host_id
, signal_strategy
.get(), host_key_pair
, directory_bot_jid
));
73 return scoped_ptr
<HostSignalingManager
>(new HostSignalingManager(
74 weak_factory
.Pass(), network_task_runner
, signal_strategy
.Pass(),
75 signaling_connector
.Pass(), heartbeat_sender
.Pass()));
78 HostSignalingManager::~HostSignalingManager() {
79 DCHECK(network_task_runner_
->BelongsToCurrentThread());
82 void HostSignalingManager::SendHostOfflineReasonAndDelete(
83 const std::string
& host_offline_reason
,
84 const base::TimeDelta
& timeout
) {
85 DCHECK(network_task_runner_
->BelongsToCurrentThread());
87 HOST_LOG
<< "SendHostOfflineReason: sending " << host_offline_reason
;
89 // Ensure that any subsequent callbacks from the HeartbeatSender or
90 // SignalingConnector don't touch the Listener.
91 weak_factory_for_listener_
->InvalidateWeakPtrs();
93 heartbeat_sender_
->SetHostOfflineReason(
94 host_offline_reason
, timeout
,
95 base::Bind(&HostSignalingManager::OnHostOfflineReasonAck
,
96 base::Unretained(this)));
99 void HostSignalingManager::OnHostOfflineReasonAck(bool success
) {
100 DCHECK(network_task_runner_
->BelongsToCurrentThread());
102 HOST_LOG
<< "SendHostOfflineReason: succeeded";
104 HOST_LOG
<< "SendHostOfflineReason: timed out";
110 } // namespace remoting