OLD | NEW |
| (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/client_gateway.pb.h" | |
9 #include "google/cacheinvalidation/types.pb.h" | |
10 #include "jingle/notifier/listener/push_client.h" | |
11 | |
12 namespace syncer { | |
13 | |
14 namespace { | |
15 | |
16 const char kBotJid[] = "tango@bot.talk.google.com"; | |
17 const char kChannelName[] = "tango_raw"; | |
18 | |
19 } // namespace | |
20 | |
21 PushClientChannel::PushClientChannel( | |
22 scoped_ptr<notifier::PushClient> push_client) | |
23 : push_client_(push_client.Pass()), | |
24 scheduling_hash_(0), | |
25 sent_messages_count_(0) { | |
26 push_client_->AddObserver(this); | |
27 notifier::Subscription subscription; | |
28 subscription.channel = kChannelName; | |
29 subscription.from = ""; | |
30 notifier::SubscriptionList subscriptions; | |
31 subscriptions.push_back(subscription); | |
32 push_client_->UpdateSubscriptions(subscriptions); | |
33 } | |
34 | |
35 PushClientChannel::~PushClientChannel() { | |
36 push_client_->RemoveObserver(this); | |
37 } | |
38 | |
39 void PushClientChannel::UpdateCredentials( | |
40 const std::string& email, const std::string& token) { | |
41 push_client_->UpdateCredentials(email, token); | |
42 } | |
43 | |
44 int PushClientChannel::GetInvalidationClientType() { | |
45 #if defined(OS_IOS) | |
46 return ipc::invalidation::ClientType::CHROME_SYNC_IOS; | |
47 #else | |
48 return ipc::invalidation::ClientType::CHROME_SYNC; | |
49 #endif | |
50 } | |
51 | |
52 void PushClientChannel::RequestDetailedStatus( | |
53 base::Callback<void(const base::DictionaryValue&)> callback) { | |
54 callback.Run(*CollectDebugData()); | |
55 } | |
56 | |
57 void PushClientChannel::SendMessage(const std::string& message) { | |
58 std::string encoded_message; | |
59 EncodeMessage(&encoded_message, message, service_context_, scheduling_hash_); | |
60 | |
61 notifier::Recipient recipient; | |
62 recipient.to = kBotJid; | |
63 notifier::Notification notification; | |
64 notification.channel = kChannelName; | |
65 notification.recipients.push_back(recipient); | |
66 notification.data = encoded_message; | |
67 push_client_->SendNotification(notification); | |
68 sent_messages_count_++; | |
69 } | |
70 | |
71 void PushClientChannel::OnNotificationsEnabled() { | |
72 NotifyStateChange(INVALIDATIONS_ENABLED); | |
73 } | |
74 | |
75 void PushClientChannel::OnNotificationsDisabled( | |
76 notifier::NotificationsDisabledReason reason) { | |
77 NotifyStateChange(FromNotifierReason(reason)); | |
78 } | |
79 | |
80 void PushClientChannel::OnIncomingNotification( | |
81 const notifier::Notification& notification) { | |
82 std::string message; | |
83 std::string service_context; | |
84 int64 scheduling_hash; | |
85 if (!DecodeMessage( | |
86 notification.data, &message, &service_context, &scheduling_hash)) { | |
87 DLOG(ERROR) << "Could not parse ClientGatewayMessage"; | |
88 return; | |
89 } | |
90 if (DeliverIncomingMessage(message)) { | |
91 service_context_ = service_context; | |
92 scheduling_hash_ = scheduling_hash; | |
93 } | |
94 } | |
95 | |
96 const std::string& PushClientChannel::GetServiceContextForTest() const { | |
97 return service_context_; | |
98 } | |
99 | |
100 int64 PushClientChannel::GetSchedulingHashForTest() const { | |
101 return scheduling_hash_; | |
102 } | |
103 | |
104 std::string PushClientChannel::EncodeMessageForTest( | |
105 const std::string& message, | |
106 const std::string& service_context, | |
107 int64 scheduling_hash) { | |
108 std::string encoded_message; | |
109 EncodeMessage(&encoded_message, message, service_context, scheduling_hash); | |
110 return encoded_message; | |
111 } | |
112 | |
113 bool PushClientChannel::DecodeMessageForTest(const std::string& data, | |
114 std::string* message, | |
115 std::string* service_context, | |
116 int64* scheduling_hash) { | |
117 return DecodeMessage(data, message, service_context, scheduling_hash); | |
118 } | |
119 | |
120 void PushClientChannel::EncodeMessage(std::string* encoded_message, | |
121 const std::string& message, | |
122 const std::string& service_context, | |
123 int64 scheduling_hash) { | |
124 ipc::invalidation::ClientGatewayMessage envelope; | |
125 envelope.set_is_client_to_server(true); | |
126 if (!service_context.empty()) { | |
127 envelope.set_service_context(service_context); | |
128 envelope.set_rpc_scheduling_hash(scheduling_hash); | |
129 } | |
130 envelope.set_network_message(message); | |
131 envelope.SerializeToString(encoded_message); | |
132 } | |
133 | |
134 bool PushClientChannel::DecodeMessage(const std::string& data, | |
135 std::string* message, | |
136 std::string* service_context, | |
137 int64* scheduling_hash) { | |
138 ipc::invalidation::ClientGatewayMessage envelope; | |
139 if (!envelope.ParseFromString(data)) { | |
140 return false; | |
141 } | |
142 *message = envelope.network_message(); | |
143 if (envelope.has_service_context()) { | |
144 *service_context = envelope.service_context(); | |
145 } | |
146 if (envelope.has_rpc_scheduling_hash()) { | |
147 *scheduling_hash = envelope.rpc_scheduling_hash(); | |
148 } | |
149 return true; | |
150 } | |
151 | |
152 scoped_ptr<base::DictionaryValue> PushClientChannel::CollectDebugData() const { | |
153 scoped_ptr<base::DictionaryValue> status(new base::DictionaryValue); | |
154 status->SetString("PushClientChannel.NetworkChannel", "Push Client"); | |
155 status->SetInteger("PushClientChannel.SentMessages", sent_messages_count_); | |
156 status->SetInteger("PushClientChannel.ReceivedMessages", | |
157 SyncNetworkChannel::GetReceivedMessagesCount()); | |
158 return status.Pass(); | |
159 } | |
160 | |
161 } // namespace syncer | |
OLD | NEW |