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