Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/most_visited_sites.h" | 5 #include "chrome/browser/android/most_visited_sites.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/metrics/sparse_histogram.h" | 14 #include "base/metrics/sparse_histogram.h" |
| 15 #include "base/prefs/pref_service.h" | |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 19 #include "chrome/browser/android/popular_sites.h" | 20 #include "chrome/browser/android/popular_sites.h" |
| 20 #include "chrome/browser/history/top_sites_factory.h" | 21 #include "chrome/browser/history/top_sites_factory.h" |
| 21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/browser/profiles/profile_android.h" | 23 #include "chrome/browser/profiles/profile_android.h" |
| 23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 24 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 24 #include "chrome/browser/search/suggestions/suggestions_source.h" | 25 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| 25 #include "chrome/browser/sync/profile_sync_service.h" | 26 #include "chrome/browser/sync/profile_sync_service.h" |
| 26 #include "chrome/browser/sync/profile_sync_service_factory.h" | 27 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 27 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 28 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 28 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/common/pref_names.h" | |
| 29 #include "components/history/core/browser/top_sites.h" | 31 #include "components/history/core/browser/top_sites.h" |
| 32 #include "components/pref_registry/pref_registry_syncable.h" | |
| 30 #include "components/suggestions/suggestions_service.h" | 33 #include "components/suggestions/suggestions_service.h" |
| 31 #include "components/suggestions/suggestions_utils.h" | 34 #include "components/suggestions/suggestions_utils.h" |
| 32 #include "content/public/browser/browser_thread.h" | 35 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/url_data_source.h" | 36 #include "content/public/browser/url_data_source.h" |
| 34 #include "jni/MostVisitedSites_jni.h" | 37 #include "jni/MostVisitedSites_jni.h" |
| 35 #include "third_party/skia/include/core/SkBitmap.h" | 38 #include "third_party/skia/include/core/SkBitmap.h" |
| 36 #include "ui/gfx/android/java_bitmap.h" | 39 #include "ui/gfx/android/java_bitmap.h" |
| 37 #include "ui/gfx/codec/jpeg_codec.h" | 40 #include "ui/gfx/codec/jpeg_codec.h" |
| 38 #include "url/gurl.h" | 41 #include "url/gurl.h" |
| 39 | 42 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 if (cmd_line->HasSwitch(switches::kEnableNTPPopularSites)) | 125 if (cmd_line->HasSwitch(switches::kEnableNTPPopularSites)) |
| 123 return true; | 126 return true; |
| 124 return group_name == "Enabled"; | 127 return group_name == "Enabled"; |
| 125 } | 128 } |
| 126 | 129 |
| 127 std::string GetPopularSitesFilename() { | 130 std::string GetPopularSitesFilename() { |
| 128 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, | 131 return variations::GetVariationParamValue(kPopularSitesFieldTrialName, |
| 129 "filename"); | 132 "filename"); |
| 130 } | 133 } |
| 131 | 134 |
| 135 void GetPreviousNTPSites(std::vector<std::string>* old_sites_url, | |
|
Marc Treib
2015/09/07 11:12:59
nit: Inputs first, then outputs.
knn
2015/09/08 10:48:55
Done.
| |
| 136 std::vector<bool>* old_sites_source, | |
| 137 const PrefService* prefs, | |
| 138 size_t num_tiles, | |
| 139 const std::vector<std::string>& personal_urls) { | |
| 140 const base::ListValue* url_list = prefs->GetList(prefs::kNTPSuggestionsURL); | |
| 141 std::string url_string; | |
| 142 DCHECK_LE(personal_urls.size(), num_tiles); | |
| 143 if (url_list->GetSize() < num_tiles) | |
| 144 num_tiles = url_list->GetSize(); | |
| 145 old_sites_url->reserve(num_tiles); | |
| 146 for (size_t i = 0; i < num_tiles; ++i) { | |
| 147 if (url_list->GetString(i, &url_string)) { | |
| 148 old_sites_url->emplace_back(url_string); | |
|
Marc Treib
2015/09/07 11:12:59
push_back - no C++11 library yet. It'll do the sam
knn
2015/09/08 10:48:55
Done.
| |
| 149 } | |
| 150 } | |
| 151 if (num_tiles == 0) { | |
|
Marc Treib
2015/09/07 11:12:59
Move this above the loop.
knn
2015/09/08 10:48:55
Done.
| |
| 152 // No fallback required as Personal suggestions take precedence anyway. | |
| 153 return; | |
| 154 } | |
| 155 old_sites_source->reserve(num_tiles); | |
| 156 const base::ListValue* host_list = | |
| 157 prefs->GetList(prefs::kNTPSuggestionsSource); | |
| 158 bool is_personal; | |
| 159 DCHECK_EQ(num_tiles, host_list->GetSize()); | |
| 160 for (size_t i = 0; i < num_tiles; ++i) { | |
| 161 if (host_list->GetBoolean(i, &is_personal)) { | |
| 162 old_sites_source->push_back(is_personal); | |
| 163 } | |
| 164 } | |
| 165 DCHECK_EQ(num_tiles, old_sites_source->size()); | |
| 166 } | |
| 167 | |
| 168 void SaveCurrentNTPSites(const std::vector<std::string>& urls, | |
| 169 const std::vector<bool>& sources, | |
| 170 PrefService* prefs) { | |
| 171 base::ListValue url_list; | |
| 172 url_list.AppendStrings(urls); | |
| 173 prefs->Set(prefs::kNTPSuggestionsURL, url_list); | |
| 174 | |
| 175 base::ListValue source_list; | |
| 176 for (const bool& source : sources) | |
| 177 source_list.AppendBoolean(source); | |
| 178 prefs->Set(prefs::kNTPSuggestionsSource, source_list); | |
| 179 } | |
| 180 | |
| 132 } // namespace | 181 } // namespace |
| 133 | 182 |
| 134 MostVisitedSites::MostVisitedSites(Profile* profile) | 183 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 135 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), | 184 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), |
| 136 received_popular_sites_(false), recorded_uma_(false), | 185 received_popular_sites_(false), recorded_uma_(false), |
| 137 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), | 186 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), |
| 138 scoped_observer_(this), weak_ptr_factory_(this) { | 187 scoped_observer_(this), weak_ptr_factory_(this) { |
| 139 // Register the debugging page for the Suggestions Service and the thumbnails | 188 // Register the debugging page for the Suggestions Service and the thumbnails |
| 140 // debugging page. | 189 // debugging page. |
| 141 content::URLDataSource::Add(profile_, | 190 content::URLDataSource::Add(profile_, |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 // (just initialized, enabled/disabled or history sync state changed). Re-run | 351 // (just initialized, enabled/disabled or history sync state changed). Re-run |
| 303 // the query code which will use the proper state. | 352 // the query code which will use the proper state. |
| 304 QueryMostVisitedURLs(); | 353 QueryMostVisitedURLs(); |
| 305 } | 354 } |
| 306 | 355 |
| 307 // static | 356 // static |
| 308 bool MostVisitedSites::Register(JNIEnv* env) { | 357 bool MostVisitedSites::Register(JNIEnv* env) { |
| 309 return RegisterNativesImpl(env); | 358 return RegisterNativesImpl(env); |
| 310 } | 359 } |
| 311 | 360 |
| 361 // static | |
| 362 void MostVisitedSites::RegisterProfilePrefs( | |
| 363 user_prefs::PrefRegistrySyncable* registry) { | |
| 364 registry->RegisterListPref(prefs::kNTPSuggestionsURL); | |
| 365 registry->RegisterListPref(prefs::kNTPSuggestionsSource); | |
| 366 } | |
| 367 | |
| 312 void MostVisitedSites::QueryMostVisitedURLs() { | 368 void MostVisitedSites::QueryMostVisitedURLs() { |
| 313 SuggestionsService* suggestions_service = | 369 SuggestionsService* suggestions_service = |
| 314 SuggestionsServiceFactory::GetForProfile(profile_); | 370 SuggestionsServiceFactory::GetForProfile(profile_); |
| 315 if (suggestions_service) { | 371 if (suggestions_service) { |
| 316 // Suggestions service is enabled, initiate a query. | 372 // Suggestions service is enabled, initiate a query. |
| 317 suggestions_service->FetchSuggestionsData( | 373 suggestions_service->FetchSuggestionsData( |
| 318 GetSyncState(profile_), | 374 GetSyncState(profile_), |
| 319 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, | 375 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, |
| 320 weak_ptr_factory_.GetWeakPtr())); | 376 weak_ptr_factory_.GetWeakPtr())); |
| 321 } else { | 377 } else { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 346 num_tiles = i; | 402 num_tiles = i; |
| 347 break; // This is the signal that there are no more real visited sites. | 403 break; // This is the signal that there are no more real visited sites. |
| 348 } | 404 } |
| 349 titles.push_back(visited.title); | 405 titles.push_back(visited.title); |
| 350 urls.push_back(visited.url.spec()); | 406 urls.push_back(visited.url.spec()); |
| 351 tile_sources_.push_back(kHistogramClientName); | 407 tile_sources_.push_back(kHistogramClientName); |
| 352 } | 408 } |
| 353 | 409 |
| 354 received_most_visited_sites_ = true; | 410 received_most_visited_sites_ = true; |
| 355 mv_source_ = TOP_SITES; | 411 mv_source_ = TOP_SITES; |
| 356 AddPopularSites(&titles, &urls); | 412 std::vector<base::string16> ordered_titles; |
| 357 NotifyMostVisitedURLsObserver(titles, urls); | 413 std::vector<std::string> ordered_urls; |
| 414 AddPopularSites(&ordered_titles, &ordered_urls, &titles, &urls); | |
| 415 NotifyMostVisitedURLsObserver(ordered_titles, ordered_urls); | |
| 358 } | 416 } |
| 359 | 417 |
| 360 void MostVisitedSites::OnSuggestionsProfileAvailable( | 418 void MostVisitedSites::OnSuggestionsProfileAvailable( |
| 361 const SuggestionsProfile& suggestions_profile) { | 419 const SuggestionsProfile& suggestions_profile) { |
| 362 int num_tiles = suggestions_profile.suggestions_size(); | 420 int num_tiles = suggestions_profile.suggestions_size(); |
| 363 // With no server suggestions, fall back to local Most Visited. | 421 // With no server suggestions, fall back to local Most Visited. |
| 364 if (num_tiles == 0) { | 422 if (num_tiles == 0) { |
| 365 InitiateTopSitesQuery(); | 423 InitiateTopSitesQuery(); |
| 366 return; | 424 return; |
| 367 } | 425 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 380 tile_source = | 438 tile_source = |
| 381 base::StringPrintf(kHistogramServerFormat, suggestion.providers(0)); | 439 base::StringPrintf(kHistogramServerFormat, suggestion.providers(0)); |
| 382 } else { | 440 } else { |
| 383 tile_source = kHistogramServerName; | 441 tile_source = kHistogramServerName; |
| 384 } | 442 } |
| 385 tile_sources_.push_back(tile_source); | 443 tile_sources_.push_back(tile_source); |
| 386 } | 444 } |
| 387 | 445 |
| 388 received_most_visited_sites_ = true; | 446 received_most_visited_sites_ = true; |
| 389 mv_source_ = SUGGESTIONS_SERVICE; | 447 mv_source_ = SUGGESTIONS_SERVICE; |
| 390 AddPopularSites(&titles, &urls); | 448 std::vector<base::string16> ordered_titles; |
| 391 NotifyMostVisitedURLsObserver(titles, urls); | 449 std::vector<std::string> ordered_urls; |
| 450 AddPopularSites(&ordered_titles, &ordered_urls, &titles, &urls); | |
| 451 NotifyMostVisitedURLsObserver(ordered_titles, ordered_urls); | |
| 392 } | 452 } |
| 393 | 453 |
| 394 void MostVisitedSites::AddPopularSites(std::vector<base::string16>* titles, | 454 void MostVisitedSites::AddPopularSites( |
|
Marc Treib
2015/09/07 11:12:59
Any reason why this doesn't just update |titles| a
knn
2015/09/08 10:48:56
Did you mean inplace? I did not want to complicate
Marc Treib
2015/09/08 11:39:49
I just meant that the caller shouldn't have to pas
| |
| 395 std::vector<std::string>* urls) { | 455 std::vector<base::string16>* result_titles, |
| 396 if (!popular_sites_) | 456 std::vector<std::string>* result_urls, |
| 397 return; | 457 std::vector<base::string16>* personal_titles, |
| 398 | 458 std::vector<std::string>* personal_urls) { |
| 399 DCHECK_EQ(titles->size(), urls->size()); | 459 size_t num_personal_suggestions = personal_titles->size(); |
| 400 DCHECK_EQ(titles->size(), tile_sources_.size()); | 460 DCHECK_EQ(num_personal_suggestions, personal_urls->size()); |
| 401 DCHECK_LE(static_cast<int>(titles->size()), num_sites_); | 461 DCHECK_EQ(num_personal_suggestions, tile_sources_.size()); |
| 402 | 462 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
| 403 // Collect all non-blacklisted popular suggestions. | 463 std::vector<std::string> personal_hosts; |
|
Marc Treib
2015/09/07 11:12:59
Make this an std::set? Would make checking members
knn
2015/09/08 10:48:55
Done.
| |
| 404 std::vector<base::string16> popular_titles; | 464 for (const std::string& personal_url : *personal_urls) { |
| 405 std::vector<std::string> popular_urls; | 465 personal_hosts.emplace_back(GURL(personal_url).host()); |
| 406 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | |
| 407 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | |
| 408 // Skip blacklisted sites. | |
| 409 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | |
| 410 continue; | |
| 411 | |
| 412 popular_titles.push_back(popular_site.title); | |
| 413 popular_urls.push_back(popular_site.url.spec()); | |
| 414 if (static_cast<int>(popular_titles.size()) >= num_sites_) | |
| 415 break; | |
| 416 } | 466 } |
| 417 | 467 |
| 418 AddPopularSitesImpl( | 468 // Collect non-blacklisted popular suggestions removing duplicates from |
|
Marc Treib
2015/09/07 11:12:59
nit: You're not removing anything from personal su
knn
2015/09/08 10:48:55
Done.
| |
| 419 num_sites_, popular_titles, popular_urls, titles, urls, &tile_sources_); | 469 // personal suggestions. |
| 470 size_t num_popular_suggestions = num_sites_ - num_personal_suggestions; | |
| 471 std::vector<base::string16> popular_titles; | |
| 472 popular_titles.reserve(num_popular_suggestions); | |
| 473 std::vector<std::string> popular_urls; | |
| 474 popular_urls.reserve(num_popular_suggestions); | |
| 475 std::vector<std::string> popular_hosts; | |
| 476 popular_hosts.reserve(num_popular_suggestions); | |
| 477 | |
| 478 if (num_popular_suggestions > 0 && popular_sites_) { | |
| 479 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | |
| 480 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | |
| 481 if (popular_titles.size() >= num_popular_suggestions) | |
|
Marc Treib
2015/09/07 11:12:59
Put this at the end, right after you've added one?
knn
2015/09/08 10:48:55
Done.
| |
| 482 break; | |
| 483 // Skip blacklisted sites. | |
| 484 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | |
| 485 continue; | |
| 486 std::string host = popular_site.url.host(); | |
| 487 // Skip suggestions already present in personal. | |
| 488 if (std::find(personal_hosts.begin(), personal_hosts.end(), host) != | |
| 489 personal_hosts.end()) { | |
| 490 continue; | |
| 491 } | |
| 492 | |
| 493 popular_titles.push_back(popular_site.title); | |
| 494 popular_urls.push_back(popular_site.url.spec()); | |
| 495 popular_hosts.emplace_back(host); | |
|
Marc Treib
2015/09/07 11:12:59
push_back
knn
2015/09/08 10:48:55
Done.
| |
| 496 } | |
| 497 } | |
| 498 num_popular_suggestions = popular_titles.size(); | |
| 499 size_t num_actual_tiles = num_personal_suggestions + num_popular_suggestions; | |
| 500 std::vector<std::string> old_sites_url; | |
| 501 std::vector<bool> old_sites_source; | |
| 502 GetPreviousNTPSites(&old_sites_url, &old_sites_source, profile_->GetPrefs(), | |
| 503 num_actual_tiles, *personal_urls); | |
| 504 std::vector<bool> result_is_personal; | |
| 505 MergeSuggestions(&result_is_personal, result_urls, result_titles, | |
| 506 num_actual_tiles, num_personal_suggestions, | |
| 507 num_popular_suggestions, personal_urls, personal_titles, | |
| 508 &popular_urls, &popular_titles, personal_hosts, | |
| 509 popular_hosts, old_sites_url, old_sites_source); | |
| 510 DCHECK_EQ(num_actual_tiles, result_urls->size()); | |
| 511 DCHECK_EQ(num_actual_tiles, result_titles->size()); | |
| 512 DCHECK_EQ(num_actual_tiles, result_is_personal.size()); | |
| 513 SaveCurrentNTPSites(*result_urls, result_is_personal, profile_->GetPrefs()); | |
| 420 } | 514 } |
| 421 | 515 |
| 422 // static | 516 // static |
| 423 void MostVisitedSites::AddPopularSitesImpl( | 517 void MostVisitedSites::MergeSuggestions( |
| 424 int num_sites, | 518 // Return values. |
| 425 const std::vector<base::string16>& popular_titles, | 519 std::vector<bool>* result_is_personal, |
| 426 const std::vector<std::string>& popular_urls, | 520 std::vector<std::string>* result_urls, |
| 427 std::vector<base::string16>* titles, | 521 std::vector<base::string16>* result_titles, |
| 428 std::vector<std::string>* urls, | 522 // Arguments. |
| 429 std::vector<std::string>* tile_sources) { | 523 size_t num_tiles, |
| 430 // Start off with the popular suggestions. | 524 size_t num_personal_suggestions, |
| 431 std::vector<base::string16> new_titles(popular_titles); | 525 size_t num_popular_suggestions, |
| 432 std::vector<std::string> new_urls(popular_urls); | 526 // Passed as ptr for move semantics only. |
| 433 std::vector<std::string> new_tile_sources(new_titles.size(), | 527 std::vector<std::string>* personal_urls, |
| 434 kHistogramPopularName); | 528 std::vector<base::string16>* personal_titles, |
| 529 std::vector<std::string>* popular_urls, | |
| 530 std::vector<base::string16>* popular_titles, | |
| 531 const std::vector<std::string>& personal_hosts, | |
| 532 const std::vector<std::string>& popular_hosts, | |
| 533 const std::vector<std::string>& old_sites_url, | |
| 534 const std::vector<bool>& old_sites_source) { | |
| 535 // Initialize return values. | |
| 536 result_is_personal->resize(num_tiles, false); | |
| 537 result_urls->resize(num_tiles); | |
| 538 result_titles->resize(num_tiles); | |
| 435 | 539 |
| 436 // Now, go over the personalized suggestions and replace matching popular | 540 std::vector<bool> result_is_filled(num_tiles, false); |
| 437 // suggestions. This is so that when some of the popular suggestions become | 541 size_t num_old_tiles = old_sites_url.size(); |
| 438 // personal, they retain their absolute positions. | 542 DCHECK_LE(num_old_tiles, num_tiles); |
|
Marc Treib
2015/09/07 11:12:59
I don't think this is necessarily always true?
knn
2015/09/08 10:48:56
It is because GetPreviousNTPSites ensures that.
| |
| 439 std::vector<base::string16> titles_to_insert; | 543 std::vector<std::string> old_sites_host; |
| 440 std::vector<std::string> urls_to_insert; | 544 old_sites_host.reserve(num_old_tiles); |
| 441 std::vector<std::string> tile_sources_to_insert; | 545 for (size_t i = 0; i < num_old_tiles; ++i) { |
| 442 for (size_t site_index = 0; site_index < titles->size(); site_index++) { | 546 old_sites_host.emplace_back( |
| 443 const base::string16& title = (*titles)[site_index]; | 547 old_sites_source[i] ? std::string() : GURL(old_sites_url[i]).host()); |
| 444 const std::string& url = (*urls)[site_index]; | 548 } |
| 445 const std::string& tile_source = (*tile_sources)[site_index]; | 549 |
| 446 // See if we already have a matching popular site. | 550 std::vector<size_t> personal_suggestions_to_insert; |
| 447 bool found = false; | 551 // Insert personal suggestions if they existed previously. |
| 448 for (size_t i = 0; i < new_urls.size(); i++) { | 552 for (size_t i = 0; i < num_personal_suggestions; ++i) { |
| 449 if (new_tile_sources[i] == kHistogramPopularName && | 553 size_t position; |
| 450 GURL(new_urls[i]).host() == GURL(url).host()) { | 554 for (position = 0; position < num_old_tiles; ++position) { |
| 451 // We have a matching popular sites suggestion. Replace it with the | 555 if (result_is_filled[position]) |
| 452 // actual URL and title. | 556 continue; |
| 453 new_titles[i] = title; | 557 if (old_sites_url[position] == (*personal_urls)[i]) |
| 454 new_urls[i] = url; | |
| 455 new_tile_sources[i] = tile_source; | |
| 456 found = true; | |
| 457 break; | 558 break; |
| 458 } | 559 if (old_sites_host[position] == personal_hosts[i]) |
|
Marc Treib
2015/09/07 11:12:59
I think here you also need to check that the old s
knn
2015/09/08 10:48:55
I do that by only populating the host field for po
Marc Treib
2015/09/08 11:39:49
Acknowledged.
| |
| 560 break; | |
| 459 } | 561 } |
| 460 if (!found) { | 562 if (position == num_old_tiles) { |
| 461 titles_to_insert.push_back(title); | 563 personal_suggestions_to_insert.push_back(i); |
| 462 urls_to_insert.push_back(url); | 564 } else { |
| 463 tile_sources_to_insert.push_back(tile_source); | 565 (*result_urls)[position] = std::move((*personal_urls)[i]); |
|
Marc Treib
2015/09/07 11:12:59
We're not allowed to use C++11 library features ye
knn
2015/09/08 10:48:55
Done.
| |
| 566 (*result_titles)[position] = std::move((*personal_titles)[i]); | |
| 567 (*result_is_personal)[position] = true; | |
| 568 result_is_filled[position] = true; | |
| 464 } | 569 } |
| 465 } | 570 } |
| 466 | 571 |
| 467 // Append personalized suggestions at the end if there's room. | 572 std::vector<size_t> popular_suggestions_to_insert; |
| 468 size_t num_to_append = | 573 // Insert popular suggestions if they existed previously. |
| 469 std::min(static_cast<size_t>(num_sites) - new_titles.size(), | 574 for (size_t i = 0; i < num_popular_suggestions; ++i) { |
| 470 titles_to_insert.size()); | 575 size_t position; |
| 471 new_titles.insert(new_titles.end(), | 576 for (position = 0; position < num_old_tiles; ++position) { |
| 472 titles_to_insert.end() - num_to_append, | 577 if (result_is_filled[position]) |
| 473 titles_to_insert.end()); | 578 continue; |
| 474 new_urls.insert(new_urls.end(), | 579 if (old_sites_host[position] == popular_hosts[i]) |
| 475 urls_to_insert.end() - num_to_append, | |
| 476 urls_to_insert.end()); | |
| 477 new_tile_sources.insert(new_tile_sources.end(), | |
| 478 tile_sources_to_insert.end() - num_to_append, | |
| 479 tile_sources_to_insert.end()); | |
| 480 | |
| 481 // Finally, go over the remaining personalized suggestions and evict popular | |
| 482 // suggestions to accommodate them. Do it in reverse order, so the least | |
| 483 // important popular suggestions will be evicted. | |
| 484 for (size_t i = titles_to_insert.size() - num_to_append; i > 0; --i) { | |
| 485 const base::string16& title = titles_to_insert[i - 1]; | |
| 486 const std::string& url = urls_to_insert[i - 1]; | |
| 487 const std::string& tile_source = tile_sources_to_insert[i - 1]; | |
| 488 for (size_t insert_i = new_titles.size(); insert_i > 0; --insert_i) { | |
| 489 size_t insert_index = insert_i - 1; | |
| 490 if (new_tile_sources[insert_index] == kHistogramPopularName) { | |
| 491 new_titles[insert_index] = title; | |
| 492 new_urls[insert_index] = url; | |
| 493 new_tile_sources[insert_index] = tile_source; | |
| 494 break; | 580 break; |
| 495 } | 581 } |
| 582 if (position == num_old_tiles) { | |
| 583 popular_suggestions_to_insert.push_back(i); | |
| 584 } else { | |
| 585 (*result_urls)[position] = std::move((*popular_urls)[i]); | |
|
Marc Treib
2015/09/07 11:12:59
^^
| |
| 586 (*result_titles)[position] = std::move((*popular_titles)[i]); | |
| 587 result_is_filled[position] = true; | |
| 496 } | 588 } |
| 497 } | 589 } |
| 498 | 590 |
| 499 titles->swap(new_titles); | 591 // Insert leftover personal suggestions. |
| 500 urls->swap(new_urls); | 592 size_t insert_position = 0; |
| 501 tile_sources->swap(new_tile_sources); | 593 size_t insert_size = personal_suggestions_to_insert.size(); |
| 594 for (/*insert_position continued*/ size_t i = 0; | |
| 595 insert_position < num_tiles && i < insert_size; ++insert_position) { | |
| 596 if (result_is_filled[insert_position]) | |
| 597 continue; | |
| 598 size_t to_insert = personal_suggestions_to_insert[i++]; | |
| 599 (*result_urls)[insert_position] = std::move((*personal_urls)[to_insert]); | |
| 600 (*result_titles)[insert_position] = | |
| 601 std::move((*personal_titles)[to_insert]); | |
| 602 (*result_is_personal)[insert_position] = true; | |
| 603 // Ignore setting result_is_filled as it is isn't used anymore. | |
|
Marc Treib
2015/09/07 11:12:59
s/Ignore/Skip/g
| |
| 604 } | |
| 605 | |
| 606 // Insert leftover popular suggestions. | |
| 607 insert_size = popular_suggestions_to_insert.size(); | |
| 608 for (/*insert_position continued*/ size_t i = 0; | |
| 609 insert_position < num_tiles && i < insert_size; ++insert_position) { | |
| 610 if (result_is_filled[insert_position]) | |
| 611 continue; | |
| 612 size_t to_insert = popular_suggestions_to_insert[i++]; | |
| 613 (*result_urls)[insert_position] = std::move((*popular_urls)[to_insert]); | |
| 614 (*result_titles)[insert_position] = std::move((*popular_titles)[to_insert]); | |
| 615 // Ignore setting result_is_filled as it is isn't used anymore. | |
| 616 } | |
| 502 } | 617 } |
| 503 | 618 |
| 504 void MostVisitedSites::NotifyMostVisitedURLsObserver( | 619 void MostVisitedSites::NotifyMostVisitedURLsObserver( |
| 505 const std::vector<base::string16>& titles, | 620 const std::vector<base::string16>& titles, |
| 506 const std::vector<std::string>& urls) { | 621 const std::vector<std::string>& urls) { |
| 507 DCHECK_EQ(titles.size(), urls.size()); | 622 DCHECK_EQ(titles.size(), urls.size()); |
| 508 if (received_most_visited_sites_ && received_popular_sites_ && | 623 if (received_most_visited_sites_ && received_popular_sites_ && |
| 509 !recorded_uma_) { | 624 !recorded_uma_) { |
| 510 RecordImpressionUMAMetrics(); | 625 RecordImpressionUMAMetrics(); |
| 511 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, titles.size()); | 626 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, titles.size()); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 569 // The displayed suggestions are invalidated. | 684 // The displayed suggestions are invalidated. |
| 570 QueryMostVisitedURLs(); | 685 QueryMostVisitedURLs(); |
| 571 } | 686 } |
| 572 } | 687 } |
| 573 | 688 |
| 574 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 689 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 575 MostVisitedSites* most_visited_sites = | 690 MostVisitedSites* most_visited_sites = |
| 576 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 691 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 577 return reinterpret_cast<intptr_t>(most_visited_sites); | 692 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 578 } | 693 } |
| OLD | NEW |