| 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_factory.h" | 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager.h" | 13 #include "chrome/browser/signin/signin_manager.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 14 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "components/signin/core/profile_oauth2_token_service.h" | 15 #include "components/signin/core/profile_oauth2_token_service.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), |
| 155 auth_provider_(auth_provider), |
| 153 subscribed_for_incoming_messages_(false), | 156 subscribed_for_incoming_messages_(false), |
| 154 weak_factory_(this) {} | 157 weak_factory_(this) {} |
| 155 | 158 |
| 156 GCMInvalidationBridge::~GCMInvalidationBridge() { | 159 GCMInvalidationBridge::~GCMInvalidationBridge() { |
| 157 if (subscribed_for_incoming_messages_) { | 160 if (subscribed_for_incoming_messages_) |
| 158 gcm::GCMProfileService* gcm_profile_service = | 161 gcm_profile_service_->RemoveAppHandler(kInvalidationsAppId); |
| 159 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 160 DCHECK(gcm_profile_service); | |
| 161 | |
| 162 gcm_profile_service->RemoveAppHandler(kInvalidationsAppId); | |
| 163 } | |
| 164 } | 162 } |
| 165 | 163 |
| 166 scoped_ptr<syncer::GCMNetworkChannelDelegate> | 164 scoped_ptr<syncer::GCMNetworkChannelDelegate> |
| 167 GCMInvalidationBridge::CreateDelegate() { | 165 GCMInvalidationBridge::CreateDelegate() { |
| 168 DCHECK(CalledOnValidThread()); | 166 DCHECK(CalledOnValidThread()); |
| 169 scoped_ptr<syncer::GCMNetworkChannelDelegate> core(new Core( | 167 scoped_ptr<syncer::GCMNetworkChannelDelegate> core(new Core( |
| 170 weak_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get())); | 168 weak_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get())); |
| 171 return core.Pass(); | 169 return core.Pass(); |
| 172 } | 170 } |
| 173 | 171 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 187 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 185 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 188 std::string access_token; | 186 std::string access_token; |
| 189 core_thread_task_runner_->PostTask( | 187 core_thread_task_runner_->PostTask( |
| 190 FROM_HERE, | 188 FROM_HERE, |
| 191 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, | 189 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, |
| 192 core_, | 190 core_, |
| 193 request_token_callback_, | 191 request_token_callback_, |
| 194 error, | 192 error, |
| 195 access_token)); | 193 access_token)); |
| 196 } | 194 } |
| 197 ProfileOAuth2TokenService* token_service = | |
| 198 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 199 request_token_callback_ = callback; | 195 request_token_callback_ = callback; |
| 200 SigninManagerBase* signin_manager = | |
| 201 SigninManagerFactory::GetForProfile(profile_); | |
| 202 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 203 OAuth2TokenService::ScopeSet scopes; | 196 OAuth2TokenService::ScopeSet scopes; |
| 204 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 197 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 205 access_token_request_ = token_service->StartRequest(account_id, scopes, this); | 198 access_token_request_ = auth_provider_->GetTokenService()->StartRequest( |
| 199 auth_provider_->GetAccountId(), scopes, this); |
| 206 } | 200 } |
| 207 | 201 |
| 208 void GCMInvalidationBridge::OnGetTokenSuccess( | 202 void GCMInvalidationBridge::OnGetTokenSuccess( |
| 209 const OAuth2TokenService::Request* request, | 203 const OAuth2TokenService::Request* request, |
| 210 const std::string& access_token, | 204 const std::string& access_token, |
| 211 const base::Time& expiration_time) { | 205 const base::Time& expiration_time) { |
| 212 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
| 213 DCHECK_EQ(access_token_request_, request); | 207 DCHECK_EQ(access_token_request_, request); |
| 214 core_thread_task_runner_->PostTask( | 208 core_thread_task_runner_->PostTask( |
| 215 FROM_HERE, | 209 FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 232 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, | 226 base::Bind(&GCMInvalidationBridge::Core::RequestTokenFinished, |
| 233 core_, | 227 core_, |
| 234 request_token_callback_, | 228 request_token_callback_, |
| 235 error, | 229 error, |
| 236 std::string())); | 230 std::string())); |
| 237 request_token_callback_.Reset(); | 231 request_token_callback_.Reset(); |
| 238 access_token_request_.reset(); | 232 access_token_request_.reset(); |
| 239 } | 233 } |
| 240 | 234 |
| 241 void GCMInvalidationBridge::InvalidateToken(const std::string& token) { | 235 void GCMInvalidationBridge::InvalidateToken(const std::string& token) { |
| 242 ProfileOAuth2TokenService* token_service = | |
| 243 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_); | |
| 244 DCHECK(CalledOnValidThread()); | 236 DCHECK(CalledOnValidThread()); |
| 245 SigninManagerBase* signin_manager = | |
| 246 SigninManagerFactory::GetForProfile(profile_); | |
| 247 std::string account_id = signin_manager->GetAuthenticatedAccountId(); | |
| 248 OAuth2TokenService::ScopeSet scopes; | 237 OAuth2TokenService::ScopeSet scopes; |
| 249 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | 238 scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); |
| 250 token_service->InvalidateToken(account_id, scopes, token); | 239 auth_provider_->GetTokenService()->InvalidateToken( |
| 240 auth_provider_->GetAccountId(), scopes, token); |
| 251 } | 241 } |
| 252 | 242 |
| 253 void GCMInvalidationBridge::Register( | 243 void GCMInvalidationBridge::Register( |
| 254 syncer::GCMNetworkChannelDelegate::RegisterCallback callback) { | 244 syncer::GCMNetworkChannelDelegate::RegisterCallback callback) { |
| 255 DCHECK(CalledOnValidThread()); | 245 DCHECK(CalledOnValidThread()); |
| 256 // No-op if GCMClient is disabled. | 246 // No-op if GCMClient is disabled. |
| 257 gcm::GCMProfileService* gcm_profile_service = | 247 if (gcm_profile_service_ == NULL) |
| 258 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 259 if (gcm_profile_service == NULL) | |
| 260 return; | 248 return; |
| 261 | 249 |
| 262 std::vector<std::string> sender_ids; | 250 std::vector<std::string> sender_ids; |
| 263 sender_ids.push_back(kInvalidationsSenderId); | 251 sender_ids.push_back(kInvalidationsSenderId); |
| 264 gcm_profile_service->Register( | 252 gcm_profile_service_->Register( |
| 265 kInvalidationsAppId, | 253 kInvalidationsAppId, |
| 266 sender_ids, | 254 sender_ids, |
| 267 base::Bind(&GCMInvalidationBridge::RegisterFinished, | 255 base::Bind(&GCMInvalidationBridge::RegisterFinished, |
| 268 weak_factory_.GetWeakPtr(), | 256 weak_factory_.GetWeakPtr(), |
| 269 callback)); | 257 callback)); |
| 270 } | 258 } |
| 271 | 259 |
| 272 void GCMInvalidationBridge::RegisterFinished( | 260 void GCMInvalidationBridge::RegisterFinished( |
| 273 syncer::GCMNetworkChannelDelegate::RegisterCallback callback, | 261 syncer::GCMNetworkChannelDelegate::RegisterCallback callback, |
| 274 const std::string& registration_id, | 262 const std::string& registration_id, |
| 275 gcm::GCMClient::Result result) { | 263 gcm::GCMClient::Result result) { |
| 276 DCHECK(CalledOnValidThread()); | 264 DCHECK(CalledOnValidThread()); |
| 277 core_thread_task_runner_->PostTask( | 265 core_thread_task_runner_->PostTask( |
| 278 FROM_HERE, | 266 FROM_HERE, |
| 279 base::Bind(&GCMInvalidationBridge::Core::RegisterFinished, | 267 base::Bind(&GCMInvalidationBridge::Core::RegisterFinished, |
| 280 core_, | 268 core_, |
| 281 callback, | 269 callback, |
| 282 registration_id, | 270 registration_id, |
| 283 result)); | 271 result)); |
| 284 } | 272 } |
| 285 | 273 |
| 286 void GCMInvalidationBridge::SubscribeForIncomingMessages() { | 274 void GCMInvalidationBridge::SubscribeForIncomingMessages() { |
| 287 // No-op if GCMClient is disabled. | 275 // No-op if GCMClient is disabled. |
| 288 gcm::GCMProfileService* gcm_profile_service = | 276 if (gcm_profile_service_ == NULL) |
| 289 gcm::GCMProfileServiceFactory::GetForProfile(profile_); | |
| 290 if (gcm_profile_service == NULL) | |
| 291 return; | 277 return; |
| 292 | 278 |
| 293 DCHECK(!subscribed_for_incoming_messages_); | 279 DCHECK(!subscribed_for_incoming_messages_); |
| 294 gcm_profile_service->AddAppHandler(kInvalidationsAppId, this); | 280 gcm_profile_service_->AddAppHandler(kInvalidationsAppId, this); |
| 295 subscribed_for_incoming_messages_ = true; | 281 subscribed_for_incoming_messages_ = true; |
| 296 } | 282 } |
| 297 | 283 |
| 298 void GCMInvalidationBridge::ShutdownHandler() { | 284 void GCMInvalidationBridge::ShutdownHandler() { |
| 299 // Nothing to do. | 285 // Nothing to do. |
| 300 } | 286 } |
| 301 | 287 |
| 302 void GCMInvalidationBridge::OnMessage( | 288 void GCMInvalidationBridge::OnMessage( |
| 303 const std::string& app_id, | 289 const std::string& app_id, |
| 304 const gcm::GCMClient::IncomingMessage& message) { | 290 const gcm::GCMClient::IncomingMessage& message) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 329 } | 315 } |
| 330 | 316 |
| 331 void GCMInvalidationBridge::OnSendError( | 317 void GCMInvalidationBridge::OnSendError( |
| 332 const std::string& app_id, | 318 const std::string& app_id, |
| 333 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 319 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
| 334 // cacheinvalidation doesn't send messages over GCM. | 320 // cacheinvalidation doesn't send messages over GCM. |
| 335 NOTREACHED(); | 321 NOTREACHED(); |
| 336 } | 322 } |
| 337 | 323 |
| 338 } // namespace invalidation | 324 } // namespace invalidation |
| OLD | NEW |