| 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 25 matching lines...) Expand all Loading... |
| 36 class PrefService; | 36 class PrefService; |
| 37 class TemplateURLService; | 37 class TemplateURLService; |
| 38 | 38 |
| 39 namespace ntp_tiles { | 39 namespace ntp_tiles { |
| 40 | 40 |
| 41 using ParseJSONCallback = base::Callback<void( | 41 using ParseJSONCallback = base::Callback<void( |
| 42 const std::string& unsafe_json, | 42 const std::string& unsafe_json, |
| 43 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, | 43 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, |
| 44 const base::Callback<void(const std::string&)>& error_callback)>; | 44 const base::Callback<void(const std::string&)>& error_callback)>; |
| 45 | 45 |
| 46 // Downloads and provides a list of suggested popular sites, for display on | 46 // Interface to provide a list of suggested popular sites, for display on the |
| 47 // the NTP when there are not enough personalized tiles. Caches the downloaded | 47 // NTP when there are not enough personalized tiles. |
| 48 // file on disk to avoid re-downloading on every startup. | 48 class PopularSites { |
| 49 class PopularSites : public net::URLFetcherDelegate { | |
| 50 public: | 49 public: |
| 51 struct Site { | 50 struct Site { |
| 52 Site(const base::string16& title, | 51 Site(const base::string16& title, |
| 53 const GURL& url, | 52 const GURL& url, |
| 54 const GURL& favicon_url, | 53 const GURL& favicon_url, |
| 55 const GURL& large_icon_url, | 54 const GURL& large_icon_url, |
| 56 const GURL& thumbnail_url); | 55 const GURL& thumbnail_url); |
| 57 Site(const Site& other); | 56 Site(const Site& other); |
| 58 ~Site(); | 57 ~Site(); |
| 59 | 58 |
| 60 base::string16 title; | 59 base::string16 title; |
| 61 GURL url; | 60 GURL url; |
| 62 GURL favicon_url; | 61 GURL favicon_url; |
| 63 GURL large_icon_url; | 62 GURL large_icon_url; |
| 64 GURL thumbnail_url; | 63 GURL thumbnail_url; |
| 65 }; | 64 }; |
| 66 | 65 |
| 66 using SitesVector = std::vector<Site>; |
| 67 using FinishedCallback = base::Callback<void(bool /* success */)>; | 67 using FinishedCallback = base::Callback<void(bool /* success */)>; |
| 68 | 68 |
| 69 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | 69 virtual ~PopularSites() = default; |
| 70 PrefService* prefs, | |
| 71 const TemplateURLService* template_url_service, | |
| 72 variations::VariationsService* variations_service, | |
| 73 net::URLRequestContextGetter* download_context, | |
| 74 const base::FilePath& directory, | |
| 75 ParseJSONCallback parse_json); | |
| 76 | 70 |
| 77 // Starts the process of retrieving popular sites. When they are available, | 71 // Starts the process of retrieving popular sites. When they are available, |
| 78 // invokes |callback| with the result, on the same thread as the caller. Never | 72 // invokes |callback| with the result, on the same thread as the caller. Never |
| 79 // invokes |callback| before returning control to the caller, even if the | 73 // invokes |callback| before returning control to the caller, even if the |
| 80 // result is immediately known. | 74 // result is immediately known. |
| 81 // | 75 // |
| 82 // Set |force_download| to enforce re-downloading the popular sites file, even | 76 // Set |force_download| to enforce re-downloading the popular sites file, even |
| 83 // if it already exists on disk. | 77 // if it already exists on disk. |
| 84 // | 78 // |
| 85 // Must be called at most once on a given PopularSites object. | 79 // Must be called at most once on a given PopularSites object. |
| 86 void StartFetch(bool force_download, const FinishedCallback& callback); | 80 // TODO(mastiz): Remove this restriction? |
| 81 virtual void StartFetch(bool force_download, |
| 82 const FinishedCallback& callback) = 0; |
| 87 | 83 |
| 88 ~PopularSites() override; | 84 // Returns the list of available sites. |
| 85 virtual const SitesVector& sites() const = 0; |
| 89 | 86 |
| 90 const std::vector<Site>& sites() const { return sites_; } | 87 // Various internals exposed publicly for diagnostic pages only. |
| 88 virtual GURL GetLastURLFetched() const = 0; |
| 89 virtual const base::FilePath& local_path() const = 0; |
| 90 virtual GURL GetURLToFetch() = 0; |
| 91 virtual std::string GetCountryToFetch() = 0; |
| 92 virtual std::string GetVersionToFetch() = 0; |
| 93 }; |
| 91 | 94 |
| 92 // The URL of the file that was last downloaded. | 95 // Actual (non-test) implementation of the PopularSites interface. Caches the |
| 93 GURL LastURL() const; | 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); |
| 94 | 107 |
| 95 const base::FilePath& local_path() const { return local_path_; } | 108 ~PopularSitesImpl() override; |
| 96 | 109 |
| 97 // Public for diagnostic pages only. | 110 // PopularSites implementation. |
| 98 GURL GetURLToUse(); | 111 void StartFetch(bool force_download, |
| 99 std::string GetCountryToUse(); | 112 const FinishedCallback& callback) override; |
| 100 std::string GetVersionToUse(); | 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; |
| 101 | 119 |
| 102 // Register preferences used by this class. | 120 // Register preferences used by this class. |
| 103 static void RegisterProfilePrefs( | 121 static void RegisterProfilePrefs( |
| 104 user_prefs::PrefRegistrySyncable* user_prefs); | 122 user_prefs::PrefRegistrySyncable* user_prefs); |
| 105 | 123 |
| 106 private: | 124 private: |
| 107 void OnReadFileDone(std::unique_ptr<std::string> data, bool success); | 125 void OnReadFileDone(std::unique_ptr<std::string> data, bool success); |
| 108 | 126 |
| 109 // Fetch the popular sites at the given URL, overwriting any file that already | 127 // Fetch the popular sites at the given URL, overwriting any file that already |
| 110 // exists. | 128 // exists. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 126 variations::VariationsService* const variations_; | 144 variations::VariationsService* const variations_; |
| 127 net::URLRequestContextGetter* const download_context_; | 145 net::URLRequestContextGetter* const download_context_; |
| 128 base::FilePath const local_path_; | 146 base::FilePath const local_path_; |
| 129 ParseJSONCallback parse_json_; | 147 ParseJSONCallback parse_json_; |
| 130 | 148 |
| 131 // Set by StartFetch() and called after fetch completes. | 149 // Set by StartFetch() and called after fetch completes. |
| 132 FinishedCallback callback_; | 150 FinishedCallback callback_; |
| 133 | 151 |
| 134 std::unique_ptr<net::URLFetcher> fetcher_; | 152 std::unique_ptr<net::URLFetcher> fetcher_; |
| 135 bool is_fallback_; | 153 bool is_fallback_; |
| 136 std::vector<Site> sites_; | 154 SitesVector sites_; |
| 137 GURL pending_url_; | 155 GURL pending_url_; |
| 138 | 156 |
| 139 base::WeakPtrFactory<PopularSites> weak_ptr_factory_; | 157 base::WeakPtrFactory<PopularSitesImpl> weak_ptr_factory_; |
| 140 | 158 |
| 141 DISALLOW_COPY_AND_ASSIGN(PopularSites); | 159 DISALLOW_COPY_AND_ASSIGN(PopularSitesImpl); |
| 142 }; | 160 }; |
| 143 | 161 |
| 144 } // namespace ntp_tiles | 162 } // namespace ntp_tiles |
| 145 | 163 |
| 146 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ | 164 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ |
| OLD | NEW |