Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / it2me / it2me_host.h
blob0b16085590738ba092da60f12d5e1ecbe612ab60
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"
17 namespace base {
18 class DictionaryValue;
21 namespace policy {
22 class PolicyService;
23 } // namespace policy
25 namespace remoting {
27 class ChromotingHost;
28 class ChromotingHostContext;
29 class DesktopEnvironmentFactory;
30 class HostEventLogger;
31 class HostNPScriptObject;
32 class HostStatusLogger;
33 class PolicyWatcher;
34 class RegisterSupportHostRequest;
35 class RsaKeyPair;
37 // These state values are duplicated in host_session.js. Remember to update
38 // both copies when making changes.
39 enum It2MeHostState {
40 kDisconnected,
41 kStarting,
42 kRequestedAccessCode,
43 kReceivedAccessCode,
44 kConnected,
45 kDisconnecting,
46 kError,
47 kInvalidDomainError
50 // Internal implementation of the plugin's It2Me host function.
51 class It2MeHost : public base::RefCountedThreadSafe<It2MeHost>,
52 public HostStatusObserver {
53 public:
54 class Observer {
55 public:
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,
61 const std::string& error_message) = 0;
64 It2MeHost(
65 scoped_ptr<ChromotingHostContext> context,
66 scoped_ptr<PolicyWatcher> policy_watcher,
67 scoped_ptr<It2MeConfirmationDialogFactory> confirmation_dialog_factory,
68 base::WeakPtr<It2MeHost::Observer> observer,
69 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
70 const std::string& directory_bot_jid);
72 // Methods called by the script object, from the plugin thread.
74 // Creates It2Me host structures and starts the host.
75 virtual void Connect();
77 // Disconnects the host, ready for tear-down.
78 // Also called internally, from the network thread.
79 virtual void Disconnect();
81 // TODO (weitaosu): Remove RequestNatPolicy from It2MeHost.
82 // Request a NAT policy notification.
83 virtual void RequestNatPolicy();
85 // remoting::HostStatusObserver implementation.
86 void OnAccessDenied(const std::string& jid) override;
87 void OnClientAuthenticated(const std::string& jid) override;
88 void OnClientDisconnected(const std::string& jid) override;
90 void SetStateForTesting(It2MeHostState state,
91 const std::string& error_message) {
92 SetState(state, error_message);
95 protected:
96 friend class base::RefCountedThreadSafe<It2MeHost>;
98 ~It2MeHost() override;
100 ChromotingHostContext* host_context() { return host_context_.get(); }
101 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
102 return task_runner_;
104 base::WeakPtr<It2MeHost::Observer> observer() { return observer_; }
106 private:
107 // Updates state of the host. Can be called only on the network thread.
108 void SetState(It2MeHostState state, const std::string& error_message);
110 // Returns true if the host is connected.
111 bool IsConnected() const;
113 // Presents a confirmation dialog to the user before starting the connection
114 // process.
115 void ShowConfirmationPrompt();
117 // Processes the result of the confirmation dialog.
118 void OnConfirmationResult(It2MeConfirmationDialog::Result result);
120 // Called by Connect() to check for policies and start connection process.
121 void ReadPolicyAndConnect();
123 // Called by ReadPolicyAndConnect once policies have been read.
124 void FinishConnect();
126 // Called when the support host registration completes.
127 void OnReceivedSupportID(const std::string& support_id,
128 const base::TimeDelta& lifetime,
129 const std::string& error_message);
131 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread()
132 // to shut down UI thread resources.
133 void ShutdownOnNetworkThread();
135 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on
136 // the UI thread.
137 void ShutdownOnUiThread();
139 // Called when initial policies are read, and when they change.
140 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies);
142 // Called when malformed policies are detected.
143 void OnPolicyError();
145 // Handlers for NAT traversal and host domain policies.
146 void UpdateNatPolicy(bool nat_traversal_enabled);
147 void UpdateHostDomainPolicy(const std::string& host_domain);
149 // Caller supplied fields.
150 scoped_ptr<ChromotingHostContext> host_context_;
151 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
152 base::WeakPtr<It2MeHost::Observer> observer_;
153 XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
154 std::string directory_bot_jid_;
156 It2MeHostState state_;
158 scoped_refptr<RsaKeyPair> host_key_pair_;
159 scoped_ptr<SignalStrategy> signal_strategy_;
160 scoped_ptr<RegisterSupportHostRequest> register_request_;
161 scoped_ptr<HostStatusLogger> host_status_logger_;
162 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_;
163 scoped_ptr<HostEventLogger> host_event_logger_;
165 scoped_ptr<ChromotingHost> host_;
166 int failed_login_attempts_;
168 scoped_ptr<PolicyWatcher> policy_watcher_;
169 scoped_ptr<It2MeConfirmationDialogFactory> confirmation_dialog_factory_;
170 scoped_ptr<It2MeConfirmationDialogProxy> confirmation_dialog_proxy_;
172 // Host the current nat traversal policy setting.
173 bool nat_traversal_enabled_;
175 // The host domain policy setting.
176 std::string required_host_domain_;
178 // Indicates whether or not a policy has ever been read. This is to ensure
179 // that on startup, we do not accidentally start a connection before we have
180 // queried our policy restrictions.
181 bool policy_received_;
183 // On startup, it is possible to have Connect() called before the policy read
184 // is completed. Rather than just failing, we thunk the connection call so
185 // it can be executed after at least one successful policy read. This
186 // variable contains the thunk if it is necessary.
187 base::Closure pending_connect_;
189 DISALLOW_COPY_AND_ASSIGN(It2MeHost);
192 // Having a factory interface makes it possible for the test to provide a mock
193 // implementation of the It2MeHost.
194 class It2MeHostFactory {
195 public:
196 It2MeHostFactory();
197 virtual ~It2MeHostFactory();
199 // |policy_service| is used for creating the policy watcher for new
200 // instances of It2MeHost on ChromeOS. The caller must ensure that
201 // |policy_service| is valid throughout the lifetime of the It2MeHostFactory
202 // and each created It2MeHost object. This is currently possible because
203 // |policy_service| is a global singleton available from the browser process.
204 virtual void set_policy_service(policy::PolicyService* policy_service);
206 virtual scoped_refptr<It2MeHost> CreateIt2MeHost(
207 scoped_ptr<ChromotingHostContext> context,
208 base::WeakPtr<It2MeHost::Observer> observer,
209 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
210 const std::string& directory_bot_jid);
212 private:
213 policy::PolicyService* policy_service_;
214 DISALLOW_COPY_AND_ASSIGN(It2MeHostFactory);
217 } // namespace remoting
219 #endif // REMOTING_HOST_IT2ME_IT2ME_HOST_H_