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

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

Issue 9610006: Refactoring, moving and renaming the NetworkActionPredictor. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Addressing shess's comments. Created 8 years, 9 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 const FilePath::CharType kPredictorDatabaseName[] =
22 FILE_PATH_LITERAL("Network Action Predictor");
23
24 } // namespace
25
26 namespace predictors {
27
28 // Refcounted as it is created, initialized and destroyed on a different thread
29 // to the DB thread that is required for all methods performing database access.
30 class PredictorDatabaseInternal
31 : public base::RefCountedThreadSafe<PredictorDatabaseInternal> {
32 private:
33 friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>;
34 friend class PredictorDatabase;
35
36 explicit PredictorDatabaseInternal(Profile* profile);
37 virtual ~PredictorDatabaseInternal();
38
39 // Opens the database file from the profile path. Separated from the
40 // constructor to ease construction/destruction of this object on one thread
41 // but database access on the DB thread.
42 void Initialize();
43 void LogDatabaseStats(); // DB Thread.
44
45 // Cancels pending DB transactions. Should only be called on the UI thread.
46 void SetCancelled();
47
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("Predictor.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 AutocompleteActionPredictorTable* PredictorDatabase::autocomplete_table() {
112 return db_->autocomplete_table_.get();
113 }
114
115 sql::Connection* PredictorDatabase::GetDatabase() {
116 return &db_->db_;
117 }
118
119 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698