WebKit Roll 97377:97400.
[chromium-blink-merge.git] / remoting / jingle_glue / javascript_signal_strategy.cc
blobbb423d63cca7eca7b7174669328767c91863662d
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"
7 #include <algorithm>
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"
15 namespace remoting {
17 JavascriptSignalStrategy::JavascriptSignalStrategy(const std::string& your_jid)
18 : your_jid_(your_jid),
19 listener_(NULL),
20 last_id_(0) {
23 JavascriptSignalStrategy::~JavascriptSignalStrategy() {
24 DCHECK(listener_ == NULL);
25 Close();
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
38 // already connected.
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());
51 if (xmpp_proxy_) {
52 xmpp_proxy_->DetachCallback();
53 xmpp_proxy_ = NULL;
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);
63 listener_ = listener;
66 void JavascriptSignalStrategy::SendStanza(buzz::XmlElement* stanza) {
67 DCHECK(CalledOnValidThread());
69 xmpp_proxy_->SendIq(stanza->Str());
70 delete stanza;
73 std::string JavascriptSignalStrategy::GetNextId() {
74 ++last_id_;
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));
87 if (!stanza.get()) {
88 LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str;
89 return;
92 if (listener_ && listener_->OnIncomingStanza(stanza.get()))
93 return;
95 iq_registry_.OnIncomingStanza(stanza.get());
98 } // namespace remoting