diff options
author | sdefresne <sdefresne@chromium.org> | 2015-01-12 08:24:04 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-12 16:25:00 +0000 |
commit | 71524662845133324432049a5e3829900451ed4e (patch) | |
tree | 338230ca100f65dde47e34b8d559ae4075872c10 | |
parent | b7d9049eb4253af4e7875f87554b041a7688bfb3 (diff) | |
download | chromium_src-71524662845133324432049a5e3829900451ed4e.zip chromium_src-71524662845133324432049a5e3829900451ed4e.tar.gz chromium_src-71524662845133324432049a5e3829900451ed4e.tar.bz2 |
Remove dependency on //content from history DownloadDatabase
Move the mapping between content enumeration from download_row.h and
download_database.h into an embedder specific file and provide a
content implementation.
BUG=371836
Review URL: https://codereview.chromium.org/833033002
Cr-Commit-Position: refs/heads/master@{#311057}
35 files changed, 792 insertions, 407 deletions
diff --git a/chrome/browser/BUILD.gn b/chrome/browser/BUILD.gn index d4c4510..daf5bad 100644 --- a/chrome/browser/BUILD.gn +++ b/chrome/browser/BUILD.gn @@ -226,6 +226,7 @@ static_library("browser") { "//components/app_modal", "//components/autofill/content/browser", "//components/dom_distiller/content", + "//components/history/content/browser", "//components/keyed_service/content", "//components/navigation_interception", "//components/password_manager/content/browser", diff --git a/chrome/browser/download/download_browsertest.cc b/chrome/browser/download/download_browsertest.cc index 6237ab5..d315768 100644 --- a/chrome/browser/download/download_browsertest.cc +++ b/chrome/browser/download/download_browsertest.cc @@ -40,7 +40,6 @@ #include "chrome/browser/extensions/extension_install_prompt.h" #include "chrome/browser/extensions/extension_install_prompt_show_params.h" #include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_service.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/infobars/infobar_service.h" @@ -65,6 +64,9 @@ #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/test_switches.h" #include "chrome/test/base/ui_test_utils.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/infobars/core/confirm_infobar_delegate.h" #include "components/infobars/core/infobar.h" #include "content/public/browser/download_interrupt_reasons.h" @@ -1774,12 +1776,12 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { ASSERT_EQ(2u, row.url_chain.size()); EXPECT_EQ(redirect_url.spec(), row.url_chain[0].spec()); EXPECT_EQ(download_url.spec(), row.url_chain[1].spec()); - EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, row.danger_type); + EXPECT_EQ(history::DownloadDangerType::NOT_DANGEROUS, row.danger_type); EXPECT_LE(start, row.start_time); EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize, row.received_bytes); EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize + URLRequestSlowDownloadJob::kSecondDownloadSize, row.total_bytes); - EXPECT_EQ(content::DownloadItem::IN_PROGRESS, row.state); + EXPECT_EQ(history::DownloadState::IN_PROGRESS, row.state); EXPECT_FALSE(row.opened); // Finish the download. We're ok relying on the history to be flushed @@ -1807,15 +1809,16 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryCheck) { ASSERT_EQ(2u, row1.url_chain.size()); EXPECT_EQ(redirect_url.spec(), row1.url_chain[0].spec()); EXPECT_EQ(download_url.spec(), row1.url_chain[1].spec()); - EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, row1.danger_type); + EXPECT_EQ(history::DownloadDangerType::NOT_DANGEROUS, row1.danger_type); EXPECT_LE(start, row1.start_time); EXPECT_GE(end, row1.end_time); EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize, row1.received_bytes); EXPECT_EQ(URLRequestSlowDownloadJob::kFirstDownloadSize + URLRequestSlowDownloadJob::kSecondDownloadSize, row1.total_bytes); - EXPECT_EQ(content::DownloadItem::INTERRUPTED, row1.state); - EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED, + EXPECT_EQ(history::DownloadState::INTERRUPTED, row1.state); + EXPECT_EQ(history::ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED), row1.interrupt_reason); EXPECT_FALSE(row1.opened); } @@ -1857,9 +1860,9 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryDangerCheck) { EXPECT_NE(DownloadTargetDeterminer::GetCrDownloadPath( DestinationFile(browser(), file)), row.current_path); - EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, row.danger_type); + EXPECT_EQ(history::DownloadDangerType::DANGEROUS_FILE, row.danger_type); EXPECT_LE(start, row.start_time); - EXPECT_EQ(content::DownloadItem::IN_PROGRESS, row.state); + EXPECT_EQ(history::DownloadState::IN_PROGRESS, row.state); EXPECT_FALSE(row.opened); // Validate the download and wait for it to finish. @@ -1877,9 +1880,9 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, DownloadHistoryDangerCheck) { history::DownloadRow& row1(downloads_in_database->at(0)); EXPECT_EQ(DestinationFile(browser(), file), row1.target_path); EXPECT_EQ(DestinationFile(browser(), file), row1.current_path); - EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED, row1.danger_type); + EXPECT_EQ(history::DownloadDangerType::USER_VALIDATED, row1.danger_type); EXPECT_LE(start, row1.start_time); - EXPECT_EQ(content::DownloadItem::COMPLETE, row1.state); + EXPECT_EQ(history::DownloadState::COMPLETE, row1.state); EXPECT_FALSE(row1.opened); // Not checking file size--not relevant to the point of the test, and // the file size is actually different on Windows and other platforms, diff --git a/chrome/browser/download/download_history.cc b/chrome/browser/download/download_history.cc index 9e96082..3ee464c 100644 --- a/chrome/browser/download/download_history.cc +++ b/chrome/browser/download/download_history.cc @@ -30,9 +30,10 @@ #include "base/metrics/histogram.h" #include "chrome/browser/download/download_crx_util.h" -#include "chrome/browser/history/download_database.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_service.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "components/history/core/browser/download_database.h" +#include "components/history/core/browser/download_row.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" @@ -135,10 +136,10 @@ history::DownloadRow GetDownloadRow( item->GetLastModifiedTime(), item->GetReceivedBytes(), item->GetTotalBytes(), - item->GetState(), - item->GetDangerType(), - item->GetLastReason(), - item->GetId(), + history::ToHistoryDownloadState(item->GetState()), + history::ToHistoryDownloadDangerType(item->GetDangerType()), + history::ToHistoryDownloadInterruptReason(item->GetLastReason()), + history::ToHistoryDownloadId(item->GetId()), item->GetOpened(), by_ext_id, by_ext_name); @@ -257,7 +258,7 @@ void DownloadHistory::QueryCallback(scoped_ptr<InfoVector> infos) { return; for (InfoVector::const_iterator it = infos->begin(); it != infos->end(); ++it) { - loading_id_ = it->id; + loading_id_ = history::ToContentDownloadId(it->id); content::DownloadItem* item = notifier_.GetManager()->CreateDownloadItem( loading_id_, it->current_path, @@ -272,9 +273,9 @@ void DownloadHistory::QueryCallback(scoped_ptr<InfoVector> infos) { it->last_modified, it->received_bytes, it->total_bytes, - it->state, - it->danger_type, - it->interrupt_reason, + history::ToContentDownloadState(it->state), + history::ToContentDownloadDangerType(it->danger_type), + history::ToContentDownloadInterruptReason(it->interrupt_reason), it->opened); #if defined(ENABLE_EXTENSIONS) if (!it->by_ext_id.empty() && !it->by_ext_name.empty()) { diff --git a/chrome/browser/download/download_history_unittest.cc b/chrome/browser/download/download_history_unittest.cc index e11d0a8..5c43c67 100644 --- a/chrome/browser/download/download_history_unittest.cc +++ b/chrome/browser/download/download_history_unittest.cc @@ -8,9 +8,10 @@ #include "base/rand_util.h" #include "base/stl_util.h" #include "chrome/browser/download/download_history.h" -#include "chrome/browser/history/download_database.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_service.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "content/public/test/mock_download_item.h" #include "content/public/test/mock_download_manager.h" #include "content/public/test/test_browser_thread.h" @@ -228,24 +229,25 @@ class DownloadHistoryTest : public testing::Test { EXPECT_CALL(manager(), RemoveObserver(_)); download_created_index_ = 0; for (size_t index = 0; index < infos->size(); ++index) { + const history::DownloadRow& row = infos->at(index); content::MockDownloadManager::CreateDownloadItemAdapter adapter( - infos->at(index).id, - infos->at(index).current_path, - 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, - infos->at(index).last_modified, - infos->at(index).received_bytes, - infos->at(index).total_bytes, - infos->at(index).state, - infos->at(index).danger_type, - infos->at(index).interrupt_reason, - infos->at(index).opened); + history::ToContentDownloadId(row.id), + row.current_path, + row.target_path, + row.url_chain, + row.referrer_url, + row.mime_type, + row.original_mime_type, + row.start_time, + row.end_time, + row.etag, + row.last_modified, + row.received_bytes, + row.total_bytes, + history::ToContentDownloadState(row.state), + history::ToContentDownloadDangerType(row.danger_type), + history::ToContentDownloadInterruptReason(row.interrupt_reason), + row.opened); EXPECT_CALL(manager(), MockCreateDownloadItem(adapter)) .WillOnce(DoAll( InvokeWithoutArgs( @@ -405,10 +407,11 @@ class DownloadHistoryTest : public testing::Test { info->last_modified = last_modified; info->received_bytes = received_bytes; info->total_bytes = total_bytes; - info->state = state; - info->danger_type = danger_type; - info->interrupt_reason = interrupt_reason; - info->id = id; + info->state = history::ToHistoryDownloadState(state); + info->danger_type = history::ToHistoryDownloadDangerType(danger_type); + info->interrupt_reason = + history::ToHistoryDownloadInterruptReason(interrupt_reason); + info->id = history::ToHistoryDownloadId(id); info->opened = opened; info->by_ext_id = by_extension_id; info->by_ext_name = by_extension_name; @@ -661,21 +664,22 @@ TEST_F(DownloadHistoryTest, DownloadHistoryTest_Update) { // state EXPECT_CALL(item(0), GetState()) .WillRepeatedly(Return(content::DownloadItem::INTERRUPTED)); - info.state = content::DownloadItem::INTERRUPTED; + info.state = history::DownloadState::INTERRUPTED; item(0).NotifyObserversDownloadUpdated(); ExpectDownloadUpdated(info); // danger_type EXPECT_CALL(item(0), GetDangerType()) .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT)); - info.danger_type = content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; + info.danger_type = history::DownloadDangerType::DANGEROUS_CONTENT; item(0).NotifyObserversDownloadUpdated(); ExpectDownloadUpdated(info); // interrupt_reason EXPECT_CALL(item(0), GetLastReason()) .WillRepeatedly(Return(content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED)); - info.interrupt_reason = content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; + info.interrupt_reason = history::ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED); item(0).NotifyObserversDownloadUpdated(); ExpectDownloadUpdated(info); diff --git a/chrome/browser/download/download_ui_controller_unittest.cc b/chrome/browser/download/download_ui_controller_unittest.cc index 59aa68b..23b21be 100644 --- a/chrome/browser/download/download_ui_controller_unittest.cc +++ b/chrome/browser/download/download_ui_controller_unittest.cc @@ -13,9 +13,9 @@ #include "chrome/browser/download/download_service.h" #include "chrome/browser/download/download_service_factory.h" #include "chrome/browser/download/download_ui_controller.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/profiles/profile.h" #include "chrome/test/base/chrome_render_view_host_test_harness.h" +#include "components/history/core/browser/download_row.h" #include "content/public/test/mock_download_item.h" #include "content/public/test/mock_download_manager.h" #include "testing/gmock/include/gmock/gmock.h" diff --git a/chrome/browser/download/save_page_browsertest.cc b/chrome/browser/download/save_page_browsertest.cc index b3e3b58..a6d4795 100644 --- a/chrome/browser/download/save_page_browsertest.cc +++ b/chrome/browser/download/save_page_browsertest.cc @@ -19,7 +19,6 @@ #include "chrome/browser/download/download_service.h" #include "chrome/browser/download/download_service_factory.h" #include "chrome/browser/download/save_package_file_picker.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/net/url_request_mock_util.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" @@ -32,6 +31,8 @@ #include "chrome/common/url_constants.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "content/public/browser/download_item.h" #include "content/public/browser/download_manager.h" #include "content/public/browser/notification_service.h" @@ -141,7 +142,7 @@ bool DownloadStoredProperly( const GURL& expected_url, const base::FilePath& expected_path, int64 num_files, - DownloadItem::DownloadState expected_state, + history::DownloadState expected_state, DownloadItem* item, const history::DownloadRow& info) { // This function may be called multiple times for a given test. Returning @@ -383,7 +384,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnly) { GetDestinationPaths("a", &full_file_name, &dir); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, 1, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( @@ -414,7 +415,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, DISABLED_SaveHTMLOnlyCancel) { DownloadItemCreatedObserver creation_observer(manager); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, -1, - DownloadItem::CANCELLED)); + history::DownloadState::CANCELLED)); // -1 to disable number of files check; we don't update after cancel, and // we don't know when the single file completed in relationship to // the cancel. @@ -510,7 +511,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveViewSourceHTMLOnly) { GetDestinationPaths("a", &full_file_name, &dir); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, actual_page_url, full_file_name, 1, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( @@ -544,7 +545,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveCompleteHTML) { GetDestinationPaths("b", &full_file_name, &dir); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, 3, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( @@ -639,7 +640,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_FileNameFromPageTitle) { "Test page for saving page feature_files"); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, 3, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( @@ -680,7 +681,7 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_RemoveFromList) { GetDestinationPaths("a", &full_file_name, &dir); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, 1, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( @@ -770,7 +771,7 @@ IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { SavePackageFilePicker::SetShouldPromptUser(false); DownloadPersistedObserver persisted(browser()->profile(), base::Bind( &DownloadStoredProperly, url, full_file_name, -1, - DownloadItem::COMPLETE)); + history::DownloadState::COMPLETE)); scoped_refptr<content::MessageLoopRunner> loop_runner( new content::MessageLoopRunner); SavePackageFinishedObserver observer( diff --git a/chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc b/chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc index 0060fc0..6028304 100644 --- a/chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc +++ b/chrome/browser/extensions/api/downloads/downloads_api_browsertest.cc @@ -23,7 +23,6 @@ #include "chrome/browser/extensions/browser_action_test_util.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_function_test_utils.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/net/url_request_mock_util.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" diff --git a/chrome/browser/history/download_row.cc b/chrome/browser/history/download_row.cc deleted file mode 100644 index 6621dac6..0000000 --- a/chrome/browser/history/download_row.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2011 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/history/download_row.h" - -namespace history { - -DownloadRow::DownloadRow() - : received_bytes(0), - total_bytes(0), - state(content::DownloadItem::IN_PROGRESS), - danger_type(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS), - interrupt_reason(content::DOWNLOAD_INTERRUPT_REASON_NONE), - id(content::DownloadItem::kInvalidId), - opened(false) { -} - -DownloadRow::DownloadRow( - const base::FilePath& current_path, - 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, - const std::string& last_modified, - int64 received, - int64 total, - content::DownloadItem::DownloadState download_state, - content::DownloadDangerType danger_type, - content::DownloadInterruptReason interrupt_reason, - uint32 id, - bool download_opened, - const std::string& ext_id, - const std::string& ext_name) - : current_path(current_path), - 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), - last_modified(last_modified), - received_bytes(received), - total_bytes(total), - state(download_state), - danger_type(danger_type), - interrupt_reason(interrupt_reason), - id(id), - opened(download_opened), - by_ext_id(ext_id), - by_ext_name(ext_name) { -} - -DownloadRow::~DownloadRow() { -} - -} // namespace history diff --git a/chrome/browser/history/history_backend.cc b/chrome/browser/history/history_backend.cc index 2054fba..8ac71f2 100644 --- a/chrome/browser/history/history_backend.cc +++ b/chrome/browser/history/history_backend.cc @@ -24,7 +24,6 @@ #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_notifications.h" #include "chrome/browser/history/in_memory_history_backend.h" #include "chrome/browser/history/in_memory_history_backend.h" @@ -35,6 +34,8 @@ #include "chrome/common/importer/imported_favicon_usage.h" #include "chrome/common/url_constants.h" #include "components/favicon_base/select_favicon_frames.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_backend_observer.h" #include "components/history/core/browser/history_client.h" #include "components/history/core/browser/history_constants.h" @@ -1111,7 +1112,7 @@ void HistoryBackend::RemoveObserver(HistoryBackendObserver* observer) { // Downloads ------------------------------------------------------------------- uint32 HistoryBackend::GetNextDownloadId() { - return db_ ? db_->GetNextDownloadId() : content::DownloadItem::kInvalidId; + return db_ ? db_->GetNextDownloadId() : kInvalidDownloadId; } // Get all the download entries from the database. diff --git a/chrome/browser/history/history_database.cc b/chrome/browser/history/history_database.cc index a0786eb..0eb7a90 100644 --- a/chrome/browser/history/history_database.cc +++ b/chrome/browser/history/history_database.cc @@ -14,6 +14,8 @@ #include "base/rand_util.h" #include "base/strings/string_util.h" #include "base/time/time.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "content/public/browser/download_interrupt_reasons.h" #include "sql/transaction.h" #if defined(OS_MACOSX) @@ -33,7 +35,11 @@ const char kEarlyExpirationThresholdKey[] = "early_expiration_threshold"; } // namespace -HistoryDatabase::HistoryDatabase() { +HistoryDatabase::HistoryDatabase() + : DownloadDatabase(ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_NONE), + ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_CRASH)) { } HistoryDatabase::~HistoryDatabase() { diff --git a/chrome/browser/history/history_database.h b/chrome/browser/history/history_database.h index 4a4ac9e..687ef88 100644 --- a/chrome/browser/history/history_database.h +++ b/chrome/browser/history/history_database.h @@ -9,7 +9,7 @@ #include "base/compiler_specific.h" #include "base/gtest_prod_util.h" #include "build/build_config.h" -#include "chrome/browser/history/download_database.h" +#include "components/history/core/browser/download_database.h" #include "components/history/core/browser/url_database.h" #include "components/history/core/browser/visit_database.h" #include "components/history/core/browser/visitsegment_database.h" diff --git a/chrome/browser/history/history_service.cc b/chrome/browser/history/history_service.cc index f010de6..8eb490b 100644 --- a/chrome/browser/history/history_service.cc +++ b/chrome/browser/history/history_service.cc @@ -31,7 +31,6 @@ #include "base/time/time.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/history_notifications.h" #include "chrome/browser/history/in_memory_history_backend.h" @@ -46,6 +45,7 @@ #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "components/dom_distiller/core/url_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_client.h" #include "components/history/core/browser/history_service_observer.h" #include "components/history/core/browser/history_types.h" diff --git a/chrome/browser/history/history_unittest.cc b/chrome/browser/history/history_unittest.cc index d344ca5..b6022f5 100644 --- a/chrome/browser/history/history_unittest.cc +++ b/chrome/browser/history/history_unittest.cc @@ -42,7 +42,6 @@ #include "base/task/cancelable_task_tracker.h" #include "base/threading/platform_thread.h" #include "base/time/time.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/history_database.h" #include "chrome/browser/history/history_notifications.h" @@ -52,6 +51,9 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" #include "chrome/tools/profiles/thumbnail-inl.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_constants.h" #include "components/history/core/browser/history_db_task.h" #include "components/history/core/browser/in_memory_database.h" @@ -180,9 +182,7 @@ class HistoryBackendDBTest : public HistoryUnitTestBase { base::MessageLoop::current()->Run(); } - bool AddDownload(uint32 id, - DownloadItem::DownloadState state, - const Time& time) { + bool AddDownload(uint32 id, DownloadState state, const Time& time) { std::vector<GURL> url_chain; url_chain.push_back(GURL("foo-url")); @@ -199,8 +199,9 @@ class HistoryBackendDBTest : public HistoryUnitTestBase { 0, 512, state, - content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, - content::DOWNLOAD_INTERRUPT_REASON_NONE, + DownloadDangerType::NOT_DANGEROUS, + ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_NONE), id, false, "by_ext_id", @@ -250,7 +251,7 @@ TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { // was removed. Time now = Time(); uint32 id = 1; - EXPECT_TRUE(AddDownload(id, DownloadItem::COMPLETE, Time())); + EXPECT_TRUE(AddDownload(id, DownloadState::COMPLETE, Time())); db_->QueryDownloads(&downloads); EXPECT_EQ(1U, downloads.size()); @@ -266,9 +267,8 @@ TEST_F(HistoryBackendDBTest, ClearBrowsingData_Downloads) { EXPECT_EQ(now, downloads[0].end_time); EXPECT_EQ(0, downloads[0].received_bytes); EXPECT_EQ(512, downloads[0].total_bytes); - EXPECT_EQ(DownloadItem::COMPLETE, downloads[0].state); - EXPECT_EQ(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, - downloads[0].danger_type); + EXPECT_EQ(DownloadState::COMPLETE, downloads[0].state); + EXPECT_EQ(DownloadDangerType::NOT_DANGEROUS, downloads[0].danger_type); EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, downloads[0].interrupt_reason); EXPECT_FALSE(downloads[0].opened); @@ -729,9 +729,9 @@ TEST_F(HistoryBackendDBTest, ConfirmDownloadRowCreateAndDelete) { // Add some downloads. uint32 id1 = 1, id2 = 2, id3 = 3; - AddDownload(id1, DownloadItem::COMPLETE, now); - AddDownload(id2, DownloadItem::COMPLETE, now + base::TimeDelta::FromDays(2)); - AddDownload(id3, DownloadItem::COMPLETE, now - base::TimeDelta::FromDays(2)); + AddDownload(id1, DownloadState::COMPLETE, now); + AddDownload(id2, DownloadState::COMPLETE, now + base::TimeDelta::FromDays(2)); + AddDownload(id3, DownloadState::COMPLETE, now - base::TimeDelta::FromDays(2)); // Confirm that resulted in the correct number of rows in the DB. DeleteBackend(); @@ -785,9 +785,10 @@ TEST_F(HistoryBackendDBTest, DownloadNukeRecordsMissingURLs) { std::string(), 0, 512, - DownloadItem::COMPLETE, - content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, - content::DOWNLOAD_INTERRUPT_REASON_NONE, + DownloadState::COMPLETE, + DownloadDangerType::NOT_DANGEROUS, + ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_NONE), 1, 0, "by_ext_id", @@ -834,7 +835,7 @@ TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { base::Time now(base::Time::Now()); // Put an IN_PROGRESS download in the DB. - AddDownload(1, DownloadItem::IN_PROGRESS, now); + AddDownload(1, DownloadState::IN_PROGRESS, now); // Confirm that they made it into the DB unchanged. DeleteBackend(); @@ -849,7 +850,8 @@ TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { sql::Statement statement1(db.GetUniqueStatement( "Select state, interrupt_reason from downloads")); EXPECT_TRUE(statement1.Step()); - EXPECT_EQ(DownloadDatabase::kStateInProgress, statement1.ColumnInt(0)); + EXPECT_EQ(DownloadStateToInt(DownloadState::IN_PROGRESS), + statement1.ColumnInt(0)); EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, statement1.ColumnInt(1)); EXPECT_FALSE(statement1.Step()); } @@ -860,7 +862,7 @@ TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { std::vector<DownloadRow> results; db_->QueryDownloads(&results); ASSERT_EQ(1u, results.size()); - EXPECT_EQ(content::DownloadItem::INTERRUPTED, results[0].state); + EXPECT_EQ(DownloadState::INTERRUPTED, results[0].state); EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, results[0].interrupt_reason); @@ -879,7 +881,8 @@ TEST_F(HistoryBackendDBTest, ConfirmDownloadInProgressCleanup) { sql::Statement statement1(db.GetUniqueStatement( "Select state, interrupt_reason from downloads")); EXPECT_TRUE(statement1.Step()); - EXPECT_EQ(DownloadDatabase::kStateInterrupted, statement1.ColumnInt(0)); + EXPECT_EQ(DownloadStateToInt(DownloadState::INTERRUPTED), + statement1.ColumnInt(0)); EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_CRASH, statement1.ColumnInt(1)); EXPECT_FALSE(statement1.Step()); diff --git a/chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc b/chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc index 7c311c9..7d18098 100644 --- a/chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc +++ b/chrome/browser/safe_browsing/incident_reporting/last_download_finder.cc @@ -19,7 +19,7 @@ #include "chrome/common/pref_names.h" #include "chrome/common/safe_browsing/csd.pb.h" #include "chrome/common/safe_browsing/download_protection_util.h" -#include "content/public/browser/download_item.h" +#include "components/history/core/browser/download_constants.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_source.h" @@ -85,7 +85,7 @@ const history::DownloadRow* FindMostInteresting( for (size_t i = 0; i < downloads.size(); ++i) { const history::DownloadRow& row = downloads[i]; // Ignore incomplete downloads. - if (row.state != content::DownloadItem::COMPLETE) + if (row.state != history::DownloadState::COMPLETE) continue; if (!most_recent_row || IsMoreInterestingThan(row, *most_recent_row)) most_recent_row = &row; diff --git a/chrome/browser/safe_browsing/incident_reporting/last_download_finder.h b/chrome/browser/safe_browsing/incident_reporting/last_download_finder.h index 0f7503d..a3ef0d6 100644 --- a/chrome/browser/safe_browsing/incident_reporting/last_download_finder.h +++ b/chrome/browser/safe_browsing/incident_reporting/last_download_finder.h @@ -14,8 +14,8 @@ #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/scoped_observer.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_service_observer.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" @@ -28,10 +28,6 @@ class NotificationDetails; class NotificationSource; } -namespace history { -struct DownloadRow; -} - namespace safe_browsing { class ClientIncidentReport_DownloadDetails; diff --git a/chrome/browser/safe_browsing/incident_reporting/last_download_finder_unittest.cc b/chrome/browser/safe_browsing/incident_reporting/last_download_finder_unittest.cc index 4897b51..dd11d40 100644 --- a/chrome/browser/safe_browsing/incident_reporting/last_download_finder_unittest.cc +++ b/chrome/browser/safe_browsing/incident_reporting/last_download_finder_unittest.cc @@ -15,7 +15,6 @@ #include "base/strings/utf_string_conversions.h" #include "chrome/browser/history/chrome_history_client.h" #include "chrome/browser/history/chrome_history_client_factory.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/history/history_service.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/history/web_history_service_factory.h" @@ -28,6 +27,9 @@ #include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile_manager.h" +#include "components/history/content/browser/download_constants_utils.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_constants.h" #include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_utils.h" @@ -219,20 +221,21 @@ class LastDownloadFinderTest : public testing::Test { std::vector<GURL>(1, GURL("http://www.google.com")), // url_chain GURL(), // referrer "application/octet-stream", // mime_type - "application/octet-stream", // original_mime_type - now - base::TimeDelta::FromMinutes(10), // start - now - base::TimeDelta::FromMinutes(9), // end - std::string(), // etag - std::string(), // last_modified - 47LL, // received - 47LL, // total - content::DownloadItem::COMPLETE, // download_state - content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, // danger_type - content::DOWNLOAD_INTERRUPT_REASON_NONE, // interrupt_reason, - 1, // id - false, // download_opened - std::string(), // ext_id - std::string()); // ext_name + "application/octet-stream", // original_mime_type + now - base::TimeDelta::FromMinutes(10), // start + now - base::TimeDelta::FromMinutes(9), // end + std::string(), // etag + std::string(), // last_modified + 47LL, // received + 47LL, // total + history::DownloadState::COMPLETE, // download_state + history::DownloadDangerType::NOT_DANGEROUS, // danger_type + history::ToHistoryDownloadInterruptReason( + content::DOWNLOAD_INTERRUPT_REASON_NONE), // interrupt_reason, + 1, // id + false, // download_opened + std::string(), // ext_id + std::string()); // ext_name } void ExpectNoDownloadFound( diff --git a/chrome/browser/ui/webui/downloads_dom_handler_browsertest.cc b/chrome/browser/ui/webui/downloads_dom_handler_browsertest.cc index fbc6a48..4d8c060 100644 --- a/chrome/browser/ui/webui/downloads_dom_handler_browsertest.cc +++ b/chrome/browser/ui/webui/downloads_dom_handler_browsertest.cc @@ -7,7 +7,6 @@ #include "base/json/json_reader.h" #include "base/prefs/pref_service.h" #include "base/values.h" -#include "chrome/browser/history/download_row.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/webui/downloads_dom_handler.h" diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 263c385..25abd54 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1502,10 +1502,6 @@ 'browser/history/chrome_history_client_factory.h', 'browser/history/delete_directive_handler.cc', 'browser/history/delete_directive_handler.h', - 'browser/history/download_database.cc', - 'browser/history/download_database.h', - 'browser/history/download_row.cc', - 'browser/history/download_row.h', 'browser/history/expire_history_backend.cc', 'browser/history/expire_history_backend.h', 'browser/history/history_backend.cc', @@ -2989,6 +2985,7 @@ '../cc/cc.gyp:cc', '../components/components.gyp:autofill_content_browser', '../components/components.gyp:dom_distiller_content', + '../components/components.gyp:history_content_browser', '../components/components.gyp:keyed_service_content', '../components/components.gyp:navigation_interception', '../components/components.gyp:packed_ct_ev_whitelist', diff --git a/components/BUILD.gn b/components/BUILD.gn index ca44c58..739d387 100644 --- a/components/BUILD.gn +++ b/components/BUILD.gn @@ -41,6 +41,7 @@ group("all_components") { "//components/feedback", "//components/gcm_driver", "//components/google/core/browser", + "//components/history/content/browser", "//components/history/core/browser", "//components/history/core/common", "//components/history/core/test", @@ -124,8 +125,9 @@ group("all_components") { } if (is_ios) { deps -= [ - "//components/keyed_service/content", "//components/copresence_endpoints", + "//components/history/content/browser", + "//components/keyed_service/content", ] } diff --git a/components/components_tests.gyp b/components/components_tests.gyp index c9437e5..6c6b332 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -559,6 +559,9 @@ # Dependencies of error_page 'components.gyp:error_page_renderer', + # Dependencies of history + 'components.gyp:history_content_browser', + # Dependencies of # intercept_navigation_resource_throttle_unittest.cc '../skia/skia.gyp:skia', diff --git a/components/history.gypi b/components/history.gypi index 434771a..3335644 100644 --- a/components/history.gypi +++ b/components/history.gypi @@ -26,6 +26,13 @@ ], 'sources': [ # Note: sources list duplicated in GN build. + 'history/core/browser/download_constants.h', + 'history/core/browser/download_database.cc', + 'history/core/browser/download_database.h', + 'history/core/browser/download_row.cc', + 'history/core/browser/download_row.h', + 'history/core/browser/download_types.cc', + 'history/core/browser/download_types.h', 'history/core/browser/history_backend_notifier.h', 'history/core/browser/history_backend_observer.h', 'history/core/browser/history_client.cc', @@ -119,6 +126,27 @@ }, ], 'conditions': [ + ['OS!="ios"', { + 'targets': [ + { + # GN version: //components/history/content/browser + 'target_name': 'history_content_browser', + 'type': 'static_library', + 'include_dirs': [ + '..', + ], + 'dependencies': [ + '../base/base.gyp:base', + '../content/content.gyp:content_browser', + 'history_core_browser', + ], + 'sources': [ + 'history/content/browser/download_constants_utils.cc', + 'history/content/browser/download_constants_utils.h', + ], + } + ], + }], ['OS=="android"', { 'targets': [ { diff --git a/components/history/README b/components/history/README new file mode 100644 index 0000000..206d654 --- /dev/null +++ b/components/history/README @@ -0,0 +1,13 @@ +History is in the process of becoming a layered compoent +(https://sites.google.com/a/chromium.org/dev/developers/design-documents/layered-components-design) +to enable it to be shared cleanly on iOS. + +When this process is complete, this component will have the following structure: + +core/: shared code that does not depends on src/content/ or src/ios/ + browser/: browser process code + common/: code shared by the browser and the renderer +content/: driver for the shared code based on the content layer + browser/: browser process code + common/: code shared by the browser and the renderer +ios/: driver for the shared code based on src/ios diff --git a/components/history/content/DEPS b/components/history/content/DEPS new file mode 100644 index 0000000..60dbcf4 --- /dev/null +++ b/components/history/content/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "+content", +] diff --git a/components/history/content/browser/BUILD.gn b/components/history/content/browser/BUILD.gn new file mode 100644 index 0000000..30628c2 --- /dev/null +++ b/components/history/content/browser/BUILD.gn @@ -0,0 +1,16 @@ +# Copyright 2015 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. + +static_library("browser") { + sources = [ + "download_constants_utils.cc", + "download_constants_utils.h", + ] + + deps = [ + "//base", + "//components/history/core/browser", + "//content/public/browser", + ] +} diff --git a/components/history/content/browser/download_constants_utils.cc b/components/history/content/browser/download_constants_utils.cc new file mode 100644 index 0000000..ffdfe23 --- /dev/null +++ b/components/history/content/browser/download_constants_utils.cc @@ -0,0 +1,129 @@ +// Copyright 2015 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 "components/history/content/browser/download_constants_utils.h" + +#include "base/logging.h" +#include "components/history/core/browser/download_constants.h" + +namespace history { + +content::DownloadItem::DownloadState ToContentDownloadState( + DownloadState state) { + switch (state) { + case DownloadState::IN_PROGRESS: + return content::DownloadItem::IN_PROGRESS; + case DownloadState::COMPLETE: + return content::DownloadItem::COMPLETE; + case DownloadState::CANCELLED: + return content::DownloadItem::CANCELLED; + case DownloadState::INTERRUPTED: + return content::DownloadItem::INTERRUPTED; + case DownloadState::INVALID: + case DownloadState::BUG_140687: + NOTREACHED(); + return content::DownloadItem::MAX_DOWNLOAD_STATE; + } + NOTREACHED(); + return content::DownloadItem::MAX_DOWNLOAD_STATE; +} + +DownloadState ToHistoryDownloadState( + content::DownloadItem::DownloadState state) { + switch (state) { + case content::DownloadItem::IN_PROGRESS: + return DownloadState::IN_PROGRESS; + case content::DownloadItem::COMPLETE: + return DownloadState::COMPLETE; + case content::DownloadItem::CANCELLED: + return DownloadState::CANCELLED; + case content::DownloadItem::INTERRUPTED: + return DownloadState::INTERRUPTED; + case content::DownloadItem::MAX_DOWNLOAD_STATE: + NOTREACHED(); + return DownloadState::INVALID; + } + NOTREACHED(); + return DownloadState::INVALID; +} + +content::DownloadDangerType ToContentDownloadDangerType( + DownloadDangerType danger_type) { + switch (danger_type) { + case DownloadDangerType::NOT_DANGEROUS: + return content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; + case DownloadDangerType::DANGEROUS_FILE: + return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; + case DownloadDangerType::DANGEROUS_URL: + return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; + case DownloadDangerType::DANGEROUS_CONTENT: + return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; + case DownloadDangerType::MAYBE_DANGEROUS_CONTENT: + return content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT; + case DownloadDangerType::UNCOMMON_CONTENT: + return content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT; + case DownloadDangerType::USER_VALIDATED: + return content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED; + case DownloadDangerType::DANGEROUS_HOST: + return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST; + case DownloadDangerType::POTENTIALLY_UNWANTED: + return content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED; + case DownloadDangerType::INVALID: + NOTREACHED(); + return content::DOWNLOAD_DANGER_TYPE_MAX; + } + NOTREACHED(); + return content::DOWNLOAD_DANGER_TYPE_MAX; +} + +DownloadDangerType ToHistoryDownloadDangerType( + content::DownloadDangerType danger_type) { + switch (danger_type) { + case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: + return DownloadDangerType::NOT_DANGEROUS; + case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: + return DownloadDangerType::DANGEROUS_FILE; + case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: + return DownloadDangerType::DANGEROUS_URL; + case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: + return DownloadDangerType::DANGEROUS_CONTENT; + case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: + return DownloadDangerType::MAYBE_DANGEROUS_CONTENT; + case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: + return DownloadDangerType::UNCOMMON_CONTENT; + case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: + return DownloadDangerType::USER_VALIDATED; + case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: + return DownloadDangerType::DANGEROUS_HOST; + case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: + return DownloadDangerType::POTENTIALLY_UNWANTED; + default: + NOTREACHED(); + return DownloadDangerType::INVALID; + } + NOTREACHED(); + return DownloadDangerType::INVALID; +} + +content::DownloadInterruptReason ToContentDownloadInterruptReason( + DownloadInterruptReason interrupt_reason) { + return static_cast<content::DownloadInterruptReason>(interrupt_reason); +} + +DownloadInterruptReason ToHistoryDownloadInterruptReason( + content::DownloadInterruptReason interrupt_reason) { + return static_cast<DownloadInterruptReason>(interrupt_reason); +} + +uint32 ToContentDownloadId(DownloadId id) { + DCHECK_NE(id, kInvalidDownloadId); + return static_cast<uint32>(id); +} + +DownloadId ToHistoryDownloadId(uint32 id) { + DCHECK_NE(id, content::DownloadItem::kInvalidId); + return static_cast<DownloadId>(id); +} + +} // namespace history diff --git a/components/history/content/browser/download_constants_utils.h b/components/history/content/browser/download_constants_utils.h new file mode 100644 index 0000000..2529dc7 --- /dev/null +++ b/components/history/content/browser/download_constants_utils.h @@ -0,0 +1,47 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_HISTORY_CONTENT_BROWSER_DOWNLOAD_CONSTANTS_UTILS_H_ +#define COMPONENTS_HISTORY_CONTENT_BROWSER_DOWNLOAD_CONSTANTS_UTILS_H_ + +#include <string> + +#include "components/history/core/browser/download_types.h" +#include "content/public/browser/download_danger_type.h" +#include "content/public/browser/download_interrupt_reasons.h" +#include "content/public/browser/download_item.h" + +namespace history { + +// Utility functions to convert between content::DownloadItem::DownloadState +// enumeration and history::DownloadState constants. +content::DownloadItem::DownloadState ToContentDownloadState( + DownloadState state); +DownloadState ToHistoryDownloadState( + content::DownloadItem::DownloadState state); + +// Utility functions to convert between content::DownloadDangerType enumeration +// and history::DownloadDangerType constants. +content::DownloadDangerType ToContentDownloadDangerType( + DownloadDangerType danger_type); +DownloadDangerType ToHistoryDownloadDangerType( + content::DownloadDangerType danger_type); + +// Utility functions to convert between content::DownloadInterrupReason +// enumeration and history::DownloadInterruptReason type (value have no +// meaning in history, but have a different type to avoid bugs due to +// implicit conversions). +content::DownloadInterruptReason ToContentDownloadInterruptReason( + DownloadInterruptReason interrupt_reason); +DownloadInterruptReason ToHistoryDownloadInterruptReason( + content::DownloadInterruptReason interrupt_reason); + +// Utility functions to convert between content download id values and +// history::DownloadId type (value have no meaning in history, except +// for kInvalidDownloadId). +uint32 ToContentDownloadId(DownloadId id); +DownloadId ToHistoryDownloadId(uint32 id); +} // namespace history + +#endif // COMPONENTS_HISTORY_CONTENT_BROWSER_DOWNLOAD_CONSTANTS_UTILS_H_ diff --git a/components/history/core/DEPS b/components/history/core/DEPS new file mode 100644 index 0000000..970da79 --- /dev/null +++ b/components/history/core/DEPS @@ -0,0 +1,3 @@ +include_rules = [ + "-components/history/content", +] diff --git a/components/history/core/browser/BUILD.gn b/components/history/core/browser/BUILD.gn index 977ea49..6494dc5 100644 --- a/components/history/core/browser/BUILD.gn +++ b/components/history/core/browser/BUILD.gn @@ -6,6 +6,13 @@ import("//third_party/protobuf/proto_library.gni") static_library("browser") { sources = [ + "download_constants.h", + "download_database.cc", + "download_database.h", + "download_row.cc", + "download_row.h", + "download_types.cc", + "download_types.h", "history_backend_notifier.h", "history_backend_observer.h", "history_client.cc", diff --git a/components/history/core/browser/download_constants.h b/components/history/core/browser/download_constants.h new file mode 100644 index 0000000..ea21725 --- /dev/null +++ b/components/history/core/browser/download_constants.h @@ -0,0 +1,47 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_CONSTANTS_H_ +#define COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_CONSTANTS_H_ + +#include "components/history/core/browser/download_types.h" + +namespace history { + +// DownloadState represents the state of a DownloadRow saved into the +// DownloadDatabase. The values must not be changed as they are saved +// to disk in the database. +enum class DownloadState { + INVALID = -1, + IN_PROGRESS = 0, + COMPLETE = 1, + CANCELLED = 2, + BUG_140687 = 3, + INTERRUPTED = 4, +}; + +// DownloadDangerType represents the danger of a DownloadRow into the +// DownloadDatabase. The values must not be changed as they are saved +// to disk in the database. +enum class DownloadDangerType { + INVALID = -1, + NOT_DANGEROUS = 0, + DANGEROUS_FILE = 1, + DANGEROUS_URL = 2, + DANGEROUS_CONTENT = 3, + MAYBE_DANGEROUS_CONTENT = 4, + UNCOMMON_CONTENT = 5, + USER_VALIDATED = 6, + DANGEROUS_HOST = 7, + POTENTIALLY_UNWANTED = 8, +}; + +// DownloadId represents the id of a DownloadRow into the DownloadDatabase. +// The value is controlled by the embedder except for the reserved id +// kInvalidDownloadId. +extern const DownloadId kInvalidDownloadId; + +} // namespace + +#endif // COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_CONSTANTS_H_ diff --git a/chrome/browser/history/download_database.cc b/components/history/core/browser/download_database.cc index 9461e45..e699bea6 100644 --- a/chrome/browser/history/download_database.cc +++ b/components/history/core/browser/download_database.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/history/download_database.h" +#include "components/history/core/browser/download_database.h" #include <limits> #include <string> @@ -17,14 +17,11 @@ #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "build/build_config.h" -#include "chrome/browser/history/download_row.h" +#include "components/history/core/browser/download_constants.h" +#include "components/history/core/browser/download_row.h" #include "components/history/core/browser/history_types.h" -#include "content/public/browser/download_interrupt_reasons.h" -#include "content/public/browser/download_item.h" #include "sql/statement.h" -using content::DownloadItem; - namespace history { namespace { @@ -41,7 +38,8 @@ enum DroppedReason { #if defined(OS_POSIX) // Binds/reads the given file path to the given column of the given statement. -void BindFilePath(sql::Statement& statement, const base::FilePath& path, +void BindFilePath(sql::Statement& statement, + const base::FilePath& path, int col) { statement.BindString(col, path.value()); } @@ -52,7 +50,8 @@ base::FilePath ColumnFilePath(sql::Statement& statement, int col) { #else // See above. -void BindFilePath(sql::Statement& statement, const base::FilePath& path, +void BindFilePath(sql::Statement& statement, + const base::FilePath& path, int col) { statement.BindString16(col, path.value()); } @@ -64,121 +63,21 @@ base::FilePath ColumnFilePath(sql::Statement& statement, int col) { } // namespace -// These constants and the transformation functions below are used to allow -// DownloadItem::DownloadState and DownloadDangerType to change without -// breaking the database schema. -// They guarantee that the values of the |state| field in the database are one -// of the values returned by StateToInt, and that the values of the |state| -// field of the DownloadRows returned by QueryDownloads() are one of the values -// returned by IntToState(). -const int DownloadDatabase::kStateInvalid = -1; -const int DownloadDatabase::kStateInProgress = 0; -const int DownloadDatabase::kStateComplete = 1; -const int DownloadDatabase::kStateCancelled = 2; -const int DownloadDatabase::kStateBug140687 = 3; -const int DownloadDatabase::kStateInterrupted = 4; - -const int DownloadDatabase::kDangerTypeInvalid = -1; -const int DownloadDatabase::kDangerTypeNotDangerous = 0; -const int DownloadDatabase::kDangerTypeDangerousFile = 1; -const int DownloadDatabase::kDangerTypeDangerousUrl = 2; -const int DownloadDatabase::kDangerTypeDangerousContent = 3; -const int DownloadDatabase::kDangerTypeMaybeDangerousContent = 4; -const int DownloadDatabase::kDangerTypeUncommonContent = 5; -const int DownloadDatabase::kDangerTypeUserValidated = 6; -const int DownloadDatabase::kDangerTypeDangerousHost = 7; -const int DownloadDatabase::kDangerTypePotentiallyUnwanted = 8; - -int DownloadDatabase::StateToInt(DownloadItem::DownloadState state) { - switch (state) { - case DownloadItem::IN_PROGRESS: return DownloadDatabase::kStateInProgress; - case DownloadItem::COMPLETE: return DownloadDatabase::kStateComplete; - case DownloadItem::CANCELLED: return DownloadDatabase::kStateCancelled; - case DownloadItem::INTERRUPTED: return DownloadDatabase::kStateInterrupted; - case DownloadItem::MAX_DOWNLOAD_STATE: - NOTREACHED(); - return DownloadDatabase::kStateInvalid; - } - NOTREACHED(); - return DownloadDatabase::kStateInvalid; -} - -DownloadItem::DownloadState DownloadDatabase::IntToState(int state) { - switch (state) { - case DownloadDatabase::kStateInProgress: return DownloadItem::IN_PROGRESS; - case DownloadDatabase::kStateComplete: return DownloadItem::COMPLETE; - case DownloadDatabase::kStateCancelled: return DownloadItem::CANCELLED; - // We should not need kStateBug140687 here because MigrateDownloadsState() - // is called in HistoryDatabase::Init(). - case DownloadDatabase::kStateInterrupted: return DownloadItem::INTERRUPTED; - default: return DownloadItem::MAX_DOWNLOAD_STATE; - } -} - -int DownloadDatabase::DangerTypeToInt(content::DownloadDangerType danger_type) { - switch (danger_type) { - case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: - return DownloadDatabase::kDangerTypeNotDangerous; - case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: - return DownloadDatabase::kDangerTypeDangerousFile; - case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: - return DownloadDatabase::kDangerTypeDangerousUrl; - case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: - return DownloadDatabase::kDangerTypeDangerousContent; - case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: - return DownloadDatabase::kDangerTypeMaybeDangerousContent; - case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: - return DownloadDatabase::kDangerTypeUncommonContent; - case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: - return DownloadDatabase::kDangerTypeUserValidated; - case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: - return DownloadDatabase::kDangerTypeDangerousHost; - case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: - return DownloadDatabase::kDangerTypePotentiallyUnwanted; - case content::DOWNLOAD_DANGER_TYPE_MAX: - NOTREACHED(); - return DownloadDatabase::kDangerTypeInvalid; - } - NOTREACHED(); - return DownloadDatabase::kDangerTypeInvalid; -} - -content::DownloadDangerType DownloadDatabase::IntToDangerType(int danger_type) { - switch (danger_type) { - case DownloadDatabase::kDangerTypeNotDangerous: - return content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS; - case DownloadDatabase::kDangerTypeDangerousFile: - return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE; - case DownloadDatabase::kDangerTypeDangerousUrl: - return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL; - case DownloadDatabase::kDangerTypeDangerousContent: - return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT; - case DownloadDatabase::kDangerTypeMaybeDangerousContent: - return content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT; - case DownloadDatabase::kDangerTypeUncommonContent: - return content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT; - case DownloadDatabase::kDangerTypeUserValidated: - return content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED; - case DownloadDatabase::kDangerTypeDangerousHost: - return content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST; - case DownloadDatabase::kDangerTypePotentiallyUnwanted: - return content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED; - default: - return content::DOWNLOAD_DANGER_TYPE_MAX; - } -} - -DownloadDatabase::DownloadDatabase() +DownloadDatabase::DownloadDatabase( + DownloadInterruptReason download_interrupt_no_reason, + DownloadInterruptReason download_interrupt_crash) : owning_thread_set_(false), owning_thread_(0), - in_progress_entry_cleanup_completed_(false) { + in_progress_entry_cleanup_completed_(false), + download_interrupt_no_reason_(download_interrupt_no_reason), + download_interrupt_crash_(download_interrupt_crash) { } DownloadDatabase::~DownloadDatabase() { } -bool DownloadDatabase::EnsureColumnExists( - const std::string& name, const std::string& type) { +bool DownloadDatabase::EnsureColumnExists(const std::string& name, + const std::string& type) { std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type; return GetDB().DoesColumnExist("downloads", name.c_str()) || GetDB().Execute(add_col.c_str()); @@ -194,8 +93,8 @@ bool DownloadDatabase::MigrateMimeType() { bool DownloadDatabase::MigrateDownloadsState() { sql::Statement statement(GetDB().GetUniqueStatement( "UPDATE downloads SET state=? WHERE state=?")); - statement.BindInt(0, kStateInterrupted); - statement.BindInt(1, kStateBug140687); + statement.BindInt(0, DownloadStateToInt(DownloadState::INTERRUPTED)); + statement.BindInt(1, DownloadStateToInt(DownloadState::BUG_140687)); return statement.Run(); } @@ -250,8 +149,10 @@ bool DownloadDatabase::MigrateDownloadsReasonPathsAndDangerType() { " (end_time + 11644473600) * 1000000 END, " " opened " "FROM downloads_tmp")); - statement_populate.BindInt(0, content::DOWNLOAD_INTERRUPT_REASON_NONE); - statement_populate.BindInt(1, kDangerTypeNotDangerous); + statement_populate.BindInt( + 0, DownloadInterruptReasonToInt(download_interrupt_no_reason_)); + statement_populate.BindInt( + 1, DownloadDangerTypeToInt(DownloadDangerType::NOT_DANGEROUS)); if (!statement_populate.Run()) return false; @@ -296,7 +197,7 @@ bool DownloadDatabase::InitDownloadTable() { "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 + "interrupt_reason INTEGER NOT NULL," // DownloadInterruptReason "end_time INTEGER NOT NULL," // When the download completed. "opened INTEGER NOT NULL," // 1 if it has ever been opened // else 0 @@ -334,15 +235,20 @@ uint32 DownloadDatabase::GetNextDownloadId() { "SELECT max(id) FROM downloads")); bool result = select_max_id.Step(); DCHECK(result); - // If there are zero records in the downloads table, then max(id) will return - // 0 = kInvalidId, so GetNextDownloadId() will set *id = kInvalidId + 1. - // If there is at least one record but all of the |id|s are <= kInvalidId, - // then max(id) will return <= kInvalidId, so GetNextDownloadId should return - // kInvalidId + 1. Note that any records with |id <= kInvalidId| will be - // dropped in QueryDownloads() + // If there are zero records in the downloads table, then max(id) will + // return 0 = kInvalidDownloadId, so GetNextDownloadId() will set + // *id = kInvalidDownloadId + 1. + // + // If there is at least one record but all of the |id|s are + // <= kInvalidDownloadId, then max(id) will return <= kInvalidDownloadId, + // so GetNextDownloadId() should return kInvalidDownloadId + 1. + // + // Note that any records with |id <= kInvalidDownloadId| will be dropped in + // QueryDownloads(). + // // SQLITE doesn't have unsigned integers. return 1 + static_cast<uint32>(std::max( - static_cast<int64>(content::DownloadItem::kInvalidId), + static_cast<int64>(kInvalidDownloadId), select_max_id.ColumnInt64(0))); } @@ -350,8 +256,7 @@ bool DownloadDatabase::DropDownloadTable() { return GetDB().Execute("DROP TABLE downloads"); } -void DownloadDatabase::QueryDownloads( - std::vector<DownloadRow>* results) { +void DownloadDatabase::QueryDownloads(std::vector<DownloadRow>* results) { EnsureInProgressEntriesCleanedUp(); results->clear(); @@ -375,24 +280,25 @@ void DownloadDatabase::QueryDownloads( // |id|s instead of casting them to very large uint32s, which would break // the max(id) logic in GetNextDownloadId(). int64 signed_id = statement_main.ColumnInt64(column++); - info->id = static_cast<uint32>(signed_id); + info->id = IntToDownloadId(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->start_time = + base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); info->received_bytes = statement_main.ColumnInt64(column++); info->total_bytes = statement_main.ColumnInt64(column++); int state = statement_main.ColumnInt(column++); - info->state = IntToState(state); - if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) + info->state = IntToDownloadState(state); + if (info->state == DownloadState::INVALID) UMA_HISTOGRAM_COUNTS("Download.DatabaseInvalidState", state); - info->danger_type = IntToDangerType(statement_main.ColumnInt(column++)); - info->interrupt_reason = static_cast<content::DownloadInterruptReason>( - statement_main.ColumnInt(column++)); - info->end_time = base::Time::FromInternalValue( - statement_main.ColumnInt64(column++)); + info->danger_type = + IntToDownloadDangerType(statement_main.ColumnInt(column++)); + info->interrupt_reason = + IntToDownloadInterruptReason(statement_main.ColumnInt(column++)); + info->end_time = + base::Time::FromInternalValue(statement_main.ColumnInt64(column++)); info->opened = statement_main.ColumnInt(column++) != 0; info->referrer_url = GURL(statement_main.ColumnString(column++)); info->by_ext_id = statement_main.ColumnString(column++); @@ -403,15 +309,15 @@ void DownloadDatabase::QueryDownloads( // If the record is corrupted, note that and drop it. // http://crbug.com/251269 DroppedReason dropped_reason = DROPPED_REASON_MAX; - if (signed_id <= static_cast<int64>(content::DownloadItem::kInvalidId)) { + if (signed_id <= static_cast<int64>(kInvalidDownloadId)) { // SQLITE doesn't have unsigned integers. dropped_reason = DROPPED_REASON_BAD_ID; } else if (!ids.insert(info->id).second) { dropped_reason = DROPPED_REASON_DUPLICATE_ID; NOTREACHED() << info->id; - } else if (info->state == DownloadItem::MAX_DOWNLOAD_STATE) { + } else if (info->state == DownloadState::INVALID) { dropped_reason = DROPPED_REASON_BAD_STATE; - } else if (info->danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { + } else if (info->danger_type == DownloadDangerType::INVALID) { dropped_reason = DROPPED_REASON_BAD_DANGER_TYPE; } if (dropped_reason != DROPPED_REASON_MAX) { @@ -436,9 +342,9 @@ void DownloadDatabase::QueryDownloads( int64 signed_id = statement_chain.ColumnInt64(column++); int chain_index = statement_chain.ColumnInt(column++); - if (signed_id <= static_cast<int64>(content::DownloadItem::kInvalidId)) + if (signed_id <= static_cast<int64>(kInvalidDownloadId)) continue; - uint32 id = static_cast<uint32>(signed_id); + uint32 id = IntToDownloadId(signed_id); // Note that these DCHECKs may trip as a result of corrupted databases. // We have them because in debug builds the chances are higher there's @@ -486,14 +392,12 @@ void DownloadDatabase::QueryDownloads( bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { EnsureInProgressEntriesCleanedUp(); - DCHECK_NE(content::DownloadItem::kInvalidId, data.id); - int state = StateToInt(data.state); - if (state == kStateInvalid) { + DCHECK_NE(kInvalidDownloadId, data.id); + if (data.state == DownloadState::INVALID) { NOTREACHED(); return false; } - int danger_type = DangerTypeToInt(data.danger_type); - if (danger_type == kDangerTypeInvalid) { + if (data.danger_type == DownloadDangerType::INVALID) { NOTREACHED(); return false; } @@ -512,9 +416,10 @@ bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { 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); - statement.BindInt(column++, static_cast<int>(data.interrupt_reason)); + statement.BindInt(column++, DownloadStateToInt(data.state)); + statement.BindInt(column++, DownloadDangerTypeToInt(data.danger_type)); + statement.BindInt(column++, + DownloadInterruptReasonToInt(data.interrupt_reason)); statement.BindInt64(column++, data.end_time.ToInternalValue()); statement.BindInt64(column++, data.total_bytes); statement.BindInt(column++, (data.opened ? 1 : 0)); @@ -522,7 +427,7 @@ bool DownloadDatabase::UpdateDownload(const DownloadRow& data) { statement.BindString(column++, data.by_ext_name); statement.BindString(column++, data.etag); statement.BindString(column++, data.last_modified); - statement.BindInt(column++, data.id); + statement.BindInt(column++, DownloadIdToInt(data.id)); return statement.Run(); } @@ -533,27 +438,25 @@ void DownloadDatabase::EnsureInProgressEntriesCleanedUp() { sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, "UPDATE downloads SET state=?, interrupt_reason=? WHERE state=?")); - statement.BindInt(0, kStateInterrupted); - statement.BindInt(1, content::DOWNLOAD_INTERRUPT_REASON_CRASH); - statement.BindInt(2, kStateInProgress); + statement.BindInt(0, DownloadStateToInt(DownloadState::INTERRUPTED)); + statement.BindInt(1, DownloadInterruptReasonToInt(download_interrupt_crash_)); + statement.BindInt(2, DownloadStateToInt(DownloadState::IN_PROGRESS)); statement.Run(); in_progress_entry_cleanup_completed_ = true; } bool DownloadDatabase::CreateDownload(const DownloadRow& info) { - DCHECK_NE(content::DownloadItem::kInvalidId, info.id); + DCHECK_NE(kInvalidDownloadId, info.id); EnsureInProgressEntriesCleanedUp(); if (info.url_chain.empty()) return false; - int state = StateToInt(info.state); - if (state == kStateInvalid) + if (info.state == DownloadState::INVALID) return false; - int danger_type = DangerTypeToInt(info.danger_type); - if (danger_type == kDangerTypeInvalid) + if (info.danger_type == DownloadDangerType::INVALID) return false; { @@ -569,7 +472,7 @@ bool DownloadDatabase::CreateDownload(const DownloadRow& info) { "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); int column = 0; - statement_insert.BindInt(column++, info.id); + statement_insert.BindInt(column++, DownloadIdToInt(info.id)); BindFilePath(statement_insert, info.current_path, column++); BindFilePath(statement_insert, info.target_path, column++); statement_insert.BindString(column++, info.mime_type); @@ -577,9 +480,11 @@ bool DownloadDatabase::CreateDownload(const DownloadRow& info) { statement_insert.BindInt64(column++, info.start_time.ToInternalValue()); statement_insert.BindInt64(column++, info.received_bytes); statement_insert.BindInt64(column++, info.total_bytes); - statement_insert.BindInt(column++, state); - statement_insert.BindInt(column++, danger_type); - statement_insert.BindInt(column++, info.interrupt_reason); + statement_insert.BindInt(column++, DownloadStateToInt(info.state)); + statement_insert.BindInt(column++, + DownloadDangerTypeToInt(info.danger_type)); + statement_insert.BindInt( + column++, DownloadInterruptReasonToInt(info.interrupt_reason)); statement_insert.BindInt64(column++, info.end_time.ToInternalValue()); statement_insert.BindInt(column++, info.opened ? 1 : 0); statement_insert.BindString(column++, info.referrer_url.spec()); diff --git a/chrome/browser/history/download_database.h b/components/history/core/browser/download_database.h index 10ef59c..90d0b36 100644 --- a/chrome/browser/history/download_database.h +++ b/components/history/core/browser/download_database.h @@ -2,15 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ -#define CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ +#ifndef COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ +#define COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ #include <string> #include <vector> #include "base/gtest_prod_util.h" #include "base/threading/platform_thread.h" -#include "content/public/browser/download_item.h" +#include "components/history/core/browser/download_types.h" #include "sql/meta_table.h" namespace sql { @@ -25,14 +25,14 @@ struct DownloadRow; class DownloadDatabase { public: // Must call InitDownloadTable before using any other functions. - DownloadDatabase(); + DownloadDatabase(DownloadInterruptReason download_interrupt_no_reason, + DownloadInterruptReason download_interrupt_crash); virtual ~DownloadDatabase(); uint32 GetNextDownloadId(); // Get all the downloads from the database. - void QueryDownloads( - std::vector<DownloadRow>* results); + void QueryDownloads(std::vector<DownloadRow>* results); // Update the state of one download. Returns true if successful. // Does not update |url|, |start_time|; uses |id| only @@ -84,28 +84,8 @@ class DownloadDatabase { bool DropDownloadTable(); private: - FRIEND_TEST_ALL_PREFIXES( - HistoryBackendDBTest, ConfirmDownloadInProgressCleanup); - - // Values used in the database for DownloadItem::State. - static const int kStateInvalid; - static const int kStateInProgress; - static const int kStateComplete; - static const int kStateCancelled; - static const int kStateBug140687; - static const int kStateInterrupted; - - // Values used in the database for DownloadItem::DangerType - static const int kDangerTypeInvalid; - static const int kDangerTypeNotDangerous; - static const int kDangerTypeDangerousFile; - static const int kDangerTypeDangerousUrl; - static const int kDangerTypeDangerousContent; - static const int kDangerTypeMaybeDangerousContent; - static const int kDangerTypeUncommonContent; - static const int kDangerTypeUserValidated; - static const int kDangerTypeDangerousHost; - static const int kDangerTypePotentiallyUnwanted; + FRIEND_TEST_ALL_PREFIXES(HistoryBackendDBTest, + ConfirmDownloadInProgressCleanup); // Fixes state of the download entries. Sometimes entries with IN_PROGRESS // state are not updated during browser shutdown (particularly when crashing). @@ -118,13 +98,6 @@ class DownloadDatabase { void RemoveDownloadURLs(uint32 id); - // Utility functions for conversion between DownloadItem types - // and DownloadDatabase constants. - static int StateToInt(content::DownloadItem::DownloadState state); - static content::DownloadItem::DownloadState IntToState(int state); - static int DangerTypeToInt(content::DownloadDangerType danger_type); - static content::DownloadDangerType IntToDangerType(int danger_type); - bool owning_thread_set_; base::PlatformThreadId owning_thread_; @@ -135,9 +108,15 @@ class DownloadDatabase { // actually use the downloads database. bool in_progress_entry_cleanup_completed_; + // Those constants are defined in the embedder and injected into the + // database in the constructor. They represent the interrupt reason + // to use for respectively an undefined value and in case of a crash. + DownloadInterruptReason download_interrupt_no_reason_; + DownloadInterruptReason download_interrupt_crash_; + DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); }; } // namespace history -#endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ +#endif // COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ diff --git a/components/history/core/browser/download_row.cc b/components/history/core/browser/download_row.cc new file mode 100644 index 0000000..ffd02e5 --- /dev/null +++ b/components/history/core/browser/download_row.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2011 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 "components/history/core/browser/download_row.h" + +#include "components/history/core/browser/download_constants.h" + +namespace history { + +DownloadRow::DownloadRow() + : received_bytes(0), + total_bytes(0), + state(DownloadState::IN_PROGRESS), + danger_type(DownloadDangerType::NOT_DANGEROUS), + id(kInvalidDownloadId), + opened(false) { + // |interrupt_reason| is left undefined by this constructor as the value + // has no meaning unless |state| is equal to kStateInterrupted. +} + +DownloadRow::DownloadRow(const base::FilePath& current_path, + 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, + const std::string& last_modified, + int64 received, + int64 total, + DownloadState download_state, + DownloadDangerType danger_type, + DownloadInterruptReason interrupt_reason, + DownloadId id, + bool download_opened, + const std::string& ext_id, + const std::string& ext_name) + : current_path(current_path), + 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), + last_modified(last_modified), + received_bytes(received), + total_bytes(total), + state(download_state), + danger_type(danger_type), + interrupt_reason(interrupt_reason), + id(id), + opened(download_opened), + by_ext_id(ext_id), + by_ext_name(ext_name) { +} + +DownloadRow::~DownloadRow() { +} + +} // namespace history diff --git a/chrome/browser/history/download_row.h b/components/history/core/browser/download_row.h index a0c094f..0553a67 100644 --- a/chrome/browser/history/download_row.h +++ b/components/history/core/browser/download_row.h @@ -2,17 +2,15 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_ROW_H_ -#define CHROME_BROWSER_HISTORY_DOWNLOAD_ROW_H_ +#ifndef COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_ROW_H_ +#define COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_ROW_H_ #include <string> #include <vector> #include "base/files/file_path.h" #include "base/time/time.h" -#include "content/public/browser/download_danger_type.h" -#include "content/public/browser/download_interrupt_reasons.h" -#include "content/public/browser/download_item.h" +#include "components/history/core/browser/download_types.h" #include "url/gurl.h" namespace history { @@ -22,26 +20,25 @@ namespace history { // DownloadDatabase through the HistoryService. struct DownloadRow { DownloadRow(); - DownloadRow( - const base::FilePath& current_path, - 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, - const std::string& last_modified, - int64 received, - int64 total, - content::DownloadItem::DownloadState download_state, - content::DownloadDangerType danger_type, - content::DownloadInterruptReason interrupt_reason, - uint32 id, - bool download_opened, - const std::string& ext_id, - const std::string& ext_name); + DownloadRow(const base::FilePath& current_path, + 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, + const std::string& last_modified, + int64 received, + int64 total, + DownloadState download_state, + DownloadDangerType danger_type, + DownloadInterruptReason interrupt_reason, + DownloadId id, + bool download_opened, + const std::string& ext_id, + const std::string& ext_name); ~DownloadRow(); // The current path to the download (potentially different from final if @@ -86,17 +83,16 @@ struct DownloadRow { int64 total_bytes; // The current state of the download. - content::DownloadItem::DownloadState state; + DownloadState state; // Whether and how the download is dangerous. - content::DownloadDangerType danger_type; + DownloadDangerType danger_type; - // The reason the download was interrupted, if - // state == DownloadItem::INTERRUPTED - content::DownloadInterruptReason interrupt_reason; + // The reason the download was interrupted, if state == kStateInterrupted. + DownloadInterruptReason interrupt_reason; // The id of the download in the database. Is not changed by UpdateDownload(). - uint32 id; + DownloadId id; // Whether this download has ever been opened from the browser. bool opened; @@ -108,4 +104,4 @@ struct DownloadRow { } // namespace history -#endif // CHROME_BROWSER_HISTORY_DOWNLOAD_ROW_H_ +#endif // COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_ROW_H_ diff --git a/components/history/core/browser/download_types.cc b/components/history/core/browser/download_types.cc new file mode 100644 index 0000000..ccc2018 --- /dev/null +++ b/components/history/core/browser/download_types.cc @@ -0,0 +1,129 @@ +// Copyright 2015 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 "components/history/core/browser/download_types.h" + +#include <ostream> + +#include "base/logging.h" +#include "components/history/core/browser/download_constants.h" + +namespace history { + +DownloadState IntToDownloadState(int state) { + switch (static_cast<DownloadState>(state)) { + case DownloadState::IN_PROGRESS: + case DownloadState::COMPLETE: + case DownloadState::CANCELLED: + case DownloadState::INTERRUPTED: + return static_cast<DownloadState>(state); + + case DownloadState::INVALID: + case DownloadState::BUG_140687: + NOTREACHED(); + return DownloadState::INVALID; + } + NOTREACHED(); + return DownloadState::INVALID; +} + +int DownloadStateToInt(DownloadState state) { + DCHECK_NE(state, DownloadState::INVALID); + return static_cast<int>(state); +} + +std::ostream& operator<<(std::ostream& stream, DownloadState state) { + switch (state) { + case DownloadState::INVALID: + return stream << "history::DownloadState::COMPLETE"; + case DownloadState::IN_PROGRESS: + return stream << "history::DownloadState::IN_PROGRESS"; + case DownloadState::COMPLETE: + return stream << "history::DownloadState::COMPLETE"; + case DownloadState::CANCELLED: + return stream << "history::DownloadState::CANCELLED"; + case DownloadState::BUG_140687: + return stream << "history::DownloadState::BUG_140687"; + case DownloadState::INTERRUPTED: + return stream << "history::DownloadState::INTERRUPTED"; + } + NOTREACHED(); + return stream; +} + +DownloadDangerType IntToDownloadDangerType(int danger_type) { + switch (static_cast<DownloadDangerType>(danger_type)) { + case DownloadDangerType::NOT_DANGEROUS: + case DownloadDangerType::DANGEROUS_FILE: + case DownloadDangerType::DANGEROUS_URL: + case DownloadDangerType::DANGEROUS_CONTENT: + case DownloadDangerType::MAYBE_DANGEROUS_CONTENT: + case DownloadDangerType::UNCOMMON_CONTENT: + case DownloadDangerType::USER_VALIDATED: + case DownloadDangerType::DANGEROUS_HOST: + case DownloadDangerType::POTENTIALLY_UNWANTED: + return static_cast<DownloadDangerType>(danger_type); + + case DownloadDangerType::INVALID: + NOTREACHED(); + return DownloadDangerType::INVALID; + } + NOTREACHED(); + return DownloadDangerType::INVALID; +} + +int DownloadDangerTypeToInt(DownloadDangerType danger_type) { + DCHECK_NE(danger_type, DownloadDangerType::INVALID); + return static_cast<int>(danger_type); +} + +std::ostream& operator<<(std::ostream& stream, DownloadDangerType danger_type) { + switch (danger_type) { + case DownloadDangerType::INVALID: + return stream << "history::DownloadDangerType::INVALID"; + case DownloadDangerType::NOT_DANGEROUS: + return stream << "history::DownloadDangerType::NOT_DANGEROUS"; + case DownloadDangerType::DANGEROUS_FILE: + return stream << "history::DownloadDangerType::DANGEROUS_FILE"; + case DownloadDangerType::DANGEROUS_URL: + return stream << "history::DownloadDangerType::DANGEROUS_URL"; + case DownloadDangerType::DANGEROUS_CONTENT: + return stream << "history::DownloadDangerType::DANGEROUS_CONTENT"; + case DownloadDangerType::MAYBE_DANGEROUS_CONTENT: + return stream << "history::DownloadDangerType::MAYBE_DANGEROUS_CONTENT"; + case DownloadDangerType::UNCOMMON_CONTENT: + return stream << "history::DownloadDangerType::UNCOMMON_CONTENT"; + case DownloadDangerType::USER_VALIDATED: + return stream << "history::DownloadDangerType::USER_VALIDATED"; + case DownloadDangerType::DANGEROUS_HOST: + return stream << "history::DownloadDangerType::DANGEROUS_HOST"; + case DownloadDangerType::POTENTIALLY_UNWANTED: + return stream << "history::DownloadDangerType::POTENTIALLY_UNWANTED"; + } + NOTREACHED(); + return stream; +} + +DownloadInterruptReason IntToDownloadInterruptReason(int interrupt_reason) { + return static_cast<DownloadInterruptReason>(interrupt_reason); +} + +int DownloadInterruptReasonToInt(DownloadInterruptReason interrupt_reason) { + return static_cast<int>(interrupt_reason); +} + +const DownloadId kInvalidDownloadId = 0; + +DownloadId IntToDownloadId(int64_t id) { + DCHECK_GE(id, static_cast<int64_t>(0)); + DCHECK_NE(id, static_cast<int64_t>(kInvalidDownloadId)); + return static_cast<DownloadId>(id); +} + +int64_t DownloadIdToInt(DownloadId id) { + DCHECK_NE(id, kInvalidDownloadId); + return static_cast<int64_t>(id); +} + +} // namespace history diff --git a/components/history/core/browser/download_types.h b/components/history/core/browser/download_types.h new file mode 100644 index 0000000..1c6650d --- /dev/null +++ b/components/history/core/browser/download_types.h @@ -0,0 +1,62 @@ +// Copyright 2015 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. + +#ifndef COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_TYPES_H_ +#define COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_TYPES_H_ + +#include <stdint.h> +#include <iosfwd> + +namespace history { + +// DownloadState represents the state of a DownloadRow saved into the +// DownloadDatabase. The values must not be changed as they are saved +// to disk in the database. +enum class DownloadState; + +// Utility functions to convert between int and DownloadState for +// serialization to the download database. +DownloadState IntToDownloadState(int state); +int DownloadStateToInt(DownloadState state); + +// operator<< is defined to allow DownloadState to work with DCHECK/EXPECT. +std::ostream& operator<<(std::ostream& stream, DownloadState state); + +// DownloadDangerType represents the danger of a DownloadRow into the +// DownloadDatabase. The values must not be changed as they are saved +// to disk in the database. +enum class DownloadDangerType; + +// Utility functions to convert between int and DownloadDangerType for +// serialization to the download database. +DownloadDangerType IntToDownloadDangerType(int danger_type); +int DownloadDangerTypeToInt(DownloadDangerType danger_type); + +// operator<< is defined to allow DownloadDangerType to work with DCHECK/EXPECT. +std::ostream& operator<<(std::ostream& stream, DownloadDangerType danger_type); + +// DownloadInterruptReason represents the reason a download was interrupted +// of a DownloadRow into the DownloadDatabase. The values must not be changed +// as they are saved to disk in the database. They have no meaning for the +// history component. +typedef int32_t DownloadInterruptReason; + +// Utility functions to convert between int and DownloadInterruptReason for +// serialization to the download database. +DownloadInterruptReason IntToDownloadInterruptReason(int interrupt_reason); +int DownloadInterruptReasonToInt(DownloadInterruptReason interrupt_reason); + +// DownloadId represents the id of a DownloadRow into the DownloadDatabase. +// The value is controlled by the embedder except for the reserved id +// kInvalidDownloadId. +typedef uint32_t DownloadId; + +// Utility functions to convert between int and DownloadId for +// serialization to the download database. +DownloadId IntToDownloadId(int64_t id); +int64_t DownloadIdToInt(DownloadId id); + +} // namespace history + +#endif // COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_TYPES_H_ |