| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/location.h" | 6 #include "base/location.h" |
| 7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
| 8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
| 9 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" | 9 #include "chrome/browser/invalidation/gcm_invalidation_bridge.h" |
| 10 #include "chrome/browser/invalidation/invalidation_auth_provider.h" |
| 10 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 11 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 11 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 12 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 14 #include "chrome/browser/signin/signin_manager.h" | 14 #include "chrome/browser/signin/signin_manager.h" |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "google_apis/gaia/gaia_constants.h" | 16 #include "google_apis/gaia/gaia_constants.h" |
| 17 | 17 |
| 18 namespace invalidation { | 18 namespace invalidation { |
| 19 namespace { | 19 namespace { |
| 20 // For 3rd party developers SenderId should come from application dashboard when | 20 // For 3rd party developers SenderId should come from application dashboard when |
| 21 // server side application is registered with Google. Android invalidations use | 21 // server side application is registered with Google. Android invalidations use |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 callback.Run(registration_id, result); | 140 callback.Run(registration_id, result); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void GCMInvalidationBridge::Core::OnIncomingMessage( | 143 void GCMInvalidationBridge::Core::OnIncomingMessage( |
| 144 const std::string& message, | 144 const std::string& message, |
| 145 const std::string& echo_token) { | 145 const std::string& echo_token) { |
| 146 DCHECK(!message_callback_.is_null()); | 146 DCHECK(!message_callback_.is_null()); |
| 147 message_callback_.Run(message, echo_token); | 147 message_callback_.Run(message, echo_token); |
| 148 } | 148 } |
| 149 | 149 |
| 150 GCMInvalidationBridge::GCMInvalidationBridge(Profile* profile) | 150 GCMInvalidationBridge::GCMInvalidationBridge( |
| 151 gcm::GCMProfileService* gcm_profile_service, |
| 152 InvalidationAuthProvider* auth_provider) |
| 151 : OAuth2TokenService::Consumer("gcm_network_channel"), | 153 : OAuth2TokenService::Consumer("gcm_network_channel"), |
| 152 profile_(profile), | 154 gcm_profile_service_(gcm_profile_service), |
| 153 subscribed_for_incoming_messages_(false), | 155 auth_provider_(auth_provider), |
| 154 weak_factory_(this) {} | 156 weak_factory_(this) {} |
| 155 | 157 |
| 156 GCMInvalidationBridge::~GCMInvalidationBridge() { | 158 GCMInvalidationBridge::~GCMInvalidationBridge() { |
| 157 if (subscribed_for_incoming_messages_) { | 159 if (subscribed_for_incoming_messages_) |
| 158 gcm::GCMProfileService* gcm_profile_service = | 160 gcm_profile_service_->RemoveAppHandler(kInvalidationsAppId); |
| 159 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 160 DCHECK(gcm_profile_service); | |
| 161 | |
| 162 gcm_profile_service->RemoveAppHandler(kInvalidationsAppId); | |
| 163 } | |
| 164 } | 161 } |
| 165 | 162 |
| 166 scoped_ptr<syncer::GCMNetworkChannelDelegate> | 163 scoped_ptr<syncer::GCMNetworkChannelDelegate> |
| 167 GCMInvalidationBridge::CreateDelegate() { | 164 GCMInvalidationBridge::CreateDelegate() { |
| 168 DCHECK(CalledOnValidThread()); | 165 DCHECK(CalledOnValidThread()); |
| 169 scoped_ptr<syncer::GCMNetworkChannelDelegate> core(new Core( | 166 scoped_ptr<syncer::GCMNetworkChannelDelegate> core(new Core( |
| 170 weak_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get())); | 167 weak_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get())); |
| 171 return core.Pass(); | 168 return core.Pass(); |
| 172 } | 169 } |
| 173 | 170 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 187 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 184 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 188 std::string access_token; | 185 std::string access_token; |
| 189 core_thread_task_runner_->PostTask( | 186 core_thread_task_runner_->PostTask( |
| 190 FROM_HERE, | 187 FROM_HERE, |
| 191 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, | 188 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, |
| 192 core_, | 189 core_, |
| 193 request_token_callback_, | 190 request_token_callback_, |
| 194 error, | 191 error, |
| 195 access_token)); | 192 access_token)); |
| 196 } | 193 } |
| 197 ProfileOAuth2TokenService* token_service = | |
| 198 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 199 request_token_callback_ = callback; | 194 request_token_callback_ = callback; |
| 200 SigninManagerBase* signin_manager = | |
| 201 SigninManagerFactory::GetForProfile(profile_); | |
| 202 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 203 OAuth2TokenService::ScopeSet scopes; | 195 OAuth2TokenService::ScopeSet scopes; |
| 204 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 196 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 205 access_token_request_ = token_service->StartRequest(account_id, scopes, this); | 197 access_token_request_ = auth_provider_->GetTokenService()->StartRequest( |
| 198 auth_provider_->GetAccountId(), scopes, this); |
| 206 } | 199 } |
| 207 | 200 |
| 208 void GCMInvalidationBridge::OnGetTokenSuccess( | 201 void GCMInvalidationBridge::OnGetTokenSuccess( |
| 209 const OAuth2TokenService::Request* request, | 202 const OAuth2TokenService::Request* request, |
| 210 const std::string& access_token, | 203 const std::string& access_token, |
| 211 const base::Time& expiration_time) { | 204 const base::Time& expiration_time) { |
| 212 DCHECK(CalledOnValidThread()); | 205 DCHECK(CalledOnValidThread()); |
| 213 DCHECK_EQ(access_token_request_, request); | 206 DCHECK_EQ(access_token_request_, request); |
| 214 core_thread_task_runner_->PostTask( | 207 core_thread_task_runner_->PostTask( |
| 215 FROM_HERE, | 208 FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 232 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, | 225 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, |
| 233 core_, | 226 core_, |
| 234 request_token_callback_, | 227 request_token_callback_, |
| 235 error, | 228 error, |
| 236 std::string())); | 229 std::string())); |
| 237 request_token_callback_.Reset(); | 230 request_token_callback_.Reset(); |
| 238 access_token_request_.reset(); | 231 access_token_request_.reset(); |
| 239 } | 232 } |
| 240 | 233 |
| 241 void GCMInvalidationBridge::InvalidateToken(const std::string& token) { | 234 void GCMInvalidationBridge::InvalidateToken(const std::string& token) { |
| 242 ProfileOAuth2TokenService* token_service = | |
| 243 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 244 DCHECK(CalledOnValidThread()); | 235 DCHECK(CalledOnValidThread()); |
| 245 SigninManagerBase* signin_manager = | |
| 246 SigninManagerFactory::GetForProfile(profile_); | |
| 247 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 248 OAuth2TokenService::ScopeSet scopes; | 236 OAuth2TokenService::ScopeSet scopes; |
| 249 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 237 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 250 token_service->InvalidateToken(account_id, scopes, token); | 238 auth_provider_->GetTokenService()->InvalidateToken( |
| 239 auth_provider_->GetAccountId(), scopes, token); |
| 251 } | 240 } |
| 252 | 241 |
| 253 void GCMInvalidationBridge::Register( | 242 void GCMInvalidationBridge::Register( |
| 254 syncer::GCMNetworkChannelDelegate::RegisterCallback callback) { | 243 syncer::GCMNetworkChannelDelegate::RegisterCallback callback) { |
| 255 DCHECK(CalledOnValidThread()); | 244 DCHECK(CalledOnValidThread()); |
| 256 // No-op if GCMClient is disabled. | 245 // No-op if GCMClient is disabled. |
| 257 gcm::GCMProfileService* gcm_profile_service = | 246 if (gcm_profile_service_ == NULL) |
| 258 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 259 if (gcm_profile_service == NULL) | |
| 260 return; | 247 return; |
| 261 | 248 |
| 262 std::vector<std::string> sender_ids; | 249 std::vector<std::string> sender_ids; |
| 263 sender_ids.push_back(kInvalidationsSenderId); | 250 sender_ids.push_back(kInvalidationsSenderId); |
| 264 gcm_profile_service->Register( | 251 gcm_profile_service_->Register( |
| 265 kInvalidationsAppId, | 252 kInvalidationsAppId, |
| 266 sender_ids, | 253 sender_ids, |
| 267 base::Bind(&GCMInvalidationBridge::RegisterFinished, | 254 base::Bind(&GCMInvalidationBridge::RegisterFinished, |
| 268 weak_factory_.GetWeakPtr(), | 255 weak_factory_.GetWeakPtr(), |
| 269 callback)); | 256 callback)); |
| 270 } | 257 } |
| 271 | 258 |
| 272 void GCMInvalidationBridge::RegisterFinished( | 259 void GCMInvalidationBridge::RegisterFinished( |
| 273 syncer::GCMNetworkChannelDelegate::RegisterCallback callback, | 260 syncer::GCMNetworkChannelDelegate::RegisterCallback callback, |
| 274 const std::string& registration_id, | 261 const std::string& registration_id, |
| 275 gcm::GCMClient::Result result) { | 262 gcm::GCMClient::Result result) { |
| 276 DCHECK(CalledOnValidThread()); | 263 DCHECK(CalledOnValidThread()); |
| 277 core_thread_task_runner_->PostTask( | 264 core_thread_task_runner_->PostTask( |
| 278 FROM_HERE, | 265 FROM_HERE, |
| 279 base::Bind(&GCMInvalidationBridge::Core::RegisterFinished, | 266 base::Bind(&GCMInvalidationBridge::Core::RegisterFinished, |
| 280 core_, | 267 core_, |
| 281 callback, | 268 callback, |
| 282 registration_id, | 269 registration_id, |
| 283 result)); | 270 result)); |
| 284 } | 271 } |
| 285 | 272 |
| 286 void GCMInvalidationBridge::SubscribeForIncomingMessages() { | 273 void GCMInvalidationBridge::SubscribeForIncomingMessages() { |
| 287 // No-op if GCMClient is disabled. | 274 // No-op if GCMClient is disabled. |
| 288 gcm::GCMProfileService* gcm_profile_service = | 275 if (gcm_profile_service_ == NULL) |
| 289 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 290 if (gcm_profile_service == NULL) | |
| 291 return; | 276 return; |
| 292 | 277 |
| 293 DCHECK(!subscribed_for_incoming_messages_); | 278 DCHECK(!subscribed_for_incoming_messages_); |
| 294 gcm_profile_service->AddAppHandler(kInvalidationsAppId, this); | 279 gcm_profile_service_->AddAppHandler(kInvalidationsAppId, this); |
| 295 subscribed_for_incoming_messages_ = true; | 280 subscribed_for_incoming_messages_ = true; |
| 296 } | 281 } |
| 297 | 282 |
| 298 void GCMInvalidationBridge::ShutdownHandler() { | 283 void GCMInvalidationBridge::ShutdownHandler() { |
| 299 // Nothing to do. | 284 // Nothing to do. |
| 300 } | 285 } |
| 301 | 286 |
| 302 void GCMInvalidationBridge::OnMessage( | 287 void GCMInvalidationBridge::OnMessage( |
| 303 const std::string& app_id, | 288 const std::string& app_id, |
| 304 const gcm::GCMClient::IncomingMessage& message) { | 289 const gcm::GCMClient::IncomingMessage& message) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 329 } | 314 } |
| 330 | 315 |
| 331 void GCMInvalidationBridge::OnSendError( | 316 void GCMInvalidationBridge::OnSendError( |
| 332 const std::string& app_id, | 317 const std::string& app_id, |
| 333 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 318 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
| 334 // cacheinvalidation doesn't send messages over GCM. | 319 // cacheinvalidation doesn't send messages over GCM. |
| 335 NOTREACHED(); | 320 NOTREACHED(); |
| 336 } | 321 } |
| 337 | 322 |
| 338 } // namespace invalidation | 323 } // namespace invalidation |
| OLD | NEW |