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

Side by Side Diff: chrome/browser/android/most_visited_sites.cc

Issue 1330773002: [Android] Persist ordering of NTP suggestions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
132 } // namespace 135 } // namespace
133 136
137 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
138 const std::string& url,
139 MostVisitedSource source)
140 : title(title),
141 url(url),
142 host(GURL(url).host()),
143 source(source),
144 provider_index(-1) {}
145
146 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
147 const std::string& url,
148 const std::string& host,
149 MostVisitedSource source)
150 : title(title), url(url), host(host), source(source), provider_index(-1) {}
151
152 MostVisitedSites::Suggestion::Suggestion(const base::string16& title,
153 const std::string& url,
154 MostVisitedSource source,
155 int provider_index)
156 : title(title),
157 url(url),
158 host(GURL(url).host()),
159 source(source),
160 provider_index(provider_index) {
161 DCHECK(source == MostVisitedSites::SUGGESTIONS_SERVICE);
Bernhard Bauer 2015/09/09 12:52:15 Use DCHECK_EQ for nicer error messages.
knn 2015/09/09 13:54:31 Done.
162 }
163
164 MostVisitedSites::Suggestion::~Suggestion() {}
165
166 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const {
167 switch (source) {
168 case MostVisitedSites::TOP_SITES:
169 return kHistogramClientName;
170 case MostVisitedSites::POPULAR:
171 return kHistogramPopularName;
172 case MostVisitedSites::SUGGESTIONS_SERVICE:
173 return provider_index >= 0
174 ? base::StringPrintf(kHistogramServerFormat, provider_index)
175 : kHistogramServerName;
176 }
177 NOTREACHED();
178 return std::string();
179 }
180
134 MostVisitedSites::MostVisitedSites(Profile* profile) 181 MostVisitedSites::MostVisitedSites(Profile* profile)
135 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), 182 : profile_(profile), num_sites_(0), received_most_visited_sites_(false),
136 received_popular_sites_(false), recorded_uma_(false), 183 received_popular_sites_(false), recorded_uma_(false),
137 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), 184 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0),
138 scoped_observer_(this), weak_ptr_factory_(this) { 185 scoped_observer_(this), weak_ptr_factory_(this) {
139 // Register the debugging page for the Suggestions Service and the thumbnails 186 // Register the debugging page for the Suggestions Service and the thumbnails
140 // debugging page. 187 // debugging page.
141 content::URLDataSource::Add(profile_, 188 content::URLDataSource::Add(profile_,
142 new suggestions::SuggestionsSource(profile_)); 189 new suggestions::SuggestionsSource(profile_));
143 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 190 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 url, base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, 331 url, base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable,
285 weak_ptr_factory_.GetWeakPtr()), 332 weak_ptr_factory_.GetWeakPtr()),
286 base::Closure()); 333 base::Closure());
287 } 334 }
288 } 335 }
289 336
290 void MostVisitedSites::RecordOpenedMostVisitedItem(JNIEnv* env, 337 void MostVisitedSites::RecordOpenedMostVisitedItem(JNIEnv* env,
291 jobject obj, 338 jobject obj,
292 jint index) { 339 jint index) {
293 DCHECK_GE(index, 0); 340 DCHECK_GE(index, 0);
294 DCHECK_LT(index, static_cast<int>(tile_sources_.size())); 341 DCHECK_LT(index, static_cast<int>(current_suggestions_.size()));
295 std::string histogram = base::StringPrintf(kOpenedItemHistogramFormat, 342 std::string histogram = base::StringPrintf(
296 tile_sources_[index].c_str()); 343 kOpenedItemHistogramFormat,
344 current_suggestions_[index]->GetSourceHistogramName().c_str());
297 LogHistogramEvent(histogram, index, num_sites_); 345 LogHistogramEvent(histogram, index, num_sites_);
298 } 346 }
299 347
300 void MostVisitedSites::OnStateChanged() { 348 void MostVisitedSites::OnStateChanged() {
301 // There have been changes to the sync state. This class cares about a few 349 // There have been changes to the sync state. This class cares about a few
302 // (just initialized, enabled/disabled or history sync state changed). Re-run 350 // (just initialized, enabled/disabled or history sync state changed). Re-run
303 // the query code which will use the proper state. 351 // the query code which will use the proper state.
304 QueryMostVisitedURLs(); 352 QueryMostVisitedURLs();
305 } 353 }
306 354
307 // static 355 // static
308 bool MostVisitedSites::Register(JNIEnv* env) { 356 bool MostVisitedSites::Register(JNIEnv* env) {
309 return RegisterNativesImpl(env); 357 return RegisterNativesImpl(env);
310 } 358 }
311 359
360 // static
361 void MostVisitedSites::RegisterProfilePrefs(
362 user_prefs::PrefRegistrySyncable* registry) {
363 registry->RegisterListPref(prefs::kNTPSuggestionsURL);
364 registry->RegisterListPref(prefs::kNTPSuggestionsIsPersonal);
365 }
366
312 void MostVisitedSites::QueryMostVisitedURLs() { 367 void MostVisitedSites::QueryMostVisitedURLs() {
313 SuggestionsService* suggestions_service = 368 SuggestionsService* suggestions_service =
314 SuggestionsServiceFactory::GetForProfile(profile_); 369 SuggestionsServiceFactory::GetForProfile(profile_);
315 if (suggestions_service) { 370 if (suggestions_service) {
316 // Suggestions service is enabled, initiate a query. 371 // Suggestions service is enabled, initiate a query.
317 suggestions_service->FetchSuggestionsData( 372 suggestions_service->FetchSuggestionsData(
318 GetSyncState(profile_), 373 GetSyncState(profile_),
319 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, 374 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable,
320 weak_ptr_factory_.GetWeakPtr())); 375 weak_ptr_factory_.GetWeakPtr()));
321 } else { 376 } else {
322 InitiateTopSitesQuery(); 377 InitiateTopSitesQuery();
323 } 378 }
324 } 379 }
325 380
326 void MostVisitedSites::InitiateTopSitesQuery() { 381 void MostVisitedSites::InitiateTopSitesQuery() {
327 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); 382 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_);
328 if (!top_sites) 383 if (!top_sites)
329 return; 384 return;
330 385
331 top_sites->GetMostVisitedURLs( 386 top_sites->GetMostVisitedURLs(
332 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, 387 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable,
333 weak_ptr_factory_.GetWeakPtr()), 388 weak_ptr_factory_.GetWeakPtr()),
334 false); 389 false);
335 } 390 }
336 391
337 void MostVisitedSites::OnMostVisitedURLsAvailable( 392 void MostVisitedSites::OnMostVisitedURLsAvailable(
338 const history::MostVisitedURLList& visited_list) { 393 const history::MostVisitedURLList& visited_list) {
339 std::vector<base::string16> titles; 394 ScopedVector<Suggestion> suggestions;
340 std::vector<std::string> urls; 395 size_t num_tiles =
341 tile_sources_.clear(); 396 std::min(visited_list.size(), static_cast<size_t>(num_sites_));
342 int num_tiles = std::min(static_cast<int>(visited_list.size()), num_sites_); 397 for (size_t i = 0; i < num_tiles; ++i) {
343 for (int i = 0; i < num_tiles; ++i) {
344 const history::MostVisitedURL& visited = visited_list[i]; 398 const history::MostVisitedURL& visited = visited_list[i];
345 if (visited.url.is_empty()) { 399 if (visited.url.is_empty()) {
346 num_tiles = i; 400 num_tiles = i;
347 break; // This is the signal that there are no more real visited sites. 401 break; // This is the signal that there are no more real visited sites.
348 } 402 }
349 titles.push_back(visited.title); 403 suggestions.push_back(
350 urls.push_back(visited.url.spec()); 404 new Suggestion(visited.title, visited.url.spec(), TOP_SITES));
351 tile_sources_.push_back(kHistogramClientName);
352 } 405 }
353 406
354 received_most_visited_sites_ = true; 407 received_most_visited_sites_ = true;
355 mv_source_ = TOP_SITES; 408 mv_source_ = TOP_SITES;
356 AddPopularSites(&titles, &urls); 409 AddPopularSites(&suggestions);
357 NotifyMostVisitedURLsObserver(titles, urls); 410 NotifyMostVisitedURLsObserver();
358 } 411 }
359 412
360 void MostVisitedSites::OnSuggestionsProfileAvailable( 413 void MostVisitedSites::OnSuggestionsProfileAvailable(
361 const SuggestionsProfile& suggestions_profile) { 414 const SuggestionsProfile& suggestions_profile) {
362 int num_tiles = suggestions_profile.suggestions_size(); 415 int num_tiles = suggestions_profile.suggestions_size();
363 // With no server suggestions, fall back to local Most Visited. 416 // With no server suggestions, fall back to local Most Visited.
364 if (num_tiles == 0) { 417 if (num_tiles == 0) {
365 InitiateTopSitesQuery(); 418 InitiateTopSitesQuery();
366 return; 419 return;
367 } 420 }
368 if (num_sites_ < num_tiles) 421 if (num_sites_ < num_tiles)
369 num_tiles = num_sites_; 422 num_tiles = num_sites_;
370 423
371 std::vector<base::string16> titles; 424 ScopedVector<Suggestion> suggestions;
372 std::vector<std::string> urls;
373 tile_sources_.clear();
374 for (int i = 0; i < num_tiles; ++i) { 425 for (int i = 0; i < num_tiles; ++i) {
375 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); 426 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i);
376 titles.push_back(base::UTF8ToUTF16(suggestion.title())); 427 suggestions.push_back(new Suggestion(
377 urls.push_back(suggestion.url()); 428 base::UTF8ToUTF16(suggestion.title()), suggestion.url(),
378 std::string tile_source; 429 SUGGESTIONS_SERVICE,
379 if (suggestion.providers_size() > 0) { 430 suggestion.providers_size() > 0 ? suggestion.providers(0) : -1));
380 tile_source =
381 base::StringPrintf(kHistogramServerFormat, suggestion.providers(0));
382 } else {
383 tile_source = kHistogramServerName;
384 }
385 tile_sources_.push_back(tile_source);
386 } 431 }
387 432
388 received_most_visited_sites_ = true; 433 received_most_visited_sites_ = true;
389 mv_source_ = SUGGESTIONS_SERVICE; 434 mv_source_ = SUGGESTIONS_SERVICE;
390 AddPopularSites(&titles, &urls); 435 AddPopularSites(&suggestions);
391 NotifyMostVisitedURLsObserver(titles, urls); 436 NotifyMostVisitedURLsObserver();
392 } 437 }
393 438
394 void MostVisitedSites::AddPopularSites(std::vector<base::string16>* titles, 439 void MostVisitedSites::AddPopularSites(
395 std::vector<std::string>* urls) { 440 ScopedVector<Suggestion>* personal_suggestions) {
396 if (!popular_sites_) 441 size_t num_personal_suggestions = personal_suggestions->size();
442 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_));
443
444 // Collect non-blacklisted popular suggestions, skipping those already present
445 // in the personal suggestions.
446 size_t num_popular_suggestions = num_sites_ - num_personal_suggestions;
447 ScopedVector<Suggestion> popular_suggestions;
448 popular_suggestions.reserve(num_popular_suggestions);
449
450 if (num_popular_suggestions > 0 && popular_sites_) {
451 std::set<std::string> personal_hosts;
452 for (const Suggestion* suggestion : *personal_suggestions)
453 personal_hosts.insert(suggestion->host);
454 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_));
455 for (const PopularSites::Site& popular_site : popular_sites_->sites()) {
456 // Skip blacklisted sites.
457 if (top_sites && top_sites->IsBlacklisted(popular_site.url))
458 continue;
459 std::string host = popular_site.url.host();
460 // Skip suggestions already present in personal.
461 if (personal_hosts.find(host) != personal_hosts.end())
462 continue;
463
464 popular_suggestions.push_back(new Suggestion(
465 popular_site.title, popular_site.url.spec(), host, POPULAR));
466 if (popular_suggestions.size() >= num_popular_suggestions)
467 break;
468 }
469 }
470 num_popular_suggestions = popular_suggestions.size();
471 size_t num_actual_tiles = num_personal_suggestions + num_popular_suggestions;
472 std::vector<std::string> old_sites_url;
473 std::vector<bool> old_sites_is_personal;
474 GetPreviousNTPSites(num_actual_tiles, &old_sites_url, &old_sites_is_personal);
475 ScopedVector<Suggestion> merged_suggestions =
476 MergeSuggestions(personal_suggestions, &popular_suggestions,
477 old_sites_url, old_sites_is_personal);
478 DCHECK_EQ(num_actual_tiles, merged_suggestions.size());
479 current_suggestions_.swap(merged_suggestions);
480 if (received_popular_sites_)
481 SaveCurrentNTPSites();
482 }
483
484 // static
485 ScopedVector<MostVisitedSites::Suggestion> MostVisitedSites::MergeSuggestions(
486 ScopedVector<Suggestion>* personal_suggestions,
487 ScopedVector<Suggestion>* popular_suggestions,
488 const std::vector<std::string>& old_sites_url,
489 const std::vector<bool>& old_sites_is_personal) {
490 size_t num_personal_suggestions = personal_suggestions->size();
491 size_t num_popular_suggestions = popular_suggestions->size();
492 size_t num_tiles = num_popular_suggestions + num_personal_suggestions;
493 ScopedVector<Suggestion> merged_suggestions;
494 merged_suggestions.resize(num_tiles);
495
496 size_t num_old_tiles = old_sites_url.size();
497 DCHECK_LE(num_old_tiles, num_tiles);
498 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size());
499 std::vector<std::string> old_sites_host;
500 old_sites_host.reserve(num_old_tiles);
501 // Only populate the hosts for popular suggestions as only they can be
502 // replaced by host. Personal suggestions require an exact url match to be
503 // replaced.
504 for (size_t i = 0; i < num_old_tiles; ++i) {
505 old_sites_host.push_back(old_sites_is_personal[i]
506 ? std::string()
507 : GURL(old_sites_url[i]).host());
508 }
509
510 // Insert personal suggestions if they existed previously.
511 std::vector<size_t> new_personal_suggestions = InsertMatchingSuggestions(
512 personal_suggestions, &merged_suggestions, old_sites_url, old_sites_host);
513 // Insert popular suggestions if they existed previously.
514 std::vector<size_t> new_popular_suggestions = InsertMatchingSuggestions(
515 popular_suggestions, &merged_suggestions, old_sites_url, old_sites_host);
516 // Insert leftover personal suggestions.
517 size_t filled_so_far = InsertAllSuggestions(
518 0, new_personal_suggestions, personal_suggestions, &merged_suggestions);
519 // Insert leftover popular suggestions.
520 InsertAllSuggestions(filled_so_far, new_popular_suggestions,
521 popular_suggestions, &merged_suggestions);
522 return merged_suggestions.Pass();
523 }
524
525 void MostVisitedSites::GetPreviousNTPSites(
526 size_t num_tiles,
527 std::vector<std::string>* old_sites_url,
528 std::vector<bool>* old_sites_is_personal) {
529 const PrefService* prefs = profile_->GetPrefs();
530 const base::ListValue* url_list = prefs->GetList(prefs::kNTPSuggestionsURL);
531 const base::ListValue* source_list =
532 prefs->GetList(prefs::kNTPSuggestionsIsPersonal);
533 DCHECK_EQ(url_list->GetSize(), source_list->GetSize());
534 if (url_list->GetSize() < num_tiles)
535 num_tiles = url_list->GetSize();
536 if (num_tiles == 0) {
537 // No fallback required as Personal suggestions take precedence anyway.
397 return; 538 return;
398 539 }
399 DCHECK_EQ(titles->size(), urls->size()); 540 old_sites_url->reserve(num_tiles);
400 DCHECK_EQ(titles->size(), tile_sources_.size()); 541 old_sites_is_personal->reserve(num_tiles);
401 DCHECK_LE(static_cast<int>(titles->size()), num_sites_); 542 for (size_t i = 0; i < num_tiles; ++i) {
402 543 std::string url_string;
403 // Collect all non-blacklisted popular suggestions. 544 bool success = url_list->GetString(i, &url_string);
404 std::vector<base::string16> popular_titles; 545 DCHECK(success);
405 std::vector<std::string> popular_urls; 546 old_sites_url->push_back(url_string);
406 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); 547 bool is_personal;
407 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { 548 success = source_list->GetBoolean(i, &is_personal);
408 // Skip blacklisted sites. 549 DCHECK(success);
409 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) 550 old_sites_is_personal->push_back(is_personal);
551 }
552 }
553
554 void MostVisitedSites::SaveCurrentNTPSites() {
555 base::ListValue url_list;
556 base::ListValue source_list;
557 for (const Suggestion* suggestion : current_suggestions_) {
558 url_list.AppendString(suggestion->url);
559 source_list.AppendBoolean(suggestion->source != MostVisitedSites::POPULAR);
560 }
561 PrefService* prefs = profile_->GetPrefs();
562 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list);
563 prefs->Set(prefs::kNTPSuggestionsURL, url_list);
564 }
565
566 // static
567 std::vector<size_t> MostVisitedSites::InsertMatchingSuggestions(
568 ScopedVector<Suggestion>* src_suggestions,
569 ScopedVector<Suggestion>* dst_suggestions,
570 const std::vector<std::string>& match_urls,
571 const std::vector<std::string>& match_hosts) {
572 std::vector<size_t> unmatched_suggestions;
573 size_t num_src_suggestions = src_suggestions->size();
574 size_t num_matchers = match_urls.size();
575 for (size_t i = 0; i < num_src_suggestions; ++i) {
576 size_t position;
577 for (position = 0; position < num_matchers; ++position) {
578 if ((*dst_suggestions)[position] != nullptr)
579 continue;
580 if (match_urls[position] == (*src_suggestions)[i]->url)
581 break;
582 // match_hosts is only populated for suggestions which can be replaced by
583 // host matching like popular suggestions.
584 if (match_hosts[position] == (*src_suggestions)[i]->host)
585 break;
586 }
587 if (position == num_matchers) {
588 unmatched_suggestions.push_back(i);
589 } else {
590 // A move is required as the source and destination containers own the
591 // elements.
592 std::swap((*dst_suggestions)[position], (*src_suggestions)[i]);
593 }
594 }
595 return unmatched_suggestions;
596 }
597
598 // static
599 size_t MostVisitedSites::InsertAllSuggestions(
600 size_t start_position,
601 const std::vector<size_t>& insert_positions,
602 ScopedVector<Suggestion>* src_suggestions,
603 ScopedVector<Suggestion>* dst_suggestions) {
604 size_t num_inserts = insert_positions.size();
605 size_t num_dests = dst_suggestions->size();
606
607 size_t src_pos = 0;
608 size_t i = start_position;
609 for (; i < num_dests && src_pos < num_inserts; ++i) {
610 if ((*dst_suggestions)[i] != nullptr)
410 continue; 611 continue;
411 612 size_t src = insert_positions[src_pos++];
412 popular_titles.push_back(popular_site.title); 613 std::swap((*dst_suggestions)[i], (*src_suggestions)[src]);
413 popular_urls.push_back(popular_site.url.spec()); 614 }
414 if (static_cast<int>(popular_titles.size()) >= num_sites_) 615 // Return destination postions filled so far which becomes the start_position
415 break; 616 // for future runs.
416 } 617 return i;
417 618 }
418 AddPopularSitesImpl( 619
419 num_sites_, popular_titles, popular_urls, titles, urls, &tile_sources_); 620 void MostVisitedSites::NotifyMostVisitedURLsObserver() {
420 } 621 size_t num_suggestions = current_suggestions_.size();
421
422 // static
423 void MostVisitedSites::AddPopularSitesImpl(
424 int num_sites,
425 const std::vector<base::string16>& popular_titles,
426 const std::vector<std::string>& popular_urls,
427 std::vector<base::string16>* titles,
428 std::vector<std::string>* urls,
429 std::vector<std::string>* tile_sources) {
430 // Start off with the popular suggestions.
431 std::vector<base::string16> new_titles(popular_titles);
432 std::vector<std::string> new_urls(popular_urls);
433 std::vector<std::string> new_tile_sources(new_titles.size(),
434 kHistogramPopularName);
435
436 // Now, go over the personalized suggestions and replace matching popular
437 // suggestions. This is so that when some of the popular suggestions become
438 // personal, they retain their absolute positions.
439 std::vector<base::string16> titles_to_insert;
440 std::vector<std::string> urls_to_insert;
441 std::vector<std::string> tile_sources_to_insert;
442 for (size_t site_index = 0; site_index < titles->size(); site_index++) {
443 const base::string16& title = (*titles)[site_index];
444 const std::string& url = (*urls)[site_index];
445 const std::string& tile_source = (*tile_sources)[site_index];
446 // See if we already have a matching popular site.
447 bool found = false;
448 for (size_t i = 0; i < new_urls.size(); i++) {
449 if (new_tile_sources[i] == kHistogramPopularName &&
450 GURL(new_urls[i]).host() == GURL(url).host()) {
451 // We have a matching popular sites suggestion. Replace it with the
452 // actual URL and title.
453 new_titles[i] = title;
454 new_urls[i] = url;
455 new_tile_sources[i] = tile_source;
456 found = true;
457 break;
458 }
459 }
460 if (!found) {
461 titles_to_insert.push_back(title);
462 urls_to_insert.push_back(url);
463 tile_sources_to_insert.push_back(tile_source);
464 }
465 }
466
467 // Append personalized suggestions at the end if there's room.
468 size_t num_to_append =
469 std::min(static_cast<size_t>(num_sites) - new_titles.size(),
470 titles_to_insert.size());
471 new_titles.insert(new_titles.end(),
472 titles_to_insert.end() - num_to_append,
473 titles_to_insert.end());
474 new_urls.insert(new_urls.end(),
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;
495 }
496 }
497 }
498
499 titles->swap(new_titles);
500 urls->swap(new_urls);
501 tile_sources->swap(new_tile_sources);
502 }
503
504 void MostVisitedSites::NotifyMostVisitedURLsObserver(
505 const std::vector<base::string16>& titles,
506 const std::vector<std::string>& urls) {
507 DCHECK_EQ(titles.size(), urls.size());
508 if (received_most_visited_sites_ && received_popular_sites_ && 622 if (received_most_visited_sites_ && received_popular_sites_ &&
509 !recorded_uma_) { 623 !recorded_uma_) {
510 RecordImpressionUMAMetrics(); 624 RecordImpressionUMAMetrics();
511 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, titles.size()); 625 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, num_suggestions);
512 recorded_uma_ = true; 626 recorded_uma_ = true;
513 } 627 }
628 std::vector<base::string16> titles;
629 std::vector<std::string> urls;
630 titles.reserve(num_suggestions);
631 urls.reserve(num_suggestions);
632 for (const Suggestion* suggestion : current_suggestions_) {
633 titles.push_back(suggestion->title);
634 urls.push_back(suggestion->url);
635 }
514 JNIEnv* env = AttachCurrentThread(); 636 JNIEnv* env = AttachCurrentThread();
637 DCHECK_EQ(titles.size(), urls.size());
515 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( 638 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable(
516 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), 639 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(),
517 ToJavaArrayOfStrings(env, urls).obj()); 640 ToJavaArrayOfStrings(env, urls).obj());
518 } 641 }
519 642
520 void MostVisitedSites::OnPopularSitesAvailable(bool success) { 643 void MostVisitedSites::OnPopularSitesAvailable(bool success) {
521 received_popular_sites_ = true; 644 received_popular_sites_ = true;
522 645
523 if (!success) { 646 if (!success) {
524 LOG(WARNING) << "Download of popular sites failed"; 647 LOG(WARNING) << "Download of popular sites failed";
(...skipping 21 matching lines...) Expand all
546 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName, 669 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName,
547 num_local_thumbs_); 670 num_local_thumbs_);
548 num_local_thumbs_ = 0; 671 num_local_thumbs_ = 0;
549 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_); 672 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_);
550 num_empty_thumbs_ = 0; 673 num_empty_thumbs_ = 0;
551 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); 674 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_);
552 num_server_thumbs_ = 0; 675 num_server_thumbs_ = 0;
553 } 676 }
554 677
555 void MostVisitedSites::RecordImpressionUMAMetrics() { 678 void MostVisitedSites::RecordImpressionUMAMetrics() {
556 for (size_t i = 0; i < tile_sources_.size(); i++) { 679 for (size_t i = 0; i < current_suggestions_.size(); i++) {
557 std::string histogram = base::StringPrintf(kImpressionHistogramFormat, 680 std::string histogram = base::StringPrintf(
558 tile_sources_[i].c_str()); 681 kImpressionHistogramFormat,
682 current_suggestions_[i]->GetSourceHistogramName().c_str());
559 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); 683 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_);
560 } 684 }
561 } 685 }
562 686
563 void MostVisitedSites::TopSitesLoaded(history::TopSites* top_sites) { 687 void MostVisitedSites::TopSitesLoaded(history::TopSites* top_sites) {
564 } 688 }
565 689
566 void MostVisitedSites::TopSitesChanged(history::TopSites* top_sites, 690 void MostVisitedSites::TopSitesChanged(history::TopSites* top_sites,
567 ChangeReason change_reason) { 691 ChangeReason change_reason) {
568 if (mv_source_ == TOP_SITES) { 692 if (mv_source_ == TOP_SITES) {
569 // The displayed suggestions are invalidated. 693 // The displayed suggestions are invalidated.
570 QueryMostVisitedURLs(); 694 QueryMostVisitedURLs();
571 } 695 }
572 } 696 }
573 697
574 static jlong Init(JNIEnv* env, 698 static jlong Init(JNIEnv* env,
575 const JavaParamRef<jobject>& obj, 699 const JavaParamRef<jobject>& obj,
576 const JavaParamRef<jobject>& jprofile) { 700 const JavaParamRef<jobject>& jprofile) {
577 MostVisitedSites* most_visited_sites = 701 MostVisitedSites* most_visited_sites =
578 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); 702 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile));
579 return reinterpret_cast<intptr_t>(most_visited_sites); 703 return reinterpret_cast<intptr_t>(most_visited_sites);
580 } 704 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698