Chromium Code Reviews| Index: chrome/browser/interests/interests_fetcher.cc |
| diff --git a/chrome/browser/interests/interests_fetcher.cc b/chrome/browser/interests/interests_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dd9d1e2cbfc0600e1bd7f1abb81b68531b9cfe81 |
| --- /dev/null |
| +++ b/chrome/browser/interests/interests_fetcher.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/interests/interests_fetcher.h" |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/logging.h" |
| +#include "base/values.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "net/url_request/url_fetcher_factory.h" |
|
Marc Treib
2015/09/22 12:02:59
not needed
tache
2015/09/29 09:33:34
Done.
|
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "net/url_request/url_request_status.h" |
| + |
| +using net::URLFetcher; |
| +using net::URLFetcherDelegate; |
| +using net::URLRequestContextGetter; |
| +using net::URLRequestStatus; |
| +using net::URLFetcherFactory; |
|
Marc Treib
2015/09/22 12:02:59
not needed
tache
2015/09/29 09:33:33
Done.
|
| + |
| +namespace { |
| + |
| +const int kNumRetries = 1; |
| +const char kIdInterests[] = "interest"; |
| +const char kIdInterestName[] = "name"; |
| +const char kIdInterestImageUrl[] = "imageUrl"; |
| +const char kIdInterestRelevance[] = "relevance"; |
| + |
| +const char kInterestsUrl[] = "https://www.googleapis.com/TBD/v1/interests"; |
|
Marc Treib
2015/09/22 12:02:59
Do we know the correct URL? (Even if it doesn't ac
tache
2015/09/29 09:33:34
This should be https://www-googleapis-test.sandbox
|
| + |
| +std::vector<InterestsFetcher::Interest> EmptyResponse() { |
| + return std::vector<InterestsFetcher::Interest>(); |
| +} |
| + |
| +} // namespace |
| + |
| +InterestsFetcher::InterestsFetcher( |
| + net::URLRequestContextGetter* url_request_context, |
| + const std::string& access_token) |
| + : url_request_context_(url_request_context), |
| + access_token_(access_token) { |
| +} |
| + |
| +void InterestsFetcher::Start( |
| + const InterestsFetcher::InterestsCallback& callback) { |
| + DCHECK(callback_.is_null()); |
| + callback_ = callback; |
| + |
| + fetcher_ = CreateFetcher(); |
| + |
| + // Setup fetcher |
| + fetcher_->SetRequestContext(url_request_context_); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + fetcher_->SetAutomaticallyRetryOnNetworkChanges(kNumRetries); |
| + |
| + // Add oauth access token |
| + fetcher_->AddExtraRequestHeader(std::string("Authorization: Bearer ") + |
| + access_token_); |
| + |
| + fetcher_->Start(); |
| +} |
| + |
| +scoped_ptr<URLFetcher> InterestsFetcher::CreateFetcher() { |
| + return URLFetcher::Create(GURL(kInterestsUrl), URLFetcher::GET, this); |
| +} |
| + |
| +void InterestsFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| + const URLRequestStatus& status = source->GetStatus(); |
| + |
| + if (!status.is_success()) { |
| + DLOG(WARNING) << "URL request failed!"; |
| + callback_.Run(EmptyResponse()); |
| + return; |
| + } |
| + |
| + std::string response_body; |
| + source->GetResponseAsString(&response_body); |
| + |
| + callback_.Run(ExtractInterests(response_body)); |
| +} |
| + |
| +std::vector<InterestsFetcher::Interest> InterestsFetcher::ExtractInterests( |
| + const std::string& response) { |
| + scoped_ptr<base::Value> value = base::JSONReader::Read(response); |
| + |
| + const base::DictionaryValue* dict = nullptr; |
| + if (!value || !value->GetAsDictionary(&dict)) { |
| + DLOG(WARNING) << "ExtractInterests failed to parse global dictionary"; |
| + return EmptyResponse(); |
| + } |
| + |
| + const base::ListValue* interests_list = nullptr; |
| + std::vector<Interest> res; |
| + |
| + if (!dict->GetList(kIdInterests, &interests_list)) { |
| + DLOG(WARNING) << "ExtractInterests failed to parse interests list"; |
| + return EmptyResponse(); |
| + } |
| + |
| + for (const base::Value* entry : *interests_list) { |
| + const base::DictionaryValue* interest_dict = nullptr; |
| + if (!entry->GetAsDictionary(&interest_dict)) { |
| + DLOG(WARNING) << "ExtractInterests failed to parse interest dictionary"; |
| + return EmptyResponse(); |
| + } |
| + |
| + std::string name; |
| + if (!interest_dict->GetString(kIdInterestName, &name)) { |
| + DLOG(WARNING) << "ExtractInterests failed to parse interest name"; |
| + return EmptyResponse(); |
| + } |
| + |
| + std::string image_url; |
| + if (!interest_dict->GetString(kIdInterestImageUrl, &image_url)) { |
| + // image_url is allowed to be missing. |
| + // |
| + // However this is still logged as a warning, since, currently, the server |
| + // should always provide an image_url. |
| + // |
|
Marc Treib
2015/09/22 12:02:59
nit: remove empty comment line
tache
2015/09/29 09:33:33
Done.
|
| + DLOG(WARNING) << "ExtractInterests failed to parse interest image URL"; |
| + } |
| + |
| + double relevance; |
| + if (!interest_dict->GetDouble(kIdInterestRelevance, &relevance)) { |
| + DLOG(WARNING) << "ExtractInterests failed to parse interest relevance"; |
| + return EmptyResponse(); |
| + } |
| + |
| + res.push_back(Interest{name, image_url, relevance}); |
| + } |
| + |
| + return res; |
| +} |
| + |
| +InterestsFetcher::~InterestsFetcher() {} |
| + |
| +bool InterestsFetcher::Interest::operator==(const Interest& interest) const { |
| + return name == interest.name && |
| + image_url == interest.image_url && |
| + relevance == interest.relevance; |
| +} |