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