| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(const URLFetcher* source) { |
| 114 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); | 114 CHECK_EQ(GET_ACCESS_TOKEN_STARTED, state_); |
| 115 state_ = GET_ACCESS_TOKEN_DONE; | 115 state_ = GET_ACCESS_TOKEN_DONE; |
| 116 | 116 |
| 117 URLRequestStatus status = source->GetStatus(); | 117 URLRequestStatus status = source->GetStatus(); |
| 118 if (!status.is_success()) { | 118 if (!status.is_success()) { |
| 119 ReportFailure(CreateAuthError(status)); | 119 OnGetTokenFailure(CreateAuthError(status)); |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (source->GetResponseCode() != RC_REQUEST_OK) { | 123 if (source->GetResponseCode() != RC_REQUEST_OK) { |
| 124 ReportFailure(GoogleServiceAuthError( | 124 OnGetTokenFailure(GoogleServiceAuthError( |
| 125 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); | 125 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)); |
| 126 return; | 126 return; |
| 127 } | 127 } |
| 128 | 128 |
| 129 // The request was successfully fetched and it returned OK. | 129 // The request was successfully fetched and it returned OK. |
| 130 // Parse out the access token. | 130 // Parse out the access token. |
| 131 std::string access_token; | 131 std::string access_token; |
| 132 if (!ParseGetAccessTokenResponse(source, &access_token)) { | 132 ParseGetAccessTokenResponse(source, &access_token); |
| 133 ReportFailure(GoogleServiceAuthError( | 133 OnGetTokenSuccess(access_token); |
| 134 GoogleServiceAuthError::UNEXPECTED_RESPONSE)); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 ReportSuccess(access_token); | |
| 139 } | 134 } |
| 140 | 135 |
| 141 void OAuth2AccessTokenFetcher::ReportSuccess(const std::string& access_token) { | 136 void OAuth2AccessTokenFetcher::OnGetTokenSuccess( |
| 137 const std::string& access_token) { |
| 142 consumer_->OnGetTokenSuccess(access_token); | 138 consumer_->OnGetTokenSuccess(access_token); |
| 143 } | 139 } |
| 144 | 140 |
| 145 void OAuth2AccessTokenFetcher::ReportFailure(GoogleServiceAuthError error) { | 141 void OAuth2AccessTokenFetcher::OnGetTokenFailure(GoogleServiceAuthError error) { |
| 146 state_ = ERROR_STATE; | 142 state_ = ERROR_STATE; |
| 147 consumer_->OnGetTokenFailure(error); | 143 consumer_->OnGetTokenFailure(error); |
| 148 } | 144 } |
| 149 | 145 |
| 150 void OAuth2AccessTokenFetcher::OnURLFetchComplete(const URLFetcher* source) { | 146 void OAuth2AccessTokenFetcher::OnURLFetchComplete(const URLFetcher* source) { |
| 151 CHECK(source); | 147 CHECK(source); |
| 152 CHECK(state_ == GET_ACCESS_TOKEN_STARTED); | 148 CHECK(state_ == GET_ACCESS_TOKEN_STARTED); |
| 153 EndGetAccessToken(source); | 149 EndGetAccessToken(source); |
| 154 } | 150 } |
| 155 | 151 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 180 std::string data; | 176 std::string data; |
| 181 source->GetResponseAsString(&data); | 177 source->GetResponseAsString(&data); |
| 182 base::JSONReader reader; | 178 base::JSONReader reader; |
| 183 scoped_ptr<base::Value> value(reader.Read(data, false)); | 179 scoped_ptr<base::Value> value(reader.Read(data, false)); |
| 184 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) | 180 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) |
| 185 return false; | 181 return false; |
| 186 | 182 |
| 187 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); | 183 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
| 188 return GetStringFromDictionary(dict, kAccessTokenKey, access_token); | 184 return GetStringFromDictionary(dict, kAccessTokenKey, access_token); |
| 189 } | 185 } |
| OLD | NEW |