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