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

Side by Side Diff: chrome/browser/android/most_visited_sites.cc

Issue 1308723005: Popular sites on the NTP: re-download popular suggestions once per Chrome run (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@knn_ordering
Patch Set: rebase Created 5 years, 3 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 #include "chrome/browser/android/most_visited_sites.h" 5 #include "chrome/browser/android/most_visited_sites.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h" 8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h" 9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h" 10 #include "base/android/scoped_java_ref.h"
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 return true; 127 return true;
128 return base::StartsWith(group_name, "Enabled", 128 return base::StartsWith(group_name, "Enabled",
129 base::CompareCase::INSENSITIVE_ASCII); 129 base::CompareCase::INSENSITIVE_ASCII);
130 } 130 }
131 131
132 std::string GetPopularSitesFilename() { 132 std::string GetPopularSitesFilename() {
133 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, 133 return variations::GetVariationParamValue(kPopularSitesFieldTrialName,
134 "filename"); 134 "filename");
135 } 135 }
136 136
137 bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) {
battre 2015/09/14 14:31:25 Please document the meaning of num_tiles.
Marc Treib 2015/09/18 11:44:24 Done.
138 const base::ListValue* source_list =
139 prefs->GetList(prefs::kNTPSuggestionsIsPersonal);
140 // If there aren't enough previous suggestions to fill the grid, we need
141 // popular suggestions.
142 if (source_list->GetSize() < num_tiles)
143 return true;
144 // Otherwise, if any of the previous suggestions is not personal, then also
145 // get popular suggestions.
146 for (size_t i = 0; i < num_tiles; ++i) {
147 bool is_personal;
battre 2015/09/14 14:31:25 please initialize this variable.
Marc Treib 2015/09/18 11:44:24 Done.
148 if (source_list->GetBoolean(i, &is_personal) && !is_personal)
149 return true;
150 }
151 // The whole grid is already filled with personal suggestions, no point in
152 // bothering with popular ones.
153 return false;
154 }
155
137 } // namespace 156 } // namespace
138 157
139 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, 158 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
140 const std::string& url, 159 const std::string& url,
141 MostVisitedSource source) 160 MostVisitedSource source)
142 : title(title), url(url), source(source), provider_index(-1) {} 161 : title(title), url(url), source(source), provider_index(-1) {}
143 162
144 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, 163 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
145 const GURL& url, 164 const GURL& url,
146 MostVisitedSource source) 165 MostVisitedSource source)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 new suggestions::SuggestionsSource(profile_)); 201 new suggestions::SuggestionsSource(profile_));
183 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 202 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
184 203
185 // Register this class as an observer to the sync service. It is important to 204 // Register this class as an observer to the sync service. It is important to
186 // be notified of changes in the sync state such as initialization, sync 205 // be notified of changes in the sync state such as initialization, sync
187 // being enabled or disabled, etc. 206 // being enabled or disabled, etc.
188 ProfileSyncService* profile_sync_service = 207 ProfileSyncService* profile_sync_service =
189 ProfileSyncServiceFactory::GetForProfile(profile_); 208 ProfileSyncServiceFactory::GetForProfile(profile_);
190 if (profile_sync_service) 209 if (profile_sync_service)
191 profile_sync_service->AddObserver(this); 210 profile_sync_service->AddObserver(this);
192
193 if (ShouldShowPopularSites()) {
194 popular_sites_.reset(new PopularSites(
195 profile,
196 GetPopularSitesFilename(),
197 profile_->GetRequestContext(),
198 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
199 base::Unretained(this))));
200 } else {
201 received_popular_sites_ = true;
202 }
203 } 211 }
204 212
205 MostVisitedSites::~MostVisitedSites() { 213 MostVisitedSites::~MostVisitedSites() {
206 ProfileSyncService* profile_sync_service = 214 ProfileSyncService* profile_sync_service =
207 ProfileSyncServiceFactory::GetForProfile(profile_); 215 ProfileSyncServiceFactory::GetForProfile(profile_);
208 if (profile_sync_service && profile_sync_service->HasObserver(this)) 216 if (profile_sync_service && profile_sync_service->HasObserver(this))
209 profile_sync_service->RemoveObserver(this); 217 profile_sync_service->RemoveObserver(this);
210 } 218 }
211 219
212 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) { 220 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) {
213 delete this; 221 delete this;
214 } 222 }
215 223
216 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) { 224 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) {
217 RecordThumbnailUMAMetrics(); 225 RecordThumbnailUMAMetrics();
218 } 226 }
219 227
220 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, 228 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env,
221 jobject obj, 229 jobject obj,
222 jobject j_observer, 230 jobject j_observer,
223 jint num_sites) { 231 jint num_sites) {
224 observer_.Reset(env, j_observer); 232 observer_.Reset(env, j_observer);
225 num_sites_ = num_sites; 233 num_sites_ = num_sites;
226 234
235 if (ShouldShowPopularSites() &&
236 NeedPopularSites(profile_->GetPrefs(), num_sites_)) {
237 popular_sites_.reset(new PopularSites(
238 profile_,
239 GetPopularSitesFilename(),
240 profile_->GetRequestContext(),
241 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
242 base::Unretained(this))));
243 } else {
244 received_popular_sites_ = true;
245 }
246
227 QueryMostVisitedURLs(); 247 QueryMostVisitedURLs();
228 248
229 scoped_refptr<history::TopSites> top_sites = 249 scoped_refptr<history::TopSites> top_sites =
230 TopSitesFactory::GetForProfile(profile_); 250 TopSitesFactory::GetForProfile(profile_);
231 if (top_sites) { 251 if (top_sites) {
232 // TopSites updates itself after a delay. To ensure up-to-date results, 252 // TopSites updates itself after a delay. To ensure up-to-date results,
233 // force an update now. 253 // force an update now.
234 top_sites->SyncWithHistory(); 254 top_sites->SyncWithHistory();
235 255
236 // Register as TopSitesObserver so that we can update ourselves when the 256 // Register as TopSitesObserver so that we can update ourselves when the
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 } 725 }
706 } 726 }
707 727
708 static jlong Init(JNIEnv* env, 728 static jlong Init(JNIEnv* env,
709 const JavaParamRef<jobject>& obj, 729 const JavaParamRef<jobject>& obj,
710 const JavaParamRef<jobject>& jprofile) { 730 const JavaParamRef<jobject>& jprofile) {
711 MostVisitedSites* most_visited_sites = 731 MostVisitedSites* most_visited_sites =
712 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 732 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
713 return reinterpret_cast<intptr_t>(most_visited_sites); 733 return reinterpret_cast<intptr_t>(most_visited_sites);
714 } 734 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/android/popular_sites.cc » ('j') | chrome/browser/net/file_downloader.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698