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 // TODO (tburkard): initialize logged_in_table_ member. | |
73 ResourcePrefetchPredictorConfig config; | 75 ResourcePrefetchPredictorConfig config; |
74 is_resource_prefetch_predictor_enabled_ = | 76 is_resource_prefetch_predictor_enabled_ = |
75 IsSpeculativeResourcePrefetchingEnabled(profile, &config); | 77 IsSpeculativeResourcePrefetchingEnabled(profile, &config); |
76 } | 78 } |
77 | 79 |
78 PredictorDatabaseInternal::~PredictorDatabaseInternal() { | 80 PredictorDatabaseInternal::~PredictorDatabaseInternal() { |
79 // 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 |
80 // 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. |
81 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release()); | 83 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release()); |
82 } | 84 } |
83 | 85 |
84 void PredictorDatabaseInternal::Initialize() { | 86 void PredictorDatabaseInternal::Initialize() { |
85 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
86 db_->set_exclusive_locking(); | 88 // TODO(tburkard): figure out if we need this. |
| 89 // db_->set_exclusive_locking(); |
87 bool success = db_->Open(db_path_); | 90 bool success = db_->Open(db_path_); |
88 | 91 |
89 if (!success) | 92 if (!success) |
90 return; | 93 return; |
91 | 94 |
92 autocomplete_table_->Initialize(db_.get()); | 95 autocomplete_table_->Initialize(db_.get()); |
| 96 logged_in_table_->Initialize(db_.get()); |
93 resource_prefetch_tables_->Initialize(db_.get()); | 97 resource_prefetch_tables_->Initialize(db_.get()); |
94 | 98 |
95 LogDatabaseStats(); | 99 LogDatabaseStats(); |
96 } | 100 } |
97 | 101 |
98 void PredictorDatabaseInternal::SetCancelled() { | 102 void PredictorDatabaseInternal::SetCancelled() { |
99 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 103 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
100 | 104 |
101 autocomplete_table_->SetCancelled(); | 105 autocomplete_table_->SetCancelled(); |
| 106 logged_in_table_->SetCancelled(); |
102 resource_prefetch_tables_->SetCancelled(); | 107 resource_prefetch_tables_->SetCancelled(); |
103 } | 108 } |
104 | 109 |
105 void PredictorDatabaseInternal::LogDatabaseStats() { | 110 void PredictorDatabaseInternal::LogDatabaseStats() { |
106 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 111 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
107 | 112 |
108 int64 db_size; | 113 int64 db_size; |
109 bool success = file_util::GetFileSize(db_path_, &db_size); | 114 bool success = file_util::GetFileSize(db_path_, &db_size); |
110 DCHECK(success) << "Failed to get file size for " << db_path_.value(); | 115 DCHECK(success) << "Failed to get file size for " << db_path_.value(); |
111 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", | 116 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", |
112 static_cast<int>(db_size / 1024)); | 117 static_cast<int>(db_size / 1024)); |
113 | 118 |
114 autocomplete_table_->LogDatabaseStats(); | 119 autocomplete_table_->LogDatabaseStats(); |
| 120 logged_in_table_->LogDatabaseStats(); |
115 if (is_resource_prefetch_predictor_enabled_) | 121 if (is_resource_prefetch_predictor_enabled_) |
116 resource_prefetch_tables_->LogDatabaseStats(); | 122 resource_prefetch_tables_->LogDatabaseStats(); |
117 } | 123 } |
118 | 124 |
119 PredictorDatabase::PredictorDatabase(Profile* profile) | 125 PredictorDatabase::PredictorDatabase(Profile* profile) |
120 : db_(new PredictorDatabaseInternal(profile)) { | 126 : db_(new PredictorDatabaseInternal(profile)) { |
121 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 127 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
122 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); | 128 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); |
123 } | 129 } |
124 | 130 |
125 PredictorDatabase::~PredictorDatabase() { | 131 PredictorDatabase::~PredictorDatabase() { |
126 } | 132 } |
127 | 133 |
128 void PredictorDatabase::Shutdown() { | 134 void PredictorDatabase::Shutdown() { |
129 db_->SetCancelled(); | 135 db_->SetCancelled(); |
130 } | 136 } |
131 | 137 |
132 scoped_refptr<AutocompleteActionPredictorTable> | 138 scoped_refptr<AutocompleteActionPredictorTable> |
133 PredictorDatabase::autocomplete_table() { | 139 PredictorDatabase::autocomplete_table() { |
134 return db_->autocomplete_table_; | 140 return db_->autocomplete_table_; |
135 } | 141 } |
136 | 142 |
| 143 scoped_refptr<LoggedInPredictorTable> |
| 144 PredictorDatabase::logged_in_table() { |
| 145 return db_->logged_in_table_; |
| 146 } |
| 147 |
137 scoped_refptr<ResourcePrefetchPredictorTables> | 148 scoped_refptr<ResourcePrefetchPredictorTables> |
138 PredictorDatabase::resource_prefetch_tables() { | 149 PredictorDatabase::resource_prefetch_tables() { |
139 return db_->resource_prefetch_tables_; | 150 return db_->resource_prefetch_tables_; |
140 } | 151 } |
141 | 152 |
142 sql::Connection* PredictorDatabase::GetDatabase() { | 153 sql::Connection* PredictorDatabase::GetDatabase() { |
143 return db_->db_.get(); | 154 return db_->db_.get(); |
144 } | 155 } |
145 | 156 |
146 } // namespace predictors | 157 } // namespace predictors |
OLD | NEW |