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