| 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/android/ntp/popular_sites.h" |
| 20 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
| 21 #include "chrome/browser/supervised_user/supervised_user_service_observer.h" | |
| 22 #include "components/history/core/browser/history_types.h" | 20 #include "components/history/core/browser/history_types.h" |
| 23 #include "components/history/core/browser/top_sites_observer.h" | 21 #include "components/history/core/browser/top_sites_observer.h" |
| 24 #include "components/suggestions/proto/suggestions.pb.h" | 22 #include "components/suggestions/proto/suggestions.pb.h" |
| 25 #include "components/suggestions/suggestions_service.h" | 23 #include "components/suggestions/suggestions_service.h" |
| 26 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 27 | 25 |
| 28 namespace history { | 26 namespace history { |
| 29 class TopSites; | 27 class TopSites; |
| 30 } | 28 } |
| 31 | 29 |
| 32 namespace suggestions { | 30 namespace suggestions { |
| 33 class SuggestionsService; | 31 class SuggestionsService; |
| 34 } | 32 } |
| 35 | 33 |
| 36 namespace user_prefs { | 34 namespace user_prefs { |
| 37 class PrefRegistrySyncable; | 35 class PrefRegistrySyncable; |
| 38 } | 36 } |
| 39 | 37 |
| 40 namespace variations { | 38 namespace variations { |
| 41 class VariationsService; | 39 class VariationsService; |
| 42 } | 40 } |
| 43 | 41 |
| 42 // Shim interface for SupervisedUserService. |
| 43 class MostVisitedSitesSupervisor { |
| 44 public: |
| 45 struct Whitelist { |
| 46 base::string16 title; |
| 47 GURL entry_point; |
| 48 base::FilePath large_icon_path; |
| 49 }; |
| 50 |
| 51 class Observer { |
| 52 public: |
| 53 virtual void OnBlockedSitesChanged() = 0; |
| 54 |
| 55 protected: |
| 56 ~Observer() {} |
| 57 }; |
| 58 |
| 59 // Pass non-null to set observer, or null to remove observer. |
| 60 // If setting observer, there must not yet be an observer set. |
| 61 // If removing observer, there must already be one to remove. |
| 62 // Does not take ownership. Observer must outlive this object. |
| 63 virtual void SetObserver(Observer* new_observer) = 0; |
| 64 |
| 65 // If true, |url| should not be shown on the NTP. |
| 66 virtual bool IsBlocked(const GURL& url) = 0; |
| 67 |
| 68 // Explicit suggestions for sites to show on NTP. |
| 69 virtual std::vector<Whitelist> whitelists() = 0; |
| 70 |
| 71 // If true, be conservative about suggesting sites from outside sources. |
| 72 virtual bool IsChildProfile() = 0; |
| 73 |
| 74 protected: |
| 75 virtual ~MostVisitedSitesSupervisor() {} |
| 76 }; |
| 77 |
| 44 // Tracks the list of most visited sites and their thumbnails. | 78 // Tracks the list of most visited sites and their thumbnails. |
| 45 // | 79 // |
| 46 // Do not use, except from MostVisitedSitesBridge. The interface is in flux | 80 // Do not use, except from MostVisitedSitesBridge. The interface is in flux |
| 47 // while we are extracting the functionality of the Java class to make available | 81 // while we are extracting the functionality of the Java class to make available |
| 48 // in C++. | 82 // in C++. |
| 49 // | 83 // |
| 50 // TODO(sfiera): finalize interface. | 84 // TODO(sfiera): finalize interface. |
| 51 class MostVisitedSites : public history::TopSitesObserver, | 85 class MostVisitedSites : public history::TopSitesObserver, |
| 52 public SupervisedUserServiceObserver { | 86 public MostVisitedSitesSupervisor::Observer { |
| 53 public: | 87 public: |
| 54 struct Suggestion; | 88 struct Suggestion; |
| 55 using SuggestionsVector = std::vector<Suggestion>; | 89 using SuggestionsVector = std::vector<Suggestion>; |
| 56 using PopularSitesVector = std::vector<PopularSites::Site>; | 90 using PopularSitesVector = std::vector<PopularSites::Site>; |
| 57 | 91 |
| 58 // The source of the Most Visited sites. | 92 // The source of the Most Visited sites. |
| 59 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; | 93 enum MostVisitedSource { TOP_SITES, SUGGESTIONS_SERVICE, POPULAR, WHITELIST }; |
| 60 | 94 |
| 61 // The observer to be notified when the list of most visited sites changes. | 95 // The observer to be notified when the list of most visited sites changes. |
| 62 class Observer { | 96 class Observer { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 90 DISALLOW_COPY_AND_ASSIGN(Suggestion); | 124 DISALLOW_COPY_AND_ASSIGN(Suggestion); |
| 91 }; | 125 }; |
| 92 | 126 |
| 93 MostVisitedSites(PrefService* prefs, | 127 MostVisitedSites(PrefService* prefs, |
| 94 const TemplateURLService* template_url_service, | 128 const TemplateURLService* template_url_service, |
| 95 variations::VariationsService* variations_service, | 129 variations::VariationsService* variations_service, |
| 96 net::URLRequestContextGetter* download_context, | 130 net::URLRequestContextGetter* download_context, |
| 97 const base::FilePath& popular_sites_directory, | 131 const base::FilePath& popular_sites_directory, |
| 98 scoped_refptr<history::TopSites> top_sites, | 132 scoped_refptr<history::TopSites> top_sites, |
| 99 suggestions::SuggestionsService* suggestions, | 133 suggestions::SuggestionsService* suggestions, |
| 100 bool is_child_profile, | 134 MostVisitedSitesSupervisor* supervisor); |
| 101 Profile* profile); | |
| 102 | 135 |
| 103 ~MostVisitedSites() override; | 136 ~MostVisitedSites() override; |
| 104 | 137 |
| 105 // Does not take ownership of |observer|, which must outlive this object and | 138 // Does not take ownership of |observer|, which must outlive this object and |
| 106 // must not be null. | 139 // must not be null. |
| 107 void SetMostVisitedURLsObserver(Observer* observer, int num_sites); | 140 void SetMostVisitedURLsObserver(Observer* observer, int num_sites); |
| 108 | 141 |
| 109 using ThumbnailCallback = base::Callback< | 142 using ThumbnailCallback = base::Callback< |
| 110 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>; | 143 void(bool /* is_local_thumbnail */, const SkBitmap* /* bitmap */)>; |
| 111 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback); | 144 void GetURLThumbnail(const GURL& url, const ThumbnailCallback& callback); |
| 112 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); | 145 void AddOrRemoveBlacklistedUrl(const GURL& url, bool add_url); |
| 113 void RecordTileTypeMetrics(const std::vector<int>& tile_types); | 146 void RecordTileTypeMetrics(const std::vector<int>& tile_types); |
| 114 void RecordOpenedMostVisitedItem(int index, int tile_type); | 147 void RecordOpenedMostVisitedItem(int index, int tile_type); |
| 115 | 148 |
| 116 // SupervisedUserServiceObserver implementation. | 149 // MostVisitedSitesSupervisor::Observer implementation. |
| 117 void OnURLFilterChanged() override; | 150 void OnBlockedSitesChanged() override; |
| 118 | 151 |
| 119 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 152 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
| 120 | 153 |
| 121 private: | 154 private: |
| 122 friend class MostVisitedSitesTest; | 155 friend class MostVisitedSitesTest; |
| 123 | 156 |
| 124 // TODO(treib): use SuggestionsVector in internal functions. crbug.com/601734 | 157 // TODO(treib): use SuggestionsVector in internal functions. crbug.com/601734 |
| 125 using SuggestionsPtrVector = std::vector<std::unique_ptr<Suggestion>>; | 158 using SuggestionsPtrVector = std::vector<std::unique_ptr<Suggestion>>; |
| 126 | 159 |
| 127 void BuildCurrentSuggestions(); | 160 void BuildCurrentSuggestions(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 void RecordThumbnailUMAMetrics(); | 248 void RecordThumbnailUMAMetrics(); |
| 216 | 249 |
| 217 // Records UMA histogram metrics related to the number of impressions. | 250 // Records UMA histogram metrics related to the number of impressions. |
| 218 void RecordImpressionUMAMetrics(); | 251 void RecordImpressionUMAMetrics(); |
| 219 | 252 |
| 220 // history::TopSitesObserver implementation. | 253 // history::TopSitesObserver implementation. |
| 221 void TopSitesLoaded(history::TopSites* top_sites) override; | 254 void TopSitesLoaded(history::TopSites* top_sites) override; |
| 222 void TopSitesChanged(history::TopSites* top_sites, | 255 void TopSitesChanged(history::TopSites* top_sites, |
| 223 ChangeReason change_reason) override; | 256 ChangeReason change_reason) override; |
| 224 | 257 |
| 225 // The profile whose most visited sites will be queried. | |
| 226 Profile* profile_; | |
| 227 | |
| 228 PrefService* prefs_; | 258 PrefService* prefs_; |
| 229 const TemplateURLService* template_url_service_; | 259 const TemplateURLService* template_url_service_; |
| 230 variations::VariationsService* variations_service_; | 260 variations::VariationsService* variations_service_; |
| 231 net::URLRequestContextGetter* download_context_; | 261 net::URLRequestContextGetter* download_context_; |
| 232 base::FilePath popular_sites_directory_; | 262 base::FilePath popular_sites_directory_; |
| 233 scoped_refptr<history::TopSites> top_sites_; | 263 scoped_refptr<history::TopSites> top_sites_; |
| 234 suggestions::SuggestionsService* suggestions_service_; | 264 suggestions::SuggestionsService* suggestions_service_; |
| 235 | 265 MostVisitedSitesSupervisor* supervisor_; |
| 236 // Children will not be shown popular sites as suggestions. | |
| 237 // TODO(sfiera): enable/disable suggestions if the profile is marked or | |
| 238 // unmarked as a child profile during the lifetime of the object. For now, it | |
| 239 // doesn't matter, because a MostVisitedSites is instantiated for each NTP, | |
| 240 // but a longer-lived object would need to update. | |
| 241 bool is_child_profile_; | |
| 242 | 266 |
| 243 Observer* observer_; | 267 Observer* observer_; |
| 244 | 268 |
| 245 // The maximum number of most visited sites to return. | 269 // The maximum number of most visited sites to return. |
| 246 int num_sites_; | 270 int num_sites_; |
| 247 | 271 |
| 248 // Whether we have received an initial set of most visited sites (from either | 272 // Whether we have received an initial set of most visited sites (from either |
| 249 // TopSites or the SuggestionsService). | 273 // TopSites or the SuggestionsService). |
| 250 bool received_most_visited_sites_; | 274 bool received_most_visited_sites_; |
| 251 | 275 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 269 | 293 |
| 270 SuggestionsVector current_suggestions_; | 294 SuggestionsVector current_suggestions_; |
| 271 | 295 |
| 272 // For callbacks may be run after destruction. | 296 // For callbacks may be run after destruction. |
| 273 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; | 297 base::WeakPtrFactory<MostVisitedSites> weak_ptr_factory_; |
| 274 | 298 |
| 275 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); | 299 DISALLOW_COPY_AND_ASSIGN(MostVisitedSites); |
| 276 }; | 300 }; |
| 277 | 301 |
| 278 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ | 302 #endif // CHROME_BROWSER_ANDROID_NTP_MOST_VISITED_SITES_H_ |
| OLD | NEW |