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

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

Issue 12225093: chrome/browser/predictors componentization part-1 Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 10 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
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_path.h"
9 #include "base/file_util.h" 8 #include "base/file_util.h"
10 #include "base/logging.h" 9 #include "base/logging.h"
11 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
12 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
13 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h" 12 #include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" 13 #include "chrome/browser/predictors/resource_prefetch_predictor.h"
15 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 14 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
16 #include "chrome/browser/prerender/prerender_field_trial.h" 15 #include "chrome/browser/prerender/prerender_field_trial.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
19 #include "sql/connection.h" 17 #include "sql/connection.h"
20 #include "sql/statement.h" 18 #include "sql/statement.h"
21 19
22 using content::BrowserThread; 20 using content::BrowserThread;
23 21
24 namespace { 22 namespace {
25 23
26 // TODO(shishir): This should move to a more generic name. 24 // TODO(shishir): This should move to a more generic name.
27 const FilePath::CharType kPredictorDatabaseName[] = 25 const FilePath::CharType kPredictorDatabaseName[] =
28 FILE_PATH_LITERAL("Network Action Predictor"); 26 FILE_PATH_LITERAL("Network Action Predictor");
29 27
30 } // namespace 28 } // namespace
31 29
32 namespace predictors { 30 namespace predictors {
33 31
34 // Refcounted as it is created, initialized and destroyed on a different thread 32 PredictorDatabase::PredictorDatabase(
35 // to the DB thread that is required for all methods performing database access. 33 const FilePath& path,
36 class PredictorDatabaseInternal 34 bool is_resource_prefetch_predictor_enabled)
37 : public base::RefCountedThreadSafe<PredictorDatabaseInternal> { 35 : db_path_(path.Append(kPredictorDatabaseName)),
38 private: 36 is_resource_prefetch_predictor_enabled_(
39 friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>; 37 is_resource_prefetch_predictor_enabled),
40 friend class PredictorDatabase;
41
42 explicit PredictorDatabaseInternal(Profile* profile);
43 virtual ~PredictorDatabaseInternal();
44
45 // Opens the database file from the profile path. Separated from the
46 // constructor to ease construction/destruction of this object on one thread
47 // but database access on the DB thread.
48 void Initialize();
49 void LogDatabaseStats(); // DB Thread.
50
51 // Cancels pending DB transactions. Should only be called on the UI thread.
52 void SetCancelled();
53
54 bool is_resource_prefetch_predictor_enabled_;
55 FilePath db_path_;
56 scoped_ptr<sql::Connection> db_;
57
58 // TODO(shishir): These tables may not need to be refcounted. Maybe move them
59 // to using a WeakPtr instead.
60 scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
61 scoped_refptr<ResourcePrefetchPredictorTables> resource_prefetch_tables_;
62
63 DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal);
64 };
65
66
67 PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile)
68 : db_path_(profile->GetPath().Append(kPredictorDatabaseName)),
69 db_(new sql::Connection()), 38 db_(new sql::Connection()),
70 autocomplete_table_(new AutocompleteActionPredictorTable()), 39 autocomplete_table_(new AutocompleteActionPredictorTable()),
71 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) { 40 resource_prefetch_tables_(new ResourcePrefetchPredictorTables()) {
72 ResourcePrefetchPredictorConfig config; 41 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
73 is_resource_prefetch_predictor_enabled_ = 42 base::Bind(&PredictorDatabase::Initialize, this));
74 IsSpeculativeResourcePrefetchingEnabled(profile, &config);
75 } 43 }
76 44
77 PredictorDatabaseInternal::~PredictorDatabaseInternal() { 45 PredictorDatabase::~PredictorDatabase() {
78 // The connection pointer needs to be deleted on the DB thread since there 46 // The connection pointer needs to be deleted on the DB thread since there
79 // might be a task in progress on the DB thread which uses this connection. 47 // might be a task in progress on the DB thread which uses this connection.
80 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release()); 48 BrowserThread::DeleteSoon(BrowserThread::DB, FROM_HERE, db_.release());
81 } 49 }
82 50
83 void PredictorDatabaseInternal::Initialize() { 51 scoped_refptr<AutocompleteActionPredictorTable>
52 PredictorDatabase::autocomplete_table() {
53 return autocomplete_table_;
54 }
55
56 scoped_refptr<ResourcePrefetchPredictorTables>
57 PredictorDatabase::resource_prefetch_tables() {
58 return resource_prefetch_tables_;
59 }
60
61 sql::Connection* PredictorDatabase::GetDatabase() {
62 return db_.get();
63 }
64
65 void PredictorDatabase::Initialize() {
84 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 66 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
85 db_->set_exclusive_locking(); 67 db_->set_exclusive_locking();
86 bool success = db_->Open(db_path_); 68 bool success = db_->Open(db_path_);
87 69
88 if (!success) 70 if (!success)
89 return; 71 return;
90 72
91 autocomplete_table_->Initialize(db_.get()); 73 autocomplete_table_->Initialize(db_.get());
92 resource_prefetch_tables_->Initialize(db_.get()); 74 resource_prefetch_tables_->Initialize(db_.get());
93 75
94 LogDatabaseStats(); 76 LogDatabaseStats();
95 } 77 }
96 78
97 void PredictorDatabaseInternal::SetCancelled() { 79 void PredictorDatabase::SetCancelled() {
98 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 80 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
99 81
100 autocomplete_table_->SetCancelled(); 82 autocomplete_table_->SetCancelled();
101 resource_prefetch_tables_->SetCancelled(); 83 resource_prefetch_tables_->SetCancelled();
102 } 84 }
103 85
104 void PredictorDatabaseInternal::LogDatabaseStats() { 86 void PredictorDatabase::LogDatabaseStats() {
105 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
106 88
107 int64 db_size; 89 int64 db_size;
108 bool success = file_util::GetFileSize(db_path_, &db_size); 90 bool success = file_util::GetFileSize(db_path_, &db_size);
109 DCHECK(success) << "Failed to get file size for " << db_path_.value(); 91 DCHECK(success) << "Failed to get file size for " << db_path_.value();
110 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB", 92 UMA_HISTOGRAM_MEMORY_KB("PredictorDatabase.DatabaseSizeKB",
111 static_cast<int>(db_size / 1024)); 93 static_cast<int>(db_size / 1024));
112 94
113 autocomplete_table_->LogDatabaseStats(); 95 autocomplete_table_->LogDatabaseStats();
114 if (is_resource_prefetch_predictor_enabled_) 96 if (is_resource_prefetch_predictor_enabled_)
115 resource_prefetch_tables_->LogDatabaseStats(); 97 resource_prefetch_tables_->LogDatabaseStats();
116 } 98 }
117 99
118 PredictorDatabase::PredictorDatabase(Profile* profile)
119 : db_(new PredictorDatabaseInternal(profile)) {
120 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
121 base::Bind(&PredictorDatabaseInternal::Initialize, db_));
122 }
123
124 PredictorDatabase::~PredictorDatabase() {
125 }
126
127 void PredictorDatabase::Shutdown() {
128 db_->SetCancelled();
129 }
130
131 scoped_refptr<AutocompleteActionPredictorTable>
132 PredictorDatabase::autocomplete_table() {
133 return db_->autocomplete_table_;
134 }
135
136 scoped_refptr<ResourcePrefetchPredictorTables>
137 PredictorDatabase::resource_prefetch_tables() {
138 return db_->resource_prefetch_tables_;
139 }
140
141 sql::Connection* PredictorDatabase::GetDatabase() {
142 return db_->db_.get();
143 }
144
145 } // namespace predictors 100 } // 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