summaryrefslogtreecommitdiffstats
path: root/remoting/client/jni/chromoting_jni_instance.cc
blob: fcb4db332dbba13e7549317a654f05376da11889 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2013 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/client/jni/chromoting_jni_instance.h"

#include "base/bind.h"
#include "base/logging.h"
#include "remoting/client/audio_player.h"
#include "remoting/client/jni/chromoting_jni.h"
#include "remoting/protocol/libjingle_transport_factory.h"

// TODO(solb) Move into location shared with client plugin.
const char* const CHAT_SERVER = "talk.google.com";
const int CHAT_PORT = 5222;
const bool CHAT_USE_TLS = true;

namespace remoting {

ChromotingJniInstance::ChromotingJniInstance(const char* username,
                                             const char* auth_token,
                                             const char* host_jid,
                                             const char* host_id,
                                             const char* host_pubkey) {
  DCHECK(ChromotingJni::GetInstance()->
      ui_task_runner()->BelongsToCurrentThread());

  username_ = username;
  auth_token_ = auth_token;
  host_jid_ = host_jid;
  host_id_ = host_id;
  host_pubkey_ = host_pubkey;

  ChromotingJni::GetInstance()->display_task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread,
                 this));
}

ChromotingJniInstance::~ChromotingJniInstance() {}

void ChromotingJniInstance::Cleanup() {
  if (!ChromotingJni::GetInstance()->
      display_task_runner()->BelongsToCurrentThread()) {
    ChromotingJni::GetInstance()->display_task_runner()->PostTask(
        FROM_HERE,
        base::Bind(&ChromotingJniInstance::Cleanup, this));
    return;
  }

  // This must be destroyed on the display thread before the producer is gone.
  view_.reset();

  // The weak pointers must be invalidated on the same thread they were used.
  view_weak_factory_->InvalidateWeakPtrs();

  ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE,
      base::Bind(&ChromotingJniInstance::DisconnectFromHostOnNetworkThread,
                 this));
}

void ChromotingJniInstance::ProvideSecret(const char* pin) {
  DCHECK(ChromotingJni::GetInstance()->
      ui_task_runner()->BelongsToCurrentThread());
  DCHECK(!pin_callback_.is_null());

  // We invoke the string constructor to ensure |pin| gets copied *before* the
  // asynchronous run, since Java might want it back as soon as we return.
  ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE,
                                 base::Bind(pin_callback_, pin));
}

void ChromotingJniInstance::RedrawDesktop() {
  if (!ChromotingJni::GetInstance()->
      display_task_runner()->BelongsToCurrentThread()) {
    ChromotingJni::GetInstance()->display_task_runner()->PostTask(
        FROM_HERE,
        base::Bind(&ChromotingJniInstance::RedrawDesktop, this));
    return;
  }

  ChromotingJni::GetInstance()->RedrawCanvas();
}

void ChromotingJniInstance::PerformMouseAction(
    int x,
    int y,
    protocol::MouseEvent_MouseButton button,
    bool buttonDown) {
  if(!ChromotingJni::GetInstance()->
      network_task_runner()->BelongsToCurrentThread()) {
    ChromotingJni::GetInstance()->network_task_runner()->PostTask(
        FROM_HERE,
        base::Bind(&ChromotingJniInstance::PerformMouseAction,
                   this,
                   x,
                   y,
                   button,
                   buttonDown));
    return;
  }

  protocol::MouseEvent action;
  action.set_x(x);
  action.set_y(y);
  action.set_button(button);
  if (button != protocol::MouseEvent::BUTTON_UNDEFINED)
    action.set_button_down(buttonDown);

  connection_->input_stub()->InjectMouseEvent(action);
}

void ChromotingJniInstance::OnConnectionState(
    protocol::ConnectionToHost::State state,
    protocol::ErrorCode error) {
  if (!ChromotingJni::GetInstance()->
      ui_task_runner()->BelongsToCurrentThread()) {
    ChromotingJni::GetInstance()->
        ui_task_runner()->PostTask(
            FROM_HERE,
            base::Bind(&ChromotingJniInstance::OnConnectionState,
                       this,
                       state,
                       error));
    return;
  }

  ChromotingJni::GetInstance()->ReportConnectionStatus(state, error);
}

void ChromotingJniInstance::OnConnectionReady(bool ready) {
  // We ignore this message, since OnConnectionState() tells us the same thing.
}

void ChromotingJniInstance::SetCapabilities(const std::string& capabilities) {}

