[Sync] Rename PSS::IsSyncEnabled to PSS::IsSyncAllowedByFlag.
[chromium-blink-merge.git] / chrome / browser / signin / fake_profile_oauth2_token_service.cc
blob6901cdf4d890b840551abeee04f0f9545a0b46a0
1 // Copyright 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.
5 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
16 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService()
17 : auto_post_fetch_response_on_message_loop_(false),
18 weak_ptr_factory_(this) {
21 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
24 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
25 const std::string& account_id) const {
26 return !GetRefreshToken(account_id).empty();
29 void FakeProfileOAuth2TokenService::LoadCredentials(
30 const std::string& primary_account_id) {
31 // Empty implementation as FakeProfileOAuth2TokenService does not have any
32 // credentials to load.
35 std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() {
36 std::vector<std::string> account_ids;
37 for (std::map<std::string, std::string>::const_iterator iter =
38 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) {
39 account_ids.push_back(iter->first);
41 return account_ids;
44 void FakeProfileOAuth2TokenService::UpdateCredentials(
45 const std::string& account_id,
46 const std::string& refresh_token) {
47 IssueRefreshTokenForUser(account_id, refresh_token);
50 void FakeProfileOAuth2TokenService::IssueRefreshToken(
51 const std::string& token) {
52 IssueRefreshTokenForUser("account_id", token);
55 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
56 const std::string& account_id,
57 const std::string& token) {
58 ScopedBatchChange batch(this);
59 if (token.empty()) {
60 refresh_tokens_.erase(account_id);
61 FireRefreshTokenRevoked(account_id);
62 } else {
63 refresh_tokens_[account_id] = token;
64 FireRefreshTokenAvailable(account_id);
65 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
69 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() {
70 FireRefreshTokensLoaded();
73 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
74 const std::string& account_id,
75 const std::string& access_token,
76 const base::Time& expiration) {
77 CompleteRequests(account_id,
78 true,
79 ScopeSet(),
80 GoogleServiceAuthError::AuthErrorNone(),
81 access_token,
82 expiration);
85 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
86 const std::string& account_id,
87 const GoogleServiceAuthError& error) {
88 CompleteRequests(account_id,
89 true,
90 ScopeSet(),
91 error,
92 std::string(),
93 base::Time());
96 void FakeProfileOAuth2TokenService::IssueTokenForScope(
97 const ScopeSet& scope,
98 const std::string& access_token,
99 const base::Time& expiration) {
100 CompleteRequests("",
101 false,
102 scope,
103 GoogleServiceAuthError::AuthErrorNone(),
104 access_token,
105 expiration);
108 void FakeProfileOAuth2TokenService::IssueErrorForScope(
109 const ScopeSet& scope,
110 const GoogleServiceAuthError& error) {
111 CompleteRequests("", false, scope, error, std::string(), base::Time());
114 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
115 const GoogleServiceAuthError& error) {
116 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
119 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
120 const std::string& access_token,
121 const base::Time& expiration) {
122 CompleteRequests("",
123 true,
124 ScopeSet(),
125 GoogleServiceAuthError::AuthErrorNone(),
126 access_token,
127 expiration);
130 void FakeProfileOAuth2TokenService::CompleteRequests(
131 const std::string& account_id,
132 bool all_scopes,
133 const ScopeSet& scope,
134 const GoogleServiceAuthError& error,
135 const std::string& access_token,
136 const base::Time& expiration) {
137 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
138 GetPendingRequests();
140 // Walk the requests and notify the callbacks.
141 for (std::vector<PendingRequest>::iterator it = requests.begin();
142 it != requests.end(); ++it) {
143 DCHECK(it->request);
145 bool scope_matches = all_scopes || it->scopes == scope;
146 bool account_matches = account_id.empty() || account_id == it->account_id;
147 if (account_matches && scope_matches)
148 it->request->InformConsumer(error, access_token, expiration);
152 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
153 const std::string& account_id) const {
154 std::map<std::string, std::string>::const_iterator it =
155 refresh_tokens_.find(account_id);
156 if (it != refresh_tokens_.end())
157 return it->second;
158 return std::string();
161 net::URLRequestContextGetter*
162 FakeProfileOAuth2TokenService::GetRequestContext() {
163 return NULL;
166 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
167 FakeProfileOAuth2TokenService::GetPendingRequests() {
168 std::vector<PendingRequest> valid_requests;
169 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
170 it != pending_requests_.end(); ++it) {
171 if (it->request)
172 valid_requests.push_back(*it);
174 return valid_requests;
177 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
178 RequestImpl* request,
179 const std::string& account_id,
180 net::URLRequestContextGetter* getter,
181 const std::string& client_id,
182 const std::string& client_secret,
183 const ScopeSet& scopes) {
184 PendingRequest pending_request;
185 pending_request.account_id = account_id;
186 pending_request.client_id = client_id;
187 pending_request.client_secret = client_secret;
188 pending_request.scopes = scopes;
189 pending_request.request = request->AsWeakPtr();
190 pending_requests_.push_back(pending_request);
192 if (auto_post_fetch_response_on_message_loop_) {
193 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
194 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount,
195 weak_ptr_factory_.GetWeakPtr(),
196 account_id,
197 "access_token",
198 base::Time::Max()));
202 OAuth2AccessTokenFetcher*
203 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
204 const std::string& account_id,
205 net::URLRequestContextGetter* getter,
206 OAuth2AccessTokenConsumer* consumer) {
207 NOTREACHED();
208 return NULL;
211 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
212 const std::string& account_id,
213 const std::string& client_id,
214 const ScopeSet& scopes,
215 const std::string& access_token) {
216 // Do nothing, as we don't have a cache from which to remove the token.