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

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

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Add injectable JSON parser and tested expiry_time. 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 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_
6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_
7
8 #include <memory>
9 #include <string>
10 #include <utility>
11
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/url_request/url_fetcher_delegate.h"
16 #include "url/gurl.h"
17
18 namespace base {
19 class Clock;
20 class DictionaryValue;
21 class Value;
22 }
23
24 namespace net {
25 class URLRequestContextGetter;
26 }
27
28 namespace doodle {
29
30 enum class DoodleState {
31 AVAILABLE,
32 NO_DOODLE,
33 DOWNLOAD_ERROR,
34 PARSING_ERROR,
35 };
36
37 enum class DoodleType {
38 SIMPLE,
39 RANDOM,
40 VIDEO,
41 INTERACTIVE,
42 INLINE_INTERACTIVE,
43 SLIDESHOW,
44 };
45
46 // Information about a Doodle image. If the image is invalid, the |url| will be
47 // empty and invalid. By default, the background color is #ffffff and dimensions
48 // are 0.
49 struct DoodleImage {
50 DoodleImage();
51 ~DoodleImage();
Marc Treib 2017/01/31 14:46:38 nit: ctors first, then dtor
fhorschig 2017/02/01 11:16:13 Done.
52 explicit DoodleImage(GURL url);
53 DoodleImage(const DoodleImage& config);
54
55 GURL url;
56 int height;
57 int width;
58 std::string background_color;
Marc Treib 2017/01/31 14:46:38 If we actually need this, we should find a proper
fhorschig 2017/02/01 11:16:13 Removed for now. Are you sure about that? I guess
Marc Treib 2017/02/01 11:53:13 Does the desktop NTP actually use the background c
59 bool is_animated_gif;
60 bool is_cta;
Marc Treib 2017/01/31 14:46:39 Hm, what exactly do these two mean? Could they bot
fhorschig 2017/02/01 11:16:13 They can but for our sample doodle, they never are
Marc Treib 2017/02/01 11:53:13 Okay, I'll try to figure it out with the doodle fo
61 };
62
63 // All information about a current doodle that can be fetched from the remote
64 // end. If no information could be fetched, the reason is stored in |state|.
65 // By default, all URLs are empty and therefore invalid.
66 struct DoodleConfig {
67 DoodleConfig() = delete;
68 ~DoodleConfig();
Marc Treib 2017/01/31 14:46:38 Also here: dtor after ctors
fhorschig 2017/02/01 11:16:13 Done.
69 explicit DoodleConfig(DoodleState doodle_state);
70 DoodleConfig(const DoodleConfig& config);
Marc Treib 2017/01/31 14:46:39 Is this required? Won't the default ctor actually
fhorschig 2017/02/01 11:16:13 Added that comment to DoodleImage. It's needed for
Marc Treib 2017/02/01 11:53:13 Ah, I see. Okay then.
71
72 DoodleState state;
Marc Treib 2017/01/31 14:46:38 Hm, so if state isn't AVAILABLE, then all the othe
fhorschig 2017/02/01 11:16:13 It's now part of the Callback.
73 DoodleType doodle_type;
74 std::string alt_text;
75
76 base::Time expiry_date;
77 GURL search_url;
78 GURL target_url;
79 GURL fullpage_interactive_url;
80
81 DoodleImage small_image;
82 DoodleImage medium_image;
83 DoodleImage hires_image;
84 DoodleImage large_image;
85 DoodleImage transparent_large_image;
86 };
87
88 // This class provides information about any recent doodle, based on the
89 // base_url of a user's account.
Marc Treib 2017/01/31 14:46:39 base_url isn't really a thing, it's google_base_ur
fhorschig 2017/02/01 11:16:13 Done. (Deprecated since UrlTracker anyway.)
90 // It works asynchronously and calls a callback when finished fetching the
91 // information from the remote enpoint. Every instance can only handle one fetch
92 // call at a time.
93 class DoodleFetcher : public net::URLFetcherDelegate {
94 public:
95 using FinishedCallback =
96 base::Callback<void(const DoodleConfig& doodle_config)>;
97 using ParseJSONCallback = base::Callback<void(
98 const std::string& unsafe_json,
99 const base::Callback<void(std::unique_ptr<base::Value> json)>& success,
100 const base::Callback<void(const std::string&)>& error)>;
101
102 DoodleFetcher(net::URLRequestContextGetter* download_context, GURL base_url);
Marc Treib 2017/01/31 14:46:39 Eventually, the version without ParseJSONCallback
fhorschig 2017/02/01 11:16:13 Done.
103 DoodleFetcher(net::URLRequestContextGetter* download_context,
104 GURL base_url,
Marc Treib 2017/01/31 14:46:38 Hm, could we get the Google base URL within this c
fhorschig 2017/02/01 11:16:13 Okay, we now depend on the tracker to fetch a URL.
Marc Treib 2017/02/01 11:53:13 Nice! Yes, the dep should be fine. (Let's hope tha
105 ParseJSONCallback json_parsing_callback);
106 ~DoodleFetcher() override;
107
108 // Fetches a doodle asynchronously. The |callback| is always called and
109 // provides a valid DoodleConfig object.
110 void FetchDoodle(const FinishedCallback& callback);
111
112 // Overrides internal clock for testing purposes.
113 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
Marc Treib 2017/01/31 14:46:39 Haven't we decided against this kind of thing in t
fhorschig 2017/02/01 11:16:13 I would like to add a TODO for now as my reasonin
Marc Treib 2017/02/01 11:53:13 SGTM
114 clock_ = std::move(clock);
115 }
116
117 private:
118 // net::URLFetcherDelegate implementation.
119 void OnURLFetchComplete(const net::URLFetcher* source) override;
120
121 void OnJsonParsed(std::unique_ptr<base::Value> json);
122 void OnJsonParseFailed(const std::string& error_message);
123 void ParseDoodle(std::unique_ptr<base::DictionaryValue> config);
124
125 bool ParseImage(const base::DictionaryValue* image_dict,
Marc Treib 2017/01/31 14:46:38 Could these all be static? Or, better yet, local f
fhorschig 2017/02/01 11:16:13 These functions depend on base_url_ and clock_. Ea
Marc Treib 2017/02/01 11:53:13 Ah right, makes sense. Then it's fine as is.
126 const std::string& image_name,
127 DoodleImage* image) const;
128 void ParseBaseInformation(const base::DictionaryValue* ddljson,
129 DoodleConfig* config) const;
130 void ParseRelativeUrl(const base::DictionaryValue* dict_value,
131 const std::string& key,
132 GURL* url) const;
133
134 // Parameter set from constructor.
135 net::URLRequestContextGetter* const download_context_;
136 ParseJSONCallback json_parsing_callback_;
137 GURL base_url_;
138
139 // Allow for an injectable tick clock for testing.
140 std::unique_ptr<base::Clock> clock_;
141
142 FinishedCallback callback_;
143 std::unique_ptr<net::URLFetcher> fetcher_;
144
145 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
146
147 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
148 };
149
150 extern const char kDoodleConfigUrl[];
Marc Treib 2017/01/31 14:46:39 Why is this here? For testing?
fhorschig 2017/02/01 11:16:13 Mainly.
Marc Treib 2017/02/01 11:53:13 Then please add a comment saying that. It'd be eve
fhorschig 2017/02/06 11:48:22 Removed.
151
152 } // namespace doodle
153
154 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698