profile_identity_provider.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  3. // found in the LICENSE file.
  4. #include "components/invalidation/impl/profile_identity_provider.h"
  5. #include "base/bind.h"
  6. #include "components/signin/public/identity_manager/access_token_info.h"
  7. namespace invalidation {
  8. namespace {
  9. // ActiveAccountAccessTokenFetcher implementation that is backed by
  10. // IdentityManager and wraps an AccessTokenFetcher internally.
  11. class AccessTokenFetcherAdaptor : public ActiveAccountAccessTokenFetcher {
  12. public:
  13. AccessTokenFetcherAdaptor(const CoreAccountId& active_account_id,
  14. const std::string& oauth_consumer_name,
  15. signin::IdentityManager* identity_manager,
  16. const signin::ScopeSet& scopes,
  17. ActiveAccountAccessTokenCallback callback);
  18. AccessTokenFetcherAdaptor(const AccessTokenFetcherAdaptor& other) = delete;
  19. AccessTokenFetcherAdaptor& operator=(const AccessTokenFetcherAdaptor& other) =
  20. delete;
  21. ~AccessTokenFetcherAdaptor() override = default;
  22. private:
  23. // Invokes |callback_| with (|error|, |access_token_info.token|).
  24. void HandleTokenRequestCompletion(GoogleServiceAuthError error,
  25. signin::AccessTokenInfo access_token_info);
  26. ActiveAccountAccessTokenCallback callback_;
  27. std::unique_ptr<signin::AccessTokenFetcher> access_token_fetcher_;
  28. };
  29. AccessTokenFetcherAdaptor::AccessTokenFetcherAdaptor(
  30. const CoreAccountId& active_account_id,
  31. const std::string& oauth_consumer_name,
  32. signin::IdentityManager* identity_manager,
  33. const signin::ScopeSet& scopes,
  34. ActiveAccountAccessTokenCallback callback)
  35. : callback_(std::move(callback)) {
  36. access_token_fetcher_ = identity_manager->CreateAccessTokenFetcherForAccount(
  37. active_account_id, oauth_consumer_name, scopes,
  38. base::BindOnce(&AccessTokenFetcherAdaptor::HandleTokenRequestCompletion,
  39. base::Unretained(this)),
  40. signin::AccessTokenFetcher::Mode::kImmediate);
  41. }
  42. void AccessTokenFetcherAdaptor::HandleTokenRequestCompletion(
  43. GoogleServiceAuthError error,
  44. signin::AccessTokenInfo access_token_info) {
  45. access_token_fetcher_.reset();
  46. std::move(callback_).Run(error, access_token_info.token);
  47. }
  48. } // namespace
  49. ProfileIdentityProvider::ProfileIdentityProvider(
  50. signin::IdentityManager* identity_manager)
  51. : identity_manager_(identity_manager) {
  52. identity_manager_->AddObserver(this);
  53. active_account_id_ =
  54. identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin);
  55. }
  56. ProfileIdentityProvider::~ProfileIdentityProvider() {
  57. identity_manager_->RemoveObserver(this);
  58. }
  59. CoreAccountId ProfileIdentityProvider::GetActiveAccountId() {
  60. return active_account_id_;
  61. }
  62. bool ProfileIdentityProvider::IsActiveAccountWithRefreshToken() {
  63. if (GetActiveAccountId().empty() || !identity_manager_ ||
  64. !identity_manager_->HasAccountWithRefreshToken(GetActiveAccountId())) {
  65. return false;
  66. }
  67. return true;
  68. }
  69. std::unique_ptr<ActiveAccountAccessTokenFetcher>
  70. ProfileIdentityProvider::FetchAccessToken(
  71. const std::string& oauth_consumer_name,
  72. const signin::ScopeSet& scopes,
  73. ActiveAccountAccessTokenCallback callback) {
  74. return std::make_unique<AccessTokenFetcherAdaptor>(
  75. GetActiveAccountId(), oauth_consumer_name, identity_manager_, scopes,
  76. std::move(callback));
  77. }
  78. void ProfileIdentityProvider::InvalidateAccessToken(
  79. const signin::ScopeSet& scopes,
  80. const std::string& access_token) {
  81. identity_manager_->RemoveAccessTokenFromCache(GetActiveAccountId(), scopes,
  82. access_token);
  83. }
  84. void ProfileIdentityProvider::OnPrimaryAccountChanged(
  85. const signin::PrimaryAccountChangeEvent& event_details) {
  86. CoreAccountId account_id =
  87. event_details.GetCurrentState().primary_account.account_id;
  88. if (account_id == active_account_id_) {
  89. return;
  90. }
  91. if (!active_account_id_.empty()) {
  92. FireOnActiveAccountLogout();
  93. }
  94. active_account_id_ = account_id;
  95. if (!active_account_id_.empty()) {
  96. FireOnActiveAccountLogin();
  97. }
  98. }
  99. void ProfileIdentityProvider::OnRefreshTokenUpdatedForAccount(
  100. const CoreAccountInfo& account_info) {
  101. ProcessRefreshTokenUpdateForAccount(account_info.account_id);
  102. }
  103. void ProfileIdentityProvider::OnRefreshTokenRemovedForAccount(
  104. const CoreAccountId& account_id) {
  105. ProcessRefreshTokenRemovalForAccount(account_id);
  106. }
  107. } // namespace invalidation