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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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. |
154 const char kSql[] = | 154 const char kSql[] = |
155 "SELECT host_name, time, opt_out" | 155 "SELECT host_name, time, opt_out" |
156 " FROM " PREVIEWS_TABLE_NAME; | 156 " FROM " PREVIEWS_TABLE_NAME " ORDER BY host_name, time DESC"; |
tbansal1
2016/10/25 16:15:48
May be add a comment explaining why ordering is im
RyanSturm
2016/10/25 18:15:44
Done.
| |
157 | 157 |
158 sql::Statement statement(db->GetUniqueStatement(kSql)); | 158 sql::Statement statement(db->GetUniqueStatement(kSql)); |
159 | 159 |
160 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); | 160 std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); |
161 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item = | |
162 PreviewsBlackList::CreateHostIndifferentBlackListItem(); | |
161 int count = 0; | 163 int count = 0; |
162 // Add the host name, the visit time, and opt out history to | 164 // Add the host name, the visit time, and opt out history to |
163 // |black_list_item_map|. | 165 // |black_list_item_map|. |
164 while (statement.Step()) { | 166 while (statement.Step()) { |
165 ++count; | 167 ++count; |
166 std::string host_name = statement.ColumnString(0); | 168 std::string host_name = statement.ColumnString(0); |
167 PreviewsBlackListItem* black_list_item = | 169 PreviewsBlackListItem* black_list_item = |
168 PreviewsBlackList::GetOrCreateBlackListItem(black_list_item_map.get(), | 170 PreviewsBlackList::GetOrCreateBlackListItemForMap( |
169 host_name); | 171 black_list_item_map.get(), host_name); |
170 DCHECK_LE(black_list_item_map->size(), | 172 DCHECK_LE(black_list_item_map->size(), |
171 params::MaxInMemoryHostsInBlackList()); | 173 params::MaxInMemoryHostsInBlackList()); |
172 // Allows the internal logic of PreviewsBlackListItem to determine how to | 174 // Allows the internal logic of PreviewsBlackListItem to determine how to |
173 // evict entries when there are more than | 175 // evict entries when there are more than |
174 // |StoredHistoryLengthForBlackList()| for the host. | 176 // |StoredHistoryLengthForBlackList()| for the host. |
175 black_list_item->AddPreviewNavigation( | 177 black_list_item->AddPreviewNavigation( |
176 statement.ColumnBool(2), | 178 statement.ColumnBool(2), |
177 base::Time::FromInternalValue(statement.ColumnInt64(1))); | 179 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
180 host_indifferent_black_list_item->AddPreviewNavigation( | |
181 statement.ColumnBool(2), | |
182 base::Time::FromInternalValue(statement.ColumnInt64(1))); | |
178 } | 183 } |
179 | 184 |
180 // TODO(ryansturm): Add UMA to log |count|. crbug.com/656739 | 185 // TODO(ryansturm): Add UMA to log |count|. crbug.com/656739 |
181 if (count > kMaxRowsInDB) { | 186 if (count > kMaxRowsInDB) { |
182 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. | 187 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. |
183 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 188 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
184 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 189 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
185 " WHERE ROWID IN" | 190 " WHERE ROWID IN" |
186 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 191 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
187 " ORDER BY time DESC" | 192 " ORDER BY time DESC" |
188 " LIMIT -1 OFFSET ?)"; | 193 " LIMIT -1 OFFSET ?)"; |
189 | 194 |
190 sql::Statement statement_delete( | 195 sql::Statement statement_delete( |
191 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); | 196 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); |
192 statement_delete.BindInt(0, kMaxRowsInDB); | 197 statement_delete.BindInt(0, kMaxRowsInDB); |
193 statement_delete.Run(); | 198 statement_delete.Run(); |
194 } | 199 } |
195 | 200 |
196 runner->PostTask(FROM_HERE, | 201 runner->PostTask(FROM_HERE, |
197 base::Bind(callback, base::Passed(&black_list_item_map))); | 202 base::Bind(callback, base::Passed(&black_list_item_map), |
203 base::Passed(&host_indifferent_black_list_item))); | |
198 } | 204 } |
199 | 205 |
200 // Synchronous implementations, these are run on the background thread | 206 // Synchronous implementations, these are run on the background thread |
201 // and actually do the work to access the SQL data base. | 207 // and actually do the work to access the SQL data base. |
202 void LoadBlackListSync(sql::Connection* db, | 208 void LoadBlackListSync(sql::Connection* db, |
203 const base::FilePath& path, | 209 const base::FilePath& path, |
204 scoped_refptr<base::SingleThreadTaskRunner> runner, | 210 scoped_refptr<base::SingleThreadTaskRunner> runner, |
205 LoadBlackListCallback callback) { | 211 LoadBlackListCallback callback) { |
206 if (!db->is_open()) | 212 if (!db->is_open()) |
207 InitDatabase(db, path); | 213 InitDatabase(db, path); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { | 282 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
277 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 283 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
278 if (!db_) | 284 if (!db_) |
279 db_ = base::MakeUnique<sql::Connection>(); | 285 db_ = base::MakeUnique<sql::Connection>(); |
280 background_task_runner_->PostTask( | 286 background_task_runner_->PostTask( |
281 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, | 287 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, |
282 base::ThreadTaskRunnerHandle::Get(), callback)); | 288 base::ThreadTaskRunnerHandle::Get(), callback)); |
283 } | 289 } |
284 | 290 |
285 } // namespace previews | 291 } // namespace previews |
OLD | NEW |