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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 GetRemoteSuggestionsScheduler(); | 141 GetRemoteSuggestionsScheduler(); |
142 // Can be null if the feature has been disabled but the scheduler has not been | 142 // Can be null if the feature has been disabled but the scheduler has not been |
143 // unregistered yet. The next start should unregister it. | 143 // unregistered yet. The next start should unregister it. |
144 if (!scheduler) { | 144 if (!scheduler) { |
145 return; | 145 return; |
146 } | 146 } |
147 | 147 |
148 scheduler->RescheduleFetching(); | 148 scheduler->RescheduleFetching(); |
149 } | 149 } |
150 | 150 |
151 static void OnSuggestionTargetVisited(JNIEnv* env, | |
152 const JavaParamRef<jclass>& caller, | |
153 jint j_category_id, | |
154 jlong visit_time_ms) { | |
155 ntp_snippets::metrics::OnSuggestionTargetVisited( | |
156 Category::FromIDValue(j_category_id), | |
157 base::TimeDelta::FromMilliseconds(visit_time_ms)); | |
158 } | |
159 | |
160 static void SetRemoteSuggestionsEnabled(JNIEnv* env, | 151 static void SetRemoteSuggestionsEnabled(JNIEnv* env, |
161 const JavaParamRef<jclass>& caller, | 152 const JavaParamRef<jclass>& caller, |
162 jboolean enabled) { | 153 jboolean enabled) { |
163 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 154 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
164 ContentSuggestionsServiceFactory::GetForProfile( | 155 ContentSuggestionsServiceFactory::GetForProfile( |
165 ProfileManager::GetLastUsedProfile()); | 156 ProfileManager::GetLastUsedProfile()); |
166 if (!content_suggestions_service) | 157 if (!content_suggestions_service) |
167 return; | 158 return; |
168 | 159 |
169 content_suggestions_service->SetRemoteSuggestionsEnabled(enabled); | 160 content_suggestions_service->SetRemoteSuggestionsEnabled(enabled); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 | 375 |
385 ntp_snippets::metrics::OnCategoryDismissed(category); | 376 ntp_snippets::metrics::OnCategoryDismissed(category); |
386 } | 377 } |
387 | 378 |
388 void NTPSnippetsBridge::RestoreDismissedCategories( | 379 void NTPSnippetsBridge::RestoreDismissedCategories( |
389 JNIEnv* env, | 380 JNIEnv* env, |
390 const JavaParamRef<jobject>& obj) { | 381 const JavaParamRef<jobject>& obj) { |
391 content_suggestions_service_->RestoreDismissedCategories(); | 382 content_suggestions_service_->RestoreDismissedCategories(); |
392 } | 383 } |
393 | 384 |
394 void NTPSnippetsBridge::OnPageShown( | |
395 JNIEnv* env, | |
396 const JavaParamRef<jobject>& obj, | |
397 const JavaParamRef<jintArray>& jcategories, | |
398 const JavaParamRef<jintArray>& jsuggestions_per_category) { | |
399 std::vector<int> categories_int; | |
400 JavaIntArrayToIntVector(env, jcategories, &categories_int); | |
401 std::vector<int> suggestions_per_category_int; | |
402 JavaIntArrayToIntVector(env, jsuggestions_per_category, | |
403 &suggestions_per_category_int); | |
404 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); | |
405 std::vector<std::pair<Category, int>> suggestions_per_category; | |
406 for (size_t i = 0; i < categories_int.size(); i++) { | |
407 suggestions_per_category.push_back( | |
408 std::make_pair(Category::FromIDValue(categories_int[i]), | |
409 suggestions_per_category_int[i])); | |
410 } | |
411 ntp_snippets::metrics::OnPageShown(suggestions_per_category); | |
412 content_suggestions_service_->user_classifier()->OnEvent( | |
413 ntp_snippets::UserClassifier::Metric::NTP_OPENED); | |
414 } | |
415 | |
416 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, | |
417 const JavaParamRef<jobject>& obj, | |
418 jint global_position, | |
419 jint j_category_id, | |
420 jint position_in_category, | |
421 jlong publish_timestamp_ms, | |
422 jfloat score, | |
423 jlong fetch_timestamp_ms) { | |
424 ntp_snippets::metrics::OnSuggestionShown( | |
425 global_position, Category::FromIDValue(j_category_id), | |
426 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), | |
427 score, base::Time::FromJavaTime(fetch_timestamp_ms)); | |
428 if (global_position == 0) { | |
429 content_suggestions_service_->user_classifier()->OnEvent( | |
430 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); | |
431 } | |
432 } | |
433 | |
434 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, | |
435 const JavaParamRef<jobject>& obj, | |
436 jint global_position, | |
437 jint j_category_id, | |
438 jint category_index, | |
439 jint position_in_category, | |
440 jlong publish_timestamp_ms, | |
441 jfloat score, | |
442 int windowOpenDisposition) { | |
443 const Category category = Category::FromIDValue(j_category_id); | |
444 ntp_snippets::metrics::OnSuggestionOpened( | |
445 global_position, category, category_index, position_in_category, | |
446 base::Time::FromJavaTime(publish_timestamp_ms), score, | |
447 static_cast<WindowOpenDisposition>(windowOpenDisposition)); | |
448 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and | |
449 // notify the ranker and the classifier there instead. Do not expose both of | |
450 // them at all. See crbug.com/674080. | |
451 content_suggestions_service_->category_ranker()->OnSuggestionOpened(category); | |
452 content_suggestions_service_->user_classifier()->OnEvent( | |
453 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | |
454 } | |
455 | |
456 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, | |
457 const JavaParamRef<jobject>& obj, | |
458 jint global_position, | |
459 jint j_category_id, | |
460 jint position_in_category, | |
461 jlong publish_timestamp_ms, | |
462 jfloat score) { | |
463 ntp_snippets::metrics::OnSuggestionMenuOpened( | |
464 global_position, Category::FromIDValue(j_category_id), | |
465 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), | |
466 score); | |
467 } | |
468 | |
469 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, | |
470 const JavaParamRef<jobject>& obj, | |
471 jint j_category_id, | |
472 jint position) { | |
473 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), | |
474 position); | |
475 } | |
476 | |
477 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, | |
478 const JavaParamRef<jobject>& obj, | |
479 jint j_category_id, | |
480 jint position) { | |
481 ntp_snippets::metrics::OnMoreButtonClicked( | |
482 Category::FromIDValue(j_category_id), position); | |
483 content_suggestions_service_->user_classifier()->OnEvent( | |
484 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | |
485 } | |
486 | |
487 void NTPSnippetsBridge::OnNTPInitialized(JNIEnv* env, | |
488 const JavaParamRef<jobject>& obj) { | |
489 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | |
490 GetRemoteSuggestionsScheduler(); | |
491 // Can be null if the feature has been disabled but the scheduler has not been | |
492 // unregistered yet. The next start should unregister it. | |
493 if (!scheduler) { | |
494 return; | |
495 } | |
496 | |
497 scheduler->OnNTPOpened(); | |
498 } | |
499 | |
500 void NTPSnippetsBridge::OnColdStart(JNIEnv* env, | |
501 const JavaParamRef<jobject>& obj) { | |
502 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | |
503 GetRemoteSuggestionsScheduler(); | |
504 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | |
505 if (!scheduler) { | |
506 return; | |
507 } | |
508 scheduler->OnBrowserColdStart(); | |
509 } | |
510 | |
511 void NTPSnippetsBridge::OnActivityWarmResumed( | |
512 JNIEnv* env, | |
513 const JavaParamRef<jobject>& obj) { | |
514 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | |
515 GetRemoteSuggestionsScheduler(); | |
516 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | |
517 if (!scheduler) { | |
518 return; | |
519 } | |
520 scheduler->OnBrowserForegrounded(); | |
521 } | |
522 | |
523 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 385 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
524 | 386 |
525 void NTPSnippetsBridge::OnNewSuggestions(Category category) { | 387 void NTPSnippetsBridge::OnNewSuggestions(Category category) { |
526 JNIEnv* env = AttachCurrentThread(); | 388 JNIEnv* env = AttachCurrentThread(); |
527 Java_SnippetsBridge_onNewSuggestions(env, bridge_, | 389 Java_SnippetsBridge_onNewSuggestions(env, bridge_, |
528 static_cast<int>(category.id())); | 390 static_cast<int>(category.id())); |
529 } | 391 } |
530 | 392 |
531 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 393 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
532 CategoryStatus new_status) { | 394 CategoryStatus new_status) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 JNIEnv* env = AttachCurrentThread(); | 433 JNIEnv* env = AttachCurrentThread(); |
572 Java_SnippetsBridge_onMoreSuggestions( | 434 Java_SnippetsBridge_onMoreSuggestions( |
573 env, bridge_, category.id(), | 435 env, bridge_, category.id(), |
574 ToJavaSuggestionList(env, category, suggestions)); | 436 ToJavaSuggestionList(env, category, suggestions)); |
575 } | 437 } |
576 | 438 |
577 // static | 439 // static |
578 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 440 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
579 return RegisterNativesImpl(env); | 441 return RegisterNativesImpl(env); |
580 } | 442 } |
OLD | NEW |