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/visit_database.h" | 5 #include "chrome/browser/history/visit_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
16 #include "base/timer.h" | 16 #include "base/timer.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "chrome/browser/history/time_filter.h" |
18 #include "chrome/browser/history/url_database.h" | 19 #include "chrome/browser/history/url_database.h" |
19 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
20 #include "content/public/common/page_transition_types.h" | 21 #include "content/public/common/page_transition_types.h" |
21 #include "sql/statement.h" | 22 #include "sql/statement.h" |
22 | 23 |
23 // Rows, in order, of the visit table. | 24 // Rows, in order, of the visit table. |
24 #define HISTORY_VISIT_ROW_FIELDS \ | 25 #define HISTORY_VISIT_ROW_FIELDS \ |
25 " id,url,visit_time,from_visit,transition,segment_id,is_indexed " | 26 " id,url,visit_time,from_visit,transition,segment_id,is_indexed " |
26 | 27 |
27 namespace history { | 28 namespace history { |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 // See GetVisibleVisitsInRange for more info on how these times are bound. | 525 // See GetVisibleVisitsInRange for more info on how these times are bound. |
525 int64 end = end_time.ToInternalValue(); | 526 int64 end = end_time.ToInternalValue(); |
526 statement.BindInt64(0, begin_time.ToInternalValue()); | 527 statement.BindInt64(0, begin_time.ToInternalValue()); |
527 statement.BindInt64(1, end ? end : std::numeric_limits<int64>::max()); | 528 statement.BindInt64(1, end ? end : std::numeric_limits<int64>::max()); |
528 statement.BindInt64(2, | 529 statement.BindInt64(2, |
529 max_results ? max_results : std::numeric_limits<int64>::max()); | 530 max_results ? max_results : std::numeric_limits<int64>::max()); |
530 | 531 |
531 return FillVisitVector(statement, visits); | 532 return FillVisitVector(statement, visits); |
532 } | 533 } |
533 | 534 |
| 535 bool VisitDatabase::GetAllVisitsDuringTimes(const TimeFilter& time_filter, |
| 536 int max_results, |
| 537 VisitVector* visits) { |
| 538 visits->clear(); |
| 539 if (max_results) |
| 540 visits->reserve(max_results); |
| 541 |
| 542 for (TimeFilter::TimeVector::const_iterator it = |
| 543 time_filter.get_times().begin(); |
| 544 it != time_filter.get_times().end(); ++it) { |
| 545 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 546 "SELECT" HISTORY_VISIT_ROW_FIELDS "FROM visits " |
| 547 "WHERE visit_time >= ? AND visit_time < ?" |
| 548 "ORDER BY visit_time LIMIT ?")); |
| 549 |
| 550 // See GetVisibleVisitsInRange for more info on how these times are bound. |
| 551 statement.BindInt64(0, it->first.ToInternalValue()); |
| 552 statement.BindInt64(1, it->second.is_null() ? |
| 553 it->second.ToInternalValue() : std::numeric_limits<int64>::max()); |
| 554 statement.BindInt64(2, |
| 555 max_results ? max_results : std::numeric_limits<int64>::max()); |
| 556 |
| 557 VisitVector v; |
| 558 if (!FillVisitVector(statement, &v)) |
| 559 return false; |
| 560 size_t take_only = 0; |
| 561 if (max_results && |
| 562 static_cast<int>(visits->size() + v.size()) > max_results) { |
| 563 take_only = max_results - visits->size(); |
| 564 } |
| 565 |
| 566 visits->insert(visits->end(), |
| 567 v.begin(), take_only ? v.begin() + take_only : v.end()); |
| 568 if (max_results && static_cast<int>(visits->size()) == max_results) |
| 569 return true; |
| 570 } |
| 571 return true; |
| 572 } |
| 573 |
| 574 |
534 bool VisitDatabase::GetVisitsInRangeForTransition( | 575 bool VisitDatabase::GetVisitsInRangeForTransition( |
535 base::Time begin_time, | 576 base::Time begin_time, |
536 base::Time end_time, | 577 base::Time end_time, |
537 int max_results, | 578 int max_results, |
538 content::PageTransition transition, | 579 content::PageTransition transition, |
539 VisitVector* visits) { | 580 VisitVector* visits) { |
540 DCHECK(visits); | 581 DCHECK(visits); |
541 visits->clear(); | 582 visits->clear(); |
542 | 583 |
543 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 584 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 if (found_urls.find(visit.url_id) != found_urls.end()) | 634 if (found_urls.find(visit.url_id) != found_urls.end()) |
594 continue; | 635 continue; |
595 found_urls.insert(visit.url_id); | 636 found_urls.insert(visit.url_id); |
596 visits->push_back(visit); | 637 visits->push_back(visit); |
597 | 638 |
598 if (max_count > 0 && static_cast<int>(visits->size()) >= max_count) | 639 if (max_count > 0 && static_cast<int>(visits->size()) >= max_count) |
599 break; | 640 break; |
600 } | 641 } |
601 } | 642 } |
602 | 643 |
| 644 void VisitDatabase::GetVisibleVisitsDuringTimes(const TimeFilter& time_filter, |
| 645 int max_results, |
| 646 VisitVector* visits) { |
| 647 visits->clear(); |
| 648 if (max_results) |
| 649 visits->reserve(max_results); |
| 650 for (TimeFilter::TimeVector::const_iterator it = |
| 651 time_filter.get_times().begin(); |
| 652 it != time_filter.get_times().end(); ++it) { |
| 653 VisitVector v; |
| 654 GetVisibleVisitsInRange(it->first, it->second, max_results, &v); |
| 655 size_t take_only = 0; |
| 656 if (max_results && |
| 657 static_cast<int>(visits->size() + v.size()) > max_results) { |
| 658 take_only = max_results - visits->size(); |
| 659 } |
| 660 |
| 661 visits->insert(visits->end(), |
| 662 v.begin(), take_only ? v.begin() + take_only : v.end()); |
| 663 if (max_results && static_cast<int>(visits->size()) == max_results) |
| 664 return; |
| 665 } |
| 666 } |
| 667 |
| 668 |
603 VisitID VisitDatabase::GetMostRecentVisitForURL(URLID url_id, | 669 VisitID VisitDatabase::GetMostRecentVisitForURL(URLID url_id, |
604 VisitRow* visit_row) { | 670 VisitRow* visit_row) { |
605 // The visit_time values can be duplicated in a redirect chain, so we sort | 671 // The visit_time values can be duplicated in a redirect chain, so we sort |
606 // by id too, to ensure a consistent ordering just in case. | 672 // by id too, to ensure a consistent ordering just in case. |
607 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | 673 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
608 "SELECT" HISTORY_VISIT_ROW_FIELDS "FROM visits " | 674 "SELECT" HISTORY_VISIT_ROW_FIELDS "FROM visits " |
609 "WHERE url=? " | 675 "WHERE url=? " |
610 "ORDER BY visit_time DESC, id DESC " | 676 "ORDER BY visit_time DESC, id DESC " |
611 "LIMIT 1")); | 677 "LIMIT 1")); |
612 statement.BindInt64(0, url_id); | 678 statement.BindInt64(0, url_id); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 // Get the source entries out of the query result. | 839 // Get the source entries out of the query result. |
774 while (statement.Step()) { | 840 while (statement.Step()) { |
775 std::pair<VisitID, VisitSource> source_entry(statement.ColumnInt64(0), | 841 std::pair<VisitID, VisitSource> source_entry(statement.ColumnInt64(0), |
776 static_cast<VisitSource>(statement.ColumnInt(1))); | 842 static_cast<VisitSource>(statement.ColumnInt(1))); |
777 sources->insert(source_entry); | 843 sources->insert(source_entry); |
778 } | 844 } |
779 } | 845 } |
780 } | 846 } |
781 | 847 |
782 } // namespace history | 848 } // namespace history |
OLD | NEW |