Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / fake_profile_oauth2_token_service.cc
blob8c7d3f39490f7b60c84ccda498dee7d3acd5b271
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"
6 #include "chrome/browser/signin/signin_account_id_helper.h"
8 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
11 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
14 // static
15 BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
16 content::BrowserContext* profile) {
17 FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
18 service->Initialize(reinterpret_cast<Profile*>(profile));
19 return service;
22 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() {
23 SigninAccountIdHelper::SetDisableForTest(true);
26 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
27 SigninAccountIdHelper::SetDisableForTest(false);
30 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
31 const std::string& account_id) {
32 return !GetRefreshToken(account_id).empty();
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 if (token.empty()) {
59 refresh_tokens_.erase(account_id);
60 FireRefreshTokenRevoked(account_id);
61 } else {
62 refresh_tokens_[account_id] = token;
63 FireRefreshTokenAvailable(account_id);
64 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
68 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
69 const std::string& account_id,
70 const std::string& access_token,
71 const base::Time& expiration) {
72 CompleteRequests(account_id,
73 true,
74 ScopeSet(),
75 GoogleServiceAuthError::AuthErrorNone(),
76 access_token,
77 expiration);
80 void FakeProfileOAuth2TokenService::IssueTokenForScope(
81 const ScopeSet& scope,
82 const std::string& access_token,
83 const base::Time& expiration) {
84 CompleteRequests("",
85 false,
86 scope,
87 GoogleServiceAuthError::AuthErrorNone(),
88 access_token,
89 expiration);
92 void FakeProfileOAuth2TokenService::IssueErrorForScope(
93 const ScopeSet& scope,
94 const GoogleServiceAuthError& error) {
95 CompleteRequests("", false, scope, error, std::string(), base::Time());
98 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
99 const GoogleServiceAuthError& error) {
100 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
103 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
104 const std::string& access_token,
105 const base::Time& expiration) {
106 CompleteRequests("",
107 true,
108 ScopeSet(),
109 GoogleServiceAuthError::AuthErrorNone(),
110 access_token,
111 expiration);
114 void FakeProfileOAuth2TokenService::CompleteRequests(
115 const std::string& account_id,
116 bool all_scopes,
117 const ScopeSet& scope,
118 const GoogleServiceAuthError& error,
119 const std::string& access_token,
120 const base::Time& expiration) {
121 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
122 GetPendingRequests();
124 // Walk the requests and notify the callbacks.
125 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
126 it != pending_requests_.end(); ++it) {
127 if (!it->request)
128 continue;
130 bool scope_matches = all_scopes || it->scopes == scope;
131 bool account_matches = account_id.empty() || account_id == it->account_id;
132 if (account_matches && scope_matches)
133 it->request->InformConsumer(error, access_token, expiration);
137 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
138 const std::string& account_id) {
139 return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] :
140 std::string();
143 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
144 FakeProfileOAuth2TokenService::GetPendingRequests() {
145 std::vector<PendingRequest> valid_requests;
146 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
147 it != pending_requests_.end(); ++it) {
148 if (it->request)
149 valid_requests.push_back(*it);
151 return valid_requests;
154 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
155 RequestImpl* request,
156 const std::string& account_id,
157 net::URLRequestContextGetter* getter,
158 const std::string& client_id,
159 const std::string& client_secret,
160 const ScopeSet& scopes) {
161 PendingRequest pending_request;
162 pending_request.account_id = account_id;
163 pending_request.client_id = client_id;
164 pending_request.client_secret = client_secret;
165 pending_request.scopes = scopes;
166 pending_request.request = request->AsWeakPtr();
167 pending_requests_.push_back(pending_request);
170 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
171 const std::string& account_id,
172 const std::string& client_id,
173 const ScopeSet& scopes,
174 const std::string& access_token) {
175 // Do nothing, as we don't have a cache from which to remove the token.