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

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

Issue 815983002: Topsites become keyedService based. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 5 years, 11 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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
11 #include "base/android/jni_array.h" 11 #include "base/android/jni_array.h"
12 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
13 #include "base/android/scoped_java_ref.h" 13 #include "base/android/scoped_java_ref.h"
14 #include "base/callback.h" 14 #include "base/callback.h"
15 #include "base/metrics/histogram.h" 15 #include "base/metrics/histogram.h"
16 #include "base/metrics/sparse_histogram.h" 16 #include "base/metrics/sparse_histogram.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "chrome/browser/chrome_notification_types.h" 21 #include "chrome/browser/chrome_notification_types.h"
22 #include "chrome/browser/history/top_sites.h" 22 #include "chrome/browser/history/top_sites.h"
23 #include "chrome/browser/history/top_sites_service_factory.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_android.h" 25 #include "chrome/browser/profiles/profile_android.h"
25 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" 26 #include "chrome/browser/search/suggestions/suggestions_service_factory.h"
26 #include "chrome/browser/search/suggestions/suggestions_source.h" 27 #include "chrome/browser/search/suggestions/suggestions_source.h"
27 #include "chrome/browser/sync/profile_sync_service.h" 28 #include "chrome/browser/sync/profile_sync_service.h"
28 #include "chrome/browser/sync/profile_sync_service_factory.h" 29 #include "chrome/browser/sync/profile_sync_service_factory.h"
29 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 30 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
30 #include "components/suggestions/suggestions_service.h" 31 #include "components/suggestions/suggestions_service.h"
31 #include "components/suggestions/suggestions_utils.h" 32 #include "components/suggestions/suggestions_utils.h"
32 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 226
226 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, 227 void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env,
227 jobject obj, 228 jobject obj,
228 jobject j_observer, 229 jobject j_observer,
229 jint num_sites) { 230 jint num_sites) {
230 observer_.Reset(env, j_observer); 231 observer_.Reset(env, j_observer);
231 num_sites_ = num_sites; 232 num_sites_ = num_sites;
232 233
233 QueryMostVisitedURLs(); 234 QueryMostVisitedURLs();
234 235
235 history::TopSites* top_sites = profile_->GetTopSites(); 236 scoped_refptr<history::TopSites> top_sites =
236 if (top_sites) { 237 TopSitesServiceFactory::GetForProfile(profile_);
238 if (top_sites.get()) {
237 // TopSites updates itself after a delay. To ensure up-to-date results, 239 // TopSites updates itself after a delay. To ensure up-to-date results,
238 // force an update now. 240 // force an update now.
239 top_sites->SyncWithHistory(); 241 top_sites->SyncWithHistory();
240 242
241 // Register for notification when TopSites changes so that we can update 243 // Register for notification when TopSites changes so that we can update
242 // ourself. 244 // ourself.
243 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, 245 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
244 content::Source<history::TopSites>(top_sites)); 246 content::Source<history::TopSites>(top_sites.get()));
245 } 247 }
246 } 248 }
247 249
248 // Called from the UI Thread. 250 // Called from the UI Thread.
249 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, 251 void MostVisitedSites::GetURLThumbnail(JNIEnv* env,
250 jobject obj, 252 jobject obj,
251 jstring url, 253 jstring url,
252 jobject j_callback_obj) { 254 jobject j_callback_obj) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
254 ScopedJavaGlobalRef<jobject>* j_callback = 256 ScopedJavaGlobalRef<jobject>* j_callback =
255 new ScopedJavaGlobalRef<jobject>(); 257 new ScopedJavaGlobalRef<jobject>();
256 j_callback->Reset(env, j_callback_obj); 258 j_callback->Reset(env, j_callback_obj);
257 259
258 std::string url_string = ConvertJavaStringToUTF8(env, url); 260 std::string url_string = ConvertJavaStringToUTF8(env, url);
259 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); 261 scoped_refptr<TopSites> top_sites(
262 TopSitesServiceFactory::GetForProfile(profile_));
260 263
261 // If the Suggestions service is enabled and in use, create a callback to 264 // If the Suggestions service is enabled and in use, create a callback to
262 // fetch a server thumbnail from it, in case the local thumbnail is not found. 265 // fetch a server thumbnail from it, in case the local thumbnail is not found.
263 SuggestionsService* suggestions_service = 266 SuggestionsService* suggestions_service =
264 SuggestionsServiceFactory::GetForProfile(profile_); 267 SuggestionsServiceFactory::GetForProfile(profile_);
265 bool use_suggestions_service = suggestions_service && 268 bool use_suggestions_service = suggestions_service &&
266 mv_source_ == SUGGESTIONS_SERVICE; 269 mv_source_ == SUGGESTIONS_SERVICE;
267 base::Closure lookup_failed_callback = use_suggestions_service ? 270 base::Closure lookup_failed_callback = use_suggestions_service ?
268 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread, 271 base::Bind(&MostVisitedSites::GetSuggestionsThumbnailOnUIThread,
269 weak_ptr_factory_.GetWeakPtr(), 272 weak_ptr_factory_.GetWeakPtr(),
270 suggestions_service, url_string, 273 suggestions_service, url_string,
271 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : 274 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) :
272 base::Closure(); 275 base::Closure();
273 LookupSuccessCallback lookup_success_callback = 276 LookupSuccessCallback lookup_success_callback =
274 base::Bind(&MostVisitedSites::OnObtainedThumbnail, 277 base::Bind(&MostVisitedSites::OnObtainedThumbnail,
275 weak_ptr_factory_.GetWeakPtr()); 278 weak_ptr_factory_.GetWeakPtr());
276 279
277 BrowserThread::PostTask( 280 BrowserThread::PostTask(
278 BrowserThread::DB, FROM_HERE, 281 BrowserThread::DB, FROM_HERE,
279 base::Bind( 282 base::Bind(&GetUrlThumbnailTask, url_string, top_sites,
280 &GetUrlThumbnailTask, url_string, top_sites, 283 base::Owned(j_callback), lookup_success_callback,
281 base::Owned(j_callback), lookup_success_callback, 284 lookup_failed_callback));
282 lookup_failed_callback));
283 } 285 }
284 286
285 void MostVisitedSites::BlacklistUrl(JNIEnv* env, 287 void MostVisitedSites::BlacklistUrl(JNIEnv* env,
286 jobject obj, 288 jobject obj,
287 jstring j_url) { 289 jstring j_url) {
288 std::string url = ConvertJavaStringToUTF8(env, j_url); 290 std::string url = ConvertJavaStringToUTF8(env, j_url);
289 291
290 switch (mv_source_) { 292 switch (mv_source_) {
291 case TOP_SITES: { 293 case TOP_SITES: {
292 TopSites* top_sites = profile_->GetTopSites(); 294 scoped_refptr<TopSites> top_sites =
293 DCHECK(top_sites); 295 TopSitesServiceFactory::GetForProfile(profile_);
296 DCHECK(top_sites.get());
294 top_sites->AddBlacklistedURL(GURL(url)); 297 top_sites->AddBlacklistedURL(GURL(url));
295 break; 298 break;
296 } 299 }
297 300
298 case SUGGESTIONS_SERVICE: { 301 case SUGGESTIONS_SERVICE: {
299 SuggestionsService* suggestions_service = 302 SuggestionsService* suggestions_service =
300 SuggestionsServiceFactory::GetForProfile(profile_); 303 SuggestionsServiceFactory::GetForProfile(profile_);
301 DCHECK(suggestions_service); 304 DCHECK(suggestions_service);
302 suggestions_service->BlacklistURL( 305 suggestions_service->BlacklistURL(
303 GURL(url), 306 GURL(url),
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 base::Bind( 373 base::Bind(
371 &MostVisitedSites::OnSuggestionsProfileAvailable, 374 &MostVisitedSites::OnSuggestionsProfileAvailable,
372 weak_ptr_factory_.GetWeakPtr(), 375 weak_ptr_factory_.GetWeakPtr(),
373 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); 376 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_))));
374 } else { 377 } else {
375 InitiateTopSitesQuery(); 378 InitiateTopSitesQuery();
376 } 379 }
377 } 380 }
378 381
379 void MostVisitedSites::InitiateTopSitesQuery() { 382 void MostVisitedSites::InitiateTopSitesQuery() {
380 TopSites* top_sites = profile_->GetTopSites(); 383 scoped_refptr<TopSites> top_sites =
381 if (!top_sites) 384 TopSitesServiceFactory::GetForProfile(profile_);
385 if (top_sites.get() == NULL)
sdefresne 2014/12/29 09:48:14 nit: "if (!top_sites)" should work here and everyw
Jitu( very slow this week) 2014/12/30 10:09:02 Done.
382 return; 386 return;
383 387
384 top_sites->GetMostVisitedURLs( 388 top_sites->GetMostVisitedURLs(
385 base::Bind( 389 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable,
386 &MostVisitedSites::OnMostVisitedURLsAvailable, 390 weak_ptr_factory_.GetWeakPtr(),
387 weak_ptr_factory_.GetWeakPtr(), 391 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)),
388 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), 392 num_sites_),
389 num_sites_),
390 false); 393 false);
391 } 394 }
392 395
393 void MostVisitedSites::OnMostVisitedURLsAvailable( 396 void MostVisitedSites::OnMostVisitedURLsAvailable(
394 ScopedJavaGlobalRef<jobject>* j_observer, 397 ScopedJavaGlobalRef<jobject>* j_observer,
395 int num_sites, 398 int num_sites,
396 const history::MostVisitedURLList& visited_list) { 399 const history::MostVisitedURLList& visited_list) {
397 std::vector<base::string16> titles; 400 std::vector<base::string16> titles;
398 std::vector<std::string> urls; 401 std::vector<std::string> urls;
399 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); 402 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites);
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 num_empty_thumbs_ = 0; 530 num_empty_thumbs_ = 0;
528 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); 531 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_);
529 num_server_thumbs_ = 0; 532 num_server_thumbs_ = 0;
530 } 533 }
531 534
532 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { 535 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) {
533 MostVisitedSites* most_visited_sites = 536 MostVisitedSites* most_visited_sites =
534 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 537 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
535 return reinterpret_cast<intptr_t>(most_visited_sites); 538 return reinterpret_cast<intptr_t>(most_visited_sites);
536 } 539 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698