| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/predictors/predictor_database.h" | 5 #include "chrome/browser/predictors/predictor_database.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" | 13 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" |
| 14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 14 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "sql/connection.h" | 18 #include "sql/connection.h" |
| 18 #include "sql/statement.h" | 19 #include "sql/statement.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // TODO(shishir): This should move to a more generic name. | 23 // TODO(shishir): This should move to a more generic name. |
| 23 const FilePath::CharType kPredictorDatabaseName[] = | 24 const FilePath::CharType kPredictorDatabaseName[] = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 40 | 41 |
| 41 // Opens the database file from the profile path. Separated from the | 42 // Opens the database file from the profile path. Separated from the |
| 42 // constructor to ease construction/destruction of this object on one thread | 43 // constructor to ease construction/destruction of this object on one thread |
| 43 // but database access on the DB thread. | 44 // but database access on the DB thread. |
| 44 void Initialize(); | 45 void Initialize(); |
| 45 void LogDatabaseStats(); // DB Thread. | 46 void LogDatabaseStats(); // DB Thread. |
| 46 | 47 |
| 47 // Cancels pending DB transactions. Should only be called on the UI thread. | 48 // Cancels pending DB transactions. Should only be called on the UI thread. |
| 48 void SetCancelled(); | 49 void SetCancelled(); |
| 49 | 50 |
| 51 bool is_resource_prefetch_predictor_enabled_; |
| 50 FilePath db_path_; | 52 FilePath db_path_; |
| 51 sql::Connection db_; | 53 sql::Connection db_; |
| 52 | 54 |
| 53 // TODO(shishir): These tables may not need to be refcounted. Maybe move them | 55 // TODO(shishir): These tables may not need to be refcounted. Maybe move them |
| 54 // to using a WeakPtr instead. | 56 // to using a WeakPtr instead. |
| 55 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; | 57 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; |
| 56 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_; | 58 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_; |
| 57 | 59 |
| 58 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal); | 60 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal); |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 | 63 |
| 62 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile) | 64 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile) |
| 63 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)), | 65 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)), |
| 64 autocomplete_table_(new AutocompleteActionPredictorTable()), | 66 autocomplete_table_(new AutocompleteActionPredictorTable()), |
| 65 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) { | 67 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) { |
| 68 is_resource_prefetch_predictor_enabled_ = |
| 69 ResourcePrefetchPredictor::IsEnabled(profile); |
| 66 } | 70 } |
| 67 | 71 |
| 68 PredictorDatabaseInternal::~PredictorDatabaseInternal() { | 72 PredictorDatabaseInternal::~PredictorDatabaseInternal() { |
| 69 } | 73 } |
| 70 | 74 |
| 71 void PredictorDatabaseInternal::Initialize() { | 75 void PredictorDatabaseInternal::Initialize() { |
| 72 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 76 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 73 db_.set_exclusive_locking(); | 77 db_.set_exclusive_locking(); |
| 74 bool success = db_.Open(db_path_); | 78 bool success = db_.Open(db_path_); |
| 75 | 79 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 92 void PredictorDatabaseInternal::LogDatabaseStats() { | 96 void PredictorDatabaseInternal::LogDatabaseStats() { |
| 93 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | 97 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); |
| 94 | 98 |
| 95 int64 db_size; | 99 int64 db_size; |
| 96 bool success = file_util::GetFileSize(db_path_, &db_size); | 100 bool success = file_util::GetFileSize(db_path_, &db_size); |
| 97 DCHECK(success) << "Failed to get file size for " << db_path_.value(); | 101 DCHECK(success) << "Failed to get file size for " << db_path_.value(); |
| 98 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", | 102 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", |
| 99 static_cast<int>(db_size / 1024)); | 103 static_cast<int>(db_size / 1024)); |
| 100 | 104 |
| 101 autocomplete_table_->LogDatabaseStats(); | 105 autocomplete_table_->LogDatabaseStats(); |
| 102 resource_prefetch_tables_->LogDatabaseStats(); | 106 if (is_resource_prefetch_predictor_enabled_) |
| 107 resource_prefetch_tables_->LogDatabaseStats(); |
| 103 } | 108 } |
| 104 | 109 |
| 105 PredictorDatabase::PredictorDatabase(Profile* profile) | 110 PredictorDatabase::PredictorDatabase(Profile* profile) |
| 106 : db_(new PredictorDatabaseInternal(profile)) { | 111 : db_(new PredictorDatabaseInternal(profile)) { |
| 107 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | 112 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, |
| 108 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); | 113 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); |
| 109 } | 114 } |
| 110 | 115 |
| 111 PredictorDatabase::~PredictorDatabase() { | 116 PredictorDatabase::~PredictorDatabase() { |
| 112 } | 117 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 123 scoped_refptr<ResourcePrefetchPredictorTables> | 128 scoped_refptr<ResourcePrefetchPredictorTables> |
| 124 PredictorDatabase::resource_prefetch_tables() { | 129 PredictorDatabase::resource_prefetch_tables() { |
| 125 return db_->resource_prefetch_tables_; | 130 return db_->resource_prefetch_tables_; |
| 126 } | 131 } |
| 127 | 132 |
| 128 sql::Connection* PredictorDatabase::GetDatabase() { | 133 sql::Connection* PredictorDatabase::GetDatabase() { |
| 129 return &db_->db_; | 134 return &db_->db_; |
| 130 } | 135 } |
| 131 | 136 |
| 132 } // namespace predictors | 137 } // namespace predictors |
| OLD | NEW |