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 #ifndef REMOTING_HOST_IT2ME_IT2ME_HOST_H_
6 #define REMOTING_HOST_IT2ME_IT2ME_HOST_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "remoting/host/host_status_observer.h"
13 #include "remoting/signaling/xmpp_signal_strategy.h"
16 class DictionaryValue
;
22 class ChromotingHostContext
;
23 class DesktopEnvironmentFactory
;
24 class HostEventLogger
;
25 class HostNPScriptObject
;
26 class HostStatusLogger
;
27 class RegisterSupportHostRequest
;
30 namespace policy_hack
{
34 } // namespace policy_hack
36 // These state values are duplicated in host_session.js. Remember to update
37 // both copies when making changes.
49 // Internal implementation of the plugin's It2Me host function.
50 class It2MeHost
: public base::RefCountedThreadSafe
<It2MeHost
>,
51 public HostStatusObserver
{
55 virtual void OnClientAuthenticated(const std::string
& client_username
) = 0;
56 virtual void OnStoreAccessCode(const std::string
& access_code
,
57 base::TimeDelta access_code_lifetime
) = 0;
58 virtual void OnNatPolicyChanged(bool nat_traversal_enabled
) = 0;
59 virtual void OnStateChanged(It2MeHostState state
) = 0;
63 ChromotingHostContext
* context
,
64 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
65 base::WeakPtr
<It2MeHost::Observer
> observer
,
66 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
67 const std::string
& directory_bot_jid
);
69 // Methods called by the script object, from the plugin thread.
71 // Creates It2Me host structures and starts the host.
72 virtual void Connect();
74 // Disconnects the host, ready for tear-down.
75 // Also called internally, from the network thread.
76 virtual void Disconnect();
78 // TODO (weitaosu): Remove RequestNatPolicy from It2MeHost.
79 // Request a NAT policy notification.
80 virtual void RequestNatPolicy();
82 // remoting::HostStatusObserver implementation.
83 virtual void OnAccessDenied(const std::string
& jid
) OVERRIDE
;
84 virtual void OnClientAuthenticated(const std::string
& jid
) OVERRIDE
;
85 virtual void OnClientDisconnected(const std::string
& jid
) OVERRIDE
;
87 void SetStateForTesting(It2MeHostState state
) { SetState(state
); }
90 friend class base::RefCountedThreadSafe
<It2MeHost
>;
94 ChromotingHostContext
* host_context() { return host_context_
; }
95 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() {
98 base::WeakPtr
<It2MeHost::Observer
> observer() { return observer_
; }
101 // Updates state of the host. Can be called only on the network thread.
102 void SetState(It2MeHostState state
);
104 // Returns true if the host is connected.
105 bool IsConnected() const;
107 // Called by Connect() to check for policies and start connection process.
108 void ReadPolicyAndConnect();
110 // Called by ReadPolicyAndConnect once policies have been read.
111 void FinishConnect();
113 // Called when the support host registration completes.
114 void OnReceivedSupportID(bool success
,
115 const std::string
& support_id
,
116 const base::TimeDelta
& lifetime
);
118 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread()
119 // to shut down UI thread resources.
120 void ShutdownOnNetworkThread();
122 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on
124 void ShutdownOnUiThread();
126 // Called when initial policies are read, and when they change.
127 void OnPolicyUpdate(scoped_ptr
<base::DictionaryValue
> policies
);
129 // Handlers for NAT traversal and host domain policies.
130 void UpdateNatPolicy(bool nat_traversal_enabled
);
131 void UpdateHostDomainPolicy(const std::string
& host_domain
);
133 // Caller supplied fields.
135 // The creator of the It2MeHost object owns the the host context and is
136 // responsible for keeping it alive throughout the liftime of the host.
137 ChromotingHostContext
* host_context_
;
138 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
139 base::WeakPtr
<It2MeHost::Observer
> observer_
;
140 XmppSignalStrategy::XmppServerConfig xmpp_server_config_
;
141 std::string directory_bot_jid_
;
143 It2MeHostState state_
;
145 scoped_refptr
<RsaKeyPair
> host_key_pair_
;
146 scoped_ptr
<SignalStrategy
> signal_strategy_
;
147 scoped_ptr
<RegisterSupportHostRequest
> register_request_
;
148 scoped_ptr
<HostStatusLogger
> host_status_logger_
;
149 scoped_ptr
<DesktopEnvironmentFactory
> desktop_environment_factory_
;
150 scoped_ptr
<HostEventLogger
> host_event_logger_
;
152 scoped_ptr
<ChromotingHost
> host_
;
153 int failed_login_attempts_
;
155 scoped_ptr
<policy_hack::PolicyWatcher
> policy_watcher_
;
157 // Host the current nat traversal policy setting.
158 bool nat_traversal_enabled_
;
160 // The host domain policy setting.
161 std::string required_host_domain_
;
163 // Indicates whether or not a policy has ever been read. This is to ensure
164 // that on startup, we do not accidentally start a connection before we have
165 // queried our policy restrictions.
166 bool policy_received_
;
168 // On startup, it is possible to have Connect() called before the policy read
169 // is completed. Rather than just failing, we thunk the connection call so
170 // it can be executed after at least one successful policy read. This
171 // variable contains the thunk if it is necessary.
172 base::Closure pending_connect_
;
174 DISALLOW_COPY_AND_ASSIGN(It2MeHost
);
177 // Having a factory interface makes it possible for the test to provide a mock
178 // implementation of the It2MeHost.
179 class It2MeHostFactory
{
182 virtual ~It2MeHostFactory();
184 virtual scoped_refptr
<It2MeHost
> CreateIt2MeHost(
185 ChromotingHostContext
* context
,
186 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
187 base::WeakPtr
<It2MeHost::Observer
> observer
,
188 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
189 const std::string
& directory_bot_jid
);
192 DISALLOW_COPY_AND_ASSIGN(It2MeHostFactory
);
195 } // namespace remoting
197 #endif // REMOTING_HOST_IT2ME_IT2ME_HOST_H_