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