summaryrefslogtreecommitdiffstats
path: root/chrome/browser/history
diff options
context:
space:
mode:
authorbenjhayden@chromium.org <benjhayden@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-07 21:09:12 +0000
committerbenjhayden@chromium.org <benjhayden@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-07 21:09:12 +0000
commit5d051ea84671aa5092500360d5ab619e108ebadd (patch)
treeb9a39fd5d583b9a10b1472932298fcd58e934754 /chrome/browser/history
parent7179d2f456e41eca799a6c2070fca9ad123e0b65 (diff)
downloadchromium_src-5d051ea84671aa5092500360d5ab619e108ebadd.zip
chromium_src-5d051ea84671aa5092500360d5ab619e108ebadd.tar.gz
chromium_src-5d051ea84671aa5092500360d5ab619e108ebadd.tar.bz2
Make a new integer field in sql::MetaTable (a per-profile db) containing a counter for the next download id, so that this id is unique across sessions. This id will allow us to merge download id with db_handle and merge most/all of the maps in DownloadManager in future CLs.
(Retry of r98656 again. That CL included <iostream> in a "promiscuous" header file, dramatically increasing the number of static initializers, and was reverted. This is the same CL, but includes <iosfwd> instead of <iostream>.) Make DownloadManager read this field to initialize its next_id_ counter in Init(). Put a fine-grained mutex in DownloadManager::GetNextId() so that it can be called directly from any thread. Define a thunk wrapping DM::GNI() to be passed around between threads to guard against other threads calling any other DM methods. This thunk owns a scoped_refptr<DM> to manage life-time issues. This pattern is implemented for DM elsewhere. Store this thunk in ResourceContext to be called by ResourceDispatchHost/DownloadThrottlingResourceHandler on the IO thread. Pass the returned DownloadId into DownloadResourceHandler. The alternative way to obtain ids on the IO thread is to jump over to the UI thread and back. This way would add significant latency to a critical path. GetNextId() should be fast and easily accessible from any thread. Now that ids are per-profile, define a class DownloadId containing a per-profile id and an indication of which profile, currently the DownloadManager*. DownloadIds are hashable, comparable, globally unique, not persistent, and are used by DownloadFileManager. When the download is added to the history, MetaTable.next_download_id will be set to the new download's id +1 if that number is greater than MT.next_download_id. Increasing this counter at the same time as the download is added to the db prevents the counter from desyncing from the db, which was the primary concern re storing the counter in the BrowserPrefs. Review URL: http://codereview.chromium.org/7776012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@100017 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/history')
-rw-r--r--chrome/browser/history/download_database.cc17
-rw-r--r--chrome/browser/history/download_database.h6
-rw-r--r--chrome/browser/history/history.cc7
-rw-r--r--chrome/browser/history/history.h7
-rw-r--r--chrome/browser/history/history_backend.cc11
-rw-r--r--chrome/browser/history/history_backend.h1
-rw-r--r--chrome/browser/history/history_marshaling.h5
7 files changed, 51 insertions, 3 deletions
diff --git a/chrome/browser/history/download_database.cc b/chrome/browser/history/download_database.cc
index 906ac83..ccaed51 100644
--- a/chrome/browser/history/download_database.cc
+++ b/chrome/browser/history/download_database.cc
@@ -52,9 +52,14 @@ FilePath ColumnFilePath(sql::Statement& statement, int col) {
#endif
+// Key in the meta_table containing the next id to use for a new download in
+// this profile.
+static const char kNextDownloadId[] = "next_download_id";
+
} // namespace
-DownloadDatabase::DownloadDatabase() {
+DownloadDatabase::DownloadDatabase()
+ : next_id_(0) {
}
DownloadDatabase::~DownloadDatabase() {
@@ -73,6 +78,8 @@ bool DownloadDatabase::InitDownloadTable() {
"state INTEGER NOT NULL)"))
return false;
}
+ meta_table_.Init(&GetDB(), 0, 0);
+ meta_table_.GetValue(kNextDownloadId, &next_id_);
return true;
}
@@ -161,8 +168,12 @@ int64 DownloadDatabase::CreateDownload(
statement.BindInt64(4, info.total_bytes);
statement.BindInt(5, info.state);
- if (statement.Run())
- return GetDB().GetLastInsertRowId();
+ if (statement.Run()) {
+ int64 db_handle = GetDB().GetLastInsertRowId();
+ // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;}
+ meta_table_.SetValue(kNextDownloadId, ++next_id_);
+ return db_handle;
+ }
return 0;
}
diff --git a/chrome/browser/history/download_database.h b/chrome/browser/history/download_database.h
index c77500c..8f97e55 100644
--- a/chrome/browser/history/download_database.h
+++ b/chrome/browser/history/download_database.h
@@ -7,6 +7,7 @@
#pragma once
#include "chrome/browser/history/history_types.h"
+#include "sql/meta_table.h"
struct DownloadPersistentStoreInfo;
class FilePath;
@@ -24,6 +25,8 @@ class DownloadDatabase {
DownloadDatabase();
virtual ~DownloadDatabase();
+ int next_download_id() const { return next_id_; }
+
// Get all the downloads from the database.
void QueryDownloads(std::vector<DownloadPersistentStoreInfo>* results);
@@ -63,6 +66,9 @@ class DownloadDatabase {
bool DropDownloadTable();
private:
+ int next_id_;
+ sql::MetaTable meta_table_;
+
DISALLOW_COPY_AND_ASSIGN(DownloadDatabase);
};
diff --git a/chrome/browser/history/history.cc b/chrome/browser/history/history.cc
index cdab352..6b62b89 100644
--- a/chrome/browser/history/history.cc
+++ b/chrome/browser/history/history.cc
@@ -511,6 +511,13 @@ HistoryService::Handle HistoryService::CreateDownload(
create_info);
}
+HistoryService::Handle HistoryService::GetNextDownloadId(
+ CancelableRequestConsumerBase* consumer,
+ DownloadNextIdCallback* callback) {
+ return Schedule(PRIORITY_NORMAL, &HistoryBackend::GetNextDownloadId, consumer,
+ new history::DownloadNextIdRequest(callback));
+}
+
// Handle queries for a list of all downloads in the history database's
// 'downloads' table.
HistoryService::Handle HistoryService::QueryDownloads(
diff --git a/chrome/browser/history/history.h b/chrome/browser/history/history.h
index 120d764..0a09a6a 100644
--- a/chrome/browser/history/history.h
+++ b/chrome/browser/history/history.h
@@ -423,6 +423,13 @@ class HistoryService : public CancelableRequestProvider,
CancelableRequestConsumerBase* consumer,
DownloadCreateCallback* callback);
+ // Implemented by the caller of 'GetNextDownloadId' below.
+ typedef Callback1<int/*next_download_id*/>::Type DownloadNextIdCallback;
+
+ // Runs the callback with the next available download id.
+ Handle GetNextDownloadId(CancelableRequestConsumerBase* consumer,
+ DownloadNextIdCallback* callback);
+
// Implemented by the caller of 'QueryDownloads' below, and is called when the
// history service has retrieved a list of all download state. The call
typedef Callback1<std::vector<DownloadPersistentStoreInfo>*>::Type
diff --git a/chrome/browser/history/history_backend.cc b/chrome/browser/history/history_backend.cc
index f1ff383..4ca4e0f 100644
--- a/chrome/browser/history/history_backend.cc
+++ b/chrome/browser/history/history_backend.cc
@@ -1076,6 +1076,17 @@ void HistoryBackend::GetMostRecentKeywordSearchTerms(
// Downloads -------------------------------------------------------------------
+void HistoryBackend::GetNextDownloadId(
+ scoped_refptr<DownloadNextIdRequest> request) {
+ if (request->canceled()) return;
+ if (db_.get()) {
+ request->value = db_->next_download_id();
+ } else {
+ request->value = 0;
+ }
+ request->ForwardResult(DownloadNextIdRequest::TupleType(request->value));
+}
+
// Get all the download entries from the database.
void HistoryBackend::QueryDownloads(
scoped_refptr<DownloadQueryRequest> request) {
diff --git a/chrome/browser/history/history_backend.h b/chrome/browser/history/history_backend.h
index a7c7317..c9c3096 100644
--- a/chrome/browser/history/history_backend.h
+++ b/chrome/browser/history/history_backend.h
@@ -236,6 +236,7 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
// Downloads -----------------------------------------------------------------
+ void GetNextDownloadId(scoped_refptr<DownloadNextIdRequest> request);
void QueryDownloads(scoped_refptr<DownloadQueryRequest> request);
void CleanUpInProgressEntries();
void UpdateDownload(int64 received_bytes, int32 state, int64 db_handle);
diff --git a/chrome/browser/history/history_marshaling.h b/chrome/browser/history/history_marshaling.h
index f0f1073..845ea9c 100644
--- a/chrome/browser/history/history_marshaling.h
+++ b/chrome/browser/history/history_marshaling.h
@@ -55,6 +55,11 @@ typedef CancelableRequest<FaviconService::FaviconDataCallback>
// Downloads ------------------------------------------------------------------
+typedef CancelableRequest1<HistoryService::DownloadNextIdCallback,
+ int/*next_id*/>
+ DownloadNextIdRequest;
+
+
typedef CancelableRequest1<HistoryService::DownloadQueryCallback,
std::vector<DownloadPersistentStoreInfo> >
DownloadQueryRequest;