1 // Copyright 2015 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 "components/signin/internal/identity_manager/profile_oauth2_token_service_delegate.h"
6 
7 #include "components/signin/internal/identity_manager/profile_oauth2_token_service_observer.h"
8 #include "google_apis/gaia/oauth2_access_token_consumer.h"
9 #include "services/network/public/cpp/shared_url_loader_factory.h"
10 
ScopedBatchChange(ProfileOAuth2TokenServiceDelegate * delegate)11 ProfileOAuth2TokenServiceDelegate::ScopedBatchChange::ScopedBatchChange(
12     ProfileOAuth2TokenServiceDelegate* delegate)
13     : delegate_(delegate) {
14   DCHECK(delegate_);
15   delegate_->StartBatchChanges();
16 }
17 
~ScopedBatchChange()18 ProfileOAuth2TokenServiceDelegate::ScopedBatchChange::~ScopedBatchChange() {
19   delegate_->EndBatchChanges();
20 }
21 
ProfileOAuth2TokenServiceDelegate()22 ProfileOAuth2TokenServiceDelegate::ProfileOAuth2TokenServiceDelegate()
23     : batch_change_depth_(0) {}
24 
25 ProfileOAuth2TokenServiceDelegate::~ProfileOAuth2TokenServiceDelegate() =
26     default;
27 
ValidateAccountId(const CoreAccountId & account_id) const28 bool ProfileOAuth2TokenServiceDelegate::ValidateAccountId(
29     const CoreAccountId& account_id) const {
30   bool valid = !account_id.empty();
31 
32   // If the account is given as an email, make sure its a canonical email.
33   // Note that some tests don't use email strings as account id, and after
34   // the gaia id migration it won't be an email.  So only check for
35   // canonicalization if the account_id is suspected to be an email.
36   if (account_id.ToString().find('@') != std::string::npos &&
37       gaia::CanonicalizeEmail(account_id.ToString()) != account_id.ToString()) {
38     valid = false;
39   }
40 
41   DCHECK(valid);
42   return valid;
43 }
44 
AddObserver(ProfileOAuth2TokenServiceObserver * observer)45 void ProfileOAuth2TokenServiceDelegate::AddObserver(
46     ProfileOAuth2TokenServiceObserver* observer) {
47   observer_list_.AddObserver(observer);
48 }
49 
RemoveObserver(ProfileOAuth2TokenServiceObserver * observer)50 void ProfileOAuth2TokenServiceDelegate::RemoveObserver(
51     ProfileOAuth2TokenServiceObserver* observer) {
52   observer_list_.RemoveObserver(observer);
53 }
54 
StartBatchChanges()55 void ProfileOAuth2TokenServiceDelegate::StartBatchChanges() {
56   ++batch_change_depth_;
57 }
58 
EndBatchChanges()59 void ProfileOAuth2TokenServiceDelegate::EndBatchChanges() {
60   --batch_change_depth_;
61   DCHECK_LE(0, batch_change_depth_);
62   if (batch_change_depth_ == 0) {
63     for (auto& observer : observer_list_)
64       observer.OnEndBatchChanges();
65   }
66 }
67 
FireRefreshTokenAvailable(const CoreAccountId & account_id)68 void ProfileOAuth2TokenServiceDelegate::FireRefreshTokenAvailable(
69     const CoreAccountId& account_id) {
70   DCHECK(!account_id.empty());
71   for (auto& observer : observer_list_)
72     observer.OnRefreshTokenAvailable(account_id);
73 }
74 
FireRefreshTokenRevoked(const CoreAccountId & account_id)75 void ProfileOAuth2TokenServiceDelegate::FireRefreshTokenRevoked(
76     const CoreAccountId& account_id) {
77   DCHECK(!account_id.empty());
78   for (auto& observer : observer_list_)
79     observer.OnRefreshTokenRevoked(account_id);
80 }
81 
FireRefreshTokensLoaded()82 void ProfileOAuth2TokenServiceDelegate::FireRefreshTokensLoaded() {
83   for (auto& observer : observer_list_)
84     observer.OnRefreshTokensLoaded();
85 }
86 
FireAuthErrorChanged(const CoreAccountId & account_id,const GoogleServiceAuthError & error)87 void ProfileOAuth2TokenServiceDelegate::FireAuthErrorChanged(
88     const CoreAccountId& account_id,
89     const GoogleServiceAuthError& error) {
90   DCHECK(!account_id.empty());
91   for (auto& observer : observer_list_)
92     observer.OnAuthErrorChanged(account_id, error);
93 }
94 
GetTokenForMultilogin(const CoreAccountId & account_id) const95 std::string ProfileOAuth2TokenServiceDelegate::GetTokenForMultilogin(
96     const CoreAccountId& account_id) const {
97   return std::string();
98 }
99 
100 scoped_refptr<network::SharedURLLoaderFactory>
GetURLLoaderFactory() const101 ProfileOAuth2TokenServiceDelegate::GetURLLoaderFactory() const {
102   return nullptr;
103 }
104 
GetAuthError(const CoreAccountId & account_id) const105 GoogleServiceAuthError ProfileOAuth2TokenServiceDelegate::GetAuthError(
106     const CoreAccountId& account_id) const {
107   return GoogleServiceAuthError::AuthErrorNone();
108 }
109 
GetAccounts() const110 std::vector<CoreAccountId> ProfileOAuth2TokenServiceDelegate::GetAccounts()
111     const {
112   return std::vector<CoreAccountId>();
113 }
114 
BackoffEntry() const115 const net::BackoffEntry* ProfileOAuth2TokenServiceDelegate::BackoffEntry()
116     const {
117   return nullptr;
118 }
119 
LoadCredentials(const CoreAccountId & primary_account_id)120 void ProfileOAuth2TokenServiceDelegate::LoadCredentials(
121     const CoreAccountId& primary_account_id) {
122   NOTREACHED()
123       << "ProfileOAuth2TokenServiceDelegate does not load credentials. "
124          "Subclasses that need to load credentials must provide "
125          "an implemenation of this method";
126 }
127 
ExtractCredentials(ProfileOAuth2TokenService * to_service,const CoreAccountId & account_id)128 void ProfileOAuth2TokenServiceDelegate::ExtractCredentials(
129     ProfileOAuth2TokenService* to_service,
130     const CoreAccountId& account_id) {
131   NOTREACHED();
132 }
133 
FixRequestErrorIfPossible()134 bool ProfileOAuth2TokenServiceDelegate::FixRequestErrorIfPossible() {
135   return false;
136 }
137