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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// Copyright (c) 2013 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/predictors/logged_in_predictor_table.h"
#include <algorithm>
#include <utility>
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "sql/statement.h"
using content::BrowserThread;
using sql::Statement;
using std::string;
namespace {
const char kTableName[] = "logged_in_predictor";
} // namespace
namespace predictors {
LoggedInPredictorTable::LoggedInPredictorTable()
: PredictorTableBase() {
}
LoggedInPredictorTable::~LoggedInPredictorTable() {
}
// static
string LoggedInPredictorTable::GetKey(const GURL& url) {
return GetKeyFromDomain(url.host());
}
// static
string LoggedInPredictorTable::GetKeyFromDomain(const std::string& domain) {
string effective_domain(
net::registry_controlled_domains::GetDomainAndRegistry(
domain,
net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES));
if (effective_domain.empty())
effective_domain = domain;
// Strip off a preceding ".", if present.
if (!effective_domain.empty() && effective_domain[0] == '.')
return effective_domain.substr(1);
return effective_domain;
}
void LoggedInPredictorTable::AddDomainFromURL(const GURL& url) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (CantAccessDatabase())
return;
Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE,
base::StringPrintf("INSERT OR IGNORE INTO %s (domain, time) VALUES (?,?)",
kTableName).c_str()));
statement.BindString(0, GetKey(url));
statement.BindInt64(1, base::Time::Now().ToInternalValue());
statement.Run();
}
void LoggedInPredictorTable::DeleteDomainFromURL(const GURL& url) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (CantAccessDatabase())
return;
Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE,
base::StringPrintf("DELETE FROM %s WHERE domain=?", kTableName).c_str()));
statement.BindString(0, GetKey(url));
statement.Run();
}
void LoggedInPredictorTable::DeleteDomain(const std::string& domain) {
DeleteDomainFromURL(GURL("http://" + domain));
}
void LoggedInPredictorTable::HasUserLoggedIn(const GURL& url, bool* is_present,
bool* lookup_succeeded) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
*lookup_succeeded = false;
if (CantAccessDatabase())
return;
Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE,
base::StringPrintf("SELECT count(*) FROM %s WHERE domain=?",
kTableName).c_str()));
statement.BindString(0, GetKey(url));
if (statement.Step()) {
*is_present = (statement.ColumnInt(0) > 0);
*lookup_succeeded = true;
}
}
void LoggedInPredictorTable::DeleteAllCreatedBetween(
const base::Time& delete_begin, const base::Time& delete_end) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (CantAccessDatabase())
return;
Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE,
base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?",
kTableName).c_str()));
statement.BindInt64(0, delete_begin.ToInternalValue());
statement.BindInt64(1, delete_end.ToInternalValue());
statement.Run();
}
void LoggedInPredictorTable::GetAllData(
LoggedInPredictorTable::LoggedInStateMap* state_map) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
DCHECK(state_map != NULL);
state_map->clear();
if (CantAccessDatabase())
return;
Statement statement(DB()->GetUniqueStatement(
base::StringPrintf("SELECT * FROM %s", kTableName).c_str()));
while (statement.Step()) {
string domain = statement.ColumnString(0);
int64 value = statement.ColumnInt64(1);
(*state_map)[domain] = value;
}
}
void LoggedInPredictorTable::CreateTableIfNonExistent() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (CantAccessDatabase())
return;
sql::Connection* db = DB();
if (db->DoesTableExist(kTableName))
return;
const char* table_creator =
"CREATE TABLE %s (domain TEXT, time INTEGER, PRIMARY KEY(domain))";
if (!db->Execute(base::StringPrintf(table_creator, kTableName).c_str()))
ResetDB();
}
void LoggedInPredictorTable::LogDatabaseStats() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
if (CantAccessDatabase())
return;
Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE,
base::StringPrintf("SELECT count(*) FROM %s", kTableName).c_str()));
if (statement.Step())
UMA_HISTOGRAM_COUNTS("LoggedInPredictor.TableRowCount",
statement.ColumnInt(0));
}
} // namespace predictors
|