| 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_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.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/logged_in_predictor_table.h" |
| 14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 15 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 16 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 16 #include "chrome/browser/prerender/prerender_field_trial.h" | 17 #include "chrome/browser/prerender/prerender_field_trial.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "sql/connection.h" | 20 #include "sql/connection.h" |
| 20 #include "sql/statement.h" | 21 #include "sql/statement.h" |
| 21 | 22 |
| 22 using content::BrowserThread; | 23 using content::BrowserThread; |
| 23 | 24 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 51 // Cancels pending DB transactions. Should only be called on the UI thread. | 52 // Cancels pending DB transactions. Should only be called on the UI thread. |
| 52 void SetCancelled(); | 53 void SetCancelled(); |
| 53 | 54 |
| 54 bool is_resource_prefetch_predictor_enabled_; | 55 bool is_resource_prefetch_predictor_enabled_; |
| 55 base::FilePath db_path_; | 56 base::FilePath db_path_; |
| 56 scoped_ptr<sql::Connection> db_; | 57 scoped_ptr<sql::Connection> db_; |
| 57 | 58 |
| 58 // TODO(shishir): These tables may not need to be refcounted. Maybe move them | 59 // TODO(shishir): These tables may not need to be refcounted. Maybe move them |
| 59 // to using a WeakPtr instead. | 60 // to using a WeakPtr instead. |
| 60 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; | 61 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; |
| 62 scoped_refptr<LoggedInPredictorTable> logged_in_table_; |
| 61 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_; | 63 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_; |
| 62 | 64 |
| 63 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal); | 65 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal); |
| 64 }; | 66 }; |
| 65 | 67 |
| 66 | 68 |
| 67 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile) | 69 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile) |
| 68 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)), | 70 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)), |
| 69 db_(new sql::Connection()), | 71 db_(new sql::Connection()), |
| 70 autocomplete_table_(new AutocompleteActionPredictorTable()), | 72 autocomplete_table_(new AutocompleteActionPredictorTable()), |
| 73 logged_in_table_(new LoggedInPredictorTable()), |
| 71 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) { | 74 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) { |
| 72 ResourcePrefetchPredictorConfig config; | 75 ResourcePrefetchPredictorConfig config; |
| 73 is_resource_prefetch_predictor_enabled_ = | 76 is_resource_prefetch_predictor_enabled_ = |
| 74 IsSpeculativeResourcePrefetchingEnabled(profile, &config); | 77 IsSpeculativeResourcePrefetchingEnabled(profile, &config); |
| 75 } | 78 } |
| 76 | 79 |
| 77 PredictorDatabaseInternal::~PredictorDatabaseInternal() { | 80 PredictorDatabaseInternal::~PredictorDatabaseInternal() { |
| 78 // The connection pointer needs to be deleted on the DB thread since there | 81 // The connection pointer needs to be deleted on the DB thread since there |
| 79 // might be a task in progress on the DB thread which uses this connection. | 82 // might be a task in progress on the DB thread which uses this connection. |
| 80 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release()); | 83 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release()); |
| 81 } | 84 } |
| 82 | 85 |
| 83 void PredictorDatabaseInternal::Initialize() { | 86 void PredictorDatabaseInternal::Initialize() { |
| 84 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 85 db_->set_exclusive_locking(); | 88 db_->set_exclusive_locking(); |
| 86 bool success = db_->Open(db_path_); | 89 bool success = db_->Open(db_path_); |
| 87 | 90 |
| 88 if (!success) | 91 if (!success) |
| 89 return; | 92 return; |
| 90 | 93 |
| 91 autocomplete_table_->Initialize(db_.get()); | 94 autocomplete_table_->Initialize(db_.get()); |
| 95 logged_in_table_->Initialize(db_.get()); |
| 92 resource_prefetch_tables_->Initialize(db_.get()); | 96 resource_prefetch_tables_->Initialize(db_.get()); |
| 93 | 97 |
| 94 LogDatabaseStats(); | 98 LogDatabaseStats(); |
| 95 } | 99 } |
| 96 | 100 |
| 97 void PredictorDatabaseInternal::SetCancelled() { | 101 void PredictorDatabaseInternal::SetCancelled() { |
| 98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 | 103 |
| 100 autocomplete_table_->SetCancelled(); | 104 autocomplete_table_->SetCancelled(); |
| 105 logged_in_table_->SetCancelled(); |
| 101 resource_prefetch_tables_->SetCancelled(); | 106 resource_prefetch_tables_->SetCancelled(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 void PredictorDatabaseInternal::LogDatabaseStats() { | 109 void PredictorDatabaseInternal::LogDatabaseStats() { |
| 105 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 110 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 106 | 111 |
| 107 int64 db_size; | 112 int64 db_size; |
| 108 bool success = file_util::GetFileSize(db_path_, &db_size); | 113 bool success = file_util::GetFileSize(db_path_, &db_size); |
| 109 DCHECK(success) << "Failed to get file size for " << db_path_.value(); | 114 DCHECK(success) << "Failed to get file size for " << db_path_.value(); |
| 110 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", | 115 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", |
| 111 static_cast<int>(db_size / 1024)); | 116 static_cast<int>(db_size / 1024)); |
| 112 | 117 |
| 113 autocomplete_table_->LogDatabaseStats(); | 118 autocomplete_table_->LogDatabaseStats(); |
| 119 logged_in_table_->LogDatabaseStats(); |
| 114 if (is_resource_prefetch_predictor_enabled_) | 120 if (is_resource_prefetch_predictor_enabled_) |
| 115 resource_prefetch_tables_->LogDatabaseStats(); | 121 resource_prefetch_tables_->LogDatabaseStats(); |
| 116 } | 122 } |
| 117 | 123 |
| 118 PredictorDatabase::PredictorDatabase(Profile* profile) | 124 PredictorDatabase::PredictorDatabase(Profile* profile) |
| 119 : db_(new PredictorDatabaseInternal(profile)) { | 125 : db_(new PredictorDatabaseInternal(profile)) { |
| 120 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 126 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 121 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); | 127 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); |
| 122 } | 128 } |
| 123 | 129 |
| 124 PredictorDatabase::~PredictorDatabase() { | 130 PredictorDatabase::~PredictorDatabase() { |
| 125 } | 131 } |
| 126 | 132 |
| 127 void PredictorDatabase::Shutdown() { | 133 void PredictorDatabase::Shutdown() { |
| 128 db_->SetCancelled(); | 134 db_->SetCancelled(); |
| 129 } | 135 } |
| 130 | 136 |
| 131 scoped_refptr<AutocompleteActionPredictorTable> | 137 scoped_refptr<AutocompleteActionPredictorTable> |
| 132 PredictorDatabase::autocomplete_table() { | 138 PredictorDatabase::autocomplete_table() { |
| 133 return db_->autocomplete_table_; | 139 return db_->autocomplete_table_; |
| 134 } | 140 } |
| 135 | 141 |
| 142 scoped_refptr<LoggedInPredictorTable> |
| 143 PredictorDatabase::logged_in_table() { |
| 144 return db_->logged_in_table_; |
| 145 } |
| 146 |
| 136 scoped_refptr<ResourcePrefetchPredictorTables> | 147 scoped_refptr<ResourcePrefetchPredictorTables> |
| 137 PredictorDatabase::resource_prefetch_tables() { | 148 PredictorDatabase::resource_prefetch_tables() { |
| 138 return db_->resource_prefetch_tables_; | 149 return db_->resource_prefetch_tables_; |
| 139 } | 150 } |
| 140 | 151 |
| 141 sql::Connection* PredictorDatabase::GetDatabase() { | 152 sql::Connection* PredictorDatabase::GetDatabase() { |
| 142 return db_->db_.get(); | 153 return db_->db_.get(); |
| 143 } | 154 } |
| 144 | 155 |
| 145 } // namespace predictors | 156 } // namespace predictors |
| OLD | NEW |