Commit d33adb40 authored by mathp@chromium.org's avatar mathp@chromium.org

[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
parent 29a32976
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "base/android/jni_array.h" #include "base/android/jni_array.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
#include "base/callback.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/chrome_notification_types.h"
...@@ -84,10 +85,31 @@ void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, ...@@ -84,10 +85,31 @@ void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites,
top_sites->AddForcedURL(url, base::Time::Now()); 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( void GetUrlThumbnailTask(
std::string url_string, std::string url_string,
scoped_refptr<TopSites> top_sites, scoped_refptr<TopSites> top_sites,
ScopedJavaGlobalRef<jobject>* j_callback) { ScopedJavaGlobalRef<jobject>* j_callback,
base::Closure lookup_failed_ui_callback) {
JNIEnv* env = AttachCurrentThread(); JNIEnv* env = AttachCurrentThread();
ScopedJavaGlobalRef<jobject>* j_bitmap_ref = ScopedJavaGlobalRef<jobject>* j_bitmap_ref =
...@@ -109,6 +131,13 @@ void GetUrlThumbnailTask( ...@@ -109,6 +131,13 @@ void GetUrlThumbnailTask(
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); 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 // Since j_callback is owned by this callback, when the callback falls out of
...@@ -122,6 +151,16 @@ void GetUrlThumbnailTask( ...@@ -122,6 +151,16 @@ void GetUrlThumbnailTask(
base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); 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 } // namespace
MostVisitedSites::MostVisitedSites(Profile* profile) MostVisitedSites::MostVisitedSites(Profile* profile)
...@@ -162,22 +201,33 @@ void MostVisitedSites::SetMostVisitedURLsObserver(JNIEnv* env, ...@@ -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, void MostVisitedSites::GetURLThumbnail(JNIEnv* env,
jobject obj, jobject obj,
jstring url, jstring url,
jobject j_callback_obj) { jobject j_callback_obj) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
ScopedJavaGlobalRef<jobject>* j_callback = ScopedJavaGlobalRef<jobject>* j_callback =
new ScopedJavaGlobalRef<jobject>(); new ScopedJavaGlobalRef<jobject>();
j_callback->Reset(env, j_callback_obj); j_callback->Reset(env, j_callback_obj);
std::string url_string = ConvertJavaStringToUTF8(env, url); std::string url_string = ConvertJavaStringToUTF8(env, url);
scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); 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::PostTask(
BrowserThread::DB, FROM_HERE, base::Bind( BrowserThread::DB, FROM_HERE,
&GetUrlThumbnailTask, base::Bind(
url_string, &GetUrlThumbnailTask, url_string, top_sites,
top_sites, base::Owned(j_callback))); base::Owned(j_callback), lookup_failed_callback));
} }
void MostVisitedSites::BlacklistUrl(JNIEnv* env, void MostVisitedSites::BlacklistUrl(JNIEnv* env,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment