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 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
10 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
14 BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
15 content::BrowserContext* profile) {
16 FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
17 service->Initialize(reinterpret_cast<Profile*>(profile));
21 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() {
24 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
27 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
28 const std::string& account_id) {
29 return !GetRefreshToken(account_id).empty();
32 void FakeProfileOAuth2TokenService::IssueRefreshToken(
33 const std::string& token) {
34 IssueRefreshTokenForUser("account_id", token);
37 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
38 const std::string& account_id,
39 const std::string& token) {
40 refresh_token_ = token;
41 if (refresh_token_.empty())
42 FireRefreshTokenRevoked(account_id);
44 FireRefreshTokenAvailable(account_id);
45 // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
48 void FakeProfileOAuth2TokenService::IssueTokenForScope(
49 const ScopeSet& scope,
50 const std::string& access_token,
51 const base::Time& expiration) {
52 CompleteRequests(false,
54 GoogleServiceAuthError::AuthErrorNone(),
59 void FakeProfileOAuth2TokenService::IssueErrorForScope(
60 const ScopeSet& scope,
61 const GoogleServiceAuthError& error) {
62 CompleteRequests(false, scope, error, std::string(), base::Time());
65 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
66 const GoogleServiceAuthError& error) {
67 CompleteRequests(true, ScopeSet(), error, std::string(), base::Time());
70 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
71 const std::string& access_token,
72 const base::Time& expiration) {
73 CompleteRequests(true,
75 GoogleServiceAuthError::AuthErrorNone(),
80 void FakeProfileOAuth2TokenService::CompleteRequests(
82 const ScopeSet& scope,
83 const GoogleServiceAuthError& error,
84 const std::string& access_token,
85 const base::Time& expiration) {
86 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
89 // Walk the requests and notify the callbacks.
90 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
91 it != pending_requests_.end(); ++it) {
92 if (it->request && (all_scopes || it->scopes == scope))
93 it->request->InformConsumer(error, access_token, expiration);
97 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
98 const std::string& account_id) {
99 return refresh_token_;
102 net::URLRequestContextGetter*
103 FakeProfileOAuth2TokenService::GetRequestContext() {
107 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
108 FakeProfileOAuth2TokenService::GetPendingRequests() {
109 std::vector<PendingRequest> valid_requests;
110 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
111 it != pending_requests_.end(); ++it) {
113 valid_requests.push_back(*it);
115 return valid_requests;
118 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
119 RequestImpl* request,
120 const std::string& account_id,
121 net::URLRequestContextGetter* getter,
122 const std::string& client_id,
123 const std::string& client_secret,
124 const ScopeSet& scopes) {
125 PendingRequest pending_request;
126 pending_request.account_id = account_id;
127 pending_request.client_id = client_id;
128 pending_request.client_secret = client_secret;
129 pending_request.scopes = scopes;
130 pending_request.request = request->AsWeakPtr();
131 pending_requests_.push_back(pending_request);