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

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

Issue 2619993002: ntp_tiles: Migrate to multi-observer model
Patch Set: Fixed build. Created 3 years, 11 months 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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_MOST_VISITED_SITES_H_ 5 #ifndef COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
6 #define COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_ 6 #define COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
17 #include "base/scoped_observer.h" 18 #include "base/scoped_observer.h"
18 #include "base/strings/string16.h" 19 #include "base/strings/string16.h"
19 #include "components/history/core/browser/history_types.h" 20 #include "components/history/core/browser/history_types.h"
20 #include "components/history/core/browser/top_sites_observer.h" 21 #include "components/history/core/browser/top_sites_observer.h"
21 #include "components/ntp_tiles/ntp_tile.h" 22 #include "components/ntp_tiles/ntp_tile.h"
22 #include "components/ntp_tiles/popular_sites.h" 23 #include "components/ntp_tiles/popular_sites.h"
23 #include "components/suggestions/proto/suggestions.pb.h" 24 #include "components/suggestions/proto/suggestions.pb.h"
24 #include "components/suggestions/suggestions_service.h" 25 #include "components/suggestions/suggestions_service.h"
25 #include "url/gurl.h" 26 #include "url/gurl.h"
26 27
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 virtual std::vector<Whitelist> whitelists() = 0; 71 virtual std::vector<Whitelist> whitelists() = 0;
71 72
72 // If true, be conservative about suggesting sites from outside sources. 73 // If true, be conservative about suggesting sites from outside sources.
73 virtual bool IsChildProfile() = 0; 74 virtual bool IsChildProfile() = 0;
74 }; 75 };
75 76
76 // Tracks the list of most visited sites and their thumbnails. 77 // Tracks the list of most visited sites and their thumbnails.
77 class MostVisitedSites : public history::TopSitesObserver, 78 class MostVisitedSites : public history::TopSitesObserver,
78 public MostVisitedSitesSupervisor::Observer { 79 public MostVisitedSitesSupervisor::Observer {
79 public: 80 public:
80 // The observer to be notified when the list of most visited sites changes. 81 // Observer to be notified when the list of most visited sites changes.
81 class Observer { 82 class Observer {
82 public: 83 public:
83 virtual void OnMostVisitedURLsAvailable(const NTPTilesVector& tiles) = 0; 84 virtual void OnMostVisitedURLsAvailable(const NTPTilesVector& tiles) = 0;
84 virtual void OnIconMadeAvailable(const GURL& site_url) = 0; 85 virtual void OnIconMadeAvailable(const GURL& site_url) = 0;
85 86
86 protected: 87 protected:
87 virtual ~Observer() {} 88 virtual ~Observer() {}
88 }; 89 };
89 90
90 // Construct a MostVisitedSites instance. 91 // Construct a MostVisitedSites instance.
91 // 92 //
92 // |prefs| and |suggestions| are required and may not be null. |top_sites|, 93 // |prefs| and |suggestions| are required and may not be null. |top_sites|,
93 // |popular_sites|, and |supervisor| are optional and if null the associated 94 // |popular_sites|, and |supervisor| are optional and if null the associated
94 // features will be disabled. 95 // features will be disabled.
96 //
97 // Clients must call Refresh() to fetch the initial tiles.
95 MostVisitedSites(PrefService* prefs, 98 MostVisitedSites(PrefService* prefs,
96 scoped_refptr<history::TopSites> top_sites, 99 scoped_refptr<history::TopSites> top_sites,
97 suggestions::SuggestionsService* suggestions, 100 suggestions::SuggestionsService* suggestions,
98 std::unique_ptr<PopularSites> popular_sites, 101 std::unique_ptr<PopularSites> popular_sites,
99 std::unique_ptr<IconCacher> icon_cacher, 102 std::unique_ptr<IconCacher> icon_cacher,
100 std::unique_ptr<MostVisitedSitesSupervisor> supervisor); 103 std::unique_ptr<MostVisitedSitesSupervisor> supervisor);
101 104
102 ~MostVisitedSites() override; 105 ~MostVisitedSites() override;
103 106
104 // Sets the observer, and immediately fetches the current suggestions. 107 // Observer registration.
105 // Does not take ownership of |observer|, which must outlive this object and 108 void AddObserver(Observer* observer);
106 // must not be null. 109 void RemoveObserver(Observer* observer);
107 void SetMostVisitedURLsObserver(Observer* observer, int num_sites);
108 110
109 // Requests an asynchronous refresh of the suggestions. Notifies the observer 111 // Requests an asynchronous refresh of the suggestions. Notifies the observers
110 // once the request completes. 112 // once the request completes.
sfiera 2017/01/18 09:58:33 This is now also necessary after every AddObserver
111 void Refresh(); 113 void Refresh();
112 114
115 // Blacklist manipulation.
113 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); 116 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url);
114 void ClearBlacklistedUrls(); 117 void ClearBlacklistedUrls();
115 118
116 // MostVisitedSitesSupervisor::Observer implementation. 119 // MostVisitedSitesSupervisor::Observer implementation.
117 void OnBlockedSitesChanged() override; 120 void OnBlockedSitesChanged() override;
118 121
119 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 122 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
120 123
121 // Workhorse for SaveNewTiles. Implemented as a separate static and public 124 // Workhorse for SaveNewTiles. Implemented as a separate static and public
122 // method for ease of testing. 125 // method for ease of testing.
(...skipping 26 matching lines...) Expand all
149 152
150 // Takes the personal and whitelist tiles and creates popular tiles if 153 // Takes the personal and whitelist tiles and creates popular tiles if
151 // necessary. 154 // necessary.
152 NTPTilesVector CreatePopularSitesTiles(const NTPTilesVector& personal_tiles, 155 NTPTilesVector CreatePopularSitesTiles(const NTPTilesVector& personal_tiles,
153 const NTPTilesVector& whitelist_tiles); 156 const NTPTilesVector& whitelist_tiles);
154 157
155 // Takes the personal tiles, creates and merges in whitelist and popular tiles 158 // Takes the personal tiles, creates and merges in whitelist and popular tiles
156 // if appropriate, and saves the new tiles. 159 // if appropriate, and saves the new tiles.
157 void SaveNewTiles(NTPTilesVector personal_tiles); 160 void SaveNewTiles(NTPTilesVector personal_tiles);
158 161
159 // Notifies the observer about the availability of tiles. 162 // Notifies the observers about the availability of tiles.
160 // Also records impressions UMA if not done already. 163 // Also records impressions UMA if not done already.
161 void NotifyMostVisitedURLsObserver(); 164 void NotifyMostVisitedURLsObservers();
162 165
163 void OnPopularSitesAvailable(bool success); 166 void OnPopularSitesAvailable(bool success);
164 167
165 void OnIconMadeAvailable(const GURL& site_url, bool newly_available); 168 void OnIconMadeAvailable(const GURL& site_url, bool newly_available);
166 169
167 // history::TopSitesObserver implementation. 170 // history::TopSitesObserver implementation.
168 void TopSitesLoaded(history::TopSites* top_sites) override; 171 void TopSitesLoaded(history::TopSites* top_sites) override;
169 void TopSitesChanged(history::TopSites* top_sites, 172 void TopSitesChanged(history::TopSites* top_sites,
170 ChangeReason change_reason) override; 173 ChangeReason change_reason) override;
171 174
172 PrefService* prefs_; 175 PrefService* prefs_;
173 scoped_refptr<history::TopSites> top_sites_; 176 scoped_refptr<history::TopSites> top_sites_;
174 suggestions::SuggestionsService* suggestions_service_; 177 suggestions::SuggestionsService* suggestions_service_;
175 std::unique_ptr<PopularSites> const popular_sites_; 178 std::unique_ptr<PopularSites> const popular_sites_;
176 std::unique_ptr<IconCacher> const icon_cacher_; 179 std::unique_ptr<IconCacher> const icon_cacher_;
177 std::unique_ptr<MostVisitedSitesSupervisor> supervisor_; 180 std::unique_ptr<MostVisitedSitesSupervisor> supervisor_;
178 181
179 Observer* observer_; 182 base::ObserverList<Observer> observer_list_;
180
181 // The maximum number of most visited sites to return.
182 int num_sites_;
183 183
184 std::unique_ptr< 184 std::unique_ptr<
185 suggestions::SuggestionsService::ResponseCallbackList::Subscription> 185 suggestions::SuggestionsService::ResponseCallbackList::Subscription>
186 suggestions_subscription_; 186 suggestions_subscription_;
187 187
188 ScopedObserver<history::TopSites, history::TopSitesObserver> 188 ScopedObserver<history::TopSites, history::TopSitesObserver>
189 top_sites_observer_; 189 top_sites_observer_;
190 190
191 // The main source of personal tiles - either TOP_SITES or SUGGESTIONS_SEVICE. 191 // The main source of personal tiles - either TOP_SITES or SUGGESTIONS_SEVICE.
192 NTPTileSource mv_source_; 192 NTPTileSource mv_source_;
193 193
194 NTPTilesVector current_tiles_; 194 NTPTilesVector current_tiles_;
195 195
196 // For callbacks may be run after destruction, used exclusively for TopSites 196 // For callbacks may be run after destruction, used exclusively for TopSites
197 // (since it's used to detect whether there's a query in flight). 197 // (since it's used to detect whether there's a query in flight).
198 base::WeakPtrFactory<MostVisitedSites> top_sites_weak_ptr_factory_; 198 base::WeakPtrFactory<MostVisitedSites> top_sites_weak_ptr_factory_;
199 199
200 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); 200 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites);
201 }; 201 };
202 202
203 } // namespace ntp_tiles 203 } // namespace ntp_tiles
204 204
205 #endif // COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_ 205 #endif // COMPONENTS_NTP_TILES_MOST_VISITED_SITES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698