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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 CategoryFromIDValue(category)); | 194 CategoryFromIDValue(category)); |
195 DCHECK(info); | 195 DCHECK(info); |
196 | 196 |
197 const std::vector<ContentSuggestion>& suggestions = | 197 const std::vector<ContentSuggestion>& suggestions = |
198 content_suggestions_service_->GetSuggestionsForCategory( | 198 content_suggestions_service_->GetSuggestionsForCategory( |
199 CategoryFromIDValue(category)); | 199 CategoryFromIDValue(category)); |
200 ScopedJavaLocalRef<jobject> result = | 200 ScopedJavaLocalRef<jobject> result = |
201 Java_SnippetsBridge_createSuggestionList(env); | 201 Java_SnippetsBridge_createSuggestionList(env); |
202 for (const ContentSuggestion& suggestion : suggestions) { | 202 for (const ContentSuggestion& suggestion : suggestions) { |
203 Java_SnippetsBridge_addSuggestion( | 203 Java_SnippetsBridge_addSuggestion( |
204 env, result, category, ConvertUTF8ToJavaString(env, suggestion.id()), | 204 env, result, category, |
| 205 ConvertUTF8ToJavaString(env, suggestion.id().id_within_category()), |
205 ConvertUTF16ToJavaString(env, suggestion.title()), | 206 ConvertUTF16ToJavaString(env, suggestion.title()), |
206 ConvertUTF16ToJavaString(env, suggestion.publisher_name()), | 207 ConvertUTF16ToJavaString(env, suggestion.publisher_name()), |
207 ConvertUTF16ToJavaString(env, suggestion.snippet_text()), | 208 ConvertUTF16ToJavaString(env, suggestion.snippet_text()), |
208 ConvertUTF8ToJavaString(env, suggestion.url().spec()), | 209 ConvertUTF8ToJavaString(env, suggestion.url().spec()), |
209 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()), | 210 ConvertUTF8ToJavaString(env, suggestion.amp_url().spec()), |
210 suggestion.publish_date().ToJavaTime(), suggestion.score(), | 211 suggestion.publish_date().ToJavaTime(), suggestion.score(), |
211 static_cast<int>(info->card_layout())); | 212 static_cast<int>(info->card_layout())); |
212 } | 213 } |
213 return result; | 214 return result; |
214 } | 215 } |
215 | 216 |
216 void NTPSnippetsBridge::FetchSuggestionImage( | 217 void NTPSnippetsBridge::FetchSuggestionImage( |
217 JNIEnv* env, | 218 JNIEnv* env, |
218 const JavaParamRef<jobject>& obj, | 219 const JavaParamRef<jobject>& obj, |
219 const JavaParamRef<jstring>& suggestion_id, | 220 jint category, |
| 221 const JavaParamRef<jstring>& id_within_category, |
220 const JavaParamRef<jobject>& j_callback) { | 222 const JavaParamRef<jobject>& j_callback) { |
221 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); | 223 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); |
222 content_suggestions_service_->FetchSuggestionImage( | 224 content_suggestions_service_->FetchSuggestionImage( |
223 ConvertJavaStringToUTF8(env, suggestion_id), | 225 ContentSuggestion::ID(CategoryFromIDValue(category), |
| 226 ConvertJavaStringToUTF8(env, id_within_category)), |
224 base::Bind(&NTPSnippetsBridge::OnImageFetched, | 227 base::Bind(&NTPSnippetsBridge::OnImageFetched, |
225 weak_ptr_factory_.GetWeakPtr(), callback)); | 228 weak_ptr_factory_.GetWeakPtr(), callback)); |
226 } | 229 } |
227 | 230 |
228 void NTPSnippetsBridge::DismissSuggestion( | 231 void NTPSnippetsBridge::DismissSuggestion( |
229 JNIEnv* env, | 232 JNIEnv* env, |
230 const JavaParamRef<jobject>& obj, | 233 const JavaParamRef<jobject>& obj, |
231 const JavaParamRef<jstring>& suggestion_id) { | 234 jint category, |
| 235 const JavaParamRef<jstring>& id_within_category) { |
232 content_suggestions_service_->DismissSuggestion( | 236 content_suggestions_service_->DismissSuggestion( |
233 ConvertJavaStringToUTF8(env, suggestion_id)); | 237 ContentSuggestion::ID(CategoryFromIDValue(category), |
| 238 ConvertJavaStringToUTF8(env, id_within_category))); |
234 } | 239 } |
235 | 240 |
236 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, | 241 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, |
237 const JavaParamRef<jobject>& obj, | 242 const JavaParamRef<jobject>& obj, |
238 jint category) { | 243 jint category) { |
239 content_suggestions_service_->DismissCategory(CategoryFromIDValue(category)); | 244 content_suggestions_service_->DismissCategory(CategoryFromIDValue(category)); |
240 } | 245 } |
241 | 246 |
242 void NTPSnippetsBridge::GetURLVisited(JNIEnv* env, | 247 void NTPSnippetsBridge::GetURLVisited(JNIEnv* env, |
243 const JavaParamRef<jobject>& obj, | 248 const JavaParamRef<jobject>& obj, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 if (observer_.is_null()) | 355 if (observer_.is_null()) |
351 return; | 356 return; |
352 | 357 |
353 JNIEnv* env = base::android::AttachCurrentThread(); | 358 JNIEnv* env = base::android::AttachCurrentThread(); |
354 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_, | 359 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_, |
355 static_cast<int>(category.id()), | 360 static_cast<int>(category.id()), |
356 static_cast<int>(new_status)); | 361 static_cast<int>(new_status)); |
357 } | 362 } |
358 | 363 |
359 void NTPSnippetsBridge::OnSuggestionInvalidated( | 364 void NTPSnippetsBridge::OnSuggestionInvalidated( |
360 Category category, | 365 const ContentSuggestion::ID& suggestion_id) { |
361 const std::string& suggestion_id) { | |
362 if (observer_.is_null()) | 366 if (observer_.is_null()) |
363 return; | 367 return; |
364 | 368 |
365 JNIEnv* env = base::android::AttachCurrentThread(); | 369 JNIEnv* env = base::android::AttachCurrentThread(); |
366 Java_SnippetsBridge_onSuggestionInvalidated( | 370 Java_SnippetsBridge_onSuggestionInvalidated( |
367 env, observer_.obj(), static_cast<int>(category.id()), | 371 env, observer_.obj(), static_cast<int>(suggestion_id.category().id()), |
368 ConvertUTF8ToJavaString(env, suggestion_id).obj()); | 372 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); |
369 } | 373 } |
370 | 374 |
371 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 375 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
372 observer_.Reset(); | 376 observer_.Reset(); |
373 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 377 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
374 } | 378 } |
375 | 379 |
376 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 380 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
377 const gfx::Image& image) { | 381 const gfx::Image& image) { |
378 ScopedJavaLocalRef<jobject> j_bitmap; | 382 ScopedJavaLocalRef<jobject> j_bitmap; |
379 if (!image.IsEmpty()) | 383 if (!image.IsEmpty()) |
380 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 384 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
381 | 385 |
382 base::android::RunCallbackAndroid(callback, j_bitmap); | 386 base::android::RunCallbackAndroid(callback, j_bitmap); |
383 } | 387 } |
384 | 388 |
385 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { | 389 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { |
386 return content_suggestions_service_->category_factory()->FromIDValue(id); | 390 return content_suggestions_service_->category_factory()->FromIDValue(id); |
387 } | 391 } |
388 | 392 |
389 // static | 393 // static |
390 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 394 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
391 return RegisterNativesImpl(env); | 395 return RegisterNativesImpl(env); |
392 } | 396 } |
OLD | NEW |