[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 1 | // Copyright (c) 2011 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_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] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 34 | virtual void OnSessionAuthenticated(ClientSession* client) = 0; |
| 35 | virtual void OnSessionClosed(ClientSession* client) = 0; |
| 36 | virtual void OnSessionSequenceNumber(ClientSession* client, |
| 37 | int64 sequence_number) = 0; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 38 | }; |
| 39 | |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 40 | // Takes ownership of |connection|. Does not take ownership of |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 41 | // |event_handler|, |input_stub| or |capturer|. |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 42 | ClientSession(EventHandler* event_handler, |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 43 | protocol::ConnectionToClient* connection, |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 44 | protocol::InputStub* input_stub, |
| 45 | Capturer* capturer); |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 46 | virtual ~ClientSession(); |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 47 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 48 | // protocol::InputStub interface. |
[email protected] | 6a6fe806 | 2011-11-19 00:06:02 | [diff] [blame^] | 49 | virtual void InjectKeyEvent(const protocol::KeyEvent& event) OVERRIDE; |
| 50 | virtual void InjectMouseEvent(const protocol::MouseEvent& event) OVERRIDE; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 51 | |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 52 | // protocol::ConnectionToClient::EventHandler interface. |
| 53 | virtual void OnConnectionOpened( |
| 54 | protocol::ConnectionToClient* connection) OVERRIDE; |
| 55 | virtual void OnConnectionClosed( |
| 56 | protocol::ConnectionToClient* connection) OVERRIDE; |
| 57 | virtual void OnConnectionFailed( |
| 58 | protocol::ConnectionToClient* connection) OVERRIDE; |
| 59 | virtual void OnSequenceNumberUpdated( |
| 60 | protocol::ConnectionToClient* connection, int64 sequence_number) OVERRIDE; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 61 | |
[email protected] | ee910fd | 2011-11-10 18:23:31 | [diff] [blame] | 62 | // Disconnects the session and destroys the transport. Event handler |
| 63 | // is guaranteed not to be called after this method is called. Can |
| 64 | // be called multiple times. The object should not be used after |
| 65 | // this method returns. |
| 66 | void Disconnect(); |
[email protected] | 1ce457a | 2011-05-19 19:59:48 | [diff] [blame] | 67 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 68 | protocol::ConnectionToClient* connection() const { |
| 69 | return connection_.get(); |
| 70 | } |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 71 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 72 | bool authenticated() const { |
| 73 | return authenticated_; |
| 74 | } |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 75 | |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 76 | void set_awaiting_continue_approval(bool awaiting) { |
| 77 | awaiting_continue_approval_ = awaiting; |
| 78 | } |
| 79 | |
[email protected] | f19d9bd | 2011-09-13 05:21:11 | [diff] [blame] | 80 | const std::string& client_jid() { return client_jid_; } |
| 81 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 82 | // Indicate that local mouse activity has been detected. This causes remote |
| 83 | // inputs to be ignored for a short time so that the local user will always |
| 84 | // have the upper hand in 'pointer wars'. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 85 | void LocalMouseMoved(const SkIPoint& new_pos); |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 86 | |
[email protected] | b25ff3b | 2011-09-13 18:17:30 | [diff] [blame] | 87 | bool ShouldIgnoreRemoteMouseInput(const protocol::MouseEvent& event) const; |
| 88 | bool ShouldIgnoreRemoteKeyboardInput(const protocol::KeyEvent& event) const; |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 89 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 90 | private: |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 91 | friend class ClientSessionTest_RestoreEventState_Test; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 92 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 93 | // Keep track of input state so that we can clean up the event queue when |
| 94 | // the user disconnects. |
[email protected] | b25ff3b | 2011-09-13 18:17:30 | [diff] [blame] | 95 | void RecordKeyEvent(const protocol::KeyEvent& event); |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 96 | void RecordMouseButtonState(const protocol::MouseEvent& event); |
[email protected] | 86c5a1e | 2011-06-29 20:50:15 | [diff] [blame] | 97 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 98 | // Synthesize KeyUp and MouseUp events so that we can undo these events |
| 99 | // when the user disconnects. |
| 100 | void RestoreEventState(); |
[email protected] | 86c5a1e | 2011-06-29 20:50:15 | [diff] [blame] | 101 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 102 | EventHandler* event_handler_; |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 103 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 104 | // The connection to the client. |
[email protected] | ec641187 | 2011-11-11 03:28:55 | [diff] [blame] | 105 | scoped_ptr<protocol::ConnectionToClient> connection_; |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 106 | |
[email protected] | f19d9bd | 2011-09-13 05:21:11 | [diff] [blame] | 107 | std::string client_jid_; |
| 108 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 109 | // The input stub to which this object delegates. |
| 110 | protocol::InputStub* input_stub_; |
| 111 | |
[email protected] | 995c2c6d | 2011-09-15 05:08:25 | [diff] [blame] | 112 | // Capturer, used to determine current screen size for ensuring injected |
| 113 | // mouse events fall within the screen area. |
| 114 | // TODO(lambroslambrou): Move floor-control logic, and clamping to screen |
| 115 | // area, out of this class (crbug.com/96508). |
| 116 | Capturer* capturer_; |
| 117 | |
[email protected] | 4ea2c7c | 2011-03-31 14:20:06 | [diff] [blame] | 118 | // Whether this client is authenticated. |
| 119 | bool authenticated_; |
| 120 | |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 121 | // Whether or not inputs from this client are blocked pending approval from |
| 122 | // the host user to continue the connection. |
| 123 | bool awaiting_continue_approval_; |
| 124 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 125 | // State to control remote input blocking while the local pointer is in use. |
| 126 | uint32 remote_mouse_button_state_; |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 127 | |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 128 | // Current location of the mouse pointer. This is used to provide appropriate |
| 129 | // coordinates when we release the mouse buttons after a user disconnects. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 130 | SkIPoint remote_mouse_pos_; |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 131 | |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 132 | // Queue of recently-injected mouse positions. This is used to detect whether |
| 133 | // mouse events from the local input monitor are echoes of injected positions, |
| 134 | // or genuine mouse movements of a local input device. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 135 | std::list<SkIPoint> injected_mouse_positions_; |
[email protected] | 4fe827a | 2011-08-10 03:30:19 | [diff] [blame] | 136 | |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 137 | base::Time latest_local_input_time_; |
[email protected] | b67fb930 | 2011-09-26 01:55:52 | [diff] [blame] | 138 | |
| 139 | // Set of keys that are currently pressed down by the user. This is used so |
| 140 | // we can release them if the user disconnects. |
[email protected] | 35c14ee | 2011-06-20 19:32:45 | [diff] [blame] | 141 | std::set<int> pressed_keys_; |
[email protected] | c78669c9 | 2011-06-13 22:42:38 | [diff] [blame] | 142 | |
[email protected] | 44f6076 | 2011-03-23 12:13:35 | [diff] [blame] | 143 | DISALLOW_COPY_AND_ASSIGN(ClientSession); |
| 144 | }; |
| 145 | |
| 146 | } // namespace remoting |
| 147 | |
| 148 | #endif // REMOTING_HOST_CLIENT_SESSION_H_ |