OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/predictors/logged_in_predictor_table.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <utility> |
| 9 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/stringprintf.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 14 #include "sql/statement.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 using sql::Statement; |
| 18 using std::string; |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kTableName[] = "logged_in_predictor"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 namespace predictors { |
| 27 |
| 28 LoggedInPredictorTable::LoggedInPredictorTable() |
| 29 : PredictorTableBase() { |
| 30 } |
| 31 |
| 32 LoggedInPredictorTable::~LoggedInPredictorTable() { |
| 33 } |
| 34 |
| 35 string LoggedInPredictorTable::GetKey(const GURL& url) const { |
| 36 string effective_domain( |
| 37 net::RegistryControlledDomainService::GetDomainAndRegistry(url.host())); |
| 38 if (effective_domain.empty()) |
| 39 effective_domain = url.host(); |
| 40 |
| 41 // Strip off a preceding ".", if present. |
| 42 if (!effective_domain.empty() && effective_domain[0] == '.') |
| 43 return effective_domain.substr(1); |
| 44 return effective_domain; |
| 45 } |
| 46 |
| 47 void LoggedInPredictorTable::Add(const GURL& url) { |
| 48 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 49 if (CantAccessDatabase()) |
| 50 return; |
| 51 |
| 52 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 53 base::StringPrintf("INSERT OR IGNORE INTO %s (domain, time) VALUES (?,?)", |
| 54 kTableName).c_str())); |
| 55 |
| 56 statement.BindString(0, GetKey(url)); |
| 57 statement.BindInt64(1, base::Time::Now().ToInternalValue()); |
| 58 |
| 59 statement.Run(); |
| 60 } |
| 61 |
| 62 void LoggedInPredictorTable::HasUserLoggedIn(const GURL& url, bool* is_present, |
| 63 bool* lookup_succeeded) { |
| 64 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 65 *lookup_succeeded = false; |
| 66 if (CantAccessDatabase()) |
| 67 return; |
| 68 |
| 69 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 70 base::StringPrintf("SELECT count(*) FROM %s WHERE domain=?", |
| 71 kTableName).c_str())); |
| 72 |
| 73 statement.BindString(0, GetKey(url)); |
| 74 |
| 75 if (statement.Step()) { |
| 76 *is_present = (statement.ColumnInt(0) > 0); |
| 77 *lookup_succeeded = true; |
| 78 } |
| 79 } |
| 80 |
| 81 void LoggedInPredictorTable::DeleteAllCreatedBetween( |
| 82 const base::Time& delete_begin, const base::Time& delete_end) { |
| 83 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 84 if (CantAccessDatabase()) |
| 85 return; |
| 86 |
| 87 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 88 base::StringPrintf("DELETE FROM %s WHERE time >= ? AND time <= ?", |
| 89 kTableName).c_str())); |
| 90 |
| 91 statement.BindInt64(0, delete_begin.ToInternalValue()); |
| 92 statement.BindInt64(1, delete_end.ToInternalValue()); |
| 93 |
| 94 statement.Run(); |
| 95 } |
| 96 |
| 97 void LoggedInPredictorTable::CreateTableIfNonExistent() { |
| 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 99 if (CantAccessDatabase()) |
| 100 return; |
| 101 |
| 102 sql::Connection* db = DB(); |
| 103 if (db->DoesTableExist(kTableName)) |
| 104 return; |
| 105 |
| 106 const char* table_creator = |
| 107 "CREATE TABLE %s (domain TEXT, time INTEGER, PRIMARY KEY(domain))"; |
| 108 |
| 109 if (!db->Execute(base::StringPrintf(table_creator, kTableName).c_str())) |
| 110 ResetDB(); |
| 111 } |
| 112 |
| 113 void LoggedInPredictorTable::LogDatabaseStats() { |
| 114 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 115 if (CantAccessDatabase()) |
| 116 return; |
| 117 |
| 118 Statement statement(DB()->GetCachedStatement(SQL_FROM_HERE, |
| 119 base::StringPrintf("SELECT count(*) FROM %s", kTableName).c_str())); |
| 120 if (statement.Step()) |
| 121 UMA_HISTOGRAM_COUNTS("LoggedInPredictor.TableRowCount", |
| 122 statement.ColumnInt(0)); |
| 123 } |
| 124 |
| 125 } // namespace predictors |
OLD | NEW |