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;
public class ContextualSuggestionsBridge {
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.
*
......@@ -45,8 +65,7 @@ public class ContextualSuggestionsBridge {
* @param url URL for which to fetch suggestions.
* @param callback Callback used to return suggestions for a given URL.
*/
public void fetchSuggestions(
String url, Callback<List<ContextualSuggestionsCluster>> callback) {
public void fetchSuggestions(String url, Callback<ContextualSuggestionsResult> callback) {
assert mNativeContextualSuggestionsBridge != 0;
nativeFetchSuggestions(mNativeContextualSuggestionsBridge, url, callback);
}
......@@ -85,32 +104,30 @@ public class ContextualSuggestionsBridge {
}
@CalledByNative
private static List<ContextualSuggestionsCluster> createContextualSuggestionsClusterList() {
return new ArrayList<>();
private static ContextualSuggestionsResult createContextualSuggestionsResult(String peekText) {
return new ContextualSuggestionsResult(peekText);
}
@CalledByNative
private static void addNewClusterToList(
List<ContextualSuggestionsCluster> clusters, String title) {
clusters.add(new ContextualSuggestionsCluster(title));
private static void addNewClusterToResult(ContextualSuggestionsResult result, String title) {
result.getClusters().add(new ContextualSuggestionsCluster(title));
}
@CalledByNative
private static void addSuggestionToLastCluster(List<ContextualSuggestionsCluster> clusters,
String id, String title, String publisher, String url, long timestamp, float score,
long fetchTime, boolean isVideoSuggestion, int thumbnailDominantColor) {
String id, String title, String publisher, String url) {
assert clusters.size() > 0;
clusters.get(clusters.size() - 1)
.getSuggestions()
.add(new SnippetArticle(KnownCategories.CONTEXTUAL, id, title, publisher, url,
timestamp, score, fetchTime, isVideoSuggestion,
thumbnailDominantColor == 0 ? null : thumbnailDominantColor));
/*publishTimestamp=*/0, /*score=*/0f, /*fetchTimestamp=*/0,
/*isVideoSuggestion=*/false, /*thumbnailDominantColor=*/0));
}
private native long nativeInit(Profile profile);
private native void nativeDestroy(long nativeContextualSuggestionsBridge);
private native void nativeFetchSuggestions(long nativeContextualSuggestionsBridge, String url,
Callback<List<ContextualSuggestionsCluster>> callback);
Callback<ContextualSuggestionsResult> callback);
private native void nativeFetchSuggestionImage(
long nativeContextualSuggestionsBridge, String suggestionId, Callback<Bitmap> callback);
private native void nativeFetchSuggestionFavicon(
......
......@@ -79,7 +79,7 @@ void ContextualSuggestionsBridge::FetchSuggestions(
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& j_url,
const JavaParamRef<jobject>& j_callback) {
if (contextual_content_suggestions_service_ == nullptr)
if (!contextual_content_suggestions_service_)
return;
GURL url(ConvertJavaStringToUTF8(env, j_url));
......@@ -94,7 +94,7 @@ void ContextualSuggestionsBridge::FetchSuggestionImage(
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& j_suggestion_id,
const JavaParamRef<jobject>& j_callback) {
if (contextual_content_suggestions_service_ == nullptr)
if (!contextual_content_suggestions_service_)
return;
contextual_content_suggestions_service_->FetchContextualSuggestionImage(
......@@ -144,28 +144,24 @@ void ContextualSuggestionsBridge::ReportEvent(
void ContextualSuggestionsBridge::OnSuggestionsAvailable(
ScopedJavaGlobalRef<jobject> j_callback,
std::string peek_text,
std::vector<Cluster> clusters) {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> j_clusters =
Java_ContextualSuggestionsBridge_createContextualSuggestionsClusterList(
env);
ScopedJavaLocalRef<jobject> j_result =
Java_ContextualSuggestionsBridge_createContextualSuggestionsResult(
env, ConvertUTF8ToJavaString(env, peek_text));
for (auto& cluster : clusters) {
Java_ContextualSuggestionsBridge_addNewClusterToList(
env, j_clusters, ConvertUTF8ToJavaString(env, cluster.title));
Java_ContextualSuggestionsBridge_addNewClusterToResult(
env, j_result, ConvertUTF8ToJavaString(env, cluster.title));
for (auto& suggestion : cluster.suggestions) {
Java_ContextualSuggestionsBridge_addSuggestionToLastCluster(
env, j_clusters,
ConvertUTF8ToJavaString(env, suggestion.id().id_within_category()),
ConvertUTF16ToJavaString(env, suggestion.title()),
ConvertUTF16ToJavaString(env, suggestion.publisher_name()),
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));
env, j_result, ConvertUTF8ToJavaString(env, suggestion->id()),
ConvertUTF8ToJavaString(env, suggestion->title()),
ConvertUTF8ToJavaString(env, suggestion->publisher_name()),
ConvertUTF8ToJavaString(env, suggestion->url().spec()));
}
}
RunCallbackAndroid(j_callback, j_clusters);
RunCallbackAndroid(j_callback, j_result);
}
void ContextualSuggestionsBridge::OnImageFetched(
......
......@@ -66,6 +66,7 @@ class ContextualSuggestionsBridge {
void OnSuggestionsAvailable(
base::android::ScopedJavaGlobalRef<jobject> j_callback,
std::string peek_text,
std::vector<ntp_snippets::ContextualContentSuggestionsService::Cluster>
clusters);
......
......@@ -17,6 +17,10 @@
#include "ui/gfx/image/image.h"
namespace ntp_snippets {
namespace {
static const char kSamplePeekText[] = "Peek text";
static const char kSampleClusterTitle[] = "Cluster title filler";
} // namespace
ContextualContentSuggestionsService::Cluster::Cluster() = default;
......@@ -106,17 +110,12 @@ void ContextualContentSuggestionsService::
ContextualSuggestionsFetcher::OptionalSuggestions fetched_suggestions) {
std::vector<Cluster> clusters;
if (fetched_suggestions.has_value()) {
clusters.emplace_back();
Cluster& cluster = clusters.back();
for (const std::unique_ptr<ContextualSuggestion>& suggestion :
fetched_suggestions.value()) {
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;
}
Cluster cluster;
cluster.title = kSampleClusterTitle;
cluster.suggestions = std::move(fetched_suggestions.value());
clusters.push_back(std::move(cluster));
}
std::move(callback).Run(std::move(clusters));
std::move(callback).Run(kSamplePeekText, std::move(clusters));
}
} // namespace ntp_snippets
......@@ -36,7 +36,7 @@ class ContextualContentSuggestionsService : public KeyedService {
~Cluster();
std::string title;
std::vector<ContentSuggestion> suggestions;
ContextualSuggestion::PtrVector suggestions;
private:
DISALLOW_COPY_AND_ASSIGN(Cluster);
......@@ -56,7 +56,8 @@ class ContextualContentSuggestionsService : public KeyedService {
std::vector<ContentSuggestion> suggestions)>;
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.
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