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