|
OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/predictors/logged_in_predictor_table.h" | |
6 | |
7 #include <algorithm> | |
8 #include <utility> | |
9 #include "base/logging.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/stringprintf.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
14 #include "sql/statement.h" | |
15 | |
16 using content::BrowserThread; | |
17 using sql::Statement; | |
18 using std::string; | |
19 | |
20 namespace { | |
21 | |
22 const char kTableName[] = "logged_in_predictor"; | |
23 | |
24 } // namespace | |
25 | |
26 namespace predictors { | |
27 | |
28 LoggedInPredictorTable::LoggedInPredictorTable() | |
29 : PredictorTableBase() { | |
30 } | |
31 | |
32 LoggedInPredictorTable::~LoggedInPredictorTable() { | |
33 } | |
34 | |
35 string LoggedInPredictorTable::GetKey(const GURL& url) { | |
36 string effective_domain( | |
37 net::RegistryControlledDomainService::GetDomainAndRegistry(url.host())); | |
38 if (effective_domain.empty()) | |
39 effective_domain = url.host(); | |
40 | |
41 if (!effective_domain.empty() && effective_domain[0] == '.') | |
Shishir
2013/04/16 21:22:04
Comment on this code.
tburkard
2013/04/16 21:47:19
Done.
| |
42 return effective_domain.substr(1); | |
43 return effective_domain; | |
44 } | |
45 | |
46 void LoggedInPredictorTable::Add(const GURL& url) { | |
47 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
48 if (CantAccessDatabase()) | |
49 return; | |
50 | |
51 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
52 base::StringPrintf("INSERT OR IGNORE INTO %s (domain, time) VALUES (?,?)", | |
53 kTableName).c_str())); | |
54 | |
55 statement.BindString(0, GetKey(url)); | |
56 statement.BindInt64(1, base::Time::Now().ToInternalValue()); | |
57 | |
58 statement.Run(); | |
59 } | |
60 | |
61 void LoggedInPredictorTable::Exists(const GURL& url, bool* is_present, | |
62 bool* lookup_succeeded) { | |
63 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
64 *lookup_succeeded = false; | |
65 if (CantAccessDatabase()) | |
66 return; | |
67 | |
68 Statement statement(DB()->GetUniqueStatement( | |
Shishir
2013/04/16 21:22:04
GetCachedStatement.
tburkard
2013/04/16 21:47:19
Done.
| |
69 base::StringPrintf("SELECT count(*) FROM %s WHERE domain=?", | |
70 kTableName).c_str())); | |
71 | |
72 statement.BindString(0, GetKey(url)); | |
73 | |
74 if (statement.Step()) { | |
75 *is_present = (statement.ColumnInt(0) > 0); | |
76 *lookup_succeeded = true; | |
77 } | |
78 } | |
79 | |
80 void LoggedInPredictorTable::DeleteAllCreatedBetween( | |
81 const base::Time& delete_begin, const base::Time& delete_end) { | |
82 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
83 if (CantAccessDatabase()) | |
84 return; | |
85 | |
86 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, | |
87 base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?", | |
88 kTableName).c_str())); | |
89 | |
90 statement.BindInt64(0, delete_begin.ToInternalValue()); | |
91 statement.BindInt64(1, delete_end.ToInternalValue()); | |
92 | |
93 statement.Run(); | |
94 } | |
95 | |
96 void LoggedInPredictorTable::CreateTableIfNonExistent() { | |
97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
98 if (CantAccessDatabase()) | |
99 return; | |
100 | |
101 const char* table_creator = | |
102 "CREATE TABLE %s ( " | |
Shishir
2013/04/16 21:22:04
Will probably fit in one line?
tburkard
2013/04/16 21:47:19
Done.
| |
103 "domain TEXT, " | |
104 "time INTEGER, " | |
105 "PRIMARY KEY(domain))"; | |
106 | |
107 sql::Connection* db = DB(); | |
108 bool success = | |
Shishir
2013/04/16 21:22:04
Since you have only one table, put
if (db->DoesTa
tburkard
2013/04/16 21:47:19
Done.
| |
109 (db->DoesTableExist(kTableName) || | |
110 db->Execute(base::StringPrintf(table_creator, kTableName).c_str())); | |
111 | |
112 if (!success) | |
113 ResetDB(); | |
114 } | |
115 | |
116 void LoggedInPredictorTable::LogDatabaseStats() { | |
117 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
118 if (CantAccessDatabase()) | |
119 return; | |
120 | |
121 Statement statement(DB()->GetUniqueStatement( | |
122 base::StringPrintf("SELECT count(*) FROM %s", | |
Shishir
2013/04/16 21:22:04
one line?
tburkard
2013/04/16 21:47:19
Done.
| |
123 kTableName).c_str())); | |
124 if (statement.Step()) | |
125 UMA_HISTOGRAM_COUNTS("LoggedInPredictor.TableRowCount", | |
126 statement.ColumnInt(0)); | |
127 } | |
128 | |
129 } // namespace predictors | |
OLD | NEW |