| 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> | |
| 9 #include <string> | |
| 10 #include <vector> | 8 #include <vector> |
| 11 | 9 |
| 12 #include "base/callback.h" | 10 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 14 #include "base/macros.h" | 12 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | |
| 16 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | |
| 18 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 19 | 15 |
| 20 namespace base { | |
| 21 class Value; | |
| 22 } | |
| 23 | |
| 24 namespace net { | |
| 25 class URLRequestContextGetter; | |
| 26 } | |
| 27 | |
| 28 namespace user_prefs { | |
| 29 class PrefRegistrySyncable; | |
| 30 } | |
| 31 | |
| 32 namespace variations { | |
| 33 class VariationsService; | |
| 34 } | |
| 35 | |
| 36 class PrefService; | |
| 37 class TemplateURLService; | |
| 38 | |
| 39 namespace ntp_tiles { | 16 namespace ntp_tiles { |
| 40 | 17 |
| 41 using ParseJSONCallback = base::Callback<void( | |
| 42 const std::string& unsafe_json, | |
| 43 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, | |
| 44 const base::Callback<void(const std::string&)>& error_callback)>; | |
| 45 | |
| 46 // Interface to provide a list of suggested popular sites, for display on the | 18 // Interface to provide a list of suggested popular sites, for display on the |
| 47 // NTP when there are not enough personalized tiles. | 19 // NTP when there are not enough personalized tiles. |
| 48 class PopularSites { | 20 class PopularSites { |
| 49 public: | 21 public: |
| 50 struct Site { | 22 struct Site { |
| 51 Site(const base::string16& title, | 23 Site(const base::string16& title, |
| 52 const GURL& url, | 24 const GURL& url, |
| 53 const GURL& favicon_url, | 25 const GURL& favicon_url, |
| 54 const GURL& large_icon_url, | 26 const GURL& large_icon_url, |
| 55 const GURL& thumbnail_url); | 27 const GURL& thumbnail_url); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 85 virtual const SitesVector& sites() const = 0; | 57 virtual const SitesVector& sites() const = 0; |
| 86 | 58 |
| 87 // Various internals exposed publicly for diagnostic pages only. | 59 // Various internals exposed publicly for diagnostic pages only. |
| 88 virtual GURL GetLastURLFetched() const = 0; | 60 virtual GURL GetLastURLFetched() const = 0; |
| 89 virtual const base::FilePath& local_path() const = 0; | 61 virtual const base::FilePath& local_path() const = 0; |
| 90 virtual GURL GetURLToFetch() = 0; | 62 virtual GURL GetURLToFetch() = 0; |
| 91 virtual std::string GetCountryToFetch() = 0; | 63 virtual std::string GetCountryToFetch() = 0; |
| 92 virtual std::string GetVersionToFetch() = 0; | 64 virtual std::string GetVersionToFetch() = 0; |
| 93 }; | 65 }; |
| 94 | 66 |
| 95 // Actual (non-test) implementation of the PopularSites interface. Caches the | |
| 96 // downloaded file on disk to avoid re-downloading on every startup. | |
| 97 class PopularSitesImpl : public PopularSites, public net::URLFetcherDelegate { | |
| 98 public: | |
| 99 PopularSitesImpl( | |
| 100 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | |
| 101 PrefService* prefs, | |
| 102 const TemplateURLService* template_url_service, | |
| 103 variations::VariationsService* variations_service, | |
| 104 net::URLRequestContextGetter* download_context, | |
| 105 const base::FilePath& directory, | |
| 106 ParseJSONCallback parse_json); | |
| 107 | |
| 108 ~PopularSitesImpl() override; | |
| 109 | |
| 110 // PopularSites implementation. | |
| 111 void StartFetch(bool force_download, | |
| 112 const FinishedCallback& callback) override; | |
| 113 const SitesVector& sites() const override; | |
| 114 GURL GetLastURLFetched() const override; | |
| 115 const base::FilePath& local_path() const override; | |
| 116 GURL GetURLToFetch() override; | |
| 117 std::string GetCountryToFetch() override; | |
| 118 std::string GetVersionToFetch() override; | |
| 119 | |
| 120 // Register preferences used by this class. | |
| 121 static void RegisterProfilePrefs( | |
| 122 user_prefs::PrefRegistrySyncable* user_prefs); | |
| 123 | |
| 124 private: | |
| 125 void OnReadFileDone(std::unique_ptr<std::string> data, bool success); | |
| 126 | |
| 127 // Fetch the popular sites at the given URL, overwriting any file that already | |
| 128 // exists. | |
| 129 void FetchPopularSites(); | |
| 130 | |
| 131 // net::URLFetcherDelegate implementation. | |
| 132 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 133 | |
| 134 void OnJsonParsed(std::unique_ptr<base::Value> json); | |
| 135 void OnJsonParseFailed(const std::string& error_message); | |
| 136 void OnFileWriteDone(std::unique_ptr<base::Value> json, bool success); | |
| 137 void ParseSiteList(std::unique_ptr<base::Value> json); | |
| 138 void OnDownloadFailed(); | |
| 139 | |
| 140 // Parameters set from constructor. | |
| 141 scoped_refptr<base::TaskRunner> const blocking_runner_; | |
| 142 PrefService* const prefs_; | |
| 143 const TemplateURLService* const template_url_service_; | |
| 144 variations::VariationsService* const variations_; | |
| 145 net::URLRequestContextGetter* const download_context_; | |
| 146 base::FilePath const local_path_; | |
| 147 ParseJSONCallback parse_json_; | |
| 148 | |
| 149 // Set by StartFetch() and called after fetch completes. | |
| 150 FinishedCallback callback_; | |
| 151 | |
| 152 std::unique_ptr<net::URLFetcher> fetcher_; | |
| 153 bool is_fallback_; | |
| 154 SitesVector sites_; | |
| 155 GURL pending_url_; | |
| 156 | |
| 157 base::WeakPtrFactory<PopularSitesImpl> weak_ptr_factory_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(PopularSitesImpl); | |
| 160 }; | |
| 161 | |
| 162 } // namespace ntp_tiles | 67 } // namespace ntp_tiles |
| 163 | 68 |
| 164 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ | 69 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ |
| OLD | NEW |