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(const PrefService* prefs, | |
| 136 size_t num_tiles, | |
| 137 std::vector<std::string>* old_sites_url, | |
| 138 std::vector<bool>* old_sites_source) { | |
| 139 const base::ListValue* url_list = prefs->GetList(prefs::kNTPSuggestionsURL); | |
| 140 std::string url_string; | |
|
Bernhard Bauer
2015/09/08 15:44:25
Move this to line 149?
knn
2015/09/09 09:35:37
Done.
| |
| 141 if (url_list->GetSize() < num_tiles) | |
| 142 num_tiles = url_list->GetSize(); | |
| 143 if (num_tiles == 0) { | |
| 144 // No fallback required as Personal suggestions take precedence anyway. | |
| 145 return; | |
| 146 } | |
| 147 old_sites_url->reserve(num_tiles); | |
| 148 for (size_t i = 0; i < num_tiles; ++i) { | |
| 149 if (url_list->GetString(i, &url_string)) { | |
| 150 old_sites_url->push_back(url_string); | |
| 151 } | |
| 152 } | |
| 153 old_sites_source->reserve(num_tiles); | |
| 154 const base::ListValue* source_list = | |
| 155 prefs->GetList(prefs::kNTPSuggestionsIsPersonal); | |
| 156 DCHECK_EQ(url_list->GetSize(), source_list->GetSize()); | |
| 157 bool is_personal; | |
| 158 for (size_t i = 0; i < num_tiles; ++i) { | |
| 159 if (source_list->GetBoolean(i, &is_personal)) { | |
| 160 old_sites_source->push_back(is_personal); | |
| 161 } | |
| 162 } | |
| 163 DCHECK_EQ(num_tiles, old_sites_source->size()); | |
|
Bernhard Bauer
2015/09/08 15:44:25
If you DCHECK this, it implies you expect the chec
knn
2015/09/09 09:35:38
Done.
| |
| 164 } | |
| 165 | |
| 166 void SaveCurrentNTPSites( | |
| 167 const ScopedVector<MostVisitedSites::Suggestion>& suggestions, | |
| 168 PrefService* prefs) { | |
| 169 base::ListValue url_list; | |
| 170 base::ListValue source_list; | |
| 171 for (const auto suggestion : suggestions) { | |
|
Bernhard Bauer
2015/09/08 15:44:25
Use a const reference (otherwise you'll make a cop
knn
2015/09/09 09:35:38
Done.
| |
| 172 url_list.AppendString(suggestion->url); | |
| 173 source_list.AppendBoolean(suggestion->source != MostVisitedSites::POPULAR); | |
| 174 } | |
| 175 prefs->Set(prefs::kNTPSuggestionsIsPersonal, source_list); | |
| 176 prefs->Set(prefs::kNTPSuggestionsURL, url_list); | |
| 177 } | |
| 178 | |
| 179 // Takes suggestions from |src_suggestions| and moves them to |dst_suggestions| | |
| 180 // if the suggestion's url/host matches |match_urls|/|match_hosts| respectively. | |
| 181 // Unmatched suggestion indices from |src_suggestions| are returned for ease of | |
| 182 // insertion later. | |
| 183 std::vector<size_t> InsertMatchingSuggestions( | |
| 184 ScopedVector<MostVisitedSites::Suggestion>* src_suggestions, | |
| 185 ScopedVector<MostVisitedSites::Suggestion>* dst_suggestions, | |
| 186 const std::vector<std::string>& match_urls, | |
| 187 const std::vector<std::string>& match_hosts) { | |
| 188 std::vector<size_t> unmatched_suggestions; | |
| 189 size_t num_src_suggestions = src_suggestions->size(); | |
| 190 size_t num_matchers = match_urls.size(); | |
| 191 for (size_t i = 0; i < num_src_suggestions; ++i) { | |
| 192 size_t position; | |
|
Bernhard Bauer
2015/09/08 15:44:25
Move this into the loop header?
knn
2015/09/09 09:35:38
I am using the result outside the loop.
| |
| 193 for (position = 0; position < num_matchers; ++position) { | |
| 194 if ((*dst_suggestions)[position] != nullptr) | |
| 195 continue; | |
| 196 if (match_urls[position] == (*src_suggestions)[i]->url) | |
| 197 break; | |
| 198 // match_hosts is only populated for suggestions which can be replaced by | |
| 199 // host matching like popular suggestions. | |
| 200 if (match_hosts[position] == (*src_suggestions)[i]->host) | |
| 201 break; | |
| 202 } | |
| 203 if (position == num_matchers) { | |
| 204 unmatched_suggestions.push_back(i); | |
| 205 } else { | |
| 206 // A move is required as the source and destination containers own the | |
| 207 // elements. | |
| 208 std::swap((*dst_suggestions)[position], (*src_suggestions)[i]); | |
| 209 } | |
| 210 } | |
| 211 return unmatched_suggestions; | |
| 212 } | |
| 213 | |
| 214 // Inserts suggestions from |src_suggestions| at positions |insert_positions| | |
| 215 // into dst_suggestions where ever empty starting from |start_position|. Returns | |
|
Bernhard Bauer
2015/09/08 15:44:25
Nit: |dst_suggestions| in pipes.
knn
2015/09/09 09:35:38
Done. Also moved to header.
| |
| 216 // the last filled position so that future insertions can start from there. | |
| 217 size_t InsertAllSuggestions( | |
| 218 size_t start_position, | |
| 219 const std::vector<size_t>& insert_positions, | |
| 220 ScopedVector<MostVisitedSites::Suggestion>* src_suggestions, | |
| 221 ScopedVector<MostVisitedSites::Suggestion>* dst_suggestions) { | |
| 222 size_t num_inserts = insert_positions.size(); | |
| 223 size_t num_dests = dst_suggestions->size(); | |
| 224 | |
| 225 size_t src_pos = 0; | |
| 226 size_t i = start_position; | |
|
Bernhard Bauer
2015/09/08 15:44:25
Can you move this into the loop header?
knn
2015/09/09 09:35:37
This is used as a return value outside the loop.
| |
| 227 for (; i < num_dests && src_pos < num_inserts; ++i) { | |
| 228 if ((*dst_suggestions)[i] != nullptr) | |
| 229 continue; | |
| 230 size_t src = insert_positions[src_pos++]; | |
| 231 std::swap((*dst_suggestions)[i], (*src_suggestions)[src]); | |
| 232 } | |
| 233 // Return destination postions filled so far which becomes the start_position | |
| 234 // for future runs. | |
| 235 return i; | |
| 236 } | |
| 237 | |
| 132 } // namespace | 238 } // namespace |
| 133 | 239 |
| 240 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | |
| 241 const std::string& url, | |
| 242 MostVisitedSource source) | |
| 243 : title(title), | |
| 244 url(url), | |
| 245 host(GURL(url).host()), | |
| 246 source(source), | |
| 247 provider_index(-1) {} | |
| 248 | |
| 249 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | |
| 250 const std::string& url, | |
| 251 const std::string& host, | |
| 252 MostVisitedSource source) | |
| 253 : title(title), url(url), host(host), source(source), provider_index(-1) {} | |
| 254 | |
| 255 MostVisitedSites::Suggestion::Suggestion(const base::string16& title, | |
| 256 const std::string& url, | |
| 257 MostVisitedSource source, | |
| 258 int provider_index) | |
| 259 : title(title), | |
| 260 url(url), | |
| 261 host(GURL(url).host()), | |
| 262 source(source), | |
| 263 provider_index(provider_index) {} | |
|
Bernhard Bauer
2015/09/08 15:44:25
DCHECK that source == SUGGESTIONS_SERVICE?
knn
2015/09/09 09:35:37
Done.
| |
| 264 | |
| 265 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { | |
| 266 switch (source) { | |
| 267 case MostVisitedSites::TOP_SITES: | |
| 268 return kHistogramClientName; | |
| 269 case MostVisitedSites::POPULAR: | |
| 270 return kHistogramPopularName; | |
| 271 case MostVisitedSites::SUGGESTIONS_SERVICE: | |
| 272 return provider_index >= 0 | |
| 273 ? base::StringPrintf(kHistogramServerFormat, provider_index) | |
| 274 : kHistogramServerName; | |
| 275 } | |
| 276 NOTREACHED(); | |
| 277 return std::string(); | |
| 278 } | |
| 279 | |
| 134 MostVisitedSites::MostVisitedSites(Profile* profile) | 280 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 135 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), | 281 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), |
| 136 received_popular_sites_(false), recorded_uma_(false), | 282 received_popular_sites_(false), recorded_uma_(false), |
| 137 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), | 283 num_local_thumbs_(0), num_server_thumbs_(0), num_empty_thumbs_(0), |
| 138 scoped_observer_(this), weak_ptr_factory_(this) { | 284 scoped_observer_(this), weak_ptr_factory_(this) { |
| 139 // Register the debugging page for the Suggestions Service and the thumbnails | 285 // Register the debugging page for the Suggestions Service and the thumbnails |
| 140 // debugging page. | 286 // debugging page. |
| 141 content::URLDataSource::Add(profile_, | 287 content::URLDataSource::Add(profile_, |
| 142 new suggestions::SuggestionsSource(profile_)); | 288 new suggestions::SuggestionsSource(profile_)); |
| 143 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 289 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, | 430 url, base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, |
| 285 weak_ptr_factory_.GetWeakPtr()), | 431 weak_ptr_factory_.GetWeakPtr()), |
| 286 base::Closure()); | 432 base::Closure()); |
| 287 } | 433 } |
| 288 } | 434 } |
| 289 | 435 |
| 290 void MostVisitedSites::RecordOpenedMostVisitedItem(JNIEnv* env, | 436 void MostVisitedSites::RecordOpenedMostVisitedItem(JNIEnv* env, |
| 291 jobject obj, | 437 jobject obj, |
| 292 jint index) { | 438 jint index) { |
| 293 DCHECK_GE(index, 0); | 439 DCHECK_GE(index, 0); |
| 294 DCHECK_LT(index, static_cast<int>(tile_sources_.size())); | 440 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); |
| 295 std::string histogram = base::StringPrintf(kOpenedItemHistogramFormat, | 441 std::string histogram = base::StringPrintf( |
| 296 tile_sources_[index].c_str()); | 442 kOpenedItemHistogramFormat, |
| 443 current_suggestions_[index]->GetSourceHistogramName().c_str()); | |
| 297 LogHistogramEvent(histogram, index, num_sites_); | 444 LogHistogramEvent(histogram, index, num_sites_); |
| 298 } | 445 } |
| 299 | 446 |
| 300 void MostVisitedSites::OnStateChanged() { | 447 void MostVisitedSites::OnStateChanged() { |
| 301 // There have been changes to the sync state. This class cares about a few | 448 // 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 | 449 // (just initialized, enabled/disabled or history sync state changed). Re-run |
| 303 // the query code which will use the proper state. | 450 // the query code which will use the proper state. |
| 304 QueryMostVisitedURLs(); | 451 QueryMostVisitedURLs(); |
| 305 } | 452 } |
| 306 | 453 |
| 307 // static | 454 // static |
| 308 bool MostVisitedSites::Register(JNIEnv* env) { | 455 bool MostVisitedSites::Register(JNIEnv* env) { |
| 309 return RegisterNativesImpl(env); | 456 return RegisterNativesImpl(env); |
| 310 } | 457 } |
| 311 | 458 |
| 459 // static | |
| 460 void MostVisitedSites::RegisterProfilePrefs( | |
| 461 user_prefs::PrefRegistrySyncable* registry) { | |
| 462 registry->RegisterListPref(prefs::kNTPSuggestionsURL); | |
| 463 registry->RegisterListPref(prefs::kNTPSuggestionsIsPersonal); | |
| 464 } | |
| 465 | |
| 312 void MostVisitedSites::QueryMostVisitedURLs() { | 466 void MostVisitedSites::QueryMostVisitedURLs() { |
| 313 SuggestionsService* suggestions_service = | 467 SuggestionsService* suggestions_service = |
| 314 SuggestionsServiceFactory::GetForProfile(profile_); | 468 SuggestionsServiceFactory::GetForProfile(profile_); |
| 315 if (suggestions_service) { | 469 if (suggestions_service) { |
| 316 // Suggestions service is enabled, initiate a query. | 470 // Suggestions service is enabled, initiate a query. |
| 317 suggestions_service->FetchSuggestionsData( | 471 suggestions_service->FetchSuggestionsData( |
| 318 GetSyncState(profile_), | 472 GetSyncState(profile_), |
| 319 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, | 473 base::Bind(&MostVisitedSites::OnSuggestionsProfileAvailable, |
| 320 weak_ptr_factory_.GetWeakPtr())); | 474 weak_ptr_factory_.GetWeakPtr())); |
| 321 } else { | 475 } else { |
| 322 InitiateTopSitesQuery(); | 476 InitiateTopSitesQuery(); |
| 323 } | 477 } |
| 324 } | 478 } |
| 325 | 479 |
| 326 void MostVisitedSites::InitiateTopSitesQuery() { | 480 void MostVisitedSites::InitiateTopSitesQuery() { |
| 327 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); | 481 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
| 328 if (!top_sites) | 482 if (!top_sites) |
| 329 return; | 483 return; |
| 330 | 484 |
| 331 top_sites->GetMostVisitedURLs( | 485 top_sites->GetMostVisitedURLs( |
| 332 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, | 486 base::Bind(&MostVisitedSites::OnMostVisitedURLsAvailable, |
| 333 weak_ptr_factory_.GetWeakPtr()), | 487 weak_ptr_factory_.GetWeakPtr()), |
| 334 false); | 488 false); |
| 335 } | 489 } |
| 336 | 490 |
| 337 void MostVisitedSites::OnMostVisitedURLsAvailable( | 491 void MostVisitedSites::OnMostVisitedURLsAvailable( |
| 338 const history::MostVisitedURLList& visited_list) { | 492 const history::MostVisitedURLList& visited_list) { |
| 339 std::vector<base::string16> titles; | 493 ScopedVector<Suggestion> suggestions; |
| 340 std::vector<std::string> urls; | 494 size_t num_tiles = |
| 341 tile_sources_.clear(); | 495 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_); | 496 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]; | 497 const history::MostVisitedURL& visited = visited_list[i]; |
| 345 if (visited.url.is_empty()) { | 498 if (visited.url.is_empty()) { |
| 346 num_tiles = i; | 499 num_tiles = i; |
| 347 break; // This is the signal that there are no more real visited sites. | 500 break; // This is the signal that there are no more real visited sites. |
| 348 } | 501 } |
| 349 titles.push_back(visited.title); | 502 suggestions.push_back( |
| 350 urls.push_back(visited.url.spec()); | 503 new Suggestion(visited.title, visited.url.spec(), TOP_SITES)); |
| 351 tile_sources_.push_back(kHistogramClientName); | |
| 352 } | 504 } |
| 353 | 505 |
| 354 received_most_visited_sites_ = true; | 506 received_most_visited_sites_ = true; |
| 355 mv_source_ = TOP_SITES; | 507 mv_source_ = TOP_SITES; |
| 356 AddPopularSites(&titles, &urls); | 508 AddPopularSites(&suggestions); |
| 357 NotifyMostVisitedURLsObserver(titles, urls); | 509 NotifyMostVisitedURLsObserver(); |
| 358 } | 510 } |
| 359 | 511 |
| 360 void MostVisitedSites::OnSuggestionsProfileAvailable( | 512 void MostVisitedSites::OnSuggestionsProfileAvailable( |
| 361 const SuggestionsProfile& suggestions_profile) { | 513 const SuggestionsProfile& suggestions_profile) { |
| 362 int num_tiles = suggestions_profile.suggestions_size(); | 514 int num_tiles = suggestions_profile.suggestions_size(); |
| 363 // With no server suggestions, fall back to local Most Visited. | 515 // With no server suggestions, fall back to local Most Visited. |
| 364 if (num_tiles == 0) { | 516 if (num_tiles == 0) { |
| 365 InitiateTopSitesQuery(); | 517 InitiateTopSitesQuery(); |
| 366 return; | 518 return; |
| 367 } | 519 } |
| 368 if (num_sites_ < num_tiles) | 520 if (num_sites_ < num_tiles) |
| 369 num_tiles = num_sites_; | 521 num_tiles = num_sites_; |
| 370 | 522 |
| 371 std::vector<base::string16> titles; | 523 ScopedVector<Suggestion> suggestions; |
| 372 std::vector<std::string> urls; | |
| 373 tile_sources_.clear(); | |
| 374 for (int i = 0; i < num_tiles; ++i) { | 524 for (int i = 0; i < num_tiles; ++i) { |
| 375 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 525 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 376 titles.push_back(base::UTF8ToUTF16(suggestion.title())); | 526 suggestions.push_back(new Suggestion( |
| 377 urls.push_back(suggestion.url()); | 527 base::UTF8ToUTF16(suggestion.title()), suggestion.url(), |
| 378 std::string tile_source; | 528 SUGGESTIONS_SERVICE, |
| 379 if (suggestion.providers_size() > 0) { | 529 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 } | 530 } |
| 387 | 531 |
| 388 received_most_visited_sites_ = true; | 532 received_most_visited_sites_ = true; |
| 389 mv_source_ = SUGGESTIONS_SERVICE; | 533 mv_source_ = SUGGESTIONS_SERVICE; |
| 390 AddPopularSites(&titles, &urls); | 534 AddPopularSites(&suggestions); |
| 391 NotifyMostVisitedURLsObserver(titles, urls); | 535 NotifyMostVisitedURLsObserver(); |
| 392 } | 536 } |
| 393 | 537 |
| 394 void MostVisitedSites::AddPopularSites(std::vector<base::string16>* titles, | 538 void MostVisitedSites::AddPopularSites( |
| 395 std::vector<std::string>* urls) { | 539 ScopedVector<Suggestion>* personal_suggestions) { |
| 396 if (!popular_sites_) | 540 size_t num_personal_suggestions = personal_suggestions->size(); |
| 397 return; | 541 DCHECK_LE(num_personal_suggestions, static_cast<size_t>(num_sites_)); |
| 398 | 542 |
| 399 DCHECK_EQ(titles->size(), urls->size()); | 543 // Collect non-blacklisted popular suggestions, skipping those already present |
| 400 DCHECK_EQ(titles->size(), tile_sources_.size()); | 544 // in the personal suggestions. |
| 401 DCHECK_LE(static_cast<int>(titles->size()), num_sites_); | 545 size_t num_popular_suggestions = num_sites_ - num_personal_suggestions; |
| 546 ScopedVector<Suggestion> popular_suggestions; | |
| 547 popular_suggestions.reserve(num_popular_suggestions); | |
| 402 | 548 |
| 403 // Collect all non-blacklisted popular suggestions. | 549 if (num_popular_suggestions > 0 && popular_sites_) { |
| 404 std::vector<base::string16> popular_titles; | 550 std::set<std::string> personal_hosts; |
| 405 std::vector<std::string> popular_urls; | 551 for (const auto& suggestion : *personal_suggestions) |
|
Bernhard Bauer
2015/09/08 15:44:25
Use the actual type :)
knn
2015/09/09 09:35:38
Done.
| |
| 406 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 552 personal_hosts.insert(suggestion->host); |
| 407 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { | 553 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 408 // Skip blacklisted sites. | 554 for (const PopularSites::Site& popular_site : popular_sites_->sites()) { |
| 409 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) | 555 // Skip blacklisted sites. |
| 410 continue; | 556 if (top_sites && top_sites->IsBlacklisted(popular_site.url)) |
| 557 continue; | |
| 558 std::string host = popular_site.url.host(); | |
| 559 // Skip suggestions already present in personal. | |
| 560 if (personal_hosts.find(host) != personal_hosts.end()) | |
| 561 continue; | |
| 411 | 562 |
| 412 popular_titles.push_back(popular_site.title); | 563 popular_suggestions.push_back(new Suggestion( |
| 413 popular_urls.push_back(popular_site.url.spec()); | 564 popular_site.title, popular_site.url.spec(), host, POPULAR)); |
| 414 if (static_cast<int>(popular_titles.size()) >= num_sites_) | 565 if (popular_suggestions.size() >= num_popular_suggestions) |
| 415 break; | 566 break; |
| 567 } | |
| 416 } | 568 } |
| 417 | 569 num_popular_suggestions = popular_suggestions.size(); |
| 418 AddPopularSitesImpl( | 570 size_t num_actual_tiles = num_personal_suggestions + num_popular_suggestions; |
| 419 num_sites_, popular_titles, popular_urls, titles, urls, &tile_sources_); | 571 std::vector<std::string> old_sites_url; |
| 572 std::vector<bool> old_sites_is_personal; | |
| 573 GetPreviousNTPSites(profile_->GetPrefs(), num_actual_tiles, &old_sites_url, | |
| 574 &old_sites_is_personal); | |
| 575 ScopedVector<Suggestion> merged_suggestions = | |
| 576 MergeSuggestions(personal_suggestions, &popular_suggestions, | |
| 577 old_sites_url, old_sites_is_personal); | |
| 578 DCHECK_EQ(num_actual_tiles, merged_suggestions.size()); | |
| 579 if (received_popular_sites_) | |
| 580 SaveCurrentNTPSites(merged_suggestions, profile_->GetPrefs()); | |
| 581 current_suggestions_.swap(merged_suggestions); | |
| 420 } | 582 } |
| 421 | 583 |
| 422 // static | 584 // static |
| 423 void MostVisitedSites::AddPopularSitesImpl( | 585 ScopedVector<MostVisitedSites::Suggestion> MostVisitedSites::MergeSuggestions( |
| 424 int num_sites, | 586 ScopedVector<Suggestion>* personal_suggestions, |
| 425 const std::vector<base::string16>& popular_titles, | 587 ScopedVector<Suggestion>* popular_suggestions, |
| 426 const std::vector<std::string>& popular_urls, | 588 const std::vector<std::string>& old_sites_url, |
| 427 std::vector<base::string16>* titles, | 589 const std::vector<bool>& old_sites_is_personal) { |
| 428 std::vector<std::string>* urls, | 590 size_t num_personal_suggestions = personal_suggestions->size(); |
| 429 std::vector<std::string>* tile_sources) { | 591 size_t num_popular_suggestions = popular_suggestions->size(); |
| 430 // Start off with the popular suggestions. | 592 size_t num_tiles = num_popular_suggestions + num_personal_suggestions; |
| 431 std::vector<base::string16> new_titles(popular_titles); | 593 ScopedVector<Suggestion> merged_suggestions; |
| 432 std::vector<std::string> new_urls(popular_urls); | 594 merged_suggestions.resize(num_tiles); |
| 433 std::vector<std::string> new_tile_sources(new_titles.size(), | |
| 434 kHistogramPopularName); | |
| 435 | 595 |
| 436 // Now, go over the personalized suggestions and replace matching popular | 596 size_t num_old_tiles = old_sites_url.size(); |
| 437 // suggestions. This is so that when some of the popular suggestions become | 597 DCHECK_LE(num_old_tiles, num_tiles); |
| 438 // personal, they retain their absolute positions. | 598 DCHECK_EQ(num_old_tiles, old_sites_is_personal.size()); |
| 439 std::vector<base::string16> titles_to_insert; | 599 std::vector<std::string> old_sites_host; |
| 440 std::vector<std::string> urls_to_insert; | 600 old_sites_host.reserve(num_old_tiles); |
| 441 std::vector<std::string> tile_sources_to_insert; | 601 // Only populate the hosts for popular suggestions as only they can be |
| 442 for (size_t site_index = 0; site_index < titles->size(); site_index++) { | 602 // replaced by host. Personal suggestions require an exact url match to be |
| 443 const base::string16& title = (*titles)[site_index]; | 603 // replaced. |
| 444 const std::string& url = (*urls)[site_index]; | 604 for (size_t i = 0; i < num_old_tiles; ++i) { |
| 445 const std::string& tile_source = (*tile_sources)[site_index]; | 605 old_sites_host.push_back(old_sites_is_personal[i] |
| 446 // See if we already have a matching popular site. | 606 ? std::string() |
| 447 bool found = false; | 607 : GURL(old_sites_url[i]).host()); |
| 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 } | 608 } |
| 466 | 609 |
| 467 // Append personalized suggestions at the end if there's room. | 610 // Insert personal suggestions if they existed previously. |
| 468 size_t num_to_append = | 611 std::vector<size_t> new_personal_suggestions = InsertMatchingSuggestions( |
| 469 std::min(static_cast<size_t>(num_sites) - new_titles.size(), | 612 personal_suggestions, &merged_suggestions, old_sites_url, old_sites_host); |
| 470 titles_to_insert.size()); | 613 // Insert popular suggestions if they existed previously. |
| 471 new_titles.insert(new_titles.end(), | 614 std::vector<size_t> new_popular_suggestions = InsertMatchingSuggestions( |
| 472 titles_to_insert.end() - num_to_append, | 615 popular_suggestions, &merged_suggestions, old_sites_url, old_sites_host); |
| 473 titles_to_insert.end()); | 616 // Insert leftover personal suggestions. |
| 474 new_urls.insert(new_urls.end(), | 617 size_t filled_so_far = InsertAllSuggestions( |
| 475 urls_to_insert.end() - num_to_append, | 618 0, new_personal_suggestions, personal_suggestions, &merged_suggestions); |
| 476 urls_to_insert.end()); | 619 // Insert leftover popular suggestions. |
| 477 new_tile_sources.insert(new_tile_sources.end(), | 620 InsertAllSuggestions(filled_so_far, new_popular_suggestions, |
| 478 tile_sources_to_insert.end() - num_to_append, | 621 popular_suggestions, &merged_suggestions); |
| 479 tile_sources_to_insert.end()); | 622 return merged_suggestions.Pass(); |
| 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 } | 623 } |
| 503 | 624 |
| 504 void MostVisitedSites::NotifyMostVisitedURLsObserver( | 625 void MostVisitedSites::NotifyMostVisitedURLsObserver() { |
| 505 const std::vector<base::string16>& titles, | 626 size_t num_suggestions = current_suggestions_.size(); |
| 506 const std::vector<std::string>& urls) { | |
| 507 DCHECK_EQ(titles.size(), urls.size()); | |
| 508 if (received_most_visited_sites_ && received_popular_sites_ && | 627 if (received_most_visited_sites_ && received_popular_sites_ && |
| 509 !recorded_uma_) { | 628 !recorded_uma_) { |
| 510 RecordImpressionUMAMetrics(); | 629 RecordImpressionUMAMetrics(); |
| 511 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, titles.size()); | 630 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumTilesHistogramName, num_suggestions); |
| 512 recorded_uma_ = true; | 631 recorded_uma_ = true; |
| 513 } | 632 } |
| 633 std::vector<base::string16> titles; | |
| 634 std::vector<std::string> urls; | |
| 635 titles.reserve(num_suggestions); | |
| 636 urls.reserve(num_suggestions); | |
| 637 for (const auto suggestion : current_suggestions_) { | |
|
Bernhard Bauer
2015/09/08 15:44:25
And here as well :)
knn
2015/09/09 09:35:38
Done.
| |
| 638 titles.push_back(suggestion->title); | |
| 639 urls.push_back(suggestion->url); | |
| 640 } | |
| 514 JNIEnv* env = AttachCurrentThread(); | 641 JNIEnv* env = AttachCurrentThread(); |
| 642 DCHECK_EQ(titles.size(), urls.size()); | |
| 515 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | 643 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
| 516 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | 644 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), |
| 517 ToJavaArrayOfStrings(env, urls).obj()); | 645 ToJavaArrayOfStrings(env, urls).obj()); |
| 518 } | 646 } |
| 519 | 647 |
| 520 void MostVisitedSites::OnPopularSitesAvailable(bool success) { | 648 void MostVisitedSites::OnPopularSitesAvailable(bool success) { |
| 521 received_popular_sites_ = true; | 649 received_popular_sites_ = true; |
| 522 | 650 |
| 523 if (!success) { | 651 if (!success) { |
| 524 LOG(WARNING) << "Download of popular sites failed"; | 652 LOG(WARNING) << "Download of popular sites failed"; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 546 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName, | 674 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumLocalThumbnailTilesHistogramName, |
| 547 num_local_thumbs_); | 675 num_local_thumbs_); |
| 548 num_local_thumbs_ = 0; | 676 num_local_thumbs_ = 0; |
| 549 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_); | 677 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumEmptyTilesHistogramName, num_empty_thumbs_); |
| 550 num_empty_thumbs_ = 0; | 678 num_empty_thumbs_ = 0; |
| 551 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); | 679 UMA_HISTOGRAM_SPARSE_SLOWLY(kNumServerTilesHistogramName, num_server_thumbs_); |
| 552 num_server_thumbs_ = 0; | 680 num_server_thumbs_ = 0; |
| 553 } | 681 } |
| 554 | 682 |
| 555 void MostVisitedSites::RecordImpressionUMAMetrics() { | 683 void MostVisitedSites::RecordImpressionUMAMetrics() { |
| 556 for (size_t i = 0; i < tile_sources_.size(); i++) { | 684 for (size_t i = 0; i < current_suggestions_.size(); i++) { |
| 557 std::string histogram = base::StringPrintf(kImpressionHistogramFormat, | 685 std::string histogram = base::StringPrintf( |
| 558 tile_sources_[i].c_str()); | 686 kImpressionHistogramFormat, |
| 687 current_suggestions_[i]->GetSourceHistogramName().c_str()); | |
| 559 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); | 688 LogHistogramEvent(histogram, static_cast<int>(i), num_sites_); |
| 560 } | 689 } |
| 561 } | 690 } |
| 562 | 691 |
| 563 void MostVisitedSites::TopSitesLoaded(history::TopSites* top_sites) { | 692 void MostVisitedSites::TopSitesLoaded(history::TopSites* top_sites) { |
| 564 } | 693 } |
| 565 | 694 |
| 566 void MostVisitedSites::TopSitesChanged(history::TopSites* top_sites, | 695 void MostVisitedSites::TopSitesChanged(history::TopSites* top_sites, |
| 567 ChangeReason change_reason) { | 696 ChangeReason change_reason) { |
| 568 if (mv_source_ == TOP_SITES) { | 697 if (mv_source_ == TOP_SITES) { |
| 569 // The displayed suggestions are invalidated. | 698 // The displayed suggestions are invalidated. |
| 570 QueryMostVisitedURLs(); | 699 QueryMostVisitedURLs(); |
| 571 } | 700 } |
| 572 } | 701 } |
| 573 | 702 |
| 574 static jlong Init(JNIEnv* env, | 703 static jlong Init(JNIEnv* env, |
| 575 const JavaParamRef<jobject>& obj, | 704 const JavaParamRef<jobject>& obj, |
| 576 const JavaParamRef<jobject>& jprofile) { | 705 const JavaParamRef<jobject>& jprofile) { |
| 577 MostVisitedSites* most_visited_sites = | 706 MostVisitedSites* most_visited_sites = |
| 578 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 707 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 579 return reinterpret_cast<intptr_t>(most_visited_sites); | 708 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 580 } | 709 } |
| OLD | NEW |