Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ |
| 6 #define CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | 6 #define CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
| 19 #include "chrome/browser/android/ntp/popular_sites.h" | |
| 19 #include "chrome/browser/supervised_user/supervised_user_service.h" | 20 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 20 #include "chrome/browser/supervised_user/supervised_user_service_observer.h" | 21 #include "chrome/browser/supervised_user/supervised_user_service_observer.h" |
| 21 #include "components/history/core/browser/history_types.h" | 22 #include "components/history/core/browser/history_types.h" |
| 22 #include "components/history/core/browser/top_sites_observer.h" | 23 #include "components/history/core/browser/top_sites_observer.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 |
| 27 namespace suggestions { | 28 namespace suggestions { |
| 28 class SuggestionsService; | 29 class SuggestionsService; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace user_prefs { | 32 namespace user_prefs { |
| 32 class PrefRegistrySyncable; | 33 class PrefRegistrySyncable; |
| 33 } | 34 } |
| 34 | 35 |
| 35 class PopularSites; | 36 class PopularSites; |
| 36 class Profile; | 37 class Profile; |
| 37 | 38 |
| 38 // The observer to be notified when the list of most visited sites changes. | |
| 39 class MostVisitedSitesObserver { | |
| 40 public: | |
| 41 virtual ~MostVisitedSitesObserver() {} | |
| 42 | |
| 43 virtual void OnMostVisitedURLsAvailable( | |
| 44 const std::vector<base::string16>& titles, | |
| 45 const std::vector<std::string>& urls, | |
| 46 const std::vector<std::string>& whitelist_icon_paths) = 0; | |
| 47 virtual void OnPopularURLsAvailable( | |
| 48 const std::vector<std::string>& urls, | |
| 49 const std::vector<std::string>& favicon_urls, | |
| 50 const std::vector<std::string>& large_icon_urls) = 0; | |
| 51 }; | |
| 52 | |
| 53 // Tracks the list of most visited sites and their thumbnails. | 39 // Tracks the list of most visited sites and their thumbnails. |
| 54 // | 40 // |
| 55 // Do not use, except from MostVisitedSitesBridge. The interface is in flux | 41 // Do not use, except from MostVisitedSitesBridge. The interface is in flux |
| 56 // while we are extracting the functionality of the Java class to make available | 42 // while we are extracting the functionality of the Java class to make available |
| 57 // in C++. | 43 // in C++. |
| 58 // | 44 // |
| 59 // TODO(sfiera): finalize interface. | 45 // TODO(sfiera): finalize interface. |
| 60 class MostVisitedSites : public history::TopSitesObserver, | 46 class MostVisitedSites : public history::TopSitesObserver, |
| 61 public SupervisedUserServiceObserver { | 47 public SupervisedUserServiceObserver { |
| 62 public: | 48 public: |
| 49 struct Suggestion; | |
| 50 using SuggestionsVector = std::vector<Suggestion>; | |
| 51 using PopularSitesVector = std::vector<PopularSites::Site>; | |
| 52 | |
| 53 // The source of the Most Visited sites. | |
| 54 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; | |
| 55 | |
| 56 // The observer to be notified when the list of most visited sites changes. | |
| 57 class Observer { | |
| 58 public: | |
| 59 virtual ~Observer() {} | |
|
Bernhard Bauer
2016/04/26 14:29:58
Is there a specific reason to include the destruct
sfiera
2016/04/26 14:42:26
Wouldn't that require subclasses to re-declare the
Bernhard Bauer
2016/04/26 14:50:49
I think that would happen automatically, as the su
| |
| 60 | |
| 61 virtual void OnMostVisitedURLsAvailable( | |
| 62 const SuggestionsVector& suggestions) = 0; | |
| 63 virtual void OnPopularURLsAvailable(const PopularSitesVector& sites) = 0; | |
| 64 }; | |
| 65 | |
| 66 struct Suggestion { | |
| 67 base::string16 title; | |
| 68 GURL url; | |
| 69 MostVisitedSource source; | |
| 70 | |
| 71 // Only valid for source == WHITELIST (empty otherwise). | |
| 72 base::FilePath whitelist_icon_path; | |
| 73 | |
| 74 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise). | |
| 75 int provider_index; | |
| 76 | |
| 77 Suggestion(); | |
| 78 ~Suggestion(); | |
| 79 | |
| 80 Suggestion(Suggestion&&); | |
| 81 Suggestion& operator=(Suggestion&&); | |
| 82 | |
| 83 private: | |
| 84 DISALLOW_COPY_AND_ASSIGN(Suggestion); | |
| 85 }; | |
| 86 | |
| 63 explicit MostVisitedSites(Profile* profile); | 87 explicit MostVisitedSites(Profile* profile); |
| 64 | 88 |
| 65 ~MostVisitedSites() override; | 89 ~MostVisitedSites() override; |
| 66 | 90 |
| 67 // Does not take ownership of |observer|, which must outlive this object. | 91 // Does not take ownership of |observer|, which must outlive this object. |
| 68 void SetMostVisitedURLsObserver( | 92 void SetMostVisitedURLsObserver( |
| 69 MostVisitedSitesObserver* observer, int num_sites); | 93 Observer* observer, int num_sites); |
| 70 | 94 |
| 71 using ThumbnailCallback = base::Callback< | 95 using ThumbnailCallback = base::Callback< |
| 72 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>; | 96 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>; |
| 73 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback); | 97 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback); |
| 74 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); | 98 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); |
| 75 void RecordTileTypeMetrics(const std::vector<int>& tile_types); | 99 void RecordTileTypeMetrics(const std::vector<int>& tile_types); |
| 76 void RecordOpenedMostVisitedItem(int index, int tile_type); | 100 void RecordOpenedMostVisitedItem(int index, int tile_type); |
| 77 | 101 |
| 78 // SupervisedUserServiceObserver implementation. | 102 // SupervisedUserServiceObserver implementation. |
| 79 void OnURLFilterChanged() override; | 103 void OnURLFilterChanged() override; |
| 80 | 104 |
| 81 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 105 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 82 | 106 |
| 83 private: | 107 private: |
| 84 friend class MostVisitedSitesTest; | 108 friend class MostVisitedSitesTest; |
| 85 | 109 |
| 86 // The source of the Most Visited sites. | 110 // TODO(treib): use SuggestionsVector in internal functions. crbug.com/601734 |
| 87 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; | 111 using SuggestionsPtrVector = std::vector<std::unique_ptr<Suggestion>>; |
| 88 | |
| 89 struct Suggestion { | |
| 90 base::string16 title; | |
| 91 GURL url; | |
| 92 MostVisitedSource source; | |
| 93 | |
| 94 // Only valid for source == WHITELIST (empty otherwise). | |
| 95 base::FilePath whitelist_icon_path; | |
| 96 | |
| 97 // Only valid for source == SUGGESTIONS_SERVICE (-1 otherwise). | |
| 98 int provider_index; | |
| 99 | |
| 100 Suggestion(); | |
| 101 ~Suggestion(); | |
| 102 | |
| 103 // Get the Histogram name associated with the source. | |
| 104 std::string GetSourceHistogramName() const; | |
| 105 | |
| 106 private: | |
| 107 DISALLOW_COPY_AND_ASSIGN(Suggestion); | |
| 108 }; | |
| 109 | |
| 110 using SuggestionsVector = std::vector<std::unique_ptr<Suggestion>>; | |
| 111 | 112 |
| 112 void QueryMostVisitedURLs(); | 113 void QueryMostVisitedURLs(); |
| 113 | 114 |
| 114 // Initialize the query to Top Sites. Called if the SuggestionsService is not | 115 // Initialize the query to Top Sites. Called if the SuggestionsService is not |
| 115 // enabled, or if it returns no data. | 116 // enabled, or if it returns no data. |
| 116 void InitiateTopSitesQuery(); | 117 void InitiateTopSitesQuery(); |
| 117 | 118 |
| 118 // If there's a whitelist entry point for the URL, return the large icon path. | 119 // If there's a whitelist entry point for the URL, return the large icon path. |
| 119 base::FilePath GetWhitelistLargeIconPath(const GURL& url); | 120 base::FilePath GetWhitelistLargeIconPath(const GURL& url); |
| 120 | 121 |
| 121 // Callback for when data is available from TopSites. | 122 // Callback for when data is available from TopSites. |
| 122 void OnMostVisitedURLsAvailable( | 123 void OnMostVisitedURLsAvailable( |
| 123 const history::MostVisitedURLList& visited_list); | 124 const history::MostVisitedURLList& visited_list); |
| 124 | 125 |
| 125 // Callback for when data is available from the SuggestionsService. | 126 // Callback for when data is available from the SuggestionsService. |
| 126 void OnSuggestionsProfileAvailable( | 127 void OnSuggestionsProfileAvailable( |
| 127 const suggestions::SuggestionsProfile& suggestions_profile); | 128 const suggestions::SuggestionsProfile& suggestions_profile); |
| 128 | 129 |
| 129 // Takes the personal suggestions and creates whitelist entry point | 130 // Takes the personal suggestions and creates whitelist entry point |
| 130 // suggestions if necessary. | 131 // suggestions if necessary. |
| 131 SuggestionsVector CreateWhitelistEntryPointSuggestions( | 132 SuggestionsPtrVector CreateWhitelistEntryPointSuggestions( |
| 132 const SuggestionsVector& personal_suggestions); | 133 const SuggestionsPtrVector& personal_suggestions); |
| 133 | 134 |
| 134 // Takes the personal and whitelist suggestions and creates popular | 135 // Takes the personal and whitelist suggestions and creates popular |
| 135 // suggestions if necessary. | 136 // suggestions if necessary. |
| 136 SuggestionsVector CreatePopularSitesSuggestions( | 137 SuggestionsPtrVector CreatePopularSitesSuggestions( |
| 137 const SuggestionsVector& personal_suggestions, | 138 const SuggestionsPtrVector& personal_suggestions, |
| 138 const SuggestionsVector& whitelist_suggestions); | 139 const SuggestionsPtrVector& whitelist_suggestions); |
| 139 | 140 |
| 140 // Takes the personal suggestions, creates and merges in whitelist and popular | 141 // Takes the personal suggestions, creates and merges in whitelist and popular |
| 141 // suggestions if appropriate, and saves the new suggestions. | 142 // suggestions if appropriate, and saves the new suggestions. |
| 142 void SaveNewNTPSuggestions(SuggestionsVector* personal_suggestions); | 143 void SaveNewNTPSuggestions(SuggestionsPtrVector* personal_suggestions); |
| 143 | 144 |
| 144 // Workhorse for SaveNewNTPSuggestions above. Implemented as a separate static | 145 // Workhorse for SaveNewNTPSuggestions above. Implemented as a separate static |
| 145 // method for ease of testing. | 146 // method for ease of testing. |
| 146 static SuggestionsVector MergeSuggestions( | 147 static SuggestionsPtrVector MergeSuggestions( |
| 147 SuggestionsVector* personal_suggestions, | 148 SuggestionsPtrVector* personal_suggestions, |
| 148 SuggestionsVector* whitelist_suggestions, | 149 SuggestionsPtrVector* whitelist_suggestions, |
| 149 SuggestionsVector* popular_suggestions, | 150 SuggestionsPtrVector* popular_suggestions, |
| 150 const std::vector<std::string>& old_sites_url, | 151 const std::vector<std::string>& old_sites_url, |
| 151 const std::vector<bool>& old_sites_is_personal); | 152 const std::vector<bool>& old_sites_is_personal); |
| 152 | 153 |
| 153 void GetPreviousNTPSites(size_t num_tiles, | 154 void GetPreviousNTPSites(size_t num_tiles, |
| 154 std::vector<std::string>* old_sites_url, | 155 std::vector<std::string>* old_sites_url, |
| 155 std::vector<bool>* old_sites_source) const; | 156 std::vector<bool>* old_sites_source) const; |
| 156 | 157 |
| 157 void SaveCurrentNTPSites(); | 158 void SaveCurrentNTPSites(); |
| 158 | 159 |
| 159 // Takes suggestions from |src_suggestions| and moves them to | 160 // Takes suggestions from |src_suggestions| and moves them to |
| 160 // |dst_suggestions| if the suggestion's url/host matches | 161 // |dst_suggestions| if the suggestion's url/host matches |
| 161 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from | 162 // |match_urls|/|match_hosts| respectively. Unmatched suggestion indices from |
| 162 // |src_suggestions| are returned for ease of insertion later. | 163 // |src_suggestions| are returned for ease of insertion later. |
| 163 static std::vector<size_t> InsertMatchingSuggestions( | 164 static std::vector<size_t> InsertMatchingSuggestions( |
| 164 SuggestionsVector* src_suggestions, | 165 SuggestionsPtrVector* src_suggestions, |
| 165 SuggestionsVector* dst_suggestions, | 166 SuggestionsPtrVector* dst_suggestions, |
| 166 const std::vector<std::string>& match_urls, | 167 const std::vector<std::string>& match_urls, |
| 167 const std::vector<std::string>& match_hosts); | 168 const std::vector<std::string>& match_hosts); |
| 168 | 169 |
| 169 // Inserts suggestions from |src_suggestions| at positions |insert_positions| | 170 // Inserts suggestions from |src_suggestions| at positions |insert_positions| |
| 170 // into |dst_suggestions| where ever empty starting from |start_position|. | 171 // into |dst_suggestions| where ever empty starting from |start_position|. |
| 171 // Returns the last filled position so that future insertions can start from | 172 // Returns the last filled position so that future insertions can start from |
| 172 // there. | 173 // there. |
| 173 static size_t InsertAllSuggestions( | 174 static size_t InsertAllSuggestions( |
| 174 size_t start_position, | 175 size_t start_position, |
| 175 const std::vector<size_t>& insert_positions, | 176 const std::vector<size_t>& insert_positions, |
| 176 SuggestionsVector* src_suggestions, | 177 SuggestionsPtrVector* src_suggestions, |
| 177 SuggestionsVector* dst_suggestions); | 178 SuggestionsPtrVector* dst_suggestions); |
| 178 | 179 |
| 179 // Notifies the observer about the availability of suggestions. | 180 // Notifies the observer about the availability of suggestions. |
| 180 // Also records impressions UMA if not done already. | 181 // Also records impressions UMA if not done already. |
| 181 void NotifyMostVisitedURLsObserver(); | 182 void NotifyMostVisitedURLsObserver(); |
| 182 | 183 |
| 183 void OnPopularSitesAvailable(bool success); | 184 void OnPopularSitesAvailable(bool success); |
| 184 | 185 |
| 185 // Runs on the UI Thread. | 186 // Runs on the UI Thread. |
| 186 void OnLocalThumbnailFetched( | 187 void OnLocalThumbnailFetched( |
| 187 const GURL& url, | 188 const GURL& url, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 203 void RecordImpressionUMAMetrics(); | 204 void RecordImpressionUMAMetrics(); |
| 204 | 205 |
| 205 // history::TopSitesObserver implementation. | 206 // history::TopSitesObserver implementation. |
| 206 void TopSitesLoaded(history::TopSites* top_sites) override; | 207 void TopSitesLoaded(history::TopSites* top_sites) override; |
| 207 void TopSitesChanged(history::TopSites* top_sites, | 208 void TopSitesChanged(history::TopSites* top_sites, |
| 208 ChangeReason change_reason) override; | 209 ChangeReason change_reason) override; |
| 209 | 210 |
| 210 // The profile whose most visited sites will be queried. | 211 // The profile whose most visited sites will be queried. |
| 211 Profile* profile_; | 212 Profile* profile_; |
| 212 | 213 |
| 213 MostVisitedSitesObserver* observer_; | 214 Observer* observer_; |
| 214 | 215 |
| 215 // The maximum number of most visited sites to return. | 216 // The maximum number of most visited sites to return. |
| 216 int num_sites_; | 217 int num_sites_; |
| 217 | 218 |
| 218 // Whether we have received an initial set of most visited sites (from either | 219 // Whether we have received an initial set of most visited sites (from either |
| 219 // TopSites or the SuggestionsService). | 220 // TopSites or the SuggestionsService). |
| 220 bool received_most_visited_sites_; | 221 bool received_most_visited_sites_; |
| 221 | 222 |
| 222 // Whether we have received the set of popular sites. Immediately set to true | 223 // Whether we have received the set of popular sites. Immediately set to true |
| 223 // if popular sites are disabled. | 224 // if popular sites are disabled. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 239 | 240 |
| 240 SuggestionsVector current_suggestions_; | 241 SuggestionsVector current_suggestions_; |
| 241 | 242 |
| 242 // For callbacks may be run after destruction. | 243 // For callbacks may be run after destruction. |
| 243 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; | 244 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; |
| 244 | 245 |
| 245 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); | 246 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); |
| 246 }; | 247 }; |
| 247 | 248 |
| 248 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | 249 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ |
| OLD | NEW |