| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "remoting/jingle_glue/iq_request.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "remoting/jingle_glue/signal_strategy.h" | |
| 10 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
| 11 #include "third_party/libjingle/source/talk/xmpp/constants.h" | |
| 12 | |
| 13 namespace remoting { | |
| 14 | |
| 15 IqRegistry::IqRegistry() { | |
| 16 } | |
| 17 | |
| 18 IqRegistry::~IqRegistry() { | |
| 19 } | |
| 20 | |
| 21 void IqRegistry::RemoveAllRequests(IqRequest* request) { | |
| 22 IqRequestMap::iterator it = requests_.begin(); | |
| 23 while (it != requests_.end()) { | |
| 24 IqRequestMap::iterator cur = it; | |
| 25 ++it; | |
| 26 if (cur->second == request) { | |
| 27 requests_.erase(cur); | |
| 28 } | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 bool IqRegistry::OnIncomingStanza(const buzz::XmlElement* stanza) { | |
| 33 // TODO(ajwong): Can we cleanup this dispatch at all? The send is from | |
| 34 // IqRequest but the return is in IqRegistry. | |
| 35 | |
| 36 if (stanza->Name() != buzz::QN_IQ) { | |
| 37 LOG(WARNING) << "Received unexpected non-IQ packet" << stanza->Str(); | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 if (!stanza->HasAttr(buzz::QN_ID)) { | |
| 42 LOG(WARNING) << "IQ packet missing id" << stanza->Str(); | |
| 43 return false; | |
| 44 } | |
| 45 | |
| 46 const std::string& id = stanza->Attr(buzz::QN_ID); | |
| 47 | |
| 48 IqRequestMap::iterator it = requests_.find(id); | |
| 49 if (it == requests_.end()) { | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 // TODO(ajwong): We should look at the logic inside libjingle's | |
| 54 // XmppTask::MatchResponseIq() and make sure we're fully in sync. | |
| 55 // They check more fields and conditions than us. | |
| 56 | |
| 57 // TODO(ajwong): This logic is weird. We add to the register in | |
| 58 // IqRequest::SendIq(), but remove in | |
| 59 // IqRegistry::OnIncomingStanza(). We should try to keep the | |
| 60 // registration/deregistration in one spot. | |
| 61 if (!it->second->callback_.is_null()) { | |
| 62 it->second->callback_.Run(stanza); | |
| 63 it->second->callback_.Reset(); | |
| 64 } else { | |
| 65 VLOG(1) << "No callback, so dropping: " << stanza->Str(); | |
| 66 } | |
| 67 requests_.erase(it); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 void IqRegistry::RegisterRequest(IqRequest* request, const std::string& id) { | |
| 72 DCHECK(requests_.find(id) == requests_.end()); | |
| 73 requests_[id] = request; | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 buzz::XmlElement* IqRequest::MakeIqStanza(const std::string& type, | |
| 78 const std::string& addressee, | |
| 79 buzz::XmlElement* iq_body) { | |
| 80 buzz::XmlElement* stanza = new buzz::XmlElement(buzz::QN_IQ); | |
| 81 stanza->AddAttr(buzz::QN_TYPE, type); | |
| 82 if (!addressee.empty()) | |
| 83 stanza->AddAttr(buzz::QN_TO, addressee); | |
| 84 stanza->AddElement(iq_body); | |
| 85 return stanza; | |
| 86 } | |
| 87 | |
| 88 IqRequest::IqRequest() | |
| 89 : signal_strategy_(NULL), | |
| 90 registry_(NULL) { | |
| 91 } | |
| 92 | |
| 93 IqRequest::IqRequest(SignalStrategy* signal_strategy, IqRegistry* registry) | |
| 94 : signal_strategy_(signal_strategy), | |
| 95 registry_(registry) { | |
| 96 } | |
| 97 | |
| 98 IqRequest::~IqRequest() { | |
| 99 if (registry_) | |
| 100 registry_->RemoveAllRequests(this); | |
| 101 } | |
| 102 | |
| 103 void IqRequest::SendIq(buzz::XmlElement* stanza) { | |
| 104 std::string id = signal_strategy_->GetNextId(); | |
| 105 stanza->AddAttr(buzz::QN_ID, id); | |
| 106 registry_->RegisterRequest(this, id); | |
| 107 signal_strategy_->SendStanza(stanza); | |
| 108 } | |
| 109 | |
| 110 void IqRequest::set_callback(const ReplyCallback& callback) { | |
| 111 callback_ = callback; | |
| 112 } | |
| 113 | |
| 114 } // namespace remoting | |
| OLD | NEW |