diff options
author | mathp@chromium.org <mathp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-03 21:38:46 +0000 |
---|---|---|
committer | mathp@chromium.org <mathp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-03 21:38:46 +0000 |
commit | d33adb40dd824bca0cd71320457d76b68be5223c (patch) | |
tree | 1499eeb2b512f3f068a1b4e30d64b42d1d6ef135 /chrome/browser/android | |
parent | 29a32976be5d2c24f2df0b737bbe33c2954e1e9d (diff) | |
download | chromium_src-d33adb40dd824bca0cd71320457d76b68be5223c.zip chromium_src-d33adb40dd824bca0cd71320457d76b68be5223c.tar.gz chromium_src-d33adb40dd824bca0cd71320457d76b68be5223c.tar.bz2 |
[NTP] Use server thumbnails from SuggestionsService on Android NTP.
No-op for non-server recommendations. Will prioritize local thumbnails.
BUG=None
TEST=Manual
Review URL: https://codereview.chromium.org/297233008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274637 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/android')
-rw-r--r-- | chrome/browser/android/most_visited_sites.cc | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/chrome/browser/android/most_visited_sites.cc b/chrome/browser/android/most_visited_sites.cc index 5ff3d30..955e410 100644 --- a/chrome/browser/android/most_visited_sites.cc +++ b/chrome/browser/android/most_visited_sites.cc @@ -11,6 +11,7 @@ #include "base/android/jni_array.h" #include "base/android/jni_string.h" #include "base/android/scoped_java_ref.h" +#include "base/callback.h" #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "chrome/browser/chrome_notification_types.h" @@ -84,10 +85,31 @@ void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, top_sites->AddForcedURL(url, base::Time::Now()); } +void OnSuggestionsThumbnailAvailable( + ScopedJavaGlobalRef<jobject>* j_callback, + const GURL& url, + const SkBitmap* bitmap) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + JNIEnv* env = AttachCurrentThread(); + + ScopedJavaGlobalRef<jobject>* j_bitmap_ref = + new ScopedJavaGlobalRef<jobject>(); + if (bitmap) { + j_bitmap_ref->Reset( + env, + gfx::ConvertToJavaBitmap(bitmap).obj()); + } + + Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( + env, j_callback->obj(), j_bitmap_ref->obj()); +} + +// Runs on the DB thread. void GetUrlThumbnailTask( std::string url_string, scoped_refptr<TopSites> top_sites, - ScopedJavaGlobalRef<jobject>* j_callback) { + ScopedJavaGlobalRef<jobject>* j_callback, + base::Closure lookup_failed_ui_callback) { JNIEnv* env = AttachCurrentThread(); ScopedJavaGlobalRef<jobject>* j_bitmap_ref = @@ -109,6 +131,13 @@ void GetUrlThumbnailTask( BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); + + // If appropriate, return on the UI thread to execute the proper callback. + if (!lookup_failed_ui_callback.is_null()) { + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, lookup_failed_ui_callback); + return; + } } // Since j_callback is owned by this callback, when the callback falls out of @@ -122,6 +151,16 @@ void GetUrlThumbnailTask( base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); } +void GetSuggestionsThumbnailOnUIThread( + SuggestionsService* suggestions_service, + const std::string& url_string, + ScopedJavaGlobalRef<jobject>* j_callback) { + suggestions_service->GetPageThumbnail( + GURL(url_string), + base::Bind(&OnSuggestionsThumbnailAvailable, + base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback)))); +} + } // namespace MostVisitedSites::MostVisitedSites(Profile* profile) @@ -162,22 +201,33 @@ void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, } } -// May be called from any thread +// Called from the UI Thread. void MostVisitedSites::GetURLThumbnail(JNIEnv* env, jobject obj, jstring url, jobject j_callback_obj) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); ScopedJavaGlobalRef<jobject>* j_callback = new ScopedJavaGlobalRef<jobject>(); j_callback->Reset(env, j_callback_obj); std::string url_string = ConvertJavaStringToUTF8(env, url); scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); + + // If the Suggestions service is enabled, create a callback to fetch a + // server thumbnail from it, in case the local thumbnail is not found. + SuggestionsService* suggestions_service = + SuggestionsServiceFactory::GetForProfile(profile_); + base::Closure lookup_failed_callback = suggestions_service ? + base::Bind(&GetSuggestionsThumbnailOnUIThread, + suggestions_service, url_string, + base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : + base::Closure(); BrowserThread::PostTask( - BrowserThread::DB, FROM_HERE, base::Bind( - &GetUrlThumbnailTask, - url_string, - top_sites, base::Owned(j_callback))); + BrowserThread::DB, FROM_HERE, + base::Bind( + &GetUrlThumbnailTask, url_string, top_sites, + base::Owned(j_callback), lookup_failed_callback)); } void MostVisitedSites::BlacklistUrl(JNIEnv* env, |