| 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 |
| 119 void InterestsFetcher::OnGetTokenSuccess( | 120 void InterestsFetcher::OnGetTokenSuccess( |
| 120 const OAuth2TokenService::Request* request, | 121 const OAuth2TokenService::Request* request, |
| 121 const std::string& access_token, | 122 const std::string& access_token, |
| 122 const base::Time& expiration_time) { | 123 const base::Time& expiration_time) { |
| 123 access_token_ = access_token; | 124 access_token_ = access_token; |
| 124 | 125 |
| 125 fetcher_ = CreateFetcher(); | 126 fetcher_ = CreateFetcher(); |
| 126 fetcher_->SetRequestContext(url_request_context_); | 127 fetcher_->SetRequestContext(url_request_context_); |
| 127 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 128 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 128 net::LOAD_DO_NOT_SAVE_COOKIES); | 129 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 129 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | 130 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 130 | 131 |
| 131 fetcher_->AddExtraRequestHeader( | 132 // TODO(peconn): Activate this when we have a proper server. |
| 132 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); | 133 DLOG(WARNING) << "Skipping authorization header"; |
| 134 // fetcher_->AddExtraRequestHeader( |
| 135 // base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); |
| 133 | 136 |
| 134 fetcher_->Start(); | 137 fetcher_->Start(); |
| 135 } | 138 } |
| 136 | 139 |
| 137 void InterestsFetcher::OnGetTokenFailure( | 140 void InterestsFetcher::OnGetTokenFailure( |
| 138 const OAuth2TokenService::Request* request, | 141 const OAuth2TokenService::Request* request, |
| 139 const GoogleServiceAuthError& error) { | 142 const GoogleServiceAuthError& error) { |
| 140 DLOG(WARNING) << error.ToString(); | 143 DLOG(WARNING) << "Failed to get OAuth2 Token: " << error.ToString(); |
| 141 | 144 |
| 142 callback_.Run(nullptr); | 145 callback_.Run(nullptr); |
| 143 } | 146 } |
| 144 | 147 |
| 145 void InterestsFetcher::StartOAuth2Request() { | 148 void InterestsFetcher::StartOAuth2Request() { |
| 146 oauth_request_ = | 149 oauth_request_ = |
| 147 token_service_->StartRequest(account_id_, GetApiScopes(), this); | 150 token_service_->StartRequest(account_id_, GetApiScopes(), this); |
| 148 } | 151 } |
| 149 | 152 |
| 150 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { | 153 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | 204 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { |
| 202 DLOG(WARNING) << "Failed to parse interest relevance."; | 205 DLOG(WARNING) << "Failed to parse interest relevance."; |
| 203 continue; | 206 continue; |
| 204 } | 207 } |
| 205 | 208 |
| 206 res->push_back(Interest{name, GURL(image_url), relevance}); | 209 res->push_back(Interest{name, GURL(image_url), relevance}); |
| 207 } | 210 } |
| 208 | 211 |
| 209 return res; | 212 return res; |
| 210 } | 213 } |
| OLD | NEW |