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