OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "google_apis/gaia/gaia_oauth_client.h" | 5 #include "google_apis/gaia/gaia_oauth_client.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 const std::string& refresh_token, | 49 const std::string& refresh_token, |
50 const std::vector<std::string>& scopes, | 50 const std::vector<std::string>& scopes, |
51 int max_retries, | 51 int max_retries, |
52 GaiaOAuthClient::Delegate* delegate); | 52 GaiaOAuthClient::Delegate* delegate); |
53 void GetUserEmail(const std::string& oauth_access_token, | 53 void GetUserEmail(const std::string& oauth_access_token, |
54 int max_retries, | 54 int max_retries, |
55 Delegate* delegate); | 55 Delegate* delegate); |
56 void GetUserId(const std::string& oauth_access_token, | 56 void GetUserId(const std::string& oauth_access_token, |
57 int max_retries, | 57 int max_retries, |
58 Delegate* delegate); | 58 Delegate* delegate); |
59 void GetUserInfo(const std::string& oauth_access_token, | |
60 int max_retries, | |
61 Delegate* delegate); | |
59 void GetTokenInfo(const std::string& oauth_access_token, | 62 void GetTokenInfo(const std::string& oauth_access_token, |
60 int max_retries, | 63 int max_retries, |
61 Delegate* delegate); | 64 Delegate* delegate); |
62 | 65 |
63 // net::URLFetcherDelegate implementation. | 66 // net::URLFetcherDelegate implementation. |
64 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 67 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
65 | 68 |
66 private: | 69 private: |
67 friend class base::RefCountedThreadSafe<Core>; | 70 friend class base::RefCountedThreadSafe<Core>; |
68 | 71 |
69 enum RequestType { | 72 enum RequestType { |
70 NO_PENDING_REQUEST, | 73 NO_PENDING_REQUEST, |
71 TOKENS_FROM_AUTH_CODE, | 74 TOKENS_FROM_AUTH_CODE, |
72 REFRESH_TOKEN, | 75 REFRESH_TOKEN, |
73 TOKEN_INFO, | 76 TOKEN_INFO, |
74 USER_EMAIL, | 77 USER_EMAIL, |
75 USER_ID, | 78 USER_ID, |
79 USER_INFO | |
noms (inactive)
2014/04/25 17:07:40
nit: I think the last enum should end in a trailin
Roger Tawa OOO till Jul 10th
2014/04/28 20:44:12
Done.
| |
76 }; | 80 }; |
77 | 81 |
78 virtual ~Core() {} | 82 virtual ~Core() {} |
79 | 83 |
80 void GetUserInfo(const std::string& oauth_access_token, | 84 void PeopleGet(const std::string& oauth_access_token, |
81 int max_retries, | 85 int max_retries, |
82 Delegate* delegate); | 86 Delegate* delegate); |
83 void MakeGaiaRequest(const GURL& url, | 87 void MakeGaiaRequest(const GURL& url, |
84 const std::string& post_body, | 88 const std::string& post_body, |
85 int max_retries, | 89 int max_retries, |
86 GaiaOAuthClient::Delegate* delegate); | 90 GaiaOAuthClient::Delegate* delegate); |
87 void HandleResponse(const net::URLFetcher* source, | 91 void HandleResponse(const net::URLFetcher* source, |
88 bool* should_retry_request); | 92 bool* should_retry_request); |
89 | 93 |
90 int num_retries_; | 94 int num_retries_; |
91 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 95 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
92 GaiaOAuthClient::Delegate* delegate_; | 96 GaiaOAuthClient::Delegate* delegate_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), | 142 MakeGaiaRequest(GURL(GaiaUrls::GetInstance()->oauth2_token_url()), |
139 post_body, max_retries, delegate); | 143 post_body, max_retries, delegate); |
140 } | 144 } |
141 | 145 |
142 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, | 146 void GaiaOAuthClient::Core::GetUserEmail(const std::string& oauth_access_token, |
143 int max_retries, | 147 int max_retries, |
144 Delegate* delegate) { | 148 Delegate* delegate) { |
145 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | 149 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); |
146 DCHECK(!request_.get()); | 150 DCHECK(!request_.get()); |
147 request_type_ = USER_EMAIL; | 151 request_type_ = USER_EMAIL; |
148 GetUserInfo(oauth_access_token, max_retries, delegate); | 152 PeopleGet(oauth_access_token, max_retries, delegate); |
149 } | 153 } |
150 | 154 |
151 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, | 155 void GaiaOAuthClient::Core::GetUserId(const std::string& oauth_access_token, |
152 int max_retries, | 156 int max_retries, |
153 Delegate* delegate) { | 157 Delegate* delegate) { |
154 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | 158 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); |
155 DCHECK(!request_.get()); | 159 DCHECK(!request_.get()); |
156 request_type_ = USER_ID; | 160 request_type_ = USER_ID; |
157 GetUserInfo(oauth_access_token, max_retries, delegate); | 161 PeopleGet(oauth_access_token, max_retries, delegate); |
158 } | 162 } |
159 | 163 |
160 void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, | 164 void GaiaOAuthClient::Core::GetUserInfo(const std::string& oauth_access_token, |
161 int max_retries, | 165 int max_retries, |
162 Delegate* delegate) { | 166 Delegate* delegate) { |
167 DCHECK_EQ(request_type_, NO_PENDING_REQUEST); | |
168 DCHECK(!request_.get()); | |
169 request_type_ = USER_INFO; | |
170 PeopleGet(oauth_access_token, max_retries, delegate); | |
171 } | |
172 | |
173 void GaiaOAuthClient::Core::PeopleGet(const std::string& oauth_access_token, | |
174 int max_retries, | |
175 Delegate* delegate) { | |
163 delegate_ = delegate; | 176 delegate_ = delegate; |
164 num_retries_ = 0; | 177 num_retries_ = 0; |
165 request_.reset(net::URLFetcher::Create( | 178 request_.reset(net::URLFetcher::Create( |
166 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->oauth_user_info_url()), | 179 kUrlFetcherId, GURL(GaiaUrls::GetInstance()->people_get_url()), |
167 net::URLFetcher::GET, this)); | 180 net::URLFetcher::GET, this)); |
168 request_->SetRequestContext(request_context_getter_.get()); | 181 request_->SetRequestContext(request_context_getter_.get()); |
169 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); | 182 request_->AddExtraRequestHeader("Authorization: OAuth " + oauth_access_token); |
170 request_->SetMaxRetriesOn5xx(max_retries); | 183 request_->SetMaxRetriesOn5xx(max_retries); |
171 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 184 request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
172 net::LOAD_DO_NOT_SAVE_COOKIES); | 185 net::LOAD_DO_NOT_SAVE_COOKIES); |
173 | 186 |
174 // Fetchers are sometimes cancelled because a network change was detected, | 187 // Fetchers are sometimes cancelled because a network change was detected, |
175 // especially at startup and after sign-in on ChromeOS. Retrying once should | 188 // especially at startup and after sign-in on ChromeOS. Retrying once should |
176 // be enough in those cases; let the fetcher retry up to 3 times just in case. | 189 // be enough in those cases; let the fetcher retry up to 3 times just in case. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
275 *should_retry_request = true; | 288 *should_retry_request = true; |
276 } | 289 } |
277 return; | 290 return; |
278 } | 291 } |
279 | 292 |
280 RequestType type = request_type_; | 293 RequestType type = request_type_; |
281 request_type_ = NO_PENDING_REQUEST; | 294 request_type_ = NO_PENDING_REQUEST; |
282 | 295 |
283 switch (type) { | 296 switch (type) { |
284 case USER_EMAIL: { | 297 case USER_EMAIL: { |
285 std::string email; | 298 const base::ListValue* emails_list; |
286 response_dict->GetString("email", &email); | 299 if (response_dict->GetList("emails", &emails_list)) { |
noms (inactive)
2014/04/25 17:07:40
nit: please add a comment. this looks scary :)
Roger Tawa OOO till Jul 10th
2014/04/28 20:44:12
Added comment, but also forgotten error case.
| |
287 delegate_->OnGetUserEmailResponse(email); | 300 for (size_t i = 0; i < emails_list->GetSize(); ++i) { |
301 const base::DictionaryValue* email_dict; | |
302 if (emails_list->GetDictionary(i, &email_dict)) { | |
303 std::string email; | |
304 std::string type; | |
305 if (email_dict->GetString("type", &type) && | |
306 type == "account" && | |
307 email_dict->GetString("value", &email)) { | |
308 delegate_->OnGetUserEmailResponse(email); | |
309 break; | |
310 } | |
311 } | |
312 } | |
313 } | |
288 break; | 314 break; |
289 } | 315 } |
290 | 316 |
291 case USER_ID: { | 317 case USER_ID: { |
292 std::string id; | 318 std::string id; |
293 response_dict->GetString("id", &id); | 319 response_dict->GetString("id", &id); |
294 delegate_->OnGetUserIdResponse(id); | 320 delegate_->OnGetUserIdResponse(id); |
295 break; | 321 break; |
296 } | 322 } |
297 | 323 |
324 case USER_INFO: { | |
325 delegate_->OnGetUserInfoResponse(response_dict.Pass()); | |
326 break; | |
327 } | |
328 | |
298 case TOKEN_INFO: { | 329 case TOKEN_INFO: { |
299 delegate_->OnGetTokenInfoResponse(response_dict.Pass()); | 330 delegate_->OnGetTokenInfoResponse(response_dict.Pass()); |
300 break; | 331 break; |
301 } | 332 } |
302 | 333 |
303 case TOKENS_FROM_AUTH_CODE: | 334 case TOKENS_FROM_AUTH_CODE: |
304 case REFRESH_TOKEN: { | 335 case REFRESH_TOKEN: { |
305 std::string access_token; | 336 std::string access_token; |
306 std::string refresh_token; | 337 std::string refresh_token; |
307 int expires_in_seconds = 0; | 338 int expires_in_seconds = 0; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 Delegate* delegate) { | 396 Delegate* delegate) { |
366 return core_->GetUserEmail(access_token, max_retries, delegate); | 397 return core_->GetUserEmail(access_token, max_retries, delegate); |
367 } | 398 } |
368 | 399 |
369 void GaiaOAuthClient::GetUserId(const std::string& access_token, | 400 void GaiaOAuthClient::GetUserId(const std::string& access_token, |
370 int max_retries, | 401 int max_retries, |
371 Delegate* delegate) { | 402 Delegate* delegate) { |
372 return core_->GetUserId(access_token, max_retries, delegate); | 403 return core_->GetUserId(access_token, max_retries, delegate); |
373 } | 404 } |
374 | 405 |
406 void GaiaOAuthClient::GetUserInfo(const std::string& access_token, | |
407 int max_retries, | |
noms (inactive)
2014/04/25 17:07:40
nit: wrong indentation
Roger Tawa OOO till Jul 10th
2014/04/28 20:44:12
Done.
| |
408 Delegate* delegate) { | |
409 return core_->GetUserInfo(access_token, max_retries, delegate); | |
410 } | |
411 | |
375 void GaiaOAuthClient::GetTokenInfo(const std::string& access_token, | 412 void GaiaOAuthClient::GetTokenInfo(const std::string& access_token, |
376 int max_retries, | 413 int max_retries, |
377 Delegate* delegate) { | 414 Delegate* delegate) { |
378 return core_->GetTokenInfo(access_token, max_retries, delegate); | 415 return core_->GetTokenInfo(access_token, max_retries, delegate); |
379 } | 416 } |
380 | 417 |
381 } // namespace gaia | 418 } // namespace gaia |
OLD | NEW |