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

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: Changing DB to PKS from RefcountedPKS and addressing 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 LOG(ERROR) << "Could not open predictor database @ "
73 << db_path_.AsUTF8Unsafe();
Scott Hess - ex-Googler 2012/03/15 00:13:33 Is this likely to be more helpful than the default
Shishir 2012/03/15 07:55:48 Only helpful in the release version. Removed.
74 return;
75 }
76
77 autocomplete_table_->Initialize(&db_);
78
79 LogDatabaseStats();
80 }
81
82 void PredictorDatabaseInternal::SetCancelled() {
83 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
84
85 autocomplete_table_->cancelled_.Set();
86 }
87
88 void PredictorDatabaseInternal::LogDatabaseStats() {
89 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB));
90
91 int64 db_size;
92 bool success = file_util::GetFileSize(db_path_, &db_size);
93 DCHECK(success) << "Failed to get file size for " << db_path_.value();
94 UMA_HISTOGRAM_MEMORY_KB("Predictor.DatabaseSizeKB",
95 static_cast<int>(db_size / 1024));
96
97 autocomplete_table_->LogDatabaseStats();
98 }
99
100
101 PredictorDatabase::PredictorDatabase(Profile* profile)
102 : db_(new PredictorDatabaseInternal(profile)) {
103 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
104 base::Bind(&PredictorDatabaseInternal::Initialize, db_));
105 }
106
107 PredictorDatabase::~PredictorDatabase() {
108 }
109
110 void PredictorDatabase::Shutdown() {
111 db_->SetCancelled();
112 }
113
114 AutocompleteActionPredictorTable* PredictorDatabase::autocomplete_table() {
115 return db_->autocomplete_table_.get();
116 }
117
118 sql::Connection* PredictorDatabase::GetDatabase() {
119 return &db_->db_;
120 }
121
122 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698