forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiq_sender.cc
179 lines (150 loc) · 5.03 KB
/
iq_sender.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/signaling/iq_sender.h"
#include <memory>
#include <utility>
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "remoting/signaling/signal_strategy.h"
#include "remoting/signaling/signaling_id_util.h"
#include "remoting/signaling/xmpp_constants.h"
#include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
namespace remoting {
// static
std::unique_ptr<jingle_xmpp::XmlElement> IqSender::MakeIqStanza(
const std::string& type,
const std::string& addressee,
std::unique_ptr<jingle_xmpp::XmlElement> iq_body) {
std::unique_ptr<jingle_xmpp::XmlElement> stanza(
new jingle_xmpp::XmlElement(kQNameIq));
stanza->AddAttr(kQNameType, type);
if (!addressee.empty()) {
stanza->AddAttr(kQNameTo, addressee);
}
stanza->AddElement(iq_body.release());
return stanza;
}
IqSender::IqSender(SignalStrategy* signal_strategy)
: signal_strategy_(signal_strategy) {
signal_strategy_->AddListener(this);
}
IqSender::~IqSender() {
signal_strategy_->RemoveListener(this);
}
std::unique_ptr<IqRequest> IqSender::SendIq(
std::unique_ptr<jingle_xmpp::XmlElement> stanza,
ReplyCallback callback) {
std::string addressee = stanza->Attr(kQNameTo);
std::string id = stanza->Attr(kQNameId);
if (id.empty()) {
id = signal_strategy_->GetNextId();
stanza->AddAttr(kQNameId, id);
}
if (!signal_strategy_->SendStanza(std::move(stanza))) {
return nullptr;
}
DCHECK(requests_.find(id) == requests_.end());
bool callback_exists = !callback.is_null();
auto request =
std::make_unique<IqRequest>(this, std::move(callback), addressee);
if (callback_exists) {
requests_[id] = request.get();
}
return request;
}
std::unique_ptr<IqRequest> IqSender::SendIq(
const std::string& type,
const std::string& addressee,
std::unique_ptr<jingle_xmpp::XmlElement> iq_body,
ReplyCallback callback) {
return SendIq(MakeIqStanza(type, addressee, std::move(iq_body)),
std::move(callback));
}
void IqSender::RemoveRequest(IqRequest* request) {
auto it = requests_.begin();
while (it != requests_.end()) {
auto cur = it;
++it;
if (cur->second == request) {
requests_.erase(cur);
break;
}
}
}
void IqSender::OnSignalStrategyStateChange(SignalStrategy::State state) {}
bool IqSender::OnSignalStrategyIncomingStanza(
const jingle_xmpp::XmlElement* stanza) {
if (stanza->Name() != kQNameIq) {
LOG(WARNING) << "Received unexpected non-IQ packet " << stanza->Str();
return false;
}
const std::string& type = stanza->Attr(kQNameType);
if (type.empty()) {
LOG(WARNING) << "IQ packet missing type " << stanza->Str();
return false;
}
if (type != "result" && type != "error") {
return false;
}
const std::string& id = stanza->Attr(kQNameId);
if (id.empty()) {
LOG(WARNING) << "IQ packet missing id " << stanza->Str();
return false;
}
std::string from = stanza->Attr(kQNameFrom);
auto it = requests_.find(id);
if (it == requests_.end()) {
return false;
}
IqRequest* request = it->second;
if (NormalizeSignalingId(request->addressee_) != NormalizeSignalingId(from)) {
LOG(ERROR) << "Received IQ response from an invalid JID. Ignoring it."
<< " Message received from: " << from
<< " Original JID: " << request->addressee_;
return false;
}
requests_.erase(it);
request->OnResponse(stanza);
return true;
}
IqRequest::IqRequest(IqSender* sender,
IqSender::ReplyCallback callback,
const std::string& addressee)
: sender_(sender), callback_(std::move(callback)), addressee_(addressee) {}
IqRequest::~IqRequest() {
sender_->RemoveRequest(this);
}
void IqRequest::SetTimeout(base::TimeDelta timeout) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&IqRequest::OnTimeout, weak_factory_.GetWeakPtr()),
timeout);
}
void IqRequest::CallCallback(const jingle_xmpp::XmlElement* stanza) {
if (!callback_.is_null()) {
std::move(callback_).Run(this, stanza);
}
}
void IqRequest::OnTimeout() {
CallCallback(nullptr);
}
void IqRequest::OnResponse(const jingle_xmpp::XmlElement* stanza) {
// It's unsafe to delete signal strategy here, and the callback may
// want to do that, so we post task to invoke the callback later.
std::unique_ptr<jingle_xmpp::XmlElement> stanza_copy(
new jingle_xmpp::XmlElement(*stanza));
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&IqRequest::DeliverResponse, weak_factory_.GetWeakPtr(),
std::move(stanza_copy)));
}
void IqRequest::DeliverResponse(
std::unique_ptr<jingle_xmpp::XmlElement> stanza) {
CallCallback(stanza.get());
}
} // namespace remoting