| 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 <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" |
| 15 |
| 16 namespace base { |
| 17 class DictionaryValue; |
| 18 class Value; |
| 19 } |
| 20 |
| 21 namespace net { |
| 22 class URLRequestContextGetter; |
| 23 } |
| 24 |
| 25 using ParseJSONCallback = base::Callback<void( |
| 26 const std::string& unsafe_json, |
| 27 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, |
| 28 const base::Callback<void(const std::string&)>& error_callback)>; |
| 29 |
| 30 struct DoodleImage { |
| 31 std::string url; |
| 32 int height; |
| 33 int width; |
| 34 std::string background_color; |
| 35 }; |
| 36 |
| 37 struct DoodleConfig { |
| 38 DoodleConfig(); |
| 39 DoodleConfig(DoodleImage large_image); |
| 40 DoodleConfig(const DoodleConfig& config); |
| 41 bool error_during_fetch; |
| 42 std::string search_url; |
| 43 std::string fullpage_interactive_url; |
| 44 DoodleImage large_image; |
| 45 }; |
| 46 |
| 47 class DoodleFetcher : public net::URLFetcherDelegate { |
| 48 public: |
| 49 using FinishedCallback = base::Callback<void(DoodleConfig doodle_config)>; |
| 50 |
| 51 DoodleFetcher(net::URLRequestContextGetter* download_context, |
| 52 ParseJSONCallback parse_json); |
| 53 |
| 54 ~DoodleFetcher() override; |
| 55 |
| 56 void FetchDoodle(const FinishedCallback& callback); |
| 57 |
| 58 private: |
| 59 // net::URLFetcherDelegate implementation. |
| 60 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 61 |
| 62 void OnJsonParsed(std::unique_ptr<base::Value> json); |
| 63 void OnJsonParseFailed(const std::string& error_message); |
| 64 void ParseDoodle(std::unique_ptr<base::DictionaryValue> config); |
| 65 void OnDownloadFailed(); |
| 66 |
| 67 // Parameters set from constructor. |
| 68 net::URLRequestContextGetter* const download_context_; |
| 69 ParseJSONCallback parse_json_; |
| 70 |
| 71 FinishedCallback callback_; |
| 72 |
| 73 std::unique_ptr<net::URLFetcher> fetcher_; |
| 74 |
| 75 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher); |
| 78 }; |
| 79 |
| 80 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ |
| OLD | NEW |