Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: sync/notifier/push_client_channel.cc

Issue 10436013: [Sync] Make InvalidationNotifier use PushClient (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "sync/notifier/push_client_channel.h"
6
7 #include "base/stl_util.h"
8 #include "google/cacheinvalidation/v2/client_gateway.pb.h"
9 #include "jingle/notifier/listener/push_client.h"
10
11 namespace sync_notifier {
12
13 namespace {
14
15 const char kBotJid[] = "tango@bot.talk.google.com";
16 const char kChannelName[] = "tango_raw";
17
18 } // namespace
19
20 PushClientChannel::PushClientChannel(
21 scoped_ptr<notifier::PushClient> push_client)
22 : push_client_(push_client.Pass()),
23 notifications_enabled_(false),
24 scheduling_hash_(0) {
25 push_client_->AddObserver(this);
26 notifier::Subscription subscription;
27 subscription.channel = kChannelName;
28 subscription.from = "";
29 notifier::SubscriptionList subscriptions;
30 subscriptions.push_back(subscription);
31 push_client_->UpdateSubscriptions(subscriptions);
32 }
33
34 PushClientChannel::~PushClientChannel() {
35 push_client_->RemoveObserver(this);
36 STLDeleteElements(&network_status_receivers_);
37 }
38
39 void PushClientChannel::UpdateCredentials(
40 const std::string& email, const std::string& token) {
41 push_client_->UpdateCredentials(email, token);
42 }
43
44 void PushClientChannel::SendMessage(const std::string& outgoing_message) {
45 push_client_->SendNotification(
46 EncodeMessage(outgoing_message, service_context_, scheduling_hash_));
47 }
48
49 void PushClientChannel::SetMessageReceiver(
50 invalidation::MessageCallback* incoming_receiver) {
51 incoming_receiver_.reset(incoming_receiver);
52 }
53
54 void PushClientChannel::AddNetworkStatusReceiver(
55 invalidation::NetworkStatusCallback* network_status_receiver) {
56 network_status_receiver->Run(notifications_enabled_);
57 network_status_receivers_.push_back(network_status_receiver);
58 }
59
60 void PushClientChannel::SetSystemResources(
61 invalidation::SystemResources* resources) {
62 // Do nothing.
63 }
64
65 void PushClientChannel::OnNotificationStateChange(
66 bool notifications_enabled) {
67 for (NetworkStatusReceiverList::const_iterator it =
68 network_status_receivers_.begin();
69 it != network_status_receivers_.end(); ++it) {
70 (*it)->Run(notifications_enabled);
71 }
72 }
73
74 void PushClientChannel::OnIncomingNotification(
75 const notifier::Notification& notification) {
76 if (!incoming_receiver_.get()) {
77 DLOG(ERROR) << "No receiver for incoming notification";
78 return;
79 }
80 std::string message;
81 if (!DecodeMessage(notification,
82 &message, &service_context_, &scheduling_hash_)) {
83 DLOG(ERROR) << "Could not parse ClientGatewayMessage from: "
84 << notification.ToString();
85 }
86 incoming_receiver_->Run(message);
87 }
88
89 const std::string& PushClientChannel::GetServiceContextForTest() const {
90 return service_context_;
91 }
92
93 int64 PushClientChannel::GetSchedulingHashForTest() const {
94 return scheduling_hash_;
95 }
96
97 notifier::Notification PushClientChannel::EncodeMessageForTest(
98 const std::string& message, const std::string& service_context,
99 int64 scheduling_hash) {
100 return EncodeMessage(message, service_context, scheduling_hash);
101 }
102
103 bool PushClientChannel::DecodeMessageForTest(
104 const notifier::Notification& notification,
105 std::string* message,
106 std::string* service_context,
107 int64* scheduling_hash) {
108 return DecodeMessage(
109 notification, message, service_context, scheduling_hash);
110 }
111
112 notifier::Notification PushClientChannel::EncodeMessage(
113 const std::string& message, const std::string& service_context,
114 int64 scheduling_hash) {
115 ipc::invalidation::ClientGatewayMessage envelope;
116 envelope.set_is_client_to_server(true);
117 if (!service_context.empty()) {
118 envelope.set_service_context(service_context);
119 envelope.set_rpc_scheduling_hash(scheduling_hash);
120 }
121 envelope.set_network_message(message);
122
123 notifier::Recipient recipient;
124 recipient.to = kBotJid;
125 notifier::Notification notification;
126 notification.channel = kChannelName;
127 notification.recipients.push_back(recipient);
128 envelope.SerializeToString(&notification.data);
129 return notification;
130 }
131
132 bool PushClientChannel::DecodeMessage(
133 const notifier::Notification& notification,
134 std::string* message,
135 std::string* service_context,
136 int64* scheduling_hash) {
137 ipc::invalidation::ClientGatewayMessage envelope;
138 if (!envelope.ParseFromString(notification.data)) {
139 return false;
140 }
141 *message = envelope.network_message();
142 if (envelope.has_service_context()) {
143 *service_context = envelope.service_context();
144 }
145 if (envelope.has_rpc_scheduling_hash()) {
146 *scheduling_hash = envelope.rpc_scheduling_hash();
147 }
148 return true;
149 }
150
151 } // namespace sync_notifier
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698