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 "chrome/common/net/gaia/oauth2_api_call_flow.h" | 5 #include "chrome/common/net/gaia/oauth2_api_call_flow.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 const std::string& access_token, | 27 const std::string& access_token, |
28 const std::vector<std::string>& scopes) | 28 const std::vector<std::string>& scopes) |
29 : context_(context), | 29 : context_(context), |
30 refresh_token_(refresh_token), | 30 refresh_token_(refresh_token), |
31 access_token_(access_token), | 31 access_token_(access_token), |
32 scopes_(scopes), | 32 scopes_(scopes), |
33 state_(INITIAL), | 33 state_(INITIAL), |
34 tried_mint_access_token_(false) { | 34 tried_mint_access_token_(false) { |
35 } | 35 } |
36 | 36 |
37 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() { } | 37 OAuth2ApiCallFlow::~OAuth2ApiCallFlow() {} |
38 | 38 |
39 void OAuth2ApiCallFlow::Start() { | 39 void OAuth2ApiCallFlow::Start() { |
40 BeginApiCall(); | 40 BeginApiCall(); |
41 } | 41 } |
42 | 42 |
43 void OAuth2ApiCallFlow::BeginApiCall() { | 43 void OAuth2ApiCallFlow::BeginApiCall() { |
44 CHECK(state_ == INITIAL || state_ == MINT_ACCESS_TOKEN_DONE); | 44 CHECK(state_ == INITIAL || state_ == MINT_ACCESS_TOKEN_DONE); |
45 | 45 |
46 // If the access token is empty then directly try to mint one. | 46 // If the access token is empty then directly try to mint one. |
47 if (access_token_.empty()) { | 47 if (access_token_.empty()) { |
48 BeginMintAccessToken(); | 48 BeginMintAccessToken(); |
49 return; | |
50 } else { | 49 } else { |
51 state_ = API_CALL_STARTED; | 50 state_ = API_CALL_STARTED; |
52 | |
53 url_fetcher_.reset(CreateURLFetcher()); | 51 url_fetcher_.reset(CreateURLFetcher()); |
54 url_fetcher_->Start(); // OnURLFetchComplete will be called. | 52 url_fetcher_->Start(); // OnURLFetchComplete will be called. |
55 } | 53 } |
56 } | 54 } |
57 | 55 |
58 void OAuth2ApiCallFlow::EndApiCall(const URLFetcher* source) { | 56 void OAuth2ApiCallFlow::EndApiCall(const URLFetcher* source) { |
59 CHECK_EQ(API_CALL_STARTED, state_); | 57 CHECK_EQ(API_CALL_STARTED, state_); |
60 state_ = API_CALL_DONE; | 58 state_ = API_CALL_DONE; |
61 | 59 |
62 URLRequestStatus status = source->GetStatus(); | 60 URLRequestStatus status = source->GetStatus(); |
63 if (!status.is_success()) { | 61 if (!status.is_success()) { |
| 62 state_ = ERROR_STATE; |
64 ProcessApiCallFailure(source); | 63 ProcessApiCallFailure(source); |
65 return; | 64 return; |
66 } | 65 } |
67 | 66 |
68 // If the response code is 401 Unauthorized then access token may have | 67 // If the response code is 401 Unauthorized then access token may have |
69 // expired. So try generating a new access token. | 68 // expired. So try generating a new access token. |
70 if (source->GetResponseCode() == net::HTTP_UNAUTHORIZED) { | 69 if (source->GetResponseCode() == net::HTTP_UNAUTHORIZED) { |
71 // If we already tried minting a new access token, don't do it again. | 70 // If we already tried minting a new access token, don't do it again. |
72 if (tried_mint_access_token_) | 71 if (tried_mint_access_token_) { |
| 72 state_ = ERROR_STATE; |
73 ProcessApiCallFailure(source); | 73 ProcessApiCallFailure(source); |
74 else | 74 } else { |
75 BeginMintAccessToken(); | 75 BeginMintAccessToken(); |
| 76 } |
76 | 77 |
77 return; | 78 return; |
78 } | 79 } |
79 | 80 |
80 if (source->GetResponseCode() != net::HTTP_OK) { | 81 if (source->GetResponseCode() != net::HTTP_OK) { |
| 82 state_ = ERROR_STATE; |
81 ProcessApiCallFailure(source); | 83 ProcessApiCallFailure(source); |
82 return; | 84 return; |
83 } | 85 } |
84 | 86 |
85 ProcessApiCallSuccess(source); | 87 ProcessApiCallSuccess(source); |
86 } | 88 } |
87 | 89 |
88 void OAuth2ApiCallFlow::BeginMintAccessToken() { | 90 void OAuth2ApiCallFlow::BeginMintAccessToken() { |
89 CHECK(state_ == INITIAL || state_ == API_CALL_DONE); | 91 CHECK(state_ == INITIAL || state_ == API_CALL_DONE); |
90 CHECK(!tried_mint_access_token_); | 92 CHECK(!tried_mint_access_token_); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 145 |
144 result->SetRequestContext(context_); | 146 result->SetRequestContext(context_); |
145 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 147 result->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
146 net::LOAD_DO_NOT_SAVE_COOKIES); | 148 net::LOAD_DO_NOT_SAVE_COOKIES); |
147 | 149 |
148 if (!empty_body) | 150 if (!empty_body) |
149 result->SetUploadData("application/x-www-form-urlencoded", body); | 151 result->SetUploadData("application/x-www-form-urlencoded", body); |
150 | 152 |
151 return result; | 153 return result; |
152 } | 154 } |
OLD | NEW |