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