| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/notifier/chrome_invalidation_client.h" | 5 #include "chrome/browser/sync/notifier/chrome_invalidation_client.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" | 11 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" |
| 12 #include "chrome/browser/sync/notifier/invalidation_util.h" | 12 #include "chrome/browser/sync/notifier/invalidation_util.h" |
| 13 #include "chrome/browser/sync/notifier/registration_manager.h" |
| 13 #include "chrome/browser/sync/syncable/model_type.h" | 14 #include "chrome/browser/sync/syncable/model_type.h" |
| 14 | 15 |
| 15 namespace sync_notifier { | 16 namespace sync_notifier { |
| 16 | 17 |
| 17 ChromeInvalidationClient::Listener::~Listener() {} | 18 ChromeInvalidationClient::Listener::~Listener() {} |
| 18 | 19 |
| 19 ChromeInvalidationClient::ChromeInvalidationClient() | 20 ChromeInvalidationClient::ChromeInvalidationClient() |
| 20 : listener_(NULL) { | 21 : listener_(NULL) { |
| 21 DCHECK(non_thread_safe_.CalledOnValidThread()); | 22 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 22 } | 23 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 39 listener_ = listener; | 40 listener_ = listener; |
| 40 | 41 |
| 41 invalidation::ClientType client_type; | 42 invalidation::ClientType client_type; |
| 42 client_type.set_type(invalidation::ClientType::CHROME_SYNC); | 43 client_type.set_type(invalidation::ClientType::CHROME_SYNC); |
| 43 invalidation_client_.reset( | 44 invalidation_client_.reset( |
| 44 invalidation::InvalidationClient::Create( | 45 invalidation::InvalidationClient::Create( |
| 45 &chrome_system_resources_, client_type, client_id, this)); | 46 &chrome_system_resources_, client_type, client_id, this)); |
| 46 cache_invalidation_packet_handler_.reset( | 47 cache_invalidation_packet_handler_.reset( |
| 47 new CacheInvalidationPacketHandler(xmpp_client, | 48 new CacheInvalidationPacketHandler(xmpp_client, |
| 48 invalidation_client_.get())); | 49 invalidation_client_.get())); |
| 50 registration_manager_.reset( |
| 51 new RegistrationManager(invalidation_client_.get())); |
| 49 RegisterTypes(); | 52 RegisterTypes(); |
| 50 } | 53 } |
| 51 | 54 |
| 52 void ChromeInvalidationClient::Stop() { | 55 void ChromeInvalidationClient::Stop() { |
| 53 DCHECK(non_thread_safe_.CalledOnValidThread()); | 56 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 54 if (!invalidation_client_.get()) { | 57 if (!invalidation_client_.get()) { |
| 55 DCHECK(!cache_invalidation_packet_handler_.get()); | 58 DCHECK(!cache_invalidation_packet_handler_.get()); |
| 56 return; | 59 return; |
| 57 } | 60 } |
| 58 | 61 |
| 59 chrome_system_resources_.StopScheduler(); | 62 chrome_system_resources_.StopScheduler(); |
| 60 | 63 |
| 64 registration_manager_.reset(); |
| 61 cache_invalidation_packet_handler_.reset(); | 65 cache_invalidation_packet_handler_.reset(); |
| 62 invalidation_client_.reset(); | 66 invalidation_client_.reset(); |
| 63 listener_ = NULL; | 67 listener_ = NULL; |
| 64 } | 68 } |
| 65 | 69 |
| 66 void ChromeInvalidationClient::RegisterTypes() { | 70 void ChromeInvalidationClient::RegisterTypes() { |
| 67 DCHECK(non_thread_safe_.CalledOnValidThread()); | 71 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 68 | 72 |
| 69 // TODO(akalin): Make this configurable instead of listening to | 73 // TODO(akalin): Make this configurable instead of listening to |
| 70 // notifications for all possible types. | 74 // notifications for all possible types. |
| 71 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 75 for (int i = syncable::FIRST_REAL_MODEL_TYPE; |
| 72 i < syncable::MODEL_TYPE_COUNT; ++i) { | 76 i < syncable::MODEL_TYPE_COUNT; ++i) { |
| 73 syncable::ModelType model_type = syncable::ModelTypeFromInt(i); | 77 registration_manager_->RegisterType(syncable::ModelTypeFromInt(i)); |
| 74 std::string notification_type; | |
| 75 if (!syncable::RealModelTypeToNotificationType( | |
| 76 model_type, ¬ification_type)) { | |
| 77 LOG(ERROR) << "Could not get notification type for model type " | |
| 78 << syncable::ModelTypeToString(model_type); | |
| 79 continue; | |
| 80 } | |
| 81 invalidation::ObjectId object_id; | |
| 82 object_id.mutable_name()->set_string_value(notification_type); | |
| 83 object_id.set_source(invalidation::ObjectId::CHROME_SYNC); | |
| 84 invalidation_client_->Register( | |
| 85 object_id, | |
| 86 invalidation::NewPermanentCallback( | |
| 87 this, &ChromeInvalidationClient::OnRegister)); | |
| 88 } | 78 } |
| 89 } | 79 } |
| 90 | 80 |
| 91 namespace { | |
| 92 | |
| 93 bool GetInvalidationModelType(const invalidation::Invalidation& invalidation, | |
| 94 syncable::ModelType* model_type) { | |
| 95 return | |
| 96 syncable::NotificationTypeToRealModelType( | |
| 97 invalidation.object_id().name().string_value(), model_type); | |
| 98 } | |
| 99 | |
| 100 } // namespace | |
| 101 | |
| 102 void ChromeInvalidationClient::Invalidate( | 81 void ChromeInvalidationClient::Invalidate( |
| 103 const invalidation::Invalidation& invalidation, | 82 const invalidation::Invalidation& invalidation, |
| 104 invalidation::Closure* callback) { | 83 invalidation::Closure* callback) { |
| 105 DCHECK(non_thread_safe_.CalledOnValidThread()); | 84 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 106 DCHECK(invalidation::IsCallbackRepeatable(callback)); | 85 DCHECK(invalidation::IsCallbackRepeatable(callback)); |
| 107 LOG(INFO) << "Invalidate: " << InvalidationToString(invalidation); | 86 LOG(INFO) << "Invalidate: " << InvalidationToString(invalidation); |
| 108 syncable::ModelType model_type; | 87 syncable::ModelType model_type; |
| 109 if (GetInvalidationModelType(invalidation, &model_type)) { | 88 if (ObjectIdToRealModelType(invalidation.object_id(), &model_type)) { |
| 110 listener_->OnInvalidate(model_type); | 89 listener_->OnInvalidate(model_type); |
| 111 } else { | 90 } else { |
| 112 LOG(WARNING) << "Could not get invalidation model type; " | 91 LOG(WARNING) << "Could not get invalidation model type; " |
| 113 << "invalidating everything"; | 92 << "invalidating everything"; |
| 114 listener_->OnInvalidateAll(); | 93 listener_->OnInvalidateAll(); |
| 115 } | 94 } |
| 116 RunAndDeleteClosure(callback); | 95 RunAndDeleteClosure(callback); |
| 117 } | 96 } |
| 118 | 97 |
| 119 void ChromeInvalidationClient::InvalidateAll( | 98 void ChromeInvalidationClient::InvalidateAll( |
| 120 invalidation::Closure* callback) { | 99 invalidation::Closure* callback) { |
| 121 DCHECK(non_thread_safe_.CalledOnValidThread()); | 100 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 122 DCHECK(invalidation::IsCallbackRepeatable(callback)); | 101 DCHECK(invalidation::IsCallbackRepeatable(callback)); |
| 123 LOG(INFO) << "InvalidateAll"; | 102 LOG(INFO) << "InvalidateAll"; |
| 124 listener_->OnInvalidateAll(); | 103 listener_->OnInvalidateAll(); |
| 125 RunAndDeleteClosure(callback); | 104 RunAndDeleteClosure(callback); |
| 126 } | 105 } |
| 127 | 106 |
| 128 void ChromeInvalidationClient::AllRegistrationsLost( | 107 void ChromeInvalidationClient::AllRegistrationsLost( |
| 129 invalidation::Closure* callback) { | 108 invalidation::Closure* callback) { |
| 130 DCHECK(non_thread_safe_.CalledOnValidThread()); | 109 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 131 DCHECK(invalidation::IsCallbackRepeatable(callback)); | 110 DCHECK(invalidation::IsCallbackRepeatable(callback)); |
| 132 LOG(INFO) << "AllRegistrationsLost; reregistering"; | 111 LOG(INFO) << "AllRegistrationsLost"; |
| 133 RegisterTypes(); | 112 registration_manager_->MarkAllRegistrationsLost(); |
| 134 RunAndDeleteClosure(callback); | 113 RunAndDeleteClosure(callback); |
| 135 } | 114 } |
| 136 | 115 |
| 137 void ChromeInvalidationClient::RegistrationLost( | 116 void ChromeInvalidationClient::RegistrationLost( |
| 138 const invalidation::ObjectId& object_id, | 117 const invalidation::ObjectId& object_id, |
| 139 invalidation::Closure* callback) { | 118 invalidation::Closure* callback) { |
| 140 DCHECK(non_thread_safe_.CalledOnValidThread()); | 119 DCHECK(non_thread_safe_.CalledOnValidThread()); |
| 141 DCHECK(invalidation::IsCallbackRepeatable(callback)); | 120 DCHECK(invalidation::IsCallbackRepeatable(callback)); |
| 142 LOG(INFO) << "RegistrationLost; reregistering: " | 121 LOG(INFO) << "RegistrationLost: " << ObjectIdToString(object_id); |
| 143 << ObjectIdToString(object_id); | 122 syncable::ModelType model_type; |
| 144 RegisterTypes(); | 123 if (ObjectIdToRealModelType(object_id, &model_type)) { |
| 124 registration_manager_->MarkRegistrationLost(model_type); |
| 125 } else { |
| 126 LOG(WARNING) << "Could not get object id model type; ignoring"; |
| 127 } |
| 145 RunAndDeleteClosure(callback); | 128 RunAndDeleteClosure(callback); |
| 146 } | 129 } |
| 147 | 130 |
| 148 void ChromeInvalidationClient::OnRegister( | |
| 149 const invalidation::RegistrationUpdateResult& result) { | |
| 150 DCHECK(non_thread_safe_.CalledOnValidThread()); | |
| 151 // TODO(akalin): Do something meaningful here. | |
| 152 LOG(INFO) << "Registered: " << RegistrationUpdateResultToString(result); | |
| 153 } | |
| 154 | |
| 155 } // namespace sync_notifier | 131 } // namespace sync_notifier |
| OLD | NEW |