| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/ntp/ntp_snippets_bridge.h" | 5 #include "chrome/browser/android/ntp/ntp_snippets_bridge.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/callback_android.h" | 9 #include "base/android/callback_android.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 KnownCategories::ARTICLES))); | 133 KnownCategories::ARTICLES))); |
| 134 } | 134 } |
| 135 | 135 |
| 136 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 136 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 137 | 137 |
| 138 void NTPSnippetsBridge::OnNewSuggestions() { | 138 void NTPSnippetsBridge::OnNewSuggestions() { |
| 139 if (observer_.is_null()) | 139 if (observer_.is_null()) |
| 140 return; | 140 return; |
| 141 | 141 |
| 142 std::vector<std::string> ids; | 142 std::vector<std::string> ids; |
| 143 std::vector<std::string> titles; | 143 std::vector<base::string16> titles; |
| 144 // URL for the article. This will also be used to find the favicon for the | 144 // URL for the article. This will also be used to find the favicon for the |
| 145 // article. | 145 // article. |
| 146 std::vector<std::string> urls; | 146 std::vector<std::string> urls; |
| 147 // URL for the AMP version of the article if it exists. This will be used as | 147 // URL for the AMP version of the article if it exists. This will be used as |
| 148 // the URL to direct the user to on tap. | 148 // the URL to direct the user to on tap. |
| 149 std::vector<std::string> amp_urls; | 149 std::vector<std::string> amp_urls; |
| 150 std::vector<std::string> snippets; | 150 std::vector<base::string16> snippet_texts; |
| 151 std::vector<int64_t> timestamps; | 151 std::vector<int64_t> timestamps; |
| 152 std::vector<std::string> publishers; | 152 std::vector<base::string16> publisher_names; |
| 153 std::vector<float> scores; | 153 std::vector<float> scores; |
| 154 | 154 |
| 155 // Show all suggestions from all categories, even though we currently display | 155 // Show all suggestions from all categories, even though we currently display |
| 156 // them in a single section on the UI. | 156 // them in a single section on the UI. |
| 157 // TODO(pke): This is only for debugging new sections and will be replaced | 157 // TODO(pke): This is only for debugging new sections and will be replaced |
| 158 // with proper multi-section UI support. | 158 // with proper multi-section UI support. |
| 159 for (Category category : content_suggestions_service_->GetCategories()) { | 159 for (Category category : content_suggestions_service_->GetCategories()) { |
| 160 if (content_suggestions_service_->GetCategoryStatus(category) != | 160 if (content_suggestions_service_->GetCategoryStatus(category) != |
| 161 CategoryStatus::AVAILABLE) { | 161 CategoryStatus::AVAILABLE) { |
| 162 continue; | 162 continue; |
| 163 } | 163 } |
| 164 for (const ntp_snippets::ContentSuggestion& suggestion : | 164 for (const ntp_snippets::ContentSuggestion& suggestion : |
| 165 content_suggestions_service_->GetSuggestionsForCategory(category)) { | 165 content_suggestions_service_->GetSuggestionsForCategory(category)) { |
| 166 ids.push_back(suggestion.id()); | 166 ids.push_back(suggestion.id()); |
| 167 titles.push_back(suggestion.title()); | 167 titles.push_back(suggestion.title()); |
| 168 // The url from source_info is a url for a site that is one of the | 168 // The url from source_info is a url for a site that is one of the |
| 169 // HOST_RESTRICT parameters, so this is preferred. | 169 // HOST_RESTRICT parameters, so this is preferred. |
| 170 urls.push_back(suggestion.url().spec()); | 170 urls.push_back(suggestion.url().spec()); |
| 171 amp_urls.push_back(suggestion.amp_url().spec()); | 171 amp_urls.push_back(suggestion.amp_url().spec()); |
| 172 snippets.push_back(suggestion.snippet_text()); | 172 snippet_texts.push_back(suggestion.snippet_text()); |
| 173 timestamps.push_back(suggestion.publish_date().ToJavaTime()); | 173 timestamps.push_back(suggestion.publish_date().ToJavaTime()); |
| 174 publishers.push_back(suggestion.publisher_name()); | 174 publisher_names.push_back(suggestion.publisher_name()); |
| 175 scores.push_back(suggestion.score()); | 175 scores.push_back(suggestion.score()); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 JNIEnv* env = base::android::AttachCurrentThread(); | 179 JNIEnv* env = base::android::AttachCurrentThread(); |
| 180 Java_SnippetsBridge_onSnippetsAvailable( | 180 Java_SnippetsBridge_onSnippetsAvailable( |
| 181 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), | 181 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), |
| 182 ToJavaArrayOfStrings(env, titles).obj(), | 182 ToJavaArrayOfStrings(env, titles).obj(), |
| 183 ToJavaArrayOfStrings(env, urls).obj(), | 183 ToJavaArrayOfStrings(env, urls).obj(), |
| 184 ToJavaArrayOfStrings(env, amp_urls).obj(), | 184 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 185 ToJavaArrayOfStrings(env, snippets).obj(), | 185 ToJavaArrayOfStrings(env, snippet_texts).obj(), |
| 186 ToJavaLongArray(env, timestamps).obj(), | 186 ToJavaLongArray(env, timestamps).obj(), |
| 187 ToJavaArrayOfStrings(env, publishers).obj(), | 187 ToJavaArrayOfStrings(env, publisher_names).obj(), |
| 188 ToJavaFloatArray(env, scores).obj()); | 188 ToJavaFloatArray(env, scores).obj()); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 191 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
| 192 CategoryStatus new_status) { | 192 CategoryStatus new_status) { |
| 193 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) | 193 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) |
| 194 return; | 194 return; |
| 195 | 195 |
| 196 JNIEnv* env = base::android::AttachCurrentThread(); | 196 JNIEnv* env = base::android::AttachCurrentThread(); |
| 197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), | 197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 210 if (!image.IsEmpty()) | 210 if (!image.IsEmpty()) |
| 211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 212 | 212 |
| 213 base::android::RunCallbackAndroid(callback, j_bitmap); | 213 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 214 } | 214 } |
| 215 | 215 |
| 216 // static | 216 // static |
| 217 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 217 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 218 return RegisterNativesImpl(env); | 218 return RegisterNativesImpl(env); |
| 219 } | 219 } |
| OLD | NEW |