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

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

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Address some more comments 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/OWNERS ('k') | components/doodle/doodle_fetcher.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 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_
6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_
7
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
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);
sdefresne 2017/02/13 15:55:26 Two questions: 1. why is this taking GURL by copy
fhorschig 2017/02/13 17:25:51 Removed the unnecessary constructor.
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 std::string interactive_html;
77
78 base::Time expiry_date;
79 GURL search_url;
80 GURL target_url;
81 GURL fullpage_interactive_url;
82
83 DoodleImage large_image;
84 DoodleImage large_cta_image;
85 DoodleImage transparent_large_image;
86 };
87
88 // This class provides information about any recent doodle.
89 // It works asynchronously and calls a callback when finished fetching the
90 // information from the remote enpoint.
91 class DoodleFetcher : public net::URLFetcherDelegate {
92 public:
93 // Callback that is invoked when the fetching is done.
94 // |doodle_config| will only contain a value if |state| is AVAILABLE.
95 using FinishedCallback = base::OnceCallback<void(
96 DoodleState state,
97 const base::Optional<DoodleConfig>& doodle_config)>;
98 // Callback for JSON parsing to allow injecting platform-dependent code.
99 using ParseJSONCallback = base::Callback<void(
100 const std::string& unsafe_json,
101 const base::Callback<void(std::unique_ptr<base::Value> json)>& success,
102 const base::Callback<void(const std::string&)>& error)>;
103
104 DoodleFetcher(net::URLRequestContextGetter* download_context,
105 GoogleURLTracker* google_url_tracker,
106 const ParseJSONCallback& json_parsing_callback);
107 ~DoodleFetcher() override;
108
109 // Fetches a doodle asynchronously. The |callback| is called with a
110 // DoodleState indicating whether the request succeded in fetching a doodle.
111 // If a fetch is already running, the callback will be queued and invoked with
112 // result from the next completed request.
113 void FetchDoodle(FinishedCallback callback);
114
115 // Overrides internal clock for testing purposes.
116 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
117 clock_ = std::move(clock);
118 }
119
120 private:
121 // net::URLFetcherDelegate implementation.
122 void OnURLFetchComplete(const net::URLFetcher* source) override;
123
124 // ParseJSONCallback Success callback
125 void OnJsonParsed(std::unique_ptr<base::Value> json);
126 // ParseJSONCallback Failure callback
127 void OnJsonParseFailed(const std::string& error_message);
128
129 base::Optional<DoodleConfig> ParseDoodle(
130 const base::DictionaryValue& ddljson) const;
131 bool ParseImage(const base::DictionaryValue& image_dict,
132 const std::string& image_name,
133 DoodleImage* image) const;
134 void ParseBaseInformation(const base::DictionaryValue& ddljson,
135 DoodleConfig* config) const;
136 GURL ParseRelativeUrl(const base::DictionaryValue& dict_value,
137 const std::string& key) const;
138
139 void RespondToAllCallbacks(DoodleState state,
140 const base::Optional<DoodleConfig>& config);
141 GURL GetGoogleBaseUrl() const;
142
143 // Returns whether a fetch is still in progress. A fetch begins when a
144 // callback is added and ends when the last callback was called.
145 bool IsFetchInProgress() const { return !callbacks_.empty(); }
146
147 // Parameters set from constructor.
148 net::URLRequestContextGetter* const download_context_;
149 ParseJSONCallback json_parsing_callback_;
150 GoogleURLTracker* google_url_tracker_;
151
152 // Allow for an injectable tick clock for testing.
153 std::unique_ptr<base::Clock> clock_;
154
155 std::vector<FinishedCallback> callbacks_;
156 std::unique_ptr<net::URLFetcher> fetcher_;
157
158 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
159
160 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
161 };
162
163 } // namespace doodle
164
165 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_
OLDNEW
« no previous file with comments | « components/doodle/OWNERS ('k') | components/doodle/doodle_fetcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698