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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 JNIEnv* env, | 133 JNIEnv* env, |
134 const base::android::JavaParamRef<jobject>& obj, | 134 const base::android::JavaParamRef<jobject>& obj, |
135 jint category) { | 135 jint category) { |
136 base::Optional<CategoryInfo> info = | 136 base::Optional<CategoryInfo> info = |
137 content_suggestions_service_->GetCategoryInfo( | 137 content_suggestions_service_->GetCategoryInfo( |
138 content_suggestions_service_->category_factory()->FromIDValue( | 138 content_suggestions_service_->category_factory()->FromIDValue( |
139 category)); | 139 category)); |
140 if (!info) | 140 if (!info) |
141 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); | 141 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); |
142 return Java_SnippetsBridge_createSuggestionsCategoryInfo( | 142 return Java_SnippetsBridge_createSuggestionsCategoryInfo( |
143 env, ConvertUTF16ToJavaString(env, info->title()).obj(), | 143 env, ConvertUTF16ToJavaString(env, info->title()), |
144 static_cast<int>(info->card_layout()), info->has_more_button()); | 144 static_cast<int>(info->card_layout()), info->has_more_button()); |
145 } | 145 } |
146 | 146 |
147 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( | 147 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( |
148 JNIEnv* env, | 148 JNIEnv* env, |
149 const base::android::JavaParamRef<jobject>& obj, | 149 const base::android::JavaParamRef<jobject>& obj, |
150 jint category) { | 150 jint category) { |
151 // Get layout for the category. | 151 // Get layout for the category. |
152 base::Optional<CategoryInfo> info = | 152 base::Optional<CategoryInfo> info = |
153 content_suggestions_service_->GetCategoryInfo( | 153 content_suggestions_service_->GetCategoryInfo( |
154 content_suggestions_service_->category_factory()->FromIDValue( | 154 content_suggestions_service_->category_factory()->FromIDValue( |
155 category)); | 155 category)); |
156 DCHECK(info); | 156 DCHECK(info); |
157 | 157 |
158 const std::vector<ContentSuggestion>& suggestions = | 158 const std::vector<ContentSuggestion>& suggestions = |
159 content_suggestions_service_->GetSuggestionsForCategory( | 159 content_suggestions_service_->GetSuggestionsForCategory( |
160 content_suggestions_service_->category_factory()->FromIDValue( | 160 content_suggestions_service_->category_factory()->FromIDValue( |
161 category)); | 161 category)); |
162 ScopedJavaLocalRef<jobject> result = | 162 ScopedJavaLocalRef<jobject> result = |
163 Java_SnippetsBridge_createSuggestionList(env); | 163 Java_SnippetsBridge_createSuggestionList(env); |
164 for (const ContentSuggestion& suggestion : suggestions) { | 164 for (const ContentSuggestion& suggestion : suggestions) { |
165 Java_SnippetsBridge_addSuggestion( | 165 Java_SnippetsBridge_addSuggestion( |
166 env, result.obj(), ConvertUTF8ToJavaString(env, suggestion.id()).obj(), | 166 env, result, ConvertUTF8ToJavaString(env, suggestion.id()), |
167 ConvertUTF16ToJavaString(env, suggestion.title()).obj(), | 167 ConvertUTF16ToJavaString(env, suggestion.title()), |
168 ConvertUTF16ToJavaString(env, suggestion.publisher_name()).obj(), | 168 ConvertUTF16ToJavaString(env, suggestion.publisher_name()), |
169 ConvertUTF16ToJavaString(env, suggestion.snippet_text()).obj(), | 169 ConvertUTF16ToJavaString(env, suggestion.snippet_text()), |
170 ConvertUTF8ToJavaString(env, suggestion.url().spec()).obj(), | 170 ConvertUTF8ToJavaString(env, suggestion.url().spec()), |
171 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()).obj(), | 171 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()), |
172 suggestion.publish_date().ToJavaTime(), suggestion.score(), | 172 suggestion.publish_date().ToJavaTime(), suggestion.score(), |
173 static_cast<int>(info->card_layout())); | 173 static_cast<int>(info->card_layout())); |
174 } | 174 } |
175 return result; | 175 return result; |
176 } | 176 } |
177 | 177 |
178 void NTPSnippetsBridge::FetchSuggestionImage( | 178 void NTPSnippetsBridge::FetchSuggestionImage( |
179 JNIEnv* env, | 179 JNIEnv* env, |
180 const JavaParamRef<jobject>& obj, | 180 const JavaParamRef<jobject>& obj, |
181 const JavaParamRef<jstring>& suggestion_id, | 181 const JavaParamRef<jstring>& suggestion_id, |
(...skipping 24 matching lines...) Expand all Loading... |
206 base::Bind(&URLVisitedHistoryRequestCallback, callback), &tracker_); | 206 base::Bind(&URLVisitedHistoryRequestCallback, callback), &tracker_); |
207 } | 207 } |
208 | 208 |
209 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 209 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
210 | 210 |
211 void NTPSnippetsBridge::OnNewSuggestions(Category category) { | 211 void NTPSnippetsBridge::OnNewSuggestions(Category category) { |
212 if (observer_.is_null()) | 212 if (observer_.is_null()) |
213 return; | 213 return; |
214 | 214 |
215 JNIEnv* env = base::android::AttachCurrentThread(); | 215 JNIEnv* env = base::android::AttachCurrentThread(); |
216 Java_SnippetsBridge_onNewSuggestions(env, observer_.obj(), | 216 Java_SnippetsBridge_onNewSuggestions(env, observer_, |
217 static_cast<int>(category.id())); | 217 static_cast<int>(category.id())); |
218 } | 218 } |
219 | 219 |
220 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 220 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
221 CategoryStatus new_status) { | 221 CategoryStatus new_status) { |
222 if (observer_.is_null()) | 222 if (observer_.is_null()) |
223 return; | 223 return; |
224 | 224 |
225 JNIEnv* env = base::android::AttachCurrentThread(); | 225 JNIEnv* env = base::android::AttachCurrentThread(); |
226 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_.obj(), | 226 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_, |
227 static_cast<int>(category.id()), | 227 static_cast<int>(category.id()), |
228 static_cast<int>(new_status)); | 228 static_cast<int>(new_status)); |
229 } | 229 } |
230 | 230 |
231 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 231 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
232 observer_.Reset(); | 232 observer_.Reset(); |
233 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 233 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
234 } | 234 } |
235 | 235 |
236 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 236 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
237 const std::string& snippet_id, | 237 const std::string& snippet_id, |
238 const gfx::Image& image) { | 238 const gfx::Image& image) { |
239 ScopedJavaLocalRef<jobject> j_bitmap; | 239 ScopedJavaLocalRef<jobject> j_bitmap; |
240 if (!image.IsEmpty()) | 240 if (!image.IsEmpty()) |
241 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 241 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
242 | 242 |
243 base::android::RunCallbackAndroid(callback, j_bitmap); | 243 base::android::RunCallbackAndroid(callback, j_bitmap); |
244 } | 244 } |
245 | 245 |
246 // static | 246 // static |
247 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 247 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
248 return RegisterNativesImpl(env); | 248 return RegisterNativesImpl(env); |
249 } | 249 } |
OLD | NEW |