| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/ntp/suggestions_event_reporter_bridge.h" |
| 6 |
| 7 #include <jni.h> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" |
| 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "components/ntp_snippets/content_suggestions_metrics.h" |
| 18 #include "components/ntp_snippets/content_suggestions_service.h" |
| 19 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
| 20 #include "components/ntp_snippets/user_classifier.h" |
| 21 #include "jni/SuggestionsEventReporterBridge_jni.h" |
| 22 #include "ui/base/window_open_disposition.h" |
| 23 |
| 24 using base::android::AttachCurrentThread; |
| 25 using base::android::JavaParamRef; |
| 26 using ntp_snippets::Category; |
| 27 using ntp_snippets::UserClassifier; |
| 28 |
| 29 namespace { |
| 30 |
| 31 ntp_snippets::RemoteSuggestionsScheduler* GetRemoteSuggestionsScheduler() { |
| 32 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 33 ContentSuggestionsServiceFactory::GetForProfile( |
| 34 ProfileManager::GetLastUsedProfile()); |
| 35 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
| 36 if (!content_suggestions_service) { |
| 37 return nullptr; |
| 38 } |
| 39 return content_suggestions_service->remote_suggestions_scheduler(); |
| 40 } |
| 41 |
| 42 UserClassifier* GetUserClassifier() { |
| 43 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 44 ContentSuggestionsServiceFactory::GetForProfile( |
| 45 ProfileManager::GetLastUsedProfile()); |
| 46 DCHECK(content_suggestions_service); // TODO(dgn): remove |
| 47 |
| 48 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
| 49 if (!content_suggestions_service) { |
| 50 return nullptr; |
| 51 } |
| 52 return content_suggestions_service->user_classifier(); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 static void OnSuggestionTargetVisited(JNIEnv* env, |
| 58 const JavaParamRef<jclass>& caller, |
| 59 jint j_category_id, |
| 60 jlong visit_time_ms) { |
| 61 ntp_snippets::metrics::OnSuggestionTargetVisited( |
| 62 Category::FromIDValue(j_category_id), |
| 63 base::TimeDelta::FromMilliseconds(visit_time_ms)); |
| 64 } |
| 65 |
| 66 static void OnPageShown( |
| 67 JNIEnv* env, |
| 68 const JavaParamRef<jclass>& caller, |
| 69 const JavaParamRef<jintArray>& jcategories, |
| 70 const JavaParamRef<jintArray>& jsuggestions_per_category) { |
| 71 std::vector<int> categories_int; |
| 72 JavaIntArrayToIntVector(env, jcategories, &categories_int); |
| 73 std::vector<int> suggestions_per_category_int; |
| 74 JavaIntArrayToIntVector(env, jsuggestions_per_category, |
| 75 &suggestions_per_category_int); |
| 76 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); |
| 77 std::vector<std::pair<Category, int>> suggestions_per_category; |
| 78 for (size_t i = 0; i < categories_int.size(); i++) { |
| 79 suggestions_per_category.push_back( |
| 80 std::make_pair(Category::FromIDValue(categories_int[i]), |
| 81 suggestions_per_category_int[i])); |
| 82 } |
| 83 ntp_snippets::metrics::OnPageShown(suggestions_per_category); |
| 84 GetUserClassifier()->OnEvent(UserClassifier::Metric::NTP_OPENED); |
| 85 } |
| 86 |
| 87 static void OnSuggestionShown(JNIEnv* env, |
| 88 const JavaParamRef<jclass>& caller, |
| 89 jint global_position, |
| 90 jint j_category_id, |
| 91 jint position_in_category, |
| 92 jlong publish_timestamp_ms, |
| 93 jfloat score, |
| 94 jlong fetch_timestamp_ms) { |
| 95 ntp_snippets::metrics::OnSuggestionShown( |
| 96 global_position, Category::FromIDValue(j_category_id), |
| 97 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
| 98 score, base::Time::FromJavaTime(fetch_timestamp_ms)); |
| 99 if (global_position == 0) { |
| 100 GetUserClassifier()->OnEvent(UserClassifier::Metric::SUGGESTIONS_SHOWN); |
| 101 } |
| 102 } |
| 103 |
| 104 static void OnSuggestionOpened(JNIEnv* env, |
| 105 const JavaParamRef<jclass>& caller, |
| 106 jint global_position, |
| 107 jint j_category_id, |
| 108 jint category_index, |
| 109 jint position_in_category, |
| 110 jlong publish_timestamp_ms, |
| 111 jfloat score, |
| 112 int windowOpenDisposition) { |
| 113 const Category category = Category::FromIDValue(j_category_id); |
| 114 ntp_snippets::metrics::OnSuggestionOpened( |
| 115 global_position, category, category_index, position_in_category, |
| 116 base::Time::FromJavaTime(publish_timestamp_ms), score, |
| 117 static_cast<WindowOpenDisposition>(windowOpenDisposition)); |
| 118 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 119 ContentSuggestionsServiceFactory::GetForProfile( |
| 120 ProfileManager::GetLastUsedProfile()); |
| 121 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and |
| 122 // notify the ranker and the classifier there instead. Do not expose both of |
| 123 // them at all. See crbug.com/674080. |
| 124 content_suggestions_service->category_ranker()->OnSuggestionOpened(category); |
| 125 content_suggestions_service->user_classifier()->OnEvent( |
| 126 UserClassifier::Metric::SUGGESTIONS_USED); |
| 127 } |
| 128 |
| 129 static void OnSuggestionMenuOpened(JNIEnv* env, |
| 130 const JavaParamRef<jclass>& caller, |
| 131 jint global_position, |
| 132 jint j_category_id, |
| 133 jint position_in_category, |
| 134 jlong publish_timestamp_ms, |
| 135 jfloat score) { |
| 136 ntp_snippets::metrics::OnSuggestionMenuOpened( |
| 137 global_position, Category::FromIDValue(j_category_id), |
| 138 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
| 139 score); |
| 140 } |
| 141 |
| 142 static void OnMoreButtonShown(JNIEnv* env, |
| 143 const JavaParamRef<jclass>& caller, |
| 144 jint j_category_id, |
| 145 jint position) { |
| 146 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), |
| 147 position); |
| 148 } |
| 149 |
| 150 static void OnMoreButtonClicked(JNIEnv* env, |
| 151 const JavaParamRef<jclass>& caller, |
| 152 jint j_category_id, |
| 153 jint position) { |
| 154 ntp_snippets::metrics::OnMoreButtonClicked( |
| 155 Category::FromIDValue(j_category_id), position); |
| 156 GetUserClassifier()->OnEvent(UserClassifier::Metric::SUGGESTIONS_USED); |
| 157 } |
| 158 |
| 159 static void OnSurfaceOpened(JNIEnv* env, const JavaParamRef<jclass>& caller) { |
| 160 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 161 GetRemoteSuggestionsScheduler(); |
| 162 // Can be null if the feature has been disabled but the scheduler has not been |
| 163 // unregistered yet. The next start should unregister it. |
| 164 if (!scheduler) { |
| 165 return; |
| 166 } |
| 167 |
| 168 scheduler->OnNTPOpened(); |
| 169 } |
| 170 |
| 171 static void OnColdStart(JNIEnv* env, const JavaParamRef<jclass>& caller) { |
| 172 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 173 GetRemoteSuggestionsScheduler(); |
| 174 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
| 175 if (!scheduler) { |
| 176 return; |
| 177 } |
| 178 scheduler->OnBrowserColdStart(); |
| 179 } |
| 180 |
| 181 static void OnActivityWarmResumed(JNIEnv* env, |
| 182 const JavaParamRef<jclass>& caller) { |
| 183 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 184 GetRemoteSuggestionsScheduler(); |
| 185 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
| 186 if (!scheduler) { |
| 187 return; |
| 188 } |
| 189 scheduler->OnBrowserForegrounded(); |
| 190 } |
| 191 |
| 192 bool RegisterSuggestionsEventReporterBridge(JNIEnv* env) { |
| 193 return RegisterNativesImpl(env); |
| 194 } |
| OLD | NEW |