Chromium Code Reviews| 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 GURL interests_url( | |
| 42 command_line->GetSwitchValueASCII(switches::kInterestsURL)); | |
| 43 return interests_url; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 InterestsFetcher::InterestsFetcher( | |
| 49 OAuth2TokenService* oauth2_token_service, | |
| 50 const std::string& account_id, | |
| 51 net::URLRequestContextGetter* url_request_context) | |
| 52 : OAuth2TokenService::Consumer("interests_fetcher"), | |
| 53 token_service_(oauth2_token_service), | |
| 54 account_id_(account_id), | |
| 55 url_request_context_(url_request_context), | |
| 56 access_token_expired_(false) { | |
| 57 } | |
| 58 | |
| 59 InterestsFetcher::~InterestsFetcher() {} | |
| 60 | |
| 61 // static | |
| 62 scoped_ptr<InterestsFetcher> | |
| 63 InterestsFetcher::CreateFromProfile(Profile* profile) { | |
| 64 ProfileOAuth2TokenService* token_service = | |
| 65 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 66 | |
| 67 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
| 68 | |
| 69 return make_scoped_ptr(new InterestsFetcher( | |
| 70 token_service, | |
| 71 signin->GetAuthenticatedAccountId(), | |
| 72 profile->GetRequestContext())); | |
| 73 } | |
| 74 | |
| 75 void InterestsFetcher::Start( | |
| 76 const InterestsFetcher::InterestsCallback& callback) { | |
| 77 DCHECK(callback_.is_null()); | |
| 78 callback_ = callback; | |
| 79 StartOAuth2Request(); | |
| 80 } | |
| 81 | |
| 82 void InterestsFetcher::StartOAuth2Request(){ | |
| 83 oauth_request_ = | |
| 84 token_service_->StartRequest(account_id_, GetApiScopes(), this); | |
| 85 } | |
| 86 | |
| 87 void InterestsFetcher::OnGetTokenFailure( | |
| 88 const OAuth2TokenService::Request* request, | |
| 89 const GoogleServiceAuthError& error) { | |
| 90 DLOG(WARNING) << error.ToString(); | |
| 91 | |
| 92 callback_.Run(EmptyResponse()); | |
| 93 } | |
| 94 | |
| 95 void InterestsFetcher::OnGetTokenSuccess( | |
| 96 const OAuth2TokenService::Request* request, | |
| 97 const std::string& access_token, | |
| 98 const base::Time& expiration_time) { | |
| 99 | |
| 100 access_token_ = access_token; | |
| 101 fetcher_ = CreateFetcher(); | |
| 102 | |
| 103 // Setup fetcher | |
| 104 fetcher_->SetRequestContext(url_request_context_); | |
| 105 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 106 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 107 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
| 108 | |
| 109 // Add oauth access token | |
|
jochen (gone - plz use gerrit)
2015/10/02 13:51:00
please no comments of the form
// Do foo
DoFoo()
PEConn
2015/10/05 14:12:04
Done.
| |
| 110 fetcher_->AddExtraRequestHeader( | |
| 111 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); | |
| 112 | |
| 113 fetcher_->Start(); | |
| 114 | |
|
jochen (gone - plz use gerrit)
2015/10/02 13:51:00
no empty line here
PEConn
2015/10/05 14:12:04
Done.
| |
| 115 } | |
| 116 | |
| 117 scoped_ptr<net::URLFetcher> InterestsFetcher::CreateFetcher() { | |
| 118 return net::URLFetcher::Create(0, GetInterestsURL(), net::URLFetcher::GET, | |
| 119 this); | |
| 120 } | |
| 121 | |
| 122 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 123 | |
| 124 const net::URLRequestStatus& status = source->GetStatus(); | |
| 125 if (!status.is_success()) { | |
| 126 LOG(WARNING) << "Network error " << status.error(); | |
| 127 callback_.Run(EmptyResponse()); | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 int response_code = source->GetResponseCode(); | |
| 132 // If we get an authorization error, refresh token and retry once. | |
| 133 if (response_code == net::HTTP_UNAUTHORIZED && !access_token_expired_) { | |
| 134 access_token_expired_ = true; | |
| 135 token_service_->InvalidateAccessToken(account_id_, GetApiScopes(), | |
| 136 access_token_); | |
| 137 StartOAuth2Request(); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 if (response_code != net::HTTP_OK) { | |
| 142 LOG(WARNING) << "HTTP error " << response_code; | |
|
jochen (gone - plz use gerrit)
2015/10/02 13:51:00
VLOG(2), here and everywhere
PEConn
2015/10/05 14:12:04
Done.
| |
| 143 callback_.Run(EmptyResponse()); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 std::string response_body; | |
| 148 source->GetResponseAsString(&response_body); | |
| 149 | |
| 150 callback_.Run(ExtractInterests(response_body)); | |
| 151 } | |
| 152 | |
| 153 std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( | |
| 154 const std::string& response) { | |
| 155 scoped_ptr<base::Value> value = base::JSONReader::Read(response); | |
| 156 | |
| 157 const base::DictionaryValue* dict = nullptr; | |
| 158 if (!value || !value->GetAsDictionary(&dict)) { | |
| 159 DLOG(WARNING) << "ExtractInterests failed to parse global dictionary"; | |
| 160 return EmptyResponse(); | |
| 161 } | |
| 162 | |
| 163 const base::ListValue* interests_list = nullptr; | |
| 164 std::vector<Interest> res; | |
| 165 | |
| 166 if (!dict->GetList(kIdInterests, &interests_list)) { | |
| 167 DLOG(WARNING) << "ExtractInterests failed to parse interests list"; | |
| 168 return EmptyResponse(); | |
| 169 } | |
| 170 | |
| 171 for (const base::Value* entry : *interests_list) { | |
| 172 const base::DictionaryValue* interest_dict = nullptr; | |
| 173 if (!entry->GetAsDictionary(&interest_dict)) { | |
| 174 DLOG(WARNING) << "ExtractInterests failed to parse interest dictionary"; | |
| 175 return EmptyResponse(); | |
| 176 } | |
| 177 | |
| 178 std::string name; | |
| 179 if (!interest_dict->GetString(kIdInterestName, &name)) { | |
| 180 DLOG(WARNING) << "ExtractInterests failed to parse interest name"; | |
| 181 return EmptyResponse(); | |
| 182 } | |
| 183 | |
| 184 std::string image_url; | |
| 185 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { | |
| 186 // image_url is allowed to be missing. | |
| 187 // | |
| 188 // However this is still logged as a warning, since, currently, the server | |
| 189 // should always provide an image_url. | |
| 190 DLOG(WARNING) << "ExtractInterests failed to parse interest image URL"; | |
| 191 } | |
| 192 | |
| 193 double relevance; | |
| 194 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | |
| 195 DLOG(WARNING) << "ExtractInterests failed to parse interest relevance"; | |
| 196 return EmptyResponse(); | |
| 197 } | |
| 198 | |
| 199 res.push_back(Interest{name, GURL(image_url), relevance}); | |
| 200 } | |
| 201 | |
| 202 return res; | |
| 203 } | |
| 204 | |
| 205 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { | |
| 206 OAuth2TokenService::ScopeSet scopes; | |
| 207 scopes.insert(kApiScope); | |
| 208 return scopes; | |
| 209 } | |
| 210 | |
| 211 InterestsFetcher::Interest::Interest(const std::string& name, | |
|
jochen (gone - plz use gerrit)
2015/10/02 13:51:00
i'd recommend to use the same order in the cc file
PEConn
2015/10/05 14:12:04
Done.
| |
| 212 const GURL& image_url, float relevance) | |
| 213 : name(name), image_url(image_url), relevance(relevance) {} | |
| 214 | |
| 215 InterestsFetcher::Interest::~Interest() {} | |
| 216 | |
| 217 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { | |
| 218 return name == interest.name && | |
| 219 image_url == interest.image_url && | |
| 220 relevance == interest.relevance; | |
| 221 } | |
| OLD | NEW |