Commit d6866efd authored by gaschler's avatar gaschler Committed by Commit Bot

[NTP] SnippetsBridge fetchContextualSuggestions

fetchContextualSuggestions in SnippetsBridge fetches contextual
suggestions.
SnippetsBridge provides a new method fetchContextualSuggestionImage
to retrieve an image for a given contextual suggestion.
With this, the contextual feature is operational behind the
flag "contextual-suggestions-carousel".

Bug: 749988
Change-Id: Iee862d3d103ecc2c3e513e425ac9e0e9bd508093
Reviewed-on: https://chromium-review.googlesource.com/589148
Commit-Queue: Andre Gaschler <gaschler@chromium.org>
Reviewed-by: default avatarNicolas Dossou-Gbété <dgn@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491293}
parent c9b867ae
......@@ -133,6 +133,11 @@ public class SnippetArticle implements OfflinableSuggestion {
return mCategory == KnownCategories.ARTICLES;
}
/** @return whether a snippet is a contextual suggestion. */
public boolean isContextual() {
return mCategory == KnownCategories.CONTEXTUAL;
}
/** @return whether a snippet is either offline page or asset download. */
public boolean isDownload() {
return mCategory == KnownCategories.DOWNLOADS;
......
......@@ -158,6 +158,14 @@ public class SnippetsBridge implements SuggestionsSource {
nativeFetchContextualSuggestions(mNativeSnippetsBridge, url, callback);
}
@Override
public void fetchContextualSuggestionImage(
SnippetArticle suggestion, Callback<Bitmap> callback) {
assert mNativeSnippetsBridge != 0;
nativeFetchContextualSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory,
suggestion.mIdWithinCategory, callback);
}
@Override
public void dismissSuggestion(SnippetArticle suggestion) {
assert mNativeSnippetsBridge != 0;
......@@ -287,6 +295,8 @@ public class SnippetsBridge implements SuggestionsSource {
String[] knownSuggestions, Callback<List<SnippetArticle>> callback);
private native void nativeFetchContextualSuggestions(
long nativeNTPSnippetsBridge, String url, Callback<List<SnippetArticle>> callback);
private native void nativeFetchContextualSuggestionImage(long nativeNTPSnippetsBridge,
int category, String idWithinCategory, Callback<Bitmap> callback);
private native void nativeDismissSuggestion(long nativeNTPSnippetsBridge, String url,
int globalPosition, int category, int positionInCategory, String idWithinCategory);
private native void nativeDismissCategory(long nativeNTPSnippetsBridge, int category);
......
......@@ -112,6 +112,12 @@ public interface SuggestionsSource {
*/
void fetchContextualSuggestions(String url, Callback<List<SnippetArticle>> callback);
/**
* Fetches the thumbnail image for a contextual suggestion. A {@code null} Bitmap is returned if
* no image is available.
*/
void fetchContextualSuggestionImage(SnippetArticle suggestion, Callback<Bitmap> callback);
/**
* Tells the source to dismiss the content suggestion.
*/
......
......@@ -98,8 +98,12 @@ public class ImageFetcher {
public void makeArticleThumbnailRequest(SnippetArticle suggestion, Callback<Bitmap> callback) {
assert !mIsDestroyed;
if (suggestion.isContextual()) {
mSuggestionsSource.fetchContextualSuggestionImage(suggestion, callback);
} else {
mSuggestionsSource.fetchSuggestionImage(suggestion, callback);
}
}
/**
* Creates a request for favicon for the URL of a suggestion.
......
......@@ -337,24 +337,28 @@ void NTPSnippetsBridge::FetchContextualSuggestions(
const JavaParamRef<jobject>& j_callback) {
DCHECK(base::FeatureList::IsEnabled(
chrome::android::kContextualSuggestionsCarousel));
GURL url(ConvertJavaStringToUTF8(env, j_url));
content_suggestions_service_->contextual_suggestions_source()
->FetchContextualSuggestions(
url, base::Bind(&NTPSnippetsBridge::OnContextualSuggestionsFetched,
weak_ptr_factory_.GetWeakPtr(),
ScopedJavaGlobalRef<jobject>(j_callback)));
}
// We don't currently have a contextual suggestions service or provider, so
// we use articles as placeholders.
Category category = Category::FromKnownCategory(KnownCategories::ARTICLES);
auto suggestions = ToJavaSuggestionList(
env, category,
content_suggestions_service_->GetSuggestionsForCategory(category));
// We would eventually have to hit the network or a database, so let's
// pretend here the call is asynchronous.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(
[](const base::android::JavaRef<jobject>& j_callback,
const base::android::JavaRef<jobject>& j_suggestions) {
RunCallbackAndroid(j_callback, j_suggestions);
},
ScopedJavaGlobalRef<jobject>(j_callback),
ScopedJavaGlobalRef<jobject>(suggestions)));
void NTPSnippetsBridge::FetchContextualSuggestionImage(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint j_category_id,
const JavaParamRef<jstring>& id_within_category,
const JavaParamRef<jobject>& j_callback) {
ScopedJavaGlobalRef<jobject> callback(j_callback);
content_suggestions_service_->contextual_suggestions_source()
->FetchContextualSuggestionImage(
ContentSuggestion::ID(
Category::FromIDValue(j_category_id),
ConvertJavaStringToUTF8(env, id_within_category)),
base::Bind(&NTPSnippetsBridge::OnImageFetched,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void NTPSnippetsBridge::ReloadSuggestions(JNIEnv* env,
......@@ -458,3 +462,15 @@ void NTPSnippetsBridge::OnSuggestionsFetched(
RunCallbackAndroid(callback,
ToJavaSuggestionList(env, category, suggestions));
}
void NTPSnippetsBridge::OnContextualSuggestionsFetched(
ScopedJavaGlobalRef<jobject> j_callback,
ntp_snippets::Status status,
const GURL& url,
std::vector<ContentSuggestion> suggestions) {
JNIEnv* env = AttachCurrentThread();
auto j_suggestions = ToJavaSuggestionList(
env, Category::FromKnownCategory(KnownCategories::CONTEXTUAL),
suggestions);
RunCallbackAndroid(j_callback, j_suggestions);
}
......@@ -84,6 +84,13 @@ class NTPSnippetsBridge
const base::android::JavaParamRef<jstring>& j_url,
const base::android::JavaParamRef<jobject>& j_callback);
void FetchContextualSuggestionImage(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
jint j_category_id,
const base::android::JavaParamRef<jstring>& id_within_category,
const base::android::JavaParamRef<jobject>& j_callback);
void ReloadSuggestions(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj);
......@@ -125,6 +132,12 @@ class NTPSnippetsBridge
ntp_snippets::Status status,
std::vector<ntp_snippets::ContentSuggestion> suggestions);
void OnContextualSuggestionsFetched(
base::android::ScopedJavaGlobalRef<jobject> j_callback,
ntp_snippets::Status status,
const GURL& url,
std::vector<ntp_snippets::ContentSuggestion> suggestions);
ntp_snippets::ContentSuggestionsService* content_suggestions_service_;
history::HistoryService* history_service_;
base::CancelableTaskTracker tracker_;
......
......@@ -243,6 +243,12 @@ public class FakeSuggestionsSource implements SuggestionsSource {
throw new UnsupportedOperationException();
}
@Override
public void fetchContextualSuggestionImage(
SnippetArticle suggestion, Callback<Bitmap> callback) {
throw new UnsupportedOperationException();
}
@Override
public void addObserver(Observer observer) {
mObserverList.addObserver(observer);
......
......@@ -280,6 +280,11 @@ class ContentSuggestionsService : public KeyedService,
CategoryRanker* category_ranker() { return category_ranker_.get(); }
ContextualSuggestionsSource* contextual_suggestions_source() {
DCHECK(contextual_suggestions_source_);
return contextual_suggestions_source_.get();
}
private:
friend class ContentSuggestionsServiceTest;
......
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