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

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

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Tests work. 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
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/strings/string_number_conversions.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "components/safe_json/safe_json_parser.h"
13 #include "net/base/load_flags.h"
14 #include "net/http/http_status_code.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "url/gurl.h"
17
18 using net::URLFetcher;
19
20 namespace doodle {
21
22 const char kDoodleConfigUrl[] =
23 "https://www.google.com/async/ddljson?ogdeb=ct~2000000007";
24
25 namespace {
26
27 void ParseJson(
28 const std::string& json,
29 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback,
30 const base::Callback<void(const std::string&)>& error_callback) {
31 // The response may start with )]}'. Ignore this.
32 const char kResponsePreamble[] = ")]}'";
33
34 base::StringPiece json_sp(json);
35 if (json_sp.starts_with(kResponsePreamble)) {
36 json_sp.remove_prefix(strlen(kResponsePreamble));
37 }
38
39 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
40 error_callback);
41 }
42
43 } // namespace
44
45 DoodleConfig::DoodleConfig() : state(DoodleState::UNKNOWN) {}
46 DoodleConfig::DoodleConfig(DoodleState doodle_state) : state(doodle_state) {}
47 DoodleConfig::~DoodleConfig() = default;
48 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default;
49
50 DoodleFetcher::DoodleFetcher(net::URLRequestContextGetter* download_context,
51 GURL base_url)
52 : download_context_(download_context),
53 base_url_(base_url),
54 weak_ptr_factory_(this) {}
55
56 DoodleFetcher::~DoodleFetcher() {}
57
58 void DoodleFetcher::FetchDoodle(const FinishedCallback& callback) {
59 DCHECK(!callback_);
60 callback_ = callback;
61 // TODO(fhorschig): Store multiple callbacks.
62 // TODO(fhorschig): Cache responses?
63 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.
64 fetcher_->SetRequestContext(download_context_);
65 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
66 net::LOAD_DO_NOT_SAVE_COOKIES);
67 fetcher_->SetAutomaticallyRetryOnNetworkChanges(1);
68 fetcher_->Start();
69 }
70
71 // net::URLFetcherDelegate implementation.
72 void DoodleFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
73 DCHECK_EQ(fetcher_.get(), source);
74 std::unique_ptr<net::URLFetcher> free_fetcher = std::move(fetcher_);
75
76 std::string json_string;
77 if (!(source->GetStatus().is_success() &&
78 source->GetResponseCode() == net::HTTP_OK &&
79 source->GetResponseAsString(&json_string))) {
80 callback_.Run(DoodleConfig(DoodleState::DOWNLOAD_ERROR));
81 return;
82 }
83
84 ParseJson(json_string, base::Bind(&DoodleFetcher::OnJsonParsed,
85 weak_ptr_factory_.GetWeakPtr()),
86 base::Bind(&DoodleFetcher::OnJsonParseFailed,
87 weak_ptr_factory_.GetWeakPtr()));
88 }
89
90 // ParseJSONCallback Success callback
91 void DoodleFetcher::OnJsonParsed(std::unique_ptr<base::Value> json) {
92 std::unique_ptr<base::DictionaryValue> dict =
93 base::DictionaryValue::From(std::move(json));
94 if (!dict.get()) {
95 LOG(ERROR) << "Doodle JSON is not valid";
96 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR));
97 return;
98 }
99
100 ParseDoodle(std::move(dict));
101 }
102
103 // ParseJSONCallback Failure callback
104 void DoodleFetcher::OnJsonParseFailed(const std::string& error_message) {
105 LOG(ERROR) << "JSON parsing failed: " << error_message;
106 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR));
107 }
108
109 void DoodleFetcher::ParseDoodle(std::unique_ptr<base::DictionaryValue> config) {
110 const base::DictionaryValue* ddljson = nullptr;
111 if (!config->GetDictionary("ddljson", &ddljson)) {
112 LOG(ERROR) << "Doodle JSON reponse did not contain 'ddljson' key.";
113 callback_.Run(DoodleConfig(DoodleState::PARSING_ERROR));
114 return;
115 }
116 const base::DictionaryValue* image = nullptr;
117 if (!ddljson->GetDictionary("large_image", &image)) {
118 callback_.Run(DoodleConfig(DoodleState::NO_DOODLE));
119 return;
120 }
121 std::string temp;
122 DoodleConfig result(DoodleState::AVAILABLE);
123 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
124 result.search_url = base_url_.Resolve(temp);
125 ddljson->GetString("doodle_type", &result.doodle_type);
126 double timediff;
127 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.
128 result.time_to_live_ms =
129 base::Time::Now() + base::TimeDelta::FromMillisecondsD(timediff);
130 ddljson->GetString("fullpage_interactive_url", &temp);
131 result.fullpage_interactive_url = base_url_.Resolve(temp);
132 image->GetString("url", &temp);
133 result.large_image.url = base_url_.Resolve(temp);
134 image->GetString("background_color", &result.large_image.background_color);
135 image->GetInteger("height", &result.large_image.height);
136 image->GetInteger("width", &result.large_image.width);
137
138 callback_.Run(std::move(result));
139 }
140
141 } // namespace doodle
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698