| 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 16 matching lines...) Expand all Loading... |
| 27 using base::android::AttachCurrentThread; | 27 using base::android::AttachCurrentThread; |
| 28 using base::android::ConvertJavaStringToUTF8; | 28 using base::android::ConvertJavaStringToUTF8; |
| 29 using base::android::JavaParamRef; | 29 using base::android::JavaParamRef; |
| 30 using base::android::ToJavaArrayOfStrings; | 30 using base::android::ToJavaArrayOfStrings; |
| 31 using base::android::ToJavaLongArray; | 31 using base::android::ToJavaLongArray; |
| 32 using base::android::ToJavaFloatArray; | 32 using base::android::ToJavaFloatArray; |
| 33 using base::android::ScopedJavaGlobalRef; | 33 using base::android::ScopedJavaGlobalRef; |
| 34 using base::android::ScopedJavaLocalRef; | 34 using base::android::ScopedJavaLocalRef; |
| 35 using ntp_snippets::ContentSuggestionsCategory; | 35 using ntp_snippets::ContentSuggestionsCategory; |
| 36 using ntp_snippets::ContentSuggestionsCategoryStatus; | 36 using ntp_snippets::ContentSuggestionsCategoryStatus; |
| 37 using ntp_snippets::KnownSuggestionsCategories; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 void SnippetVisitedHistoryRequestCallback( | 41 void SnippetVisitedHistoryRequestCallback( |
| 41 base::android::ScopedJavaGlobalRef<jobject> callback, | 42 base::android::ScopedJavaGlobalRef<jobject> callback, |
| 42 bool success, | 43 bool success, |
| 43 const history::URLRow& row, | 44 const history::URLRow& row, |
| 44 const history::VisitVector& visitVector) { | 45 const history::VisitVector& visitVector) { |
| 45 bool visited = success && row.visit_count() != 0; | 46 bool visited = success && row.visit_count() != 0; |
| 46 base::android::RunCallbackAndroid(callback, visited); | 47 base::android::RunCallbackAndroid(callback, visited); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 history_service_->QueryURL( | 122 history_service_->QueryURL( |
| 122 GURL(ConvertJavaStringToUTF8(env, jurl)), | 123 GURL(ConvertJavaStringToUTF8(env, jurl)), |
| 123 false, | 124 false, |
| 124 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), | 125 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), |
| 125 &tracker_); | 126 &tracker_); |
| 126 } | 127 } |
| 127 | 128 |
| 128 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 129 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
| 129 const JavaParamRef<jobject>& obj) { | 130 const JavaParamRef<jobject>& obj) { |
| 130 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 131 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 131 ContentSuggestionsCategory::ARTICLES)); | 132 content_suggestions_service_->category_factory()->FromKnownCategory( |
| 133 KnownSuggestionsCategories::ARTICLES))); |
| 132 } | 134 } |
| 133 | 135 |
| 134 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 136 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 135 | 137 |
| 136 void NTPSnippetsBridge::OnNewSuggestions() { | 138 void NTPSnippetsBridge::OnNewSuggestions() { |
| 137 if (observer_.is_null()) | 139 if (observer_.is_null()) |
| 138 return; | 140 return; |
| 139 | 141 |
| 140 std::vector<std::string> ids; | 142 std::vector<std::string> ids; |
| 141 std::vector<std::string> titles; | 143 std::vector<std::string> titles; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 ToJavaArrayOfStrings(env, amp_urls).obj(), | 185 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 184 ToJavaArrayOfStrings(env, snippets).obj(), | 186 ToJavaArrayOfStrings(env, snippets).obj(), |
| 185 ToJavaLongArray(env, timestamps).obj(), | 187 ToJavaLongArray(env, timestamps).obj(), |
| 186 ToJavaArrayOfStrings(env, publishers).obj(), | 188 ToJavaArrayOfStrings(env, publishers).obj(), |
| 187 ToJavaFloatArray(env, scores).obj()); | 189 ToJavaFloatArray(env, scores).obj()); |
| 188 } | 190 } |
| 189 | 191 |
| 190 void NTPSnippetsBridge::OnCategoryStatusChanged( | 192 void NTPSnippetsBridge::OnCategoryStatusChanged( |
| 191 ContentSuggestionsCategory category, | 193 ContentSuggestionsCategory category, |
| 192 ContentSuggestionsCategoryStatus new_status) { | 194 ContentSuggestionsCategoryStatus new_status) { |
| 193 if (category != ContentSuggestionsCategory::ARTICLES) | 195 if (!category.IsKnownCategory(KnownSuggestionsCategories::ARTICLES)) |
| 194 return; | 196 return; |
| 195 | 197 |
| 196 JNIEnv* env = base::android::AttachCurrentThread(); | 198 JNIEnv* env = base::android::AttachCurrentThread(); |
| 197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), | 199 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), |
| 198 static_cast<int>(new_status)); | 200 static_cast<int>(new_status)); |
| 199 } | 201 } |
| 200 | 202 |
| 201 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 203 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
| 202 observer_.Reset(); | 204 observer_.Reset(); |
| 203 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 205 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
| 204 } | 206 } |
| 205 | 207 |
| 206 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 208 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 207 const std::string& snippet_id, | 209 const std::string& snippet_id, |
| 208 const gfx::Image& image) { | 210 const gfx::Image& image) { |
| 209 ScopedJavaLocalRef<jobject> j_bitmap; | 211 ScopedJavaLocalRef<jobject> j_bitmap; |
| 210 if (!image.IsEmpty()) | 212 if (!image.IsEmpty()) |
| 211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 213 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 212 | 214 |
| 213 base::android::RunCallbackAndroid(callback, j_bitmap); | 215 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 214 } | 216 } |
| 215 | 217 |
| 216 // static | 218 // static |
| 217 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 219 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 218 return RegisterNativesImpl(env); | 220 return RegisterNativesImpl(env); |
| 219 } | 221 } |
| OLD | NEW |