OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/suggestions_event_reporter_bridge.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 #include <utility> | |
9 #include <vector> | 8 #include <vector> |
10 | 9 |
11 #include "base/android/callback_android.h" | |
12 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
13 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
14 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
15 #include "base/callback.h" | |
16 #include "base/time/time.h" | 13 #include "base/time/time.h" |
17 #include "chrome/browser/android/ntp/content_suggestions_notifier_service.h" | |
18 #include "chrome/browser/history/history_service_factory.h" | |
19 #include "chrome/browser/ntp_snippets/content_suggestions_notifier_service_facto ry.h" | |
20 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 14 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
21 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/profiles/profile_android.h" | |
23 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
24 #include "components/history/core/browser/history_service.h" | |
25 #include "components/ntp_snippets/content_suggestion.h" | |
26 #include "components/ntp_snippets/content_suggestions_metrics.h" | 17 #include "components/ntp_snippets/content_suggestions_metrics.h" |
27 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | 18 #include "components/ntp_snippets/content_suggestions_service.h" |
28 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" | 19 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
29 #include "jni/SnippetsBridge_jni.h" | 20 #include "components/ntp_snippets/user_classifier.h" |
21 #include "jni/SuggestionsEventReporterBridge_jni.h" | |
30 #include "ui/base/window_open_disposition.h" | 22 #include "ui/base/window_open_disposition.h" |
31 #include "ui/gfx/android/java_bitmap.h" | |
32 #include "ui/gfx/image/image.h" | |
33 | 23 |
34 using base::android::AttachCurrentThread; | 24 using base::android::AttachCurrentThread; |
35 using base::android::ConvertJavaStringToUTF8; | |
36 using base::android::ConvertUTF8ToJavaString; | |
37 using base::android::ConvertUTF16ToJavaString; | |
38 using base::android::JavaIntArrayToIntVector; | |
39 using base::android::AppendJavaStringArrayToStringVector; | |
40 using base::android::JavaParamRef; | 25 using base::android::JavaParamRef; |
41 using base::android::ScopedJavaGlobalRef; | |
42 using base::android::ScopedJavaLocalRef; | |
43 using base::android::ToJavaIntArray; | |
44 using ntp_snippets::Category; | 26 using ntp_snippets::Category; |
45 using ntp_snippets::CategoryInfo; | 27 using ntp_snippets::UserClassifier; |
46 using ntp_snippets::CategoryStatus; | |
47 using ntp_snippets::KnownCategories; | |
48 using ntp_snippets::ContentSuggestion; | |
49 | 28 |
50 namespace { | 29 namespace { |
51 | 30 |
52 // Converts a vector of ContentSuggestions to its Java equivalent. | |
53 ScopedJavaLocalRef<jobject> ToJavaSuggestionList( | |
54 JNIEnv* env, | |
55 const Category& category, | |
56 const std::vector<ContentSuggestion>& suggestions) { | |
57 ScopedJavaLocalRef<jobject> result = | |
58 Java_SnippetsBridge_createSuggestionList(env); | |
59 for (const ContentSuggestion& suggestion : suggestions) { | |
60 ScopedJavaLocalRef<jobject> java_suggestion = | |
61 Java_SnippetsBridge_addSuggestion( | |
62 env, result, category.id(), | |
63 ConvertUTF8ToJavaString(env, suggestion.id().id_within_category()), | |
64 ConvertUTF16ToJavaString(env, suggestion.title()), | |
65 ConvertUTF16ToJavaString(env, suggestion.publisher_name()), | |
66 ConvertUTF16ToJavaString(env, suggestion.snippet_text()), | |
67 ConvertUTF8ToJavaString(env, suggestion.url().spec()), | |
68 suggestion.publish_date().ToJavaTime(), suggestion.score(), | |
69 suggestion.fetch_date().ToJavaTime()); | |
70 if (suggestion.id().category().IsKnownCategory( | |
71 KnownCategories::DOWNLOADS) && | |
72 suggestion.download_suggestion_extra() != nullptr) { | |
73 if (suggestion.download_suggestion_extra()->is_download_asset) { | |
74 Java_SnippetsBridge_setAssetDownloadDataForSuggestion( | |
75 env, java_suggestion, | |
76 ConvertUTF8ToJavaString( | |
77 env, suggestion.download_suggestion_extra()->download_guid), | |
78 ConvertUTF8ToJavaString(env, suggestion.download_suggestion_extra() | |
79 ->target_file_path.value()), | |
80 ConvertUTF8ToJavaString( | |
81 env, suggestion.download_suggestion_extra()->mime_type)); | |
82 } else { | |
83 Java_SnippetsBridge_setOfflinePageDownloadDataForSuggestion( | |
84 env, java_suggestion, | |
85 suggestion.download_suggestion_extra()->offline_page_id); | |
86 } | |
87 } | |
88 if (suggestion.id().category().IsKnownCategory( | |
89 KnownCategories::RECENT_TABS) && | |
90 suggestion.recent_tab_suggestion_extra() != nullptr) { | |
91 Java_SnippetsBridge_setRecentTabDataForSuggestion( | |
92 env, java_suggestion, | |
93 suggestion.recent_tab_suggestion_extra()->tab_id, | |
94 suggestion.recent_tab_suggestion_extra()->offline_page_id); | |
95 } | |
96 } | |
97 | |
98 return result; | |
99 } | |
100 | |
101 ntp_snippets::RemoteSuggestionsScheduler* GetRemoteSuggestionsScheduler() { | 31 ntp_snippets::RemoteSuggestionsScheduler* GetRemoteSuggestionsScheduler() { |
102 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 32 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
103 ContentSuggestionsServiceFactory::GetForProfile( | 33 ContentSuggestionsServiceFactory::GetForProfile( |
104 ProfileManager::GetLastUsedProfile()); | 34 ProfileManager::GetLastUsedProfile()); |
105 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | 35 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
106 if (!content_suggestions_service) { | 36 if (!content_suggestions_service) { |
107 return nullptr; | 37 return nullptr; |
108 } | 38 } |
109 return content_suggestions_service->remote_suggestions_scheduler(); | 39 return content_suggestions_service->remote_suggestions_scheduler(); |
110 } | 40 } |
111 | 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 | |
Marc Treib
2017/04/28 15:06:34
:)
dgn
2017/05/02 13:42:42
Done.
| |
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 | |
112 } // namespace | 55 } // namespace |
113 | 56 |
114 static jlong Init(JNIEnv* env, | |
115 const JavaParamRef<jobject>& j_bridge, | |
116 const JavaParamRef<jobject>& j_profile) { | |
117 NTPSnippetsBridge* snippets_bridge = | |
118 new NTPSnippetsBridge(env, j_bridge, j_profile); | |
119 return reinterpret_cast<intptr_t>(snippets_bridge); | |
120 } | |
121 | |
122 // Initiates a background fetch for remote suggestions. | |
123 static void RemoteSuggestionsSchedulerOnFetchDue( | |
124 JNIEnv* env, | |
125 const JavaParamRef<jclass>& caller) { | |
126 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | |
127 GetRemoteSuggestionsScheduler(); | |
128 if (!scheduler) { | |
129 return; | |
130 } | |
131 | |
132 scheduler->OnPersistentSchedulerWakeUp(); | |
133 } | |
134 | |
135 // Reschedules the fetching of snippets. If tasks are already scheduled, they | |
136 // will be rescheduled anyway, so all running intervals will be reset. | |
137 static void RemoteSuggestionsSchedulerRescheduleFetching( | |
138 JNIEnv* env, | |
139 const JavaParamRef<jclass>& caller) { | |
140 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | |
141 GetRemoteSuggestionsScheduler(); | |
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. | |
144 if (!scheduler) { | |
145 return; | |
146 } | |
147 | |
148 scheduler->RescheduleFetching(); | |
149 } | |
150 | |
151 static void OnSuggestionTargetVisited(JNIEnv* env, | 57 static void OnSuggestionTargetVisited(JNIEnv* env, |
152 const JavaParamRef<jclass>& caller, | 58 const JavaParamRef<jclass>& caller, |
153 jint j_category_id, | 59 jint j_category_id, |
154 jlong visit_time_ms) { | 60 jlong visit_time_ms) { |
155 ntp_snippets::metrics::OnSuggestionTargetVisited( | 61 ntp_snippets::metrics::OnSuggestionTargetVisited( |
156 Category::FromIDValue(j_category_id), | 62 Category::FromIDValue(j_category_id), |
157 base::TimeDelta::FromMilliseconds(visit_time_ms)); | 63 base::TimeDelta::FromMilliseconds(visit_time_ms)); |
158 } | 64 } |
159 | 65 |
160 static void SetRemoteSuggestionsEnabled(JNIEnv* env, | 66 static void OnPageShown( |
161 const JavaParamRef<jclass>& caller, | |
162 jboolean enabled) { | |
163 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
164 ContentSuggestionsServiceFactory::GetForProfile( | |
165 ProfileManager::GetLastUsedProfile()); | |
166 if (!content_suggestions_service) | |
167 return; | |
168 | |
169 content_suggestions_service->SetRemoteSuggestionsEnabled(enabled); | |
170 } | |
171 | |
172 static jboolean AreRemoteSuggestionsEnabled( | |
173 JNIEnv* env, | |
174 const JavaParamRef<jclass>& caller) { | |
175 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
176 ContentSuggestionsServiceFactory::GetForProfile( | |
177 ProfileManager::GetLastUsedProfile()); | |
178 if (!content_suggestions_service) | |
179 return false; | |
180 | |
181 return content_suggestions_service->AreRemoteSuggestionsEnabled(); | |
182 } | |
183 | |
184 // Returns true if the remote provider is managed by an adminstrator's policy. | |
185 static jboolean AreRemoteSuggestionsManaged( | |
186 JNIEnv* env, | |
187 const JavaParamRef<jclass>& caller) { | |
188 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
189 ContentSuggestionsServiceFactory::GetForProfile( | |
190 ProfileManager::GetLastUsedProfile()); | |
191 if (!content_suggestions_service) | |
192 return false; | |
193 | |
194 return content_suggestions_service->AreRemoteSuggestionsManaged(); | |
195 } | |
196 | |
197 // Returns true if the remote provider is managed by a supervisor | |
198 static jboolean AreRemoteSuggestionsManagedByCustodian( | |
199 JNIEnv* env, | |
200 const JavaParamRef<jclass>& caller) { | |
201 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
202 ContentSuggestionsServiceFactory::GetForProfile( | |
203 ProfileManager::GetLastUsedProfile()); | |
204 if (!content_suggestions_service) | |
205 return false; | |
206 | |
207 return content_suggestions_service->AreRemoteSuggestionsManagedByCustodian(); | |
208 } | |
209 | |
210 static void SetContentSuggestionsNotificationsEnabled( | |
211 JNIEnv* env, | 67 JNIEnv* env, |
212 const JavaParamRef<jclass>& caller, | 68 const JavaParamRef<jclass>& caller, |
213 jboolean enabled) { | |
214 ContentSuggestionsNotifierService* notifier_service = | |
215 ContentSuggestionsNotifierServiceFactory::GetForProfile( | |
216 ProfileManager::GetLastUsedProfile()); | |
217 if (!notifier_service) | |
218 return; | |
219 | |
220 notifier_service->SetEnabled(enabled); | |
221 } | |
222 | |
223 static jboolean AreContentSuggestionsNotificationsEnabled( | |
224 JNIEnv* env, | |
225 const JavaParamRef<jclass>& caller) { | |
226 ContentSuggestionsNotifierService* notifier_service = | |
227 ContentSuggestionsNotifierServiceFactory::GetForProfile( | |
228 ProfileManager::GetLastUsedProfile()); | |
229 if (!notifier_service) | |
230 return false; | |
231 | |
232 return notifier_service->IsEnabled(); | |
233 } | |
234 | |
235 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | |
236 const JavaParamRef<jobject>& j_bridge, | |
237 const JavaParamRef<jobject>& j_profile) | |
238 : content_suggestions_service_observer_(this), | |
239 bridge_(env, j_bridge), | |
240 weak_ptr_factory_(this) { | |
241 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | |
242 content_suggestions_service_ = | |
243 ContentSuggestionsServiceFactory::GetForProfile(profile); | |
244 history_service_ = HistoryServiceFactory::GetForProfile( | |
245 profile, ServiceAccessType::EXPLICIT_ACCESS); | |
246 content_suggestions_service_observer_.Add(content_suggestions_service_); | |
247 } | |
248 | |
249 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
250 delete this; | |
251 } | |
252 | |
253 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( | |
254 JNIEnv* env, | |
255 const JavaParamRef<jobject>& obj) { | |
256 std::vector<int> category_ids; | |
257 for (Category category : content_suggestions_service_->GetCategories()) { | |
258 category_ids.push_back(category.id()); | |
259 } | |
260 return ToJavaIntArray(env, category_ids); | |
261 } | |
262 | |
263 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | |
264 const JavaParamRef<jobject>& obj, | |
265 jint j_category_id) { | |
266 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | |
267 Category::FromIDValue(j_category_id))); | |
268 } | |
269 | |
270 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( | |
271 JNIEnv* env, | |
272 const JavaParamRef<jobject>& obj, | |
273 jint j_category_id) { | |
274 base::Optional<CategoryInfo> info = | |
275 content_suggestions_service_->GetCategoryInfo( | |
276 Category::FromIDValue(j_category_id)); | |
277 if (!info) { | |
278 return ScopedJavaLocalRef<jobject>(env, nullptr); | |
279 } | |
280 return Java_SnippetsBridge_createSuggestionsCategoryInfo( | |
281 env, j_category_id, ConvertUTF16ToJavaString(env, info->title()), | |
282 static_cast<int>(info->card_layout()), | |
283 static_cast<int>(info->additional_action()), info->show_if_empty(), | |
284 ConvertUTF16ToJavaString(env, info->no_suggestions_message())); | |
285 } | |
286 | |
287 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( | |
288 JNIEnv* env, | |
289 const JavaParamRef<jobject>& obj, | |
290 jint j_category_id) { | |
291 Category category = Category::FromIDValue(j_category_id); | |
292 return ToJavaSuggestionList( | |
293 env, category, | |
294 content_suggestions_service_->GetSuggestionsForCategory(category)); | |
295 } | |
296 | |
297 void NTPSnippetsBridge::FetchSuggestionImage( | |
298 JNIEnv* env, | |
299 const JavaParamRef<jobject>& obj, | |
300 jint j_category_id, | |
301 const JavaParamRef<jstring>& id_within_category, | |
302 const JavaParamRef<jobject>& j_callback) { | |
303 ScopedJavaGlobalRef<jobject> callback(j_callback); | |
304 content_suggestions_service_->FetchSuggestionImage( | |
305 ContentSuggestion::ID(Category::FromIDValue(j_category_id), | |
306 ConvertJavaStringToUTF8(env, id_within_category)), | |
307 base::Bind(&NTPSnippetsBridge::OnImageFetched, | |
308 weak_ptr_factory_.GetWeakPtr(), callback)); | |
309 } | |
310 | |
311 void NTPSnippetsBridge::FetchSuggestionFavicon( | |
312 JNIEnv* env, | |
313 const JavaParamRef<jobject>& obj, | |
314 jint j_category_id, | |
315 const JavaParamRef<jstring>& id_within_category, | |
316 jint j_minimum_size_px, | |
317 jint j_desired_size_px, | |
318 const JavaParamRef<jobject>& j_callback) { | |
319 ScopedJavaGlobalRef<jobject> callback(j_callback); | |
320 content_suggestions_service_->FetchSuggestionFavicon( | |
321 ContentSuggestion::ID(Category::FromIDValue(j_category_id), | |
322 ConvertJavaStringToUTF8(env, id_within_category)), | |
323 j_minimum_size_px, j_desired_size_px, | |
324 base::Bind(&NTPSnippetsBridge::OnImageFetched, | |
325 weak_ptr_factory_.GetWeakPtr(), callback)); | |
326 } | |
327 | |
328 void NTPSnippetsBridge::Fetch( | |
329 JNIEnv* env, | |
330 const JavaParamRef<jobject>& obj, | |
331 jint j_category_id, | |
332 const JavaParamRef<jobjectArray>& j_displayed_suggestions) { | |
333 std::vector<std::string> known_suggestion_ids; | |
334 AppendJavaStringArrayToStringVector(env, j_displayed_suggestions, | |
335 &known_suggestion_ids); | |
336 | |
337 Category category = Category::FromIDValue(j_category_id); | |
338 content_suggestions_service_->Fetch( | |
339 category, | |
340 std::set<std::string>(known_suggestion_ids.begin(), | |
341 known_suggestion_ids.end()), | |
342 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, | |
343 weak_ptr_factory_.GetWeakPtr(), category)); | |
344 } | |
345 | |
346 void NTPSnippetsBridge::ReloadSuggestions(JNIEnv* env, | |
347 const JavaParamRef<jobject>& obj) { | |
348 content_suggestions_service_->ReloadSuggestions(); | |
349 } | |
350 | |
351 void NTPSnippetsBridge::DismissSuggestion( | |
352 JNIEnv* env, | |
353 const JavaParamRef<jobject>& obj, | |
354 const JavaParamRef<jstring>& jurl, | |
355 jint global_position, | |
356 jint j_category_id, | |
357 jint position_in_category, | |
358 const JavaParamRef<jstring>& id_within_category) { | |
359 Category category = Category::FromIDValue(j_category_id); | |
360 | |
361 content_suggestions_service_->DismissSuggestion(ContentSuggestion::ID( | |
362 category, ConvertJavaStringToUTF8(env, id_within_category))); | |
363 | |
364 history_service_->QueryURL( | |
365 GURL(ConvertJavaStringToUTF8(env, jurl)), /*want_visits=*/false, | |
366 base::Bind( | |
367 [](int global_position, Category category, int position_in_category, | |
368 bool success, const history::URLRow& row, | |
369 const history::VisitVector& visit_vector) { | |
370 bool visited = success && row.visit_count() != 0; | |
371 ntp_snippets::metrics::OnSuggestionDismissed( | |
372 global_position, category, position_in_category, visited); | |
373 }, | |
374 global_position, category, position_in_category), | |
375 &tracker_); | |
376 } | |
377 | |
378 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, | |
379 const JavaParamRef<jobject>& obj, | |
380 jint j_category_id) { | |
381 Category category = Category::FromIDValue(j_category_id); | |
382 | |
383 content_suggestions_service_->DismissCategory(category); | |
384 | |
385 ntp_snippets::metrics::OnCategoryDismissed(category); | |
386 } | |
387 | |
388 void NTPSnippetsBridge::RestoreDismissedCategories( | |
389 JNIEnv* env, | |
390 const JavaParamRef<jobject>& obj) { | |
391 content_suggestions_service_->RestoreDismissedCategories(); | |
392 } | |
393 | |
394 void NTPSnippetsBridge::OnPageShown( | |
395 JNIEnv* env, | |
396 const JavaParamRef<jobject>& obj, | |
397 const JavaParamRef<jintArray>& jcategories, | 69 const JavaParamRef<jintArray>& jcategories, |
398 const JavaParamRef<jintArray>& jsuggestions_per_category) { | 70 const JavaParamRef<jintArray>& jsuggestions_per_category) { |
399 std::vector<int> categories_int; | 71 std::vector<int> categories_int; |
400 JavaIntArrayToIntVector(env, jcategories, &categories_int); | 72 JavaIntArrayToIntVector(env, jcategories, &categories_int); |
401 std::vector<int> suggestions_per_category_int; | 73 std::vector<int> suggestions_per_category_int; |
402 JavaIntArrayToIntVector(env, jsuggestions_per_category, | 74 JavaIntArrayToIntVector(env, jsuggestions_per_category, |
403 &suggestions_per_category_int); | 75 &suggestions_per_category_int); |
404 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); | 76 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); |
405 std::vector<std::pair<Category, int>> suggestions_per_category; | 77 std::vector<std::pair<Category, int>> suggestions_per_category; |
406 for (size_t i = 0; i < categories_int.size(); i++) { | 78 for (size_t i = 0; i < categories_int.size(); i++) { |
407 suggestions_per_category.push_back( | 79 suggestions_per_category.push_back( |
408 std::make_pair(Category::FromIDValue(categories_int[i]), | 80 std::make_pair(Category::FromIDValue(categories_int[i]), |
409 suggestions_per_category_int[i])); | 81 suggestions_per_category_int[i])); |
410 } | 82 } |
411 ntp_snippets::metrics::OnPageShown(suggestions_per_category); | 83 ntp_snippets::metrics::OnPageShown(suggestions_per_category); |
412 content_suggestions_service_->user_classifier()->OnEvent( | 84 GetUserClassifier()->OnEvent(UserClassifier::Metric::NTP_OPENED); |
413 ntp_snippets::UserClassifier::Metric::NTP_OPENED); | |
414 } | 85 } |
415 | 86 |
416 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, | 87 static void OnSuggestionShown(JNIEnv* env, |
417 const JavaParamRef<jobject>& obj, | 88 const JavaParamRef<jclass>& caller, |
418 jint global_position, | 89 jint global_position, |
419 jint j_category_id, | 90 jint j_category_id, |
420 jint position_in_category, | 91 jint position_in_category, |
421 jlong publish_timestamp_ms, | 92 jlong publish_timestamp_ms, |
422 jfloat score, | 93 jfloat score, |
423 jlong fetch_timestamp_ms) { | 94 jlong fetch_timestamp_ms) { |
424 ntp_snippets::metrics::OnSuggestionShown( | 95 ntp_snippets::metrics::OnSuggestionShown( |
425 global_position, Category::FromIDValue(j_category_id), | 96 global_position, Category::FromIDValue(j_category_id), |
426 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), | 97 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
427 score, base::Time::FromJavaTime(fetch_timestamp_ms)); | 98 score, base::Time::FromJavaTime(fetch_timestamp_ms)); |
428 if (global_position == 0) { | 99 if (global_position == 0) { |
429 content_suggestions_service_->user_classifier()->OnEvent( | 100 GetUserClassifier()->OnEvent(UserClassifier::Metric::SUGGESTIONS_SHOWN); |
430 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); | |
431 } | 101 } |
432 } | 102 } |
433 | 103 |
434 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, | 104 static void OnSuggestionOpened(JNIEnv* env, |
435 const JavaParamRef<jobject>& obj, | 105 const JavaParamRef<jclass>& caller, |
436 jint global_position, | 106 jint global_position, |
437 jint j_category_id, | 107 jint j_category_id, |
438 jint category_index, | 108 jint category_index, |
439 jint position_in_category, | 109 jint position_in_category, |
440 jlong publish_timestamp_ms, | 110 jlong publish_timestamp_ms, |
441 jfloat score, | 111 jfloat score, |
442 int windowOpenDisposition) { | 112 int windowOpenDisposition) { |
443 const Category category = Category::FromIDValue(j_category_id); | 113 const Category category = Category::FromIDValue(j_category_id); |
444 ntp_snippets::metrics::OnSuggestionOpened( | 114 ntp_snippets::metrics::OnSuggestionOpened( |
445 global_position, category, category_index, position_in_category, | 115 global_position, category, category_index, position_in_category, |
446 base::Time::FromJavaTime(publish_timestamp_ms), score, | 116 base::Time::FromJavaTime(publish_timestamp_ms), score, |
447 static_cast<WindowOpenDisposition>(windowOpenDisposition)); | 117 static_cast<WindowOpenDisposition>(windowOpenDisposition)); |
118 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
119 ContentSuggestionsServiceFactory::GetForProfile( | |
120 ProfileManager::GetLastUsedProfile()); | |
448 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and | 121 // TODO(vitaliii): Add ContentSuggestionsService::OnSuggestionOpened and |
449 // notify the ranker and the classifier there instead. Do not expose both of | 122 // notify the ranker and the classifier there instead. Do not expose both of |
450 // them at all. See crbug.com/674080. | 123 // them at all. See crbug.com/674080. |
451 content_suggestions_service_->category_ranker()->OnSuggestionOpened(category); | 124 content_suggestions_service->category_ranker()->OnSuggestionOpened(category); |
452 content_suggestions_service_->user_classifier()->OnEvent( | 125 content_suggestions_service->user_classifier()->OnEvent( |
453 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | 126 UserClassifier::Metric::SUGGESTIONS_USED); |
454 } | 127 } |
455 | 128 |
456 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, | 129 static void OnSuggestionMenuOpened(JNIEnv* env, |
457 const JavaParamRef<jobject>& obj, | 130 const JavaParamRef<jclass>& caller, |
458 jint global_position, | 131 jint global_position, |
459 jint j_category_id, | 132 jint j_category_id, |
460 jint position_in_category, | 133 jint position_in_category, |
461 jlong publish_timestamp_ms, | 134 jlong publish_timestamp_ms, |
462 jfloat score) { | 135 jfloat score) { |
463 ntp_snippets::metrics::OnSuggestionMenuOpened( | 136 ntp_snippets::metrics::OnSuggestionMenuOpened( |
464 global_position, Category::FromIDValue(j_category_id), | 137 global_position, Category::FromIDValue(j_category_id), |
465 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), | 138 position_in_category, base::Time::FromJavaTime(publish_timestamp_ms), |
466 score); | 139 score); |
467 } | 140 } |
468 | 141 |
469 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, | 142 static void OnMoreButtonShown(JNIEnv* env, |
470 const JavaParamRef<jobject>& obj, | 143 const JavaParamRef<jclass>& caller, |
471 jint j_category_id, | 144 jint j_category_id, |
472 jint position) { | 145 jint position) { |
473 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), | 146 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), |
474 position); | 147 position); |
475 } | 148 } |
476 | 149 |
477 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, | 150 static void OnMoreButtonClicked(JNIEnv* env, |
478 const JavaParamRef<jobject>& obj, | 151 const JavaParamRef<jclass>& caller, |
479 jint j_category_id, | 152 jint j_category_id, |
480 jint position) { | 153 jint position) { |
481 ntp_snippets::metrics::OnMoreButtonClicked( | 154 ntp_snippets::metrics::OnMoreButtonClicked( |
482 Category::FromIDValue(j_category_id), position); | 155 Category::FromIDValue(j_category_id), position); |
483 content_suggestions_service_->user_classifier()->OnEvent( | 156 GetUserClassifier()->OnEvent(UserClassifier::Metric::SUGGESTIONS_USED); |
484 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | |
485 } | 157 } |
486 | 158 |
487 void NTPSnippetsBridge::OnNTPInitialized(JNIEnv* env, | 159 static void OnSurfaceOpened(JNIEnv* env, const JavaParamRef<jclass>& caller) { |
488 const JavaParamRef<jobject>& obj) { | |
489 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 160 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
490 GetRemoteSuggestionsScheduler(); | 161 GetRemoteSuggestionsScheduler(); |
491 // Can be null if the feature has been disabled but the scheduler has not been | 162 // Can be null if the feature has been disabled but the scheduler has not been |
492 // unregistered yet. The next start should unregister it. | 163 // unregistered yet. The next start should unregister it. |
493 if (!scheduler) { | 164 if (!scheduler) { |
494 return; | 165 return; |
495 } | 166 } |
496 | 167 |
497 scheduler->OnNTPOpened(); | 168 scheduler->OnNTPOpened(); |
498 } | 169 } |
499 | 170 |
500 void NTPSnippetsBridge::OnColdStart(JNIEnv* env, | 171 static void OnColdStart(JNIEnv* env, const JavaParamRef<jclass>& caller) { |
501 const JavaParamRef<jobject>& obj) { | |
502 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 172 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
503 GetRemoteSuggestionsScheduler(); | 173 GetRemoteSuggestionsScheduler(); |
504 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | 174 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
505 if (!scheduler) { | 175 if (!scheduler) { |
506 return; | 176 return; |
507 } | 177 } |
508 scheduler->OnBrowserColdStart(); | 178 scheduler->OnBrowserColdStart(); |
509 } | 179 } |
510 | 180 |
511 void NTPSnippetsBridge::OnActivityWarmResumed( | 181 static void OnActivityWarmResumed(JNIEnv* env, |
512 JNIEnv* env, | 182 const JavaParamRef<jclass>& caller) { |
513 const JavaParamRef<jobject>& obj) { | |
514 ntp_snippets::RemoteSuggestionsScheduler* scheduler = | 183 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
515 GetRemoteSuggestionsScheduler(); | 184 GetRemoteSuggestionsScheduler(); |
516 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. | 185 // TODO(fhorschig): Remove guard when https://crbug.com/678556 is resolved. |
517 if (!scheduler) { | 186 if (!scheduler) { |
518 return; | 187 return; |
519 } | 188 } |
520 scheduler->OnBrowserForegrounded(); | 189 scheduler->OnBrowserForegrounded(); |
521 } | 190 } |
522 | 191 |
523 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 192 bool RegisterSuggestionsEventReporterBridge(JNIEnv* env) { |
524 | |
525 void NTPSnippetsBridge::OnNewSuggestions(Category category) { | |
526 JNIEnv* env = AttachCurrentThread(); | |
527 Java_SnippetsBridge_onNewSuggestions(env, bridge_, | |
528 static_cast<int>(category.id())); | |
529 } | |
530 | |
531 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | |
532 CategoryStatus new_status) { | |
533 JNIEnv* env = AttachCurrentThread(); | |
534 Java_SnippetsBridge_onCategoryStatusChanged(env, bridge_, | |
535 static_cast<int>(category.id()), | |
536 static_cast<int>(new_status)); | |
537 } | |
538 | |
539 void NTPSnippetsBridge::OnSuggestionInvalidated( | |
540 const ContentSuggestion::ID& suggestion_id) { | |
541 JNIEnv* env = AttachCurrentThread(); | |
542 Java_SnippetsBridge_onSuggestionInvalidated( | |
543 env, bridge_.obj(), static_cast<int>(suggestion_id.category().id()), | |
544 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); | |
545 } | |
546 | |
547 void NTPSnippetsBridge::OnFullRefreshRequired() { | |
548 JNIEnv* env = AttachCurrentThread(); | |
549 Java_SnippetsBridge_onFullRefreshRequired(env, bridge_.obj()); | |
550 } | |
551 | |
552 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | |
553 bridge_.Reset(); | |
554 content_suggestions_service_observer_.Remove(content_suggestions_service_); | |
555 } | |
556 | |
557 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | |
558 const gfx::Image& image) { | |
559 ScopedJavaLocalRef<jobject> j_bitmap; | |
560 if (!image.IsEmpty()) { | |
561 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | |
562 } | |
563 RunCallbackAndroid(callback, j_bitmap); | |
564 } | |
565 | |
566 void NTPSnippetsBridge::OnSuggestionsFetched( | |
567 Category category, | |
568 ntp_snippets::Status status, | |
569 std::vector<ContentSuggestion> suggestions) { | |
570 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. | |
571 JNIEnv* env = AttachCurrentThread(); | |
572 Java_SnippetsBridge_onMoreSuggestions( | |
573 env, bridge_, category.id(), | |
574 ToJavaSuggestionList(env, category, suggestions)); | |
575 } | |
576 | |
577 // static | |
578 bool NTPSnippetsBridge::Register(JNIEnv* env) { | |
579 return RegisterNativesImpl(env); | 193 return RegisterNativesImpl(env); |
580 } | 194 } |
OLD | NEW |