OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_TILES_POPULAR_SITES_H_ | 5 #ifndef COMPONENTS_NTP_TILES_POPULAR_SITES_H_ |
6 #define COMPONENTS_NTP_TILES_POPULAR_SITES_H_ | 6 #define COMPONENTS_NTP_TILES_POPULAR_SITES_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 base::string16 title; | 55 base::string16 title; |
56 GURL url; | 56 GURL url; |
57 GURL favicon_url; | 57 GURL favicon_url; |
58 GURL large_icon_url; | 58 GURL large_icon_url; |
59 GURL thumbnail_url; | 59 GURL thumbnail_url; |
60 }; | 60 }; |
61 | 61 |
62 using FinishedCallback = base::Callback<void(bool /* success */)>; | 62 using FinishedCallback = base::Callback<void(bool /* success */)>; |
63 | 63 |
64 // When the suggestions have been fetched (from cache or URL) and parsed, | |
65 // invokes |callback|, on the same thread as the caller. | |
66 // | |
67 // Set |force_download| to enforce re-downloading the suggestions file, even | |
68 // if it already exists on disk. | |
69 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | 64 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, |
70 PrefService* prefs, | 65 PrefService* prefs, |
71 const TemplateURLService* template_url_service, | 66 const TemplateURLService* template_url_service, |
72 variations::VariationsService* variations_service, | 67 variations::VariationsService* variations_service, |
73 net::URLRequestContextGetter* download_context, | 68 net::URLRequestContextGetter* download_context, |
74 const base::FilePath& directory, | 69 const base::FilePath& directory); |
75 bool force_download, | 70 |
76 const FinishedCallback& callback); | 71 // Starts the process of retrieving popular sites. When they are available, |
| 72 // invokes |callback| with the result, on the same thread as the caller. Never |
| 73 // invokes |callback| before returning control to the caller, even if the |
| 74 // result is immediately known. |
| 75 // |
| 76 // Set |force_download| to enforce re-downloading the suggestions file, even |
| 77 // if it already exists on disk. |
| 78 // |
| 79 // Must be called at most once on a given PopularSites object. |
| 80 void StartFetch(bool force_download, const FinishedCallback& callback); |
77 | 81 |
78 ~PopularSites() override; | 82 ~PopularSites() override; |
79 | 83 |
80 const std::vector<Site>& sites() const { return sites_; } | 84 const std::vector<Site>& sites() const { return sites_; } |
81 | 85 |
82 // The URL of the file that was last downloaded. | 86 // The URL of the file that was last downloaded. |
83 GURL LastURL() const; | 87 GURL LastURL() const; |
84 | 88 |
85 const base::FilePath& local_path() const { return local_path_; } | 89 const base::FilePath& local_path() const { return local_path_; } |
86 | 90 |
(...skipping 10 matching lines...) Expand all Loading... |
97 | 101 |
98 // net::URLFetcherDelegate implementation. | 102 // net::URLFetcherDelegate implementation. |
99 void OnURLFetchComplete(const net::URLFetcher* source) override; | 103 void OnURLFetchComplete(const net::URLFetcher* source) override; |
100 | 104 |
101 void OnJsonParsed(std::unique_ptr<base::Value> json); | 105 void OnJsonParsed(std::unique_ptr<base::Value> json); |
102 void OnJsonParseFailed(const std::string& error_message); | 106 void OnJsonParseFailed(const std::string& error_message); |
103 void OnFileWriteDone(std::unique_ptr<base::Value> json, bool success); | 107 void OnFileWriteDone(std::unique_ptr<base::Value> json, bool success); |
104 void ParseSiteList(std::unique_ptr<base::Value> json); | 108 void ParseSiteList(std::unique_ptr<base::Value> json); |
105 void OnDownloadFailed(); | 109 void OnDownloadFailed(); |
106 | 110 |
| 111 // Parameters set from constructor. |
| 112 scoped_refptr<base::TaskRunner> const blocking_runner_; |
| 113 PrefService* const prefs_; |
| 114 const TemplateURLService* const template_url_service_; |
| 115 variations::VariationsService* const variations_; |
| 116 net::URLRequestContextGetter* const download_context_; |
| 117 base::FilePath const local_path_; |
| 118 |
| 119 // Set by StartFetch() and called after fetch completes. |
107 FinishedCallback callback_; | 120 FinishedCallback callback_; |
| 121 |
108 std::unique_ptr<net::URLFetcher> fetcher_; | 122 std::unique_ptr<net::URLFetcher> fetcher_; |
109 bool is_fallback_; | 123 bool is_fallback_; |
110 std::vector<Site> sites_; | 124 std::vector<Site> sites_; |
111 GURL pending_url_; | 125 GURL pending_url_; |
112 | 126 |
113 base::FilePath local_path_; | |
114 | |
115 PrefService* prefs_; | |
116 net::URLRequestContextGetter* download_context_; | |
117 | |
118 scoped_refptr<base::TaskRunner> blocking_runner_; | |
119 | |
120 base::WeakPtrFactory<PopularSites> weak_ptr_factory_; | 127 base::WeakPtrFactory<PopularSites> weak_ptr_factory_; |
121 | 128 |
122 DISALLOW_COPY_AND_ASSIGN(PopularSites); | 129 DISALLOW_COPY_AND_ASSIGN(PopularSites); |
123 }; | 130 }; |
124 | 131 |
125 } // namespace ntp_tiles | 132 } // namespace ntp_tiles |
126 | 133 |
127 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ | 134 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ |
OLD | NEW |