Skip to content

Commit

Permalink
Add mime type information to the download database
Browse files Browse the repository at this point in the history
BUG=328382

Review URL: https://codereview.chromium.org/319703002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276775 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
ohrn@opera.com committed Jun 12, 2014
1 parent 743530f commit 0e64856
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 46 deletions.
7 changes: 6 additions & 1 deletion chrome/browser/download/download_history.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ history::DownloadRow GetDownloadRow(
item->GetTargetFilePath(),
item->GetUrlChain(),
item->GetReferrerUrl(),
item->GetMimeType(),
item->GetOriginalMimeType(),
item->GetStartTime(),
item->GetEndTime(),
item->GetETag(),
Expand All @@ -145,7 +147,8 @@ history::DownloadRow GetDownloadRow(

bool ShouldUpdateHistory(const history::DownloadRow* previous,
const history::DownloadRow& current) {
// Ignore url, referrer, start_time, id, which don't change.
// Ignore url, referrer, mime_type, original_mime_type, start_time,
// id, db_handle, which don't change.
return ((previous == NULL) ||
(previous->current_path != current.current_path) ||
(previous->target_path != current.target_path) ||
Expand Down Expand Up @@ -262,6 +265,8 @@ void DownloadHistory::QueryCallback(scoped_ptr<InfoVector> infos) {
it->target_path,
it->url_chain,
it->referrer_url,
it->mime_type,
it->original_mime_type,
it->start_time,
it->end_time,
it->etag,
Expand Down
16 changes: 13 additions & 3 deletions chrome/browser/download/download_history_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <set>
#include <vector>

#include "base/memory/weak_ptr.h"
Expand Down Expand Up @@ -43,6 +42,8 @@ void CheckInfoEqual(const history::DownloadRow& left,
EXPECT_EQ(left.url_chain[i].spec(), right.url_chain[i].spec());
}
EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec());
EXPECT_EQ(left.mime_type, right.mime_type);
EXPECT_EQ(left.original_mime_type, right.original_mime_type);
EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT());
EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT());
EXPECT_EQ(left.etag, right.etag);
Expand Down Expand Up @@ -238,6 +239,8 @@ class DownloadHistoryTest : public testing::Test {
infos->at(index).target_path,
infos->at(index).url_chain,
infos->at(index).referrer_url,
infos->at(index).mime_type,
infos->at(index).original_mime_type,
infos->at(index).start_time,
infos->at(index).end_time,
infos->at(index).etag,
Expand Down Expand Up @@ -351,6 +354,8 @@ class DownloadHistoryTest : public testing::Test {
base::FilePath(path),
url_chain,
referrer,
"application/octet-stream",
"application/octet-stream",
(base::Time::Now() - base::TimeDelta::FromMinutes(10)),
(base::Time::Now() - base::TimeDelta::FromMinutes(1)),
"Etag",
Expand All @@ -372,6 +377,8 @@ class DownloadHistoryTest : public testing::Test {
const base::FilePath& target_path,
const std::vector<GURL>& url_chain,
const GURL& referrer,
const std::string& mime_type,
const std::string& original_mime_type,
const base::Time& start_time,
const base::Time& end_time,
const std::string& etag,
Expand All @@ -395,6 +402,8 @@ class DownloadHistoryTest : public testing::Test {
info->target_path = target_path;
info->url_chain = url_chain;
info->referrer_url = referrer;
info->mime_type = mime_type;
info->original_mime_type = original_mime_type;
info->start_time = start_time;
info->end_time = end_time;
info->etag = etag;
Expand All @@ -419,8 +428,9 @@ class DownloadHistoryTest : public testing::Test {
.WillRepeatedly(ReturnRefOfCopy(url_chain[0]));
EXPECT_CALL(item(index), GetUrlChain())
.WillRepeatedly(ReturnRefOfCopy(url_chain));
EXPECT_CALL(item(index), GetMimeType())
.WillRepeatedly(Return("application/octet-stream"));
EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return(mime_type));
EXPECT_CALL(item(index), GetOriginalMimeType()).WillRepeatedly(Return(
original_mime_type));
EXPECT_CALL(item(index), GetReferrerUrl())
.WillRepeatedly(ReturnRefOfCopy(referrer));
EXPECT_CALL(item(index), GetStartTime()).WillRepeatedly(Return(start_time));
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/download/download_ui_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ TEST_F(DownloadUIControllerTest, DownloadUIController_HistoryDownload) {
GURL url;
scoped_ptr<MockDownloadItem> item = CreateMockInProgressDownload();

EXPECT_CALL(*item, GetOriginalMimeType());
EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval());

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ class DownloadExtensionTest : public ExtensionApiTest {
downloads_directory().Append(history_info[i].filename),
downloads_directory().Append(history_info[i].filename),
url_chain, GURL(), // URL Chain, referrer
std::string(), std::string(), // mime_type, original_mime_type
current, current, // start_time, end_time
std::string(), std::string(), // etag, last_modified
1, 1, // received_bytes, total_bytes
Expand Down
121 changes: 83 additions & 38 deletions chrome/browser/history/download_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,6 @@ enum DroppedReason {
DROPPED_REASON_MAX
};

static const char kSchema[] =
"CREATE TABLE downloads ("
"id INTEGER PRIMARY KEY," // Primary key.
"current_path LONGVARCHAR NOT NULL," // Current disk location
"target_path LONGVARCHAR NOT NULL," // Final disk location
"start_time INTEGER NOT NULL," // When the download was started.
"received_bytes INTEGER NOT NULL," // Total size downloaded.
"total_bytes INTEGER NOT NULL," // Total size of the download.
"state INTEGER NOT NULL," // 1=complete, 4=interrupted
"danger_type INTEGER NOT NULL, " // Danger type, validated.
"interrupt_reason INTEGER NOT NULL," // content::DownloadInterruptReason
"end_time INTEGER NOT NULL," // When the download completed.
"opened INTEGER NOT NULL," // 1 if it has ever been opened else 0
"referrer VARCHAR NOT NULL," // HTTP Referrer
"by_ext_id VARCHAR NOT NULL," // ID of extension that started the
// download
"by_ext_name VARCHAR NOT NULL," // name of extension
"etag VARCHAR NOT NULL," // ETag
"last_modified VARCHAR NOT NULL)"; // Last-Modified header

static const char kUrlChainSchema[] =
"CREATE TABLE downloads_url_chains ("
"id INTEGER NOT NULL," // downloads.id.
"chain_index INTEGER NOT NULL," // Index of url in chain
// 0 is initial target,
// MAX is target after redirects.
"url LONGVARCHAR NOT NULL, " // URL.
"PRIMARY KEY (id, chain_index) )";

#if defined(OS_POSIX)

// Binds/reads the given file path to the given column of the given statement.
Expand Down Expand Up @@ -213,6 +184,13 @@ bool DownloadDatabase::EnsureColumnExists(
GetDB().Execute(add_col.c_str());
}

bool DownloadDatabase::MigrateMimeType() {
return EnsureColumnExists("mime_type", "VARCHAR(255) NOT NULL"
" DEFAULT \"\"") &&
EnsureColumnExists("original_mime_type", "VARCHAR(255) NOT NULL"
" DEFAULT \"\"");
}

bool DownloadDatabase::MigrateDownloadsState() {
sql::Statement statement(GetDB().GetUniqueStatement(
"UPDATE downloads SET state=? WHERE state=?"));
Expand All @@ -227,8 +205,32 @@ bool DownloadDatabase::MigrateDownloadsReasonPathsAndDangerType() {
if (!GetDB().Execute("ALTER TABLE downloads RENAME TO downloads_tmp"))
return false;

const char kReasonPathDangerSchema[] =
"CREATE TABLE downloads ("
"id INTEGER PRIMARY KEY,"
"current_path LONGVARCHAR NOT NULL,"
"target_path LONGVARCHAR NOT NULL,"
"start_time INTEGER NOT NULL,"
"received_bytes INTEGER NOT NULL,"
"total_bytes INTEGER NOT NULL,"
"state INTEGER NOT NULL,"
"danger_type INTEGER NOT NULL,"
"interrupt_reason INTEGER NOT NULL,"
"end_time INTEGER NOT NULL,"
"opened INTEGER NOT NULL)";

static const char kReasonPathDangerUrlChainSchema[] =
"CREATE TABLE downloads_url_chains ("
"id INTEGER NOT NULL," // downloads.id.
"chain_index INTEGER NOT NULL," // Index of url in chain
// 0 is initial target,
// MAX is target after redirects.
"url LONGVARCHAR NOT NULL, " // URL.
"PRIMARY KEY (id, chain_index) )";


// Recreate main table.
if (!GetDB().Execute(kSchema))
if (!GetDB().Execute(kReasonPathDangerSchema))
return false;

// Populate it. As we do so, we transform the time values from time_t
Expand All @@ -238,24 +240,23 @@ bool DownloadDatabase::MigrateDownloadsReasonPathsAndDangerType() {
sql::Statement statement_populate(GetDB().GetUniqueStatement(
"INSERT INTO downloads "
"( id, current_path, target_path, start_time, received_bytes, "
" total_bytes, state, danger_type, interrupt_reason, end_time, opened, "
" referrer, by_ext_id, by_ext_name, etag, last_modified ) "
" total_bytes, state, danger_type, interrupt_reason, end_time, opened ) "
"SELECT id, full_path, full_path, "
" CASE start_time WHEN 0 THEN 0 ELSE "
" (start_time + 11644473600) * 1000000 END, "
" received_bytes, total_bytes, "
" state, ?, ?, "
" CASE end_time WHEN 0 THEN 0 ELSE "
" (end_time + 11644473600) * 1000000 END, "
" opened, \"\", \"\", \"\", \"\", \"\" "
" opened "
"FROM downloads_tmp"));
statement_populate.BindInt(0, content::DOWNLOAD_INTERRUPT_REASON_NONE);
statement_populate.BindInt(1, kDangerTypeNotDangerous);
if (!statement_populate.Run())
return false;

// Create new chain table and populate it.
if (!GetDB().Execute(kUrlChainSchema))
if (!GetDB().Execute(kReasonPathDangerUrlChainSchema))
return false;

if (!GetDB().Execute("INSERT INTO downloads_url_chains "
Expand Down Expand Up @@ -285,6 +286,38 @@ bool DownloadDatabase::MigrateDownloadValidators() {
}

bool DownloadDatabase::InitDownloadTable() {
const char kSchema[] =
"CREATE TABLE downloads ("
"id INTEGER PRIMARY KEY," // Primary key.
"current_path LONGVARCHAR NOT NULL," // Current disk location
"target_path LONGVARCHAR NOT NULL," // Final disk location
"start_time INTEGER NOT NULL," // When the download was started.
"received_bytes INTEGER NOT NULL," // Total size downloaded.
"total_bytes INTEGER NOT NULL," // Total size of the download.
"state INTEGER NOT NULL," // 1=complete, 4=interrupted
"danger_type INTEGER NOT NULL," // Danger type, validated.
"interrupt_reason INTEGER NOT NULL," // content::DownloadInterruptReason
"end_time INTEGER NOT NULL," // When the download completed.
"opened INTEGER NOT NULL," // 1 if it has ever been opened
// else 0
"referrer VARCHAR NOT NULL," // HTTP Referrer
"by_ext_id VARCHAR NOT NULL," // ID of extension that started the
// download
"by_ext_name VARCHAR NOT NULL," // name of extension
"etag VARCHAR NOT NULL," // ETag
"last_modified VARCHAR NOT NULL," // Last-Modified header
"mime_type VARCHAR(255) NOT NULL," // MIME type.
"original_mime_type VARCHAR(255) NOT NULL)"; // Original MIME type.

const char kUrlChainSchema[] =
"CREATE TABLE downloads_url_chains ("
"id INTEGER NOT NULL," // downloads.id.
"chain_index INTEGER NOT NULL," // Index of url in chain
// 0 is initial target,
// MAX is target after redirects.
"url LONGVARCHAR NOT NULL, " // URL.
"PRIMARY KEY (id, chain_index) )";

if (GetDB().DoesTableExist("downloads")) {
return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") &&
EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0");
Expand Down Expand Up @@ -327,7 +360,9 @@ void DownloadDatabase::QueryDownloads(
std::map<uint32, DownloadRow*> info_map;

sql::Statement statement_main(GetDB().GetCachedStatement(SQL_FROM_HERE,
"SELECT id, current_path, target_path, start_time, received_bytes, "
"SELECT id, current_path, target_path, "
"mime_type, original_mime_type, "
"start_time, received_bytes, "
"total_bytes, state, danger_type, interrupt_reason, end_time, opened, "
"referrer, by_ext_id, by_ext_name, etag, last_modified "
"FROM downloads ORDER BY start_time"));
Expand All @@ -343,6 +378,8 @@ void DownloadDatabase::QueryDownloads(
info->id = static_cast<uint32>(signed_id);
info->current_path = ColumnFilePath(statement_main, column++);
info->target_path = ColumnFilePath(statement_main, column++);
info->mime_type = statement_main.ColumnString(column++);
info->original_mime_type = statement_main.ColumnString(column++);
info->start_time = base::Time::FromInternalValue(
statement_main.ColumnInt64(column++));
info->received_bytes = statement_main.ColumnInt64(column++);
Expand Down Expand Up @@ -463,13 +500,17 @@ bool DownloadDatabase::UpdateDownload(const DownloadRow& data) {

sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"UPDATE downloads "
"SET current_path=?, target_path=?, received_bytes=?, state=?, "
"SET current_path=?, target_path=?, "
"mime_type=?, original_mime_type=?, "
"received_bytes=?, state=?, "
"danger_type=?, interrupt_reason=?, end_time=?, total_bytes=?, "
"opened=?, by_ext_id=?, by_ext_name=?, etag=?, last_modified=? "
"WHERE id=?"));
int column = 0;
BindFilePath(statement, data.current_path, column++);
BindFilePath(statement, data.target_path, column++);
statement.BindString(column++, data.mime_type);
statement.BindString(column++, data.original_mime_type);
statement.BindInt64(column++, data.received_bytes);
statement.BindInt(column++, state);
statement.BindInt(column++, danger_type);
Expand Down Expand Up @@ -519,16 +560,20 @@ bool DownloadDatabase::CreateDownload(const DownloadRow& info) {
sql::Statement statement_insert(GetDB().GetCachedStatement(
SQL_FROM_HERE,
"INSERT INTO downloads "
"(id, current_path, target_path, start_time, "
"(id, current_path, target_path, "
" mime_type, original_mime_type, "
" start_time, "
" received_bytes, total_bytes, state, danger_type, interrupt_reason, "
" end_time, opened, referrer, by_ext_id, by_ext_name, etag, "
" last_modified) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));

int column = 0;
statement_insert.BindInt(column++, info.id);
BindFilePath(statement_insert, info.current_path, column++);
BindFilePath(statement_insert, info.target_path, column++);
statement_insert.BindString(column++, info.mime_type);
statement_insert.BindString(column++, info.original_mime_type);
statement_insert.BindInt64(column++, info.start_time.ToInternalValue());
statement_insert.BindInt64(column++, info.received_bytes);
statement_insert.BindInt64(column++, info.total_bytes);
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/history/download_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class DownloadDatabase {
// Returns the database for the functions in this interface.
virtual sql::Connection& GetDB() = 0;

// Returns true if able to successfully add mime types to the downloads table.
bool MigrateMimeType();

// Returns true if able to successfully rewrite the invalid values for the
// |state| field from 3 to 4. Returns false if there was an error fixing the
// database. See http://crbug.com/140687
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/history/download_row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ DownloadRow::DownloadRow(
const base::FilePath& target_path,
const std::vector<GURL>& url_chain,
const GURL& referrer,
const std::string& mime_type,
const std::string& original_mime_type,
const base::Time& start,
const base::Time& end,
const std::string& etag,
Expand All @@ -38,6 +40,8 @@ DownloadRow::DownloadRow(
target_path(target_path),
url_chain(url_chain),
referrer_url(referrer),
mime_type(mime_type),
original_mime_type(original_mime_type),
start_time(start),
end_time(end),
etag(etag),
Expand Down
9 changes: 9 additions & 0 deletions chrome/browser/history/download_row.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_ROW_H_
#define CHROME_BROWSER_HISTORY_DOWNLOAD_ROW_H_

#include <string>
#include <vector>

#include "base/files/file_path.h"
Expand All @@ -26,6 +27,8 @@ struct DownloadRow {
const base::FilePath& target_path,
const std::vector<GURL>& url_chain,
const GURL& referrer,
const std::string& mime_type,
const std::string& original_mime_type,
const base::Time& start,
const base::Time& end,
const std::string& etag,
Expand Down Expand Up @@ -57,6 +60,12 @@ struct DownloadRow {
// The URL that referred us. Is not changed by UpdateDownload().
GURL referrer_url;

// The MIME type of the download, might be based on heuristics.
std::string mime_type;

// The original MIME type of the download.
std::string original_mime_type;

// The time when the download started. Is not changed by UpdateDownload().
base::Time start_time;

Expand Down
Loading

0 comments on commit 0e64856

Please sign in to comment.