Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Unified Diff: components/doodle/doodle_fetcher.cc

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/doodle/doodle_fetcher.h ('k') | components/doodle/doodle_fetcher_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..d33506fa451ff7124fe6f5098ad591067afc439f
--- /dev/null
+++ b/components/doodle/doodle_fetcher.cc
@@ -0,0 +1,114 @@
+// 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/values.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 {
+const char kDoodleConfigUrl[] =
+ "https://www.google.com/async/ddljson?ogdeb=ct~2000000007";
+
+DoodleConfig CreateFailedEmptyConfig() {
+ DoodleConfig config;
+ config.error_during_fetch = true;
+ return config;
+}
+}
+
+DoodleConfig::DoodleConfig() : error_during_fetch(false) {}
+
+DoodleConfig::DoodleConfig(DoodleImage large_image)
+ : large_image(large_image) {}
+
+DoodleConfig::DoodleConfig(const DoodleConfig& config)
+ : DoodleConfig(config.large_image) {}
+
+DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context,
+ ParseJSONCallback parse_json)
+ : download_context_(download_context),
+ parse_json_(parse_json),
+ 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);
+ 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_);
+
+ LOG(ERROR) << "DEBUG DoodleFetcher: Fetch finished.";
+
+ std::string json_string;
+ if (!(source->GetStatus().is_success() &&
+ source->GetResponseCode() == net::HTTP_OK &&
+ source->GetResponseAsString(&json_string))) {
+ OnDownloadFailed();
+ return;
+ }
+
+ parse_json_.Run(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) {
+ DLOG(WARNING) << "JSON is not valid";
+ OnDownloadFailed();
+ return;
+ }
+
+ ParseDoodle(std::move(dict));
+}
+
+// ParseJSONCallback Failure callback
+void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) {
+ DLOG(WARNING) << "JSON parsing failed: " << error_message;
+ OnDownloadFailed();
+}
+
+void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) {
+ DoodleConfig result;
+ base::DictionaryValue* image;
+ config->GetString("search_url", &result.search_url);
+ config->GetString("fullpage_interactive_url",
+ &result.fullpage_interactive_url);
+ config->GetDictionary("large_image", &image);
+ image->GetString("url", &result.large_image.url);
+ 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(result);
+}
+
+void DoodleFetcher::OnDownloadFailed() {
+ DLOG(ERROR) << "Downloading Doodle failed";
+ callback_.Run(CreateFailedEmptyConfig());
+}
« no previous file with comments | « components/doodle/doodle_fetcher.h ('k') | components/doodle/doodle_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698