| 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 CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ |
| 6 #define CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ | 6 #define CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ |
| 7 | 7 |
| 8 #include <memory> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 14 #include "base/macros.h" | 9 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 10 #include "components/ntp_tiles/popular_sites.h" |
| 16 #include "base/strings/string16.h" | |
| 17 #include "net/url_request/url_fetcher_delegate.h" | |
| 18 #include "url/gurl.h" | |
| 19 | |
| 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 | 11 |
| 39 // TODO(sfiera): move to chrome_popular_sites.h | 12 // TODO(sfiera): move to chrome_popular_sites.h |
| 40 class ChromePopularSites { | 13 class ChromePopularSites { |
| 41 public: | 14 public: |
| 42 static base::FilePath GetDirectory(); | 15 static base::FilePath GetDirectory(); |
| 43 | 16 |
| 44 private: | 17 private: |
| 45 DISALLOW_IMPLICIT_CONSTRUCTORS(ChromePopularSites); | 18 DISALLOW_IMPLICIT_CONSTRUCTORS(ChromePopularSites); |
| 46 }; | 19 }; |
| 47 | 20 |
| 48 // Downloads and provides a list of suggested popular sites, for display on | |
| 49 // the NTP when there are not enough personalized suggestions. Caches the | |
| 50 // downloaded file on disk to avoid re-downloading on every startup. | |
| 51 class PopularSites : public net::URLFetcherDelegate { | |
| 52 public: | |
| 53 struct Site { | |
| 54 Site(const base::string16& title, | |
| 55 const GURL& url, | |
| 56 const GURL& favicon_url, | |
| 57 const GURL& large_icon_url, | |
| 58 const GURL& thumbnail_url); | |
| 59 Site(const Site& other); | |
| 60 ~Site(); | |
| 61 | |
| 62 base::string16 title; | |
| 63 GURL url; | |
| 64 GURL favicon_url; | |
| 65 GURL large_icon_url; | |
| 66 GURL thumbnail_url; | |
| 67 }; | |
| 68 | |
| 69 using FinishedCallback = base::Callback<void(bool /* success */)>; | |
| 70 | |
| 71 // When the suggestions have been fetched (from cache or URL) and parsed, | |
| 72 // invokes |callback|, on the same thread as the caller. | |
| 73 // | |
| 74 // Set |force_download| to enforce re-downloading the suggestions file, even | |
| 75 // if it already exists on disk. | |
| 76 // | |
| 77 // TODO(treib): PopularSites should query the variation params itself instead | |
| 78 // of having them passed in. | |
| 79 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | |
| 80 PrefService* prefs, | |
| 81 const TemplateURLService* template_url_service, | |
| 82 variations::VariationsService* variations_service, | |
| 83 net::URLRequestContextGetter* download_context, | |
| 84 const base::FilePath& directory, | |
| 85 const std::string& variation_param_country, | |
| 86 const std::string& variation_param_version, | |
| 87 bool force_download, | |
| 88 const FinishedCallback& callback); | |
| 89 | |
| 90 // This fetches the popular sites from a given url and is only used for | |
| 91 // debugging through the popular-sites-internals page. | |
| 92 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | |
| 93 PrefService* prefs, | |
| 94 net::URLRequestContextGetter* download_context, | |
| 95 const base::FilePath& directory, | |
| 96 const GURL& url, | |
| 97 const FinishedCallback& callback); | |
| 98 | |
| 99 ~PopularSites() override; | |
| 100 | |
| 101 const std::vector<Site>& sites() const { return sites_; } | |
| 102 | |
| 103 // The country/version of the file that was last downloaded. | |
| 104 std::string GetCountry() const; | |
| 105 std::string GetVersion() const; | |
| 106 | |
| 107 const base::FilePath& local_path() const { return local_path_; } | |
| 108 | |
| 109 // Register preferences used by this class. | |
| 110 static void RegisterProfilePrefs( | |
| 111 user_prefs::PrefRegistrySyncable* user_prefs); | |
| 112 | |
| 113 private: | |
| 114 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, | |
| 115 PrefService* prefs, | |
| 116 net::URLRequestContextGetter* download_context, | |
| 117 const base::FilePath& directory, | |
| 118 const std::string& country, | |
| 119 const std::string& version, | |
| 120 const GURL& override_url, | |
| 121 bool force_download, | |
| 122 const FinishedCallback& callback); | |
| 123 | |
| 124 GURL GetPopularSitesURL() const; | |
| 125 | |
| 126 void OnReadFileDone(const GURL& url, | |
| 127 std::unique_ptr<std::string> data, | |
| 128 bool success); | |
| 129 | |
| 130 // Fetch the popular sites at the given URL, overwriting any file that already | |
| 131 // exists. | |
| 132 void FetchPopularSites(const GURL& url); | |
| 133 | |
| 134 // net::URLFetcherDelegate implementation. | |
| 135 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
| 136 | |
| 137 void OnJsonParsed(std::unique_ptr<base::Value> json); | |
| 138 void OnJsonParseFailed(const std::string& error_message); | |
| 139 void OnFileWriteDone(std::unique_ptr<base::Value> json, bool success); | |
| 140 void ParseSiteList(std::unique_ptr<base::Value> json); | |
| 141 void OnDownloadFailed(); | |
| 142 | |
| 143 FinishedCallback callback_; | |
| 144 std::unique_ptr<net::URLFetcher> fetcher_; | |
| 145 bool is_fallback_; | |
| 146 std::vector<Site> sites_; | |
| 147 std::string pending_country_; | |
| 148 std::string pending_version_; | |
| 149 | |
| 150 base::FilePath local_path_; | |
| 151 | |
| 152 PrefService* prefs_; | |
| 153 net::URLRequestContextGetter* download_context_; | |
| 154 | |
| 155 scoped_refptr<base::TaskRunner> blocking_runner_; | |
| 156 | |
| 157 base::WeakPtrFactory<PopularSites> weak_ptr_factory_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(PopularSites); | |
| 160 }; | |
| 161 | |
| 162 #endif // CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ | 21 #endif // CHROME_BROWSER_ANDROID_NTP_POPULAR_SITES_H_ |
| OLD | NEW |