This source file includes following definitions.
- CreateSocket
- delegate_
- OnStateChange
- OnInputLog
- OnOutputLog
- ClearClient
#include "jingle/notifier/base/xmpp_connection.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_piece.h"
#include "jingle/glue/chrome_async_socket.h"
#include "jingle/glue/task_pump.h"
#include "jingle/glue/xmpp_client_socket_factory.h"
#include "jingle/notifier/base/weak_xmpp_client.h"
#include "net/socket/client_socket_factory.h"
#include "net/ssl/ssl_config_service.h"
#include "net/url_request/url_request_context.h"
#include "talk/xmpp/xmppclientsettings.h"
namespace notifier {
XmppConnection::Delegate::~Delegate() {}
namespace {
buzz::AsyncSocket* CreateSocket(
const buzz::XmppClientSettings& xmpp_client_settings,
const scoped_refptr<net::URLRequestContextGetter>& request_context_getter) {
bool use_fake_ssl_client_socket =
(xmpp_client_settings.protocol() == cricket::PROTO_SSLTCP);
const net::SSLConfig ssl_config;
const size_t kReadBufSize = 64U * 1024U;
const size_t kWriteBufSize = 64U * 1024U;
jingle_glue::XmppClientSocketFactory* const client_socket_factory =
new jingle_glue::XmppClientSocketFactory(
net::ClientSocketFactory::GetDefaultFactory(),
ssl_config,
request_context_getter,
use_fake_ssl_client_socket);
return new jingle_glue::ChromeAsyncSocket(client_socket_factory,
kReadBufSize, kWriteBufSize);
}
}
XmppConnection::XmppConnection(
const buzz::XmppClientSettings& xmpp_client_settings,
const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
Delegate* delegate,
buzz::PreXmppAuth* pre_xmpp_auth)
: task_pump_(new jingle_glue::TaskPump()),
on_connect_called_(false),
delegate_(delegate) {
DCHECK(delegate_);
WeakXmppClient* weak_xmpp_client = new WeakXmppClient(task_pump_.get());
weak_xmpp_client->SignalStateChange.connect(
this, &XmppConnection::OnStateChange);
weak_xmpp_client->SignalLogInput.connect(
this, &XmppConnection::OnInputLog);
weak_xmpp_client->SignalLogOutput.connect(
this, &XmppConnection::OnOutputLog);
const char kLanguage[] = "en";
buzz::XmppReturnStatus connect_status =
weak_xmpp_client->Connect(xmpp_client_settings, kLanguage,
CreateSocket(xmpp_client_settings,
request_context_getter),
pre_xmpp_auth);
DCHECK_EQ(connect_status, buzz::XMPP_RETURN_OK);
weak_xmpp_client->Start();
weak_xmpp_client_ = weak_xmpp_client->AsWeakPtr();
}
XmppConnection::~XmppConnection() {
DCHECK(CalledOnValidThread());
ClearClient();
task_pump_->Stop();
base::MessageLoop* current_message_loop = base::MessageLoop::current();
CHECK(current_message_loop);
current_message_loop->DeleteSoon(FROM_HERE, task_pump_.release());
}
void XmppConnection::OnStateChange(buzz::XmppEngine::State state) {
DCHECK(CalledOnValidThread());
VLOG(1) << "XmppClient state changed to " << state;
if (!weak_xmpp_client_.get()) {
LOG(DFATAL) << "weak_xmpp_client_ unexpectedly NULL";
return;
}
if (!delegate_) {
LOG(DFATAL) << "delegate_ unexpectedly NULL";
return;
}
switch (state) {
case buzz::XmppEngine::STATE_OPEN:
if (on_connect_called_) {
LOG(DFATAL) << "State changed to STATE_OPEN more than once";
} else {
delegate_->OnConnect(weak_xmpp_client_);
on_connect_called_ = true;
}
break;
case buzz::XmppEngine::STATE_CLOSED: {
int subcode = 0;
buzz::XmppEngine::Error error =
weak_xmpp_client_->GetError(&subcode);
const buzz::XmlElement* stream_error =
weak_xmpp_client_->GetStreamError();
ClearClient();
Delegate* delegate = delegate_;
delegate_ = NULL;
delegate->OnError(error, subcode, stream_error);
break;
}
default:
break;
}
}
void XmppConnection::OnInputLog(const char* data, int len) {
DCHECK(CalledOnValidThread());
VLOG(2) << "XMPP Input: " << base::StringPiece(data, len);
}
void XmppConnection::OnOutputLog(const char* data, int len) {
DCHECK(CalledOnValidThread());
VLOG(2) << "XMPP Output: " << base::StringPiece(data, len);
}
void XmppConnection::ClearClient() {
if (weak_xmpp_client_.get()) {
weak_xmpp_client_->Invalidate();
DCHECK(!weak_xmpp_client_.get());
}
}
}