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