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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h"
#include <string>
#include "base/base64.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/sync/sync_constants.h"
#include "google/cacheinvalidation/invalidation-client.h"
#include "jingle/notifier/listener/xml_element_util.h"
#include "talk/xmpp/constants.h"
#include "talk/xmpp/jid.h"
#include "talk/xmpp/xmppclient.h"
#include "talk/xmpp/xmpptask.h"
namespace sync_notifier {
namespace {
const char kBotJid[] = "tango@bot.talk.google.com";
const buzz::QName kQnData("google:notifier", "data");
const buzz::QName kQnSeq("", "seq");
const buzz::QName kQnSid("", "sid");
const buzz::QName kQnServiceUrl("", "serviceUrl");
// TODO(akalin): Move these task classes out so that they can be
// unit-tested. This'll probably be done easier once we consolidate
// all the packet sending/receiving classes.
// A task that listens for ClientInvalidation messages and calls the
// given callback on them.
class CacheInvalidationListenTask : public buzz::XmppTask {
public:
// Takes ownership of callback.
CacheInvalidationListenTask(Task* parent,
Callback1<const std::string&>::Type* callback)
: XmppTask(parent, buzz::XmppEngine::HL_TYPE), callback_(callback) {}
virtual ~CacheInvalidationListenTask() {}
virtual int ProcessStart() {
VLOG(2) << "CacheInvalidationListenTask started";
return STATE_RESPONSE;
}
virtual int ProcessResponse() {
const buzz::XmlElement* stanza = NextStanza();
if (stanza == NULL) {
VLOG(2) << "CacheInvalidationListenTask blocked";
return STATE_BLOCKED;
}
VLOG(2) << "CacheInvalidationListenTask response received";
std::string data;
if (GetCacheInvalidationIqPacketData(stanza, &data)) {
callback_->Run(data);
} else {
LOG(ERROR) << "Could not get packet data";
}
// Acknowledge receipt of the iq to the buzz server.
// TODO(akalin): Send an error response for malformed packets.
scoped_ptr<buzz::XmlElement> response_stanza(MakeIqResult(stanza));
SendStanza(response_stanza.get());
return STATE_RESPONSE;
}
virtual bool HandleStanza(const buzz::XmlElement* stanza) {
VLOG(1) << "Stanza received: "
<< notifier::XmlElementToString(*stanza);
if (IsValidCacheInvalidationIqPacket(stanza)) {
VLOG(2) << "Queueing stanza";
QueueStanza(stanza);
return true;
}
VLOG(2) << "Stanza skipped";
return false;
}
private:
bool IsValidCacheInvalidationIqPacket(const buzz::XmlElement* stanza) {
// We deliberately minimize the verification we do here: see
// http://crbug.com/71285 .
return MatchRequestIq(stanza, buzz::STR_SET, kQnData);
}
bool GetCacheInvalidationIqPacketData(const buzz::XmlElement* stanza,
std::string* data) {
DCHECK(IsValidCacheInvalidationIqPacket(stanza));
const buzz::XmlElement* cache_invalidation_iq_packet =
stanza->FirstNamed(kQnData);
if (!cache_invalidation_iq_packet) {
LOG(ERROR) << "Could not find cache invalidation IQ packet element";
return false;
}
*data = cache_invalidation_iq_packet->BodyText();
return true;
}
scoped_ptr<Callback1<const std::string&>::Type> callback_;
DISALLOW_COPY_AND_ASSIGN(CacheInvalidationListenTask);
};
// A task that sends a single outbound ClientInvalidation message.
class CacheInvalidationSendMessageTask : public buzz::XmppTask {
public:
CacheInvalidationSendMessageTask(Task* parent,
const buzz::Jid& to_jid,
const std::string& msg,
int seq,
const std::string& sid)
: XmppTask(parent, buzz::XmppEngine::HL_SINGLE),
to_jid_(to_jid), msg_(msg), seq_(seq), sid_(sid) {}
virtual ~CacheInvalidationSendMessageTask() {}
virtual int ProcessStart() {
scoped_ptr<buzz::XmlElement> stanza(
MakeCacheInvalidationIqPacket(to_jid_, task_id(), msg_,
seq_, sid_));
VLOG(1) << "Sending message: "
<< notifier::XmlElementToString(*stanza.get());
if (SendStanza(stanza.get()) != buzz::XMPP_RETURN_OK) {
VLOG(2) << "Error when sending message";
return STATE_ERROR;
}
return STATE_RESPONSE;
}
virtual int ProcessResponse() {
const buzz::XmlElement* stanza = NextStanza();
if (stanza == NULL) {
VLOG(2) << "CacheInvalidationSendMessageTask blocked...";
return STATE_BLOCKED;
}
VLOG(2) << "CacheInvalidationSendMessageTask response received: "
<< notifier::XmlElementToString(*stanza);
// TODO(akalin): Handle errors here.
return STATE_DONE;
}
virtual bool HandleStanza(const buzz::XmlElement* stanza) {
VLOG(1) << "Stanza received: "
<< notifier::XmlElementToString(*stanza);
if (!MatchResponseIq(stanza, to_jid_, task_id())) {
VLOG(2) << "Stanza skipped";
return false;
}
VLOG(2) << "Queueing stanza";
QueueStanza(stanza);
return true;
}
private:
static buzz::XmlElement* MakeCacheInvalidationIqPacket(
const buzz::Jid& to_jid,
const std::string& task_id,
const std::string& msg,
int seq, const std::string& sid) {
buzz::XmlElement* iq = MakeIq(buzz::STR_SET, to_jid, task_id);
buzz::XmlElement* cache_invalidation_iq_packet =
new buzz::XmlElement(kQnData, true);
iq->AddElement(cache_invalidation_iq_packet);
cache_invalidation_iq_packet->SetAttr(kQnSeq, base::IntToString(seq));
cache_invalidation_iq_packet->SetAttr(kQnSid, sid);
cache_invalidation_iq_packet->SetAttr(kQnServiceUrl,
browser_sync::kSyncServiceUrl);
cache_invalidation_iq_packet->SetBodyText(msg);
return iq;
}
const buzz::Jid to_jid_;
std::string msg_;
int seq_;
std::string sid_;
DISALLOW_COPY_AND_ASSIGN(CacheInvalidationSendMessageTask);
};
std::string MakeSid() {
uint64 sid = base::RandUint64();
return std::string("chrome-sync-") + base::Uint64ToString(sid);
}
} // namespace
CacheInvalidationPacketHandler::CacheInvalidationPacketHandler(
base::WeakPtr<talk_base::Task> base_task,
invalidation::InvalidationClient* invalidation_client)
: scoped_callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
base_task_(base_task),
invalidation_client_(invalidation_client),
seq_(0),
sid_(MakeSid()) {
CHECK(base_task_.get());
// Owned by base_task. Takes ownership of the callback.
CacheInvalidationListenTask* listen_task =
new CacheInvalidationListenTask(
base_task_, scoped_callback_factory_.NewCallback(
&CacheInvalidationPacketHandler::HandleInboundPacket));
listen_task->Start();
}
CacheInvalidationPacketHandler::~CacheInvalidationPacketHandler() {
DCHECK(non_thread_safe_.CalledOnValidThread());
}
void CacheInvalidationPacketHandler::HandleOutboundPacket(
invalidation::NetworkEndpoint* network_endpoint) {
DCHECK(non_thread_safe_.CalledOnValidThread());
if (!base_task_.get()) {
return;
}
CHECK_EQ(network_endpoint, invalidation_client_->network_endpoint());
invalidation::string message;
network_endpoint->TakeOutboundMessage(&message);
std::string encoded_message;
if (!base::Base64Encode(message, &encoded_message)) {
LOG(ERROR) << "Could not base64-encode message to send: "
<< message;
return;
}
// Owned by base_task_.
CacheInvalidationSendMessageTask* send_message_task =
new CacheInvalidationSendMessageTask(base_task_,
buzz::Jid(kBotJid),
encoded_message,
seq_, sid_);
send_message_task->Start();
++seq_;
}
void CacheInvalidationPacketHandler::HandleInboundPacket(
const std::string& packet) {
DCHECK(non_thread_safe_.CalledOnValidThread());
invalidation::NetworkEndpoint* network_endpoint =
invalidation_client_->network_endpoint();
std::string decoded_message;
if (!base::Base64Decode(packet, &decoded_message)) {
LOG(ERROR) << "Could not base64-decode received message: "
<< packet;
return;
}
network_endpoint->HandleInboundMessage(decoded_message);
}
} // namespace sync_notifier
|