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..e35516711fd9bf1d0668353787cfa72a22e1e22c |
| --- /dev/null |
| +++ b/components/doodle/doodle_fetcher.cc |
| @@ -0,0 +1,141 @@ |
| +// 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/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[] = |
| + "https://www.google.com/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, |
|
Marc Treib
2017/01/31 09:46:34
This is okay for now, but eventually it'll have to
fhorschig
2017/01/31 13:59:18
Now,there is a constructor that allows injecting a
|
| + error_callback); |
| +} |
| + |
| +} // namespace |
| + |
| +DoodleConfig::DoodleConfig() : state(DoodleState::UNKNOWN) {} |
| +DoodleConfig::DoodleConfig(DoodleState doodle_state) : state(doodle_state) {} |
| +DoodleConfig::~DoodleConfig() = default; |
| +DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; |
| + |
| +DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context, |
| + GURL base_url) |
| + : download_context_(download_context), |
| + base_url_(base_url), |
| + weak_ptr_factory_(this) {} |
| + |
| +DoodleFetcher::~DoodleFetcher() {} |
| + |
| +void DoodleFetcher::FetchDoodle(const FinishedCallback& callback) { |
| + DCHECK(!callback_); |
| + callback_ = callback; |
| + // TODO(fhorschig): Store multiple callbacks. |
| + // TODO(fhorschig): Cache responses? |
| + fetcher_ = URLFetcher::Create(GURL(kDoodleConfigUrl), URLFetcher::GET, this); |
|
Marc Treib
2017/01/31 09:46:34
This should use the passed-in base_url too
fhorschig
2017/01/31 13:59:18
Done.
|
| + 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))) { |
| + callback_.Run(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"; |
| + callback_.Run(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; |
| + callback_.Run(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."; |
| + callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR)); |
| + return; |
| + } |
| + const base::DictionaryValue* image = nullptr; |
| + if (!ddljson->GetDictionary("large_image", &image)) { |
| + callback_.Run(DoodleConfig(DoodleState::NO_DOODLE)); |
| + return; |
| + } |
| + std::string temp; |
| + DoodleConfig result(DoodleState::AVAILABLE); |
| + ddljson->GetString("search_url", &temp); |
|
Marc Treib
2017/01/31 09:46:34
What if this (or any of the other url fields) is e
fhorschig
2017/01/31 13:59:18
I now return an empty GURL, so is_valid() will fai
|
| + result.search_url = base_url_.Resolve(temp); |
| + ddljson->GetString("doodle_type", &result.doodle_type); |
| + double timediff; |
| + ddljson->GetDouble("time_to_live_ms", &timediff); |
|
Marc Treib
2017/01/31 09:46:34
nit: Add a comment on why double
fhorschig
2017/01/31 13:59:18
Done.
|
| + result.time_to_live_ms = |
| + base::Time::Now() + base::TimeDelta::FromMillisecondsD(timediff); |
| + ddljson->GetString("fullpage_interactive_url", &temp); |
| + result.fullpage_interactive_url = base_url_.Resolve(temp); |
| + image->GetString("url", &temp); |
| + result.large_image.url = base_url_.Resolve(temp); |
| + image->GetString("background_color", &result.large_image.background_color); |
| + image->GetInteger("height", &result.large_image.height); |
| + image->GetInteger("width", &result.large_image.width); |
| + |
| + callback_.Run(std::move(result)); |
| +} |
| + |
| +} // namespace doodle |