| 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 10 matching lines...) Expand all Loading... |
| 21 #include "components/ntp_snippets/ntp_snippets_service.h" | 21 #include "components/ntp_snippets/ntp_snippets_service.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::JavaParamRef; | 28 using base::android::JavaParamRef; |
| 29 using base::android::ToJavaArrayOfStrings; | 29 using base::android::ToJavaArrayOfStrings; |
| 30 using base::android::ToJavaLongArray; | 30 using base::android::ToJavaLongArray; |
| 31 using base::android::ToJavaFloatArray; |
| 31 using base::android::ScopedJavaGlobalRef; | 32 using base::android::ScopedJavaGlobalRef; |
| 32 using base::android::ScopedJavaLocalRef; | 33 using base::android::ScopedJavaLocalRef; |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 void SnippetVisitedHistoryRequestCallback( | 37 void SnippetVisitedHistoryRequestCallback( |
| 37 base::android::ScopedJavaGlobalRef<jobject> callback, | 38 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 38 bool success, | 39 bool success, |
| 39 const history::URLRow& row, | 40 const history::URLRow& row, |
| 40 const history::VisitVector& visitVector) { | 41 const history::VisitVector& visitVector) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // URL for the article. This will also be used to find the favicon for the | 129 // URL for the article. This will also be used to find the favicon for the |
| 129 // article. | 130 // article. |
| 130 std::vector<std::string> urls; | 131 std::vector<std::string> urls; |
| 131 // URL for the AMP version of the article if it exists. This will be used as | 132 // URL for the AMP version of the article if it exists. This will be used as |
| 132 // the URL to direct the user to on tap. | 133 // the URL to direct the user to on tap. |
| 133 std::vector<std::string> amp_urls; | 134 std::vector<std::string> amp_urls; |
| 134 std::vector<std::string> thumbnail_urls; | 135 std::vector<std::string> thumbnail_urls; |
| 135 std::vector<std::string> snippets; | 136 std::vector<std::string> snippets; |
| 136 std::vector<int64_t> timestamps; | 137 std::vector<int64_t> timestamps; |
| 137 std::vector<std::string> publishers; | 138 std::vector<std::string> publishers; |
| 139 std::vector<float> scores; |
| 138 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 140 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 139 ntp_snippets_service_->snippets()) { | 141 ntp_snippets_service_->snippets()) { |
| 140 ids.push_back(snippet->id()); | 142 ids.push_back(snippet->id()); |
| 141 titles.push_back(snippet->title()); | 143 titles.push_back(snippet->title()); |
| 142 // The url from source_info is a url for a site that is one of the | 144 // The url from source_info is a url for a site that is one of the |
| 143 // HOST_RESTRICT parameters, so this is preferred. | 145 // HOST_RESTRICT parameters, so this is preferred. |
| 144 urls.push_back(snippet->best_source().url.spec()); | 146 urls.push_back(snippet->best_source().url.spec()); |
| 145 amp_urls.push_back(snippet->best_source().amp_url.spec()); | 147 amp_urls.push_back(snippet->best_source().amp_url.spec()); |
| 146 thumbnail_urls.push_back(snippet->salient_image_url().spec()); | 148 thumbnail_urls.push_back(snippet->salient_image_url().spec()); |
| 147 snippets.push_back(snippet->snippet()); | 149 snippets.push_back(snippet->snippet()); |
| 148 timestamps.push_back(snippet->publish_date().ToJavaTime()); | 150 timestamps.push_back(snippet->publish_date().ToJavaTime()); |
| 149 publishers.push_back(snippet->best_source().publisher_name); | 151 publishers.push_back(snippet->best_source().publisher_name); |
| 152 scores.push_back(snippet->score()); |
| 150 } | 153 } |
| 151 | 154 |
| 152 JNIEnv* env = base::android::AttachCurrentThread(); | 155 JNIEnv* env = base::android::AttachCurrentThread(); |
| 153 Java_SnippetsBridge_onSnippetsAvailable( | 156 Java_SnippetsBridge_onSnippetsAvailable( |
| 154 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), | 157 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), |
| 155 ToJavaArrayOfStrings(env, titles).obj(), | 158 ToJavaArrayOfStrings(env, titles).obj(), |
| 156 ToJavaArrayOfStrings(env, urls).obj(), | 159 ToJavaArrayOfStrings(env, urls).obj(), |
| 157 ToJavaArrayOfStrings(env, amp_urls).obj(), | 160 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 158 ToJavaArrayOfStrings(env, thumbnail_urls).obj(), | 161 ToJavaArrayOfStrings(env, thumbnail_urls).obj(), |
| 159 ToJavaArrayOfStrings(env, snippets).obj(), | 162 ToJavaArrayOfStrings(env, snippets).obj(), |
| 160 ToJavaLongArray(env, timestamps).obj(), | 163 ToJavaLongArray(env, timestamps).obj(), |
| 161 ToJavaArrayOfStrings(env, publishers).obj()); | 164 ToJavaArrayOfStrings(env, publishers).obj(), |
| 165 ToJavaFloatArray(env, scores).obj()); |
| 162 } | 166 } |
| 163 | 167 |
| 164 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { | 168 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { |
| 165 observer_.Reset(); | 169 observer_.Reset(); |
| 166 snippet_service_observer_.Remove(ntp_snippets_service_); | 170 snippet_service_observer_.Remove(ntp_snippets_service_); |
| 167 } | 171 } |
| 168 | 172 |
| 169 void NTPSnippetsBridge::NTPSnippetsServiceDisabled() { | 173 void NTPSnippetsBridge::NTPSnippetsServiceDisabled() { |
| 170 // The user signed out or disabled sync. Since snippets rely on those, we | 174 // The user signed out or disabled sync. Since snippets rely on those, we |
| 171 // clear them to be consistent with the initially signed out state. | 175 // clear them to be consistent with the initially signed out state. |
| 172 JNIEnv* env = base::android::AttachCurrentThread(); | 176 JNIEnv* env = base::android::AttachCurrentThread(); |
| 173 Java_SnippetsBridge_onSnippetsDisabled(env, observer_.obj()); | 177 Java_SnippetsBridge_onSnippetsDisabled(env, observer_.obj()); |
| 174 } | 178 } |
| 175 | 179 |
| 176 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 180 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 177 const std::string& snippet_id, | 181 const std::string& snippet_id, |
| 178 const gfx::Image& image) { | 182 const gfx::Image& image) { |
| 179 ScopedJavaLocalRef<jobject> j_bitmap; | 183 ScopedJavaLocalRef<jobject> j_bitmap; |
| 180 if (!image.IsEmpty()) | 184 if (!image.IsEmpty()) |
| 181 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 185 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 182 | 186 |
| 183 base::android::RunCallbackAndroid(callback, j_bitmap); | 187 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 184 } | 188 } |
| 185 | 189 |
| 186 // static | 190 // static |
| 187 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 191 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 188 return RegisterNativesImpl(env); | 192 return RegisterNativesImpl(env); |
| 189 } | 193 } |
| OLD | NEW |