Chromium Code Reviews| Index: chrome/browser/predictors/logged_in_predictor_table.cc |
| =================================================================== |
| --- chrome/browser/predictors/logged_in_predictor_table.cc (revision 0) |
| +++ chrome/browser/predictors/logged_in_predictor_table.cc (revision 0) |
| @@ -0,0 +1,129 @@ |
| +// Copyright (c) 2013 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 "chrome/browser/predictors/logged_in_predictor_table.h" |
| + |
| +#include <algorithm> |
| +#include <utility> |
| +#include "base/logging.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/stringprintf.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| +#include "sql/statement.h" |
| + |
| +using content::BrowserThread; |
| +using sql::Statement; |
| +using std::string; |
| + |
| +namespace { |
| + |
| +const char kTableName[] = "logged_in_predictor"; |
| + |
| +} // namespace |
| + |
| +namespace predictors { |
| + |
| +LoggedInPredictorTable::LoggedInPredictorTable() |
| + : PredictorTableBase() { |
| +} |
| + |
| +LoggedInPredictorTable::~LoggedInPredictorTable() { |
| +} |
| + |
| +string LoggedInPredictorTable::GetKey(const GURL& url) { |
| + string effective_domain( |
| + net::RegistryControlledDomainService::GetDomainAndRegistry(url.host())); |
| + if (effective_domain.empty()) |
| + effective_domain = url.host(); |
| + |
| + if (!effective_domain.empty() && effective_domain[0] == '.') |
|
Shishir
2013/04/16 21:22:04
Comment on this code.
tburkard
2013/04/16 21:47:19
Done.
|
| + return effective_domain.substr(1); |
| + return effective_domain; |
| +} |
| + |
| +void LoggedInPredictorTable::Add(const GURL& url) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + if (CantAccessDatabase()) |
| + return; |
| + |
| + Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| + base::StringPrintf("INSERT OR IGNORE INTO %s (domain, time) VALUES (?,?)", |
| + kTableName).c_str())); |
| + |
| + statement.BindString(0, GetKey(url)); |
| + statement.BindInt64(1, base::Time::Now().ToInternalValue()); |
| + |
| + statement.Run(); |
| +} |
| + |
| +void LoggedInPredictorTable::Exists(const GURL& url, bool* is_present, |
| + bool* lookup_succeeded) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + *lookup_succeeded = false; |
| + if (CantAccessDatabase()) |
| + return; |
| + |
| + Statement statement(DB()->GetUniqueStatement( |
|
Shishir
2013/04/16 21:22:04
GetCachedStatement.
tburkard
2013/04/16 21:47:19
Done.
|
| + base::StringPrintf("SELECT count(*) FROM %s WHERE domain=?", |
| + kTableName).c_str())); |
| + |
| + statement.BindString(0, GetKey(url)); |
| + |
| + if (statement.Step()) { |
| + *is_present = (statement.ColumnInt(0) > 0); |
| + *lookup_succeeded = true; |
| + } |
| +} |
| + |
| +void LoggedInPredictorTable::DeleteAllCreatedBetween( |
| + const base::Time& delete_begin, const base::Time& delete_end) { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + if (CantAccessDatabase()) |
| + return; |
| + |
| + Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| + base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?", |
| + kTableName).c_str())); |
| + |
| + statement.BindInt64(0, delete_begin.ToInternalValue()); |
| + statement.BindInt64(1, delete_end.ToInternalValue()); |
| + |
| + statement.Run(); |
| +} |
| + |
| +void LoggedInPredictorTable::CreateTableIfNonExistent() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + if (CantAccessDatabase()) |
| + return; |
| + |
| + const char* table_creator = |
| + "CREATE TABLE %s ( " |
|
Shishir
2013/04/16 21:22:04
Will probably fit in one line?
tburkard
2013/04/16 21:47:19
Done.
|
| + "domain TEXT, " |
| + "time INTEGER, " |
| + "PRIMARY KEY(domain))"; |
| + |
| + sql::Connection* db = DB(); |
| + bool success = |
|
Shishir
2013/04/16 21:22:04
Since you have only one table, put
if (db->DoesTa
tburkard
2013/04/16 21:47:19
Done.
|
| + (db->DoesTableExist(kTableName) || |
| + db->Execute(base::StringPrintf(table_creator, kTableName).c_str())); |
| + |
| + if (!success) |
| + ResetDB(); |
| +} |
| + |
| +void LoggedInPredictorTable::LogDatabaseStats() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| + if (CantAccessDatabase()) |
| + return; |
| + |
| + Statement statement(DB()->GetUniqueStatement( |
| + base::StringPrintf("SELECT count(*) FROM %s", |
|
Shishir
2013/04/16 21:22:04
one line?
tburkard
2013/04/16 21:47:19
Done.
|
| + kTableName).c_str())); |
| + if (statement.Step()) |
| + UMA_HISTOGRAM_COUNTS("LoggedInPredictor.TableRowCount", |
| + statement.ColumnInt(0)); |
| +} |
| + |
| +} // namespace predictors |
| Property changes on: chrome/browser/predictors/logged_in_predictor_table.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |