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