<commit_before>// 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): Check the initialization sentinels.  Can we base it off of
// state_ instead of a member variable?  Also, 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/waitable_event.h"
#include "base/message_loop.h"
#include "chrome/common/net/notifier/communicator/gaia_token_pre_xmpp_auth.h"
#include "chrome/common/net/notifier/communicator/xmpp_socket_adapter.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/jingle_glue/relay_port_allocator.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_(START),
      callback_(NULL) {
}

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

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(callback_ == NULL);  // Init() can be called only once.

  callback_ = callback;

  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoInitialize,
                                   username, auth_token, auth_token_service));
}

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() {
  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoClose));
}

void JingleClient::DoClose() {
  DCHECK_EQ(message_loop(), MessageLoop::current());
  DCHECK(callback_ != NULL);  // Close() should only be called after Init().

  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);<commit_msg>Small fix to jingle_glue for chromoting<commit_after>// 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): Check the initialization sentinels.  Can we base it off of
// state_ instead of a member variable?  Also, 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/waitable_event.h"
#include "base/message_loop.h"
#include "chrome/common/net/notifier/communicator/gaia_token_pre_xmpp_auth.h"
#include "chrome/common/net/notifier/communicator/xmpp_socket_adapter.h"
#include "remoting/jingle_glue/jingle_thread.h"
#include "remoting/jingle_glue/relay_port_allocator.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_(START),
      callback_(NULL) {
}

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

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(callback_ == NULL);  // Init() can be called only once.

  callback_ = callback;

  message_loop()->PostTask(
      FROM_HERE, NewRunnableMethod(this, &JingleClient::DoInitialize,
                                   username, auth_token, auth_token_service));
}

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() {
  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 (!callback_ || state_ == CLOSED)
    return;

  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);
{'fraction_non_alphanumeric': 0.11767508337303478, 'fraction_numerical': 0.0014292520247737017, 'mean_word_length': 4.138922888616891, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6597367', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2667, 'n_words': 524}