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 #ifndef REMOTING_HOST_HOST_SIGNALING_MANAGER_H_
6 #define REMOTING_HOST_HOST_SIGNALING_MANAGER_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "remoting/base/auto_thread_task_runner.h"
14 #include "remoting/base/rsa_key_pair.h"
15 #include "remoting/host/oauth_token_getter.h"
16 #include "remoting/signaling/xmpp_signal_strategy.h"
23 class NetworkChangeNotifier
;
28 class ChromotingHostContext
;
29 class DnsBlackholeChecker
;
30 class HeartbeatSender
;
31 class OAuthTokenGetter
;
33 class SignalingConnector
;
35 // HostSignalingManager has 2 functions:
36 // 1. Keep sending regular heartbeats to the Chromoting Directory.
37 // 2. Keep the host process alive while sending host-offline-reason heartbeat.
38 class HostSignalingManager
{
42 virtual ~Listener() {}
44 // Invoked after the first successful heartbeat.
45 virtual void OnHeartbeatSuccessful() = 0;
47 // Invoked when the host ID is permanently not recognized by the server.
48 virtual void OnUnknownHostIdError() = 0;
50 // Invoked when authentication fails.
51 virtual void OnAuthFailed() = 0;
54 // TODO(lukasza): Refactor to limit the number of parameters below.
55 // Probably necessitates refactoring HostProcess to extract a new
56 // class to read and store config/policy/cmdline values.
58 // |listener| has to be valid until either
59 // 1) the returned HostSignalingManager is destroyed
60 // or 2) SendHostOfflineReasonAndDelete is called.
61 static scoped_ptr
<HostSignalingManager
> Create(
63 const scoped_refptr
<AutoThreadTaskRunner
>& network_task_runner
,
64 const scoped_refptr
<net::URLRequestContextGetter
>&
65 url_request_context_getter
,
66 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
67 const std::string
& talkgadget_prefix_policy
,
68 const std::string
& host_id
,
69 const scoped_refptr
<const RsaKeyPair
>& host_key_pair
,
70 const std::string
& directory_bot_jid
,
71 scoped_ptr
<OAuthTokenGetter::OAuthCredentials
> oauth_credentials
);
73 ~HostSignalingManager();
75 // Get the SignalStrategy to use for talking to the Chromoting bot.
76 // Returned SignalStrategy remains owned by the HostSignalingManager.
77 SignalStrategy
* signal_strategy() { return signal_strategy_
.get(); }
79 // Kicks off sending a heartbeat containing a host-offline-reason attribute.
80 // Prevents future calls to the |listener| provided to the Create method.
82 // Will delete |this| once either the bot acks receiving the
83 // |host_offline_reason|, or the |timeout| is reached. Deleting
84 // |this| will release |network_task_runner_| and allow the host
86 void SendHostOfflineReasonAndDelete(const std::string
& host_offline_reason
,
87 const base::TimeDelta
& timeout
);
91 scoped_ptr
<base::WeakPtrFactory
<Listener
>> weak_factory_for_listener
,
92 const scoped_refptr
<AutoThreadTaskRunner
>& network_task_runner
,
93 scoped_ptr
<SignalStrategy
> signal_strategy
,
94 scoped_ptr
<SignalingConnector
> signaling_connector
,
95 scoped_ptr
<HeartbeatSender
> heartbeat_sender
);
97 void OnHostOfflineReasonAck(bool success
);
99 // Used to bind HeartbeatSender and SignalingConnector callbacks to |listener|
100 // in a way that allows "detaching" the |listener| when either |this| is
101 // destroyed or when SendHostOfflineReasonAndDelete method is called.
102 scoped_ptr
<base::WeakPtrFactory
<Listener
>> weak_factory_for_listener_
;
104 // By holding a reference to |network_task_runner_|, HostSignalingManager is
105 // extending the lifetime of the host process. This is needed for the case
106 // where an instance of HostProcess has already been destroyed, but we want
107 // to keep the process running while we try to establish a connection and send
108 // host-offline-reason.
109 scoped_refptr
<AutoThreadTaskRunner
> network_task_runner_
;
111 // |heartbeat_sender_| and |signaling_connector_| have to be destroyed before
112 // |signal_strategy_| because their destructors need to call
113 // signal_strategy_->RemoveListener(this)
114 scoped_ptr
<SignalStrategy
> signal_strategy_
;
115 scoped_ptr
<SignalingConnector
> signaling_connector_
;
116 scoped_ptr
<HeartbeatSender
> heartbeat_sender_
;
118 DISALLOW_COPY_AND_ASSIGN(HostSignalingManager
);
121 } // namespace remoting
123 #endif // REMOTING_HOST_HOST_SIGNALING_MANAGER_H_