summaryrefslogtreecommitdiffstats
path: root/remoting/jingle_glue/jingle_client.cc
blob: b3ea334f1e5203e7cfd9247f7a19a271ee0679b5 (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
// Copyright (c) 2010 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.

// TODO(ajwong): We assign and read from a few of the member variables on
// two threads. We need to audit this for thread safety.

#include "remoting/jingle_glue/jingle_client.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "remoting/jingle_glue/gaia_token_pre_xmpp_auth.h"
#include "remoting/jingle_glue/iq_request.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/jingle_glue/relay_port_allocator.h"
#include "remoting/jingle_glue/xmpp_socket_adapter.h"
#include "third_party/libjingle/source/talk/base/asyncsocket.h"
#include "third_party/libjingle/source/talk/base/ssladapter.h"
#include "third_party/libjingle/source/talk/p2p/base/sessionmanager.h"
#include "third_party/libjingle/source/talk/p2p/client/sessionmanagertask.h"
#ifdef USE_SSL_TUNNEL
#include "third_party/libjingle/source/talk/session/tunnel/securetunnelsessionclient.h"
#endif
#include "third_party/libjingle/source/talk/session/tunnel/tunnelsessionclient.h"
#include "third_party/libjingle/source/talk/xmpp/prexmppauth.h"
#include "third_party/libjingle/source/talk/xmpp/saslcookiemechanism.h"

namespace remoting {

JingleClient::JingleClient(JingleThread* thread)
    : client_(NULL),
      thread_(thread),
      state_(CREATED),
      callback_(NULL) {
}

JingleClient::~JingleClient() {
  // JingleClient can be destroyed only after it's closed.
  DCHECK(state_ == CLOSED || state_ == CREATED);
}

void JingleClient::Init(
    const std::string& username, const std::string& auth_token,
    const std::string& auth_token_service, Callback* callback) {
  DCHECK_NE(username, "");
  DCHECK(callback != NULL);
  DCHECK(state_ == CREATED);

  callback_ = callback;
  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoInitialize,
                                   username, auth_token, auth_token_service));
  state_ = INITIALIZED;
}

JingleChannel* JingleClient::Connect(const std::string& host_jid,
                                     JingleChannel::Callback* callback) {
  // Ownership if channel is given to DoConnect.
  scoped_refptr<JingleChannel> channel = new JingleChannel(callback);
  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoConnect,
                                   channel, host_jid, callback));
  return channel;
}

void JingleClient::DoConnect(scoped_refptr<JingleChannel> channel,
                             const std::string& host_jid,
                             JingleChannel::Callback* callback) {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  talk_base::StreamInterface* stream =
      tunnel_session_client_->CreateTunnel(buzz::Jid(host_jid), "");
  DCHECK(stream != NULL);

  channel->Init(thread_, stream, host_jid);
}

void JingleClient::Close() {
  // Once we are closed we really shouldn't talk to the callback again. In the
  // case when JingleClient outlives the owner access the callback is not safe.
  // TODO(hclam): We need to lock to reset callback.
  callback_ = NULL;

  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoClose));
}

void JingleClient::DoClose() {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  // If we have not yet initialized and the client is already closed then
  // don't close again.
  if (state_ == CLOSED)
    return;

  if (client_) {
    client_->Disconnect();
    // Client is deleted by TaskRunner.
    client_ = NULL;
  }
  tunnel_session_client_.reset();
  port_allocator_.reset();
  session_manager_.reset();
  network_manager_.reset();
  UpdateState(CLOSED);
}

