| 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_IMPL_H_ |
| 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_H_ | 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/optional.h" | 17 #include "base/optional.h" |
| 18 #include "components/doodle/doodle_fetcher.h" |
| 18 #include "components/doodle/doodle_types.h" | 19 #include "components/doodle/doodle_types.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 class GoogleURLTracker; | 24 class GoogleURLTracker; |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 class Clock; | 27 class Clock; |
| 27 class DictionaryValue; | 28 class DictionaryValue; |
| 28 class Value; | 29 class Value; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace doodle { | 32 namespace doodle { |
| 32 | 33 |
| 33 // This class provides information about any recent doodle. | 34 // This class provides information about any recent doodle. |
| 34 // It works asynchronously and calls a callback when finished fetching the | 35 // It works asynchronously and calls a callback when finished fetching the |
| 35 // information from the remote enpoint. | 36 // information from the remote enpoint. |
| 36 class DoodleFetcher : public net::URLFetcherDelegate { | 37 class DoodleFetcherImpl : public DoodleFetcher, public net::URLFetcherDelegate { |
| 37 public: | 38 public: |
| 38 // Callback that is invoked when the fetching is done. | |
| 39 // |doodle_config| will only contain a value if |state| is AVAILABLE. | |
| 40 using FinishedCallback = base::OnceCallback<void( | |
| 41 DoodleState state, | |
| 42 const base::Optional<DoodleConfig>& doodle_config)>; | |
| 43 // Callback for JSON parsing to allow injecting platform-dependent code. | 39 // Callback for JSON parsing to allow injecting platform-dependent code. |
| 44 using ParseJSONCallback = base::Callback<void( | 40 using ParseJSONCallback = base::Callback<void( |
| 45 const std::string& unsafe_json, | 41 const std::string& unsafe_json, |
| 46 const base::Callback<void(std::unique_ptr<base::Value> json)>& success, | 42 const base::Callback<void(std::unique_ptr<base::Value> json)>& success, |
| 47 const base::Callback<void(const std::string&)>& error)>; | 43 const base::Callback<void(const std::string&)>& error)>; |
| 48 | 44 |
| 49 DoodleFetcher(scoped_refptr<net::URLRequestContextGetter> download_context, | 45 DoodleFetcherImpl( |
| 50 GoogleURLTracker* google_url_tracker, | 46 scoped_refptr<net::URLRequestContextGetter> download_context, |
| 51 const ParseJSONCallback& json_parsing_callback); | 47 GoogleURLTracker* google_url_tracker, |
| 52 ~DoodleFetcher() override; | 48 const ParseJSONCallback& json_parsing_callback); |
| 49 ~DoodleFetcherImpl() override; |
| 53 | 50 |
| 54 // Fetches a doodle asynchronously. The |callback| is called with a | 51 // Fetches a doodle asynchronously. The |callback| is called with a |
| 55 // DoodleState indicating whether the request succeded in fetching a doodle. | 52 // 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 | 53 // If a fetch is already running, the callback will be queued and invoked with |
| 57 // result from the next completed request. | 54 // result from the next completed request. |
| 58 void FetchDoodle(FinishedCallback callback); | 55 void FetchDoodle(FinishedCallback callback) override; |
| 59 | 56 |
| 60 // Overrides internal clock for testing purposes. | 57 // Overrides internal clock for testing purposes. |
| 61 void SetClockForTesting(std::unique_ptr<base::Clock> clock) { | 58 void SetClockForTesting(std::unique_ptr<base::Clock> clock) { |
| 62 clock_ = std::move(clock); | 59 clock_ = std::move(clock); |
| 63 } | 60 } |
| 64 | 61 |
| 65 private: | 62 private: |
| 66 // net::URLFetcherDelegate implementation. | 63 // net::URLFetcherDelegate implementation. |
| 67 void OnURLFetchComplete(const net::URLFetcher* source) override; | 64 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 68 | 65 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 93 scoped_refptr<net::URLRequestContextGetter> const download_context_; | 90 scoped_refptr<net::URLRequestContextGetter> const download_context_; |
| 94 ParseJSONCallback json_parsing_callback_; | 91 ParseJSONCallback json_parsing_callback_; |
| 95 GoogleURLTracker* google_url_tracker_; | 92 GoogleURLTracker* google_url_tracker_; |
| 96 | 93 |
| 97 // Allow for an injectable tick clock for testing. | 94 // Allow for an injectable tick clock for testing. |
| 98 std::unique_ptr<base::Clock> clock_; | 95 std::unique_ptr<base::Clock> clock_; |
| 99 | 96 |
| 100 std::vector<FinishedCallback> callbacks_; | 97 std::vector<FinishedCallback> callbacks_; |
| 101 std::unique_ptr<net::URLFetcher> fetcher_; | 98 std::unique_ptr<net::URLFetcher> fetcher_; |
| 102 | 99 |
| 103 base::WeakPtrFactory<DoodleFetcher> weak_ptr_factory_; | 100 base::WeakPtrFactory<DoodleFetcherImpl> weak_ptr_factory_; |
| 104 | 101 |
| 105 DISALLOW_COPY_AND_ASSIGN(DoodleFetcher); | 102 DISALLOW_COPY_AND_ASSIGN(DoodleFetcherImpl); |
| 106 }; | 103 }; |
| 107 | 104 |
| 108 } // namespace doodle | 105 } // namespace doodle |
| 109 | 106 |
| 110 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_H_ | 107 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_H_ |
| OLD | NEW |