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

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

Issue 2660883002: Introduce a Doodle Fetcher for NTP (Closed)
Patch Set: Remove unused context_getter 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/ref_counted.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/optional.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "url/gurl.h"
21
22 class GoogleURLTracker;
23
24 namespace base {
25 class Clock;
26 class DictionaryValue;
27 class Value;
28 }
29
30 namespace doodle {
31
32 enum class DoodleState {
33 AVAILABLE,
34 NO_DOODLE,
35 DOWNLOAD_ERROR,
36 PARSING_ERROR,
37 };
38
39 enum class DoodleType {
40 UNKNOWN,
41 SIMPLE,
42 RANDOM,
43 VIDEO,
44 INTERACTIVE,
45 INLINE_INTERACTIVE,
46 SLIDESHOW,
47 };
48
49 // Information about a Doodle image. If the image is invalid, the |url| will be
50 // empty and invalid. By default the dimensions are 0.
51 struct DoodleImage {
52 DoodleImage();
53 ~DoodleImage();
54
55 GURL url;
56 int height;
57 int width;
58 bool is_animated_gif;
59 bool is_cta;
60
61 // Copying and assignment allowed.
62 };
63
64 // All information about a current doodle that can be fetched from the remote
65 // end. By default, all URLs are empty and therefore invalid.
66 struct DoodleConfig {
67 DoodleConfig();
68 DoodleConfig(const DoodleConfig& config); // = default;
69 ~DoodleConfig();
70
71 DoodleType doodle_type;
72 std::string alt_text;
73 std::string interactive_html;
74
75 base::Time expiry_date;
76 GURL search_url;
77 GURL target_url;
78 GURL fullpage_interactive_url;
79
80 DoodleImage large_image;
81 DoodleImage large_cta_image;
82 DoodleImage transparent_large_image;
83 };
84
85 // This class provides information about any recent doodle.
86 // It works asynchronously and calls a callback when finished fetching the
87 // information from the remote enpoint.
88 class DoodleFetcher : public net::URLFetcherDelegate {
89 public:
90 // Callback that is invoked when the fetching is done.
91 // |doodle_config| will only contain a value if |state| is AVAILABLE.
92 using FinishedCallback = base::OnceCallback<void(
93 DoodleState state,
94 const base::Optional<DoodleConfig>& doodle_config)>;
95 // Callback for JSON parsing to allow injecting platform-dependent code.
96 using ParseJSONCallback = base::Callback<void(
97 const std::string& unsafe_json,
98 const base::Callback<void(std::unique_ptr<base::Value> json)>& success,
99 const base::Callback<void(const std::string&)>& error)>;
100
101 DoodleFetcher(scoped_refptr<net::URLRequestContextGetter> download_context,
102 GoogleURLTracker* google_url_tracker,
103 const ParseJSONCallback& json_parsing_callback);
104 ~DoodleFetcher() override;
105
106 // Fetches a doodle asynchronously. The |callback| is called with a
107 // DoodleState indicating whether the request succeded in fetching a doodle.
108 // If a fetch is already running, the callback will be queued and invoked with
109 // result from the next completed request.
110 void FetchDoodle(FinishedCallback callback);
111
112 // Overrides internal clock for testing purposes.
113 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
114 clock_ = std::move(clock);
115 }
116
117 private:
118 // net::URLFetcherDelegate implementation.
119 void OnURLFetchComplete(const net::URLFetcher* source) override;
120
121 // ParseJSONCallback Success callback
122 void OnJsonParsed(std::unique_ptr<base::Value> json);
123 // ParseJSONCallback Failure callback
124 void OnJsonParseFailed(const std::string& error_message);
125
126 base::Optional<DoodleConfig> ParseDoodle(
127 const base::DictionaryValue& ddljson) const;
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 const base::Optional<DoodleConfig>& config);
138 GURL GetGoogleBaseUrl() const;
139
140 // Returns whether a fetch is still in progress. A fetch begins when a
141 // callback is added and ends when the last callback was called.
142 bool IsFetchInProgress() const { return !callbacks_.empty(); }
143
144 // Parameters set from constructor.
145 scoped_refptr<net::URLRequestContextGetter> const download_context_;
146 ParseJSONCallback json_parsing_callback_;
147 GoogleURLTracker* google_url_tracker_;
148
149 // Allow for an injectable tick clock for testing.
150 std::unique_ptr<base::Clock> clock_;
151
152 std::vector<FinishedCallback> callbacks_;
153 std::unique_ptr<net::URLFetcher> fetcher_;
154
155 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_;
156
157 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher);
158 };
159
160 } // namespace doodle
161
162 #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