| 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> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" |
| 9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 10 #include "base/location.h" | 13 #include "base/location.h" |
| 11 #include "base/logging.h" | 14 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 14 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "components/previews/core/previews_black_list.h" | 20 #include "components/previews/core/previews_black_list.h" |
| 17 #include "components/previews/core/previews_black_list_item.h" | 21 #include "components/previews/core/previews_black_list_item.h" |
| 18 #include "components/previews/core/previews_experiments.h" | 22 #include "components/previews/core/previews_experiments.h" |
| 19 #include "sql/connection.h" | 23 #include "sql/connection.h" |
| 20 #include "sql/recovery.h" | 24 #include "sql/recovery.h" |
| 21 #include "sql/statement.h" | 25 #include "sql/statement.h" |
| 22 #include "sql/transaction.h" | 26 #include "sql/transaction.h" |
| 23 | 27 |
| 24 namespace previews { | 28 namespace previews { |
| 25 | 29 |
| 26 namespace { | 30 namespace { |
| 27 | 31 |
| 32 // Command line switch to change the previews per row DB size. |
| 33 const char kMaxRowsPerHost[] = "previews-max-opt-out-rows-per-host"; |
| 34 |
| 35 // Command line switch to change the previews DB size. |
| 36 const char kMaxRows[] = "previews-max-opt-out-rows"; |
| 37 |
| 38 // Returns the maximum number of table rows allowed per host for the previews |
| 39 // opt out store. This is enforced during insertion of new navigation entries. |
| 40 int MaxRowsPerHostInOptOutDB() { |
| 41 std::string max_rows = |
| 42 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 43 kMaxRowsPerHost); |
| 44 int value; |
| 45 return base::StringToInt(max_rows, &value) ? value : 32; |
| 46 } |
| 47 |
| 48 // Returns the maximum number of table rows allowed for the previews opt out |
| 49 // store. This is enforced during load time; thus the database can grow |
| 50 // larger than this temporarily. |
| 51 int MaxRowsInOptOutDB() { |
| 52 std::string max_rows = |
| 53 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kMaxRows); |
| 54 int value; |
| 55 return base::StringToInt(max_rows, &value) ? value : 3200; |
| 56 } |
| 57 |
| 28 // This is a macro instead of a const, so it can be used inline in other SQL | 58 // This is a macro instead of a const, so it can be used inline in other SQL |
| 29 // statements below. | 59 // statements below. |
| 30 #define PREVIEWS_TABLE_NAME "previews_v1" | 60 #define PREVIEWS_TABLE_NAME "previews_v1" |
| 31 | 61 |
| 32 // The maximum number of entries allowed per host. Entries are evicted based on | |
| 33 // entry time. | |
| 34 const int kMaxRowsPerHost = 32; | |
| 35 | |
| 36 // The maximum number of entries allowed in the data base. Entries are evicted | |
| 37 // based on entry time. | |
| 38 const int kMaxRowsInDB = 3200; | |
| 39 | |
| 40 void CreateSchema(sql::Connection* db) { | 62 void CreateSchema(sql::Connection* db) { |
| 41 const char kSql[] = "CREATE TABLE IF NOT EXISTS " PREVIEWS_TABLE_NAME | 63 const char kSql[] = "CREATE TABLE IF NOT EXISTS " PREVIEWS_TABLE_NAME |
| 42 " (host_name VARCHAR NOT NULL," | 64 " (host_name VARCHAR NOT NULL," |
| 43 " time INTEGER NOT NULL," | 65 " time INTEGER NOT NULL," |
| 44 " opt_out INTEGER NOT NULL," | 66 " opt_out INTEGER NOT NULL," |
| 45 " type INTEGER NOT NULL," | 67 " type INTEGER NOT NULL," |
| 46 " PRIMARY KEY(host_name, time DESC, opt_out, type))"; | 68 " PRIMARY KEY(host_name, time DESC, opt_out, type))"; |
| 47 if (!db->Execute(kSql)) | 69 if (!db->Execute(kSql)) |
| 48 return; | 70 return; |
| 49 } | 71 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 statement_insert.BindInt64(1, now.ToInternalValue()); | 144 statement_insert.BindInt64(1, now.ToInternalValue()); |
| 123 statement_insert.BindBool(2, opt_out); | 145 statement_insert.BindBool(2, opt_out); |
| 124 statement_insert.BindInt(3, static_cast<int>(type)); | 146 statement_insert.BindInt(3, static_cast<int>(type)); |
| 125 statement_insert.Run(); | 147 statement_insert.Run(); |
| 126 } | 148 } |
| 127 | 149 |
| 128 // Removes entries for |host_name| if the per-host row limit is exceeded. | 150 // Removes entries for |host_name| if the per-host row limit is exceeded. |
| 129 // Removes entries if per data base row limit is exceeded. | 151 // Removes entries if per data base row limit is exceeded. |
| 130 void MaybeEvictHostEntryFromDataBase(sql::Connection* db, | 152 void MaybeEvictHostEntryFromDataBase(sql::Connection* db, |
| 131 const std::string& host_name) { | 153 const std::string& host_name) { |
| 132 // Delete the oldest entries if there are more than |kMaxRowsPerHost| for | 154 // Delete the oldest entries if there are more than |MaxRowsPerHostInOptOutDB| |
| 133 // |host_name|. | 155 // for |host_name|. |
| 134 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 156 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
| 135 const char kSqlDeleteByHost[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 157 const char kSqlDeleteByHost[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
| 136 " WHERE ROWID IN" | 158 " WHERE ROWID IN" |
| 137 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 159 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| 138 " WHERE host_name == ?" | 160 " WHERE host_name == ?" |
| 139 " ORDER BY time DESC" | 161 " ORDER BY time DESC" |
| 140 " LIMIT -1 OFFSET ?)"; | 162 " LIMIT -1 OFFSET ?)"; |
| 141 | 163 |
| 142 sql::Statement statement_delete_by_host( | 164 sql::Statement statement_delete_by_host( |
| 143 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByHost)); | 165 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByHost)); |
| 144 statement_delete_by_host.BindString(0, host_name); | 166 statement_delete_by_host.BindString(0, host_name); |
| 145 statement_delete_by_host.BindInt(1, kMaxRowsPerHost); | 167 statement_delete_by_host.BindInt(1, MaxRowsPerHostInOptOutDB()); |
| 146 statement_delete_by_host.Run(); | 168 statement_delete_by_host.Run(); |
| 147 } | 169 } |
| 148 | 170 |
| 149 void LoadBlackListFromDataBase( | 171 void LoadBlackListFromDataBase( |
| 150 sql::Connection* db, | 172 sql::Connection* db, |
| 151 scoped_refptr<base::SingleThreadTaskRunner> runner, | 173 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 152 LoadBlackListCallback callback) { | 174 LoadBlackListCallback callback) { |
| 153 // 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 |
| 154 // most recent opt_out time as the limiting function. | 176 // most recent opt_out time as the limiting function. |
| 155 const char kSql[] = | 177 const char kSql[] = |
| (...skipping 17 matching lines...) Expand all Loading... |
| 173 // Allows the internal logic of PreviewsBlackListItem to determine how to | 195 // Allows the internal logic of PreviewsBlackListItem to determine how to |
| 174 // evict entries when there are more than | 196 // evict entries when there are more than |
| 175 // |StoredHistoryLengthForBlackList()| for the host. | 197 // |StoredHistoryLengthForBlackList()| for the host. |
| 176 black_list_item->AddPreviewNavigation( | 198 black_list_item->AddPreviewNavigation( |
| 177 statement.ColumnBool(2), | 199 statement.ColumnBool(2), |
| 178 base::Time::FromInternalValue(statement.ColumnInt64(1))); | 200 base::Time::FromInternalValue(statement.ColumnInt64(1))); |
| 179 } | 201 } |
| 180 | 202 |
| 181 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); | 203 UMA_HISTOGRAM_COUNTS_10000("Previews.OptOut.DBRowCount", count); |
| 182 | 204 |
| 183 if (count > kMaxRowsInDB) { | 205 if (count > MaxRowsInOptOutDB()) { |
| 184 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. | 206 // Delete the oldest entries if there are more than |kMaxEntriesInDB|. |
| 185 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. | 207 // DELETE ... LIMIT -1 OFFSET x means delete all but the first x entries. |
| 186 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME | 208 const char kSqlDeleteByDBSize[] = "DELETE FROM " PREVIEWS_TABLE_NAME |
| 187 " WHERE ROWID IN" | 209 " WHERE ROWID IN" |
| 188 " (SELECT ROWID from " PREVIEWS_TABLE_NAME | 210 " (SELECT ROWID from " PREVIEWS_TABLE_NAME |
| 189 " ORDER BY time DESC" | 211 " ORDER BY time DESC" |
| 190 " LIMIT -1 OFFSET ?)"; | 212 " LIMIT -1 OFFSET ?)"; |
| 191 | 213 |
| 192 sql::Statement statement_delete( | 214 sql::Statement statement_delete( |
| 193 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); | 215 db->GetCachedStatement(SQL_FROM_HERE, kSqlDeleteByDBSize)); |
| 194 statement_delete.BindInt(0, kMaxRowsInDB); | 216 statement_delete.BindInt(0, MaxRowsInOptOutDB()); |
| 195 statement_delete.Run(); | 217 statement_delete.Run(); |
| 196 } | 218 } |
| 197 | 219 |
| 198 runner->PostTask(FROM_HERE, | 220 runner->PostTask(FROM_HERE, |
| 199 base::Bind(callback, base::Passed(&black_list_item_map))); | 221 base::Bind(callback, base::Passed(&black_list_item_map))); |
| 200 } | 222 } |
| 201 | 223 |
| 202 // Synchronous implementations, these are run on the background thread | 224 // Synchronous implementations, these are run on the background thread |
| 203 // and actually do the work to access the SQL data base. | 225 // and actually do the work to access the SQL data base. |
| 204 void LoadBlackListSync(sql::Connection* db, | 226 void LoadBlackListSync(sql::Connection* db, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { | 300 void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
| 279 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 301 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 280 if (!db_) | 302 if (!db_) |
| 281 db_ = base::MakeUnique<sql::Connection>(); | 303 db_ = base::MakeUnique<sql::Connection>(); |
| 282 background_task_runner_->PostTask( | 304 background_task_runner_->PostTask( |
| 283 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, | 305 FROM_HERE, base::Bind(&LoadBlackListSync, db_.get(), db_file_path_, |
| 284 base::ThreadTaskRunnerHandle::Get(), callback)); | 306 base::ThreadTaskRunnerHandle::Get(), callback)); |
| 285 } | 307 } |
| 286 | 308 |
| 287 } // namespace previews | 309 } // namespace previews |
| OLD | NEW |