| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/browser/interests/interests_fetcher.h" | 5 #include "chrome/browser/interests/interests_fetcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 void InterestsFetcher::FetchInterests( | 81 void InterestsFetcher::FetchInterests( |
| 82 const InterestsFetcher::InterestsCallback& callback) { | 82 const InterestsFetcher::InterestsCallback& callback) { |
| 83 DCHECK(callback_.is_null()); | 83 DCHECK(callback_.is_null()); |
| 84 callback_ = callback; | 84 callback_ = callback; |
| 85 StartOAuth2Request(); | 85 StartOAuth2Request(); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 88 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 89 const net::URLRequestStatus& status = source->GetStatus(); | 89 const net::URLRequestStatus& status = source->GetStatus(); |
| 90 if (!status.is_success()) { | 90 if (!status.is_success()) { |
| 91 VLOG(2) << "Network error " << status.error(); | 91 DLOG(WARNING) << "Network error " << status.error(); |
| 92 callback_.Run(nullptr); | 92 callback_.Run(nullptr); |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 int response_code = source->GetResponseCode(); | 96 int response_code = source->GetResponseCode(); |
| 97 // If we get an authorization error, refresh token and retry once. | 97 // If we get an authorization error, refresh token and retry once. |
| 98 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | 98 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { |
| 99 DLOG(WARNING) << "Authorization error."; |
| 99 access_token_expired_ = true; | 100 access_token_expired_ = true; |
| 100 token_service_->InvalidateAccessToken(account_id_, | 101 token_service_->InvalidateAccessToken(account_id_, |
| 101 GetApiScopes(), | 102 GetApiScopes(), |
| 102 access_token_); | 103 access_token_); |
| 103 StartOAuth2Request(); | 104 StartOAuth2Request(); |
| 104 return; | 105 return; |
| 105 } | 106 } |
| 106 | 107 |
| 107 if (response_code != net::HTTP_OK) { | 108 if (response_code != net::HTTP_OK) { |
| 108 VLOG(2) << "HTTP error " << response_code; | 109 DLOG(WARNING) << "HTTP error " << response_code; |
| 109 callback_.Run(nullptr); | 110 callback_.Run(nullptr); |
| 110 return; | 111 return; |
| 111 } | 112 } |
| 112 | 113 |
| 113 std::string response_body; | 114 std::string response_body; |
| 114 source->GetResponseAsString(&response_body); | 115 source->GetResponseAsString(&response_body); |
| 115 | 116 |
| 116 callback_.Run(ExtractInterests(response_body)); | 117 callback_.Run(ExtractInterests(response_body)); |
| 117 } | 118 } |
| 118 | 119 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 130 | 131 |
| 131 fetcher_->AddExtraRequestHeader( | 132 fetcher_->AddExtraRequestHeader( |
| 132 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); | 133 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); |
| 133 | 134 |
| 134 fetcher_->Start(); | 135 fetcher_->Start(); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void InterestsFetcher::OnGetTokenFailure( | 138 void InterestsFetcher::OnGetTokenFailure( |
| 138 const OAuth2TokenService::Request* request, | 139 const OAuth2TokenService::Request* request, |
| 139 const GoogleServiceAuthError& error) { | 140 const GoogleServiceAuthError& error) { |
| 140 DLOG(WARNING) << error.ToString(); | 141 DLOG(WARNING) << "Failed to get OAuth2 Token: " << error.ToString(); |
| 141 | 142 |
| 142 callback_.Run(nullptr); | 143 callback_.Run(nullptr); |
| 143 } | 144 } |
| 144 | 145 |
| 145 void InterestsFetcher::StartOAuth2Request() { | 146 void InterestsFetcher::StartOAuth2Request() { |
| 147 if (account_id_.empty()) { |
| 148 DLOG(WARNING) << "AccountId is empty, user is not signed in."; |
| 149 return; |
| 150 } |
| 151 |
| 146 oauth_request_ = | 152 oauth_request_ = |
| 147 token_service_->StartRequest(account_id_, GetApiScopes(), this); | 153 token_service_->StartRequest(account_id_, GetApiScopes(), this); |
| 148 } | 154 } |
| 149 | 155 |
| 150 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { | 156 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { |
| 151 OAuth2TokenService::ScopeSet scopes; | 157 OAuth2TokenService::ScopeSet scopes; |
| 152 scopes.insert(kApiScope); | 158 scopes.insert(kApiScope); |
| 153 return scopes; | 159 return scopes; |
| 154 } | 160 } |
| 155 | 161 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | 207 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { |
| 202 DLOG(WARNING) << "Failed to parse interest relevance."; | 208 DLOG(WARNING) << "Failed to parse interest relevance."; |
| 203 continue; | 209 continue; |
| 204 } | 210 } |
| 205 | 211 |
| 206 res->push_back(Interest{name, GURL(image_url), relevance}); | 212 res->push_back(Interest{name, GURL(image_url), relevance}); |
| 207 } | 213 } |
| 208 | 214 |
| 209 return res; | 215 return res; |
| 210 } | 216 } |
| OLD | NEW |