OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/interests/interests_fetcher.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 16 #include "net/base/load_flags.h" |
| 17 #include "net/http/http_status_code.h" |
| 18 #include "net/url_request/url_fetcher.h" |
| 19 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "net/url_request/url_request_status.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const int kNumRetries = 1; |
| 25 const char kIdInterests[] = "interests"; |
| 26 const char kIdInterestName[] = "name"; |
| 27 const char kIdInterestImageUrl[] = "imageUrl"; |
| 28 const char kIdInterestRelevance[] = "relevance"; |
| 29 |
| 30 const char kApiScope[] = "https://www.googleapis.com/auth/googlenow"; |
| 31 |
| 32 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
| 33 |
| 34 std::vector<InterestsFetcher::Interest> EmptyResponse() { |
| 35 return std::vector<InterestsFetcher::Interest>(); |
| 36 } |
| 37 |
| 38 GURL GetInterestsURL() { |
| 39 const base::CommandLine* command_line = |
| 40 base::CommandLine::ForCurrentProcess(); |
| 41 return GURL(command_line->GetSwitchValueASCII(switches::kInterestsURL)); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 InterestsFetcher::Interest::Interest(const std::string& name, |
| 47 const GURL& image_url, |
| 48 double relevance) |
| 49 : name(name), image_url(image_url), relevance(relevance) {} |
| 50 |
| 51 InterestsFetcher::Interest::~Interest() {} |
| 52 |
| 53 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { |
| 54 return name == interest.name && image_url == interest.image_url && |
| 55 relevance == interest.relevance; |
| 56 } |
| 57 |
| 58 InterestsFetcher::InterestsFetcher( |
| 59 OAuth2TokenService* oauth2_token_service, |
| 60 const std::string& account_id, |
| 61 net::URLRequestContextGetter* url_request_context) |
| 62 : OAuth2TokenService::Consumer("interests_fetcher"), |
| 63 account_id_(account_id), |
| 64 url_request_context_(url_request_context), |
| 65 access_token_expired_(false), |
| 66 token_service_(oauth2_token_service) { |
| 67 } |
| 68 |
| 69 InterestsFetcher::~InterestsFetcher() {} |
| 70 |
| 71 // static |
| 72 scoped_ptr<InterestsFetcher> InterestsFetcher::CreateFromProfile( |
| 73 Profile* profile) { |
| 74 ProfileOAuth2TokenService* token_service = |
| 75 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 76 |
| 77 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
| 78 |
| 79 return make_scoped_ptr(new InterestsFetcher( |
| 80 token_service, |
| 81 signin->GetAuthenticatedAccountId(), |
| 82 profile->GetRequestContext())); |
| 83 } |
| 84 |
| 85 void InterestsFetcher::FetchInterests( |
| 86 const InterestsFetcher::InterestsCallback& callback) { |
| 87 DCHECK(callback_.is_null()); |
| 88 callback_ = callback; |
| 89 StartOAuth2Request(); |
| 90 } |
| 91 |
| 92 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 93 const net::URLRequestStatus& status = source->GetStatus(); |
| 94 if (!status.is_success()) { |
| 95 VLOG(2) << "Network error " << status.error(); |
| 96 callback_.Run(EmptyResponse()); |
| 97 return; |
| 98 } |
| 99 |
| 100 int response_code = source->GetResponseCode(); |
| 101 // If we get an authorization error, refresh token and retry once. |
| 102 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { |
| 103 access_token_expired_ = true; |
| 104 token_service_->InvalidateAccessToken(account_id_, |
| 105 GetApiScopes(), |
| 106 access_token_); |
| 107 StartOAuth2Request(); |
| 108 return; |
| 109 } |
| 110 |
| 111 if (response_code != net::HTTP_OK) { |
| 112 VLOG(2) << "HTTP error " << response_code; |
| 113 callback_.Run(EmptyResponse()); |
| 114 return; |
| 115 } |
| 116 |
| 117 std::string response_body; |
| 118 source->GetResponseAsString(&response_body); |
| 119 |
| 120 callback_.Run(ExtractInterests(response_body)); |
| 121 } |
| 122 |
| 123 void InterestsFetcher::OnGetTokenSuccess( |
| 124 const OAuth2TokenService::Request* request, |
| 125 const std::string& access_token, |
| 126 const base::Time& expiration_time) { |
| 127 access_token_ = access_token; |
| 128 |
| 129 fetcher_ = CreateFetcher(); |
| 130 fetcher_->SetRequestContext(url_request_context_); |
| 131 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 132 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 133 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 134 |
| 135 fetcher_->AddExtraRequestHeader( |
| 136 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); |
| 137 |
| 138 fetcher_->Start(); |
| 139 } |
| 140 |
| 141 void InterestsFetcher::OnGetTokenFailure( |
| 142 const OAuth2TokenService::Request* request, |
| 143 const GoogleServiceAuthError& error) { |
| 144 DLOG(WARNING) << error.ToString(); |
| 145 |
| 146 callback_.Run(EmptyResponse()); |
| 147 } |
| 148 |
| 149 void InterestsFetcher::StartOAuth2Request() { |
| 150 oauth_request_ = |
| 151 token_service_->StartRequest(account_id_, GetApiScopes(), this); |
| 152 } |
| 153 |
| 154 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { |
| 155 OAuth2TokenService::ScopeSet scopes; |
| 156 scopes.insert(kApiScope); |
| 157 return scopes; |
| 158 } |
| 159 |
| 160 scoped_ptr<net::URLFetcher> InterestsFetcher::CreateFetcher() { |
| 161 return |
| 162 net::URLFetcher::Create(0, GetInterestsURL(), net::URLFetcher::GET, this); |
| 163 } |
| 164 |
| 165 std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( |
| 166 const std::string& response) { |
| 167 scoped_ptr<base::Value> value = base::JSONReader::Read(response); |
| 168 DVLOG(2) << response; |
| 169 |
| 170 const base::DictionaryValue* dict = nullptr; |
| 171 if (!value || !value->GetAsDictionary(&dict)) { |
| 172 DLOG(WARNING) << "Failed to parse global dictionary."; |
| 173 return EmptyResponse(); |
| 174 } |
| 175 |
| 176 const base::ListValue* interests_list = nullptr; |
| 177 if (!dict->GetList(kIdInterests, &interests_list)) { |
| 178 DLOG(WARNING) << "Failed to parse interests list."; |
| 179 return EmptyResponse(); |
| 180 } |
| 181 |
| 182 std::vector<Interest> res; |
| 183 for (const base::Value* entry : *interests_list) { |
| 184 const base::DictionaryValue* interest_dict = nullptr; |
| 185 if (!entry->GetAsDictionary(&interest_dict)) { |
| 186 DLOG(WARNING) << "Failed to parse interest dictionary."; |
| 187 continue; |
| 188 } |
| 189 |
| 190 // Extract the parts of the interest. |
| 191 std::string name; |
| 192 std::string image_url; |
| 193 double relevance = 0; |
| 194 |
| 195 if (!interest_dict->GetString(kIdInterestName, &name)) { |
| 196 DLOG(WARNING) << "Failed to parse interest name."; |
| 197 continue; |
| 198 } |
| 199 |
| 200 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { |
| 201 DLOG(WARNING) << "Failed to parse interest image URL."; |
| 202 // image_url is not mandatory, however warn if omitted. |
| 203 } |
| 204 |
| 205 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { |
| 206 DLOG(WARNING) << "Failed to parse interest relevance."; |
| 207 continue; |
| 208 } |
| 209 |
| 210 res.push_back(Interest{name, GURL(image_url), relevance}); |
| 211 } |
| 212 |
| 213 return res; |
| 214 } |
OLD | NEW |