| 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 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 profile, ServiceAccessType::EXPLICIT_ACCESS); | 168 profile, ServiceAccessType::EXPLICIT_ACCESS); |
| 169 content_suggestions_service_observer_.Add(content_suggestions_service_); | 169 content_suggestions_service_observer_.Add(content_suggestions_service_); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 172 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 173 delete this; | 173 delete this; |
| 174 } | 174 } |
| 175 | 175 |
| 176 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( | 176 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( |
| 177 JNIEnv* env, | 177 JNIEnv* env, |
| 178 const base::android::JavaParamRef<jobject>& obj) { | 178 const JavaParamRef<jobject>& obj) { |
| 179 std::vector<int> category_ids; | 179 std::vector<int> category_ids; |
| 180 for (Category category : content_suggestions_service_->GetCategories()) { | 180 for (Category category : content_suggestions_service_->GetCategories()) { |
| 181 category_ids.push_back(category.id()); | 181 category_ids.push_back(category.id()); |
| 182 } | 182 } |
| 183 return ToJavaIntArray(env, category_ids); | 183 return ToJavaIntArray(env, category_ids); |
| 184 } | 184 } |
| 185 | 185 |
| 186 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 186 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
| 187 const JavaParamRef<jobject>& obj, | 187 const JavaParamRef<jobject>& obj, |
| 188 jint j_category_id) { | 188 jint j_category_id) { |
| 189 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 189 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
| 190 Category::FromIDValue(j_category_id))); | 190 Category::FromIDValue(j_category_id))); |
| 191 } | 191 } |
| 192 | 192 |
| 193 base::android::ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( | 193 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( |
| 194 JNIEnv* env, | 194 JNIEnv* env, |
| 195 const base::android::JavaParamRef<jobject>& obj, | 195 const JavaParamRef<jobject>& obj, |
| 196 jint j_category_id) { | 196 jint j_category_id) { |
| 197 base::Optional<CategoryInfo> info = | 197 base::Optional<CategoryInfo> info = |
| 198 content_suggestions_service_->GetCategoryInfo( | 198 content_suggestions_service_->GetCategoryInfo( |
| 199 Category::FromIDValue(j_category_id)); | 199 Category::FromIDValue(j_category_id)); |
| 200 if (!info) { | 200 if (!info) { |
| 201 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); | 201 return ScopedJavaLocalRef<jobject>(env, nullptr); |
| 202 } | 202 } |
| 203 return Java_SnippetsBridge_createSuggestionsCategoryInfo( | 203 return Java_SnippetsBridge_createSuggestionsCategoryInfo( |
| 204 env, j_category_id, ConvertUTF16ToJavaString(env, info->title()), | 204 env, j_category_id, ConvertUTF16ToJavaString(env, info->title()), |
| 205 static_cast<int>(info->card_layout()), | 205 static_cast<int>(info->card_layout()), |
| 206 static_cast<int>(info->additional_action()), info->show_if_empty(), | 206 static_cast<int>(info->additional_action()), info->show_if_empty(), |
| 207 ConvertUTF16ToJavaString(env, info->no_suggestions_message())); | 207 ConvertUTF16ToJavaString(env, info->no_suggestions_message())); |
| 208 } | 208 } |
| 209 | 209 |
| 210 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( | 210 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( |
| 211 JNIEnv* env, | 211 JNIEnv* env, |
| 212 const base::android::JavaParamRef<jobject>& obj, | 212 const JavaParamRef<jobject>& obj, |
| 213 jint j_category_id) { | 213 jint j_category_id) { |
| 214 Category category = Category::FromIDValue(j_category_id); | 214 Category category = Category::FromIDValue(j_category_id); |
| 215 return ToJavaSuggestionList( | 215 return ToJavaSuggestionList( |
| 216 env, category, | 216 env, category, |
| 217 content_suggestions_service_->GetSuggestionsForCategory(category)); | 217 content_suggestions_service_->GetSuggestionsForCategory(category)); |
| 218 } | 218 } |
| 219 | 219 |
| 220 void NTPSnippetsBridge::FetchSuggestionImage( | 220 void NTPSnippetsBridge::FetchSuggestionImage( |
| 221 JNIEnv* env, | 221 JNIEnv* env, |
| 222 const JavaParamRef<jobject>& obj, | 222 const JavaParamRef<jobject>& obj, |
| 223 jint j_category_id, | 223 jint j_category_id, |
| 224 const JavaParamRef<jstring>& id_within_category, | 224 const JavaParamRef<jstring>& id_within_category, |
| 225 const JavaParamRef<jobject>& j_callback) { | 225 const JavaParamRef<jobject>& j_callback) { |
| 226 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); | 226 ScopedJavaGlobalRef<jobject> callback(j_callback); |
| 227 content_suggestions_service_->FetchSuggestionImage( | 227 content_suggestions_service_->FetchSuggestionImage( |
| 228 ContentSuggestion::ID(Category::FromIDValue(j_category_id), | 228 ContentSuggestion::ID(Category::FromIDValue(j_category_id), |
| 229 ConvertJavaStringToUTF8(env, id_within_category)), | 229 ConvertJavaStringToUTF8(env, id_within_category)), |
| 230 base::Bind(&NTPSnippetsBridge::OnImageFetched, | 230 base::Bind(&NTPSnippetsBridge::OnImageFetched, |
| 231 weak_ptr_factory_.GetWeakPtr(), callback)); | 231 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 232 } | 232 } |
| 233 | 233 |
| 234 void NTPSnippetsBridge::FetchSuggestionFavicon( |
| 235 JNIEnv* env, |
| 236 const JavaParamRef<jobject>& obj, |
| 237 jint j_category_id, |
| 238 const JavaParamRef<jstring>& id_within_category, |
| 239 jint j_minimum_size_px, |
| 240 jint j_desired_size_px, |
| 241 const JavaParamRef<jobject>& j_callback) { |
| 242 ScopedJavaGlobalRef<jobject> callback(j_callback); |
| 243 content_suggestions_service_->FetchSuggestionFavicon( |
| 244 ContentSuggestion::ID(Category::FromIDValue(j_category_id), |
| 245 ConvertJavaStringToUTF8(env, id_within_category)), |
| 246 j_minimum_size_px, j_desired_size_px, |
| 247 base::Bind(&NTPSnippetsBridge::OnImageFetched, |
| 248 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 249 } |
| 250 |
| 234 void NTPSnippetsBridge::Fetch( | 251 void NTPSnippetsBridge::Fetch( |
| 235 JNIEnv* env, | 252 JNIEnv* env, |
| 236 const JavaParamRef<jobject>& obj, | 253 const JavaParamRef<jobject>& obj, |
| 237 jint j_category_id, | 254 jint j_category_id, |
| 238 const JavaParamRef<jobjectArray>& j_displayed_suggestions) { | 255 const JavaParamRef<jobjectArray>& j_displayed_suggestions) { |
| 239 std::vector<std::string> known_suggestion_ids; | 256 std::vector<std::string> known_suggestion_ids; |
| 240 AppendJavaStringArrayToStringVector(env, j_displayed_suggestions, | 257 AppendJavaStringArrayToStringVector(env, j_displayed_suggestions, |
| 241 &known_suggestion_ids); | 258 &known_suggestion_ids); |
| 242 | 259 |
| 243 Category category = Category::FromIDValue(j_category_id); | 260 Category category = Category::FromIDValue(j_category_id); |
| 244 content_suggestions_service_->Fetch( | 261 content_suggestions_service_->Fetch( |
| 245 category, | 262 category, |
| 246 std::set<std::string>(known_suggestion_ids.begin(), | 263 std::set<std::string>(known_suggestion_ids.begin(), |
| 247 known_suggestion_ids.end()), | 264 known_suggestion_ids.end()), |
| 248 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, | 265 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, |
| 249 weak_ptr_factory_.GetWeakPtr(), category)); | 266 weak_ptr_factory_.GetWeakPtr(), category)); |
| 250 } | 267 } |
| 251 | 268 |
| 252 void NTPSnippetsBridge::ReloadSuggestions( | 269 void NTPSnippetsBridge::ReloadSuggestions(JNIEnv* env, |
| 253 JNIEnv* env, | 270 const JavaParamRef<jobject>& obj) { |
| 254 const base::android::JavaParamRef<jobject>& obj) { | |
| 255 content_suggestions_service_->ReloadSuggestions(); | 271 content_suggestions_service_->ReloadSuggestions(); |
| 256 } | 272 } |
| 257 | 273 |
| 258 void NTPSnippetsBridge::DismissSuggestion( | 274 void NTPSnippetsBridge::DismissSuggestion( |
| 259 JNIEnv* env, | 275 JNIEnv* env, |
| 260 const JavaParamRef<jobject>& obj, | 276 const JavaParamRef<jobject>& obj, |
| 261 const JavaParamRef<jstring>& jurl, | 277 const JavaParamRef<jstring>& jurl, |
| 262 jint global_position, | 278 jint global_position, |
| 263 jint j_category_id, | 279 jint j_category_id, |
| 264 jint position_in_category, | 280 jint position_in_category, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, | 400 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, |
| 385 const JavaParamRef<jobject>& obj, | 401 const JavaParamRef<jobject>& obj, |
| 386 jint j_category_id, | 402 jint j_category_id, |
| 387 jint position) { | 403 jint position) { |
| 388 ntp_snippets::metrics::OnMoreButtonClicked( | 404 ntp_snippets::metrics::OnMoreButtonClicked( |
| 389 Category::FromIDValue(j_category_id), position); | 405 Category::FromIDValue(j_category_id), position); |
| 390 content_suggestions_service_->user_classifier()->OnEvent( | 406 content_suggestions_service_->user_classifier()->OnEvent( |
| 391 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | 407 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); |
| 392 } | 408 } |
| 393 | 409 |
| 394 void NTPSnippetsBridge::OnNTPInitialized( | 410 void NTPSnippetsBridge::OnNTPInitialized(JNIEnv* env, |
| 395 JNIEnv* env, | 411 const JavaParamRef<jobject>& obj) { |
| 396 const base::android::JavaParamRef<jobject>& obj) { | |
| 397 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 412 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 398 GetRemoteSuggestionsScheduler(); | 413 GetRemoteSuggestionsScheduler(); |
| 399 // Can be null if the feature has been disabled but the scheduler has not been | 414 // Can be null if the feature has been disabled but the scheduler has not been |
| 400 // unregistered yet. The next start should unregister it. | 415 // unregistered yet. The next start should unregister it. |
| 401 if (!scheduler) { | 416 if (!scheduler) { |
| 402 return; | 417 return; |
| 403 } | 418 } |
| 404 | 419 |
| 405 scheduler->OnNTPOpened(); | 420 scheduler->OnNTPOpened(); |
| 406 } | 421 } |
| 407 | 422 |
| 408 void NTPSnippetsBridge::OnColdStart( | 423 void NTPSnippetsBridge::OnColdStart(JNIEnv* env, |
| 409 JNIEnv* env, | 424 const JavaParamRef<jobject>& obj) { |
| 410 const base::android::JavaParamRef<jobject>& obj) { | |
| 411 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 425 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 412 GetRemoteSuggestionsScheduler(); | 426 GetRemoteSuggestionsScheduler(); |
| 413 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | 427 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
| 414 if (!scheduler) { | 428 if (!scheduler) { |
| 415 return; | 429 return; |
| 416 } | 430 } |
| 417 scheduler->OnBrowserColdStart(); | 431 scheduler->OnBrowserColdStart(); |
| 418 } | 432 } |
| 419 | 433 |
| 420 void NTPSnippetsBridge::OnActivityWarmResumed( | 434 void NTPSnippetsBridge::OnActivityWarmResumed( |
| 421 JNIEnv* env, | 435 JNIEnv* env, |
| 422 const base::android::JavaParamRef<jobject>& obj) { | 436 const JavaParamRef<jobject>& obj) { |
| 423 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 437 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 424 GetRemoteSuggestionsScheduler(); | 438 GetRemoteSuggestionsScheduler(); |
| 425 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | 439 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
| 426 if (!scheduler) { | 440 if (!scheduler) { |
| 427 return; | 441 return; |
| 428 } | 442 } |
| 429 scheduler->OnBrowserForegrounded(); | 443 scheduler->OnBrowserForegrounded(); |
| 430 } | 444 } |
| 431 | 445 |
| 432 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 446 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 433 | 447 |
| 434 void NTPSnippetsBridge::OnNewSuggestions(Category category) { | 448 void NTPSnippetsBridge::OnNewSuggestions(Category category) { |
| 435 JNIEnv* env = base::android::AttachCurrentThread(); | 449 JNIEnv* env = AttachCurrentThread(); |
| 436 Java_SnippetsBridge_onNewSuggestions(env, bridge_, | 450 Java_SnippetsBridge_onNewSuggestions(env, bridge_, |
| 437 static_cast<int>(category.id())); | 451 static_cast<int>(category.id())); |
| 438 } | 452 } |
| 439 | 453 |
| 440 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 454 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
| 441 CategoryStatus new_status) { | 455 CategoryStatus new_status) { |
| 442 JNIEnv* env = base::android::AttachCurrentThread(); | 456 JNIEnv* env = AttachCurrentThread(); |
| 443 Java_SnippetsBridge_onCategoryStatusChanged(env, bridge_, | 457 Java_SnippetsBridge_onCategoryStatusChanged(env, bridge_, |
| 444 static_cast<int>(category.id()), | 458 static_cast<int>(category.id()), |
| 445 static_cast<int>(new_status)); | 459 static_cast<int>(new_status)); |
| 446 } | 460 } |
| 447 | 461 |
| 448 void NTPSnippetsBridge::OnSuggestionInvalidated( | 462 void NTPSnippetsBridge::OnSuggestionInvalidated( |
| 449 const ContentSuggestion::ID& suggestion_id) { | 463 const ContentSuggestion::ID& suggestion_id) { |
| 450 JNIEnv* env = base::android::AttachCurrentThread(); | 464 JNIEnv* env = AttachCurrentThread(); |
| 451 Java_SnippetsBridge_onSuggestionInvalidated( | 465 Java_SnippetsBridge_onSuggestionInvalidated( |
| 452 env, bridge_.obj(), static_cast<int>(suggestion_id.category().id()), | 466 env, bridge_.obj(), static_cast<int>(suggestion_id.category().id()), |
| 453 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); | 467 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); |
| 454 } | 468 } |
| 455 | 469 |
| 456 void NTPSnippetsBridge::OnFullRefreshRequired() { | 470 void NTPSnippetsBridge::OnFullRefreshRequired() { |
| 457 JNIEnv* env = base::android::AttachCurrentThread(); | 471 JNIEnv* env = AttachCurrentThread(); |
| 458 Java_SnippetsBridge_onFullRefreshRequired(env, bridge_.obj()); | 472 Java_SnippetsBridge_onFullRefreshRequired(env, bridge_.obj()); |
| 459 } | 473 } |
| 460 | 474 |
| 461 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 475 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
| 462 bridge_.Reset(); | 476 bridge_.Reset(); |
| 463 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 477 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
| 464 } | 478 } |
| 465 | 479 |
| 466 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 480 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 467 const gfx::Image& image) { | 481 const gfx::Image& image) { |
| 468 ScopedJavaLocalRef<jobject> j_bitmap; | 482 ScopedJavaLocalRef<jobject> j_bitmap; |
| 469 if (!image.IsEmpty()) { | 483 if (!image.IsEmpty()) { |
| 470 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 484 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 471 } | 485 } |
| 472 base::android::RunCallbackAndroid(callback, j_bitmap); | 486 RunCallbackAndroid(callback, j_bitmap); |
| 473 } | 487 } |
| 474 | 488 |
| 475 void NTPSnippetsBridge::OnSuggestionsFetched( | 489 void NTPSnippetsBridge::OnSuggestionsFetched( |
| 476 Category category, | 490 Category category, |
| 477 ntp_snippets::Status status, | 491 ntp_snippets::Status status, |
| 478 std::vector<ContentSuggestion> suggestions) { | 492 std::vector<ContentSuggestion> suggestions) { |
| 479 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. | 493 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. |
| 480 JNIEnv* env = AttachCurrentThread(); | 494 JNIEnv* env = AttachCurrentThread(); |
| 481 Java_SnippetsBridge_onMoreSuggestions( | 495 Java_SnippetsBridge_onMoreSuggestions( |
| 482 env, bridge_, category.id(), | 496 env, bridge_, category.id(), |
| 483 ToJavaSuggestionList(env, category, suggestions)); | 497 ToJavaSuggestionList(env, category, suggestions)); |
| 484 } | 498 } |
| 485 | 499 |
| 486 // static | 500 // static |
| 487 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 501 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 488 return RegisterNativesImpl(env); | 502 return RegisterNativesImpl(env); |
| 489 } | 503 } |
| OLD | NEW |