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