Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: chrome/browser/predictors/predictor_database.cc

Issue 10380041: Refactoring AutocompleteActionPredictor Database. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Syncing to HEAD Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // TODO(shishir): This should move to a more generic name.
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
49 FilePath db_path_;
50 sql::Connection db_;
51 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
52
53 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal);
54 };
55
56
57 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile)
58 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)),
59 autocomplete_table_(new AutocompleteActionPredictorTable()) {
60 }
61
62 PredictorDatabaseInternal::~PredictorDatabaseInternal() {
63 }
64
65 void PredictorDatabaseInternal::Initialize() {
66 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB));
67
68 db_.set_exclusive_locking();
69 bool success = db_.Open(db_path_);
70
71 if (!success)
72 return;
73
74 autocomplete_table_->Initialize(&db_);
75
76 LogDatabaseStats();
77 }
78
79 void PredictorDatabaseInternal::SetCancelled() {
80 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
81
82 autocomplete_table_->cancelled_.Set();
83 }
84
85 void PredictorDatabaseInternal::LogDatabaseStats() {
86 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB));
87
88 int64 db_size;
89 bool success = file_util::GetFileSize(db_path_, &db_size);
90 DCHECK(success) << "Failed to get file size for " << db_path_.value();
91 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB",
92 static_cast<int>(db_size / 1024));
93
94 autocomplete_table_->LogDatabaseStats();
95 }
96
97
98 PredictorDatabase::PredictorDatabase(Profile* profile)
99 : db_(new PredictorDatabaseInternal(profile)) {
100 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
101 base::Bind(&PredictorDatabaseInternal::Initialize, db_));
102 }
103
104 PredictorDatabase::~PredictorDatabase() {
105 }
106
107 void PredictorDatabase::Shutdown() {
108 db_->SetCancelled();
109 }
110
111 scoped_refptr<AutocompleteActionPredictorTable>
112 PredictorDatabase::autocomplete_table() {
113 return db_->autocomplete_table_;
114 }
115
116 sql::Connection* PredictorDatabase::GetDatabase() {
117 return &db_->db_;
118 }
119
120 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/predictor_database.h ('k') | chrome/browser/predictors/predictor_database_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698