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