it2me_host.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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.
4
5#ifndef REMOTING_HOST_IT2ME_IT2ME_HOST_H_
6#define REMOTING_HOST_IT2ME_IT2ME_HOST_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/weak_ptr.h"
10#include "base/single_thread_task_runner.h"
11#include "remoting/host/log_to_server.h"
12#include "remoting/jingle_glue/xmpp_signal_strategy.h"
13
14namespace remoting {
15
16class RegisterSupportHostRequest;
17class HostNPScriptObject;
18class DesktopEnvironmentFactory;
19class HostEventLogger;
20class ChromotingHost;
21class ChromotingHostContext;
22
23namespace policy_hack {
24
25class PolicyWatcher;
26
27}  // namespace policy_hack
28
29// These state values are duplicated in host_session.js. Remember to update
30// both copies when making changes.
31enum It2MeHostState {
32  kDisconnected,
33  kStarting,
34  kRequestedAccessCode,
35  kReceivedAccessCode,
36  kConnected,
37  kDisconnecting,
38  kError,
39  kInvalidDomainError
40};
41
42// Internal implementation of the plugin's It2Me host function.
43class It2MeHost
44    : public base::RefCountedThreadSafe<It2MeHost>,
45      public HostStatusObserver {
46 public:
47
48  class Observer {
49   public:
50    virtual void OnClientAuthenticated(const std::string& client_username) = 0;
51    virtual void OnStoreAccessCode(const std::string& access_code,
52                                   base::TimeDelta access_code_lifetime) = 0;
53    virtual void OnNatPolicyChanged(bool nat_traversal_enabled) = 0;
54    virtual void OnStateChanged(It2MeHostState state) = 0;
55  };
56
57  It2MeHost(
58      scoped_ptr<ChromotingHostContext> context,
59      scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner,
60      base::WeakPtr<It2MeHost::Observer> observer,
61      const XmppSignalStrategy::XmppServerConfig& xmpp_server_config,
62      const std::string& directory_bot_jid);
63
64  // Methods called by the script object, from the plugin thread.
65
66  // Creates It2Me host structures and starts the host.
67  void Connect();
68
69  // Disconnects the host, ready for tear-down.
70  // Also called internally, from the network thread.
71  void Disconnect();
72
73  // Request a NAT policy notification.
74  void RequestNatPolicy();
75
76  // remoting::HostStatusObserver implementation.
77  virtual void OnAccessDenied(const std::string& jid) OVERRIDE;
78  virtual void OnClientAuthenticated(const std::string& jid) OVERRIDE;
79  virtual void OnClientDisconnected(const std::string& jid) OVERRIDE;
80
81 private:
82  friend class base::RefCountedThreadSafe<It2MeHost>;
83
84  virtual ~It2MeHost();
85
86  // Updates state of the host. Can be called only on the network thread.
87  void SetState(It2MeHostState state);
88
89  // Returns true if the host is connected.
90  bool IsConnected() const;
91
92  // Called by Connect() to check for policies and start connection process.
93  void ReadPolicyAndConnect();
94
95  // Called by ReadPolicyAndConnect once policies have been read.
96  void FinishConnect();
97
98  // Called when the support host registration completes.
99  void OnReceivedSupportID(bool success,
100                           const std::string& support_id,
101                           const base::TimeDelta& lifetime);
102
103  // Shuts down |host_| on the network thread and posts ShutdownOnUiThread()
104  // to shut down UI thread resources.
105  void ShutdownOnNetworkThread();
106
107  // Shuts down |desktop_environment_factory_| and |policy_watcher_| on
108  // the UI thread.
109  void ShutdownOnUiThread();
110
111  // Called when initial policies are read, and when they change.
112  void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies);
113
114  // Handlers for NAT traversal and host domain policies.
115  void UpdateNatPolicy(bool nat_traversal_enabled);
116  void UpdateHostDomainPolicy(const std::string& host_domain);
117
118  // Caller supplied fields.
119  scoped_ptr<ChromotingHostContext> host_context_;
120  scoped_refptr<base::SingleThreadTaskRunner> plugin_task_runner_;
121  base::WeakPtr<It2MeHost::Observer> observer_;
122  XmppSignalStrategy::XmppServerConfig xmpp_server_config_;
123  std::string directory_bot_jid_;
124
125  It2MeHostState state_;
126
127  scoped_refptr<RsaKeyPair> host_key_pair_;
128  scoped_ptr<SignalStrategy> signal_strategy_;
129  scoped_ptr<RegisterSupportHostRequest> register_request_;
130  scoped_ptr<LogToServer> log_to_server_;
131  scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_;
132  scoped_ptr<HostEventLogger> host_event_logger_;
133
134  scoped_ptr<ChromotingHost> host_;
135  int failed_login_attempts_;
136
137  scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_;
138
139  // Host the current nat traversal policy setting.
140  bool nat_traversal_enabled_;
141
142  // The host domain policy setting.
143  std::string required_host_domain_;
144
145  // Indicates whether or not a policy has ever been read. This is to ensure
146  // that on startup, we do not accidentally start a connection before we have
147  // queried our policy restrictions.
148  bool policy_received_;
149
150  // On startup, it is possible to have Connect() called before the policy read
151  // is completed.  Rather than just failing, we thunk the connection call so
152  // it can be executed after at least one successful policy read. This
153  // variable contains the thunk if it is necessary.
154  base::Closure pending_connect_;
155
156  DISALLOW_COPY_AND_ASSIGN(It2MeHost);
157};
158
159}  // namespace remoting
160
161#endif  // REMOTING_HOST_IT2ME_IT2ME_HOST_H_
162