Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: chrome/browser/android/ntp/suggestions_event_reporter_bridge.cc

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

Powered by Google App Engine
This is Rietveld 408576698