| 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/cache_invalidation_packet_handler.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/base64.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/rand_util.h" | |
| 14 #include "base/string_number_conversions.h" | |
| 15 #include "google/cacheinvalidation/impl/constants.h" | |
| 16 #include "google/cacheinvalidation/include/invalidation-client.h" | |
| 17 #include "google/cacheinvalidation/include/system-resources.h" | |
| 18 #include "google/cacheinvalidation/v2/client_gateway.pb.h" | |
| 19 #include "jingle/notifier/listener/notification_constants.h" | |
| 20 #include "jingle/notifier/listener/push_notifications_send_update_task.h" | |
| 21 #include "jingle/notifier/listener/xml_element_util.h" | |
| 22 #include "talk/xmpp/constants.h" | |
| 23 #include "talk/xmpp/jid.h" | |
| 24 #include "talk/xmpp/xmppclient.h" | |
| 25 #include "talk/xmpp/xmpptask.h" | |
| 26 | |
| 27 namespace sync_notifier { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const char kBotJid[] = "tango@bot.talk.google.com"; | |
| 32 const char kChannelName[] = "tango_raw"; | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 CacheInvalidationPacketHandler::CacheInvalidationPacketHandler( | |
| 37 base::WeakPtr<buzz::XmppTaskParentInterface> base_task) | |
| 38 : weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 39 base_task_(base_task), | |
| 40 seq_(0), | |
| 41 scheduling_hash_(0) { | |
| 42 CHECK(base_task_.get()); | |
| 43 // Owned by base_task. Takes ownership of the callback. | |
| 44 notifier::PushNotificationsListenTask* listen_task = | |
| 45 new notifier::PushNotificationsListenTask(base_task_, this); | |
| 46 listen_task->Start(); | |
| 47 } | |
| 48 | |
| 49 CacheInvalidationPacketHandler::~CacheInvalidationPacketHandler() { | |
| 50 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 51 } | |
| 52 | |
| 53 void CacheInvalidationPacketHandler::SendMessage( | |
| 54 const std::string& message) { | |
| 55 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 56 if (!base_task_.get()) { | |
| 57 return; | |
| 58 } | |
| 59 ipc::invalidation::ClientGatewayMessage envelope; | |
| 60 envelope.set_is_client_to_server(true); | |
| 61 if (!service_context_.empty()) { | |
| 62 envelope.set_service_context(service_context_); | |
| 63 envelope.set_rpc_scheduling_hash(scheduling_hash_); | |
| 64 } | |
| 65 envelope.set_network_message(message); | |
| 66 | |
| 67 notifier::Recipient recipient; | |
| 68 recipient.to = kBotJid; | |
| 69 notifier::Notification notification; | |
| 70 notification.channel = kChannelName; | |
| 71 notification.recipients.push_back(recipient); | |
| 72 envelope.SerializeToString(¬ification.data); | |
| 73 | |
| 74 // Owned by base_task_. | |
| 75 notifier::PushNotificationsSendUpdateTask* send_message_task = | |
| 76 new notifier::PushNotificationsSendUpdateTask(base_task_, notification); | |
| 77 send_message_task->Start(); | |
| 78 } | |
| 79 | |
| 80 void CacheInvalidationPacketHandler::SetMessageReceiver( | |
| 81 invalidation::MessageCallback* incoming_receiver) { | |
| 82 incoming_receiver_.reset(incoming_receiver); | |
| 83 } | |
| 84 | |
| 85 void CacheInvalidationPacketHandler::SendSubscriptionRequest() { | |
| 86 notifier::Subscription subscription; | |
| 87 subscription.channel = kChannelName; | |
| 88 subscription.from = ""; | |
| 89 notifier::SubscriptionList subscription_list; | |
| 90 subscription_list.push_back(subscription); | |
| 91 // Owned by base_task_. | |
| 92 notifier::PushNotificationsSubscribeTask* push_subscription_task = | |
| 93 new notifier::PushNotificationsSubscribeTask( | |
| 94 base_task_, subscription_list, this); | |
| 95 push_subscription_task->Start(); | |
| 96 } | |
| 97 | |
| 98 void CacheInvalidationPacketHandler::OnSubscribed() { | |
| 99 // TODO(ghc): Consider whether we should do more here. | |
| 100 } | |
| 101 | |
| 102 void CacheInvalidationPacketHandler::OnSubscriptionError() { | |
| 103 // TODO(ghc): Consider whether we should do more here. | |
| 104 } | |
| 105 | |
| 106 void CacheInvalidationPacketHandler::OnNotificationReceived( | |
| 107 const notifier::Notification& notification) { | |
| 108 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 109 const std::string& decoded_message = notification.data; | |
| 110 ipc::invalidation::ClientGatewayMessage envelope; | |
| 111 envelope.ParseFromString(decoded_message); | |
| 112 if (!envelope.IsInitialized()) { | |
| 113 LOG(ERROR) << "Could not parse ClientGatewayMessage: " | |
| 114 << decoded_message; | |
| 115 return; | |
| 116 } | |
| 117 if (envelope.has_service_context()) { | |
| 118 service_context_ = envelope.service_context(); | |
| 119 } | |
| 120 if (envelope.has_rpc_scheduling_hash()) { | |
| 121 scheduling_hash_ = envelope.rpc_scheduling_hash(); | |
| 122 } | |
| 123 incoming_receiver_->Run(envelope.network_message()); | |
| 124 } | |
| 125 | |
| 126 } // namespace sync_notifier | |
| OLD | NEW |