Chromium Code Reviews| Index: components/doodle/doodle_fetcher.cc |
| diff --git a/components/doodle/doodle_fetcher.cc b/components/doodle/doodle_fetcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..96a74cd3ff3735fc5b00c0a62136287ca243e58d |
| --- /dev/null |
| +++ b/components/doodle/doodle_fetcher.cc |
| @@ -0,0 +1,238 @@ |
| +// Copyright 2017 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 "components/doodle/doodle_fetcher.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/time/clock.h" |
| +#include "base/time/default_clock.h" |
| +#include "base/time/time.h" |
| +#include "base/values.h" |
| +#include "components/safe_json/safe_json_parser.h" |
| +#include "net/base/load_flags.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/url_fetcher.h" |
| +#include "url/gurl.h" |
| + |
| +using net::URLFetcher; |
| + |
| +namespace doodle { |
| + |
| +const char kDoodleConfigUrl[] = "/async/ddljson?ogdeb=ct~2000000007"; |
| + |
| +namespace { |
| + |
| +void ParseJson( |
| + const std::string& json, |
| + const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, |
| + const base::Callback<void(const std::string&)>& error_callback) { |
| + // The response may start with )]}'. Ignore this. |
| + const char kResponsePreamble[] = ")]}'"; |
| + |
| + base::StringPiece json_sp(json); |
| + if (json_sp.starts_with(kResponsePreamble)) { |
| + json_sp.remove_prefix(strlen(kResponsePreamble)); |
| + } |
| + |
| + safe_json::SafeJsonParser::Parse(json_sp.as_string(), success_callback, |
| + error_callback); |
| +} |
| + |
| +DoodleType ParseDoodleType(const base::DictionaryValue* ddljson) { |
| + std::string type_str; |
| + ddljson->GetString("doodle_type", &type_str); |
| + if (type_str == "SIMPLE") { |
| + return DoodleType::SIMPLE; |
| + } |
| + if (type_str == "RANDOM") { |
| + return DoodleType::RANDOM; |
| + } |
| + if (type_str == "VIDEO") { |
| + return DoodleType::VIDEO; |
| + } |
| + if (type_str == "INTERACTIVE") { |
| + return DoodleType::INTERACTIVE; |
| + } |
| + if (type_str == "INLINE_INTERACTIVE") { |
| + return DoodleType::INLINE_INTERACTIVE; |
| + } |
| + if (type_str == "SLIDESHOW") { |
| + return DoodleType::SLIDESHOW; |
| + } |
| + return DoodleType::SIMPLE; |
| +} |
| + |
| +} // namespace |
| + |
| +DoodleImage::DoodleImage() |
| + : height(0), |
| + width(0), |
| + background_color("#ffffff"), |
| + is_animated_gif(false), |
| + is_cta(false) {} |
| +DoodleImage::DoodleImage(const DoodleImage& image) = default; |
| +DoodleImage::~DoodleImage() = default; |
| + |
| +DoodleConfig::DoodleConfig(DoodleState doodle_state) |
| + : state(doodle_state), |
| + doodle_type(DoodleType::SIMPLE), |
| + expiry_date(base::Time::Now()) {} |
| +DoodleConfig::~DoodleConfig() = default; |
| +DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; |
| + |
| +DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, |
| + GURL base_url) |
| + : DoodleFetcher(download_context, base_url, base::Bind(ParseJson)) {} |
| + |
| +DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, |
| + GURL base_url, |
| + ParseJSONCallback json_parsing_callback) |
| + : download_context_(download_context), |
| + json_parsing_callback_(json_parsing_callback), |
| + base_url_(base_url), |
| + clock_(new base::DefaultClock()), |
| + weak_ptr_factory_(this) {} |
| + |
| +DoodleFetcher::~DoodleFetcher() {} |
| + |
| +void DoodleFetcher::FetchDoodle(FinishedCallback callback) { |
| + callback_lock_.Acquire(); |
|
Marc Treib
2017/02/01 10:55:58
Is this required? I don't see why we would ever us
fhorschig
2017/02/01 11:16:13
Removed.
|
| + callbacks_.push_back(std::move(callback)); |
| + if (callbacks_.size() > 1) { |
| + callback_lock_.Release(); |
| + return; // With 2 or more callbacks, a fetcher is already running. |
| + } |
| + callback_lock_.Release(); |
| + fetcher_ = URLFetcher::Create(base_url_.Resolve(kDoodleConfigUrl), |
| + URLFetcher::GET, this); |
| + fetcher_->SetRequestContext(download_context_); |
| + fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| + net::LOAD_DO_NOT_SAVE_COOKIES); |
| + fetcher_->SetAutomaticallyRetryOnNetworkChanges(1); |
| + fetcher_->Start(); |
| +} |
| + |
| +// net::URLFetcherDelegate implementation. |
| +void DoodleFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| + DCHECK_EQ(fetcher_.get(), source); |
| + std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_); |
| + |
| + std::string json_string; |
| + if (!(source->GetStatus().is_success() && |
| + source->GetResponseCode() == net::HTTP_OK && |
| + source->GetResponseAsString(&json_string))) { |
| + RespondToAllCallbacks(DoodleConfig(DoodleState::DOWNLOAD_ERROR)); |
| + return; |
| + } |
| + |
| + ParseJson(json_string, base::Bind(&DoodleFetcher::OnJsonParsed, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Bind(&DoodleFetcher::OnJsonParseFailed, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +// ParseJSONCallback Success callback |
| +void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) { |
| + std::unique_ptr<base::DictionaryValue> dict = |
| + base::DictionaryValue::From(std::move(json)); |
| + if (!dict.get()) { |
| + LOG(ERROR) << "Doodle JSON is not valid"; |
| + RespondToAllCallbacks(DoodleConfig(DoodleState::PARSING_ERROR)); |
| + return; |
| + } |
| + |
| + ParseDoodle(std::move(dict)); |
| +} |
| + |
| +// ParseJSONCallback Failure callback |
| +void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) { |
| + LOG(ERROR) << "JSON parsing failed: " << error_message; |
| + RespondToAllCallbacks(DoodleConfig(DoodleState::PARSING_ERROR)); |
| +} |
| + |
| +void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) { |
| + const base::DictionaryValue* ddljson = nullptr; |
| + if (!config->GetDictionary("ddljson", &ddljson)) { |
| + LOG(ERROR) << "Doodle JSON reponse did not contain 'ddljson' key."; |
| + RespondToAllCallbacks(DoodleConfig(DoodleState::PARSING_ERROR)); |
| + return; |
| + } |
| + |
| + DoodleConfig result(DoodleState::AVAILABLE); |
| + ParseBaseInformation(ddljson, &result); |
| + |
| + if (!ParseImage(ddljson, "large_image", &result.large_image)) { |
| + RespondToAllCallbacks(DoodleConfig(DoodleState::NO_DOODLE)); |
| + return; |
| + } |
| + ParseImage(ddljson, "transparent_large_image", |
| + &result.transparent_large_image); |
| + ParseImage(ddljson, "hires_image", &result.hires_image); |
| + ParseImage(ddljson, "medium_image", &result.medium_image); |
| + ParseImage(ddljson, "small_image", &result.small_image); |
| + |
| + RespondToAllCallbacks(std::move(result)); |
| +} |
| + |
| +bool DoodleFetcher::ParseImage(const base::DictionaryValue* image_parent, |
| + const std::string& image_name, |
| + DoodleImage* image) const { |
| + DCHECK(image); |
| + const base::DictionaryValue* image_dict = nullptr; |
| + if (!(image && image_parent && |
| + image_parent->GetDictionary(image_name, &image_dict))) { |
| + return false; |
| + } |
| + std::string url; |
| + image_dict->GetString("url", &url); |
| + image->url = base_url_.Resolve(url); |
| + image_dict->GetString("background_color", &image->background_color); |
| + image_dict->GetInteger("height", &image->height); |
| + image_dict->GetInteger("width", &image->width); |
| + image_dict->GetBoolean("is_animated_gif", &image->is_animated_gif); |
| + image_dict->GetBoolean("is_cta", &image->is_cta); |
| + return true; |
| +} |
| + |
| +void DoodleFetcher::ParseBaseInformation(const base::DictionaryValue* ddljson, |
| + DoodleConfig* config) const { |
| + ParseRelativeUrl(ddljson, "search_url", &config->search_url); |
| + ParseRelativeUrl(ddljson, "target_url", &config->target_url); |
| + ParseRelativeUrl(ddljson, "fullpage_interactive_url", |
| + &config->fullpage_interactive_url); |
| + |
| + config->doodle_type = ParseDoodleType(ddljson); |
| + ddljson->GetString("alt_text", &config->alt_text); |
| + |
| + // TODO(fhorschig): Inject TickClock and test that. |
| + // The JSON doesn't garantuee the number to fit into an int. |
| + double ttl; |
| + ddljson->GetDouble("time_to_live_ms", &ttl); |
| + config->expiry_date = clock_->Now() + base::TimeDelta::FromMillisecondsD(ttl); |
| +} |
| + |
| +void DoodleFetcher::ParseRelativeUrl(const base::DictionaryValue* dict_value, |
| + const std::string& key, |
| + GURL* url) const { |
| + std::string str_url; |
| + dict_value->GetString(key, &str_url); |
| + if (str_url.empty()) { |
| + *url = GURL(); |
| + } else { |
| + *url = base_url_.Resolve(str_url); |
| + } |
| +} |
| + |
| +void DoodleFetcher::RespondToAllCallbacks(const DoodleConfig& response) { |
| + callback_lock_.Acquire(); |
| + while (!callbacks_.empty()) { |
| + std::move(callbacks_.front()).Run(response); |
| + callbacks_.pop_front(); |
| + } |
| + callback_lock_.Release(); |
| +} |
| + |
| +} // namespace doodle |