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/message_loop/message_loop.h"
8 #include "chrome/browser/signin/signin_account_id_helper.h"
10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
17 BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
18 content::BrowserContext* profile) {
19 FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
20 service->Initialize(reinterpret_cast<Profile*>(profile));
24 BrowserContextKeyedService*
25 FakeProfileOAuth2TokenService::BuildAutoIssuingTokenService(
26 content::BrowserContext* profile) {
27 FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
28 service->set_auto_post_fetch_response_on_message_loop(true);
29 service->Initialize(reinterpret_cast<Profile*>(profile));
33 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService()
34 : auto_post_fetch_response_on_message_loop_(false) {
35 SigninAccountIdHelper::SetDisableForTest(true);
38 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
39 SigninAccountIdHelper::SetDisableForTest(false);
42 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
43 const std::string& account_id) {
44 return !GetRefreshToken(account_id).empty();
47 std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() {
48 std::vector<std::string> account_ids;
49 for (std::map<std::string, std::string>::const_iterator iter =
50 refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) {
51 account_ids.push_back(iter->first);
56 void FakeProfileOAuth2TokenService::UpdateCredentials(
57 const std::string& account_id,
58 const std::string& refresh_token) {
59 IssueRefreshTokenForUser(account_id, refresh_token);
62 void FakeProfileOAuth2TokenService::IssueRefreshToken(
63 const std::string& token) {
64 IssueRefreshTokenForUser("account_id", token);
67 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
68 const std::string& account_id,
69 const std::string& token) {
71 refresh_tokens_.erase(account_id);
72 FireRefreshTokenRevoked(account_id);
74 refresh_tokens_[account_id] = token;
75 FireRefreshTokenAvailable(account_id);
76 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
80 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
81 const std::string& account_id,
82 const std::string& access_token,
83 const base::Time& expiration) {
84 CompleteRequests(account_id,
87 GoogleServiceAuthError::AuthErrorNone(),
92 void FakeProfileOAuth2TokenService::IssueTokenForScope(
93 const ScopeSet& scope,
94 const std::string& access_token,
95 const base::Time& expiration) {
99 GoogleServiceAuthError::AuthErrorNone(),
104 void FakeProfileOAuth2TokenService::IssueErrorForScope(
105 const ScopeSet& scope,
106 const GoogleServiceAuthError& error) {
107 CompleteRequests("", false, scope, error, std::string(), base::Time());
110 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
111 const GoogleServiceAuthError& error) {
112 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
115 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
116 const std::string& access_token,
117 const base::Time& expiration) {
121 GoogleServiceAuthError::AuthErrorNone(),
126 void FakeProfileOAuth2TokenService::CompleteRequests(
127 const std::string& account_id,
129 const ScopeSet& scope,
130 const GoogleServiceAuthError& error,
131 const std::string& access_token,
132 const base::Time& expiration) {
133 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
134 GetPendingRequests();
136 // Walk the requests and notify the callbacks.
137 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
138 it != pending_requests_.end(); ++it) {
142 bool scope_matches = all_scopes || it->scopes == scope;
143 bool account_matches = account_id.empty() || account_id == it->account_id;
144 if (account_matches && scope_matches)
145 it->request->InformConsumer(error, access_token, expiration);
149 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
150 const std::string& account_id) {
151 return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] :
155 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
156 FakeProfileOAuth2TokenService::GetPendingRequests() {
157 std::vector<PendingRequest> valid_requests;
158 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
159 it != pending_requests_.end(); ++it) {
161 valid_requests.push_back(*it);
163 return valid_requests;
166 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
167 RequestImpl* request,
168 const std::string& account_id,
169 net::URLRequestContextGetter* getter,
170 const std::string& client_id,
171 const std::string& client_secret,
172 const ScopeSet& scopes) {
173 PendingRequest pending_request;
174 pending_request.account_id = account_id;
175 pending_request.client_id = client_id;
176 pending_request.client_secret = client_secret;
177 pending_request.scopes = scopes;
178 pending_request.request = request->AsWeakPtr();
179 pending_requests_.push_back(pending_request);
181 if (auto_post_fetch_response_on_message_loop_) {
182 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
183 &FakeProfileOAuth2TokenService::IssueAllTokensForAccount,
184 base::Unretained(this),
191 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
192 const std::string& account_id,
193 const std::string& client_id,
194 const ScopeSet& scopes,
195 const std::string& access_token) {
196 // Do nothing, as we don't have a cache from which to remove the token.