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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/predictors/predictor_database.cc
diff --git a/chrome/browser/predictors/predictor_database.cc b/chrome/browser/predictors/predictor_database.cc
new file mode 100644
index 0000000000000000000000000000000000000000..49ca4b6c8d41b9f4adf0db04e9c10f4ea876d2a1
--- /dev/null
+++ b/chrome/browser/predictors/predictor_database.cc
@@ -0,0 +1,122 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/predictors/predictor_database.h"
+
+#include "base/bind.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/stringprintf.h"
+#include "base/metrics/histogram.h"
+#include "chrome/browser/predictors/autocomplete_action_predictor_table.h"
+#include "chrome/browser/profiles/profile.h"
+#include "content/public/browser/browser_thread.h"
+#include "sql/connection.h"
+#include "sql/statement.h"
+
+namespace {
+
+const FilePath::CharType kPredictorDatabaseName[] =
+ FILE_PATH_LITERAL("Network Action Predictor");
+
+} // namespace
+
+namespace predictors {
+
+// Refcounted as it is created, initialized and destroyed on a different thread
+// to the DB thread that is required for all methods performing database access.
+class PredictorDatabaseInternal
+ : public base::RefCountedThreadSafe<PredictorDatabaseInternal> {
+ private:
+ friend class base::RefCountedThreadSafe<PredictorDatabaseInternal>;
+ friend class PredictorDatabase;
+
+ explicit PredictorDatabaseInternal(Profile* profile);
+ virtual ~PredictorDatabaseInternal();
+
+ // Opens the database file from the profile path. Separated from the
+ // constructor to ease construction/destruction of this object on one thread
+ // but database access on the DB thread.
+ void Initialize();
+ void LogDatabaseStats(); // DB Thread.
+
+ // Cancels pending DB transactions. Should only be called on the UI thread.
+ void SetCancelled();
+
+
+ FilePath db_path_;
+ sql::Connection db_;
+ scoped_refptr<AutocompleteActionPredictorTable> autocomplete_table_;
+
+ DISALLOW_COPY_AND_ASSIGN(PredictorDatabaseInternal);
+};
+
+
+PredictorDatabaseInternal::PredictorDatabaseInternal(Profile* profile)
+ : db_path_(profile->GetPath().Append(kPredictorDatabaseName)),
+ autocomplete_table_(new AutocompleteActionPredictorTable()) {
+}
+
+PredictorDatabaseInternal::~PredictorDatabaseInternal() {
+}
+
+void PredictorDatabaseInternal::Initialize() {
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB));
+
+ db_.set_exclusive_locking();
+ bool success = db_.Open(db_path_);
+
+ if (!success) {
+ LOG(ERROR) << "Could not open predictor database @ "
+ << 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.
+ return;
+ }
+
+ autocomplete_table_->Initialize(&db_);
+
+ LogDatabaseStats();
+}
+
+void PredictorDatabaseInternal::SetCancelled() {
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ autocomplete_table_->cancelled_.Set();
+}
+
+void PredictorDatabaseInternal::LogDatabaseStats() {
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::DB));
+
+ int64 db_size;
+ bool success = file_util::GetFileSize(db_path_, &db_size);
+ DCHECK(success) << "Failed to get file size for " << db_path_.value();
+ UMA_HISTOGRAM_MEMORY_KB("Predictor.DatabaseSizeKB",
+ static_cast<int>(db_size / 1024));
+
+ autocomplete_table_->LogDatabaseStats();
+}
+
+
+PredictorDatabase::PredictorDatabase(Profile* profile)
+ : db_(new PredictorDatabaseInternal(profile)) {
+ content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
+ base::Bind(&PredictorDatabaseInternal::Initialize, db_));
+}
+
+PredictorDatabase::~PredictorDatabase() {
+}
+
+void PredictorDatabase::Shutdown() {
+ db_->SetCancelled();
+}
+
+AutocompleteActionPredictorTable* PredictorDatabase::autocomplete_table() {
+ return db_->autocomplete_table_.get();
+}
+
+sql::Connection* PredictorDatabase::GetDatabase() {
+ return &db_->db_;
+}
+
+} // namespace predictors

Powered by Google App Engine
This is Rietveld 408576698