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/host/it2me/it2me_confirmation_dialog.h"
14 #include "remoting/host/it2me/it2me_confirmation_dialog_proxy.h"
15 #include "remoting/signaling/xmpp_signal_strategy.h"
18 class DictionaryValue
;
28 class ChromotingHostContext
;
29 class DesktopEnvironmentFactory
;
30 class HostEventLogger
;
31 class HostNPScriptObject
;
32 class HostStatusLogger
;
34 class RegisterSupportHostRequest
;
37 // These state values are duplicated in host_session.js. Remember to update
38 // both copies when making changes.
50 // Internal implementation of the plugin's It2Me host function.
51 class It2MeHost
: public base::RefCountedThreadSafe
<It2MeHost
>,
52 public HostStatusObserver
{
56 virtual void OnClientAuthenticated(const std::string
& client_username
) = 0;
57 virtual void OnStoreAccessCode(const std::string
& access_code
,
58 base::TimeDelta access_code_lifetime
) = 0;
59 virtual void OnNatPolicyChanged(bool nat_traversal_enabled
) = 0;
60 virtual void OnStateChanged(It2MeHostState state
) = 0;
64 scoped_ptr
<ChromotingHostContext
> context
,
65 scoped_ptr
<PolicyWatcher
> policy_watcher
,
66 scoped_ptr
<It2MeConfirmationDialogFactory
> confirmation_dialog_factory
,
67 base::WeakPtr
<It2MeHost::Observer
> observer
,
68 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
69 const std::string
& directory_bot_jid
);
71 // Methods called by the script object, from the plugin thread.
73 // Creates It2Me host structures and starts the host.
74 virtual void Connect();
76 // Disconnects the host, ready for tear-down.
77 // Also called internally, from the network thread.
78 virtual void Disconnect();
80 // TODO (weitaosu): Remove RequestNatPolicy from It2MeHost.
81 // Request a NAT policy notification.
82 virtual void RequestNatPolicy();
84 // remoting::HostStatusObserver implementation.
85 void OnAccessDenied(const std::string
& jid
) override
;
86 void OnClientAuthenticated(const std::string
& jid
) override
;
87 void OnClientDisconnected(const std::string
& jid
) override
;
89 void SetStateForTesting(It2MeHostState state
) { SetState(state
); }
92 friend class base::RefCountedThreadSafe
<It2MeHost
>;
94 ~It2MeHost() override
;
96 ChromotingHostContext
* host_context() { return host_context_
.get(); }
97 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner() {
100 base::WeakPtr
<It2MeHost::Observer
> observer() { return observer_
; }
103 // Updates state of the host. Can be called only on the network thread.
104 void SetState(It2MeHostState state
);
106 // Returns true if the host is connected.
107 bool IsConnected() const;
109 // Presents a confirmation dialog to the user before starting the connection
111 void ShowConfirmationPrompt();
113 // Processes the result of the confirmation dialog.
114 void OnConfirmationResult(It2MeConfirmationDialog::Result result
);
116 // Called by Connect() to check for policies and start connection process.
117 void ReadPolicyAndConnect();
119 // Called by ReadPolicyAndConnect once policies have been read.
120 void FinishConnect();
122 // Called when the support host registration completes.
123 void OnReceivedSupportID(bool success
,
124 const std::string
& support_id
,
125 const base::TimeDelta
& lifetime
);
127 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread()
128 // to shut down UI thread resources.
129 void ShutdownOnNetworkThread();
131 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on
133 void ShutdownOnUiThread();
135 // Called when initial policies are read, and when they change.
136 void OnPolicyUpdate(scoped_ptr
<base::DictionaryValue
> policies
);
138 // Called when malformed policies are detected.
139 void OnPolicyError();
141 // Handlers for NAT traversal and host domain policies.
142 void UpdateNatPolicy(bool nat_traversal_enabled
);
143 void UpdateHostDomainPolicy(const std::string
& host_domain
);
145 // Caller supplied fields.
146 scoped_ptr
<ChromotingHostContext
> host_context_
;
147 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
148 base::WeakPtr
<It2MeHost::Observer
> observer_
;
149 XmppSignalStrategy::XmppServerConfig xmpp_server_config_
;
150 std::string directory_bot_jid_
;
152 It2MeHostState state_
;
154 scoped_refptr
<RsaKeyPair
> host_key_pair_
;
155 scoped_ptr
<SignalStrategy
> signal_strategy_
;
156 scoped_ptr
<RegisterSupportHostRequest
> register_request_
;
157 scoped_ptr
<HostStatusLogger
> host_status_logger_
;
158 scoped_ptr
<DesktopEnvironmentFactory
> desktop_environment_factory_
;
159 scoped_ptr
<HostEventLogger
> host_event_logger_
;
161 scoped_ptr
<ChromotingHost
> host_
;
162 int failed_login_attempts_
;
164 scoped_ptr
<PolicyWatcher
> policy_watcher_
;
165 scoped_ptr
<It2MeConfirmationDialogFactory
> confirmation_dialog_factory_
;
166 scoped_ptr
<It2MeConfirmationDialogProxy
> confirmation_dialog_proxy_
;
168 // Host the current nat traversal policy setting.
169 bool nat_traversal_enabled_
;
171 // The host domain policy setting.
172 std::string required_host_domain_
;
174 // Indicates whether or not a policy has ever been read. This is to ensure
175 // that on startup, we do not accidentally start a connection before we have
176 // queried our policy restrictions.
177 bool policy_received_
;
179 // On startup, it is possible to have Connect() called before the policy read
180 // is completed. Rather than just failing, we thunk the connection call so
181 // it can be executed after at least one successful policy read. This
182 // variable contains the thunk if it is necessary.
183 base::Closure pending_connect_
;
185 DISALLOW_COPY_AND_ASSIGN(It2MeHost
);
188 // Having a factory interface makes it possible for the test to provide a mock
189 // implementation of the It2MeHost.
190 class It2MeHostFactory
{
193 virtual ~It2MeHostFactory();
195 // |policy_service| is used for creating the policy watcher for new
196 // instances of It2MeHost on ChromeOS. The caller must ensure that
197 // |policy_service| is valid throughout the lifetime of the It2MeHostFactory
198 // and each created It2MeHost object. This is currently possible because
199 // |policy_service| is a global singleton available from the browser process.
200 virtual void set_policy_service(policy::PolicyService
* policy_service
);
202 virtual scoped_refptr
<It2MeHost
> CreateIt2MeHost(
203 scoped_ptr
<ChromotingHostContext
> context
,
204 base::WeakPtr
<It2MeHost::Observer
> observer
,
205 const XmppSignalStrategy::XmppServerConfig
& xmpp_server_config
,
206 const std::string
& directory_bot_jid
);
209 policy::PolicyService
* policy_service_
;
210 DISALLOW_COPY_AND_ASSIGN(It2MeHostFactory
);
213 } // namespace remoting
215 #endif // REMOTING_HOST_IT2ME_IT2ME_HOST_H_