Chromium Code Reviews| Index: components/previews/core/previews_opt_out_store_sql.cc |
| diff --git a/components/previews/core/previews_opt_out_store_sql.cc b/components/previews/core/previews_opt_out_store_sql.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f482e97f82908cc49a3e51e79c76dacc8e24f827 |
| --- /dev/null |
| +++ b/components/previews/core/previews_opt_out_store_sql.cc |
| @@ -0,0 +1,191 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/previews/core/previews_opt_out_store_sql.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/files/file_util.h" |
| +#include "base/location.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "components/previews/core/previews_black_list_item.h" |
| +#include "components/previews/core/previews_experiments.h" |
| +#include "sql/connection.h" |
| +#include "sql/statement.h" |
| +#include "sql/transaction.h" |
| + |
| +namespace previews { |
| + |
| +namespace { |
| + |
| +// This is a macro instead of a const, so it can be used inline in other SQL |
| +// statements below. |
| +#define PREVIEWS_TABLE_NAME "previews_v1" |
| + |
| +// New columns should be added at the end of the list in order to avoid |
| +// complicated table upgrade. |
| +const char kPreviewsColumns[] = |
| + "(host_name VARCHAR NOT NULL DEFAULT ''," |
| + " time INTEGER NOT NULL," |
| + " opt_out INTEGER NOT NULL," |
| + " type INTEGER NOT NULL)"; |
| + |
| +struct TableInfo { |
| + const char* table_name; |
| + const char* columns; |
| +}; |
| + |
| +enum : int { |
| + OP_HOST_NAME = 0, |
| + OP_TIME, |
| + OP_OPT_OUT, |
| + OP_TYPE, |
| +}; |
| + |
| +const TableInfo kPreviewsTable{PREVIEWS_TABLE_NAME, kPreviewsColumns}; |
| + |
| +bool CreateTable(sql::Connection* db, const TableInfo& table_info) { |
| + std::string sql("CREATE TABLE "); |
| + sql += table_info.table_name; |
| + sql += table_info.columns; |
| + return db->Execute(sql.c_str()); |
| +} |
| + |
| +void CreateSchema(sql::Connection* db) { |
| + sql::Transaction transaction(db); |
| + if (!transaction.Begin()) |
| + return; |
| + |
| + if (!db->DoesTableExist(kPreviewsTable.table_name)) { |
| + if (!CreateTable(db, kPreviewsTable)) |
| + return; |
| + } |
| + |
| + transaction.Commit(); |
| +} |
| + |
| +void InitDatabase(sql::Connection* db, base::FilePath path) { |
| + db->set_page_size(4096); |
| + db->set_cache_size(250); |
| + db->set_histogram_tag("PreviewsOptOut"); |
| + db->set_exclusive_locking(); |
| + |
| + base::File::Error err; |
| + if (!base::CreateDirectoryAndGetError(path.DirName(), &err)) { |
| + return; |
| + } |
| + if (!db->Open(path)) { |
| + return; |
| + } |
| + db->Preload(); |
| + |
| + CreateSchema(db); |
| +} |
| + |
| +void LoadBlackListFromDataBase( |
| + sql::Connection* db, |
| + scoped_refptr<base::SingleThreadTaskRunner> runner, |
| + LoadBlackListCallback callback) { |
| + // Gets the table sorted by host and time. Limits the number of hosts using |
| + // most recent opt_out time as the limiting function. |
| + const char kSql[] = |
| + "SELECT full_table.host_name, full_table.time, full_table.opt_out" |
| + " FROM " PREVIEWS_TABLE_NAME |
| + " full_table INNER JOIN" |
| + " (SELECT host_name," |
| + " MAX(CASE WHEN opt_out > 0 THEN time ELSE 0 END) AS max_opt_out_time," |
| + " MAX(time) AS max_time FROM " PREVIEWS_TABLE_NAME |
| + " GROUP BY host_name" |
| + " ORDER BY max_opt_out_time DESC, max_time DESC" |
| + " LIMIT ?) max_host_table" |
| + " ON max_host_table.host_name == full_table.host_name" |
| + " ORDER BY full_table.host_name, full_table.time DESC"; |
|
tbansal1
2016/10/04 15:01:21
Can you comment on why this needs to be ordered by
RyanSturm
2016/10/04 19:10:23
Removed the ORDER BY.
|
| + |
| + sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| + statement.BindInt(0, static_cast<int>(params::MaxInMemoryHostsInBlackList())); |
| + std::unique_ptr<BlackListItemMap> black_list_item_map(new BlackListItemMap()); |
| + // Add the host name, the visit time, and opt out history to |
| + // |black_list_item_map|. |
| + while (statement.Step()) { |
| + std::string host_name = statement.ColumnString(OP_HOST_NAME); |
| + BlackListItemMap::iterator iter = black_list_item_map->find(host_name); |
| + PreviewsBlackListItem* black_list_item; |
| + if (iter == black_list_item_map->end()) { |
| + black_list_item = new PreviewsBlackListItem( |
| + params::MaxStoredHistoryLengthForBlackList(), |
| + params::BlackListOptOutThreshold(), params::BlackListDuration()); |
| + black_list_item_map->operator[](host_name) = |
| + base::WrapUnique(black_list_item); |
| + } else { |
| + black_list_item = iter->second.get(); |
| + } |
| + // Allows the internal logic of PreviewsBlackListItem to determine how to |
| + // evict entries when there are more than |
| + // |StoredHistoryLengthForBlackList()| for the host. |
| + black_list_item->AddPreviewNavigation( |
| + statement.ColumnBool(OP_OPT_OUT), |
| + base::Time::FromInternalValue(statement.ColumnInt64(OP_TIME))); |
| + } |
| + |
| + runner->PostTask(FROM_HERE, |
| + base::Bind(callback, base::Passed(&black_list_item_map))); |
| +} |
| + |
| +} // namespace |
| + |
| +PreviewsOptOutStoreSQL::PreviewsOptOutStoreSQL( |
| + scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| + scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| + const base::FilePath& path) |
| + : io_task_runner_(io_task_runner), |
| + background_task_runner_(background_task_runner), |
| + db_file_path_(path.AppendASCII("Previews.db")), |
| + initialized_(false) {} |
| + |
| +PreviewsOptOutStoreSQL::~PreviewsOptOutStoreSQL() { |
| + DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| + if (db_.get()) { |
| + background_task_runner_->DeleteSoon(FROM_HERE, db_.release()); |
| + } |
| +} |
| + |
| +void PreviewsOptOutStoreSQL::AddPreviewNavigation(bool opt_out, |
| + const std::string& host_name, |
| + PreviewsType type, |
| + base::Time now) {} |
| + |
| +void PreviewsOptOutStoreSQL::LoadBlackList(LoadBlackListCallback callback) { |
| + DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| + if (!db_) |
| + db_.reset(new sql::Connection()); |
| + background_task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&PreviewsOptOutStoreSQL::LoadBlackListSync, |
| + db_.get(), db_file_path_, !initialized_, |
| + base::ThreadTaskRunnerHandle::Get(), callback)); |
| + initialized_ = true; |
| +} |
| + |
| +void PreviewsOptOutStoreSQL::LoadBlackListSync( |
| + sql::Connection* db, |
| + const base::FilePath& path, |
| + bool needs_initialization, |
| + scoped_refptr<base::SingleThreadTaskRunner> runner, |
| + LoadBlackListCallback callback) { |
| + if (needs_initialization) { |
| + InitDatabase(db, path); |
| + } |
| + |
| + if (!db->is_open() || !db->DoesTableExist(kPreviewsTable.table_name)) { |
| + std::unique_ptr<BlackListItemMap> black_list_item_map( |
| + new BlackListItemMap()); |
| + runner->PostTask(FROM_HERE, |
| + base::Bind(callback, base::Passed(&black_list_item_map))); |
| + } |
| + |
| + LoadBlackListFromDataBase(db, runner, callback); |
| +} |
| + |
| +} // namespace previews |