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 { ...@@ -133,6 +133,11 @@ public class SnippetArticle implements OfflinableSuggestion {
return mCategory == KnownCategories.ARTICLES; 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. */ /** @return whether a snippet is either offline page or asset download. */
public boolean isDownload() { public boolean isDownload() {
return mCategory == KnownCategories.DOWNLOADS; return mCategory == KnownCategories.DOWNLOADS;
......
...@@ -158,6 +158,14 @@ public class SnippetsBridge implements SuggestionsSource { ...@@ -158,6 +158,14 @@ public class SnippetsBridge implements SuggestionsSource {
nativeFetchContextualSuggestions(mNativeSnippetsBridge, url, callback); nativeFetchContextualSuggestions(mNativeSnippetsBridge, url, callback);
} }
@Override
public void fetchContextualSuggestionImage(
SnippetArticle suggestion, Callback<Bitmap> callback) {
assert mNativeSnippetsBridge != 0;
nativeFetchContextualSuggestionImage(mNativeSnippetsBridge, suggestion.mCategory,
suggestion.mIdWithinCategory, callback);
}
@Override @Override
public void dismissSuggestion(SnippetArticle suggestion) { public void dismissSuggestion(SnippetArticle suggestion) {
assert mNativeSnippetsBridge != 0; assert mNativeSnippetsBridge != 0;
...@@ -287,6 +295,8 @@ public class SnippetsBridge implements SuggestionsSource { ...@@ -287,6 +295,8 @@ public class SnippetsBridge implements SuggestionsSource {
String[] knownSuggestions, Callback<List<SnippetArticle>> callback); String[] knownSuggestions, Callback<List<SnippetArticle>> callback);
private native void nativeFetchContextualSuggestions( private native void nativeFetchContextualSuggestions(
long nativeNTPSnippetsBridge, String url, Callback<List<SnippetArticle>> callback); 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, private native void nativeDismissSuggestion(long nativeNTPSnippetsBridge, String url,
int globalPosition, int category, int positionInCategory, String idWithinCategory); int globalPosition, int category, int positionInCategory, String idWithinCategory);
private native void nativeDismissCategory(long nativeNTPSnippetsBridge, int category); private native void nativeDismissCategory(long nativeNTPSnippetsBridge, int category);
......
...@@ -112,6 +112,12 @@ public interface SuggestionsSource { ...@@ -112,6 +112,12 @@ public interface SuggestionsSource {
*/ */
void fetchContextualSuggestions(String url, Callback<List<SnippetArticle>> callback); 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. * Tells the source to dismiss the content suggestion.
*/ */
......
...@@ -98,7 +98,11 @@ public class ImageFetcher { ...@@ -98,7 +98,11 @@ public class ImageFetcher {
public void makeArticleThumbnailRequest(SnippetArticle suggestion, Callback<Bitmap> callback) { public void makeArticleThumbnailRequest(SnippetArticle suggestion, Callback<Bitmap> callback) {
assert !mIsDestroyed; assert !mIsDestroyed;
mSuggestionsSource.fetchSuggestionImage(suggestion, callback); if (suggestion.isContextual()) {
mSuggestionsSource.fetchContextualSuggestionImage(suggestion, callback);
} else {
mSuggestionsSource.fetchSuggestionImage(suggestion, callback);
}
} }
/** /**
......
...@@ -337,24 +337,28 @@ void NTPSnippetsBridge::FetchContextualSuggestions( ...@@ -337,24 +337,28 @@ void NTPSnippetsBridge::FetchContextualSuggestions(
const JavaParamRef<jobject>& j_callback) { const JavaParamRef<jobject>& j_callback) {
DCHECK(base::FeatureList::IsEnabled( DCHECK(base::FeatureList::IsEnabled(
chrome::android::kContextualSuggestionsCarousel)); 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 void NTPSnippetsBridge::FetchContextualSuggestionImage(
// we use articles as placeholders. JNIEnv* env,
Category category = Category::FromKnownCategory(KnownCategories::ARTICLES); const JavaParamRef<jobject>& obj,
auto suggestions = ToJavaSuggestionList( jint j_category_id,
env, category, const JavaParamRef<jstring>& id_within_category,
content_suggestions_service_->GetSuggestionsForCategory(category)); const JavaParamRef<jobject>& j_callback) {
ScopedJavaGlobalRef<jobject> callback(j_callback);
// We would eventually have to hit the network or a database, so let's content_suggestions_service_->contextual_suggestions_source()
// pretend here the call is asynchronous. ->FetchContextualSuggestionImage(
base::ThreadTaskRunnerHandle::Get()->PostTask( ContentSuggestion::ID(
FROM_HERE, base::BindOnce( Category::FromIDValue(j_category_id),
[](const base::android::JavaRef<jobject>& j_callback, ConvertJavaStringToUTF8(env, id_within_category)),
const base::android::JavaRef<jobject>& j_suggestions) { base::Bind(&NTPSnippetsBridge::OnImageFetched,
RunCallbackAndroid(j_callback, j_suggestions); weak_ptr_factory_.GetWeakPtr(), callback));
},
ScopedJavaGlobalRef<jobject>(j_callback),
ScopedJavaGlobalRef<jobject>(suggestions)));
} }
void NTPSnippetsBridge::ReloadSuggestions(JNIEnv* env, void NTPSnippetsBridge::ReloadSuggestions(JNIEnv* env,
...@@ -458,3 +462,15 @@ void NTPSnippetsBridge::OnSuggestionsFetched( ...@@ -458,3 +462,15 @@ void NTPSnippetsBridge::OnSuggestionsFetched(
RunCallbackAndroid(callback, RunCallbackAndroid(callback,
ToJavaSuggestionList(env, category, suggestions)); 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 ...@@ -84,6 +84,13 @@ class NTPSnippetsBridge
const base::android::JavaParamRef<jstring>& j_url, const base::android::JavaParamRef<jstring>& j_url,
const base::android::JavaParamRef<jobject>& j_callback); 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, void ReloadSuggestions(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj); const base::android::JavaParamRef<jobject>& obj);
...@@ -125,6 +132,12 @@ class NTPSnippetsBridge ...@@ -125,6 +132,12 @@ class NTPSnippetsBridge
ntp_snippets::Status status, ntp_snippets::Status status,
std::vector<ntp_snippets::ContentSuggestion> suggestions); 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_; ntp_snippets::ContentSuggestionsService* content_suggestions_service_;
history::HistoryService* history_service_; history::HistoryService* history_service_;
base::CancelableTaskTracker tracker_; base::CancelableTaskTracker tracker_;
......
...@@ -243,6 +243,12 @@ public class FakeSuggestionsSource implements SuggestionsSource { ...@@ -243,6 +243,12 @@ public class FakeSuggestionsSource implements SuggestionsSource {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void fetchContextualSuggestionImage(
SnippetArticle suggestion, Callback<Bitmap> callback) {
throw new UnsupportedOperationException();
}
@Override @Override
public void addObserver(Observer observer) { public void addObserver(Observer observer) {
mObserverList.addObserver(observer); mObserverList.addObserver(observer);
......
...@@ -280,6 +280,11 @@ class ContentSuggestionsService : public KeyedService, ...@@ -280,6 +280,11 @@ class ContentSuggestionsService : public KeyedService,
CategoryRanker* category_ranker() { return category_ranker_.get(); } CategoryRanker* category_ranker() { return category_ranker_.get(); }
ContextualSuggestionsSource* contextual_suggestions_source() {
DCHECK(contextual_suggestions_source_);
return contextual_suggestions_source_.get();
}
private: private:
friend class ContentSuggestionsServiceTest; 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