Chromium Code Reviews| Index: components/history/core/browser/download_database.cc |
| diff --git a/components/history/core/browser/download_database.cc b/components/history/core/browser/download_database.cc |
| index 5994bf5e2564fcf5b8d27d9ef830866cedaa29d0..0bc7dd9d67d240bf8bd954ab4f708b7de5008ece 100644 |
| --- a/components/history/core/browser/download_database.cc |
| +++ b/components/history/core/browser/download_database.cc |
| @@ -66,6 +66,11 @@ base::FilePath ColumnFilePath(sql::Statement& statement, int col) { |
| } // namespace |
| +// static |
| +bool DownloadDatabase::IsParallelDownloadingEnabled() { |
| + return base::FeatureList::IsEnabled(kParallelDownloading); |
| +} |
| + |
| DownloadDatabase::DownloadDatabase( |
| DownloadInterruptReason download_interrupt_reason_none, |
| DownloadInterruptReason download_interrupt_reason_crash) |
| @@ -312,15 +317,21 @@ bool DownloadDatabase::InitDownloadTable() { |
| "url LONGVARCHAR NOT NULL, " // URL. |
| "PRIMARY KEY (id, chain_index) )"; |
| + bool ret; |
| if (GetDB().DoesTableExist("downloads")) { |
| - return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| - EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
| + ret = EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") && |
| + EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0"); |
| } else { |
| // If the "downloads" table doesn't exist, the downloads_url_chain |
| - // table better not. |
| - return (!GetDB().DoesTableExist("downloads_url_chain") && |
| - GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema)); |
| + // and the downloads_jobs table better not. |
| + ret = !GetDB().DoesTableExist("downloads_url_chain") && |
| + !GetDB().DoesTableExist("downloads_jobs") && |
| + GetDB().Execute(kSchema) && GetDB().Execute(kUrlChainSchema); |
| } |
| + if (IsParallelDownloadingEnabled()) |
| + ret = ret && EnsureDownloadJobsTableExists(); |
| + |
| + return ret; |
| } |
| uint32_t DownloadDatabase::GetNextDownloadId() { |
| @@ -473,6 +484,39 @@ void DownloadDatabase::QueryDownloads(std::vector<DownloadRow>* results) { |
| url_chain->push_back(GURL(statement_chain.ColumnString(2))); |
| } |
| + if (IsParallelDownloadingEnabled()) { |
|
asanka
2017/02/01 22:34:10
The DB schema shouldn't depend on a feature state.
qinmin
2017/02/01 23:37:26
removed all feature state checking in this file. S
|
| + sql::Statement statement_download_job(GetDB().GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "SELECT id, job_id, start_position, length, received_bytes, " |
| + "state, interrupt_reason FROM downloads_jobs " |
| + "ORDER BY id, job_id")); |
| + |
| + while (statement_download_job.Step()) { |
| + int column = 0; |
| + // See the comment above about SQLITE lacking unsigned integers. |
| + int64_t signed_id = statement_download_job.ColumnInt64(column++); |
| + if (signed_id <= static_cast<int64_t>(kInvalidDownloadId)) |
| + continue; |
| + int id = IntToDownloadId(signed_id); |
| + DownloadJobInfo info; |
| + info.id = IntToDownloadId(signed_id); |
| + info.job_id = statement_download_job.ColumnInt(column++); |
| + info.start_position = statement_download_job.ColumnInt64(column++); |
| + info.length = statement_download_job.ColumnInt64(column++); |
| + info.received_bytes = statement_download_job.ColumnInt64(column++); |
| + info.state = IntToDownloadState( |
| + statement_download_job.ColumnInt(column++)); |
| + info.interrupt_reason = IntToDownloadInterruptReason( |
| + statement_download_job.ColumnInt(column++)); |
| + // Confirm the id has already been seen--if it hasn't, discard the |
| + // record. |
| + DCHECK(base::ContainsKey(info_map, id)); |
| + if (!base::ContainsKey(info_map, id)) |
| + continue; |
| + info_map[id]->download_job_info.push_back(info); |
| + } |
| + } |
| + |
| for (std::map<uint32_t, DownloadRow*>::iterator it = info_map.begin(); |
| it != info_map.end(); ++it) { |
| DownloadRow* row = it->second; |
| @@ -533,7 +577,17 @@ bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { |
| statement.BindString(column++, data.last_modified); |
| statement.BindInt(column++, DownloadIdToInt(data.id)); |
| - return statement.Run(); |
| + if (!statement.Run()) |
| + return false; |
| + |
| + if (IsParallelDownloadingEnabled()) { |
| + for (size_t i = 0; i < data.download_job_info.size(); ++i) { |
| + if (!UpdateDownloadJob(data.id, data.download_job_info[i], false)) |
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| } |
| void DownloadDatabase::EnsureInProgressEntriesCleanedUp() { |
| @@ -548,6 +602,19 @@ void DownloadDatabase::EnsureInProgressEntriesCleanedUp() { |
| statement.BindInt(2, DownloadStateToInt(DownloadState::IN_PROGRESS)); |
| statement.Run(); |
| + |
| + if (IsParallelDownloadingEnabled()) { |
| + sql::Statement statement_download_job(GetDB().GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "UPDATE downloads_jobs SET state=?, interrupt_reason=? WHERE state=?")); |
| + statement_download_job.BindInt( |
| + 0, DownloadStateToInt(DownloadState::INTERRUPTED)); |
| + statement_download_job.BindInt( |
| + 1, DownloadInterruptReasonToInt(download_interrupt_reason_crash_)); |
| + statement_download_job.BindInt( |
| + 2, DownloadStateToInt(DownloadState::IN_PROGRESS)); |
| + statement_download_job.Run(); |
| + } |
| in_progress_entry_cleanup_completed_ = true; |
| } |
| @@ -650,6 +717,16 @@ bool DownloadDatabase::CreateDownload(const DownloadRow& info) { |
| } |
| statement_insert_chain.Reset(true); |
| } |
| + |
| + if (IsParallelDownloadingEnabled()) { |
| + for (size_t i = 0; i < info.download_job_info.size(); ++i) { |
| + if (!UpdateDownloadJob(info.id, info.download_job_info[i], true)) { |
| + RemoveDownload(info.id); |
| + return false; |
| + } |
| + } |
| + } |
| + |
| return true; |
| } |
| @@ -665,6 +742,9 @@ void DownloadDatabase::RemoveDownload(uint32_t id) { |
| return; |
| } |
| RemoveDownloadURLs(id); |
| + if (IsParallelDownloadingEnabled()) { |
| + RemoveDownloadJobs(id); |
| + } |
| } |
| void DownloadDatabase::RemoveDownloadURLs(uint32_t id) { |
| @@ -686,4 +766,88 @@ size_t DownloadDatabase::CountDownloads() { |
| return statement.ColumnInt(0); |
| } |
| +bool DownloadDatabase::EnsureDownloadJobsTableExists() { |
| + // TODO(qinmin): increment the current history database version by 1 once |
| + // this feature is no longer experimental. |
| + const char kJobsSchema[] = |
| + "CREATE TABLE downloads_jobs (" |
| + "id INTEGER NOT NULL," // downloads.id. |
| + "job_id INTEGER NOT NULL," // Download job id. |
| + "start_position INTEGER NOT NULL," // The start request position of the |
| + // download job. |
| + "length INTEGER NOT NULL," // length of the request, -1 |
| + // if not specified |
| + "received_bytes INTEGER NOT NULL," // Total bytes downloaded. |
| + "state INTEGER NOT NULL," // To be determined. |
| + "interrupt_reason INTEGER NOT NULL, " // DownloadInterruptReason |
| + "PRIMARY KEY (id, job_id) )"; |
| + |
| + return GetDB().DoesTableExist("downloads_jobs") || |
| + GetDB().Execute(kJobsSchema); |
| +} |
| + |
| +bool DownloadDatabase::UpdateDownloadJob( |
| + const DownloadJobInfo& info, bool is_new_download) { |
| + sql::Statement statement_query(GetDB().GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "SELECT count(*) " |
| + "FROM downloads_jobs " |
| + "WHERE id=? AND job_id=?")); |
| + statement_query.BindInt(0, info.id); |
| + statement_query.BindInt(1, info.job_id); |
| + |
| + bool job_exists = false; |
| + if (statement_query.Step()) { |
| + job_exists = statement_query.ColumnInt(0) > 0; |
| + // There should not be any jobs in downloads_jobs for this id. If there |
| + // are, we don't want them to interfere with inserting the correct jobs, |
| + // so just remove them. |
| + if (is_new_download && job_exists) |
| + RemoveDownloadJobs(info.id); |
| + } |
| + int column = 0; |
| + |
| + if (!is_new_download && job_exists) { |
| + sql::Statement statement_update(GetDB().GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "UPDATE downloads_jobs " |
| + "SET start_position=?, length=?, received_bytes=?, state=?, " |
| + "interrupt_reason=? " |
| + "WHERE id=? AND job_id=?")); |
| + statement_update.BindInt64(column++, info.start_position); |
| + statement_update.BindInt64(column++, info.length); |
| + statement_update.BindInt64(column++, info.received_bytes); |
| + statement_update.BindInt(column++, DownloadStateToInt(info.state)); |
| + statement_update.BindInt( |
| + column++, DownloadInterruptReasonToInt(info.interrupt_reason)); |
| + statement_update.BindInt(column++, info.id); |
| + statement_update.BindInt(column++, info.job_id); |
| + return statement_update.Run(); |
| + } else { |
| + sql::Statement statement_insert(GetDB().GetCachedStatement( |
| + SQL_FROM_HERE, |
| + "INSERT INTO downloads_jobs " |
| + "(id, job_id, start_position, length, received_bytes, state, " |
| + "interrupt_reason) " |
| + "VALUES (?, ?, ?, ?, ?, ?, ?)")); |
| + statement_insert.BindInt(column++, info.id); |
| + statement_insert.BindInt(column++, info.job_id); |
| + statement_insert.BindInt64(column++, info.start_position); |
| + statement_insert.BindInt64(column++, info.length); |
| + statement_insert.BindInt64(column++, info.received_bytes); |
| + statement_insert.BindInt(column++, DownloadStateToInt(info.state)); |
| + statement_insert.BindInt( |
| + column++, DownloadInterruptReasonToInt(info.interrupt_reason)); |
| + |
| + return statement_insert.Run(); |
| + } |
| +} |
| + |
| +void DownloadDatabase::RemoveDownloadJobs(uint32_t id) { |
| + sql::Statement statement_delete(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| + "DELETE FROM downloads_jobs WHERE id=?")); |
| + statement_delete.BindInt(0, id); |
| + statement_delete.Run(); |
| +} |
| + |
| } // namespace history |