1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/jingle_glue/javascript_signal_strategy.h"
9 #include "base/logging.h"
10 #include "base/string_number_conversions.h"
11 #include "remoting/jingle_glue/iq_request.h"
12 #include "remoting/jingle_glue/xmpp_proxy.h"
13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
17 JavascriptSignalStrategy::JavascriptSignalStrategy(const std::string
& your_jid
)
18 : your_jid_(your_jid
),
23 JavascriptSignalStrategy::~JavascriptSignalStrategy() {
24 DCHECK(listener_
== NULL
);
28 void JavascriptSignalStrategy::AttachXmppProxy(
29 scoped_refptr
<XmppProxy
> xmpp_proxy
) {
30 xmpp_proxy_
= xmpp_proxy
;
31 xmpp_proxy_
->AttachCallback(AsWeakPtr());
34 void JavascriptSignalStrategy::Init(StatusObserver
* observer
) {
35 DCHECK(CalledOnValidThread());
37 // Blast through each state since for a JavascriptSignalStrategy, we're
40 // TODO(ajwong): Clarify the status API contract to see if we have to actually
41 // walk through each state.
42 observer
->OnStateChange(StatusObserver::START
);
43 observer
->OnStateChange(StatusObserver::CONNECTING
);
44 observer
->OnJidChange(your_jid_
);
45 observer
->OnStateChange(StatusObserver::CONNECTED
);
48 void JavascriptSignalStrategy::Close() {
49 DCHECK(CalledOnValidThread());
52 xmpp_proxy_
->DetachCallback();
57 void JavascriptSignalStrategy::SetListener(Listener
* listener
) {
58 DCHECK(CalledOnValidThread());
60 // Don't overwrite an listener without explicitly going
61 // through "NULL" first.
62 DCHECK(listener_
== NULL
|| listener
== NULL
);
66 void JavascriptSignalStrategy::SendStanza(buzz::XmlElement
* stanza
) {
67 DCHECK(CalledOnValidThread());
69 xmpp_proxy_
->SendIq(stanza
->Str());
73 std::string
JavascriptSignalStrategy::GetNextId() {
75 return base::IntToString(last_id_
);
78 IqRequest
* JavascriptSignalStrategy::CreateIqRequest() {
79 DCHECK(CalledOnValidThread());
81 return new JavascriptIqRequest(this, &iq_registry_
);
84 void JavascriptSignalStrategy::OnIq(const std::string
& stanza_str
) {
85 scoped_ptr
<buzz::XmlElement
> stanza(buzz::XmlElement::ForStr(stanza_str
));
88 LOG(WARNING
) << "Malformed XMPP stanza received: " << stanza_str
;
92 if (listener_
&& listener_
->OnIncomingStanza(stanza
.get()))
95 iq_registry_
.OnIncomingStanza(stanza
.get());
98 } // namespace remoting