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