| 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_IMPL_H_ | 5 #ifndef COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_H_ |
| 6 #define COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_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 "base/time/clock.h" | |
| 19 #include "components/doodle/doodle_fetcher.h" | 18 #include "components/doodle/doodle_fetcher.h" |
| 20 #include "components/doodle/doodle_types.h" | 19 #include "components/doodle/doodle_types.h" |
| 21 #include "net/url_request/url_fetcher_delegate.h" | 20 #include "net/url_request/url_fetcher_delegate.h" |
| 22 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 23 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 24 | 23 |
| 25 class GoogleURLTracker; | 24 class GoogleURLTracker; |
| 26 | 25 |
| 27 namespace base { | 26 namespace base { |
| 28 class DictionaryValue; | 27 class DictionaryValue; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 47 GoogleURLTracker* google_url_tracker, | 46 GoogleURLTracker* google_url_tracker, |
| 48 const ParseJSONCallback& json_parsing_callback); | 47 const ParseJSONCallback& json_parsing_callback); |
| 49 ~DoodleFetcherImpl() override; | 48 ~DoodleFetcherImpl() override; |
| 50 | 49 |
| 51 // Fetches a doodle asynchronously. The |callback| is called with a | 50 // Fetches a doodle asynchronously. The |callback| is called with a |
| 52 // DoodleState indicating whether the request succeded in fetching a doodle. | 51 // DoodleState indicating whether the request succeded in fetching a doodle. |
| 53 // If a fetch is already running, the callback will be queued and invoked with | 52 // If a fetch is already running, the callback will be queued and invoked with |
| 54 // result from the next completed request. | 53 // result from the next completed request. |
| 55 void FetchDoodle(FinishedCallback callback) override; | 54 void FetchDoodle(FinishedCallback callback) override; |
| 56 | 55 |
| 57 // Overrides internal clock for testing purposes. | |
| 58 void SetClockForTesting(std::unique_ptr<base::Clock> clock) { | |
| 59 clock_ = std::move(clock); | |
| 60 } | |
| 61 | |
| 62 private: | 56 private: |
| 63 // net::URLFetcherDelegate implementation. | 57 // net::URLFetcherDelegate implementation. |
| 64 void OnURLFetchComplete(const net::URLFetcher* source) override; | 58 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 65 | 59 |
| 66 // ParseJSONCallback Success callback | 60 // ParseJSONCallback Success callback |
| 67 void OnJsonParsed(std::unique_ptr<base::Value> json); | 61 void OnJsonParsed(std::unique_ptr<base::Value> json); |
| 68 // ParseJSONCallback Failure callback | 62 // ParseJSONCallback Failure callback |
| 69 void OnJsonParseFailed(const std::string& error_message); | 63 void OnJsonParseFailed(const std::string& error_message); |
| 70 | 64 |
| 71 base::Optional<DoodleConfig> ParseDoodle( | 65 base::Optional<DoodleConfig> ParseDoodle( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 | 78 |
| 85 // Returns whether a fetch is still in progress. A fetch begins when a | 79 // Returns whether a fetch is still in progress. A fetch begins when a |
| 86 // callback is added and ends when the last callback was called. | 80 // callback is added and ends when the last callback was called. |
| 87 bool IsFetchInProgress() const { return !callbacks_.empty(); } | 81 bool IsFetchInProgress() const { return !callbacks_.empty(); } |
| 88 | 82 |
| 89 // Parameters set from constructor. | 83 // Parameters set from constructor. |
| 90 scoped_refptr<net::URLRequestContextGetter> const download_context_; | 84 scoped_refptr<net::URLRequestContextGetter> const download_context_; |
| 91 ParseJSONCallback json_parsing_callback_; | 85 ParseJSONCallback json_parsing_callback_; |
| 92 GoogleURLTracker* google_url_tracker_; | 86 GoogleURLTracker* google_url_tracker_; |
| 93 | 87 |
| 94 // Allow for an injectable clock for testing. | |
| 95 std::unique_ptr<base::Clock> clock_; | |
| 96 | |
| 97 std::vector<FinishedCallback> callbacks_; | 88 std::vector<FinishedCallback> callbacks_; |
| 98 std::unique_ptr<net::URLFetcher> fetcher_; | 89 std::unique_ptr<net::URLFetcher> fetcher_; |
| 99 | 90 |
| 100 base::WeakPtrFactory<DoodleFetcherImpl> weak_ptr_factory_; | 91 base::WeakPtrFactory<DoodleFetcherImpl> weak_ptr_factory_; |
| 101 | 92 |
| 102 DISALLOW_COPY_AND_ASSIGN(DoodleFetcherImpl); | 93 DISALLOW_COPY_AND_ASSIGN(DoodleFetcherImpl); |
| 103 }; | 94 }; |
| 104 | 95 |
| 105 } // namespace doodle | 96 } // namespace doodle |
| 106 | 97 |
| 107 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_H_ | 98 #endif // COMPONENTS_DOODLE_DOODLE_FETCHER_IMPL_H_ |
| OLD | NEW |