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

Side by Side Diff: components/doodle/doodle_fetcher.cc

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Created 3 years, 10 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/doodle/doodle_fetcher.h"
6
7 #include <utility>
8
9 #include "base/values.h"
10 #include "net/base/load_flags.h"
11 #include "net/http/http_status_code.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "url/gurl.h"
14
15 using net::URLFetcher;
16
17 namespace {
18 const char kDoodleConfigUrl[] =
19 "https://www.google.com/async/ddljson?ogdeb=ct~2000000007";
20
21 DoodleConfig CreateFailedEmptyConfig() {
22 DoodleConfig config;
23 config.error_during_fetch = true;
24 return config;
25 }
26 }
27
28 DoodleConfig::DoodleConfig() : error_during_fetch(false) {}
29
30 DoodleConfig::DoodleConfig(DoodleImage large_image)
31 : large_image(large_image) {}
32
33 DoodleConfig::DoodleConfig(const DoodleConfig& config)
34 : DoodleConfig(config.large_image) {}
35
36 DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context,
37 ParseJSONCallback parse_json)
38 : download_context_(download_context),
39 parse_json_(parse_json),
40 weak_ptr_factory_(this) {}
41
42 DoodleFetcher::~DoodleFetcher() {}
43
44 void DoodleFetcher::FetchDoodle(const FinishedCallback& callback) {
45 DCHECK(!callback_);
46 callback_ = callback;
47 // TODO(fhorschig): Store multiple callbacks.
48 // TODO(fhorschig): Cache responses?
49 fetcher_ = URLFetcher::Create(GURL(kDoodleConfigUrl), URLFetcher::GET, this);
50 fetcher_->SetRequestContext(download_context_);
51 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
52 net::LOAD_DO_NOT_SAVE_COOKIES);
53 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1);
54 fetcher_->Start();
55 }
56
57 // net::URLFetcherDelegate implementation.
58 void DoodleFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
59 DCHECK_EQ(fetcher_.get(), source);
60 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_);
61
62 LOG(ERROR) << "DEBUG DoodleFetcher: Fetch finished.";
63
64 std::string json_string;
65 if (!(source->GetStatus().is_success() &&
66 source->GetResponseCode() == net::HTTP_OK &&
67 source->GetResponseAsString(&json_string))) {
68 OnDownloadFailed();
69 return;
70 }
71
72 parse_json_.Run(json_string, base::Bind(&DoodleFetcher::OnJsonParsed,
73 weak_ptr_factory_.GetWeakPtr()),
74 base::Bind(&DoodleFetcher::OnJsonParseFailed,
75 weak_ptr_factory_.GetWeakPtr()));
76 }
77
78 // ParseJSONCallback Success callback
79 void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) {
80 std::unique_ptr<base::DictionaryValue> dict =
81 base::DictionaryValue::From(std::move(json));
82 if (!dict) {
83 DLOG(WARNING) << "JSON is not valid";
84 OnDownloadFailed();
85 return;
86 }
87
88 ParseDoodle(std::move(dict));
89 }
90
91 // ParseJSONCallback Failure callback
92 void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) {
93 DLOG(WARNING) << "JSON parsing failed: " << error_message;
94 OnDownloadFailed();
95 }
96
97 void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) {
98 DoodleConfig result;
99 base::DictionaryValue* image;
100 config->GetString("search_url", &result.search_url);
101 config->GetString("fullpage_interactive_url",
102 &result.fullpage_interactive_url);
103 config->GetDictionary("large_image", &image);
104 image->GetString("url", &result.large_image.url);
105 image->GetString("background_color", &result.large_image.background_color);
106 image->GetInteger("height", &result.large_image.height);
107 image->GetInteger("width", &result.large_image.width);
108 callback_.Run(result);
109 }
110
111 void DoodleFetcher::OnDownloadFailed() {
112 DLOG(ERROR) << "Downloading Doodle failed";
113 callback_.Run(CreateFailedEmptyConfig());
114 }
OLDNEW
« 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