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

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

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Change interface for |FetchDoodle| 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 <list>
9 #include <memory>
10 #include <string>
11 #include <utility>
12
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/optional.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "url/gurl.h"
19
20 class GoogleURLTracker;
21
22 namespace base {
23 class Clock;
24 class DictionaryValue;
25 class Value;
26 }
27
28 namespace net {
29 class URLRequestContextGetter;
30 }
31
32 namespace doodle {
33
34 enum class DoodleState {
35 AVAILABLE,
36 NO_DOODLE,
37 DOWNLOAD_ERROR,
38 PARSING_ERROR,
39 };
40
41 enum class DoodleType {
42 UNKNOWN,
43 SIMPLE,
44 RANDOM,
45 VIDEO,
46 INTERACTIVE,
47 INLINE_INTERACTIVE,
48 SLIDESHOW,
49 };
50
51 // Information about a Doodle image. If the image is invalid, the |url| will be
52 // empty and invalid. By default the dimensions are 0.
53 struct DoodleImage {
54 DoodleImage();
55 explicit DoodleImage(GURL url);
56 ~DoodleImage();
57
58 GURL url;
59 int height;
60 int width;
61 bool is_animated_gif;
62 bool is_cta;
63
64 // Copying and assignment allowed.
65 };
66
67 // All information about a current doodle that can be fetched from the remote
68 // end. By default, all URLs are empty and therefore invalid.
69 struct DoodleConfig {
70 DoodleConfig();
71 DoodleConfig(const DoodleConfig& config); // = default;
72 ~DoodleConfig();
73
74 DoodleType doodle_type;
75 std::string alt_text;
76
77 base::Time expiry_date;
78 GURL search_url;
79 GURL target_url;
80 GURL fullpage_interactive_url;
81
82 DoodleImage large_image;
83 DoodleImage large_cta_image;
84 DoodleImage transparent_large_image;
85 };
86
87 // This class provides information about any recent doodle.
88 // It works asynchronously and calls a callback when finished fetching the
89 // information from the remote enpoint.
90 class DoodleFetcher : public net::URLFetcherDelegate {
91 public:
92 // Callback that is invoked when the fetching is done.
93 // If no information could be fetched, the reason is stored in |state|.
94 using FinishedCallback =
95 base::OnceCallback<void(DoodleState state,
96 base::Optional<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,
103 GoogleURLTracker* google_url_tracker,
104 ParseJSONCallback json_parsing_callback);
Marc Treib 2017/02/01 11:53:14 nit: Pass by const ref
fhorschig 2017/02/03 13:21:05 Done.
105 DoodleFetcher(DoodleFetcher&&);
106 ~DoodleFetcher() override;
107
108 // Fetches a doodle asynchronously. The |callback| is always called and
109 // provides a valid DoodleConfig object.
Marc Treib 2017/02/01 11:53:14 This is now kinda outdated.
fhorschig 2017/02/03 13:21:04 Updated.
110 // If a fetch is already running,
Marc Treib 2017/02/01 11:53:14 ...? :)
fhorschig 2017/02/03 13:21:05 Looks like an urgent lunch break ... Added the res
111 void FetchDoodle(FinishedCallback callback);
112
113 // Overrides internal clock for testing purposes.
114 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
115 clock_ = std::move(clock);
116 }
117
118 private:
119 // net::URLFetcherDelegate implementation.
120 void OnURLFetchComplete(const net::URLFetcher* source) override;
121
122 // ParseJSONCallback Success callback
123 void OnJsonParsed(std::unique_ptr<base::Value> json);
124 // ParseJSONCallback Failure callback
125 void OnJsonParseFailed(const std::string& error_message);
126
127 void ParseDoodle(std::unique_ptr<base::DictionaryValue> config);
128 bool ParseImage(const base::DictionaryValue& image_dict,
129 const std::string& image_name,
130 DoodleImage* image) const;
131 void ParseBaseInformation(const base::DictionaryValue& ddljson,
132 DoodleConfig* config) const;
133 GURL ParseRelativeUrl(const base::DictionaryValue& dict_value,
134 const std::string& key) const;
135
136 void RespondToAllCallbacks(DoodleState state,
137 base::Optional<DoodleConfig> config);
138 GURL GetGoogleBaseUrl() const;
139
140 // Parameter set from constructor.
Marc Treib 2017/02/01 11:53:14 nitty nit: Parameters, plural :)
fhorschig 2017/02/03 13:21:04 Wow, I should really look at my comments more clos
141 net::URLRequestContextGetter* const download_context_;
142 ParseJSONCallback json_parsing_callback_;
143 GoogleURLTracker* google_url_tracker_;
144
145 // Allow for an injectable tick clock for testing.
146 std::unique_ptr<base::Clock> clock_;
147
148 std::list<FinishedCallback> callbacks_;
Marc Treib 2017/02/01 11:53:14 nit: I'd just use a vector. Rule of thumb: Don't u
fhorschig 2017/02/03 13:21:05 Done.
149 std::unique_ptr<net::URLFetcher> fetcher_;
150
151 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
152
153 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
154 };
155
156 extern const char kDoodleConfigPath[];
157
158 } // namespace doodle
159
160 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698