OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/password_manager/core/browser/statistics_table.h" | 5 #include "components/password_manager/core/browser/statistics_table.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
| 12 #include "base/memory/ptr_util.h" |
12 #include "sql/connection.h" | 13 #include "sql/connection.h" |
13 #include "sql/statement.h" | 14 #include "sql/statement.h" |
14 | 15 |
15 namespace password_manager { | 16 namespace password_manager { |
16 namespace { | 17 namespace { |
17 | 18 |
18 // Convenience enum for interacting with SQL queries that use all the columns. | 19 // Convenience enum for interacting with SQL queries that use all the columns. |
19 enum LoginTableColumns { | 20 enum LoginTableColumns { |
20 COLUMN_ORIGIN_DOMAIN = 0, | 21 COLUMN_ORIGIN_DOMAIN = 0, |
21 COLUMN_USERNAME, | 22 COLUMN_USERNAME, |
22 COLUMN_DISMISSALS, | 23 COLUMN_DISMISSALS, |
23 COLUMN_DATE, | 24 COLUMN_DATE, |
24 }; | 25 }; |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 InteractionsStats::InteractionsStats() = default; | 29 InteractionsStats::InteractionsStats() = default; |
29 | 30 |
30 bool operator==(const InteractionsStats& lhs, const InteractionsStats& rhs) { | 31 bool operator==(const InteractionsStats& lhs, const InteractionsStats& rhs) { |
31 return lhs.origin_domain == rhs.origin_domain && | 32 return lhs.origin_domain == rhs.origin_domain && |
32 lhs.username_value == rhs.username_value && | 33 lhs.username_value == rhs.username_value && |
33 lhs.dismissal_count == rhs.dismissal_count && | 34 lhs.dismissal_count == rhs.dismissal_count && |
34 lhs.update_time == rhs.update_time; | 35 lhs.update_time == rhs.update_time; |
35 } | 36 } |
36 | 37 |
37 InteractionsStats* FindStatsByUsername( | 38 InteractionsStats* FindStatsByUsername( |
38 const std::vector<scoped_ptr<InteractionsStats>>& stats, | 39 const std::vector<std::unique_ptr<InteractionsStats>>& stats, |
39 const base::string16& username) { | 40 const base::string16& username) { |
40 auto it = | 41 auto it = std::find_if( |
41 std::find_if(stats.begin(), stats.end(), | 42 stats.begin(), stats.end(), |
42 [&username](const scoped_ptr<InteractionsStats>& element) { | 43 [&username](const std::unique_ptr<InteractionsStats>& element) { |
43 return username == element->username_value; | 44 return username == element->username_value; |
44 }); | 45 }); |
45 return it == stats.end() ? nullptr : it->get(); | 46 return it == stats.end() ? nullptr : it->get(); |
46 } | 47 } |
47 | 48 |
48 StatisticsTable::StatisticsTable() : db_(nullptr) { | 49 StatisticsTable::StatisticsTable() : db_(nullptr) { |
49 } | 50 } |
50 | 51 |
51 StatisticsTable::~StatisticsTable() = default; | 52 StatisticsTable::~StatisticsTable() = default; |
52 | 53 |
53 void StatisticsTable::Init(sql::Connection* db) { | 54 void StatisticsTable::Init(sql::Connection* db) { |
54 db_ = db; | 55 db_ = db; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 bool StatisticsTable::RemoveRow(const GURL& domain) { | 99 bool StatisticsTable::RemoveRow(const GURL& domain) { |
99 if (!domain.is_valid()) | 100 if (!domain.is_valid()) |
100 return false; | 101 return false; |
101 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, | 102 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, |
102 "DELETE FROM stats WHERE " | 103 "DELETE FROM stats WHERE " |
103 "origin_domain = ? ")); | 104 "origin_domain = ? ")); |
104 s.BindString(0, domain.spec()); | 105 s.BindString(0, domain.spec()); |
105 return s.Run(); | 106 return s.Run(); |
106 } | 107 } |
107 | 108 |
108 std::vector<scoped_ptr<InteractionsStats>> StatisticsTable::GetRows( | 109 std::vector<std::unique_ptr<InteractionsStats>> StatisticsTable::GetRows( |
109 const GURL& domain) { | 110 const GURL& domain) { |
110 if (!domain.is_valid()) | 111 if (!domain.is_valid()) |
111 return std::vector<scoped_ptr<InteractionsStats>>(); | 112 return std::vector<std::unique_ptr<InteractionsStats>>(); |
112 const char query[] = | 113 const char query[] = |
113 "SELECT origin_domain, username_value, " | 114 "SELECT origin_domain, username_value, " |
114 "dismissal_count, update_time FROM stats WHERE origin_domain == ?"; | 115 "dismissal_count, update_time FROM stats WHERE origin_domain == ?"; |
115 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query)); | 116 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query)); |
116 s.BindString(0, domain.spec()); | 117 s.BindString(0, domain.spec()); |
117 std::vector<scoped_ptr<InteractionsStats>> result; | 118 std::vector<std::unique_ptr<InteractionsStats>> result; |
118 while (s.Step()) { | 119 while (s.Step()) { |
119 result.push_back(make_scoped_ptr(new InteractionsStats)); | 120 result.push_back(base::WrapUnique(new InteractionsStats)); |
120 result.back()->origin_domain = GURL(s.ColumnString(COLUMN_ORIGIN_DOMAIN)); | 121 result.back()->origin_domain = GURL(s.ColumnString(COLUMN_ORIGIN_DOMAIN)); |
121 result.back()->username_value = s.ColumnString16(COLUMN_USERNAME); | 122 result.back()->username_value = s.ColumnString16(COLUMN_USERNAME); |
122 result.back()->dismissal_count = s.ColumnInt(COLUMN_DISMISSALS); | 123 result.back()->dismissal_count = s.ColumnInt(COLUMN_DISMISSALS); |
123 result.back()->update_time = | 124 result.back()->update_time = |
124 base::Time::FromInternalValue(s.ColumnInt64(COLUMN_DATE)); | 125 base::Time::FromInternalValue(s.ColumnInt64(COLUMN_DATE)); |
125 } | 126 } |
126 return result; | 127 return result; |
127 } | 128 } |
128 | 129 |
129 bool StatisticsTable::RemoveStatsBetween(base::Time delete_begin, | 130 bool StatisticsTable::RemoveStatsBetween(base::Time delete_begin, |
130 base::Time delete_end) { | 131 base::Time delete_end) { |
131 sql::Statement s(db_->GetCachedStatement( | 132 sql::Statement s(db_->GetCachedStatement( |
132 SQL_FROM_HERE, | 133 SQL_FROM_HERE, |
133 "DELETE FROM stats WHERE update_time >= ? AND update_time < ?")); | 134 "DELETE FROM stats WHERE update_time >= ? AND update_time < ?")); |
134 s.BindInt64(0, delete_begin.ToInternalValue()); | 135 s.BindInt64(0, delete_begin.ToInternalValue()); |
135 s.BindInt64(1, delete_end.is_null() ? std::numeric_limits<int64_t>::max() | 136 s.BindInt64(1, delete_end.is_null() ? std::numeric_limits<int64_t>::max() |
136 : delete_end.ToInternalValue()); | 137 : delete_end.ToInternalValue()); |
137 return s.Run(); | 138 return s.Run(); |
138 } | 139 } |
139 | 140 |
140 } // namespace password_manager | 141 } // namespace password_manager |
OLD | NEW |