Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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/invalidation/ticl_invalidation_service.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "chrome/browser/invalidation/invalidation_service_util.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/signin/signin_manager.h" | |
| 11 #include "chrome/browser/signin/token_service.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #include "content/public/browser/notification_service.h" | |
| 14 #include "google_apis/gaia/gaia_constants.h" | |
| 15 #include "sync/notifier/invalidator.h" | |
| 16 #include "sync/notifier/invalidator_state.h" | |
| 17 #include "sync/notifier/non_blocking_invalidator.h" | |
| 18 | |
| 19 namespace invalidation { | |
| 20 | |
| 21 TiclInvalidationService::TiclInvalidationService(SigninManager* signin, | |
| 22 TokenService* token_service, | |
| 23 Profile* profile) | |
| 24 : profile_(profile), | |
| 25 signin_manager_(signin), | |
| 26 token_service_(token_service), | |
| 27 invalidator_registrar_(new syncer::InvalidatorRegistrar()) { } | |
| 28 | |
| 29 TiclInvalidationService::~TiclInvalidationService() { | |
| 30 DCHECK(CalledOnValidThread()); | |
| 31 } | |
| 32 | |
| 33 void TiclInvalidationService::Init() { | |
| 34 DCHECK(CalledOnValidThread()); | |
| 35 | |
| 36 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | |
| 37 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | |
| 38 // This also clears any existing state. We can't reuse old invalidator | |
| 39 // state with the new ID anyway. | |
| 40 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | |
| 41 } | |
| 42 | |
| 43 if (IsReadyToStart()) { | |
| 44 Start(); | |
| 45 } | |
| 46 | |
| 47 notification_registrar_.Add(this, | |
| 48 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
| 49 content::Source<TokenService>(token_service_)); | |
| 50 notification_registrar_.Add(this, | |
| 51 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
| 52 content::NotificationService::AllSources()); | |
| 53 } | |
| 54 | |
| 55 void TiclInvalidationService::InitForTest(syncer::Invalidator* invalidator) { | |
|
tim (not reviewing)
2013/05/03 22:23:50
You need to assign this somewhere?
Also, maybe Ini
rlarocque
2013/05/03 22:49:34
Start() is private method. This class is responsi
| |
| 56 // Here we perform the equivalent of Init() and Start(), but with some minor | |
| 57 // changes to account for the fact that we're injecting the invalidator. | |
| 58 invalidator_storage_.reset(new InvalidatorStorage(profile_->GetPrefs())); | |
| 59 if (invalidator_storage_->GetInvalidatorClientId().empty()) { | |
| 60 // This also clears any existing state. We can't reuse old invalidator | |
| 61 // state with the new ID anyway. | |
| 62 invalidator_storage_->SetInvalidatorClientId(GenerateInvalidatorClientId()); | |
| 63 } | |
| 64 | |
| 65 invalidator_->RegisterHandler(this); | |
| 66 invalidator_->UpdateRegisteredIds( | |
| 67 this, | |
| 68 invalidator_registrar_->GetAllRegisteredIds()); | |
| 69 } | |
| 70 | |
| 71 void TiclInvalidationService::RegisterInvalidationHandler( | |
| 72 syncer::InvalidationHandler* handler) { | |
| 73 DCHECK(CalledOnValidThread()); | |
| 74 DVLOG(2) << "Registering an invalidation handler"; | |
| 75 invalidator_registrar_->RegisterHandler(handler); | |
| 76 } | |
| 77 | |
| 78 void TiclInvalidationService::UpdateRegisteredInvalidationIds( | |
| 79 syncer::InvalidationHandler* handler, | |
| 80 const syncer::ObjectIdSet& ids) { | |
| 81 DCHECK(CalledOnValidThread()); | |
| 82 DVLOG(2) << "Registering ids: " << ids.size(); | |
| 83 invalidator_registrar_->UpdateRegisteredIds(handler, ids); | |
| 84 if (invalidator_) { | |
| 85 invalidator_->UpdateRegisteredIds( | |
| 86 this, | |
| 87 invalidator_registrar_->GetAllRegisteredIds()); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 void TiclInvalidationService::UnregisterInvalidationHandler( | |
| 92 syncer::InvalidationHandler* handler) { | |
| 93 DCHECK(CalledOnValidThread()); | |
| 94 DVLOG(2) << "Unregistering"; | |
| 95 invalidator_registrar_->UnregisterHandler(handler); | |
| 96 if (invalidator_) { | |
| 97 invalidator_->UpdateRegisteredIds( | |
| 98 this, | |
| 99 invalidator_registrar_->GetAllRegisteredIds()); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void TiclInvalidationService::AcknowledgeInvalidation( | |
| 104 const invalidation::ObjectId& id, | |
| 105 const syncer::AckHandle& ack_handle) { | |
| 106 DCHECK(CalledOnValidThread()); | |
| 107 if (invalidator_) { | |
| 108 invalidator_->Acknowledge(id, ack_handle); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 syncer::InvalidatorState TiclInvalidationService::GetInvalidatorState() const { | |
| 113 DCHECK(CalledOnValidThread()); | |
| 114 if (invalidator_) { | |
| 115 DVLOG(2) << "GetInvalidatorState returning " << GetInvalidatorState(); | |
| 116 return invalidator_->GetInvalidatorState(); | |
| 117 } else { | |
| 118 DVLOG(2) << "Invalidator currently stopped"; | |
| 119 return syncer::TRANSIENT_INVALIDATION_ERROR; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 std::string TiclInvalidationService::GetInvalidatorClientId() const { | |
| 124 DCHECK(CalledOnValidThread()); | |
| 125 // invalidator_storage_ will be initialized between calls to Init() and | |
| 126 // Stop(). No one should attempt to get the ID of an uninitialized or stopped | |
| 127 // service. | |
| 128 return invalidator_storage_->GetInvalidatorClientId(); | |
| 129 } | |
| 130 | |
| 131 void TiclInvalidationService::Observe( | |
| 132 int type, | |
| 133 const content::NotificationSource& source, | |
| 134 const content::NotificationDetails& details) { | |
| 135 DCHECK(CalledOnValidThread()); | |
| 136 | |
| 137 switch (type) { | |
| 138 case chrome::NOTIFICATION_TOKEN_AVAILABLE: { | |
| 139 const TokenService::TokenAvailableDetails& token_details = | |
| 140 *(content::Details<const TokenService::TokenAvailableDetails>( | |
| 141 details).ptr()); | |
| 142 if (token_details.service() == GaiaConstants::kSyncService) { | |
| 143 DCHECK(IsReadyToStart()); | |
| 144 if (!IsStarted()) { | |
| 145 Start(); | |
| 146 } else { | |
| 147 UpdateToken(); | |
| 148 } | |
| 149 } | |
| 150 break; | |
| 151 } | |
| 152 case chrome::NOTIFICATION_GOOGLE_SIGNED_OUT: { | |
| 153 Stop(); | |
| 154 break; | |
| 155 } | |
| 156 default: { | |
| 157 NOTREACHED(); | |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 void TiclInvalidationService::OnInvalidatorStateChange( | |
| 163 syncer::InvalidatorState state) { | |
| 164 invalidator_registrar_->UpdateInvalidatorState(state); | |
| 165 } | |
| 166 | |
| 167 void TiclInvalidationService::OnIncomingInvalidation( | |
| 168 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 169 invalidator_registrar_->DispatchInvalidationsToHandlers(invalidation_map); | |
| 170 } | |
| 171 | |
| 172 void TiclInvalidationService::Shutdown() { | |
| 173 DCHECK(CalledOnValidThread()); | |
| 174 Stop(); | |
| 175 invalidator_registrar_.reset(); | |
| 176 } | |
| 177 | |
| 178 bool TiclInvalidationService::IsReadyToStart() { | |
| 179 if (signin_manager_->GetAuthenticatedUsername().empty()) { | |
| 180 DVLOG(2) << "Not starting TiclInvalidationService: user is not signed in."; | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 if (!token_service_) { | |
| 185 DVLOG(2) | |
| 186 << "Not starting TiclInvalidationService: TokenService unavailable."; | |
| 187 return false; | |
| 188 } | |
| 189 | |
| 190 if (!token_service_->HasTokenForService(GaiaConstants::kSyncService)) { | |
| 191 DVLOG(2) << "Not starting TiclInvalidationService: Sync token unavailable."; | |
| 192 return false; | |
| 193 } | |
| 194 | |
| 195 return true; | |
| 196 } | |
| 197 | |
| 198 bool TiclInvalidationService::IsStarted() { | |
| 199 return invalidator_.get() != NULL; | |
| 200 } | |
| 201 | |
| 202 void TiclInvalidationService::Start() { | |
| 203 DCHECK(CalledOnValidThread()); | |
| 204 DCHECK(!invalidator_); | |
| 205 | |
| 206 notifier::NotifierOptions options = | |
| 207 ParseNotifierOptions(*CommandLine::ForCurrentProcess()); | |
| 208 options.request_context_getter = profile_->GetRequestContext(); | |
| 209 invalidator_.reset(new syncer::NonBlockingInvalidator( | |
| 210 options, | |
| 211 invalidator_storage_->GetInvalidatorClientId(), | |
| 212 invalidator_storage_->GetAllInvalidationStates(), | |
| 213 invalidator_storage_->GetBootstrapData(), | |
| 214 syncer::WeakHandle<syncer::InvalidationStateTracker>( | |
| 215 invalidator_storage_->AsWeakPtr()), | |
| 216 content::GetUserAgent(GURL()))); | |
| 217 | |
| 218 UpdateToken(); | |
| 219 | |
| 220 invalidator_->RegisterHandler(this); | |
| 221 invalidator_->UpdateRegisteredIds( | |
| 222 this, | |
| 223 invalidator_registrar_->GetAllRegisteredIds()); | |
| 224 } | |
| 225 | |
| 226 void TiclInvalidationService::UpdateToken() { | |
| 227 std::string email = signin_manager_->GetAuthenticatedUsername(); | |
| 228 DCHECK(!email.empty()) << "Expected user to be signed in."; | |
| 229 DCHECK(token_service_->HasTokenForService(GaiaConstants::kSyncService)); | |
| 230 | |
| 231 std::string sync_token = token_service_->GetTokenForService( | |
| 232 GaiaConstants::kSyncService); | |
| 233 | |
| 234 DVLOG(2) << "UpdateCredentials: " << email; | |
| 235 invalidator_->UpdateCredentials(email, sync_token); | |
| 236 } | |
| 237 | |
| 238 void TiclInvalidationService::Stop() { | |
| 239 if (invalidator_) { | |
| 240 invalidator_->UnregisterHandler(this); | |
| 241 invalidator_.reset(); | |
| 242 } | |
| 243 if (invalidator_storage_) { | |
| 244 invalidator_storage_->Clear(); | |
| 245 invalidator_storage_.reset(); | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 } // namespace invalidation | |
| OLD | NEW |