[email protected] | 91e4b7f6 | 2012-01-25 23:23:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 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_CLIENT_SESSION_H_ |
| 6 | #define REMOTING_HOST_CLIENT_SESSION_H_ |
| 7 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 8 | #include <list> |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 9 | #include <set> |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 10 | |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 12 | #include "base/threading/non_thread_safe.h" |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 13 | #include "remoting/protocol/connection_to_client.h" |
| 14 | #include "remoting/protocol/host_stub.h" |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 15 | #include "remoting/protocol/input_stub.h" |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 16 | #include "third_party/skia/include/core/SkPoint.h" |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 17 | |
| 18 | namespace remoting { |
| 19 | |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 20 | class Capturer; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 21 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 22 | // A ClientSession keeps a reference to a connection to a client, and maintains |
| 23 | // per-client state. |
| 24 | class ClientSession : public protocol::HostStub, |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 25 | public protocol::InputStub, |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 26 | public protocol::ConnectionToClient::EventHandler, |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 27 | public base::NonThreadSafe { |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 28 | public: |
| 29 | // Callback interface for passing events to the ChromotingHost. |
| 30 | class EventHandler { |
| 31 | public: |
| 32 | virtual ~EventHandler() {} |
| 33 | |
[email protected] | 1f249e2 | 2011-11-29 20:19:59 | [diff] [blame] | 34 | // Called after authentication has finished successfully. |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 35 | virtual void OnSessionAuthenticated(ClientSession* client) = 0; |
[email protected] | 1f249e2 | 2011-11-29 20:19:59 | [diff] [blame] | 36 | |
| 37 | // Called after authentication has failed. Must not tear down this |
| 38 | // object. OnSessionClosed() is notified after this handler |
| 39 | // returns. |
| 40 | virtual void OnSessionAuthenticationFailed(ClientSession* client) = 0; |
| 41 | |
| 42 | // Called after connection has failed or after the client closed it. |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 43 | virtual void OnSessionClosed(ClientSession* client) = 0; |
[email protected] | 1f249e2 | 2011-11-29 20:19:59 | [diff] [blame] | 44 | |
| 45 | // Called to notify of each message's sequence number. The |
| 46 | // callback must not tear down this object. |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 47 | virtual void OnSessionSequenceNumber(ClientSession* client, |
| 48 | int64 sequence_number) = 0; |
[email protected] | 91e4b7f6 | 2012-01-25 23:23:02 | [diff] [blame] | 49 | |
| 50 | // Called on notification of a route change event, when a channel is |
| 51 | // connected. |
[email protected] | 17af2ab | 2012-02-02 04:07:52 | [diff] [blame] | 52 | virtual void OnSessionRouteChange( |
| 53 | ClientSession* client, |
| 54 | const std::string& channel_name, |
| 55 | const net::IPEndPoint& remote_end_point, |
| 56 | const net::IPEndPoint& local_end_point) = 0; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 57 | }; |
| 58 | |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 59 | // Takes ownership of |connection|. Does not take ownership of |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 60 | // |event_handler|, |input_stub| or |capturer|. |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 61 | ClientSession(EventHandler* event_handler, |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 62 | protocol::ConnectionToClient* connection, |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 63 | protocol::InputStub* input_stub, |
| 64 | Capturer* capturer); |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 65 | virtual ~ClientSession(); |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 66 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 67 | // protocol::InputStub interface. |
[email protected] | 6a6fe806 | 2011-11-19 00:06:02 | [diff] [blame] | 68 | virtual void InjectKeyEvent(const protocol::KeyEvent& event) OVERRIDE; |
| 69 | virtual void InjectMouseEvent(const protocol::MouseEvent& event) OVERRIDE; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 70 | |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 71 | // protocol::ConnectionToClient::EventHandler interface. |
| 72 | virtual void OnConnectionOpened( |
| 73 | protocol::ConnectionToClient* connection) OVERRIDE; |
| 74 | virtual void OnConnectionClosed( |
| 75 | protocol::ConnectionToClient* connection) OVERRIDE; |
[email protected] | 1f249e2 | 2011-11-29 20:19:59 | [diff] [blame] | 76 | virtual void OnConnectionFailed(protocol::ConnectionToClient* connection, |
[email protected] | 204a9e3 | 2012-03-02 05:42:58 | [diff] [blame^] | 77 | protocol::ErrorCode error) OVERRIDE; |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 78 | virtual void OnSequenceNumberUpdated( |
| 79 | protocol::ConnectionToClient* connection, int64 sequence_number) OVERRIDE; |
[email protected] | 17af2ab | 2012-02-02 04:07:52 | [diff] [blame] | 80 | virtual void OnRouteChange( |
| 81 | protocol::ConnectionToClient* connection, |
| 82 | const std::string& channel_name, |
| 83 | const net::IPEndPoint& remote_end_point, |
| 84 | const net::IPEndPoint& local_end_point) OVERRIDE; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 85 | |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 86 | // Disconnects the session and destroys the transport. Event handler |
| 87 | // is guaranteed not to be called after this method is called. Can |
| 88 | // be called multiple times. The object should not be used after |
| 89 | // this method returns. |
| 90 | void Disconnect(); |
[email protected] | 1ce457a | 2011-05-19 19:59:48 | [diff] [blame] | 91 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 92 | protocol::ConnectionToClient* connection() const { |
| 93 | return connection_.get(); |
| 94 | } |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 95 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 96 | bool authenticated() const { |
| 97 | return authenticated_; |
| 98 | } |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 99 | |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 100 | void set_awaiting_continue_approval(bool awaiting) { |
| 101 | awaiting_continue_approval_ = awaiting; |
| 102 | } |
| 103 | |
[email protected] | f19d9bd | 2011-09-13 05:21:11 | [diff] [blame] | 104 | const std::string& client_jid() { return client_jid_; } |
| 105 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 106 | // Indicate that local mouse activity has been detected. This causes remote |
| 107 | // inputs to be ignored for a short time so that the local user will always |
| 108 | // have the upper hand in 'pointer wars'. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 109 | void LocalMouseMoved(const SkIPoint& new_pos); |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 110 | |
[email protected] | b25ff3b | 2011-09-13 18:17:30 | [diff] [blame] | 111 | bool ShouldIgnoreRemoteMouseInput(const protocol::MouseEvent& event) const; |
| 112 | bool ShouldIgnoreRemoteKeyboardInput(const protocol::KeyEvent& event) const; |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 113 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 114 | private: |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 115 | friend class ClientSessionTest_RestoreEventState_Test; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 116 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 117 | // Keep track of input state so that we can clean up the event queue when |
| 118 | // the user disconnects. |
[email protected] | b25ff3b | 2011-09-13 18:17:30 | [diff] [blame] | 119 | void RecordKeyEvent(const protocol::KeyEvent& event); |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 120 | void RecordMouseButtonState(const protocol::MouseEvent& event); |
[email protected] | 86c5a1e | 2011-06-29 20:50:15 | [diff] [blame] | 121 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 122 | // Synthesize KeyUp and MouseUp events so that we can undo these events |
| 123 | // when the user disconnects. |
| 124 | void RestoreEventState(); |
[email protected] | 86c5a1e | 2011-06-29 20:50:15 | [diff] [blame] | 125 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 126 | EventHandler* event_handler_; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 127 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 128 | // The connection to the client. |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 129 | scoped_ptr<protocol::ConnectionToClient> connection_; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 130 | |
[email protected] | f19d9bd | 2011-09-13 05:21:11 | [diff] [blame] | 131 | std::string client_jid_; |
| 132 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 133 | // The input stub to which this object delegates. |
| 134 | protocol::InputStub* input_stub_; |
| 135 | |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 136 | // Capturer, used to determine current screen size for ensuring injected |
| 137 | // mouse events fall within the screen area. |
| 138 | // TODO(lambroslambrou): Move floor-control logic, and clamping to screen |
| 139 | // area, out of this class (crbug.com/96508). |
| 140 | Capturer* capturer_; |
| 141 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 142 | // Whether this client is authenticated. |
| 143 | bool authenticated_; |
| 144 | |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 145 | // Whether or not inputs from this client are blocked pending approval from |
| 146 | // the host user to continue the connection. |
| 147 | bool awaiting_continue_approval_; |
| 148 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 149 | // State to control remote input blocking while the local pointer is in use. |
| 150 | uint32 remote_mouse_button_state_; |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 151 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 152 | // Current location of the mouse pointer. This is used to provide appropriate |
| 153 | // coordinates when we release the mouse buttons after a user disconnects. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 154 | SkIPoint remote_mouse_pos_; |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 155 | |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 156 | // Queue of recently-injected mouse positions. This is used to detect whether |
| 157 | // mouse events from the local input monitor are echoes of injected positions, |
| 158 | // or genuine mouse movements of a local input device. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 159 | std::list<SkIPoint> injected_mouse_positions_; |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 160 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 161 | base::Time latest_local_input_time_; |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 162 | |
| 163 | // Set of keys that are currently pressed down by the user. This is used so |
| 164 | // we can release them if the user disconnects. |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 165 | std::set<int> pressed_keys_; |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 166 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 167 | DISALLOW_COPY_AND_ASSIGN(ClientSession); |
| 168 | }; |
| 169 | |
| 170 | } // namespace remoting |
| 171 | |
| 172 | #endif // REMOTING_HOST_CLIENT_SESSION_H_ |