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