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