| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | |
| 6 #define CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/weak_ptr.h" | |
| 18 #include "base/scoped_observer.h" | |
| 19 #include "chrome/browser/android/ntp/popular_sites.h" | |
| 20 #include "components/history/core/browser/history_types.h" | |
| 21 #include "components/history/core/browser/top_sites_observer.h" | |
| 22 #include "components/suggestions/proto/suggestions.pb.h" | |
| 23 #include "components/suggestions/suggestions_service.h" | |
| 24 #include "url/gurl.h" | |
| 25 | |
| 26 namespace gfx { | |
| 27 class Image; | |
| 28 } | |
| 29 | |
| 30 namespace history { | |
| 31 class TopSites; | |
| 32 } | |
| 33 | |
| 34 namespace suggestions { | |
| 35 class SuggestionsService; | |
| 36 } | |
| 37 | |
| 38 namespace user_prefs { | |
| 39 class PrefRegistrySyncable; | |
| 40 } | |
| 41 | |
| 42 namespace variations { | |
| 43 class VariationsService; | |
| 44 } | |
| 45 | |
| 46 // Shim interface for SupervisedUserService. | |
| 47 class MostVisitedSitesSupervisor { | |
| 48 public: | |
| 49 struct Whitelist { | |
| 50 base::string16 title; | |
| 51 GURL entry_point; | |
| 52 base::FilePath large_icon_path; | |
| 53 }; | |
| 54 | |
| 55 class Observer { | |
| 56 public: | |
| 57 virtual void OnBlockedSitesChanged() = 0; | |
| 58 | |
| 59 protected: | |
| 60 ~Observer() {} | |
| 61 }; | |
| 62 | |
| 63 // Pass non-null to set observer, or null to remove observer. | |
| 64 // If setting observer, there must not yet be an observer set. | |
| 65 // If removing observer, there must already be one to remove. | |
| 66 // Does not take ownership. Observer must outlive this object. | |
| 67 virtual void SetObserver(Observer* new_observer) = 0; | |
| 68 | |
| 69 // If true, |url| should not be shown on the NTP. | |
| 70 virtual bool IsBlocked(const GURL& url) = 0; | |
| 71 | |
| 72 // Explicit suggestions for sites to show on NTP. | |
| 73 virtual std::vector<Whitelist> whitelists() = 0; | |
| 74 | |
| 75 // If true, be conservative about suggesting sites from outside sources. | |
| 76 virtual bool IsChildProfile() = 0; | |
| 77 | |
| 78 protected: | |
| 79 virtual ~MostVisitedSitesSupervisor() {} | |
| 80 }; | |
| 81 | |
| 82 // Tracks the list of most visited sites and their thumbnails. | |
| 83 // | |
| 84 // Do not use, except from MostVisitedSitesBridge. The interface is in flux | |
| 85 // while we are extracting the functionality of the Java class to make available | |
| 86 // in C++. | |
| 87 // | |
| 88 // TODO(sfiera): finalize interface. | |
| 89 class MostVisitedSites : public history::TopSitesObserver, | |
| 90 public MostVisitedSitesSupervisor::Observer { | |
| 91 public: | |
| 92 struct Suggestion; | |
| 93 using SuggestionsVector = std::vector<Suggestion>; | |
| 94 using PopularSitesVector = std::vector<PopularSites::Site>; | |
| 95 | |
| 96 // The source of the Most Visited sites. | |
| 97 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; | |
| 98 | |
| 99 // The observer to be notified when the list of most visited sites changes. | |
| 100 class Observer { | |
| 101 public: | |
| 102 virtual void OnMostVisitedURLsAvailable( | |
| 103 const SuggestionsVector& suggestions) = 0; | |
| 104 virtual void OnPopularURLsAvailable(const PopularSitesVector& sites) = 0; | |
| 105 | |
| 106 protected: | |
| 107 virtual ~Observer() {} | |
| 108 }; | |
| 109 | |
| 110 struct Suggestion { | |
| 111 base::string16 title; | |
| 112 GURL url; | |
| 113 MostVisitedSource source; | |
| 114 | |
| 115 // Only valid for source == WHITELIST (empty otherwise). | |
| 116 base::FilePath whitelist_icon_path; | |
| 117 | |
| 118 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise). | |
| 119 int provider_index; | |
| 120 | |
| 121 Suggestion(); | |
| 122 ~Suggestion(); | |
| 123 | |
| 124 Suggestion(Suggestion&&); | |
| 125 Suggestion& operator=(Suggestion&&); | |
| 126 | |
| 127 private: | |
| 128 DISALLOW_COPY_AND_ASSIGN(Suggestion); | |
| 129 }; | |
| 130 | |
| 131 MostVisitedSites(scoped_refptr<base::SequencedWorkerPool> blocking_pool, | |
| 132 PrefService* prefs, | |
| 133 const TemplateURLService* template_url_service, | |
| 134 variations::VariationsService* variations_service, | |
| 135 net::URLRequestContextGetter* download_context, | |
| 136 const base::FilePath& popular_sites_directory, | |
| 137 scoped_refptr<history::TopSites> top_sites, | |
| 138 suggestions::SuggestionsService* suggestions, | |
| 139 MostVisitedSitesSupervisor* supervisor); | |
| 140 | |
| 141 ~MostVisitedSites() override; | |
| 142 | |
| 143 // Does not take ownership of |observer|, which must outlive this object and | |
| 144 // must not be null. | |
| 145 void SetMostVisitedURLsObserver(Observer* observer, int num_sites); | |
| 146 | |
| 147 using ThumbnailCallback = base::Callback< | |
| 148 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>; | |
| 149 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback); | |
| 150 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); | |
| 151 void RecordTileTypeMetrics(const std::vector<int>& tile_types); | |
| 152 void RecordOpenedMostVisitedItem(int index, int tile_type); | |
| 153 | |
| 154 // MostVisitedSitesSupervisor::Observer implementation. | |
| 155 void OnBlockedSitesChanged() override; | |
| 156 | |
| 157 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | |
| 158 | |
| 159 private: | |
| 160 friend class MostVisitedSitesTest; | |
| 161 | |
| 162 // TODO(treib): use SuggestionsVector in internal functions. crbug.com/601734 | |
| 163 using SuggestionsPtrVector = std::vector<std::unique_ptr<Suggestion>>; | |
| 164 | |
| 165 void BuildCurrentSuggestions(); | |
| 166 | |
| 167 // Initialize the query to Top Sites. Called if the SuggestionsService | |
| 168 // returned no data. | |
| 169 void InitiateTopSitesQuery(); | |
| 170 | |
| 171 // If there's a whitelist entry point for the URL, return the large icon path. | |
| 172 base::FilePath GetWhitelistLargeIconPath(const GURL& url); | |
| 173 | |
| 174 // Callback for when data is available from TopSites. | |
| 175 void OnMostVisitedURLsAvailable( | |
| 176 const history::MostVisitedURLList& visited_list); | |
| 177 | |
| 178 // Callback for when data is available from the SuggestionsService. | |
| 179 void OnSuggestionsProfileAvailable( | |
| 180 const suggestions::SuggestionsProfile& suggestions_profile); | |
| 181 | |
| 182 // Takes the personal suggestions and creates whitelist entry point | |
| 183 // suggestions if necessary. | |
| 184 SuggestionsPtrVector CreateWhitelistEntryPointSuggestions( | |
| 185 const SuggestionsPtrVector& personal_suggestions); | |
| 186 | |
| 187 // Takes the personal and whitelist suggestions and creates popular | |
| 188 // suggestions if necessary. | |
| 189 SuggestionsPtrVector CreatePopularSitesSuggestions( | |
| 190 const SuggestionsPtrVector& personal_suggestions, | |
| 191 const SuggestionsPtrVector& whitelist_suggestions); | |
| 192 | |
| 193 // Takes the personal suggestions, creates and merges in whitelist and popular | |
| 194 // suggestions if appropriate, and saves the new suggestions. | |
| 195 void SaveNewSuggestions(SuggestionsPtrVector* personal_suggestions); | |
| 196 | |
| 197 // Workhorse for SaveNewSuggestions above. Implemented as a separate static | |
| 198 // method for ease of testing. | |
| 199 static SuggestionsPtrVector MergeSuggestions( | |
| 200 SuggestionsPtrVector* personal_suggestions, | |
| 201 SuggestionsPtrVector* whitelist_suggestions, | |
| 202 SuggestionsPtrVector* popular_suggestions); | |
| 203 | |
| 204 // Appends suggestions from |src| to |dst|. | |
| 205 static void AppendSuggestions(SuggestionsPtrVector* src, | |
| 206 SuggestionsPtrVector* dst); | |
| 207 | |
| 208 void SaveCurrentSuggestionsToPrefs(); | |
| 209 | |
| 210 // Notifies the observer about the availability of suggestions. | |
| 211 // Also records impressions UMA if not done already. | |
| 212 void NotifyMostVisitedURLsObserver(); | |
| 213 | |
| 214 void OnPopularSitesAvailable(bool success); | |
| 215 | |
| 216 // Runs on the UI Thread. | |
| 217 void OnLocalThumbnailFetched( | |
| 218 const GURL& url, | |
| 219 const ThumbnailCallback& callback, | |
| 220 std::unique_ptr<SkBitmap> bitmap); | |
| 221 | |
| 222 // Callback for when the thumbnail lookup is complete. | |
| 223 // Runs on the UI Thread. | |
| 224 void OnObtainedThumbnail( | |
| 225 bool is_local_thumbnail, | |
| 226 const ThumbnailCallback& callback, | |
| 227 const GURL& url, | |
| 228 const gfx::Image& bitmap); | |
| 229 | |
| 230 // Records thumbnail-related UMA histogram metrics. | |
| 231 void RecordThumbnailUMAMetrics(); | |
| 232 | |
| 233 // Records UMA histogram metrics related to the number of impressions. | |
| 234 void RecordImpressionUMAMetrics(); | |
| 235 | |
| 236 // history::TopSitesObserver implementation. | |
| 237 void TopSitesLoaded(history::TopSites* top_sites) override; | |
| 238 void TopSitesChanged(history::TopSites* top_sites, | |
| 239 ChangeReason change_reason) override; | |
| 240 | |
| 241 PrefService* prefs_; | |
| 242 const TemplateURLService* template_url_service_; | |
| 243 variations::VariationsService* variations_service_; | |
| 244 net::URLRequestContextGetter* download_context_; | |
| 245 base::FilePath popular_sites_directory_; | |
| 246 scoped_refptr<history::TopSites> top_sites_; | |
| 247 suggestions::SuggestionsService* suggestions_service_; | |
| 248 MostVisitedSitesSupervisor* supervisor_; | |
| 249 | |
| 250 Observer* observer_; | |
| 251 | |
| 252 // The maximum number of most visited sites to return. | |
| 253 int num_sites_; | |
| 254 | |
| 255 // Whether we have received an initial set of most visited sites (from either | |
| 256 // TopSites or the SuggestionsService). | |
| 257 bool received_most_visited_sites_; | |
| 258 | |
| 259 // Whether we have received the set of popular sites. Immediately set to true | |
| 260 // if popular sites are disabled. | |
| 261 bool received_popular_sites_; | |
| 262 | |
| 263 // Whether we have recorded one-shot UMA metrics such as impressions. They are | |
| 264 // recorded once both the previous flags are true. | |
| 265 bool recorded_uma_; | |
| 266 | |
| 267 std::unique_ptr< | |
| 268 suggestions::SuggestionsService::ResponseCallbackList::Subscription> | |
| 269 suggestions_subscription_; | |
| 270 | |
| 271 ScopedObserver<history::TopSites, history::TopSitesObserver> scoped_observer_; | |
| 272 | |
| 273 MostVisitedSource mv_source_; | |
| 274 | |
| 275 std::unique_ptr<PopularSites> popular_sites_; | |
| 276 | |
| 277 SuggestionsVector current_suggestions_; | |
| 278 | |
| 279 base::ThreadChecker thread_checker_; | |
| 280 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; | |
| 281 scoped_refptr<base::TaskRunner> blocking_runner_; | |
| 282 | |
| 283 // For callbacks may be run after destruction. | |
| 284 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; | |
| 285 | |
| 286 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); | |
| 287 }; | |
| 288 | |
| 289 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | |
| OLD | NEW |