OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/predictors/predictor_database.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | |
10 #include "base/logging.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/metrics/histogram.h" | |
13 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "sql/connection.h" | |
17 #include "sql/statement.h" | |
18 | |
19 namespace { | |
20 | |
21 // FIXME: This should move to a more generic name. | |
dominich
2012/05/08 20:35:28
TODO(shishir) instead?
Shishir
2012/05/08 21:49:03
Done.
| |
22 const FilePath::CharType kPredictorDatabaseName[] = | |
23 FILE_PATH_LITERAL("Network Action Predictor"); | |
24 | |
25 } // namespace | |
26 | |
27 namespace predictors { | |
28 | |
29 // Refcounted as it is created, initialized and destroyed on a different thread | |
30 // to the DB thread that is required for all methods performing database access. | |
31 class PredictorDatabaseInternal | |
32 : public base::RefCountedThreadSafe<PredictorDatabaseInternal> { | |
33 private: | |
34 friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>; | |
35 friend class PredictorDatabase; | |
36 | |
37 explicit PredictorDatabaseInternal(Profile* profile); | |
38 virtual ~PredictorDatabaseInternal(); | |
39 | |
40 // Opens the database file from the profile path. Separated from the | |
41 // constructor to ease construction/destruction of this object on one thread | |
42 // but database access on the DB thread. | |
43 void Initialize(); | |
44 void LogDatabaseStats(); // DB Thread. | |
45 | |
46 // Cancels pending DB transactions. Should only be called on the UI thread. | |
47 void SetCancelled(); | |
48 | |
dominich
2012/05/08 20:35:28
remove extra line
Shishir
2012/05/08 21:49:03
Done.
| |
49 | |
50 FilePath db_path_; | |
51 sql::Connection db_; | |
52 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal); | |
55 }; | |
56 | |
57 | |
58 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile) | |
59 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)), | |
60 autocomplete_table_(new AutocompleteActionPredictorTable()) { | |
61 } | |
62 | |
63 PredictorDatabaseInternal::~PredictorDatabaseInternal() { | |
64 } | |
65 | |
66 void PredictorDatabaseInternal::Initialize() { | |
67 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
68 | |
69 db_.set_exclusive_locking(); | |
70 bool success = db_.Open(db_path_); | |
71 | |
72 if (!success) | |
73 return; | |
74 | |
75 autocomplete_table_->Initialize(&db_); | |
76 | |
77 LogDatabaseStats(); | |
78 } | |
79 | |
80 void PredictorDatabaseInternal::SetCancelled() { | |
81 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
82 | |
83 autocomplete_table_->cancelled_.Set(); | |
84 } | |
85 | |
86 void PredictorDatabaseInternal::LogDatabaseStats() { | |
87 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB)); | |
88 | |
89 int64 db_size; | |
90 bool success = file_util::GetFileSize(db_path_, &db_size); | |
91 DCHECK(success) << "Failed to get file size for " << db_path_.value(); | |
92 UMA_HISTOGRAM_MEMORY_KB("Predictor.DatabaseSizeKB", | |
93 static_cast<int>(db_size / 1024)); | |
94 | |
95 autocomplete_table_->LogDatabaseStats(); | |
96 } | |
97 | |
98 | |
99 PredictorDatabase::PredictorDatabase(Profile* profile) | |
100 : db_(new PredictorDatabaseInternal(profile)) { | |
101 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, | |
dominich
2012/05/08 20:35:28
what stops users getting access to the table befor
Shishir
2012/05/08 21:49:03
Even if they have access to the table, the underly
dominich
2012/05/08 22:49:39
Fine.
| |
102 base::Bind(&PredictorDatabaseInternal::Initialize, db_)); | |
103 } | |
104 | |
105 PredictorDatabase::~PredictorDatabase() { | |
106 } | |
107 | |
108 void PredictorDatabase::Shutdown() { | |
109 db_->SetCancelled(); | |
110 } | |
111 | |
112 AutocompleteActionPredictorTable* PredictorDatabase::autocomplete_table() { | |
113 return db_->autocomplete_table_.get(); | |
114 } | |
115 | |
116 sql::Connection* PredictorDatabase::GetDatabase() { | |
117 return &db_->db_; | |
118 } | |
119 | |
120 } // namespace predictors | |
OLD | NEW |