blob: 6703c52c28f3c776f9fe6f19f1741f4976c4b1c3 (
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
|
// 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/jingle_glue/javascript_signal_strategy.h"
#include <algorithm>
#include "base/logging.h"
#include "base/string_number_conversions.h"
#include "remoting/jingle_glue/xmpp_proxy.h"
#include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
namespace remoting {
JavascriptSignalStrategy::JavascriptSignalStrategy(const std::string& local_jid)
: local_jid_(local_jid),
last_id_(0) {
}
JavascriptSignalStrategy::~JavascriptSignalStrategy() {
DCHECK_EQ(listeners_.size(), 0U);
Disconnect();
}
void JavascriptSignalStrategy::AttachXmppProxy(
scoped_refptr<XmppProxy> xmpp_proxy) {
DCHECK(CalledOnValidThread());
xmpp_proxy_ = xmpp_proxy;
}
void JavascriptSignalStrategy::Connect() {
DCHECK(CalledOnValidThread());
xmpp_proxy_->AttachCallback(AsWeakPtr());
FOR_EACH_OBSERVER(Listener, listeners_,
OnSignalStrategyStateChange(CONNECTED));
}
void JavascriptSignalStrategy::Disconnect() {
DCHECK(CalledOnValidThread());
if (xmpp_proxy_)
xmpp_proxy_->DetachCallback();
FOR_EACH_OBSERVER(Listener, listeners_,
OnSignalStrategyStateChange(DISCONNECTED));
}
SignalStrategy::State JavascriptSignalStrategy::GetState() const {
DCHECK(CalledOnValidThread());
// TODO(sergeyu): Extend XmppProxy to provide status of the
// connection.
return CONNECTED;
}
SignalStrategy::Error JavascriptSignalStrategy::GetError() const {
DCHECK(CalledOnValidThread());
// TODO(sergeyu): Extend XmppProxy to provide status of the
// connection.
return OK;
}
std::string JavascriptSignalStrategy::GetLocalJid() const {
DCHECK(CalledOnValidThread());
return local_jid_;
}
void JavascriptSignalStrategy::AddListener(Listener* listener) {
DCHECK(CalledOnValidThread());
listeners_.AddObserver(listener);
}
void JavascriptSignalStrategy::RemoveListener(Listener* listener) {
DCHECK(CalledOnValidThread());
listeners_.RemoveObserver(listener);
}
bool JavascriptSignalStrategy::SendStanza(scoped_ptr<buzz::XmlElement> stanza) {
DCHECK(CalledOnValidThread());
xmpp_proxy_->SendIq(stanza->Str());
return true;
}
std::string JavascriptSignalStrategy::GetNextId() {
DCHECK(CalledOnValidThread());
++last_id_;
return base::IntToString(last_id_);
}
void JavascriptSignalStrategy::OnIq(const std::string& stanza_str) {
DCHECK(CalledOnValidThread());
scoped_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(stanza_str));
if (!stanza.get()) {
LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str;
return;
}
ObserverListBase<Listener>::Iterator it(listeners_);
Listener* listener;
while ((listener = it.GetNext()) != NULL) {
if (listener->OnSignalStrategyIncomingStanza(stanza.get()))
break;
}
}
} // namespace remoting
|