Commit 62502ea5 authored by Filip Gorski's avatar Filip Gorski Committed by Commit Bot

[EoC] Wiring peek_text and contextual suggestions to the new bridge

* This patch updates the bridge to use contextual suggestions object
  directly
* It also wires peek_text to the bridge.

Bug: 824182, 827656
Change-Id: Iae3f3cca90bf2d0fe97953d906e270438e1f203c
Reviewed-on: https://chromium-review.googlesource.com/994456Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Commit-Queue: Filip Gorski <fgorski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#548172}
parent 330797ce
...@@ -24,6 +24,26 @@ import java.util.List; ...@@ -24,6 +24,26 @@ import java.util.List;
public class ContextualSuggestionsBridge { public class ContextualSuggestionsBridge {
private long mNativeContextualSuggestionsBridge; private long mNativeContextualSuggestionsBridge;
/** Result of fetching contextual suggestions. */
public static class ContextualSuggestionsResult {
private String mPeekText;
private List<ContextualSuggestionsCluster> mClusters = new ArrayList<>();
ContextualSuggestionsResult(String peekText) {
mPeekText = peekText;
}
/** Peek text to show in UI. */
public String getPeekText() {
return mPeekText;
}
/** Clusters of suggestions. */
public List<ContextualSuggestionsCluster> getClusters() {
return mClusters;
}
}
/** /**
* Creates a ContextualSuggestionsBridge for getting snippet data for the current user. * Creates a ContextualSuggestionsBridge for getting snippet data for the current user.
* *
...@@ -45,8 +65,7 @@ public class ContextualSuggestionsBridge { ...@@ -45,8 +65,7 @@ public class ContextualSuggestionsBridge {
* @param url URL for which to fetch suggestions. * @param url URL for which to fetch suggestions.
* @param callback Callback used to return suggestions for a given URL. * @param callback Callback used to return suggestions for a given URL.
*/ */
public void fetchSuggestions( public void fetchSuggestions(String url, Callback<ContextualSuggestionsResult> callback) {
String url, Callback<List<ContextualSuggestionsCluster>> callback) {
assert mNativeContextualSuggestionsBridge != 0; assert mNativeContextualSuggestionsBridge != 0;
nativeFetchSuggestions(mNativeContextualSuggestionsBridge, url, callback); nativeFetchSuggestions(mNativeContextualSuggestionsBridge, url, callback);
} }
...@@ -85,32 +104,30 @@ public class ContextualSuggestionsBridge { ...@@ -85,32 +104,30 @@ public class ContextualSuggestionsBridge {
} }
@CalledByNative @CalledByNative
private static List<ContextualSuggestionsCluster> createContextualSuggestionsClusterList() { private static ContextualSuggestionsResult createContextualSuggestionsResult(String peekText) {
return new ArrayList<>(); return new ContextualSuggestionsResult(peekText);
} }
@CalledByNative @CalledByNative
private static void addNewClusterToList( private static void addNewClusterToResult(ContextualSuggestionsResult result, String title) {
List<ContextualSuggestionsCluster> clusters, String title) { result.getClusters().add(new ContextualSuggestionsCluster(title));
clusters.add(new ContextualSuggestionsCluster(title));
} }
@CalledByNative @CalledByNative
private static void addSuggestionToLastCluster(List<ContextualSuggestionsCluster> clusters, private static void addSuggestionToLastCluster(List<ContextualSuggestionsCluster> clusters,
String id, String title, String publisher, String url, long timestamp, float score, String id, String title, String publisher, String url) {
long fetchTime, boolean isVideoSuggestion, int thumbnailDominantColor) {
assert clusters.size() > 0; assert clusters.size() > 0;
clusters.get(clusters.size() - 1) clusters.get(clusters.size() - 1)
.getSuggestions() .getSuggestions()
.add(new SnippetArticle(KnownCategories.CONTEXTUAL, id, title, publisher, url, .add(new SnippetArticle(KnownCategories.CONTEXTUAL, id, title, publisher, url,
timestamp, score, fetchTime, isVideoSuggestion, /*publishTimestamp=*/0, /*score=*/0f, /*fetchTimestamp=*/0,
thumbnailDominantColor == 0 ? null : thumbnailDominantColor)); /*isVideoSuggestion=*/false, /*thumbnailDominantColor=*/0));
} }
private native long nativeInit(Profile profile); private native long nativeInit(Profile profile);
private native void nativeDestroy(long nativeContextualSuggestionsBridge); private native void nativeDestroy(long nativeContextualSuggestionsBridge);
private native void nativeFetchSuggestions(long nativeContextualSuggestionsBridge, String url, private native void nativeFetchSuggestions(long nativeContextualSuggestionsBridge, String url,
Callback<List<ContextualSuggestionsCluster>> callback); Callback<ContextualSuggestionsResult> callback);
private native void nativeFetchSuggestionImage( private native void nativeFetchSuggestionImage(
long nativeContextualSuggestionsBridge, String suggestionId, Callback<Bitmap> callback); long nativeContextualSuggestionsBridge, String suggestionId, Callback<Bitmap> callback);
private native void nativeFetchSuggestionFavicon( private native void nativeFetchSuggestionFavicon(
......
...@@ -79,7 +79,7 @@ void ContextualSuggestionsBridge::FetchSuggestions( ...@@ -79,7 +79,7 @@ void ContextualSuggestionsBridge::FetchSuggestions(
const JavaParamRef<jobject>& obj, const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& j_url, const JavaParamRef<jstring>& j_url,
const JavaParamRef<jobject>& j_callback) { const JavaParamRef<jobject>& j_callback) {
if (contextual_content_suggestions_service_ == nullptr) if (!contextual_content_suggestions_service_)
return; return;
GURL url(ConvertJavaStringToUTF8(env, j_url)); GURL url(ConvertJavaStringToUTF8(env, j_url));
...@@ -94,7 +94,7 @@ void ContextualSuggestionsBridge::FetchSuggestionImage( ...@@ -94,7 +94,7 @@ void ContextualSuggestionsBridge::FetchSuggestionImage(
const JavaParamRef<jobject>& obj, const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& j_suggestion_id, const JavaParamRef<jstring>& j_suggestion_id,
const JavaParamRef<jobject>& j_callback) { const JavaParamRef<jobject>& j_callback) {
if (contextual_content_suggestions_service_ == nullptr) if (!contextual_content_suggestions_service_)
return; return;
contextual_content_suggestions_service_->FetchContextualSuggestionImage( contextual_content_suggestions_service_->FetchContextualSuggestionImage(
...@@ -144,28 +144,24 @@ void ContextualSuggestionsBridge::ReportEvent( ...@@ -144,28 +144,24 @@ void ContextualSuggestionsBridge::ReportEvent(
void ContextualSuggestionsBridge::OnSuggestionsAvailable( void ContextualSuggestionsBridge::OnSuggestionsAvailable(
ScopedJavaGlobalRef<jobject> j_callback, ScopedJavaGlobalRef<jobject> j_callback,
std::string peek_text,
std::vector<Cluster> clusters) { std::vector<Cluster> clusters) {
JNIEnv* env = AttachCurrentThread(); JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_clusters = ScopedJavaLocalRef<jobject> j_result =
Java_ContextualSuggestionsBridge_createContextualSuggestionsClusterList( Java_ContextualSuggestionsBridge_createContextualSuggestionsResult(
env); env, ConvertUTF8ToJavaString(env, peek_text));
for (auto& cluster : clusters) { for (auto& cluster : clusters) {
Java_ContextualSuggestionsBridge_addNewClusterToList( Java_ContextualSuggestionsBridge_addNewClusterToResult(
env, j_clusters, ConvertUTF8ToJavaString(env, cluster.title)); env, j_result, ConvertUTF8ToJavaString(env, cluster.title));
for (auto& suggestion : cluster.suggestions) { for (auto& suggestion : cluster.suggestions) {
Java_ContextualSuggestionsBridge_addSuggestionToLastCluster( Java_ContextualSuggestionsBridge_addSuggestionToLastCluster(
env, j_clusters, env, j_result, ConvertUTF8ToJavaString(env, suggestion->id()),
ConvertUTF8ToJavaString(env, suggestion.id().id_within_category()), ConvertUTF8ToJavaString(env, suggestion->title()),
ConvertUTF16ToJavaString(env, suggestion.title()), ConvertUTF8ToJavaString(env, suggestion->publisher_name()),
ConvertUTF16ToJavaString(env, suggestion.publisher_name()), ConvertUTF8ToJavaString(env, suggestion->url().spec()));
ConvertUTF8ToJavaString(env, suggestion.url().spec()),
suggestion.publish_date().ToJavaTime(), suggestion.score(),
suggestion.fetch_date().ToJavaTime(),
suggestion.is_video_suggestion(),
suggestion.optional_image_dominant_color().value_or(0));
} }
} }
RunCallbackAndroid(j_callback, j_clusters); RunCallbackAndroid(j_callback, j_result);
} }
void ContextualSuggestionsBridge::OnImageFetched( void ContextualSuggestionsBridge::OnImageFetched(
......
...@@ -66,6 +66,7 @@ class ContextualSuggestionsBridge { ...@@ -66,6 +66,7 @@ class ContextualSuggestionsBridge {
void OnSuggestionsAvailable( void OnSuggestionsAvailable(
base::android::ScopedJavaGlobalRef<jobject> j_callback, base::android::ScopedJavaGlobalRef<jobject> j_callback,
std::string peek_text,
std::vector<ntp_snippets::ContextualContentSuggestionsService::Cluster> std::vector<ntp_snippets::ContextualContentSuggestionsService::Cluster>
clusters); clusters);
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
namespace ntp_snippets { namespace ntp_snippets {
namespace {
static const char kSamplePeekText[] = "Peek text";
static const char kSampleClusterTitle[] = "Cluster title filler";
} // namespace
ContextualContentSuggestionsService::Cluster::Cluster() = default; ContextualContentSuggestionsService::Cluster::Cluster() = default;
...@@ -106,17 +110,12 @@ void ContextualContentSuggestionsService:: ...@@ -106,17 +110,12 @@ void ContextualContentSuggestionsService::
ContextualSuggestionsFetcher::OptionalSuggestions fetched_suggestions) { ContextualSuggestionsFetcher::OptionalSuggestions fetched_suggestions) {
std::vector<Cluster> clusters; std::vector<Cluster> clusters;
if (fetched_suggestions.has_value()) { if (fetched_suggestions.has_value()) {
clusters.emplace_back(); Cluster cluster;
Cluster& cluster = clusters.back(); cluster.title = kSampleClusterTitle;
for (const std::unique_ptr<ContextualSuggestion>& suggestion : cluster.suggestions = std::move(fetched_suggestions.value());
fetched_suggestions.value()) { clusters.push_back(std::move(cluster));
cluster.suggestions.emplace_back(suggestion->ToContentSuggestion());
ContentSuggestion::ID id = cluster.suggestions.back().id();
GURL image_url = suggestion->salient_image_url();
image_url_by_id_[id.id_within_category()] = image_url;
}
} }
std::move(callback).Run(std::move(clusters)); std::move(callback).Run(kSamplePeekText, std::move(clusters));
} }
} // namespace ntp_snippets } // namespace ntp_snippets
...@@ -36,7 +36,7 @@ class ContextualContentSuggestionsService : public KeyedService { ...@@ -36,7 +36,7 @@ class ContextualContentSuggestionsService : public KeyedService {
~Cluster(); ~Cluster();
std::string title; std::string title;
std::vector<ContentSuggestion> suggestions; ContextualSuggestion::PtrVector suggestions;
private: private:
DISALLOW_COPY_AND_ASSIGN(Cluster); DISALLOW_COPY_AND_ASSIGN(Cluster);
...@@ -56,7 +56,8 @@ class ContextualContentSuggestionsService : public KeyedService { ...@@ -56,7 +56,8 @@ class ContextualContentSuggestionsService : public KeyedService {
std::vector<ContentSuggestion> suggestions)>; std::vector<ContentSuggestion> suggestions)>;
using FetchContextualSuggestionClustersCallback = using FetchContextualSuggestionClustersCallback =
base::OnceCallback<void(std::vector<Cluster> clusters)>; base::OnceCallback<void(std::string peek_text,
std::vector<Cluster> clusters)>;
// Asynchronously fetches contextual suggestions for the given URL. // Asynchronously fetches contextual suggestions for the given URL.
void FetchContextualSuggestions(const GURL& url, void FetchContextualSuggestions(const GURL& url,
......
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