| 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" |
| 11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
| 15 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 15 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 16 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 16 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_android.h" | 18 #include "chrome/browser/profiles/profile_android.h" |
| 19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "components/history/core/browser/history_service.h" | 20 #include "components/history/core/browser/history_service.h" |
| 21 #include "components/ntp_snippets/content_suggestion.h" | 21 #include "components/ntp_snippets/content_suggestion.h" |
| 22 #include "jni/SnippetsBridge_jni.h" | 22 #include "jni/SnippetsBridge_jni.h" |
| 23 #include "ui/gfx/android/java_bitmap.h" | 23 #include "ui/gfx/android/java_bitmap.h" |
| 24 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
| 25 | 25 |
| 26 using base::android::AttachCurrentThread; | 26 using base::android::AttachCurrentThread; |
| 27 using base::android::ConvertJavaStringToUTF8; | 27 using base::android::ConvertJavaStringToUTF8; |
| 28 using base::android::ConvertUTF8ToJavaString; |
| 28 using base::android::JavaParamRef; | 29 using base::android::JavaParamRef; |
| 29 using base::android::ToJavaArrayOfStrings; | 30 using base::android::ToJavaArrayOfStrings; |
| 31 using base::android::ToJavaIntArray; |
| 30 using base::android::ToJavaLongArray; | 32 using base::android::ToJavaLongArray; |
| 31 using base::android::ToJavaFloatArray; | 33 using base::android::ToJavaFloatArray; |
| 32 using base::android::ScopedJavaGlobalRef; | 34 using base::android::ScopedJavaGlobalRef; |
| 33 using base::android::ScopedJavaLocalRef; | 35 using base::android::ScopedJavaLocalRef; |
| 36 using ntp_snippets::ContentSuggestion; |
| 34 using ntp_snippets::ContentSuggestionsCategory; | 37 using ntp_snippets::ContentSuggestionsCategory; |
| 35 using ntp_snippets::ContentSuggestionsCategoryStatus; | 38 using ntp_snippets::ContentSuggestionsCategoryStatus; |
| 36 | 39 |
| 37 namespace { | 40 namespace { |
| 38 | 41 |
| 39 void SnippetVisitedHistoryRequestCallback( | 42 void SnippetVisitedHistoryRequestCallback( |
| 40 base::android::ScopedJavaGlobalRef<jobject> callback, | 43 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 41 bool success, | 44 bool success, |
| 42 const history::URLRow& row, | 45 const history::URLRow& row, |
| 43 const history::VisitVector& visitVector) { | 46 const history::VisitVector& visitVector) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 const JavaParamRef<jstring>& jurl) { | 118 const JavaParamRef<jstring>& jurl) { |
| 116 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); | 119 base::android::ScopedJavaGlobalRef<jobject> callback(jcallback); |
| 117 | 120 |
| 118 history_service_->QueryURL( | 121 history_service_->QueryURL( |
| 119 GURL(ConvertJavaStringToUTF8(env, jurl)), | 122 GURL(ConvertJavaStringToUTF8(env, jurl)), |
| 120 false, | 123 false, |
| 121 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), | 124 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), |
| 122 &tracker_); | 125 &tracker_); |
| 123 } | 126 } |
| 124 | 127 |
| 125 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 128 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( |
| 126 const JavaParamRef<jobject>& obj) { | 129 JNIEnv* env, |
| 130 const base::android::JavaParamRef<jobject>& obj) { |
| 131 std::vector<ContentSuggestionsCategory> categories = |
| 132 content_suggestions_service_->GetCategories(); |
| 133 return ToJavaIntArray(env, reinterpret_cast<int*>(categories.data()), |
| 134 categories.size()); |
| 135 } |
| 136 |
| 137 ScopedJavaLocalRef<jobjectArray> NTPSnippetsBridge::GetSuggestionsForCategory( |
| 138 JNIEnv* env, |
| 139 const base::android::JavaParamRef<jobject>& obj, |
| 140 jint category) { |
| 141 // Could potentially cache these? |
| 142 ScopedJavaLocalRef<jclass> content_suggestion_clazz( |
| 143 env, env->FindClass( |
| 144 "org/chromium/chrome/browser/ntp/snippets/SnippetArticle")); |
| 145 const std::vector<ContentSuggestion>& suggestions = |
| 146 content_suggestions_service_->GetSuggestionsForCategory( |
| 147 static_cast<ContentSuggestionsCategory>(category)); |
| 148 ScopedJavaLocalRef<jobjectArray> result( |
| 149 env, env->NewObjectArray(suggestions.size(), |
| 150 content_suggestion_clazz.obj(), nullptr)); |
| 151 for (size_t index = 0; index < suggestions.size(); index++) { |
| 152 const ContentSuggestion& suggestion = suggestions[index]; |
| 153 ScopedJavaLocalRef<jobject> javaSuggestion = |
| 154 Java_SnippetsBridge_createContentSuggestion( |
| 155 env, obj.obj(), ConvertUTF8ToJavaString(env, suggestion.id()).obj(), |
| 156 ConvertUTF8ToJavaString(env, suggestion.title()).obj(), |
| 157 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(), |
| 158 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(), |
| 159 ConvertUTF8ToJavaString(env, suggestion.snippet_text()).obj(), |
| 160 static_cast<jlong>(suggestion.publish_date().ToJavaTime()), |
| 161 ConvertUTF8ToJavaString(env, suggestion.publisher_name()).obj(), |
| 162 static_cast<jfloat>(suggestion.score()), JniIntWrapper(index)); |
| 163 env->SetObjectArrayElement(result.obj(), index, javaSuggestion.obj()); |
| 164 } |
| 165 return result; |
| 166 } |
| 167 |
| 168 int NTPSnippetsBridge::GetArticlesCategoryStatus( |
| 169 JNIEnv* env, |
| 170 const JavaParamRef<jobject>& obj) { |
| 127 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 171 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 128 ContentSuggestionsCategory::ARTICLES)); | 172 ContentSuggestionsCategory::ARTICLES)); |
| 129 } | 173 } |
| 130 | 174 |
| 175 int NTPSnippetsBridge::GetCategoryStatus( |
| 176 JNIEnv* env, |
| 177 const base::android::JavaParamRef<jobject>& obj, |
| 178 jint category) { |
| 179 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 180 static_cast<ContentSuggestionsCategory>(category))); |
| 181 } |
| 182 |
| 131 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 183 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 132 | 184 |
| 133 void NTPSnippetsBridge::OnNewSuggestions() { | 185 void NTPSnippetsBridge::OnNewSuggestions() { |
| 134 if (observer_.is_null()) | 186 if (observer_.is_null()) |
| 135 return; | 187 return; |
| 136 | 188 |
| 189 JNIEnv* env = base::android::AttachCurrentThread(); |
| 190 Java_SnippetsBridge_onNewSuggestions(env, observer_.obj()); |
| 191 |
| 137 std::vector<std::string> ids; | 192 std::vector<std::string> ids; |
| 138 std::vector<std::string> titles; | 193 std::vector<std::string> titles; |
| 139 // URL for the article. This will also be used to find the favicon for the | 194 // URL for the article. This will also be used to find the favicon for the |
| 140 // article. | 195 // article. |
| 141 std::vector<std::string> urls; | 196 std::vector<std::string> urls; |
| 142 // URL for the AMP version of the article if it exists. This will be used as | 197 // URL for the AMP version of the article if it exists. This will be used as |
| 143 // the URL to direct the user to on tap. | 198 // the URL to direct the user to on tap. |
| 144 std::vector<std::string> amp_urls; | 199 std::vector<std::string> amp_urls; |
| 145 std::vector<std::string> snippets; | 200 std::vector<std::string> snippets; |
| 146 std::vector<int64_t> timestamps; | 201 std::vector<int64_t> timestamps; |
| 147 std::vector<std::string> publishers; | 202 std::vector<std::string> publishers; |
| 148 std::vector<float> scores; | 203 std::vector<float> scores; |
| 149 for (const ntp_snippets::ContentSuggestion& suggestion : | 204 for (const ntp_snippets::ContentSuggestion& suggestion : |
| 150 content_suggestions_service_->GetSuggestionsForCategory( | 205 content_suggestions_service_->GetSuggestionsForCategory( |
| 151 ContentSuggestionsCategory::ARTICLES)) { | 206 ContentSuggestionsCategory::ARTICLES)) { |
| 152 ids.push_back(suggestion.id()); | 207 ids.push_back(suggestion.id()); |
| 153 titles.push_back(suggestion.title()); | 208 titles.push_back(suggestion.title()); |
| 154 // The url from source_info is a url for a site that is one of the | 209 // The url from source_info is a url for a site that is one of the |
| 155 // HOST_RESTRICT parameters, so this is preferred. | 210 // HOST_RESTRICT parameters, so this is preferred. |
| 156 urls.push_back(suggestion.url().spec()); | 211 urls.push_back(suggestion.url().spec()); |
| 157 amp_urls.push_back(suggestion.amp_url().spec()); | 212 amp_urls.push_back(suggestion.amp_url().spec()); |
| 158 snippets.push_back(suggestion.snippet_text()); | 213 snippets.push_back(suggestion.snippet_text()); |
| 159 timestamps.push_back(suggestion.publish_date().ToJavaTime()); | 214 timestamps.push_back(suggestion.publish_date().ToJavaTime()); |
| 160 publishers.push_back(suggestion.publisher_name()); | 215 publishers.push_back(suggestion.publisher_name()); |
| 161 scores.push_back(suggestion.score()); | 216 scores.push_back(suggestion.score()); |
| 162 } | 217 } |
| 163 | 218 |
| 164 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 165 Java_SnippetsBridge_onSnippetsAvailable( | 219 Java_SnippetsBridge_onSnippetsAvailable( |
| 166 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), | 220 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), |
| 167 ToJavaArrayOfStrings(env, titles).obj(), | 221 ToJavaArrayOfStrings(env, titles).obj(), |
| 168 ToJavaArrayOfStrings(env, urls).obj(), | 222 ToJavaArrayOfStrings(env, urls).obj(), |
| 169 ToJavaArrayOfStrings(env, amp_urls).obj(), | 223 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 170 ToJavaArrayOfStrings(env, snippets).obj(), | 224 ToJavaArrayOfStrings(env, snippets).obj(), |
| 171 ToJavaLongArray(env, timestamps).obj(), | 225 ToJavaLongArray(env, timestamps).obj(), |
| 172 ToJavaArrayOfStrings(env, publishers).obj(), | 226 ToJavaArrayOfStrings(env, publishers).obj(), |
| 173 ToJavaFloatArray(env, scores).obj()); | 227 ToJavaFloatArray(env, scores).obj()); |
| 174 } | 228 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 196 if (!image.IsEmpty()) | 250 if (!image.IsEmpty()) |
| 197 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 251 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 198 | 252 |
| 199 base::android::RunCallbackAndroid(callback, j_bitmap); | 253 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 200 } | 254 } |
| 201 | 255 |
| 202 // static | 256 // static |
| 203 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 257 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 204 return RegisterNativesImpl(env); | 258 return RegisterNativesImpl(env); |
| 205 } | 259 } |
| OLD | NEW |