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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 return; | 164 return; |
165 } | 165 } |
166 | 166 |
167 service->RescheduleFetching(/*force=*/true); | 167 service->RescheduleFetching(/*force=*/true); |
168 } | 168 } |
169 | 169 |
170 static void OnSuggestionTargetVisited(JNIEnv* env, | 170 static void OnSuggestionTargetVisited(JNIEnv* env, |
171 const JavaParamRef<jclass>& caller, | 171 const JavaParamRef<jclass>& caller, |
172 jint j_category_id, | 172 jint j_category_id, |
173 jlong visit_time_ms) { | 173 jlong visit_time_ms) { |
174 Profile* profile = ProfileManager::GetLastUsedProfile(); | |
175 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | |
176 ContentSuggestionsServiceFactory::GetForProfile(profile); | |
177 ntp_snippets::metrics::OnSuggestionTargetVisited( | 174 ntp_snippets::metrics::OnSuggestionTargetVisited( |
178 content_suggestions_service->category_factory()->FromIDValue( | 175 Category::FromIDValue(j_category_id), |
179 j_category_id), | |
180 base::TimeDelta::FromMilliseconds(visit_time_ms)); | 176 base::TimeDelta::FromMilliseconds(visit_time_ms)); |
181 } | 177 } |
182 | 178 |
183 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | 179 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, |
184 const JavaParamRef<jobject>& j_profile) | 180 const JavaParamRef<jobject>& j_profile) |
185 : content_suggestions_service_observer_(this), weak_ptr_factory_(this) { | 181 : content_suggestions_service_observer_(this), weak_ptr_factory_(this) { |
186 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 182 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
187 content_suggestions_service_ = | 183 content_suggestions_service_ = |
188 ContentSuggestionsServiceFactory::GetForProfile(profile); | 184 ContentSuggestionsServiceFactory::GetForProfile(profile); |
189 history_service_ = | 185 history_service_ = |
(...skipping 19 matching lines...) Expand all Loading... | |
209 for (Category category : content_suggestions_service_->GetCategories()) { | 205 for (Category category : content_suggestions_service_->GetCategories()) { |
210 category_ids.push_back(category.id()); | 206 category_ids.push_back(category.id()); |
211 } | 207 } |
212 return ToJavaIntArray(env, category_ids); | 208 return ToJavaIntArray(env, category_ids); |
213 } | 209 } |
214 | 210 |
215 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, | 211 int NTPSnippetsBridge::GetCategoryStatus(JNIEnv* env, |
216 const JavaParamRef<jobject>& obj, | 212 const JavaParamRef<jobject>& obj, |
217 jint j_category_id) { | 213 jint j_category_id) { |
218 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( | 214 return static_cast<int>(content_suggestions_service_->GetCategoryStatus( |
219 CategoryFromIDValue(j_category_id))); | 215 Category::FromIDValue(j_category_id))); |
220 } | 216 } |
221 | 217 |
222 base::android::ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( | 218 base::android::ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetCategoryInfo( |
223 JNIEnv* env, | 219 JNIEnv* env, |
224 const base::android::JavaParamRef<jobject>& obj, | 220 const base::android::JavaParamRef<jobject>& obj, |
225 jint j_category_id) { | 221 jint j_category_id) { |
226 base::Optional<CategoryInfo> info = | 222 base::Optional<CategoryInfo> info = |
227 content_suggestions_service_->GetCategoryInfo( | 223 content_suggestions_service_->GetCategoryInfo( |
228 CategoryFromIDValue(j_category_id)); | 224 Category::FromIDValue(j_category_id)); |
229 if (!info) { | 225 if (!info) { |
230 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); | 226 return base::android::ScopedJavaLocalRef<jobject>(env, nullptr); |
231 } | 227 } |
232 return Java_SnippetsBridge_createSuggestionsCategoryInfo( | 228 return Java_SnippetsBridge_createSuggestionsCategoryInfo( |
233 env, j_category_id, ConvertUTF16ToJavaString(env, info->title()), | 229 env, j_category_id, ConvertUTF16ToJavaString(env, info->title()), |
234 static_cast<int>(info->card_layout()), info->has_more_action(), | 230 static_cast<int>(info->card_layout()), info->has_more_action(), |
235 info->has_reload_action(), info->has_view_all_action(), | 231 info->has_reload_action(), info->has_view_all_action(), |
236 info->show_if_empty(), | 232 info->show_if_empty(), |
237 ConvertUTF16ToJavaString(env, info->no_suggestions_message())); | 233 ConvertUTF16ToJavaString(env, info->no_suggestions_message())); |
238 } | 234 } |
239 | 235 |
240 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( | 236 ScopedJavaLocalRef<jobject> NTPSnippetsBridge::GetSuggestionsForCategory( |
241 JNIEnv* env, | 237 JNIEnv* env, |
242 const base::android::JavaParamRef<jobject>& obj, | 238 const base::android::JavaParamRef<jobject>& obj, |
243 jint j_category_id) { | 239 jint j_category_id) { |
244 Category category = CategoryFromIDValue(j_category_id); | 240 Category category = Category::FromIDValue(j_category_id); |
245 return ToJavaSuggestionList( | 241 return ToJavaSuggestionList( |
246 env, category, | 242 env, category, |
247 content_suggestions_service_->GetSuggestionsForCategory(category)); | 243 content_suggestions_service_->GetSuggestionsForCategory(category)); |
248 } | 244 } |
249 | 245 |
250 void NTPSnippetsBridge::FetchSuggestionImage( | 246 void NTPSnippetsBridge::FetchSuggestionImage( |
251 JNIEnv* env, | 247 JNIEnv* env, |
252 const JavaParamRef<jobject>& obj, | 248 const JavaParamRef<jobject>& obj, |
253 jint j_category_id, | 249 jint j_category_id, |
254 const JavaParamRef<jstring>& id_within_category, | 250 const JavaParamRef<jstring>& id_within_category, |
255 const JavaParamRef<jobject>& j_callback) { | 251 const JavaParamRef<jobject>& j_callback) { |
256 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); | 252 base::android::ScopedJavaGlobalRef<jobject> callback(j_callback); |
257 content_suggestions_service_->FetchSuggestionImage( | 253 content_suggestions_service_->FetchSuggestionImage( |
258 ContentSuggestion::ID(CategoryFromIDValue(j_category_id), | 254 ContentSuggestion::ID(Category::FromIDValue(j_category_id), |
259 ConvertJavaStringToUTF8(env, id_within_category)), | 255 ConvertJavaStringToUTF8(env, id_within_category)), |
260 base::Bind(&NTPSnippetsBridge::OnImageFetched, | 256 base::Bind(&NTPSnippetsBridge::OnImageFetched, |
261 weak_ptr_factory_.GetWeakPtr(), callback)); | 257 weak_ptr_factory_.GetWeakPtr(), callback)); |
262 } | 258 } |
263 | 259 |
264 void NTPSnippetsBridge::Fetch( | 260 void NTPSnippetsBridge::Fetch( |
265 JNIEnv* env, | 261 JNIEnv* env, |
266 const JavaParamRef<jobject>& obj, | 262 const JavaParamRef<jobject>& obj, |
267 jint j_category_id, | 263 jint j_category_id, |
268 const JavaParamRef<jobjectArray>& j_displayed_suggestions) { | 264 const JavaParamRef<jobjectArray>& j_displayed_suggestions) { |
269 std::vector<std::string> known_suggestion_ids; | 265 std::vector<std::string> known_suggestion_ids; |
270 AppendJavaStringArrayToStringVector(env, j_displayed_suggestions, | 266 AppendJavaStringArrayToStringVector(env, j_displayed_suggestions, |
271 &known_suggestion_ids); | 267 &known_suggestion_ids); |
272 | 268 |
273 Category category = CategoryFromIDValue(j_category_id); | 269 Category category = Category::FromIDValue(j_category_id); |
274 content_suggestions_service_->Fetch( | 270 content_suggestions_service_->Fetch( |
275 category, std::set<std::string>(known_suggestion_ids.begin(), | 271 category, std::set<std::string>(known_suggestion_ids.begin(), |
276 known_suggestion_ids.end()), | 272 known_suggestion_ids.end()), |
277 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, | 273 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, |
278 weak_ptr_factory_.GetWeakPtr(), category)); | 274 weak_ptr_factory_.GetWeakPtr(), category)); |
279 } | 275 } |
280 | 276 |
281 void NTPSnippetsBridge::DismissSuggestion( | 277 void NTPSnippetsBridge::DismissSuggestion( |
282 JNIEnv* env, | 278 JNIEnv* env, |
283 const JavaParamRef<jobject>& obj, | 279 const JavaParamRef<jobject>& obj, |
284 const JavaParamRef<jstring>& jurl, | 280 const JavaParamRef<jstring>& jurl, |
285 jint global_position, | 281 jint global_position, |
286 jint j_category_id, | 282 jint j_category_id, |
287 jint category_position, | 283 jint category_position, |
288 const JavaParamRef<jstring>& id_within_category) { | 284 const JavaParamRef<jstring>& id_within_category) { |
289 Category category = CategoryFromIDValue(j_category_id); | 285 Category category = Category::FromIDValue(j_category_id); |
290 | 286 |
291 content_suggestions_service_->DismissSuggestion(ContentSuggestion::ID( | 287 content_suggestions_service_->DismissSuggestion(ContentSuggestion::ID( |
292 category, ConvertJavaStringToUTF8(env, id_within_category))); | 288 category, ConvertJavaStringToUTF8(env, id_within_category))); |
293 | 289 |
294 history_service_->QueryURL( | 290 history_service_->QueryURL( |
295 GURL(ConvertJavaStringToUTF8(env, jurl)), /*want_visits=*/false, | 291 GURL(ConvertJavaStringToUTF8(env, jurl)), /*want_visits=*/false, |
296 base::Bind( | 292 base::Bind( |
297 [](int global_position, Category category, int category_position, | 293 [](int global_position, Category category, int category_position, |
298 bool success, const history::URLRow& row, | 294 bool success, const history::URLRow& row, |
299 const history::VisitVector& visit_vector) { | 295 const history::VisitVector& visit_vector) { |
300 bool visited = success && row.visit_count() != 0; | 296 bool visited = success && row.visit_count() != 0; |
301 ntp_snippets::metrics::OnSuggestionDismissed( | 297 ntp_snippets::metrics::OnSuggestionDismissed( |
302 global_position, category, category_position, visited); | 298 global_position, category, category_position, visited); |
303 }, | 299 }, |
304 global_position, category, category_position), | 300 global_position, category, category_position), |
305 &tracker_); | 301 &tracker_); |
306 } | 302 } |
307 | 303 |
308 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, | 304 void NTPSnippetsBridge::DismissCategory(JNIEnv* env, |
309 const JavaParamRef<jobject>& obj, | 305 const JavaParamRef<jobject>& obj, |
310 jint j_category_id) { | 306 jint j_category_id) { |
311 Category category = CategoryFromIDValue(j_category_id); | 307 Category category = Category::FromIDValue(j_category_id); |
312 | 308 |
313 content_suggestions_service_->DismissCategory(category); | 309 content_suggestions_service_->DismissCategory(category); |
314 | 310 |
315 ntp_snippets::metrics::OnCategoryDismissed(category); | 311 ntp_snippets::metrics::OnCategoryDismissed(category); |
316 } | 312 } |
317 | 313 |
318 void NTPSnippetsBridge::RestoreDismissedCategories( | 314 void NTPSnippetsBridge::RestoreDismissedCategories( |
319 JNIEnv* env, | 315 JNIEnv* env, |
320 const JavaParamRef<jobject>& obj) { | 316 const JavaParamRef<jobject>& obj) { |
321 content_suggestions_service_->RestoreDismissedCategories(); | 317 content_suggestions_service_->RestoreDismissedCategories(); |
322 } | 318 } |
323 | 319 |
324 void NTPSnippetsBridge::OnPageShown( | 320 void NTPSnippetsBridge::OnPageShown( |
325 JNIEnv* env, | 321 JNIEnv* env, |
326 const JavaParamRef<jobject>& obj, | 322 const JavaParamRef<jobject>& obj, |
327 const JavaParamRef<jintArray>& jcategories, | 323 const JavaParamRef<jintArray>& jcategories, |
328 const JavaParamRef<jintArray>& jsuggestions_per_category) { | 324 const JavaParamRef<jintArray>& jsuggestions_per_category) { |
329 std::vector<int> categories_int; | 325 std::vector<int> categories_int; |
330 JavaIntArrayToIntVector(env, jcategories, &categories_int); | 326 JavaIntArrayToIntVector(env, jcategories, &categories_int); |
331 std::vector<int> suggestions_per_category_int; | 327 std::vector<int> suggestions_per_category_int; |
332 JavaIntArrayToIntVector(env, jsuggestions_per_category, | 328 JavaIntArrayToIntVector(env, jsuggestions_per_category, |
333 &suggestions_per_category_int); | 329 &suggestions_per_category_int); |
334 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); | 330 DCHECK_EQ(categories_int.size(), suggestions_per_category_int.size()); |
335 std::vector<std::pair<Category, int>> suggestions_per_category; | 331 std::vector<std::pair<Category, int>> suggestions_per_category; |
336 for (size_t i = 0; i < categories_int.size(); i++) { | 332 for (size_t i = 0; i < categories_int.size(); i++) { |
337 suggestions_per_category.push_back( | 333 suggestions_per_category.push_back( |
338 std::make_pair(CategoryFromIDValue(categories_int[i]), | 334 std::make_pair(Category::FromIDValue(categories_int[i]), |
339 suggestions_per_category_int[i])); | 335 suggestions_per_category_int[i])); |
340 } | 336 } |
341 ntp_snippets::metrics::OnPageShown(suggestions_per_category); | 337 ntp_snippets::metrics::OnPageShown(suggestions_per_category); |
342 content_suggestions_service_->user_classifier()->OnEvent( | 338 content_suggestions_service_->user_classifier()->OnEvent( |
343 ntp_snippets::UserClassifier::Metric::NTP_OPENED); | 339 ntp_snippets::UserClassifier::Metric::NTP_OPENED); |
344 } | 340 } |
345 | 341 |
346 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, | 342 void NTPSnippetsBridge::OnSuggestionShown(JNIEnv* env, |
347 const JavaParamRef<jobject>& obj, | 343 const JavaParamRef<jobject>& obj, |
348 jint global_position, | 344 jint global_position, |
349 jint j_category_id, | 345 jint j_category_id, |
350 jint category_position, | 346 jint category_position, |
351 jlong publish_timestamp_ms, | 347 jlong publish_timestamp_ms, |
352 jfloat score) { | 348 jfloat score) { |
353 ntp_snippets::metrics::OnSuggestionShown( | 349 ntp_snippets::metrics::OnSuggestionShown( |
354 global_position, CategoryFromIDValue(j_category_id), category_position, | 350 global_position, Category::FromIDValue(j_category_id), category_position, |
355 base::Time::FromJavaTime(publish_timestamp_ms), score); | 351 base::Time::FromJavaTime(publish_timestamp_ms), score); |
356 if (global_position == 0) { | 352 if (global_position == 0) { |
357 content_suggestions_service_->user_classifier()->OnEvent( | 353 content_suggestions_service_->user_classifier()->OnEvent( |
358 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); | 354 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_SHOWN); |
359 } | 355 } |
360 } | 356 } |
361 | 357 |
362 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, | 358 void NTPSnippetsBridge::OnSuggestionOpened(JNIEnv* env, |
363 const JavaParamRef<jobject>& obj, | 359 const JavaParamRef<jobject>& obj, |
364 jint global_position, | 360 jint global_position, |
365 jint j_category_id, | 361 jint j_category_id, |
366 jint category_position, | 362 jint category_position, |
367 jlong publish_timestamp_ms, | 363 jlong publish_timestamp_ms, |
368 jfloat score, | 364 jfloat score, |
369 int windowOpenDisposition) { | 365 int windowOpenDisposition) { |
370 ntp_snippets::metrics::OnSuggestionOpened( | 366 ntp_snippets::metrics::OnSuggestionOpened( |
371 global_position, CategoryFromIDValue(j_category_id), category_position, | 367 global_position, Category::FromIDValue(j_category_id), category_position, |
372 base::Time::FromJavaTime(publish_timestamp_ms), score, | 368 base::Time::FromJavaTime(publish_timestamp_ms), score, |
373 static_cast<WindowOpenDisposition>(windowOpenDisposition)); | 369 static_cast<WindowOpenDisposition>(windowOpenDisposition)); |
370 content_suggestions_service_->section_ranker()->OnSuggestionOpened( | |
Marc Treib
2016/12/13 12:22:42
It'd be nice not to expose the section ranker. Sim
vitaliii
2016/12/14 08:59:38
user_classifier is used in many places (e.g. snipp
Marc Treib
2016/12/14 10:24:30
Agreed, I wasn't suggesting to do that in this CL.
vitaliii
2016/12/15 15:30:12
I added a TODO.
| |
371 Category::FromIDValue(j_category_id)); | |
374 content_suggestions_service_->user_classifier()->OnEvent( | 372 content_suggestions_service_->user_classifier()->OnEvent( |
375 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | 373 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); |
376 } | 374 } |
377 | 375 |
378 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, | 376 void NTPSnippetsBridge::OnSuggestionMenuOpened(JNIEnv* env, |
379 const JavaParamRef<jobject>& obj, | 377 const JavaParamRef<jobject>& obj, |
380 jint global_position, | 378 jint global_position, |
381 jint j_category_id, | 379 jint j_category_id, |
382 jint category_position, | 380 jint category_position, |
383 jlong publish_timestamp_ms, | 381 jlong publish_timestamp_ms, |
384 jfloat score) { | 382 jfloat score) { |
385 ntp_snippets::metrics::OnSuggestionMenuOpened( | 383 ntp_snippets::metrics::OnSuggestionMenuOpened( |
386 global_position, CategoryFromIDValue(j_category_id), category_position, | 384 global_position, Category::FromIDValue(j_category_id), category_position, |
387 base::Time::FromJavaTime(publish_timestamp_ms), score); | 385 base::Time::FromJavaTime(publish_timestamp_ms), score); |
388 } | 386 } |
389 | 387 |
390 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, | 388 void NTPSnippetsBridge::OnMoreButtonShown(JNIEnv* env, |
391 const JavaParamRef<jobject>& obj, | 389 const JavaParamRef<jobject>& obj, |
392 jint j_category_id, | 390 jint j_category_id, |
393 jint position) { | 391 jint position) { |
394 ntp_snippets::metrics::OnMoreButtonShown(CategoryFromIDValue(j_category_id), | 392 ntp_snippets::metrics::OnMoreButtonShown(Category::FromIDValue(j_category_id), |
395 position); | 393 position); |
396 } | 394 } |
397 | 395 |
398 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, | 396 void NTPSnippetsBridge::OnMoreButtonClicked(JNIEnv* env, |
399 const JavaParamRef<jobject>& obj, | 397 const JavaParamRef<jobject>& obj, |
400 jint j_category_id, | 398 jint j_category_id, |
401 jint position) { | 399 jint position) { |
402 ntp_snippets::metrics::OnMoreButtonClicked(CategoryFromIDValue(j_category_id), | 400 ntp_snippets::metrics::OnMoreButtonClicked( |
403 position); | 401 Category::FromIDValue(j_category_id), position); |
404 content_suggestions_service_->user_classifier()->OnEvent( | 402 content_suggestions_service_->user_classifier()->OnEvent( |
405 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); | 403 ntp_snippets::UserClassifier::Metric::SUGGESTIONS_USED); |
406 } | 404 } |
407 | 405 |
408 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 406 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
409 | 407 |
410 void NTPSnippetsBridge::OnNewSuggestions(Category category) { | 408 void NTPSnippetsBridge::OnNewSuggestions(Category category) { |
411 if (observer_.is_null()) { | 409 if (observer_.is_null()) { |
412 return; | 410 return; |
413 } | 411 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 Category category, | 466 Category category, |
469 ntp_snippets::Status status, | 467 ntp_snippets::Status status, |
470 std::vector<ContentSuggestion> suggestions) { | 468 std::vector<ContentSuggestion> suggestions) { |
471 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. | 469 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. |
472 JNIEnv* env = AttachCurrentThread(); | 470 JNIEnv* env = AttachCurrentThread(); |
473 Java_SnippetsBridge_onMoreSuggestions( | 471 Java_SnippetsBridge_onMoreSuggestions( |
474 env, observer_, category.id(), | 472 env, observer_, category.id(), |
475 ToJavaSuggestionList(env, category, suggestions)); | 473 ToJavaSuggestionList(env, category, suggestions)); |
476 } | 474 } |
477 | 475 |
478 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { | |
479 return content_suggestions_service_->category_factory()->FromIDValue(id); | |
480 } | |
481 | |
482 // static | 476 // static |
483 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 477 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
484 return RegisterNativesImpl(env); | 478 return RegisterNativesImpl(env); |
485 } | 479 } |
OLD | NEW |