OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/previews/core/previews_opt_out_store_sql.h" | 5 #include "components/previews/core/previews_opt_out_store_sql.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 statement_delete_by_host.BindString(0, host_name); | 143 statement_delete_by_host.BindString(0, host_name); |
144 statement_delete_by_host.BindInt(1, kMaxRowsPerHost); | 144 statement_delete_by_host.BindInt(1, kMaxRowsPerHost); |
145 statement_delete_by_host.Run(); | 145 statement_delete_by_host.Run(); |
146 } | 146 } |
147 | 147 |
148 void LoadBlackListFromDataBase( | 148 void LoadBlackListFromDataBase( |
149 sql::Connection* db, | 149 sql::Connection* db, |
150 scoped_refptr<base::SingleThreadTaskRunner> runner, | 150 scoped_refptr<base::SingleThreadTaskRunner> runner, |
151 LoadBlackListCallback callback) { | 151 LoadBlackListCallback callback) { |
152 // Gets the table sorted by host and time. Limits the number of hosts using | 152 // Gets the table sorted by host and time. Limits the number of hosts using |
153 // most recent opt_out time as the limiting function. | 153 // most recent opt_out time as the limiting function. Sorting is free due to |
| 154 // the table structure, and it improves performance in the loop below. |
154 const char kSql[] = | 155 const char kSql[] = |
155 "SELECT host_name, time, opt_out" | 156 "SELECT host_name, time, opt_out" |
156 " FROM " PREVIEWS_TABLE_NAME; | 157 " FROM " PREVIEWS_TABLE_NAME " ORDER BY host_name, time DESC"; |
157 | 158 |
158 sql::Statement statement(db->GetUniqueStatement(kSql)); | 159 sql::Statement statement(db->GetUniqueStatement(kSql)); |
159 | 160 |
160 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); | 161 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); |
| 162 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item = |
| 163 PreviewsBlackList::CreateHostIndifferentBlackListItem(); |
161 int count = 0; | 164 int count = 0; |
162 // Add the host name, the visit time, and opt out history to | 165 // Add the host name, the visit time, and opt out history to |
163 // |black_list_item_map|. | 166 // |black_list_item_map|. |
164 while (statement.Step()) { | 167 while (statement.Step()) { |
165 ++count; | 168 ++count; |
166 std::string host_name = statement.ColumnString(0); | 169 std::string host_name = statement.ColumnString(0); |
167 PreviewsBlackListItem* black_list_item = | 170 PreviewsBlackListItem* black_list_item = |
168 PreviewsBlackList::GetOrCreateBlackListItem(black_list_item_map.get(), | 171 PreviewsBlackList::GetOrCreateBlackListItemForMap( |
169 host_name); | 172 black_list_item_map.get(), host_name); |
170 DCHECK_LE(black_list_item_map->size(), | 173 DCHECK_LE(black_list_item_map->size(), |
171 params::MaxInMemoryHostsInBlackList()); | 174 params::MaxInMemoryHostsInBlackList()); |
172 // Allows the internal logic of PreviewsBlackListItem to determine how to | 175 // Allows the internal logic of PreviewsBlackListItem to determine how to |
173 // evict entries when there are more than | 176 // evict entries when there are more than |
174 // |StoredHistoryLengthForBlackList()| for the host. | 177 // |StoredHistoryLengthForBlackList()| for the host. |
175 black_list_item->AddPreviewNavigation( | 178 black_list_item->AddPreviewNavigation( |
176 statement.ColumnBool(2), | 179 statement.ColumnBool(2), |
177 base::Time::FromInternalValue(statement.ColumnInt64(1))); | 180 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
| 181 host_indifferent_black_list_item->AddPreviewNavigation( |
| 182 statement.ColumnBool(2), |
| 183 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
178 } | 184 } |
179 | 185 |
180 // TODO(ryansturm): Add UMA to log |count|. crbug.com/656739 | 186 // TODO(ryansturm): Add UMA to log |count|. crbug.com/656739 |
181 if (count > kMaxRowsInDB) { | 187 if (count > kMaxRowsInDB) { |
182 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. | 188 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. |
183 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 189 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
184 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 190 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
185 " WHERE ROWID IN" | 191 " WHERE ROWID IN" |
186 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 192 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
187 " ORDER BY time DESC" | 193 " ORDER BY time DESC" |
188 " LIMIT -1 OFFSET ?)"; | 194 " LIMIT -1 OFFSET ?)"; |
189 | 195 |
190 sql::Statement statement_delete( | 196 sql::Statement statement_delete( |
191 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); | 197 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); |
192 statement_delete.BindInt(0, kMaxRowsInDB); | 198 statement_delete.BindInt(0, kMaxRowsInDB); |
193 statement_delete.Run(); | 199 statement_delete.Run(); |
194 } | 200 } |
195 | 201 |
196 runner->PostTask(FROM_HERE, | 202 runner->PostTask(FROM_HERE, |
197 base::Bind(callback, base::Passed(&black_list_item_map))); | 203 base::Bind(callback, base::Passed(&black_list_item_map), |
| 204 base::Passed(&host_indifferent_black_list_item))); |
198 } | 205 } |
199 | 206 |
200 // Synchronous implementations, these are run on the background thread | 207 // Synchronous implementations, these are run on the background thread |
201 // and actually do the work to access the SQL data base. | 208 // and actually do the work to access the SQL data base. |
202 void LoadBlackListSync(sql::Connection* db, | 209 void LoadBlackListSync(sql::Connection* db, |
203 const base::FilePath& path, | 210 const base::FilePath& path, |
204 scoped_refptr<base::SingleThreadTaskRunner> runner, | 211 scoped_refptr<base::SingleThreadTaskRunner> runner, |
205 LoadBlackListCallback callback) { | 212 LoadBlackListCallback callback) { |
206 if (!db->is_open()) | 213 if (!db->is_open()) |
207 InitDatabase(db, path); | 214 InitDatabase(db, path); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { | 283 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
277 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 284 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
278 if (!db_) | 285 if (!db_) |
279 db_ = base::MakeUnique<sql::Connection>(); | 286 db_ = base::MakeUnique<sql::Connection>(); |
280 background_task_runner_->PostTask( | 287 background_task_runner_->PostTask( |
281 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, | 288 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, |
282 base::ThreadTaskRunnerHandle::Get(), callback)); | 289 base::ThreadTaskRunnerHandle::Get(), callback)); |
283 } | 290 } |
284 | 291 |
285 } // namespace previews | 292 } // namespace previews |
OLD | NEW |