Chromium Code Reviews| Index: chrome/browser/android/ntp/ntp_snippets_bridge.cc |
| diff --git a/chrome/browser/android/ntp/ntp_snippets_bridge.cc b/chrome/browser/android/ntp/ntp_snippets_bridge.cc |
| index 5f9cbf85b7f411f2d3751301aee002ac1df9f7ea..595ac0e18a382191c1ccf452227a78e5566e5dd4 100644 |
| --- a/chrome/browser/android/ntp/ntp_snippets_bridge.cc |
| +++ b/chrome/browser/android/ntp/ntp_snippets_bridge.cc |
| @@ -26,10 +26,8 @@ |
| using base::android::AttachCurrentThread; |
| using base::android::ConvertJavaStringToUTF8; |
| +using base::android::ConvertUTF8ToJavaString; |
| using base::android::JavaParamRef; |
| -using base::android::ToJavaArrayOfStrings; |
| -using base::android::ToJavaLongArray; |
| -using base::android::ToJavaFloatArray; |
| using base::android::ScopedJavaGlobalRef; |
| using base::android::ScopedJavaLocalRef; |
| using ntp_snippets::ContentSuggestionsCategory; |
| @@ -126,9 +124,10 @@ void NTPSnippetsBridge::SnippetVisited(JNIEnv* env, |
| } |
| int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
| - const JavaParamRef<jobject>& obj) { |
| + const JavaParamRef<jobject>& obj, |
| + jint category) { |
| return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| - ContentSuggestionsCategory::ARTICLES)); |
| + static_cast<ContentSuggestionsCategory>(category))); |
| } |
| NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| @@ -137,23 +136,13 @@ void NTPSnippetsBridge::OnNewSuggestions() { |
| if (observer_.is_null()) |
| return; |
| - std::vector<std::string> ids; |
| - std::vector<std::string> titles; |
| - // URL for the article. This will also be used to find the favicon for the |
| - // article. |
| - std::vector<std::string> urls; |
| - // URL for the AMP version of the article if it exists. This will be used as |
| - // the URL to direct the user to on tap. |
| - std::vector<std::string> amp_urls; |
| - std::vector<std::string> snippets; |
| - std::vector<int64_t> timestamps; |
| - std::vector<std::string> publishers; |
| - std::vector<float> scores; |
| - |
| // Show all suggestions from all categories, even though we currently display |
| // them in a single section on the UI. |
| // TODO(pke): This is only for debugging new sections and will be replaced |
| // with proper multi-section UI support. |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + ScopedJavaLocalRef<jobject> suggestions = |
| + Java_SnippetsBridge_createSuggestionList(env); |
| for (ContentSuggestionsCategory category : |
| content_suggestions_service_->GetCategories()) { |
| if (content_suggestions_service_->GetCategoryStatus(category) != |
| @@ -162,39 +151,35 @@ void NTPSnippetsBridge::OnNewSuggestions() { |
| } |
| for (const ntp_snippets::ContentSuggestion& suggestion : |
| content_suggestions_service_->GetSuggestionsForCategory(category)) { |
| - ids.push_back(suggestion.id()); |
| - titles.push_back(suggestion.title()); |
| - // The url from source_info is a url for a site that is one of the |
| - // HOST_RESTRICT parameters, so this is preferred. |
| - urls.push_back(suggestion.url().spec()); |
| - amp_urls.push_back(suggestion.amp_url().spec()); |
| - snippets.push_back(suggestion.snippet_text()); |
| - timestamps.push_back(suggestion.publish_date().ToJavaTime()); |
| - publishers.push_back(suggestion.publisher_name()); |
| - scores.push_back(suggestion.score()); |
| + Java_SnippetsBridge_addSuggestion( |
|
PEConn
2016/08/01 16:26:51
I do like this approach but is there any overhead
Bernhard Bauer
2016/08/02 13:03:27
My gut feeling would be a lot of the time would be
|
| + env, suggestions.obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.id()).obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.title()).obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.publisher_name()).obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.snippet_text()).obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(), |
| + ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(), |
| + suggestion.publish_date().ToJavaTime(), suggestion.score()); |
| } |
| } |
| - JNIEnv* env = base::android::AttachCurrentThread(); |
| - Java_SnippetsBridge_onSnippetsAvailable( |
| - env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), |
| - ToJavaArrayOfStrings(env, titles).obj(), |
| - ToJavaArrayOfStrings(env, urls).obj(), |
| - ToJavaArrayOfStrings(env, amp_urls).obj(), |
| - ToJavaArrayOfStrings(env, snippets).obj(), |
| - ToJavaLongArray(env, timestamps).obj(), |
| - ToJavaArrayOfStrings(env, publishers).obj(), |
| - ToJavaFloatArray(env, scores).obj()); |
| + // TODO(mvanouwerkerk): Do not hard code ARTICLES. |
| + Java_SnippetsBridge_onSuggestionsAvailable( |
| + env, observer_.obj(), |
| + static_cast<int>(ContentSuggestionsCategory::ARTICLES), |
| + suggestions.obj()); |
| } |
| void NTPSnippetsBridge::OnCategoryStatusChanged( |
| ContentSuggestionsCategory category, |
| ContentSuggestionsCategoryStatus new_status) { |
| + // TODO(mvanouwerkerk): Do not hard code ARTICLES. |
| if (category != ContentSuggestionsCategory::ARTICLES) |
| return; |
| JNIEnv* env = base::android::AttachCurrentThread(); |
| Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), |
| + static_cast<int>(category), |
| static_cast<int>(new_status)); |
| } |