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 14 namespace remoting { 15 16 class RegisterSupportHostRequest; 17 class HostNPScriptObject; 18 class DesktopEnvironmentFactory; 19 class HostEventLogger; 20 class ChromotingHost; 21 class ChromotingHostContext; 22 23 namespace policy_hack { 24 25 class 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. 31 enum 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. 43 class 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 ChromotingHostContext* context, 59 scoped_refptr<base::SingleThreadTaskRunner> 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 virtual void Connect(); 68 69 // Disconnects the host, ready for tear-down. 70 // Also called internally, from the network thread. 71 virtual void Disconnect(); 72 73 // TODO (weitaosu): Remove RequestNatPolicy from It2MeHost. 74 // Request a NAT policy notification. 75 virtual void RequestNatPolicy(); 76 77 // remoting::HostStatusObserver implementation. 78 virtual void OnAccessDenied(const std::string& jid) OVERRIDE; 79 virtual void OnClientAuthenticated(const std::string& jid) OVERRIDE; 80 virtual void OnClientDisconnected(const std::string& jid) OVERRIDE; 81 82 void SetStateForTesting(It2MeHostState state) { SetState(state); } 83 84 protected: 85 friend class base::RefCountedThreadSafe<It2MeHost>; 86 87 virtual ~It2MeHost(); 88 89 ChromotingHostContext* host_context() { return host_context_; } 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner() { 91 return task_runner_; 92 } 93 base::WeakPtr<It2MeHost::Observer> observer() { return observer_; } 94 95 private: 96 // Updates state of the host. Can be called only on the network thread. 97 void SetState(It2MeHostState state); 98 99 // Returns true if the host is connected. 100 bool IsConnected() const; 101 102 // Called by Connect() to check for policies and start connection process. 103 void ReadPolicyAndConnect(); 104 105 // Called by ReadPolicyAndConnect once policies have been read. 106 void FinishConnect(); 107 108 // Called when the support host registration completes. 109 void OnReceivedSupportID(bool success, 110 const std::string& support_id, 111 const base::TimeDelta& lifetime); 112 113 // Shuts down |host_| on the network thread and posts ShutdownOnUiThread() 114 // to shut down UI thread resources. 115 void ShutdownOnNetworkThread(); 116 117 // Shuts down |desktop_environment_factory_| and |policy_watcher_| on 118 // the UI thread. 119 void ShutdownOnUiThread(); 120 121 // Called when initial policies are read, and when they change. 122 void OnPolicyUpdate(scoped_ptr<base::DictionaryValue> policies); 123 124 // Handlers for NAT traversal and host domain policies. 125 void UpdateNatPolicy(bool nat_traversal_enabled); 126 void UpdateHostDomainPolicy(const std::string& host_domain); 127 128 // Caller supplied fields. 129 130 // The creator of the It2MeHost object owns the the host context and is 131 // responsible for keeping it alive throughout the liftime of the host. 132 ChromotingHostContext* host_context_; 133 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 134 base::WeakPtr<It2MeHost::Observer> observer_; 135 XmppSignalStrategy::XmppServerConfig xmpp_server_config_; 136 std::string directory_bot_jid_; 137 138 It2MeHostState state_; 139 140 scoped_refptr<RsaKeyPair> host_key_pair_; 141 scoped_ptr<SignalStrategy> signal_strategy_; 142 scoped_ptr<RegisterSupportHostRequest> register_request_; 143 scoped_ptr<LogToServer> log_to_server_; 144 scoped_ptr<DesktopEnvironmentFactory> desktop_environment_factory_; 145 scoped_ptr<HostEventLogger> host_event_logger_; 146 147 scoped_ptr<ChromotingHost> host_; 148 int failed_login_attempts_; 149 150 scoped_ptr<policy_hack::PolicyWatcher> policy_watcher_; 151 152 // Host the current nat traversal policy setting. 153 bool nat_traversal_enabled_; 154 155 // The host domain policy setting. 156 std::string required_host_domain_; 157 158 // Indicates whether or not a policy has ever been read. This is to ensure 159 // that on startup, we do not accidentally start a connection before we have 160 // queried our policy restrictions. 161 bool policy_received_; 162 163 // On startup, it is possible to have Connect() called before the policy read 164 // is completed. Rather than just failing, we thunk the connection call so 165 // it can be executed after at least one successful policy read. This 166 // variable contains the thunk if it is necessary. 167 base::Closure pending_connect_; 168 169 DISALLOW_COPY_AND_ASSIGN(It2MeHost); 170 }; 171 172 // Having a factory interface makes it possible for the test to provide a mock 173 // implementation of the It2MeHost. 174 class It2MeHostFactory { 175 public: 176 It2MeHostFactory(); 177 virtual ~It2MeHostFactory(); 178 179 virtual scoped_refptr<It2MeHost> CreateIt2MeHost( 180 ChromotingHostContext* context, 181 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 182 base::WeakPtr<It2MeHost::Observer> observer, 183 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, 184 const std::string& directory_bot_jid); 185 186 private: 187 DISALLOW_COPY_AND_ASSIGN(It2MeHostFactory); 188 }; 189 190 } // namespace remoting 191 192 #endif // REMOTING_HOST_IT2ME_IT2ME_HOST_H_ 193