| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_ | 5 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_H_ |
| 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_ | 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include "base/callback_forward.h" |
| 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" | 9 #include "base/optional.h" |
| 18 #include "components/doodle/doodle_types.h" | 10 #include "components/doodle/doodle_types.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 class GoogleURLTracker; | |
| 24 | |
| 25 namespace base { | |
| 26 class Clock; | |
| 27 class DictionaryValue; | |
| 28 class Value; | |
| 29 } | |
| 30 | 11 |
| 31 namespace doodle { | 12 namespace doodle { |
| 32 | 13 |
| 33 // This class provides information about any recent doodle. | 14 // Interface for fetching the current doodle from the network. |
| 34 // It works asynchronously and calls a callback when finished fetching the | 15 // It asynchronously calls a callback when fetching the doodle information from |
| 35 // information from the remote enpoint. | 16 // the remote enpoint finishes. |
| 36 class DoodleFetcher : public net::URLFetcherDelegate { | 17 // DoodleFetcherImpl is the default implementation; this interface exists to |
| 18 // make it easy to use fakes or mocks in tests. |
| 19 class DoodleFetcher { |
| 37 public: | 20 public: |
| 38 // Callback that is invoked when the fetching is done. | 21 // Callback that is invoked when the fetching is done. |
| 39 // |doodle_config| will only contain a value if |state| is AVAILABLE. | 22 // |doodle_config| will only contain a value if |state| is AVAILABLE. |
| 40 using FinishedCallback = base::OnceCallback<void( | 23 using FinishedCallback = base::OnceCallback<void( |
| 41 DoodleState state, | 24 DoodleState state, |
| 42 const base::Optional<DoodleConfig>& doodle_config)>; | 25 const base::Optional<DoodleConfig>& doodle_config)>; |
| 43 // Callback for JSON parsing to allow injecting platform-dependent code. | |
| 44 using ParseJSONCallback = base::Callback<void( | |
| 45 const std::string& unsafe_json, | |
| 46 const base::Callback<void(std::unique_ptr<base::Value> json)>& success, | |
| 47 const base::Callback<void(const std::string&)>& error)>; | |
| 48 | 26 |
| 49 DoodleFetcher(scoped_refptr<net::URLRequestContextGetter> download_context, | 27 virtual ~DoodleFetcher() = default; |
| 50 GoogleURLTracker* google_url_tracker, | |
| 51 const ParseJSONCallback& json_parsing_callback); | |
| 52 ~DoodleFetcher() override; | |
| 53 | 28 |
| 54 // Fetches a doodle asynchronously. The |callback| is called with a | 29 // Fetches a doodle asynchronously. The |callback| is called with a |
| 55 // DoodleState indicating whether the request succeded in fetching a doodle. | 30 // DoodleState indicating whether the request succeded in fetching a doodle. |
| 56 // If a fetch is already running, the callback will be queued and invoked with | 31 // If a fetch is already running, the callback will be queued and invoked with |
| 57 // result from the next completed request. | 32 // result from the next completed request. |
| 58 void FetchDoodle(FinishedCallback callback); | 33 virtual void FetchDoodle(FinishedCallback callback) = 0; |
| 59 | |
| 60 // Overrides internal clock for testing purposes. | |
| 61 void SetClockForTesting(std::unique_ptr<base::Clock> clock) { | |
| 62 clock_ = std::move(clock); | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 // net::URLFetcherDelegate implementation. | |
| 67 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 68 | |
| 69 // ParseJSONCallback Success callback | |
| 70 void OnJsonParsed(std::unique_ptr<base::Value> json); | |
| 71 // ParseJSONCallback Failure callback | |
| 72 void OnJsonParseFailed(const std::string& error_message); | |
| 73 | |
| 74 base::Optional<DoodleConfig> ParseDoodle( | |
| 75 const base::DictionaryValue& ddljson) const; | |
| 76 bool ParseImage(const base::DictionaryValue& image_dict, | |
| 77 const std::string& image_name, | |
| 78 DoodleImage* image) const; | |
| 79 void ParseBaseInformation(const base::DictionaryValue& ddljson, | |
| 80 DoodleConfig* config) const; | |
| 81 GURL ParseRelativeUrl(const base::DictionaryValue& dict_value, | |
| 82 const std::string& key) const; | |
| 83 | |
| 84 void RespondToAllCallbacks(DoodleState state, | |
| 85 const base::Optional<DoodleConfig>& config); | |
| 86 GURL GetGoogleBaseUrl() const; | |
| 87 | |
| 88 // Returns whether a fetch is still in progress. A fetch begins when a | |
| 89 // callback is added and ends when the last callback was called. | |
| 90 bool IsFetchInProgress() const { return !callbacks_.empty(); } | |
| 91 | |
| 92 // Parameters set from constructor. | |
| 93 scoped_refptr<net::URLRequestContextGetter> const download_context_; | |
| 94 ParseJSONCallback json_parsing_callback_; | |
| 95 GoogleURLTracker* google_url_tracker_; | |
| 96 | |
| 97 // Allow for an injectable tick clock for testing. | |
| 98 std::unique_ptr<base::Clock> clock_; | |
| 99 | |
| 100 std::vector<FinishedCallback> callbacks_; | |
| 101 std::unique_ptr<net::URLFetcher> fetcher_; | |
| 102 | |
| 103 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher); | |
| 106 }; | 34 }; |
| 107 | 35 |
| 108 } // namespace doodle | 36 } // namespace doodle |
| 109 | 37 |
| 110 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ | 38 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ |
| OLD | NEW |