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 GURL GetInterestsURL() { |
| 35 const base::CommandLine* command_line = |
| 36 base::CommandLine::ForCurrentProcess(); |
| 37 return GURL(command_line->GetSwitchValueASCII(switches::kInterestsURL)); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 InterestsFetcher::Interest::Interest(const std::string& name, |
| 43 const GURL& image_url, |
| 44 double relevance) |
| 45 : name(name), image_url(image_url), relevance(relevance) {} |
| 46 |
| 47 InterestsFetcher::Interest::~Interest() {} |
| 48 |
| 49 bool InterestsFetcher::Interest::operator==(const Interest& interest) const { |
| 50 return name == interest.name && image_url == interest.image_url && |
| 51 relevance == interest.relevance; |
| 52 } |
| 53 |
| 54 InterestsFetcher::InterestsFetcher( |
| 55 OAuth2TokenService* oauth2_token_service, |
| 56 const std::string& account_id, |
| 57 net::URLRequestContextGetter* url_request_context) |
| 58 : OAuth2TokenService::Consumer("interests_fetcher"), |
| 59 account_id_(account_id), |
| 60 url_request_context_(url_request_context), |
| 61 access_token_expired_(false), |
| 62 token_service_(oauth2_token_service) { |
| 63 } |
| 64 |
| 65 InterestsFetcher::~InterestsFetcher() {} |
| 66 |
| 67 // static |
| 68 scoped_ptr<InterestsFetcher> InterestsFetcher::CreateFromProfile( |
| 69 Profile* profile) { |
| 70 ProfileOAuth2TokenService* token_service = |
| 71 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 72 |
| 73 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile); |
| 74 |
| 75 return make_scoped_ptr(new InterestsFetcher( |
| 76 token_service, |
| 77 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 VLOG(2) << "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 access_token_expired_ = true; |
| 100 token_service_ |
| 101 ->InvalidateAccessToken(account_id_, GetApiScopes(), access_token_); |
| 102 StartOAuth2Request(); |
| 103 return; |
| 104 } |
| 105 |
| 106 if (response_code != net::HTTP_OK) { |
| 107 VLOG(2) << "HTTP error " << response_code; |
| 108 callback_.Run(nullptr); |
| 109 return; |
| 110 } |
| 111 |
| 112 std::string response_body; |
| 113 source->GetResponseAsString(&response_body); |
| 114 |
| 115 callback_.Run(ExtractInterests(response_body)); |
| 116 } |
| 117 |
| 118 void InterestsFetcher::OnGetTokenSuccess( |
| 119 const OAuth2TokenService::Request* request, |
| 120 const std::string& access_token, |
| 121 const base::Time& expiration_time) { |
| 122 access_token_ = access_token; |
| 123 |
| 124 fetcher_ = CreateFetcher(); |
| 125 fetcher_->SetRequestContext(url_request_context_); |
| 126 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 127 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 128 fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| 129 |
| 130 fetcher_->AddExtraRequestHeader( |
| 131 base::StringPrintf(kAuthorizationHeaderFormat, access_token_.c_str())); |
| 132 |
| 133 fetcher_->Start(); |
| 134 } |
| 135 |
| 136 void InterestsFetcher::OnGetTokenFailure( |
| 137 const OAuth2TokenService::Request* request, |
| 138 const GoogleServiceAuthError& error) { |
| 139 DLOG(WARNING) << error.ToString(); |
| 140 |
| 141 callback_.Run(nullptr); |
| 142 } |
| 143 |
| 144 void InterestsFetcher::StartOAuth2Request() { |
| 145 oauth_request_ = |
| 146 token_service_->StartRequest(account_id_, GetApiScopes(), this); |
| 147 } |
| 148 |
| 149 OAuth2TokenService::ScopeSet InterestsFetcher::GetApiScopes() { |
| 150 OAuth2TokenService::ScopeSet scopes; |
| 151 scopes.insert(kApiScope); |
| 152 return scopes; |
| 153 } |
| 154 |
| 155 scoped_ptr<net::URLFetcher> InterestsFetcher::CreateFetcher() { |
| 156 return |
| 157 net::URLFetcher::Create(0, GetInterestsURL(), net::URLFetcher::GET, this); |
| 158 } |
| 159 |
| 160 scoped_ptr<std::vector<InterestsFetcher::Interest>> |
| 161 InterestsFetcher::ExtractInterests(const std::string& response) { |
| 162 scoped_ptr<base::Value> value = base::JSONReader::Read(response); |
| 163 DVLOG(2) << response; |
| 164 |
| 165 const base::DictionaryValue* dict = nullptr; |
| 166 if (!value || !value->GetAsDictionary(&dict)) { |
| 167 DLOG(WARNING) << "Failed to parse global dictionary."; |
| 168 return nullptr; |
| 169 } |
| 170 |
| 171 const base::ListValue* interests_list = nullptr; |
| 172 if (!dict->GetList(kIdInterests, &interests_list)) { |
| 173 DLOG(WARNING) << "Failed to parse interests list."; |
| 174 return nullptr; |
| 175 } |
| 176 |
| 177 scoped_ptr<std::vector<Interest>> res(new std::vector<Interest>()); |
| 178 for (const base::Value* entry : *interests_list) { |
| 179 const base::DictionaryValue* interest_dict = nullptr; |
| 180 if (!entry->GetAsDictionary(&interest_dict)) { |
| 181 DLOG(WARNING) << "Failed to parse interest dictionary."; |
| 182 continue; |
| 183 } |
| 184 |
| 185 // Extract the parts of the interest. |
| 186 std::string name; |
| 187 std::string image_url; |
| 188 double relevance; |
| 189 |
| 190 if (!interest_dict->GetString(kIdInterestName, &name)) { |
| 191 DLOG(WARNING) << "Failed to parse interest name."; |
| 192 continue; |
| 193 } |
| 194 |
| 195 if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { |
| 196 DLOG(WARNING) << "Failed to parse interest image URL."; |
| 197 // image_url is not mandatory, however warn if omitted. |
| 198 } |
| 199 |
| 200 if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { |
| 201 DLOG(WARNING) << "Failed to parse interest relevance."; |
| 202 continue; |
| 203 } |
| 204 |
| 205 res->push_back(Interest{name, GURL(image_url), relevance}); |
| 206 } |
| 207 |
| 208 return res; |
| 209 } |
OLD | NEW |