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/json/json_reader.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 14 #include "net/base/load_flags.h" | |
| 15 #include "net/http/http_status_code.h" | |
| 16 #include "net/url_request/url_fetcher.h" | |
| 17 #include "net/url_request/url_request_context_getter.h" | |
| 18 #include "net/url_request/url_request_status.h" | |
| 19 | |
| 20 using net::URLFetcher; | |
| 21 using net::URLFetcherDelegate; | |
| 22 using net::URLRequestContextGetter; | |
| 23 using net::URLRequestStatus; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const int kNumRetries = 1; | |
| 28 const char kIdInterests[] = "interests"; | |
| 29 const char kIdInterestName[] = "name"; | |
| 30 const char kIdInterestImageUrl[] = "imageUrl"; | |
| 31 const char kIdInterestRelevance[] = "relevance"; | |
| 32 | |
| 33 const char kInterestsUrl[] = | |
| 34 "https://www-googleapis-stagin.sandbox.google.com/chromenow/contextdemo/" | |
|
Bernhard Bauer
2015/09/29 14:01:12
stagin? Also, instead of referencing internal URLs
tache
2015/09/29 19:58:23
Done.
| |
| 35 "interests/"; | |
| 36 const char kApiScope[] = "https://www.googleapis.com/auth/googlenow"; | |
| 37 | |
| 38 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | |
| 39 | |
| 40 std::vector<InterestsFetcher::Interest> EmptyResponse() { | |
| 41 return std::vector<InterestsFetcher::Interest>(); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 InterestsFetcher::InterestsFetcher( | |
| 47 OAuth2TokenService* oauth2_token_service, | |
| 48 const std::string& account_id, | |
| 49 net::URLRequestContextGetter* url_request_context) | |
| 50 : OAuth2TokenService::Consumer("interests_fetcher"), | |
| 51 token_service_(oauth2_token_service), | |
| 52 account_id_(account_id), | |
| 53 url_request_context_(url_request_context) { | |
| 54 } | |
| 55 | |
| 56 // static | |
| 57 scoped_ptr<InterestsFetcher> | |
| 58 InterestsFetcher::CreateFromProfile(Profile* profile) { | |
| 59 ProfileOAuth2TokenService* token_service = | |
| 60 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 61 | |
| 62 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); | |
| 63 | |
| 64 return make_scoped_ptr(new InterestsFetcher( | |
| 65 token_service, | |
| 66 signin->GetAuthenticatedAccountId(), | |
| 67 profile->GetRequestContext())); | |
| 68 } | |
| 69 | |
| 70 void InterestsFetcher::Start( | |
| 71 const InterestsFetcher::InterestsCallback& callback) { | |
| 72 DCHECK(callback_.is_null()); | |
| 73 callback_ = callback; | |
| 74 | |
| 75 OAuth2TokenService::ScopeSet scopes; | |
| 76 scopes.insert(kApiScope); | |
| 77 | |
| 78 oauth_request_ = token_service_->StartRequest(account_id_, scopes, this); | |
| 79 } | |
| 80 | |
| 81 void InterestsFetcher::OnGetTokenFailure( | |
| 82 const OAuth2TokenService::Request* request, | |
| 83 const GoogleServiceAuthError& error) { | |
| 84 DLOG(WARNING) << error.ToString(); | |
| 85 | |
| 86 callback_.Run(EmptyResponse()); | |
| 87 } | |
| 88 | |
| 89 void InterestsFetcher::OnGetTokenSuccess( | |
| 90 const OAuth2TokenService::Request* request, | |
| 91 const std::string& access_token, | |
| 92 const base::Time& expiration_time) { | |
| 93 | |
| 94 fetcher_ = CreateFetcher(); | |
| 95 | |
| 96 // Setup fetcher | |
| 97 fetcher_->SetRequestContext(url_request_context_); | |
| 98 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 99 net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 100 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); | |
| 101 | |
| 102 // Add oauth access token | |
| 103 fetcher_->AddExtraRequestHeader( | |
| 104 base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str())); | |
| 105 | |
| 106 fetcher_->Start(); | |
| 107 | |
| 108 } | |
| 109 | |
| 110 | |
| 111 scoped_ptr<URLFetcher> InterestsFetcher::CreateFetcher() { | |
| 112 return URLFetcher::Create(0, GURL(kInterestsUrl), URLFetcher::GET, this); | |
| 113 } | |
| 114 | |
| 115 void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 116 const URLRequestStatus& status = source->GetStatus(); | |
| 117 | |
| 118 if (!status.is_success()) { | |
| 119 DLOG(WARNING) << "URL request failed!"; | |
|
Bernhard Bauer
2015/09/29 14:01:12
At least include the status in the log message.
tache
2015/09/29 19:58:23
Done.
| |
| 120 callback_.Run(EmptyResponse()); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 std::string response_body; | |
| 125 source->GetResponseAsString(&response_body); | |
|
Bernhard Bauer
2015/09/29 14:01:12
This doesn't seem to deal with HTTP errors, includ
tache
2015/09/29 19:58:23
Improved logging in case of HTTP errors.
Also in
| |
| 126 | |
| 127 callback_.Run(ExtractInterests(response_body)); | |
| 128 } | |
| 129 | |
| 130 std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( | |
| 131 const std::string& response) { | |
| 132 scoped_ptr<base::Value> value = base::JSONReader::Read(response); | |
| 133 | |
| 134 const base::DictionaryValue* dict = nullptr; | |
| 135 if (!value || !value->GetAsDictionary(&dict)) { | |
| 136 DLOG(WARNING) << "ExtractInterests failed to parse global dictionary"; | |
| 137 return EmptyResponse(); | |
| 138 } | |
| 139 | |
| 140 const base::ListValue* interests_list = nullptr; | |
| 141 std::vector<Interest> res; | |
|
Bernhard Bauer
2015/09/29 14:01:12
If you declare this further up, you could just alw
tache
2015/09/29 19:58:23
I also use EmptyResponse() in other methods for th
| |
| 142 | |
| 143 if (!dict->GetList(kIdInterests, &interests_list)) { | |
| 144 DLOG(WARNING) << "ExtractInterests failed to parse interests list"; | |
| 145 return EmptyResponse(); | |
| 146 } | |
| 147 | |
| 148 for (const base::Value* entry : *interests_list) { | |
| 149 const base::DictionaryValue* interest_dict = nullptr; | |
| 150 if (!entry->GetAsDictionary(&interest_dict)) { | |
| 151 DLOG(WARNING) << "ExtractInterests failed to parse interest dictionary"; | |
| 152 return EmptyResponse(); | |
| 153 } | |
| 154 | |
| 155 std::string name; | |
| 156 if (!interest_dict->GetString(kIdInterestName, &name)) { | |
| 157 DLOG(WARNING) << "ExtractInterests failed to parse interest name"; | |
| 158 return EmptyResponse(); | |
| 159 } | |
| 160 | |
| 161 std::string image_url; | |
| 162 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { | |
| 163 // image_url is allowed to be missing. | |
| 164 // | |
| 165 // However this is still logged as a warning, since, currently, the server | |
| 166 // should always provide an image_url. | |
| 167 DLOG(WARNING) << "ExtractInterests failed to parse interest image URL"; | |
| 168 } | |
| 169 | |
| 170 double relevance; | |
| 171 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { | |
| 172 DLOG(WARNING) << "ExtractInterests failed to parse interest relevance"; | |
| 173 return EmptyResponse(); | |
| 174 } | |
| 175 | |
| 176 res.push_back(Interest{name, image_url, relevance}); | |
| 177 } | |
| 178 | |
| 179 return res; | |
| 180 } | |
| 181 | |
| 182 InterestsFetcher::~InterestsFetcher() {} | |
| 183 | |
| 184 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { | |
| 185 return name == interest.name && | |
| 186 image_url == interest.image_url && | |
| 187 relevance == interest.relevance; | |
| 188 } | |
| OLD | NEW |