1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/history/android/urls_sql_handler.h"
#include "base/logging.h"
#include "chrome/browser/history/history_database.h"
using base::Time;
namespace history {
namespace {
const HistoryAndBookmarkRow::ColumnID kInterestingColumns[] = {
HistoryAndBookmarkRow::URL, HistoryAndBookmarkRow::VISIT_COUNT,
HistoryAndBookmarkRow::TITLE, HistoryAndBookmarkRow::LAST_VISIT_TIME };
} // namespace
UrlsSQLHandler::UrlsSQLHandler(HistoryDatabase* history_db)
: SQLHandler(kInterestingColumns, arraysize(kInterestingColumns)),
history_db_(history_db) {
}
UrlsSQLHandler:: ~UrlsSQLHandler() {
}
bool UrlsSQLHandler::Insert(HistoryAndBookmarkRow* row) {
URLRow url_row(row->url());
URLID id = history_db_->GetRowForURL(row->url(), &url_row);
if (id) {
LOG(ERROR) << "AndroidProviderBackend::Insert Urls; url exists.";
return false; // We already has this row.
}
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::TITLE))
url_row.set_title(row->title());
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME))
url_row.set_last_visit(row->last_visit_time());
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT))
url_row.set_visit_count(row->visit_count());
// Adjust the last_visit_time if it not set.
if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME)) {
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::CREATED))
url_row.set_last_visit(row->created());
else if (row->is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT))
url_row.set_last_visit(Time::Now());
}
// Adjust the visit_count if it not set.
if (!row->is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT)) {
int visit_count = 0;
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::CREATED) &&
row->is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME) &&
row->last_visit_time() == row->created()) {
visit_count = 1;
} else {
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::CREATED))
visit_count++;
if (row->is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME))
visit_count++;
}
url_row.set_visit_count(visit_count);
}
URLID new_id = history_db_->AddURL(url_row);
// The subsequent inserts need this information.
row->set_url_id(new_id);
return new_id;
}
// Only the title, the visit time and the vist count can be updated, since the
// visit count and the visit time are related. If they are not both specified,
// The not specified one will be adjusted according the speficied one. The rule
// is:
// a. If the visit time changed and the visit count is not specified, the visit
// count will be increased by one.
// b. If the visit count increased and the visit time is not specified, the last
// visit time is set to Now.
// c. If the visit count is 0, it means clear the history, the last visit time
// will be set to 0.
// d. The new visit time should great than or equal to the current one,
// otherwise update failed.
// e. The title is free to update.
//
bool UrlsSQLHandler::Update(const HistoryAndBookmarkRow& row,
const TableIDRows& ids_set) {
// Directly updating the URL is not allowed, we should insert the new URL
// and remove the older one.
DCHECK(!row.is_value_set_explicitly(HistoryAndBookmarkRow::URL));
for (TableIDRows::const_iterator ids = ids_set.begin();
ids != ids_set.end(); ++ids) {
URLRow url_row;
if (!history_db_->GetURLRow(ids->url_id, &url_row))
return false;
URLRow update_row = url_row;
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::TITLE))
update_row.set_title(row.title());
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT))
update_row.set_visit_count(row.visit_count());
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME)) {
// The new last_visit_time can't be less than current one.
if (row.last_visit_time() < url_row.last_visit())
return false;
update_row.set_last_visit(row.last_visit_time());
}
// Adjust the visit_count if it not set.
if (!row.is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT) &&
row.is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME) &&
(row.last_visit_time() != url_row.last_visit()))
// If last visit time is changed, visit count needs increase by 1,
// as a row will be added in visit database
update_row.set_visit_count(url_row.visit_count() + 1);
// Adjust the last_vsit_time if it not set.
if (!row.is_value_set_explicitly(HistoryAndBookmarkRow::LAST_VISIT_TIME)) {
if (row.is_value_set_explicitly(HistoryAndBookmarkRow::VISIT_COUNT) &&
row.visit_count() == 0) {
// User want to clear history
update_row.set_last_visit(Time());
} else if (row.visit_count() > url_row.visit_count()) {
update_row.set_last_visit(Time::Now());
}
}
if (!history_db_->UpdateURLRow(ids->url_id, update_row))
return false;
}
return true;
}
bool UrlsSQLHandler::Delete(const TableIDRows& ids_set) {
for (TableIDRows::const_iterator ids = ids_set.begin();
ids != ids_set.end(); ++ids) {
if (!history_db_->DeleteURLRow(ids->url_id))
return false;
}
return true;
}
} // namespace history
|