void ChromotingJniInstance::SetPairingResponse(
    const protocol::PairingResponse& response) {
  NOTIMPLEMENTED();
}

protocol::ClipboardStub* ChromotingJniInstance::GetClipboardStub() {
  return this;
}

protocol::CursorShapeStub* ChromotingJniInstance::GetCursorShapeStub() {
  return this;
}

scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>
    ChromotingJniInstance::GetTokenFetcher(const std::string& host_public_key) {
  // Return null to indicate that third-party authentication is unsupported.
  return scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher>();
}

void ChromotingJniInstance::InjectClipboardEvent(
    const protocol::ClipboardEvent& event) {
  NOTIMPLEMENTED();
}

void ChromotingJniInstance::SetCursorShape(
    const protocol::CursorShapeInfo& shape) {
  NOTIMPLEMENTED();
}

void ChromotingJniInstance::ConnectToHostOnDisplayThread() {
  DCHECK(ChromotingJni::GetInstance()->
      display_task_runner()->BelongsToCurrentThread());

  frame_consumer_ = new FrameConsumerProxy(
      ChromotingJni::GetInstance()->display_task_runner());
  view_.reset(new JniFrameConsumer());
  view_weak_factory_.reset(new base::WeakPtrFactory<JniFrameConsumer>(
      view_.get()));
  frame_consumer_->Attach(view_weak_factory_->GetWeakPtr());

  ChromotingJni::GetInstance()->network_task_runner()->PostTask(
      FROM_HERE,
      base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread,
                 this));
}

void ChromotingJniInstance::ConnectToHostOnNetworkThread() {
  DCHECK(ChromotingJni::GetInstance()->
      network_task_runner()->BelongsToCurrentThread());

  client_config_.reset(new ClientConfig());
  client_config_->host_jid = host_jid_;
  client_config_->host_public_key = host_pubkey_;

  client_config_->fetch_secret_callback = base::Bind(
      &ChromotingJniInstance::FetchSecret,
      this);
  client_config_->authentication_tag = host_id_;

  client_config_->authentication_methods.push_back(
      protocol::AuthenticationMethod::FromString("spake2_hmac"));
  client_config_->authentication_methods.push_back(
      protocol::AuthenticationMethod::FromString("spake2_plain"));

  client_context_.reset(new ClientContext(
      ChromotingJni::GetInstance()->network_task_runner().get()));
  client_context_->Start();

  connection_.reset(new protocol::ConnectionToHost(true));

  client_.reset(new ChromotingClient(*client_config_,
                                     client_context_.get(),
                                     connection_.get(),
                                     this,
                                     frame_consumer_,
                                     scoped_ptr<AudioPlayer>()));

  view_->set_frame_producer(client_->GetFrameProducer());

  signaling_config_.reset(new XmppSignalStrategy::XmppServerConfig());
  signaling_config_->host = CHAT_SERVER;
  signaling_config_->port = CHAT_PORT;
  signaling_config_->use_tls = CHAT_USE_TLS;

  signaling_.reset(new XmppSignalStrategy(
      ChromotingJni::GetInstance()->url_requester(),
      username_,
      auth_token_,
      "oauth2",
      *signaling_config_));

  network_settings_.reset(new NetworkSettings(
      NetworkSettings::NAT_TRAVERSAL_OUTGOING));
  scoped_ptr<protocol::TransportFactory> fact(
      protocol::LibjingleTransportFactory::Create(
          *network_settings_,
          ChromotingJni::GetInstance()->url_requester()));

  client_->Start(signaling_.get(), fact.Pass());
}

void ChromotingJniInstance::DisconnectFromHostOnNetworkThread() {
  DCHECK(ChromotingJni::GetInstance()->
      network_task_runner()->BelongsToCurrentThread());

  username_ = "";
  auth_token_ = "";
  host_jid_ = "";
  host_id_ = "";
  host_pubkey_ = "";

  // |client_| must be torn down before |signaling_|.
  connection_.reset();
  client_.reset();
}

void ChromotingJniInstance::FetchSecret(
    bool pairable,
    const protocol::SecretFetchedCallback& callback) {
  if (!ChromotingJni::GetInstance()->
      ui_task_runner()->BelongsToCurrentThread()) {
    ChromotingJni::GetInstance()->ui_task_runner()->PostTask(
        FROM_HERE,
        base::Bind(&ChromotingJniInstance::FetchSecret,
                   this,
                   pairable,
                   callback));
    return;
  }

  pin_callback_ = callback;
  ChromotingJni::GetInstance()->DisplayAuthenticationPrompt();
}

}  // namespace remoting