Allow the minimum time between banner trigger visits to be varied via field trials.
[chromium-blink-merge.git] / chrome / browser / predictors / predictor_database.cc
blobf948c4bdfc81f163cd5c7f3fbbb99525d4528f11
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/predictors/predictor_database.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
15 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
17 #include "chrome/browser/prerender/prerender_field_trial.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "sql/connection.h"
22 using content::BrowserThread;
24 namespace {
26 // TODO(shishir): This should move to a more generic name.
27 const base::FilePath::CharType kPredictorDatabaseName[] =
28 FILE_PATH_LITERAL("Network Action Predictor");
30 } // namespace
32 namespace predictors {
34 // Refcounted as it is created, initialized and destroyed on a different thread
35 // to the DB thread that is required for all methods performing database access.
36 class PredictorDatabaseInternal
37 : public base::RefCountedThreadSafe<PredictorDatabaseInternal> {
38 private:
39 friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>;
40 friend class PredictorDatabase;
42 explicit PredictorDatabaseInternal(Profile* profile);
43 virtual ~PredictorDatabaseInternal();
45 // Opens the database file from the profile path. Separated from the
46 // constructor to ease construction/destruction of this object on one thread
47 // but database access on the DB thread.
48 void Initialize();
49 void LogDatabaseStats(); // DB Thread.
51 // Cancels pending DB transactions. Should only be called on the UI thread.
52 void SetCancelled();
54 bool is_resource_prefetch_predictor_enabled_;
55 base::FilePath db_path_;
56 scoped_ptr<sql::Connection> db_;
58 // TODO(shishir): These tables may not need to be refcounted. Maybe move them
59 // to using a WeakPtr instead.
60 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
61 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_;
63 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal);
67 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile)
68 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)),
69 db_(new sql::Connection()),
70 autocomplete_table_(new AutocompleteActionPredictorTable()),
71 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) {
72 db_->set_histogram_tag("Predictor");
73 ResourcePrefetchPredictorConfig config;
74 is_resource_prefetch_predictor_enabled_ =
75 IsSpeculativeResourcePrefetchingEnabled(profile, &config);
78 PredictorDatabaseInternal::~PredictorDatabaseInternal() {
79 // The connection pointer needs to be deleted on the DB thread since there
80 // might be a task in progress on the DB thread which uses this connection.
81 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB))
82 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release());
85 void PredictorDatabaseInternal::Initialize() {
86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB) ||
87 !BrowserThread::IsMessageLoopValid(BrowserThread::DB));
88 // TODO(tburkard): figure out if we need this.
89 // db_->set_exclusive_locking();
90 bool success = db_->Open(db_path_);
92 if (!success)
93 return;
95 autocomplete_table_->Initialize(db_.get());
96 resource_prefetch_tables_->Initialize(db_.get());
98 // The logged_in_predictor table is obsolete as of Chrome 44.
99 // TODO(davidben): Remove this after April 16, 2016.
100 ignore_result(db_->Execute("DROP TABLE IF EXISTS logged_in_predictor"));
102 LogDatabaseStats();
105 void PredictorDatabaseInternal::SetCancelled() {
106 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
107 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
109 autocomplete_table_->SetCancelled();
110 resource_prefetch_tables_->SetCancelled();
113 void PredictorDatabaseInternal::LogDatabaseStats() {
114 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB) ||
115 !BrowserThread::IsMessageLoopValid(BrowserThread::DB));
117 int64 db_size;
118 bool success = base::GetFileSize(db_path_, &db_size);
119 DCHECK(success) << "Failed to get file size for " << db_path_.value();
120 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB",
121 static_cast<int>(db_size / 1024));
123 autocomplete_table_->LogDatabaseStats();
124 if (is_resource_prefetch_predictor_enabled_)
125 resource_prefetch_tables_->LogDatabaseStats();
128 PredictorDatabase::PredictorDatabase(Profile* profile)
129 : db_(new PredictorDatabaseInternal(profile)) {
130 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
131 base::Bind(&PredictorDatabaseInternal::Initialize, db_));
134 PredictorDatabase::~PredictorDatabase() {
137 void PredictorDatabase::Shutdown() {
138 db_->SetCancelled();
141 scoped_refptr<AutocompleteActionPredictorTable>
142 PredictorDatabase::autocomplete_table() {
143 return db_->autocomplete_table_;
146 scoped_refptr<ResourcePrefetchPredictorTables>
147 PredictorDatabase::resource_prefetch_tables() {
148 return db_->resource_prefetch_tables_;
151 sql::Connection* PredictorDatabase::GetDatabase() {
152 return db_->db_.get();
155 } // namespace predictors