| 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 27 matching lines...) Expand all Loading... |
| 74 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | 75 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, |
| 75 const JavaParamRef<jobject>& j_profile) | 76 const JavaParamRef<jobject>& j_profile) |
| 76 : content_suggestions_service_observer_(this), weak_ptr_factory_(this) { | 77 : content_suggestions_service_observer_(this), weak_ptr_factory_(this) { |
| 77 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 78 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 78 content_suggestions_service_ = | 79 content_suggestions_service_ = |
| 79 ContentSuggestionsServiceFactory::GetForProfile(profile); | 80 ContentSuggestionsServiceFactory::GetForProfile(profile); |
| 80 history_service_ = | 81 history_service_ = |
| 81 HistoryServiceFactory::GetForProfile(profile, | 82 HistoryServiceFactory::GetForProfile(profile, |
| 82 ServiceAccessType::EXPLICIT_ACCESS); | 83 ServiceAccessType::EXPLICIT_ACCESS); |
| 83 content_suggestions_service_observer_.Add(content_suggestions_service_); | 84 content_suggestions_service_observer_.Add(content_suggestions_service_); |
| 85 category_factory_ = content_suggestions_service_->category_factory(); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 88 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 87 delete this; | 89 delete this; |
| 88 } | 90 } |
| 89 | 91 |
| 90 void NTPSnippetsBridge::SetObserver(JNIEnv* env, | 92 void NTPSnippetsBridge::SetObserver(JNIEnv* env, |
| 91 const JavaParamRef<jobject>& obj, | 93 const JavaParamRef<jobject>& obj, |
| 92 const JavaParamRef<jobject>& j_observer) { | 94 const JavaParamRef<jobject>& j_observer) { |
| 93 observer_.Reset(env, j_observer); | 95 observer_.Reset(env, j_observer); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 121 history_service_->QueryURL( | 123 history_service_->QueryURL( |
| 122 GURL(ConvertJavaStringToUTF8(env, jurl)), | 124 GURL(ConvertJavaStringToUTF8(env, jurl)), |
| 123 false, | 125 false, |
| 124 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), | 126 base::Bind(&SnippetVisitedHistoryRequestCallback, callback), |
| 125 &tracker_); | 127 &tracker_); |
| 126 } | 128 } |
| 127 | 129 |
| 128 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 130 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
| 129 const JavaParamRef<jobject>& obj) { | 131 const JavaParamRef<jobject>& obj) { |
| 130 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 132 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 131 ContentSuggestionsCategory::ARTICLES)); | 133 category_factory_->FromKnownCategory( |
| 134 KnownSuggestionsCategories::ARTICLES))); |
| 132 } | 135 } |
| 133 | 136 |
| 134 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 137 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 135 | 138 |
| 136 void NTPSnippetsBridge::OnNewSuggestions() { | 139 void NTPSnippetsBridge::OnNewSuggestions() { |
| 137 if (observer_.is_null()) | 140 if (observer_.is_null()) |
| 138 return; | 141 return; |
| 139 | 142 |
| 140 std::vector<std::string> ids; | 143 std::vector<std::string> ids; |
| 141 std::vector<std::string> titles; | 144 std::vector<std::string> titles; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 ToJavaArrayOfStrings(env, amp_urls).obj(), | 186 ToJavaArrayOfStrings(env, amp_urls).obj(), |
| 184 ToJavaArrayOfStrings(env, snippets).obj(), | 187 ToJavaArrayOfStrings(env, snippets).obj(), |
| 185 ToJavaLongArray(env, timestamps).obj(), | 188 ToJavaLongArray(env, timestamps).obj(), |
| 186 ToJavaArrayOfStrings(env, publishers).obj(), | 189 ToJavaArrayOfStrings(env, publishers).obj(), |
| 187 ToJavaFloatArray(env, scores).obj()); | 190 ToJavaFloatArray(env, scores).obj()); |
| 188 } | 191 } |
| 189 | 192 |
| 190 void NTPSnippetsBridge::OnCategoryStatusChanged( | 193 void NTPSnippetsBridge::OnCategoryStatusChanged( |
| 191 ContentSuggestionsCategory category, | 194 ContentSuggestionsCategory category, |
| 192 ContentSuggestionsCategoryStatus new_status) { | 195 ContentSuggestionsCategoryStatus new_status) { |
| 193 if (category != ContentSuggestionsCategory::ARTICLES) | 196 if (category != KnownSuggestionsCategories::ARTICLES) |
| 194 return; | 197 return; |
| 195 | 198 |
| 196 JNIEnv* env = base::android::AttachCurrentThread(); | 199 JNIEnv* env = base::android::AttachCurrentThread(); |
| 197 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), | 200 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), |
| 198 static_cast<int>(new_status)); | 201 static_cast<int>(new_status)); |
| 199 } | 202 } |
| 200 | 203 |
| 201 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 204 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
| 202 observer_.Reset(); | 205 observer_.Reset(); |
| 203 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 206 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
| 204 } | 207 } |
| 205 | 208 |
| 206 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 209 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 207 const std::string& snippet_id, | 210 const std::string& snippet_id, |
| 208 const gfx::Image& image) { | 211 const gfx::Image& image) { |
| 209 ScopedJavaLocalRef<jobject> j_bitmap; | 212 ScopedJavaLocalRef<jobject> j_bitmap; |
| 210 if (!image.IsEmpty()) | 213 if (!image.IsEmpty()) |
| 211 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 214 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 212 | 215 |
| 213 base::android::RunCallbackAndroid(callback, j_bitmap); | 216 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 214 } | 217 } |
| 215 | 218 |
| 216 // static | 219 // static |
| 217 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 220 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 218 return RegisterNativesImpl(env); | 221 return RegisterNativesImpl(env); |
| 219 } | 222 } |
| OLD | NEW |