summaryrefslogtreecommitdiffstats
path: root/remoting/host/client_session.cc
blob: dd32400798d757cad8f30a603d8aed2e94c0feb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/client_session.h"

#include <algorithm>

#include "base/message_loop_proxy.h"
#include "remoting/host/capturer.h"
#include "remoting/proto/control.pb.h"
#include "remoting/proto/event.pb.h"
#include "remoting/protocol/client_stub.h"
#include "remoting/protocol/clipboard_thread_proxy.h"

namespace remoting {

ClientSession::ClientSession(
    EventHandler* event_handler,
    scoped_ptr<protocol::ConnectionToClient> connection,
    protocol::HostEventStub* host_event_stub,
    Capturer* capturer)
    : event_handler_(event_handler),
      connection_(connection.Pass()),
      client_jid_(connection_->session()->jid()),
      is_authenticated_(false),
      host_event_stub_(host_event_stub),
      input_tracker_(host_event_stub_),
      remote_input_filter_(&input_tracker_),
      mouse_input_filter_(&remote_input_filter_),
      client_clipboard_factory_(clipboard_echo_filter_.client_filter()),
      capturer_(capturer) {
  connection_->SetEventHandler(this);

  // TODO(sergeyu): Currently ConnectionToClient expects stubs to be
  // set before channels are connected. Make it possible to set stubs
  // later and set them only when connection is authenticated.
  connection_->set_clipboard_stub(this);
  connection_->set_host_stub(this);
  connection_->set_input_stub(this);
  clipboard_echo_filter_.set_host_stub(host_event_stub_);
}

ClientSession::~ClientSession() {
}

void ClientSession::InjectClipboardEvent(
    const protocol::ClipboardEvent& event) {
  DCHECK(CalledOnValidThread());

  // TODO(wez): Disable clipboard in both directions on local activity, and
  // replace these tests with a HostInputFilter (or ClipboardFilter).
  if (auth_input_filter_.input_stub() == NULL)
    return;
  if (disable_input_filter_.input_stub() == NULL)
    return;

  clipboard_echo_filter_.host_filter()->InjectClipboardEvent(event);
}

void ClientSession::InjectKeyEvent(const protocol::KeyEvent& event) {
  DCHECK(CalledOnValidThread());
  auth_input_filter_.InjectKeyEvent(event);
}

void ClientSession::InjectMouseEvent(const protocol::MouseEvent& event) {
  DCHECK(CalledOnValidThread());

  // Ensure that the MouseInputFilter is clamping to the current dimensions.
  mouse_input_filter_.set_output_size(capturer_->size_most_recent());
  mouse_input_filter_.set_input_size(capturer_->size_most_recent());

  auth_input_filter_.InjectMouseEvent(event);
}

void ClientSession::NotifyClientDimensions(
    const protocol::ClientDimensions& dimensions) {
  // TODO(wez): Use the dimensions, e.g. to resize the host desktop to match.
  if (dimensions.has_width() && dimensions.has_height()) {
    VLOG(1) << "Received ClientDimensions (width="
            << dimensions.width() << ", height=" << dimensions.height() << ")";
  }
}

void ClientSession::ControlVideo(const protocol::VideoControl& video_control) {
  // TODO(wez): Pause/resume video updates, being careful not to let clients
  // override any host-initiated pause of the video channel.
  if (video_control.has_enable()) {
    VLOG(1) << "Received VideoControl (enable="
            << video_control.enable() << ")";
  }
}

void ClientSession::OnConnectionAuthenticated(
    protocol::ConnectionToClient* connection) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(connection_.get(), connection);
  is_authenticated_ = true;
  auth_input_filter_.set_input_stub(&disable_input_filter_);
  clipboard_echo_filter_.set_client_stub(connection_->client_stub());
  event_handler_->OnSessionAuthenticated(this);
}

void ClientSession::OnConnectionChannelsConnected(
    protocol::ConnectionToClient* connection) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(connection_.get(), connection);
  SetDisableInputs(false);
  event_handler_->OnSessionChannelsConnected(this);
}

void ClientSession::OnConnectionClosed(
    protocol::ConnectionToClient* connection,
    protocol::ErrorCode error) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(connection_.get(), connection);
  if (!is_authenticated_)
    event_handler_->OnSessionAuthenticationFailed(this);
  auth_input_filter_.set_input_stub(NULL);

  // Ensure that any pressed keys or buttons are released.
  input_tracker_.ReleaseAll();

  // TODO(sergeyu): Log failure reason?
  event_handler_->OnSessionClosed(this);
}

void ClientSession::OnSequenceNumberUpdated(
    protocol::ConnectionToClient* connection, int64 sequence_number) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(connection_.get(), connection);
  event_handler_->OnSessionSequenceNumber(this, sequence_number);
}

void ClientSession::OnRouteChange(
    protocol::ConnectionToClient* connection,
    const std::string& channel_name,
    const protocol::TransportRoute& route) {
  DCHECK(CalledOnValidThread());
  DCHECK_EQ(connection_.get(), connection);
  event_handler_->OnSessionRouteChange(this, channel_name, route);
}

void ClientSession::Disconnect() {
  DCHECK(CalledOnValidThread());
  DCHECK(connection_.get());

  // This triggers OnConnectionClosed(), and the session may be destroyed
  // as the result, so this call must be the last in this method.
  connection_->Disconnect();
}

void ClientSession::LocalMouseMoved(const SkIPoint& mouse_pos) {
  DCHECK(CalledOnValidThread());
  remote_input_filter_.LocalMouseMoved(mouse_pos);
}

void ClientSession::SetDisableInputs(bool disable_inputs) {
  DCHECK(CalledOnValidThread());

  if (disable_inputs) {
    disable_input_filter_.set_input_stub(NULL);
    input_tracker_.ReleaseAll();
  } else {
    disable_input_filter_.set_input_stub(&mouse_input_filter_);
  }
}

scoped_ptr<protocol::ClipboardStub> ClientSession::CreateClipboardProxy() {
  DCHECK(CalledOnValidThread());

  return scoped_ptr<protocol::ClipboardStub>(
      new protocol::ClipboardThreadProxy(
          client_clipboard_factory_.GetWeakPtr(),
          base::MessageLoopProxy::current()));
}

}  // namespace remoting