OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/history/url_index_private_data.h" | 5 #include "chrome/browser/history/url_index_private_data.h" |
6 | 6 |
7 #include <functional> | 7 #include <functional> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <limits> | 9 #include <limits> |
10 #include <numeric> | 10 #include <numeric> |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 return scored_items; | 276 return scored_items; |
277 } | 277 } |
278 | 278 |
279 bool URLIndexPrivateData::UpdateURL( | 279 bool URLIndexPrivateData::UpdateURL( |
280 HistoryService* history_service, | 280 HistoryService* history_service, |
281 const URLRow& row, | 281 const URLRow& row, |
282 const std::string& languages, | 282 const std::string& languages, |
283 const std::set<std::string>& scheme_whitelist) { | 283 const std::set<std::string>& scheme_whitelist, |
| 284 base::CancelableTaskTracker* tracker) { |
284 // The row may or may not already be in our index. If it is not already | 285 // The row may or may not already be in our index. If it is not already |
285 // indexed and it qualifies then it gets indexed. If it is already | 286 // indexed and it qualifies then it gets indexed. If it is already |
286 // indexed and still qualifies then it gets updated, otherwise it | 287 // indexed and still qualifies then it gets updated, otherwise it |
287 // is deleted from the index. | 288 // is deleted from the index. |
288 bool row_was_updated = false; | 289 bool row_was_updated = false; |
289 URLID row_id = row.id(); | 290 URLID row_id = row.id(); |
290 HistoryInfoMap::iterator row_pos = history_info_map_.find(row_id); | 291 HistoryInfoMap::iterator row_pos = history_info_map_.find(row_id); |
291 if (row_pos == history_info_map_.end()) { | 292 if (row_pos == history_info_map_.end()) { |
292 // This new row should be indexed if it qualifies. | 293 // This new row should be indexed if it qualifies. |
293 URLRow new_row(row); | 294 URLRow new_row(row); |
294 new_row.set_id(row_id); | 295 new_row.set_id(row_id); |
295 row_was_updated = RowQualifiesAsSignificant(new_row, base::Time()) && | 296 row_was_updated = RowQualifiesAsSignificant(new_row, base::Time()) && |
296 IndexRow(NULL, history_service, new_row, languages, scheme_whitelist); | 297 IndexRow(NULL, |
| 298 history_service, |
| 299 new_row, |
| 300 languages, |
| 301 scheme_whitelist, |
| 302 tracker); |
297 } else if (RowQualifiesAsSignificant(row, base::Time())) { | 303 } else if (RowQualifiesAsSignificant(row, base::Time())) { |
298 // This indexed row still qualifies and will be re-indexed. | 304 // This indexed row still qualifies and will be re-indexed. |
299 // The url won't have changed but the title, visit count, etc. | 305 // The url won't have changed but the title, visit count, etc. |
300 // might have changed. | 306 // might have changed. |
301 URLRow& row_to_update = row_pos->second.url_row; | 307 URLRow& row_to_update = row_pos->second.url_row; |
302 bool title_updated = row_to_update.title() != row.title(); | 308 bool title_updated = row_to_update.title() != row.title(); |
303 if (row_to_update.visit_count() != row.visit_count() || | 309 if (row_to_update.visit_count() != row.visit_count() || |
304 row_to_update.typed_count() != row.typed_count() || | 310 row_to_update.typed_count() != row.typed_count() || |
305 row_to_update.last_visit() != row.last_visit() || title_updated) { | 311 row_to_update.last_visit() != row.last_visit() || title_updated) { |
306 row_to_update.set_visit_count(row.visit_count()); | 312 row_to_update.set_visit_count(row.visit_count()); |
307 row_to_update.set_typed_count(row.typed_count()); | 313 row_to_update.set_typed_count(row.typed_count()); |
308 row_to_update.set_last_visit(row.last_visit()); | 314 row_to_update.set_last_visit(row.last_visit()); |
309 // If something appears to have changed, update the recent visits | 315 // If something appears to have changed, update the recent visits |
310 // information. | 316 // information. |
311 ScheduleUpdateRecentVisits(history_service, row_id); | 317 ScheduleUpdateRecentVisits(history_service, row_id, tracker); |
312 // While the URL is guaranteed to remain stable, the title may have | 318 // While the URL is guaranteed to remain stable, the title may have |
313 // changed. If so, then update the index with the changed words. | 319 // changed. If so, then update the index with the changed words. |
314 if (title_updated) { | 320 if (title_updated) { |
315 // Clear all words associated with this row and re-index both the | 321 // Clear all words associated with this row and re-index both the |
316 // URL and title. | 322 // URL and title. |
317 RemoveRowWordsFromIndex(row_to_update); | 323 RemoveRowWordsFromIndex(row_to_update); |
318 row_to_update.set_title(row.title()); | 324 row_to_update.set_title(row.title()); |
319 RowWordStarts word_starts; | 325 RowWordStarts word_starts; |
320 AddRowWordsToIndex(row_to_update, &word_starts, languages); | 326 AddRowWordsToIndex(row_to_update, &word_starts, languages); |
321 word_starts_map_[row_id] = word_starts; | 327 word_starts_map_[row_id] = word_starts; |
(...skipping 28 matching lines...) Expand all Loading... |
350 } | 356 } |
351 } | 357 } |
352 // Else: Oddly, the URL doesn't seem to exist in the private index. | 358 // Else: Oddly, the URL doesn't seem to exist in the private index. |
353 // Ignore this update. This can happen if, for instance, the user | 359 // Ignore this update. This can happen if, for instance, the user |
354 // removes the URL from URLIndexPrivateData before the historyDB call | 360 // removes the URL from URLIndexPrivateData before the historyDB call |
355 // returns. | 361 // returns. |
356 } | 362 } |
357 | 363 |
358 void URLIndexPrivateData::ScheduleUpdateRecentVisits( | 364 void URLIndexPrivateData::ScheduleUpdateRecentVisits( |
359 HistoryService* history_service, | 365 HistoryService* history_service, |
360 URLID url_id) { | 366 URLID url_id, |
| 367 base::CancelableTaskTracker* tracker) { |
361 history_service->ScheduleDBTask( | 368 history_service->ScheduleDBTask( |
362 new UpdateRecentVisitsFromHistoryDBTask(this, url_id), | 369 new UpdateRecentVisitsFromHistoryDBTask(this, url_id), tracker); |
363 &recent_visits_consumer_); | |
364 } | 370 } |
365 | 371 |
366 // Helper functor for DeleteURL. | 372 // Helper functor for DeleteURL. |
367 class HistoryInfoMapItemHasURL { | 373 class HistoryInfoMapItemHasURL { |
368 public: | 374 public: |
369 explicit HistoryInfoMapItemHasURL(const GURL& url): url_(url) {} | 375 explicit HistoryInfoMapItemHasURL(const GURL& url): url_(url) {} |
370 | 376 |
371 bool operator()(const std::pair<const HistoryID, HistoryInfoMapValue>& item) { | 377 bool operator()(const std::pair<const HistoryID, HistoryInfoMapValue>& item) { |
372 return item.second.url_row.url() == url_; | 378 return item.second.url_row.url() == url_; |
373 } | 379 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 | 443 |
438 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 444 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
439 | 445 |
440 scoped_refptr<URLIndexPrivateData> | 446 scoped_refptr<URLIndexPrivateData> |
441 rebuilt_data(new URLIndexPrivateData); | 447 rebuilt_data(new URLIndexPrivateData); |
442 URLDatabase::URLEnumerator history_enum; | 448 URLDatabase::URLEnumerator history_enum; |
443 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) | 449 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) |
444 return NULL; | 450 return NULL; |
445 rebuilt_data->last_time_rebuilt_from_history_ = base::Time::Now(); | 451 rebuilt_data->last_time_rebuilt_from_history_ = base::Time::Now(); |
446 for (URLRow row; history_enum.GetNextURL(&row); ) { | 452 for (URLRow row; history_enum.GetNextURL(&row); ) { |
447 rebuilt_data->IndexRow(history_db, NULL, row, languages, | 453 rebuilt_data->IndexRow( |
448 scheme_whitelist); | 454 history_db, NULL, row, languages, scheme_whitelist, NULL); |
449 } | 455 } |
450 | 456 |
451 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", | 457 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", |
452 base::TimeTicks::Now() - beginning_time); | 458 base::TimeTicks::Now() - beginning_time); |
453 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", | 459 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", |
454 rebuilt_data->history_id_word_map_.size()); | 460 rebuilt_data->history_id_word_map_.size()); |
455 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", | 461 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", |
456 rebuilt_data->word_map_.size()); | 462 rebuilt_data->word_map_.size()); |
457 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", | 463 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", |
458 rebuilt_data->char_word_map_.size()); | 464 rebuilt_data->char_word_map_.size()); |
459 return rebuilt_data; | 465 return rebuilt_data; |
460 } | 466 } |
461 | 467 |
462 // static | 468 // static |
463 bool URLIndexPrivateData::WritePrivateDataToCacheFileTask( | 469 bool URLIndexPrivateData::WritePrivateDataToCacheFileTask( |
464 scoped_refptr<URLIndexPrivateData> private_data, | 470 scoped_refptr<URLIndexPrivateData> private_data, |
465 const base::FilePath& file_path) { | 471 const base::FilePath& file_path) { |
466 DCHECK(private_data.get()); | 472 DCHECK(private_data.get()); |
467 DCHECK(!file_path.empty()); | 473 DCHECK(!file_path.empty()); |
468 return private_data->SaveToFile(file_path); | 474 return private_data->SaveToFile(file_path); |
469 } | 475 } |
470 | 476 |
471 void URLIndexPrivateData::CancelPendingUpdates() { | |
472 recent_visits_consumer_.CancelAllRequests(); | |
473 } | |
474 | |
475 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const { | 477 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const { |
476 scoped_refptr<URLIndexPrivateData> data_copy = new URLIndexPrivateData; | 478 scoped_refptr<URLIndexPrivateData> data_copy = new URLIndexPrivateData; |
477 data_copy->last_time_rebuilt_from_history_ = last_time_rebuilt_from_history_; | 479 data_copy->last_time_rebuilt_from_history_ = last_time_rebuilt_from_history_; |
478 data_copy->word_list_ = word_list_; | 480 data_copy->word_list_ = word_list_; |
479 data_copy->available_words_ = available_words_; | 481 data_copy->available_words_ = available_words_; |
480 data_copy->word_map_ = word_map_; | 482 data_copy->word_map_ = word_map_; |
481 data_copy->char_word_map_ = char_word_map_; | 483 data_copy->char_word_map_ = char_word_map_; |
482 data_copy->word_id_history_map_ = word_id_history_map_; | 484 data_copy->word_id_history_map_ = word_id_history_map_; |
483 data_copy->history_id_word_map_ = history_id_word_map_; | 485 data_copy->history_id_word_map_ = history_id_word_map_; |
484 data_copy->history_info_map_ = history_info_map_; | 486 data_copy->history_info_map_ = history_info_map_; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 } | 680 } |
679 } | 681 } |
680 return word_id_set; | 682 return word_id_set; |
681 } | 683 } |
682 | 684 |
683 bool URLIndexPrivateData::IndexRow( | 685 bool URLIndexPrivateData::IndexRow( |
684 HistoryDatabase* history_db, | 686 HistoryDatabase* history_db, |
685 HistoryService* history_service, | 687 HistoryService* history_service, |
686 const URLRow& row, | 688 const URLRow& row, |
687 const std::string& languages, | 689 const std::string& languages, |
688 const std::set<std::string>& scheme_whitelist) { | 690 const std::set<std::string>& scheme_whitelist, |
| 691 base::CancelableTaskTracker* tracker) { |
689 const GURL& gurl(row.url()); | 692 const GURL& gurl(row.url()); |
690 | 693 |
691 // Index only URLs with a whitelisted scheme. | 694 // Index only URLs with a whitelisted scheme. |
692 if (!URLSchemeIsWhitelisted(gurl, scheme_whitelist)) | 695 if (!URLSchemeIsWhitelisted(gurl, scheme_whitelist)) |
693 return false; | 696 return false; |
694 | 697 |
695 URLID row_id = row.id(); | 698 URLID row_id = row.id(); |
696 // Strip out username and password before saving and indexing. | 699 // Strip out username and password before saving and indexing. |
697 base::string16 url(net::FormatUrl(gurl, languages, | 700 base::string16 url(net::FormatUrl(gurl, languages, |
698 net::kFormatUrlOmitUsernamePassword, | 701 net::kFormatUrlOmitUsernamePassword, |
(...skipping 24 matching lines...) Expand all Loading... |
723 // So we don't do any thread checks. | 726 // So we don't do any thread checks. |
724 VisitVector recent_visits; | 727 VisitVector recent_visits; |
725 // Make sure the private data is going to get as many recent visits as | 728 // Make sure the private data is going to get as many recent visits as |
726 // ScoredHistoryMatch::GetFrequency() hopes to use. | 729 // ScoredHistoryMatch::GetFrequency() hopes to use. |
727 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); | 730 DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore); |
728 if (history_db->GetMostRecentVisitsForURL(row_id, | 731 if (history_db->GetMostRecentVisitsForURL(row_id, |
729 kMaxVisitsToStoreInCache, | 732 kMaxVisitsToStoreInCache, |
730 &recent_visits)) | 733 &recent_visits)) |
731 UpdateRecentVisits(row_id, recent_visits); | 734 UpdateRecentVisits(row_id, recent_visits); |
732 } else { | 735 } else { |
| 736 DCHECK(tracker); |
733 DCHECK(history_service); | 737 DCHECK(history_service); |
734 ScheduleUpdateRecentVisits(history_service, row_id); | 738 ScheduleUpdateRecentVisits(history_service, row_id, tracker); |
735 } | 739 } |
736 | 740 |
737 return true; | 741 return true; |
738 } | 742 } |
739 | 743 |
740 void URLIndexPrivateData::AddRowWordsToIndex(const URLRow& row, | 744 void URLIndexPrivateData::AddRowWordsToIndex(const URLRow& row, |
741 RowWordStarts* word_starts, | 745 RowWordStarts* word_starts, |
742 const std::string& languages) { | 746 const std::string& languages) { |
743 HistoryID history_id = static_cast<HistoryID>(row.id()); | 747 HistoryID history_id = static_cast<HistoryID>(row.id()); |
744 // Split URL into individual, unique words then add in the title words. | 748 // Split URL into individual, unique words then add in the title words. |
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 // recently visited (within the last 12/24 hours) as highly important. Get | 1341 // recently visited (within the last 12/24 hours) as highly important. Get |
1338 // input from mpearson. | 1342 // input from mpearson. |
1339 if (r1.typed_count() != r2.typed_count()) | 1343 if (r1.typed_count() != r2.typed_count()) |
1340 return (r1.typed_count() > r2.typed_count()); | 1344 return (r1.typed_count() > r2.typed_count()); |
1341 if (r1.visit_count() != r2.visit_count()) | 1345 if (r1.visit_count() != r2.visit_count()) |
1342 return (r1.visit_count() > r2.visit_count()); | 1346 return (r1.visit_count() > r2.visit_count()); |
1343 return (r1.last_visit() > r2.last_visit()); | 1347 return (r1.last_visit() > r2.last_visit()); |
1344 } | 1348 } |
1345 | 1349 |
1346 } // namespace history | 1350 } // namespace history |
OLD | NEW |