OLD | NEW |
| (Empty) |
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/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/single_thread_task_runner.h" | |
10 #include "base/thread_task_runner_handle.h" | |
11 #include "google_apis/gaia/fake_oauth2_token_service_delegate.h" | |
12 | |
13 FakeProfileOAuth2TokenService::PendingRequest::PendingRequest() { | |
14 } | |
15 | |
16 FakeProfileOAuth2TokenService::PendingRequest::~PendingRequest() { | |
17 } | |
18 | |
19 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService() | |
20 : FakeProfileOAuth2TokenService( | |
21 new FakeOAuth2TokenServiceDelegate(nullptr)) {} | |
22 | |
23 FakeProfileOAuth2TokenService::FakeProfileOAuth2TokenService( | |
24 OAuth2TokenServiceDelegate* delegate) | |
25 : ProfileOAuth2TokenService(delegate), | |
26 auto_post_fetch_response_on_message_loop_(false), | |
27 weak_ptr_factory_(this) {} | |
28 | |
29 FakeProfileOAuth2TokenService::~FakeProfileOAuth2TokenService() { | |
30 } | |
31 | |
32 void FakeProfileOAuth2TokenService::IssueAllTokensForAccount( | |
33 const std::string& account_id, | |
34 const std::string& access_token, | |
35 const base::Time& expiration) { | |
36 CompleteRequests(account_id, | |
37 true, | |
38 ScopeSet(), | |
39 GoogleServiceAuthError::AuthErrorNone(), | |
40 access_token, | |
41 expiration); | |
42 } | |
43 | |
44 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequestsForAccount( | |
45 const std::string& account_id, | |
46 const GoogleServiceAuthError& error) { | |
47 CompleteRequests(account_id, | |
48 true, | |
49 ScopeSet(), | |
50 error, | |
51 std::string(), | |
52 base::Time()); | |
53 } | |
54 | |
55 void FakeProfileOAuth2TokenService::IssueTokenForScope( | |
56 const ScopeSet& scope, | |
57 const std::string& access_token, | |
58 const base::Time& expiration) { | |
59 CompleteRequests("", | |
60 false, | |
61 scope, | |
62 GoogleServiceAuthError::AuthErrorNone(), | |
63 access_token, | |
64 expiration); | |
65 } | |
66 | |
67 void FakeProfileOAuth2TokenService::IssueErrorForScope( | |
68 const ScopeSet& scope, | |
69 const GoogleServiceAuthError& error) { | |
70 CompleteRequests("", false, scope, error, std::string(), base::Time()); | |
71 } | |
72 | |
73 void FakeProfileOAuth2TokenService::IssueErrorForAllPendingRequests( | |
74 const GoogleServiceAuthError& error) { | |
75 CompleteRequests("", true, ScopeSet(), error, std::string(), base::Time()); | |
76 } | |
77 | |
78 void FakeProfileOAuth2TokenService::IssueTokenForAllPendingRequests( | |
79 const std::string& access_token, | |
80 const base::Time& expiration) { | |
81 CompleteRequests("", | |
82 true, | |
83 ScopeSet(), | |
84 GoogleServiceAuthError::AuthErrorNone(), | |
85 access_token, | |
86 expiration); | |
87 } | |
88 | |
89 void FakeProfileOAuth2TokenService::CompleteRequests( | |
90 const std::string& account_id, | |
91 bool all_scopes, | |
92 const ScopeSet& scope, | |
93 const GoogleServiceAuthError& error, | |
94 const std::string& access_token, | |
95 const base::Time& expiration) { | |
96 std::vector<FakeProfileOAuth2TokenService::PendingRequest> requests = | |
97 GetPendingRequests(); | |
98 | |
99 // Walk the requests and notify the callbacks. | |
100 for (std::vector<PendingRequest>::iterator it = requests.begin(); | |
101 it != requests.end(); ++it) { | |
102 DCHECK(it->request); | |
103 | |
104 bool scope_matches = all_scopes || it->scopes == scope; | |
105 bool account_matches = account_id.empty() || account_id == it->account_id; | |
106 if (account_matches && scope_matches) | |
107 it->request->InformConsumer(error, access_token, expiration); | |
108 } | |
109 } | |
110 | |
111 std::vector<FakeProfileOAuth2TokenService::PendingRequest> | |
112 FakeProfileOAuth2TokenService::GetPendingRequests() { | |
113 std::vector<PendingRequest> valid_requests; | |
114 for (std::vector<PendingRequest>::iterator it = pending_requests_.begin(); | |
115 it != pending_requests_.end(); ++it) { | |
116 if (it->request) | |
117 valid_requests.push_back(*it); | |
118 } | |
119 return valid_requests; | |
120 } | |
121 | |
122 void FakeProfileOAuth2TokenService::FetchOAuth2Token( | |
123 RequestImpl* request, | |
124 const std::string& account_id, | |
125 net::URLRequestContextGetter* getter, | |
126 const std::string& client_id, | |
127 const std::string& client_secret, | |
128 const ScopeSet& scopes) { | |
129 PendingRequest pending_request; | |
130 pending_request.account_id = account_id; | |
131 pending_request.client_id = client_id; | |
132 pending_request.client_secret = client_secret; | |
133 pending_request.scopes = scopes; | |
134 pending_request.request = request->AsWeakPtr(); | |
135 pending_requests_.push_back(pending_request); | |
136 | |
137 if (auto_post_fetch_response_on_message_loop_) { | |
138 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
139 FROM_HERE, | |
140 base::Bind(&FakeProfileOAuth2TokenService::IssueAllTokensForAccount, | |
141 weak_ptr_factory_.GetWeakPtr(), account_id, "access_token", | |
142 base::Time::Max())); | |
143 } | |
144 } | |
145 | |
146 void FakeProfileOAuth2TokenService::InvalidateAccessTokenImpl( | |
147 const std::string& account_id, | |
148 const std::string& client_id, | |
149 const ScopeSet& scopes, | |
150 const std::string& access_token) { | |
151 // Do nothing, as we don't have a cache from which to remove the token. | |
152 } | |
OLD | NEW |