| 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 <algorithm> |
| 8 #include <utility> | 9 #include <utility> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/android/callback_android.h" | 12 #include "base/android/callback_android.h" |
| 12 #include "base/android/jni_android.h" | 13 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_array.h" | 14 #include "base/android/jni_array.h" |
| 14 #include "base/android/jni_string.h" | 15 #include "base/android/jni_string.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/stl_util.h" |
| 16 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
| 18 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 20 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/profiles/profile_android.h" | 22 #include "chrome/browser/profiles/profile_android.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "components/history/core/browser/history_service.h" | 24 #include "components/history/core/browser/history_service.h" |
| 23 #include "components/ntp_snippets/content_suggestion.h" | 25 #include "components/ntp_snippets/content_suggestion.h" |
| 24 #include "components/ntp_snippets/content_suggestions_metrics.h" | 26 #include "components/ntp_snippets/content_suggestions_metrics.h" |
| 25 #include "components/ntp_snippets/pref_names.h" | 27 #include "components/ntp_snippets/pref_names.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 const base::android::JavaParamRef<jobject>& obj) { | 259 const base::android::JavaParamRef<jobject>& obj) { |
| 258 content_suggestions_service_->ReloadSuggestions(); | 260 content_suggestions_service_->ReloadSuggestions(); |
| 259 } | 261 } |
| 260 | 262 |
| 261 void NTPSnippetsBridge::DismissSuggestion( | 263 void NTPSnippetsBridge::DismissSuggestion( |
| 262 JNIEnv* env, | 264 JNIEnv* env, |
| 263 const JavaParamRef<jobject>& obj, | 265 const JavaParamRef<jobject>& obj, |
| 264 const JavaParamRef<jstring>& jurl, | 266 const JavaParamRef<jstring>& jurl, |
| 265 jint global_position, | 267 jint global_position, |
| 266 jint j_category_id, | 268 jint j_category_id, |
| 267 jint category_position, | 269 jint position_in_category, |
| 268 const JavaParamRef<jstring>& id_within_category) { | 270 const JavaParamRef<jstring>& id_within_category) { |
| 269 Category category = Category::FromIDValue(j_category_id); | 271 Category category = Category::FromIDValue(j_category_id); |
| 270 | 272 |
| 271 content_suggestions_service_->DismissSuggestion(ContentSuggestion::ID( | 273 content_suggestions_service_->DismissSuggestion(ContentSuggestion::ID( |
| 272 category, ConvertJavaStringToUTF8(env, id_within_category))); | 274 category, ConvertJavaStringToUTF8(env, id_within_category))); |
| 273 | 275 |
| 274 history_service_->QueryURL( | 276 history_service_->QueryURL( |
| 275 GURL(ConvertJavaStringToUTF8(env, jurl)), /*want_visits=*/false, | 277 GURL(ConvertJavaStringToUTF8(env, jurl)), /*want_visits=*/false, |
| 276 base::Bind( | 278 base::Bind( |
| 277 [](int global_position, Category category, int category_position, | 279 [](int global_position, Category category, int position_in_category, |
| 278 bool success, const history::URLRow& row, | 280 bool success, const history::URLRow& row, |
| 279 const history::VisitVector& visit_vector) { | 281 const history::VisitVector& visit_vector) { |
| 280 bool visited = success && row.visit_count() != 0; | 282 bool visited = success && row.visit_count() != 0; |
| 281 ntp_snippets::metrics::OnSuggestionDismissed( | 283 ntp_snippets::metrics::OnSuggestionDismissed( |
| 282 global_position, category, category_position, visited); | 284 global_position, category, position_in_category, visited); |
| 283 }, | 285 }, |
| 284 global_position, category, category_position), | 286 global_position, category, position_in_category), |
| 285 &tracker_); | 287 &tracker_); |
| 286 } | 288 } |
| 287 | 289 |
| 288 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, | 290 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, |
| 289 const JavaParamRef<jobject>& obj, | 291 const JavaParamRef<jobject>& obj, |
| 290 jint j_category_id) { | 292 jint j_category_id) { |
| 291 Category category = Category::FromIDValue(j_category_id); | 293 Category category = Category::FromIDValue(j_category_id); |
| 292 | 294 |
| 293 content_suggestions_service_->DismissCategory(category); | 295 content_suggestions_service_->DismissCategory(category); |
| 294 | 296 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 320 } | 322 } |
| 321 ntp_snippets::metrics::OnPageShown(suggestions_per_category); | 323 ntp_snippets::metrics::OnPageShown(suggestions_per_category); |
| 322 content_suggestions_service_->user_classifier()->OnEvent( | 324 content_suggestions_service_->user_classifier()->OnEvent( |
| 323 ntp_snippets::UserClassifier::Metric::NTP_OPENED); | 325 ntp_snippets::UserClassifier::Metric::NTP_OPENED); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, | 328 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, |
| 327 const JavaParamRef<jobject>& obj, | 329 const JavaParamRef<jobject>& obj, |
| 328 jint global_position, | 330 jint global_position, |
| 329 jint j_category_id, | 331 jint j_category_id, |
| 330 jint category_position, | 332 jint position_in_category, |
| 331 jlong publish_timestamp_ms, | 333 jlong publish_timestamp_ms, |
| 332 jfloat score) { | 334 jfloat score) { |
| 333 PrefService* pref_service = ProfileManager::GetLastUsedProfile()->GetPrefs(); | 335 PrefService* pref_service = ProfileManager::GetLastUsedProfile()->GetPrefs(); |
| 334 base::Time last_background_fetch_time = | 336 base::Time last_background_fetch_time = |
| 335 base::Time::FromInternalValue(pref_service->GetInt64( | 337 base::Time::FromInternalValue(pref_service->GetInt64( |
| 336 ntp_snippets::prefs::kLastSuccessfulBackgroundFetchTime)); | 338 ntp_snippets::prefs::kLastSuccessfulBackgroundFetchTime)); |
| 337 | 339 |
| 338 ntp_snippets::metrics::OnSuggestionShown( | 340 ntp_snippets::metrics::OnSuggestionShown( |
| 339 global_position, Category::FromIDValue(j_category_id), category_position, | 341 global_position, Category::FromIDValue(j_category_id), |
| 340 base::Time::FromJavaTime(publish_timestamp_ms), | 342 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
| 341 last_background_fetch_time, score); | 343 last_background_fetch_time, score); |
| 342 if (global_position == 0) { | 344 if (global_position == 0) { |
| 343 content_suggestions_service_->user_classifier()->OnEvent( | 345 content_suggestions_service_->user_classifier()->OnEvent( |
| 344 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); | 346 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); |
| 345 } | 347 } |
| 346 } | 348 } |
| 347 | 349 |
| 348 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, | 350 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, |
| 349 const JavaParamRef<jobject>& obj, | 351 const JavaParamRef<jobject>& obj, |
| 350 jint global_position, | 352 jint global_position, |
| 351 jint j_category_id, | 353 jint j_category_id, |
| 352 jint category_position, | 354 jint position_in_category, |
| 353 jlong publish_timestamp_ms, | 355 jlong publish_timestamp_ms, |
| 354 jfloat score, | 356 jfloat score, |
| 355 int windowOpenDisposition) { | 357 int windowOpenDisposition) { |
| 358 const Category category = Category::FromIDValue(j_category_id); |
| 359 // TODO(crbug.com/678623): Instead of providing content_suggestions_service_ |
| 360 // as a parameter, propagate category positition from UI through the bridge. |
| 356 ntp_snippets::metrics::OnSuggestionOpened( | 361 ntp_snippets::metrics::OnSuggestionOpened( |
| 357 global_position, Category::FromIDValue(j_category_id), category_position, | 362 content_suggestions_service_, global_position, category, |
| 358 base::Time::FromJavaTime(publish_timestamp_ms), score, | 363 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
| 359 static_cast<WindowOpenDisposition>(windowOpenDisposition)); | 364 score, static_cast<WindowOpenDisposition>(windowOpenDisposition)); |
| 360 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and | 365 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and |
| 361 // notify the ranker and the classifier there instead. Do not expose both of | 366 // notify the ranker and the classifier there instead. Do not expose both of |
| 362 // them at all. See crbug.com/674080. | 367 // them at all. See crbug.com/674080. |
| 363 content_suggestions_service_->category_ranker()->OnSuggestionOpened( | 368 content_suggestions_service_->category_ranker()->OnSuggestionOpened(category); |
| 364 Category::FromIDValue(j_category_id)); | |
| 365 content_suggestions_service_->user_classifier()->OnEvent( | 369 content_suggestions_service_->user_classifier()->OnEvent( |
| 366 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | 370 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); |
| 367 } | 371 } |
| 368 | 372 |
| 369 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, | 373 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, |
| 370 const JavaParamRef<jobject>& obj, | 374 const JavaParamRef<jobject>& obj, |
| 371 jint global_position, | 375 jint global_position, |
| 372 jint j_category_id, | 376 jint j_category_id, |
| 373 jint category_position, | 377 jint position_in_category, |
| 374 jlong publish_timestamp_ms, | 378 jlong publish_timestamp_ms, |
| 375 jfloat score) { | 379 jfloat score) { |
| 376 ntp_snippets::metrics::OnSuggestionMenuOpened( | 380 ntp_snippets::metrics::OnSuggestionMenuOpened( |
| 377 global_position, Category::FromIDValue(j_category_id), category_position, | 381 global_position, Category::FromIDValue(j_category_id), |
| 378 base::Time::FromJavaTime(publish_timestamp_ms), score); | 382 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
| 383 score); |
| 379 } | 384 } |
| 380 | 385 |
| 381 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, | 386 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, |
| 382 const JavaParamRef<jobject>& obj, | 387 const JavaParamRef<jobject>& obj, |
| 383 jint j_category_id, | 388 jint j_category_id, |
| 384 jint position) { | 389 jint position) { |
| 385 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), | 390 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), |
| 386 position); | 391 position); |
| 387 } | 392 } |
| 388 | 393 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 JNIEnv* env = AttachCurrentThread(); | 474 JNIEnv* env = AttachCurrentThread(); |
| 470 Java_SnippetsBridge_onMoreSuggestions( | 475 Java_SnippetsBridge_onMoreSuggestions( |
| 471 env, observer_, category.id(), | 476 env, observer_, category.id(), |
| 472 ToJavaSuggestionList(env, category, suggestions)); | 477 ToJavaSuggestionList(env, category, suggestions)); |
| 473 } | 478 } |
| 474 | 479 |
| 475 // static | 480 // static |
| 476 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 481 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 477 return RegisterNativesImpl(env); | 482 return RegisterNativesImpl(env); |
| 478 } | 483 } |
| OLD | NEW |