| OLD | NEW |
| (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/synchronization/lock.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 namespace base { |
| 21 class Clock; |
| 22 class DictionaryValue; |
| 23 class Value; |
| 24 } |
| 25 |
| 26 namespace net { |
| 27 class URLRequestContextGetter; |
| 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 SIMPLE, |
| 41 RANDOM, |
| 42 VIDEO, |
| 43 INTERACTIVE, |
| 44 INLINE_INTERACTIVE, |
| 45 SLIDESHOW, |
| 46 }; |
| 47 |
| 48 // Information about a Doodle image. If the image is invalid, the |url| will be |
| 49 // empty and invalid. By default, the background color is #ffffff and dimensions |
| 50 // are 0. |
| 51 struct DoodleImage { |
| 52 DoodleImage(); |
| 53 ~DoodleImage(); |
| 54 explicit DoodleImage(GURL url); |
| 55 DoodleImage(const DoodleImage& config); |
| 56 |
| 57 GURL url; |
| 58 int height; |
| 59 int width; |
| 60 std::string background_color; |
| 61 bool is_animated_gif; |
| 62 bool is_cta; |
| 63 }; |
| 64 |
| 65 // All information about a current doodle that can be fetched from the remote |
| 66 // end. If no information could be fetched, the reason is stored in |state|. |
| 67 // By default, all URLs are empty and therefore invalid. |
| 68 struct DoodleConfig { |
| 69 DoodleConfig() = delete; |
| 70 ~DoodleConfig(); |
| 71 explicit DoodleConfig(DoodleState doodle_state); |
| 72 DoodleConfig(const DoodleConfig& config); |
| 73 |
| 74 DoodleState state; |
| 75 DoodleType doodle_type; |
| 76 std::string alt_text; |
| 77 |
| 78 base::Time expiry_date; |
| 79 GURL search_url; |
| 80 GURL target_url; |
| 81 GURL fullpage_interactive_url; |
| 82 |
| 83 DoodleImage small_image; |
| 84 DoodleImage medium_image; |
| 85 DoodleImage hires_image; |
| 86 DoodleImage large_image; |
| 87 DoodleImage transparent_large_image; |
| 88 }; |
| 89 |
| 90 // This class provides information about any recent doodle, based on the |
| 91 // base_url of a user's account. |
| 92 // It works asynchronously and calls a callback when finished fetching the |
| 93 // information from the remote enpoint. Every instance can only handle one fetch |
| 94 // call at a time. Not thread safe. |
| 95 class DoodleFetcher : public net::URLFetcherDelegate { |
| 96 public: |
| 97 using FinishedCallback = |
| 98 base::OnceCallback<void(const DoodleConfig& doodle_config)>; |
| 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, GURL base_url); |
| 105 DoodleFetcher(net::URLRequestContextGetter* download_context, |
| 106 GURL base_url, |
| 107 ParseJSONCallback json_parsing_callback); |
| 108 DoodleFetcher(DoodleFetcher&&); |
| 109 ~DoodleFetcher() override; |
| 110 |
| 111 // Fetches a doodle asynchronously. The |callback| is always called and |
| 112 // provides a valid DoodleConfig object. |
| 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 void OnJsonParsed(std::unique_ptr<base::Value> json); |
| 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 void ParseRelativeUrl(const base::DictionaryValue* dict_value, |
| 134 const std::string& key, |
| 135 GURL* url) const; |
| 136 |
| 137 void RespondToAllCallbacks(const DoodleConfig& response); |
| 138 |
| 139 // Parameter set from constructor. |
| 140 net::URLRequestContextGetter* const download_context_; |
| 141 ParseJSONCallback json_parsing_callback_; |
| 142 GURL base_url_; |
| 143 |
| 144 // Allow for an injectable tick clock for testing. |
| 145 std::unique_ptr<base::Clock> clock_; |
| 146 |
| 147 std::list<FinishedCallback> callbacks_; |
| 148 base::Lock callback_lock_; // Used to synchronize adding/removing callbacks. |
| 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 kDoodleConfigUrl[]; |
| 157 |
| 158 } // namespace doodle |
| 159 |
| 160 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ |
| OLD | NEW |