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_access_token_fetcher.h" | 5 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 state_ = GET_ACCESS_TOKEN_STARTED; | 103 state_ = GET_ACCESS_TOKEN_STARTED; |
104 fetcher_.reset(CreateFetcher( | 104 fetcher_.reset(CreateFetcher( |
105 getter_, | 105 getter_, |
106 MakeGetAccessTokenUrl(), | 106 MakeGetAccessTokenUrl(), |
107 MakeGetAccessTokenBody( | 107 MakeGetAccessTokenBody( |
108 client_id_, client_secret_, refresh_token_, scopes_), | 108 client_id_, client_secret_, refresh_token_, scopes_), |
109 this)); | 109 this)); |
110 fetcher_->Start(); // OnURLFetchComplete will be called. | 110 fetcher_->Start(); // OnURLFetchComplete will be called. |
111 } | 111 } |
112 | 112 |
113 void OAuth2AccessTokenFetcher::EndGetAccessToken(const URLFetcher* source) { | 113 void OAuth2AccessTokenFetcher::EndGetAccessToken( |
| 114 const net::URLFetcher* source) { |
114 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); | 115 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); |
115 state_ = GET_ACCESS_TOKEN_DONE; | 116 state_ = GET_ACCESS_TOKEN_DONE; |
116 | 117 |
117 URLRequestStatus status = source->GetStatus(); | 118 URLRequestStatus status = source->GetStatus(); |
118 if (!status.is_success()) { | 119 if (!status.is_success()) { |
119 OnGetTokenFailure(CreateAuthError(status)); | 120 OnGetTokenFailure(CreateAuthError(status)); |
120 return; | 121 return; |
121 } | 122 } |
122 | 123 |
123 if (source->GetResponseCode() != net::HTTP_OK) { | 124 if (source->GetResponseCode() != net::HTTP_OK) { |
(...skipping 13 matching lines...) Expand all Loading... |
137 const std::string& access_token) { | 138 const std::string& access_token) { |
138 consumer_->OnGetTokenSuccess(access_token); | 139 consumer_->OnGetTokenSuccess(access_token); |
139 } | 140 } |
140 | 141 |
141 void OAuth2AccessTokenFetcher::OnGetTokenFailure( | 142 void OAuth2AccessTokenFetcher::OnGetTokenFailure( |
142 const GoogleServiceAuthError& error) { | 143 const GoogleServiceAuthError& error) { |
143 state_ = ERROR_STATE; | 144 state_ = ERROR_STATE; |
144 consumer_->OnGetTokenFailure(error); | 145 consumer_->OnGetTokenFailure(error); |
145 } | 146 } |
146 | 147 |
147 void OAuth2AccessTokenFetcher::OnURLFetchComplete(const URLFetcher* source) { | 148 void OAuth2AccessTokenFetcher::OnURLFetchComplete( |
| 149 const net::URLFetcher* source) { |
148 CHECK(source); | 150 CHECK(source); |
149 CHECK(state_ == GET_ACCESS_TOKEN_STARTED); | 151 CHECK(state_ == GET_ACCESS_TOKEN_STARTED); |
150 EndGetAccessToken(source); | 152 EndGetAccessToken(source); |
151 } | 153 } |
152 | 154 |
153 // static | 155 // static |
154 GURL OAuth2AccessTokenFetcher::MakeGetAccessTokenUrl() { | 156 GURL OAuth2AccessTokenFetcher::MakeGetAccessTokenUrl() { |
155 return GURL(GaiaUrls::GetInstance()->oauth2_token_url()); | 157 return GURL(GaiaUrls::GetInstance()->oauth2_token_url()); |
156 } | 158 } |
157 | 159 |
(...skipping 20 matching lines...) Expand all Loading... |
178 kGetAccessTokenBodyWithScopeFormat, | 180 kGetAccessTokenBodyWithScopeFormat, |
179 enc_client_id.c_str(), | 181 enc_client_id.c_str(), |
180 enc_client_secret.c_str(), | 182 enc_client_secret.c_str(), |
181 enc_refresh_token.c_str(), | 183 enc_refresh_token.c_str(), |
182 net::EscapeUrlEncodedData(scopes_string, true).c_str()); | 184 net::EscapeUrlEncodedData(scopes_string, true).c_str()); |
183 } | 185 } |
184 } | 186 } |
185 | 187 |
186 // static | 188 // static |
187 bool OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | 189 bool OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( |
188 const URLFetcher* source, | 190 const net::URLFetcher* source, |
189 std::string* access_token) { | 191 std::string* access_token) { |
190 CHECK(source); | 192 CHECK(source); |
191 CHECK(access_token); | 193 CHECK(access_token); |
192 std::string data; | 194 std::string data; |
193 source->GetResponseAsString(&data); | 195 source->GetResponseAsString(&data); |
194 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | 196 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
195 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) | 197 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) |
196 return false; | 198 return false; |
197 | 199 |
198 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | 200 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
199 return dict->GetString(kAccessTokenKey, access_token); | 201 return dict->GetString(kAccessTokenKey, access_token); |
200 } | 202 } |
OLD | NEW |