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/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "chrome/browser/history/url_database.h" | 14 #include "chrome/browser/history/url_database.h" |
15 #include "chrome/browser/history/visit_filter.h" | 15 #include "chrome/browser/history/visit_filter.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "content/public/common/page_transition_types.h" | 17 #include "content/public/common/page_transition_types.h" |
18 #include "sql/statement.h" | 18 #include "sql/statement.h" |
19 | 19 |
20 // Rows, in order, of the visit table. | 20 // Rows, in order, of the visit table. |
21 #define HISTORY_VISIT_ROW_FIELDS \ | 21 #define HISTORY_VISIT_ROW_FIELDS \ |
22 " id,url,visit_time,from_visit,transition,segment_id,is_indexed," \ | 22 " id,url,visit_time,from_visit,transition,segment_id,is_indexed," \ |
23 "visit_duration " | 23 "visit_duration " |
24 | 24 |
25 namespace history { | 25 namespace history { |
26 | 26 |
27 VisitDatabase::VisitDatabase() { | 27 VisitDatabase::VisitDatabase() : |
| 28 observers_(new ObserverListThreadSafe<VisitDatabaseObserver>()) { |
28 } | 29 } |
29 | 30 |
30 VisitDatabase::~VisitDatabase() { | 31 VisitDatabase::~VisitDatabase() { |
31 } | 32 } |
32 | 33 |
33 bool VisitDatabase::InitVisitTable() { | 34 bool VisitDatabase::InitVisitTable() { |
34 if (!GetDB().DoesTableExist("visits")) { | 35 if (!GetDB().DoesTableExist("visits")) { |
35 if (!GetDB().Execute("CREATE TABLE visits(" | 36 if (!GetDB().Execute("CREATE TABLE visits(" |
36 "id INTEGER PRIMARY KEY," | 37 "id INTEGER PRIMARY KEY," |
37 "url INTEGER NOT NULL," // key of the URL this corresponds to | 38 "url INTEGER NOT NULL," // key of the URL this corresponds to |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 statement1.BindInt64(0, visit->visit_id); | 152 statement1.BindInt64(0, visit->visit_id); |
152 statement1.BindInt64(1, source); | 153 statement1.BindInt64(1, source); |
153 | 154 |
154 if (!statement1.Run()) { | 155 if (!statement1.Run()) { |
155 VLOG(0) << "Failed to execute visit_source insert statement: " | 156 VLOG(0) << "Failed to execute visit_source insert statement: " |
156 << "id = " << visit->visit_id; | 157 << "id = " << visit->visit_id; |
157 return 0; | 158 return 0; |
158 } | 159 } |
159 } | 160 } |
160 | 161 |
| 162 BriefVisitInfo info; |
| 163 info.url_id = visit->url_id; |
| 164 info.time = visit->visit_time; |
| 165 info.transition = visit->transition; |
| 166 observers_->Notify(&VisitDatabaseObserver::OnAddVisit, info); |
| 167 |
161 return visit->visit_id; | 168 return visit->visit_id; |
162 } | 169 } |
163 | 170 |
164 void VisitDatabase::DeleteVisit(const VisitRow& visit) { | 171 void VisitDatabase::DeleteVisit(const VisitRow& visit) { |
165 // Patch around this visit. Any visits that this went to will now have their | 172 // Patch around this visit. Any visits that this went to will now have their |
166 // "source" be the deleted visit's source. | 173 // "source" be the deleted visit's source. |
167 sql::Statement update_chain(GetDB().GetCachedStatement(SQL_FROM_HERE, | 174 sql::Statement update_chain(GetDB().GetCachedStatement(SQL_FROM_HERE, |
168 "UPDATE visits SET from_visit=? WHERE from_visit=?")); | 175 "UPDATE visits SET from_visit=? WHERE from_visit=?")); |
169 update_chain.BindInt64(0, visit.referring_visit); | 176 update_chain.BindInt64(0, visit.referring_visit); |
170 update_chain.BindInt64(1, visit.visit_id); | 177 update_chain.BindInt64(1, visit.visit_id); |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 if (!GetDB().DoesColumnExist("visits", "visit_duration")) { | 558 if (!GetDB().DoesColumnExist("visits", "visit_duration")) { |
552 // Old versions don't have the visit_duration column, we modify the table | 559 // Old versions don't have the visit_duration column, we modify the table |
553 // to add that field. | 560 // to add that field. |
554 if (!GetDB().Execute("ALTER TABLE visits " | 561 if (!GetDB().Execute("ALTER TABLE visits " |
555 "ADD COLUMN visit_duration INTEGER DEFAULT 0 NOT NULL")) | 562 "ADD COLUMN visit_duration INTEGER DEFAULT 0 NOT NULL")) |
556 return false; | 563 return false; |
557 } | 564 } |
558 return true; | 565 return true; |
559 } | 566 } |
560 | 567 |
| 568 void VisitDatabase::AddVisitDatabaseObserver(VisitDatabaseObserver* observer) { |
| 569 observers_->AddObserver(observer); |
| 570 } |
| 571 |
| 572 void VisitDatabase::RemoveVisitDatabaseObserver( |
| 573 VisitDatabaseObserver* observer) { |
| 574 observers_->RemoveObserver(observer); |
| 575 } |
| 576 |
| 577 void VisitDatabase::GetBriefVisitInfoOfMostRecentVisits( |
| 578 int max_visits, |
| 579 std::vector<BriefVisitInfo>* result_vector) { |
| 580 result_vector->clear(); |
| 581 |
| 582 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 583 "SELECT url,visit_time,transition FROM visits " |
| 584 "ORDER BY id DESC LIMIT ?")); |
| 585 |
| 586 statement.BindInt64(0, max_visits); |
| 587 |
| 588 if (!statement.is_valid()) |
| 589 return; |
| 590 |
| 591 while (statement.Step()) { |
| 592 BriefVisitInfo info; |
| 593 info.url_id = statement.ColumnInt64(0); |
| 594 info.time = base::Time::FromInternalValue(statement.ColumnInt64(1)); |
| 595 info.transition = content::PageTransitionFromInt(statement.ColumnInt(2)); |
| 596 result_vector->push_back(info); |
| 597 } |
| 598 } |
| 599 |
561 } // namespace history | 600 } // namespace history |
OLD | NEW |