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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 statement_delete_by_host.BindString(0, host_name); | 166 statement_delete_by_host.BindString(0, host_name); |
167 statement_delete_by_host.BindInt(1, MaxRowsPerHostInOptOutDB()); | 167 statement_delete_by_host.BindInt(1, MaxRowsPerHostInOptOutDB()); |
168 statement_delete_by_host.Run(); | 168 statement_delete_by_host.Run(); |
169 } | 169 } |
170 | 170 |
171 void LoadBlackListFromDataBase( | 171 void LoadBlackListFromDataBase( |
172 sql::Connection* db, | 172 sql::Connection* db, |
173 scoped_refptr<base::SingleThreadTaskRunner> runner, | 173 scoped_refptr<base::SingleThreadTaskRunner> runner, |
174 LoadBlackListCallback callback) { | 174 LoadBlackListCallback callback) { |
175 // Gets the table sorted by host and time. Limits the number of hosts using | 175 // Gets the table sorted by host and time. Limits the number of hosts using |
176 // most recent opt_out time as the limiting function. | 176 // most recent opt_out time as the limiting function. Sorting is free due to |
| 177 // the table structure, and it improves performance in the loop below. |
177 const char kSql[] = | 178 const char kSql[] = |
178 "SELECT host_name, time, opt_out" | 179 "SELECT host_name, time, opt_out" |
179 " FROM " PREVIEWS_TABLE_NAME; | 180 " FROM " PREVIEWS_TABLE_NAME " ORDER BY host_name, time DESC"; |
180 | 181 |
181 sql::Statement statement(db->GetUniqueStatement(kSql)); | 182 sql::Statement statement(db->GetUniqueStatement(kSql)); |
182 | 183 |
183 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); | 184 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); |
| 185 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item = |
| 186 PreviewsBlackList::CreateHostIndifferentBlackListItem(); |
184 int count = 0; | 187 int count = 0; |
185 // Add the host name, the visit time, and opt out history to | 188 // Add the host name, the visit time, and opt out history to |
186 // |black_list_item_map|. | 189 // |black_list_item_map|. |
187 while (statement.Step()) { | 190 while (statement.Step()) { |
188 ++count; | 191 ++count; |
189 std::string host_name = statement.ColumnString(0); | 192 std::string host_name = statement.ColumnString(0); |
190 PreviewsBlackListItem* black_list_item = | 193 PreviewsBlackListItem* black_list_item = |
191 PreviewsBlackList::GetOrCreateBlackListItem(black_list_item_map.get(), | 194 PreviewsBlackList::GetOrCreateBlackListItemForMap( |
192 host_name); | 195 black_list_item_map.get(), host_name); |
193 DCHECK_LE(black_list_item_map->size(), | 196 DCHECK_LE(black_list_item_map->size(), |
194 params::MaxInMemoryHostsInBlackList()); | 197 params::MaxInMemoryHostsInBlackList()); |
195 // Allows the internal logic of PreviewsBlackListItem to determine how to | 198 // Allows the internal logic of PreviewsBlackListItem to determine how to |
196 // evict entries when there are more than | 199 // evict entries when there are more than |
197 // |StoredHistoryLengthForBlackList()| for the host. | 200 // |StoredHistoryLengthForBlackList()| for the host. |
198 black_list_item->AddPreviewNavigation( | 201 black_list_item->AddPreviewNavigation( |
199 statement.ColumnBool(2), | 202 statement.ColumnBool(2), |
200 base::Time::FromInternalValue(statement.ColumnInt64(1))); | 203 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
| 204 // Allows the internal logic of PreviewsBlackListItem to determine what |
| 205 // items to evict. |
| 206 host_indifferent_black_list_item->AddPreviewNavigation( |
| 207 statement.ColumnBool(2), |
| 208 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
201 } | 209 } |
202 | 210 |
203 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); | 211 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); |
204 | 212 |
205 if (count > MaxRowsInOptOutDB()) { | 213 if (count > MaxRowsInOptOutDB()) { |
206 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. | 214 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. |
207 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 215 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
208 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 216 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
209 " WHERE ROWID IN" | 217 " WHERE ROWID IN" |
210 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 218 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
211 " ORDER BY time DESC" | 219 " ORDER BY time DESC" |
212 " LIMIT -1 OFFSET ?)"; | 220 " LIMIT -1 OFFSET ?)"; |
213 | 221 |
214 sql::Statement statement_delete( | 222 sql::Statement statement_delete( |
215 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); | 223 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); |
216 statement_delete.BindInt(0, MaxRowsInOptOutDB()); | 224 statement_delete.BindInt(0, MaxRowsInOptOutDB()); |
217 statement_delete.Run(); | 225 statement_delete.Run(); |
218 } | 226 } |
219 | 227 |
220 runner->PostTask(FROM_HERE, | 228 runner->PostTask(FROM_HERE, |
221 base::Bind(callback, base::Passed(&black_list_item_map))); | 229 base::Bind(callback, base::Passed(&black_list_item_map), |
| 230 base::Passed(&host_indifferent_black_list_item))); |
222 } | 231 } |
223 | 232 |
224 // Synchronous implementations, these are run on the background thread | 233 // Synchronous implementations, these are run on the background thread |
225 // and actually do the work to access the SQL data base. | 234 // and actually do the work to access the SQL data base. |
226 void LoadBlackListSync(sql::Connection* db, | 235 void LoadBlackListSync(sql::Connection* db, |
227 const base::FilePath& path, | 236 const base::FilePath& path, |
228 scoped_refptr<base::SingleThreadTaskRunner> runner, | 237 scoped_refptr<base::SingleThreadTaskRunner> runner, |
229 LoadBlackListCallback callback) { | 238 LoadBlackListCallback callback) { |
230 if (!db->is_open()) | 239 if (!db->is_open()) |
231 InitDatabase(db, path); | 240 InitDatabase(db, path); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { | 309 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
301 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 310 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
302 if (!db_) | 311 if (!db_) |
303 db_ = base::MakeUnique<sql::Connection>(); | 312 db_ = base::MakeUnique<sql::Connection>(); |
304 background_task_runner_->PostTask( | 313 background_task_runner_->PostTask( |
305 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, | 314 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, |
306 base::ThreadTaskRunnerHandle::Get(), callback)); | 315 base::ThreadTaskRunnerHandle::Get(), callback)); |
307 } | 316 } |
308 | 317 |
309 } // namespace previews | 318 } // namespace previews |
OLD | NEW |