1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright (c) 2012 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/predictor_database.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
#include "chrome/browser/predictors/logged_in_predictor_table.h"
#include "chrome/browser/predictors/resource_prefetch_predictor.h"
#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
#include "chrome/browser/prerender/prerender_field_trial.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "sql/connection.h"
#include "sql/statement.h"
using content::BrowserThread;
namespace {
// TODO(shishir): This should move to a more generic name.
const base::FilePath::CharType kPredictorDatabaseName[] =
FILE_PATH_LITERAL("Network Action Predictor");
} // namespace
namespace predictors {
// Refcounted as it is created, initialized and destroyed on a different thread
// to the DB thread that is required for all methods performing database access.
class PredictorDatabaseInternal
: public base::RefCountedThreadSafe<PredictorDatabaseInternal> {
private:
friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>;
friend class PredictorDatabase;
explicit PredictorDatabaseInternal(Profile* profile);
virtual ~PredictorDatabaseInternal();
// Opens the database file from the profile path. Separated from the
// constructor to ease construction/destruction of this object on one thread
// but database access on the DB thread.
void Initialize();
void LogDatabaseStats(); // DB Thread.
// Cancels pending DB transactions. Should only be called on the UI thread.
void SetCancelled();
bool is_resource_prefetch_predictor_enabled_;
base::FilePath db_path_;
scoped_ptr<sql::Connection> db_;
// TODO(shishir): These tables may not need to be refcounted. Maybe move them
// to using a WeakPtr instead.
scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
scoped_refptr<LoggedInPredictorTable> logged_in_table_;
scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_;
DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal);
};
PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile)
: db_path_(profile->GetPath().Append(kPredictorDatabaseName)),
db_(new sql::Connection()),
autocomplete_table_(new AutocompleteActionPredictorTable()),
logged_in_table_(new LoggedInPredictorTable()),
resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) {
db_->set_histogram_tag("Predictor");
ResourcePrefetchPredictorConfig config;
is_resource_prefetch_predictor_enabled_ =
IsSpeculativeResourcePrefetchingEnabled(profile, &config);
}
PredictorDatabaseInternal::~PredictorDatabaseInternal() {
// The connection pointer needs to be deleted on the DB thread since there
// might be a task in progress on the DB thread which uses this connection.
if (BrowserThread::IsMessageLoopValid(BrowserThread::DB))
BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release());
}
void PredictorDatabaseInternal::Initialize() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB) ||
!BrowserThread::IsMessageLoopValid(BrowserThread::DB));
// TODO(tburkard): figure out if we need this.
// db_->set_exclusive_locking();
bool success = db_->Open(db_path_);
if (!success)
return;
autocomplete_table_->Initialize(db_.get());
logged_in_table_->Initialize(db_.get());
resource_prefetch_tables_->Initialize(db_.get());
LogDatabaseStats();
}
void PredictorDatabaseInternal::SetCancelled() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
!BrowserThread::IsMessageLoopValid(BrowserThread::UI));
autocomplete_table_->SetCancelled();
logged_in_table_->SetCancelled();
resource_prefetch_tables_->SetCancelled();
}
void PredictorDatabaseInternal::LogDatabaseStats() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB) ||
!BrowserThread::IsMessageLoopValid(BrowserThread::DB));
int64 db_size;
bool success = base::GetFileSize(db_path_, &db_size);
DCHECK(success) << "Failed to get file size for " << db_path_.value();
UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB",
static_cast<int>(db_size / 1024));
autocomplete_table_->LogDatabaseStats();
logged_in_table_->LogDatabaseStats();
if (is_resource_prefetch_predictor_enabled_)
resource_prefetch_tables_->LogDatabaseStats();
}
PredictorDatabase::PredictorDatabase(Profile* profile)
: db_(new PredictorDatabaseInternal(profile)) {
BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
base::Bind(&PredictorDatabaseInternal::Initialize, db_));
}
PredictorDatabase::~PredictorDatabase() {
}
void PredictorDatabase::Shutdown() {
db_->SetCancelled();
}
scoped_refptr<AutocompleteActionPredictorTable>
PredictorDatabase::autocomplete_table() {
return db_->autocomplete_table_;
}
scoped_refptr<LoggedInPredictorTable>
PredictorDatabase::logged_in_table() {
return db_->logged_in_table_;
}
scoped_refptr<ResourcePrefetchPredictorTables>
PredictorDatabase::resource_prefetch_tables() {
return db_->resource_prefetch_tables_;
}
sql::Connection* PredictorDatabase::GetDatabase() {
return db_->db_.get();
}
} // namespace predictors
|