| 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/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "chrome/browser/history/history_service_factory.h" | 13 #include "chrome/browser/history/history_service_factory.h" |
| 14 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 14 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_android.h" | 16 #include "chrome/browser/profiles/profile_android.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 18 #include "components/history/core/browser/history_service.h" | 18 #include "components/history/core/browser/history_service.h" |
| 19 #include "components/ntp_snippets/ntp_snippet.h" | 19 #include "components/ntp_snippets/ntp_snippet.h" |
| 20 #include "components/ntp_snippets/ntp_snippets_service.h" | 20 #include "components/ntp_snippets/ntp_snippets_service.h" |
| 21 #include "jni/SnippetsBridge_jni.h" | 21 #include "jni/SnippetsBridge_jni.h" |
| 22 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 24 | 24 |
| 25 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
| 26 using base::android::ConvertJavaStringToUTF8; | 26 using base::android::ConvertJavaStringToUTF8; |
| 27 using base::android::JavaParamRef; | 27 using base::android::JavaParamRef; |
| 28 using base::android::ToJavaArrayOfStrings; | 28 using base::android::ToJavaArrayOfStrings; |
| 29 using base::android::ToJavaLongArray; | 29 using base::android::ToJavaLongArray; |
| 30 using base::android::ToJavaFloatArray; |
| 30 using base::android::ScopedJavaGlobalRef; | 31 using base::android::ScopedJavaGlobalRef; |
| 31 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 void SnippetVisitedHistoryRequestCallback( | 36 void SnippetVisitedHistoryRequestCallback( |
| 36 base::android::ScopedJavaGlobalRef<jobject> callback, | 37 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 37 bool success, | 38 bool success, |
| 38 const history::URLRow& row, | 39 const history::URLRow& row, |
| 39 const history::VisitVector& visitVector) { | 40 const history::VisitVector& visitVector) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 // URL for the article. This will also be used to find the favicon for the | 131 // URL for the article. This will also be used to find the favicon for the |
| 131 // article. | 132 // article. |
| 132 std::vector<std::string> urls; | 133 std::vector<std::string> urls; |
| 133 // URL for the AMP version of the article if it exists. This will be used as | 134 // URL for the AMP version of the article if it exists. This will be used as |
| 134 // the URL to direct the user to on tap. | 135 // the URL to direct the user to on tap. |
| 135 std::vector<std::string> amp_urls; | 136 std::vector<std::string> amp_urls; |
| 136 std::vector<std::string> thumbnail_urls; | 137 std::vector<std::string> thumbnail_urls; |
| 137 std::vector<std::string> snippets; | 138 std::vector<std::string> snippets; |
| 138 std::vector<int64_t> timestamps; | 139 std::vector<int64_t> timestamps; |
| 139 std::vector<std::string> publishers; | 140 std::vector<std::string> publishers; |
| 141 std::vector<float> scores; |
| 140 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 142 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 141 ntp_snippets_service_->snippets()) { | 143 ntp_snippets_service_->snippets()) { |
| 142 ids.push_back(snippet->id()); | 144 ids.push_back(snippet->id()); |
| 143 titles.push_back(snippet->title()); | 145 titles.push_back(snippet->title()); |
| 144 // The url from source_info is a url for a site that is one of the | 146 // The url from source_info is a url for a site that is one of the |
| 145 // HOST_RESTRICT parameters, so this is preferred. | 147 // HOST_RESTRICT parameters, so this is preferred. |
| 146 urls.push_back(snippet->best_source().url.spec()); | 148 urls.push_back(snippet->best_source().url.spec()); |
| 147 amp_urls.push_back(snippet->best_source().amp_url.spec()); | 149 amp_urls.push_back(snippet->best_source().amp_url.spec()); |
| 148 thumbnail_urls.push_back(snippet->salient_image_url().spec()); | 150 thumbnail_urls.push_back(snippet->salient_image_url().spec()); |
| 149 snippets.push_back(snippet->snippet()); | 151 snippets.push_back(snippet->snippet()); |
| 150 timestamps.push_back(snippet->publish_date().ToJavaTime()); | 152 timestamps.push_back(snippet->publish_date().ToJavaTime()); |
| 151 publishers.push_back(snippet->best_source().publisher_name); | 153 publishers.push_back(snippet->best_source().publisher_name); |
| 154 scores.push_back(snippet->score()); |
| 152 } | 155 } |
| 153 | 156 |
| 154 JNIEnv* env = base::android::AttachCurrentThread(); | 157 JNIEnv* env = base::android::AttachCurrentThread(); |
| 155 Java_SnippetsBridge_onSnippetsAvailable( | 158 Java_SnippetsBridge_onSnippetsAvailable( |
| 156 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), | 159 env, observer_.obj(), ToJavaArrayOfStrings(env, ids).obj(), |
| 157 ToJavaArrayOfStrings(env, titles).obj(), | 160 ToJavaArrayOfStrings(env, titles).obj(), |
| 158 ToJavaArrayOfStrings(env, urls).obj(), | 161 ToJavaArrayOfStrings(env, urls).obj(), |
| 159 ToJavaArrayOfStrings(env, amp_urls).obj(), | 162 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 160 ToJavaArrayOfStrings(env, thumbnail_urls).obj(), | 163 ToJavaArrayOfStrings(env, thumbnail_urls).obj(), |
| 161 ToJavaArrayOfStrings(env, snippets).obj(), | 164 ToJavaArrayOfStrings(env, snippets).obj(), |
| 162 ToJavaLongArray(env, timestamps).obj(), | 165 ToJavaLongArray(env, timestamps).obj(), |
| 163 ToJavaArrayOfStrings(env, publishers).obj()); | 166 ToJavaArrayOfStrings(env, publishers).obj(), |
| 167 ToJavaFloatArray(env, scores).obj()); |
| 164 } | 168 } |
| 165 | 169 |
| 166 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { | 170 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { |
| 167 observer_.Reset(); | 171 observer_.Reset(); |
| 168 snippet_service_observer_.Remove(ntp_snippets_service_); | 172 snippet_service_observer_.Remove(ntp_snippets_service_); |
| 169 } | 173 } |
| 170 | 174 |
| 171 void NTPSnippetsBridge::NTPSnippetsServiceCleared() { | 175 void NTPSnippetsBridge::NTPSnippetsServiceCleared() { |
| 172 // The user signed out or disabled sync. Since snippets rely on those, we | 176 // The user signed out or disabled sync. Since snippets rely on those, we |
| 173 // clear them to be consistent with the initially signed out state. | 177 // clear them to be consistent with the initially signed out state. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 185 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 189 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 186 | 190 |
| 187 Java_FetchSnippetImageCallback_onSnippetImageAvailable(env, callback.obj(), | 191 Java_FetchSnippetImageCallback_onSnippetImageAvailable(env, callback.obj(), |
| 188 j_bitmap.obj()); | 192 j_bitmap.obj()); |
| 189 } | 193 } |
| 190 | 194 |
| 191 // static | 195 // static |
| 192 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 196 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 193 return RegisterNativesImpl(env); | 197 return RegisterNativesImpl(env); |
| 194 } | 198 } |
| OLD | NEW |