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