iq_sender.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2014 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. #include "remoting/signaling/iq_sender.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/location.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_number_conversions.h"
  11. #include "base/task/single_thread_task_runner.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "base/time/time.h"
  14. #include "remoting/signaling/signal_strategy.h"
  15. #include "remoting/signaling/signaling_id_util.h"
  16. #include "remoting/signaling/xmpp_constants.h"
  17. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  18. namespace remoting {
  19. // static
  20. std::unique_ptr<jingle_xmpp::XmlElement> IqSender::MakeIqStanza(
  21. const std::string& type,
  22. const std::string& addressee,
  23. std::unique_ptr<jingle_xmpp::XmlElement> iq_body) {
  24. std::unique_ptr<jingle_xmpp::XmlElement> stanza(
  25. new jingle_xmpp::XmlElement(kQNameIq));
  26. stanza->AddAttr(kQNameType, type);
  27. if (!addressee.empty())
  28. stanza->AddAttr(kQNameTo, addressee);
  29. stanza->AddElement(iq_body.release());
  30. return stanza;
  31. }
  32. IqSender::IqSender(SignalStrategy* signal_strategy)
  33. : signal_strategy_(signal_strategy) {
  34. signal_strategy_->AddListener(this);
  35. }
  36. IqSender::~IqSender() {
  37. signal_strategy_->RemoveListener(this);
  38. }
  39. std::unique_ptr<IqRequest> IqSender::SendIq(
  40. std::unique_ptr<jingle_xmpp::XmlElement> stanza,
  41. ReplyCallback callback) {
  42. std::string addressee = stanza->Attr(kQNameTo);
  43. std::string id = stanza->Attr(kQNameId);
  44. if (id.empty()) {
  45. id = signal_strategy_->GetNextId();
  46. stanza->AddAttr(kQNameId, id);
  47. }
  48. if (!signal_strategy_->SendStanza(std::move(stanza))) {
  49. return nullptr;
  50. }
  51. DCHECK(requests_.find(id) == requests_.end());
  52. bool callback_exists = !callback.is_null();
  53. auto request =
  54. std::make_unique<IqRequest>(this, std::move(callback), addressee);
  55. if (callback_exists)
  56. requests_[id] = request.get();
  57. return request;
  58. }
  59. std::unique_ptr<IqRequest> IqSender::SendIq(
  60. const std::string& type,
  61. const std::string& addressee,
  62. std::unique_ptr<jingle_xmpp::XmlElement> iq_body,
  63. ReplyCallback callback) {
  64. return SendIq(MakeIqStanza(type, addressee, std::move(iq_body)),
  65. std::move(callback));
  66. }
  67. void IqSender::RemoveRequest(IqRequest* request) {
  68. auto it = requests_.begin();
  69. while (it != requests_.end()) {
  70. auto cur = it;
  71. ++it;
  72. if (cur->second == request) {
  73. requests_.erase(cur);
  74. break;
  75. }
  76. }
  77. }
  78. void IqSender::OnSignalStrategyStateChange(SignalStrategy::State state) {
  79. }
  80. bool IqSender::OnSignalStrategyIncomingStanza(const jingle_xmpp::XmlElement* stanza) {
  81. if (stanza->Name() != kQNameIq) {
  82. LOG(WARNING) << "Received unexpected non-IQ packet " << stanza->Str();
  83. return false;
  84. }
  85. const std::string& type = stanza->Attr(kQNameType);
  86. if (type.empty()) {
  87. LOG(WARNING) << "IQ packet missing type " << stanza->Str();
  88. return false;
  89. }
  90. if (type != "result" && type != "error") {
  91. return false;
  92. }
  93. const std::string& id = stanza->Attr(kQNameId);
  94. if (id.empty()) {
  95. LOG(WARNING) << "IQ packet missing id " << stanza->Str();
  96. return false;
  97. }
  98. std::string from = stanza->Attr(kQNameFrom);
  99. auto it = requests_.find(id);
  100. if (it == requests_.end()) {
  101. return false;
  102. }
  103. IqRequest* request = it->second;
  104. if (NormalizeSignalingId(request->addressee_) != NormalizeSignalingId(from)) {
  105. LOG(ERROR) << "Received IQ response from an invalid JID. Ignoring it."
  106. << " Message received from: " << from
  107. << " Original JID: " << request->addressee_;
  108. return false;
  109. }
  110. requests_.erase(it);
  111. request->OnResponse(stanza);
  112. return true;
  113. }
  114. IqRequest::IqRequest(IqSender* sender,
  115. IqSender::ReplyCallback callback,
  116. const std::string& addressee)
  117. : sender_(sender), callback_(std::move(callback)), addressee_(addressee) {}
  118. IqRequest::~IqRequest() {
  119. sender_->RemoveRequest(this);
  120. }
  121. void IqRequest::SetTimeout(base::TimeDelta timeout) {
  122. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  123. FROM_HERE,
  124. base::BindOnce(&IqRequest::OnTimeout, weak_factory_.GetWeakPtr()),
  125. timeout);
  126. }
  127. void IqRequest::CallCallback(const jingle_xmpp::XmlElement* stanza) {
  128. if (!callback_.is_null())
  129. std::move(callback_).Run(this, stanza);
  130. }
  131. void IqRequest::OnTimeout() {
  132. CallCallback(nullptr);
  133. }
  134. void IqRequest::OnResponse(const jingle_xmpp::XmlElement* stanza) {
  135. // It's unsafe to delete signal strategy here, and the callback may
  136. // want to do that, so we post task to invoke the callback later.
  137. std::unique_ptr<jingle_xmpp::XmlElement> stanza_copy(new jingle_xmpp::XmlElement(*stanza));
  138. base::ThreadTaskRunnerHandle::Get()->PostTask(
  139. FROM_HERE,
  140. base::BindOnce(&IqRequest::DeliverResponse, weak_factory_.GetWeakPtr(),
  141. std::move(stanza_copy)));
  142. }
  143. void IqRequest::DeliverResponse(std::unique_ptr<jingle_xmpp::XmlElement> stanza) {
  144. CallCallback(stanza.get());
  145. }
  146. } // namespace remoting