void JingleClient::DoInitialize(const std::string& username,
                                const std::string& auth_token,
                                const std::string& auth_token_service) {
  DCHECK_EQ(message_loop(), MessageLoop::current());

  buzz::Jid login_jid(username);

  buzz::XmppClientSettings settings;
  settings.set_user(login_jid.node());
  settings.set_host(login_jid.domain());
  settings.set_resource("chromoting");
  settings.set_use_tls(true);
  settings.set_token_service(auth_token_service);
  settings.set_auth_cookie(auth_token);
  settings.set_server(talk_base::SocketAddress("talk.google.com", 5222));

  client_ = new buzz::XmppClient(thread_->task_pump());
  client_->SignalStateChange.connect(
      this, &JingleClient::OnConnectionStateChanged);

  buzz::AsyncSocket* socket = new XmppSocketAdapter(settings, false);

  client_->Connect(settings, "", socket, CreatePreXmppAuth(settings));
  client_->Start();

  network_manager_.reset(new talk_base::NetworkManager());

  RelayPortAllocator* port_allocator =
      new RelayPortAllocator(network_manager_.get(), "transp2");
  port_allocator_.reset(port_allocator);
  port_allocator->SetJingleInfo(client_);

  session_manager_.reset(new cricket::SessionManager(port_allocator_.get()));
#ifdef USE_SSL_TUNNEL
  cricket::SecureTunnelSessionClient* session_client =
      new cricket::SecureTunnelSessionClient(client_->jid(),
                                             session_manager_.get());
  if (!session_client->GenerateIdentity())
    return false;
  tunnel_session_client_.reset(session_client);
#else  // !USE_SSL_TUNNEL
  tunnel_session_client_.reset(
      new cricket::TunnelSessionClient(client_->jid(),
                                       session_manager_.get()));
#endif  // USE_SSL_TUNNEL

  cricket::SessionManagerTask* receiver =
      new cricket::SessionManagerTask(client_, session_manager_.get());
  receiver->EnableOutgoingMessages();
  receiver->Start();

  tunnel_session_client_->SignalIncomingTunnel.connect(
      this, &JingleClient::OnIncomingTunnel);
}

std::string JingleClient::GetFullJid() {
  AutoLock auto_lock(full_jid_lock_);
  return full_jid_;
}

IqRequest* JingleClient::CreateIqRequest() {
  return new IqRequest(this);
}

MessageLoop* JingleClient::message_loop() {
  return thread_->message_loop();
}

void JingleClient::OnConnectionStateChanged(buzz::XmppEngine::State state) {
  switch (state) {
    case buzz::XmppEngine::STATE_START:
      UpdateState(INITIALIZED);
      break;
    case buzz::XmppEngine::STATE_OPENING:
      UpdateState(CONNECTING);
      break;
    case buzz::XmppEngine::STATE_OPEN:
      {
        AutoLock auto_lock(full_jid_lock_);
        full_jid_ = client_->jid().Str();
      }
      UpdateState(CONNECTED);
      break;
    case buzz::XmppEngine::STATE_CLOSED:
      UpdateState(CLOSED);
      break;
    default:
      NOTREACHED();
      break;
  }
}

void JingleClient::OnIncomingTunnel(
    cricket::TunnelSessionClient* client, buzz::Jid jid,
    std::string description, cricket::Session* session) {
  // Decline connection if we don't have callback.
  if (!callback_) {
    client->DeclineTunnel(session);
    return;
  }

  JingleChannel::Callback* channel_callback;
  if (callback_->OnAcceptConnection(this, jid.Str(), &channel_callback)) {
    DCHECK(channel_callback != NULL);
    talk_base::StreamInterface* stream = client->AcceptTunnel(session);
    scoped_refptr<JingleChannel> channel(new JingleChannel(channel_callback));
    channel->Init(thread_, stream, jid.Str());
    callback_->OnNewConnection(this, channel);
  } else {
    client->DeclineTunnel(session);
  }
}

void JingleClient::UpdateState(State new_state) {
  if (new_state != state_) {
    state_ = new_state;
    if (callback_)
      callback_->OnStateChange(this, new_state);
  }
}

buzz::PreXmppAuth* JingleClient::CreatePreXmppAuth(
    const buzz::XmppClientSettings& settings) {
  buzz::Jid jid(settings.user(), settings.host(), buzz::STR_EMPTY);
  return new GaiaTokenPreXmppAuth(jid.Str(), settings.auth_cookie(),
                                  settings.token_service());
}

}  // namespace remoting