| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 5 #ifndef COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 6 #define COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/optional.h" | 15 #include "base/optional.h" |
| 16 #include "base/time/tick_clock.h" |
| 17 #include "base/time/time.h" |
| 16 #include "components/ntp_snippets/ntp_snippet.h" | 18 #include "components/ntp_snippets/ntp_snippet.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 19 | 21 |
| 20 namespace base { | 22 namespace base { |
| 21 class Value; | 23 class Value; |
| 22 } | 24 } |
| 23 | 25 |
| 24 namespace ntp_snippets { | 26 namespace ntp_snippets { |
| 25 | 27 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, int count); | 73 void FetchSnippetsFromHosts(const std::set<std::string>& hosts, int count); |
| 72 | 74 |
| 73 // Debug string representing the status/result of the last fetch attempt. | 75 // Debug string representing the status/result of the last fetch attempt. |
| 74 const std::string& last_status() const { return last_status_; } | 76 const std::string& last_status() const { return last_status_; } |
| 75 | 77 |
| 76 // Returns the last json fetched from the server. | 78 // Returns the last json fetched from the server. |
| 77 const std::string& last_json() const { | 79 const std::string& last_json() const { |
| 78 return last_fetch_json_; | 80 return last_fetch_json_; |
| 79 } | 81 } |
| 80 | 82 |
| 83 // Overrides internal clock for testing purposes. |
| 84 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { |
| 85 tick_clock_ = std::move(tick_clock); |
| 86 } |
| 87 |
| 81 private: | 88 private: |
| 82 // URLFetcherDelegate implementation. | 89 // URLFetcherDelegate implementation. |
| 83 void OnURLFetchComplete(const net::URLFetcher* source) override; | 90 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 84 | 91 |
| 85 void OnJsonParsed(std::unique_ptr<base::Value> parsed); | 92 void OnJsonParsed(std::unique_ptr<base::Value> parsed); |
| 86 void OnJsonError(const std::string& error); | 93 void OnJsonError(const std::string& error); |
| 87 void FetchFinished(OptionalSnippets snippets, | 94 void FetchFinished(OptionalSnippets snippets, |
| 88 FetchResult result, | 95 FetchResult result, |
| 89 const std::string& extra_message); | 96 const std::string& extra_message); |
| 90 | 97 |
| 91 // Holds the URL request context. | 98 // Holds the URL request context. |
| 92 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 99 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 93 | 100 |
| 94 const ParseJSONCallback parse_json_callback_; | 101 const ParseJSONCallback parse_json_callback_; |
| 102 base::TimeTicks fetch_start_time_; |
| 95 std::string last_status_; | 103 std::string last_status_; |
| 96 std::string last_fetch_json_; | 104 std::string last_fetch_json_; |
| 97 | 105 |
| 98 // The fetcher for downloading the snippets. | 106 // The fetcher for downloading the snippets. |
| 99 std::unique_ptr<net::URLFetcher> url_fetcher_; | 107 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 100 | 108 |
| 101 // The callback to notify when new snippets get fetched. | 109 // The callback to notify when new snippets get fetched. |
| 102 SnippetsAvailableCallback snippets_available_callback_; | 110 SnippetsAvailableCallback snippets_available_callback_; |
| 103 | 111 |
| 104 // Flag for picking the right (stable/non-stable) API key for Chrome Reader | 112 // Flag for picking the right (stable/non-stable) API key for Chrome Reader |
| 105 bool is_stable_channel_; | 113 bool is_stable_channel_; |
| 106 | 114 |
| 115 // Allow for an injectable tick clock for testing. |
| 116 std::unique_ptr<base::TickClock> tick_clock_; |
| 117 |
| 107 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; | 118 base::WeakPtrFactory<NTPSnippetsFetcher> weak_ptr_factory_; |
| 108 | 119 |
| 109 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); | 120 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcher); |
| 110 }; | 121 }; |
| 111 } // namespace ntp_snippets | 122 } // namespace ntp_snippets |
| 112 | 123 |
| 113 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ | 124 #endif // COMPONENTS_NTP_SNIPPETS_NTP_SNIPPETS_FETCHER_H_ |
| OLD | NEW |