OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/history/android/visit_sql_handler.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "components/history/core/browser/url_database.h" | |
9 #include "components/history/core/browser/visit_database.h" | |
10 | |
11 using base::Time; | |
12 | |
13 namespace history { | |
14 | |
15 namespace { | |
16 | |
17 // The interesting columns of this handler. | |
18 const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = { | |
19 HistoryAndBookmarkRow::CREATED, HistoryAndBookmarkRow::VISIT_COUNT, | |
20 HistoryAndBookmarkRow::LAST_VISIT_TIME }; | |
21 | |
22 } // namespace | |
23 | |
24 VisitSQLHandler::VisitSQLHandler(URLDatabase* url_db, VisitDatabase* visit_db) | |
25 : SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)), | |
26 url_db_(url_db), | |
27 visit_db_(visit_db) { | |
28 } | |
29 | |
30 VisitSQLHandler::~VisitSQLHandler() { | |
31 } | |
32 | |
33 // The created time is updated according the given |row|. | |
34 // We simulate updating created time by | |
35 // a. Remove all visits. | |
36 // b. Insert a new visit which has visit time same as created time. | |
37 // c. Insert the number of visits according the visit count in urls table. | |
38 // | |
39 // Visit row is insertted/removed to keep consistent with urls table. | |
40 // a. If the visit count in urls table is less than the visit rows in visit | |
41 // table, all existent visits will be removed. The new visits will be | |
42 // insertted according the value in urls table. | |
43 // b. Otherwise, only add the increased number of visit count. | |
44 bool VisitSQLHandler::Update(const HistoryAndBookmarkRow& row, | |
45 const TableIDRows& ids_set) { | |
46 for (TableIDRows::const_iterator id = ids_set.begin(); | |
47 id != ids_set.end(); ++id) { | |
48 VisitVector visits; | |
49 if (!visit_db_->GetVisitsForURL(id->url_id, &visits)) | |
50 return false; | |
51 int visit_count_in_table = visits.size(); | |
52 URLRow url_row; | |
53 if (!url_db_->GetURLRow(id->url_id, &url_row)) | |
54 return false; | |
55 int visit_count_needed = url_row.visit_count(); | |
56 | |
57 if (visit_count_needed == 0) | |
58 return Delete(ids_set); | |
59 | |
60 // If created time is updated or new visit count is less than the current | |
61 // one, delete all visit rows. | |
62 if (row.is_value_set_explicitly(HistoryAndBookmarkRow::CREATED) || | |
63 visit_count_in_table > visit_count_needed) { | |
64 if (!DeleteVisitsForURL(id->url_id)) | |
65 return false; | |
66 visit_count_in_table = 0; | |
67 } | |
68 | |
69 if (row.is_value_set_explicitly(HistoryAndBookmarkRow::CREATED) && | |
70 visit_count_needed > 0) { | |
71 if (!AddVisit(id->url_id, row.created())) | |
72 return false; | |
73 visit_count_in_table++; | |
74 } | |
75 | |
76 if (!AddVisitRows(id->url_id, visit_count_needed - visit_count_in_table, | |
77 url_row.last_visit())) | |
78 return false; | |
79 } | |
80 return true; | |
81 } | |
82 | |
83 bool VisitSQLHandler::Insert(HistoryAndBookmarkRow* row) { | |
84 DCHECK(row->is_value_set_explicitly(HistoryAndBookmarkRow::URL_ID)); | |
85 | |
86 URLRow url_row; | |
87 if (!url_db_->GetURLRow(row->url_id(), &url_row)) | |
88 return false; | |
89 | |
90 int visit_count = url_row.visit_count(); | |
91 | |
92 if (visit_count == 0) | |
93 return true; | |
94 | |
95 // Add a row if the last visit time is different from created time. | |
96 if (row->is_value_set_explicitly(HistoryAndBookmarkRow::CREATED) && | |
97 row->created() != url_row.last_visit() && visit_count > 0) { | |
98 if (!AddVisit(row->url_id(), row->created())) | |
99 return false; | |
100 visit_count--; | |
101 } | |
102 | |
103 if (!AddVisitRows(row->url_id(), visit_count, url_row.last_visit())) | |
104 return false; | |
105 | |
106 return true; | |
107 } | |
108 | |
109 bool VisitSQLHandler::Delete(const TableIDRows& ids_set) { | |
110 for (TableIDRows::const_iterator ids = ids_set.begin(); | |
111 ids != ids_set.end(); ++ids) { | |
112 DeleteVisitsForURL(ids->url_id); | |
113 } | |
114 return true; | |
115 } | |
116 | |
117 bool VisitSQLHandler::AddVisit(URLID url_id, const Time& visit_time) { | |
118 // TODO : Is 'ui::PAGE_TRANSITION_AUTO_BOOKMARK' proper? | |
119 // if not, a new ui::PageTransition type will need. | |
120 VisitRow visit_row(url_id, visit_time, 0, | |
121 ui::PAGE_TRANSITION_AUTO_BOOKMARK, 0); | |
122 return visit_db_->AddVisit(&visit_row, SOURCE_BROWSED); | |
123 } | |
124 | |
125 bool VisitSQLHandler::AddVisitRows(URLID url_id, | |
126 int visit_count, | |
127 const Time& last_visit_time) { | |
128 int64 last_update_value = last_visit_time.ToInternalValue(); | |
129 for (int i = 0; i < visit_count; i++) { | |
130 if (!AddVisit(url_id, Time::FromInternalValue(last_update_value - i))) | |
131 return false; | |
132 } | |
133 return true; | |
134 } | |
135 | |
136 bool VisitSQLHandler::DeleteVisitsForURL(URLID url_id) { | |
137 VisitVector visits; | |
138 if (!visit_db_->GetVisitsForURL(url_id, &visits)) | |
139 return false; | |
140 | |
141 for (VisitVector::const_iterator v = visits.begin(); v != visits.end(); ++v) { | |
142 visit_db_->DeleteVisit(*v); | |
143 } | |
144 return true; | |
145 } | |
146 | |
147 } // namespace history. | |
OLD | NEW |