Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: components/ntp_tiles/popular_sites.h

Issue 2570783003: [Popular Sites] Split PopularSites interface and PopularSitesImpl (Closed)
Patch Set: Split PopularSitesImpl. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 COMPONENTS_NTP_TILES_POPULAR_SITES_H_ 5 #ifndef COMPONENTS_NTP_TILES_POPULAR_SITES_H_
6 #define COMPONENTS_NTP_TILES_POPULAR_SITES_H_ 6 #define COMPONENTS_NTP_TILES_POPULAR_SITES_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
(...skipping 25 matching lines...) Expand all
36 class PrefService; 36 class PrefService;
37 class TemplateURLService; 37 class TemplateURLService;
38 38
39 namespace ntp_tiles { 39 namespace ntp_tiles {
40 40
41 using ParseJSONCallback = base::Callback<void( 41 using ParseJSONCallback = base::Callback<void(
42 const std::string& unsafe_json, 42 const std::string& unsafe_json,
43 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback, 43 const base::Callback<void(std::unique_ptr<base::Value>)>& success_callback,
44 const base::Callback<void(const std::string&)>& error_callback)>; 44 const base::Callback<void(const std::string&)>& error_callback)>;
45 45
46 // Downloads and provides a list of suggested popular sites, for display on 46 // Interface to provide a list of suggested popular sites, for display on the
47 // the NTP when there are not enough personalized tiles. Caches the downloaded 47 // NTP when there are not enough personalized tiles.
48 // file on disk to avoid re-downloading on every startup. 48 class PopularSites {
49 class PopularSites : public net::URLFetcherDelegate {
50 public: 49 public:
51 struct Site { 50 struct Site {
52 Site(const base::string16& title, 51 Site(const base::string16& title,
53 const GURL& url, 52 const GURL& url,
54 const GURL& favicon_url, 53 const GURL& favicon_url,
55 const GURL& large_icon_url, 54 const GURL& large_icon_url,
56 const GURL& thumbnail_url); 55 const GURL& thumbnail_url);
57 Site(const Site& other); 56 Site(const Site& other);
58 ~Site(); 57 ~Site();
59 58
60 base::string16 title; 59 base::string16 title;
61 GURL url; 60 GURL url;
62 GURL favicon_url; 61 GURL favicon_url;
63 GURL large_icon_url; 62 GURL large_icon_url;
64 GURL thumbnail_url; 63 GURL thumbnail_url;
65 }; 64 };
66 65
66 using SitesVector = std::vector<Site>;
67 using FinishedCallback = base::Callback<void(bool /* success */)>; 67 using FinishedCallback = base::Callback<void(bool /* success */)>;
68 68
69 PopularSites(const scoped_refptr<base::SequencedWorkerPool>& blocking_pool, 69 virtual ~PopularSites() = default;
sfiera 2016/12/13 10:53:57 I think some bots are going to complain about this
mastiz 2016/12/13 12:14:56 Acknowledged, no complaints so far.
70 PrefService* prefs,
71 const TemplateURLService* template_url_service,
72 variations::VariationsService* variations_service,
73 net::URLRequestContextGetter* download_context,
74 const base::FilePath& directory,
75 ParseJSONCallback parse_json);
76 70
77 // Starts the process of retrieving popular sites. When they are available, 71 // Starts the process of retrieving popular sites. When they are available,
78 // invokes |callback| with the result, on the same thread as the caller. Never 72 // invokes |callback| with the result, on the same thread as the caller. Never
79 // invokes |callback| before returning control to the caller, even if the 73 // invokes |callback| before returning control to the caller, even if the
80 // result is immediately known. 74 // result is immediately known.
81 // 75 //
82 // Set |force_download| to enforce re-downloading the popular sites file, even 76 // Set |force_download| to enforce re-downloading the popular sites file, even
83 // if it already exists on disk. 77 // if it already exists on disk.
84 // 78 //
85 // Must be called at most once on a given PopularSites object. 79 // Must be called at most once on a given PopularSites object.
86 void StartFetch(bool force_download, const FinishedCallback& callback); 80 // TODO(mastiz): Remove this restriction?
81 virtual void StartFetch(bool force_download,
82 const FinishedCallback& callback) = 0;
87 83
88 ~PopularSites() override; 84 // Returns the list of available sites.
85 virtual const SitesVector& sites() const = 0;
86 };
89 87
90 const std::vector<Site>& sites() const { return sites_; } 88 // Actual (non-test) implementation of the PopularSites interface. Caches the
89 // downloaded file on disk to avoid re-downloading on every startup.
90 class PopularSitesImpl : public PopularSites, public net::URLFetcherDelegate {
91 public:
92 PopularSitesImpl(
93 const scoped_refptr<base::SequencedWorkerPool>& blocking_pool,
94 PrefService* prefs,
95 const TemplateURLService* template_url_service,
96 variations::VariationsService* variations_service,
97 net::URLRequestContextGetter* download_context,
98 const base::FilePath& directory,
99 ParseJSONCallback parse_json);
100
101 ~PopularSitesImpl() override;
102
103 // PopularSites implementation.
104 void StartFetch(bool force_download,
105 const FinishedCallback& callback) override;
106 const SitesVector& sites() const override;
91 107
92 // The URL of the file that was last downloaded. 108 // The URL of the file that was last downloaded.
93 GURL LastURL() const; 109 GURL LastURL() const;
94 110
95 const base::FilePath& local_path() const { return local_path_; } 111 const base::FilePath& local_path() const { return local_path_; }
96 112
97 // Register preferences used by this class. 113 // Register preferences used by this class.
98 static void RegisterProfilePrefs( 114 static void RegisterProfilePrefs(
99 user_prefs::PrefRegistrySyncable* user_prefs); 115 user_prefs::PrefRegistrySyncable* user_prefs);
100 116
(...skipping 20 matching lines...) Expand all
121 variations::VariationsService* const variations_; 137 variations::VariationsService* const variations_;
122 net::URLRequestContextGetter* const download_context_; 138 net::URLRequestContextGetter* const download_context_;
123 base::FilePath const local_path_; 139 base::FilePath const local_path_;
124 ParseJSONCallback parse_json_; 140 ParseJSONCallback parse_json_;
125 141
126 // Set by StartFetch() and called after fetch completes. 142 // Set by StartFetch() and called after fetch completes.
127 FinishedCallback callback_; 143 FinishedCallback callback_;
128 144
129 std::unique_ptr<net::URLFetcher> fetcher_; 145 std::unique_ptr<net::URLFetcher> fetcher_;
130 bool is_fallback_; 146 bool is_fallback_;
131 std::vector<Site> sites_; 147 SitesVector sites_;
132 GURL pending_url_; 148 GURL pending_url_;
133 149
134 base::WeakPtrFactory<PopularSites> weak_ptr_factory_; 150 base::WeakPtrFactory<PopularSitesImpl> weak_ptr_factory_;
135 151
136 DISALLOW_COPY_AND_ASSIGN(PopularSites); 152 DISALLOW_COPY_AND_ASSIGN(PopularSitesImpl);
137 }; 153 };
138 154
139 } // namespace ntp_tiles 155 } // namespace ntp_tiles
140 156
141 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_ 157 #endif // COMPONENTS_NTP_TILES_POPULAR_SITES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698