e1842eb558981452422fe797fd4ba7ee01348204
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / fake_profile_oauth2_token_service.cc
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.
4
5 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/signin/signin_account_id_helper.h"
9
10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
11 }
12
13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
14 }
15
16 // static
17 BrowserContextKeyedService* FakeProfileOAuth2TokenService::Build(
18     content::BrowserContext* profile) {
19   FakeProfileOAuth2TokenService* service = new FakeProfileOAuth2TokenService();
20   service->Initialize(reinterpret_cast<Profile*>(profile));
21   return service;
22 }
23
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));
30   return service;
31 }
32
33 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService()
34     : auto_post_fetch_response_on_message_loop_(false) {
35   SigninAccountIdHelper::SetDisableForTest(true);
36 }
37
38 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
39   SigninAccountIdHelper::SetDisableForTest(false);
40 }
41
42 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
43     const std::string& account_id) {
44   return !GetRefreshToken(account_id).empty();
45 }
46
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);
52   }
53   return account_ids;
54 }
55
56 void FakeProfileOAuth2TokenService::UpdateCredentials(
57     const std::string& account_id,
58     const std::string& refresh_token) {
59   IssueRefreshTokenForUser(account_id, refresh_token);
60 }
61
62 void FakeProfileOAuth2TokenService::IssueRefreshToken(
63     const std::string& token) {
64   IssueRefreshTokenForUser("account_id", token);
65 }
66
67 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
68     const std::string& account_id,
69     const std::string& token) {
70   if (token.empty()) {
71     refresh_tokens_.erase(account_id);
72     FireRefreshTokenRevoked(account_id);
73   } else {
74     refresh_tokens_[account_id] = token;
75     FireRefreshTokenAvailable(account_id);
76     // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
77   }
78 }
79
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,
85                    true,
86                    ScopeSet(),
87                    GoogleServiceAuthError::AuthErrorNone(),
88                    access_token,
89                    expiration);
90 }
91
92 void FakeProfileOAuth2TokenService::IssueTokenForScope(
93     const ScopeSet& scope,
94     const std::string& access_token,
95     const base::Time& expiration) {
96   CompleteRequests("",
97                    false,
98                    scope,
99                    GoogleServiceAuthError::AuthErrorNone(),
100                    access_token,
101                    expiration);
102 }
103
104 void FakeProfileOAuth2TokenService::IssueErrorForScope(
105     const ScopeSet& scope,
106     const GoogleServiceAuthError& error) {
107   CompleteRequests("", false, scope, error, std::string(), base::Time());
108 }
109
110 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
111     const GoogleServiceAuthError& error) {
112   CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
113 }
114
115 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
116     const std::string& access_token,
117     const base::Time& expiration) {
118   CompleteRequests("",
119                    true,
120                    ScopeSet(),
121                    GoogleServiceAuthError::AuthErrorNone(),
122                    access_token,
123                    expiration);
124 }
125
126 void FakeProfileOAuth2TokenService::CompleteRequests(
127     const std::string& account_id,
128     bool all_scopes,
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();
135
136   // Walk the requests and notify the callbacks.
137   for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
138        it != pending_requests_.end(); ++it) {
139     if (!it->request)
140       continue;
141
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);
146   }
147 }
148
149 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
150     const std::string& account_id) {
151   return refresh_tokens_.count(account_id) > 0 ? refresh_tokens_[account_id] :
152       std::string();
153 }
154
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) {
160     if (it->request)
161       valid_requests.push_back(*it);
162   }
163   return valid_requests;
164 }
165
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);
180
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),
185         account_id,
186         "access_token",
187         base::Time::Max()));
188   }
189 }
190
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.
197 }