Update To 11.40.268.0
[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 "components/signin/core/browser/signin_account_id_helper.h"
9
10 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() {
11 }
12
13 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() {
14 }
15
16 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService()
17     : auto_post_fetch_response_on_message_loop_(false),
18       weak_ptr_factory_(this) {
19   SigninAccountIdHelper::SetDisableForTest(true);
20 }
21
22 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() {
23   SigninAccountIdHelper::SetDisableForTest(false);
24 }
25
26 bool FakeProfileOAuth2TokenService::RefreshTokenIsAvailable(
27     const std::string& account_id) const {
28   return !GetRefreshToken(account_id).empty();
29 }
30
31 void FakeProfileOAuth2TokenService::LoadCredentials(
32     const std::string& primary_account_id) {
33   // Empty implementation as FakeProfileOAuth2TokenService does not have any
34   // credentials to load.
35 }
36
37 std::vector<std::string> FakeProfileOAuth2TokenService::GetAccounts() {
38   std::vector<std::string> account_ids;
39   for (std::map<std::string, std::string>::const_iterator iter =
40            refresh_tokens_.begin(); iter != refresh_tokens_.end(); ++iter) {
41     account_ids.push_back(iter->first);
42   }
43   return account_ids;
44 }
45
46 void FakeProfileOAuth2TokenService::UpdateCredentials(
47     const std::string& account_id,
48     const std::string& refresh_token) {
49   IssueRefreshTokenForUser(account_id, refresh_token);
50 }
51
52 void FakeProfileOAuth2TokenService::IssueRefreshToken(
53     const std::string& token) {
54   IssueRefreshTokenForUser("account_id", token);
55 }
56
57 void FakeProfileOAuth2TokenService::IssueRefreshTokenForUser(
58     const std::string& account_id,
59     const std::string& token) {
60   ScopedBatchChange batch(this);
61   if (token.empty()) {
62     refresh_tokens_.erase(account_id);
63     FireRefreshTokenRevoked(account_id);
64   } else {
65     refresh_tokens_[account_id] = token;
66     FireRefreshTokenAvailable(account_id);
67     // TODO(atwilson): Maybe we should also call FireRefreshTokensLoaded() here?
68   }
69 }
70
71 void FakeProfileOAuth2TokenService::IssueAllRefreshTokensLoaded() {
72   FireRefreshTokensLoaded();
73 }
74
75 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount(
76     const std::string& account_id,
77     const std::string& access_token,
78     const base::Time& expiration) {
79   CompleteRequests(account_id,
80                    true,
81                    ScopeSet(),
82                    GoogleServiceAuthError::AuthErrorNone(),
83                    access_token,
84                    expiration);
85 }
86
87 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount(
88     const std::string& account_id,
89     const GoogleServiceAuthError& error) {
90   CompleteRequests(account_id,
91                    true,
92                    ScopeSet(),
93                    error,
94                    std::string(),
95                    base::Time());
96 }
97
98 void FakeProfileOAuth2TokenService::IssueTokenForScope(
99     const ScopeSet& scope,
100     const std::string& access_token,
101     const base::Time& expiration) {
102   CompleteRequests("",
103                    false,
104                    scope,
105                    GoogleServiceAuthError::AuthErrorNone(),
106                    access_token,
107                    expiration);
108 }
109
110 void FakeProfileOAuth2TokenService::IssueErrorForScope(
111     const ScopeSet& scope,
112     const GoogleServiceAuthError& error) {
113   CompleteRequests("", false, scope, error, std::string(), base::Time());
114 }
115
116 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests(
117     const GoogleServiceAuthError& error) {
118   CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time());
119 }
120
121 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests(
122     const std::string& access_token,
123     const base::Time& expiration) {
124   CompleteRequests("",
125                    true,
126                    ScopeSet(),
127                    GoogleServiceAuthError::AuthErrorNone(),
128                    access_token,
129                    expiration);
130 }
131
132 void FakeProfileOAuth2TokenService::CompleteRequests(
133     const std::string& account_id,
134     bool all_scopes,
135     const ScopeSet& scope,
136     const GoogleServiceAuthError& error,
137     const std::string& access_token,
138     const base::Time& expiration) {
139   std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests =
140       GetPendingRequests();
141
142   // Walk the requests and notify the callbacks.
143   for (std::vector<PendingRequest>::iterator it = requests.begin();
144        it != requests.end(); ++it) {
145     DCHECK(it->request);
146
147     bool scope_matches = all_scopes || it->scopes == scope;
148     bool account_matches = account_id.empty() || account_id == it->account_id;
149     if (account_matches && scope_matches)
150       it->request->InformConsumer(error, access_token, expiration);
151   }
152 }
153
154 std::string FakeProfileOAuth2TokenService::GetRefreshToken(
155     const std::string& account_id) const {
156   std::map<std::string, std::string>::const_iterator it =
157       refresh_tokens_.find(account_id);
158   if (it != refresh_tokens_.end())
159     return it->second;
160   return std::string();
161 }
162
163 net::URLRequestContextGetter*
164 FakeProfileOAuth2TokenService::GetRequestContext() {
165   return NULL;
166 }
167
168 std::vector<FakeProfileOAuth2TokenService::PendingRequest>
169 FakeProfileOAuth2TokenService::GetPendingRequests() {
170   std::vector<PendingRequest> valid_requests;
171   for (std::vector<PendingRequest>::iterator it = pending_requests_.begin();
172        it != pending_requests_.end(); ++it) {
173     if (it->request)
174       valid_requests.push_back(*it);
175   }
176   return valid_requests;
177 }
178
179 void FakeProfileOAuth2TokenService::FetchOAuth2Token(
180     RequestImpl* request,
181     const std::string& account_id,
182     net::URLRequestContextGetter* getter,
183     const std::string& client_id,
184     const std::string& client_secret,
185     const ScopeSet& scopes) {
186   PendingRequest pending_request;
187   pending_request.account_id = account_id;
188   pending_request.client_id = client_id;
189   pending_request.client_secret = client_secret;
190   pending_request.scopes = scopes;
191   pending_request.request = request->AsWeakPtr();
192   pending_requests_.push_back(pending_request);
193
194   if (auto_post_fetch_response_on_message_loop_) {
195     base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
196         &FakeProfileOAuth2TokenService::IssueAllTokensForAccount,
197         weak_ptr_factory_.GetWeakPtr(),
198         account_id,
199         "access_token",
200         base::Time::Max()));
201   }
202 }
203
204 OAuth2AccessTokenFetcher*
205 FakeProfileOAuth2TokenService::CreateAccessTokenFetcher(
206     const std::string& account_id,
207     net::URLRequestContextGetter* getter,
208     OAuth2AccessTokenConsumer* consumer) {
209   NOTREACHED();
210   return NULL;
211 }
212
213 void FakeProfileOAuth2TokenService::InvalidateOAuth2Token(
214     const std::string& account_id,
215     const std::string& client_id,
216     const ScopeSet& scopes,
217     const std::string& access_token) {
218   // Do nothing, as we don't have a cache from which to remove the token.
219 }