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

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, 2 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
« no previous file with comments | « no previous file | chrome/browser/android/popular_sites.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Determine whether we need any popular suggestions to fill up a grid of
138 // |num_tiles| tiles.
139 bool NeedPopularSites(const PrefService* prefs, size_t num_tiles) {
140 const base::ListValue* source_list =
141 prefs->GetList(prefs::kNTPSuggestionsIsPersonal);
142 // If there aren't enough previous suggestions to fill the grid, we need
143 // popular suggestions.
144 if (source_list->GetSize() < num_tiles)
145 return true;
146 // Otherwise, if any of the previous suggestions is not personal, then also
147 // get popular suggestions.
148 for (size_t i = 0; i < num_tiles; ++i) {
149 bool is_personal = false;
150 if (source_list->GetBoolean(i, &is_personal) && !is_personal)
151 return true;
152 }
153 // The whole grid is already filled with personal suggestions, no point in
154 // bothering with popular ones.
155 return false;
156 }
157
137 } // namespace 158 } // namespace
138 159
139 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, 160 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
140 const std::string& url, 161 const std::string& url,
141 MostVisitedSource source) 162 MostVisitedSource source)
142 : title(title), url(url), source(source), provider_index(-1) {} 163 : title(title), url(url), source(source), provider_index(-1) {}
143 164
144 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, 165 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
145 const GURL& url, 166 const GURL& url,
146 MostVisitedSource source) 167 MostVisitedSource source)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 new suggestions::SuggestionsSource(profile_)); 203 new suggestions::SuggestionsSource(profile_));
183 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 204 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
184 205
185 // Register this class as an observer to the sync service. It is important to 206 // 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 207 // be notified of changes in the sync state such as initialization, sync
187 // being enabled or disabled, etc. 208 // being enabled or disabled, etc.
188 ProfileSyncService* profile_sync_service = 209 ProfileSyncService* profile_sync_service =
189 ProfileSyncServiceFactory::GetForProfile(profile_); 210 ProfileSyncServiceFactory::GetForProfile(profile_);
190 if (profile_sync_service) 211 if (profile_sync_service)
191 profile_sync_service->AddObserver(this); 212 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 } 213 }
204 214
205 MostVisitedSites::~MostVisitedSites() { 215 MostVisitedSites::~MostVisitedSites() {
206 ProfileSyncService* profile_sync_service = 216 ProfileSyncService* profile_sync_service =
207 ProfileSyncServiceFactory::GetForProfile(profile_); 217 ProfileSyncServiceFactory::GetForProfile(profile_);
208 if (profile_sync_service && profile_sync_service->HasObserver(this)) 218 if (profile_sync_service && profile_sync_service->HasObserver(this))
209 profile_sync_service->RemoveObserver(this); 219 profile_sync_service->RemoveObserver(this);
210 } 220 }
211 221
212 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) { 222 void MostVisitedSites::Destroy(JNIEnv* env, jobject obj) {
213 delete this; 223 delete this;
214 } 224 }
215 225
216 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) { 226 void MostVisitedSites::OnLoadingComplete(JNIEnv* env, jobject obj) {
217 RecordThumbnailUMAMetrics(); 227 RecordThumbnailUMAMetrics();
218 } 228 }
219 229
220 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, 230 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env,
221 jobject obj, 231 jobject obj,
222 jobject j_observer, 232 jobject j_observer,
223 jint num_sites) { 233 jint num_sites) {
224 observer_.Reset(env, j_observer); 234 observer_.Reset(env, j_observer);
225 num_sites_ = num_sites; 235 num_sites_ = num_sites;
226 236
237 if (ShouldShowPopularSites() &&
238 NeedPopularSites(profile_->GetPrefs(), num_sites_)) {
239 popular_sites_.reset(new PopularSites(
240 profile_,
241 GetPopularSitesFilename(),
242 profile_->GetRequestContext(),
243 base::Bind(&MostVisitedSites::OnPopularSitesAvailable,
244 base::Unretained(this))));
245 } else {
246 received_popular_sites_ = true;
247 }
248
227 QueryMostVisitedURLs(); 249 QueryMostVisitedURLs();
228 250
229 scoped_refptr<history::TopSites> top_sites = 251 scoped_refptr<history::TopSites> top_sites =
230 TopSitesFactory::GetForProfile(profile_); 252 TopSitesFactory::GetForProfile(profile_);
231 if (top_sites) { 253 if (top_sites) {
232 // TopSites updates itself after a delay. To ensure up-to-date results, 254 // TopSites updates itself after a delay. To ensure up-to-date results,
233 // force an update now. 255 // force an update now.
234 top_sites->SyncWithHistory(); 256 top_sites->SyncWithHistory();
235 257
236 // Register as TopSitesObserver so that we can update ourselves when the 258 // Register as TopSitesObserver so that we can update ourselves when the
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 } 727 }
706 } 728 }
707 729
708 static jlong Init(JNIEnv* env, 730 static jlong Init(JNIEnv* env,
709 const JavaParamRef<jobject>& obj, 731 const JavaParamRef<jobject>& obj,
710 const JavaParamRef<jobject>& jprofile) { 732 const JavaParamRef<jobject>& jprofile) {
711 MostVisitedSites* most_visited_sites = 733 MostVisitedSites* most_visited_sites =
712 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 734 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
713 return reinterpret_cast<intptr_t>(most_visited_sites); 735 return reinterpret_cast<intptr_t>(most_visited_sites);
714 } 736 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/android/popular_sites.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698