| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/sync/notifier/server_notifier_thread.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" |
| 12 #include "chrome/browser/sync/notifier/chrome_invalidation_client.h" |
| 13 #include "chrome/browser/sync/notifier/chrome_system_resources.h" |
| 14 #include "chrome/browser/sync/notifier/invalidation_util.h" |
| 15 #include "chrome/common/net/notifier/listener/notification_defines.h" |
| 16 #include "google/cacheinvalidation/invalidation-client-impl.h" |
| 17 #include "talk/xmpp/jid.h" |
| 18 |
| 19 namespace sync_notifier { |
| 20 |
| 21 ServerNotifierThread::ServerNotifierThread( |
| 22 chrome_common_net::NetworkChangeNotifierThread* |
| 23 network_change_notifier_thread) |
| 24 : notifier::MediatorThreadImpl(network_change_notifier_thread) {} |
| 25 |
| 26 ServerNotifierThread::~ServerNotifierThread() {} |
| 27 |
| 28 void ServerNotifierThread::ListenForUpdates() { |
| 29 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 30 worker_message_loop()->PostTask( |
| 31 FROM_HERE, |
| 32 NewRunnableMethod(this, |
| 33 &ServerNotifierThread::StartInvalidationListener)); |
| 34 } |
| 35 |
| 36 void ServerNotifierThread::SubscribeForUpdates( |
| 37 const std::vector<std::string>& subscribed_services_list) { |
| 38 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 39 worker_message_loop()->PostTask( |
| 40 FROM_HERE, |
| 41 NewRunnableMethod( |
| 42 this, &ServerNotifierThread::RegisterTypesAndSignalSubscribed)); |
| 43 } |
| 44 |
| 45 void ServerNotifierThread::Logout() { |
| 46 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 47 worker_message_loop()->PostTask( |
| 48 FROM_HERE, |
| 49 NewRunnableMethod(this, |
| 50 &ServerNotifierThread::StopInvalidationListener)); |
| 51 MediatorThreadImpl::Logout(); |
| 52 } |
| 53 |
| 54 void ServerNotifierThread::SendNotification( |
| 55 const OutgoingNotificationData& data) { |
| 56 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 57 NOTREACHED() << "Shouldn't send notifications if " |
| 58 << "ServerNotifierThread is used"; |
| 59 } |
| 60 |
| 61 void ServerNotifierThread::Invalidate( |
| 62 const invalidation::Invalidation& invalidation, |
| 63 invalidation::Closure* callback) { |
| 64 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 65 CHECK(invalidation::IsCallbackRepeatable(callback)); |
| 66 LOG(INFO) << "Invalidate: " << InvalidationToString(invalidation); |
| 67 // Signal notification only for the invalidated types. |
| 68 parent_message_loop_->PostTask( |
| 69 FROM_HERE, |
| 70 NewRunnableMethod( |
| 71 this, |
| 72 &ServerNotifierThread::SignalIncomingNotification)); |
| 73 RunAndDeleteClosure(callback); |
| 74 // A real implementation would respond to the invalidation for the |
| 75 // given object (e.g., refetch the invalidated object). |
| 76 } |
| 77 |
| 78 void ServerNotifierThread::InvalidateAll( |
| 79 invalidation::Closure* callback) { |
| 80 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 81 CHECK(invalidation::IsCallbackRepeatable(callback)); |
| 82 LOG(INFO) << "InvalidateAll"; |
| 83 parent_message_loop_->PostTask( |
| 84 FROM_HERE, |
| 85 NewRunnableMethod( |
| 86 this, |
| 87 &ServerNotifierThread::SignalIncomingNotification)); |
| 88 RunAndDeleteClosure(callback); |
| 89 } |
| 90 |
| 91 void ServerNotifierThread::AllRegistrationsLost( |
| 92 invalidation::Closure* callback) { |
| 93 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 94 CHECK(invalidation::IsCallbackRepeatable(callback)); |
| 95 LOG(INFO) << "AllRegistrationsLost; reregistering"; |
| 96 RegisterTypes(); |
| 97 RunAndDeleteClosure(callback); |
| 98 } |
| 99 |
| 100 void ServerNotifierThread::RegistrationLost( |
| 101 const invalidation::ObjectId& object_id, |
| 102 invalidation::Closure* callback) { |
| 103 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 104 CHECK(invalidation::IsCallbackRepeatable(callback)); |
| 105 LOG(INFO) << "RegistrationLost; reregistering: " |
| 106 << ObjectIdToString(object_id); |
| 107 RegisterTypes(); |
| 108 RunAndDeleteClosure(callback); |
| 109 } |
| 110 |
| 111 void ServerNotifierThread::StartInvalidationListener() { |
| 112 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 113 |
| 114 StopInvalidationListener(); |
| 115 chrome_invalidation_client_.reset(new ChromeInvalidationClient()); |
| 116 |
| 117 // TODO(akalin): If we can figure out a unique per-session key that |
| 118 // is preserved by the server, we can use that instead of kAppName. |
| 119 // What this buys us is that we then won't receive any notifications |
| 120 // that were generated by ourselves. |
| 121 const std::string kAppName = "cc_sync_listen_notifications"; |
| 122 chrome_invalidation_client_->Start(kAppName, this, xmpp_client()); |
| 123 } |
| 124 |
| 125 void ServerNotifierThread::RegisterTypesAndSignalSubscribed() { |
| 126 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 127 RegisterTypes(); |
| 128 parent_message_loop_->PostTask( |
| 129 FROM_HERE, |
| 130 NewRunnableMethod( |
| 131 this, |
| 132 &ServerNotifierThread::SignalSubscribed)); |
| 133 } |
| 134 |
| 135 void ServerNotifierThread::RegisterTypes() { |
| 136 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 137 |
| 138 // TODO(akalin): This is a giant hack! Make this configurable. Add |
| 139 // a mapping to/from ModelType. |
| 140 std::vector<std::string> data_types; |
| 141 data_types.push_back("AUTOFILL"); |
| 142 data_types.push_back("BOOKMARK"); |
| 143 data_types.push_back("EXTENSION"); |
| 144 data_types.push_back("PASSWORD"); |
| 145 data_types.push_back("THEME"); |
| 146 data_types.push_back("TYPED_URL"); |
| 147 data_types.push_back("PREFERENCE"); |
| 148 |
| 149 std::vector<invalidation::ObjectId> object_ids; |
| 150 |
| 151 for (std::vector<std::string>::const_iterator it = data_types.begin(); |
| 152 it != data_types.end(); ++it) { |
| 153 invalidation::ObjectId object_id; |
| 154 object_id.mutable_name()->set_string_value(*it); |
| 155 object_id.set_source(invalidation::ObjectId::CHROME_SYNC); |
| 156 object_ids.push_back(object_id); |
| 157 } |
| 158 |
| 159 for (std::vector<invalidation::ObjectId>::const_iterator it = |
| 160 object_ids.begin(); it != object_ids.end(); ++it) { |
| 161 chrome_invalidation_client_->Register( |
| 162 *it, |
| 163 invalidation::NewPermanentCallback( |
| 164 this, &ServerNotifierThread::RegisterCallback)); |
| 165 } |
| 166 } |
| 167 |
| 168 void ServerNotifierThread::RegisterCallback( |
| 169 const invalidation::RegistrationUpdateResult& result) { |
| 170 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 171 // TODO(akalin): Do something meaningful here. |
| 172 LOG(INFO) << "Registered: " << RegistrationUpdateResultToString(result); |
| 173 } |
| 174 |
| 175 void ServerNotifierThread::SignalSubscribed() { |
| 176 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 177 if (delegate_) { |
| 178 delegate_->OnSubscriptionStateChange(true); |
| 179 } |
| 180 } |
| 181 |
| 182 void ServerNotifierThread::SignalIncomingNotification() { |
| 183 DCHECK_EQ(MessageLoop::current(), parent_message_loop_); |
| 184 if (delegate_) { |
| 185 // TODO(akalin): Fill this in with something meaningful. |
| 186 IncomingNotificationData notification_data; |
| 187 delegate_->OnIncomingNotification(notification_data); |
| 188 } |
| 189 } |
| 190 |
| 191 void ServerNotifierThread::StopInvalidationListener() { |
| 192 DCHECK_EQ(MessageLoop::current(), worker_message_loop()); |
| 193 |
| 194 if (chrome_invalidation_client_.get()) { |
| 195 // TODO(akalin): Need to do unregisters here? |
| 196 chrome_invalidation_client_->Stop(); |
| 197 } |
| 198 chrome_invalidation_client_.reset(); |
| 199 } |
| 200 |
| 201 } // namespace sync_notifier |
| OLD | NEW |