diff options
author | sdefresne@chromium.org <sdefresne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-04 14:43:52 +0000 |
---|---|---|
committer | sdefresne@chromium.org <sdefresne@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-04 14:43:52 +0000 |
commit | ad34610c7c216f84e25de15d87215ca049b13404 (patch) | |
tree | b301146b6ab0e2f6a88c3a8d8135723b7ba3b81f | |
parent | bfd666154471a2d18da5362b852d0931c99e21e0 (diff) | |
download | chromium_src-ad34610c7c216f84e25de15d87215ca049b13404.zip chromium_src-ad34610c7c216f84e25de15d87215ca049b13404.tar.gz chromium_src-ad34610c7c216f84e25de15d87215ca049b13404.tar.bz2 |
Abstract history dependencies on bookmarks through HistoryClient
Add methods to abstract BookmarkService and BookmarkModel methods used
by HistoryService through the HistoryClient interface.
BUG=371825
TBR=sky
Review URL: https://codereview.chromium.org/285233012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274824 0039d316-1c4b-4281-b951-d872f2087c98
33 files changed, 529 insertions, 291 deletions
diff --git a/chrome/browser/history/android/android_provider_backend.cc b/chrome/browser/history/android/android_provider_backend.cc index 6a9449b..fb1cb38 100644 --- a/chrome/browser/history/android/android_provider_backend.cc +++ b/chrome/browser/history/android/android_provider_backend.cc @@ -16,7 +16,7 @@ #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/history_database.h" #include "chrome/browser/history/thumbnail_database.h" -#include "components/bookmarks/browser/bookmark_service.h" +#include "components/history/core/browser/history_client.h" #include "content/public/common/page_transition_types.h" #include "sql/connection.h" @@ -215,13 +215,13 @@ AndroidProviderBackend::AndroidProviderBackend( const base::FilePath& db_name, HistoryDatabase* history_db, ThumbnailDatabase* thumbnail_db, - BookmarkService* bookmark_service, + HistoryClient* history_client, HistoryBackend::Delegate* delegate) : android_cache_db_filename_(db_name), db_(&history_db->GetDB()), history_db_(history_db), thumbnail_db_(thumbnail_db), - bookmark_service_(bookmark_service), + history_client_(history_client), initialized_(false), delegate_(delegate) { DCHECK(delegate_); @@ -793,20 +793,19 @@ bool AndroidProviderBackend::UpdateRemovedURLs() { } bool AndroidProviderBackend::UpdateBookmarks() { - if (bookmark_service_ == NULL) { - LOG(ERROR) << "Bookmark service is not available"; + if (history_client_ == NULL) { + LOG(ERROR) << "HistoryClient is not available"; return false; } - bookmark_service_->BlockTillLoaded(); - std::vector<BookmarkService::URLAndTitle> bookmarks; - bookmark_service_->GetBookmarks(&bookmarks); + std::vector<URLAndTitle> bookmarks; + history_client_->GetBookmarks(&bookmarks); if (bookmarks.empty()) return true; std::vector<URLID> url_ids; - for (std::vector<BookmarkService::URLAndTitle>::const_iterator i = + for (std::vector<URLAndTitle>::const_iterator i = bookmarks.begin(); i != bookmarks.end(); ++i) { URLID url_id = history_db_->GetRowForURL(i->url, NULL); if (url_id == 0) { diff --git a/chrome/browser/history/android/android_provider_backend.h b/chrome/browser/history/android/android_provider_backend.h index 38c22d8..02dff9b 100644 --- a/chrome/browser/history/android/android_provider_backend.h +++ b/chrome/browser/history/android/android_provider_backend.h @@ -21,12 +21,11 @@ #include "sql/statement.h" #include "sql/transaction.h" -class BookmarkService; - namespace history { class AndroidProviderBackend; class AndroidURLsSQLHandler; +class HistoryClient; class HistoryDatabase; class ThumbnailDatabase; @@ -49,7 +48,7 @@ class AndroidProviderBackend { AndroidProviderBackend(const base::FilePath& cache_db_name, HistoryDatabase* history_db, ThumbnailDatabase* thumbnail_db, - BookmarkService* bookmark_service, + HistoryClient* history_client_, HistoryBackend::Delegate* delegate); ~AndroidProviderBackend(); @@ -351,7 +350,7 @@ class AndroidProviderBackend { ThumbnailDatabase* thumbnail_db_; - BookmarkService* bookmark_service_; + HistoryClient* history_client_; // Whether AndroidProviderBackend has been initialized. bool initialized_; diff --git a/chrome/browser/history/android/android_provider_backend_unittest.cc b/chrome/browser/history/android/android_provider_backend_unittest.cc index cd86c07..4eb1e61 100644 --- a/chrome/browser/history/android/android_provider_backend_unittest.cc +++ b/chrome/browser/history/android/android_provider_backend_unittest.cc @@ -16,6 +16,8 @@ #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/favicon/favicon_changed_details.h" #include "chrome/browser/history/android/android_time.h" +#include "chrome/browser/history/chrome_history_client.h" +#include "chrome/browser/history/chrome_history_client_factory.h" #include "chrome/browser/history/history_backend.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/common/chrome_constants.h" @@ -131,6 +133,8 @@ class AndroidProviderBackendTest : public testing::Test { chrome::kInitialProfile); testing_profile->CreateBookmarkModel(true); bookmark_model_ = BookmarkModelFactory::GetForProfile(testing_profile); + history_client_ = + ChromeHistoryClientFactory::GetForProfile(testing_profile); test::WaitForBookmarkModelToLoad(bookmark_model_); ASSERT_TRUE(bookmark_model_); @@ -202,7 +206,7 @@ class AndroidProviderBackendTest : public testing::Test { base::MessageLoopForUI message_loop_; content::TestBrowserThread ui_thread_; content::TestBrowserThread file_thread_; - + ChromeHistoryClient* history_client_; DISALLOW_COPY_AND_ASSIGN(AndroidProviderBackendTest); }; @@ -238,7 +242,7 @@ TEST_F(AndroidProviderBackendTest, UpdateTables) { { scoped_refptr<HistoryBackend> history_backend; history_backend = new HistoryBackend( - temp_dir_.path(), new AndroidProviderBackendDelegate(), bookmark_model_); + temp_dir_.path(), new AndroidProviderBackendDelegate(), history_client_); history_backend->Init(std::string(), false); history_backend->AddVisits(url1, visits1, history::SOURCE_SYNCED); history_backend->AddVisits(url2, visits2, history::SOURCE_SYNCED); @@ -274,8 +278,11 @@ TEST_F(AndroidProviderBackendTest, UpdateTables) { // Set url1 as bookmark. AddBookmark(url1); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); ASSERT_TRUE(backend->EnsureInitializedAndUpdated()); @@ -383,7 +390,7 @@ TEST_F(AndroidProviderBackendTest, QueryHistoryAndBookmarks) { { scoped_refptr<HistoryBackend> history_backend; history_backend = new HistoryBackend( - temp_dir_.path(), new AndroidProviderBackendDelegate(), bookmark_model_); + temp_dir_.path(), new AndroidProviderBackendDelegate(), history_client_); history_backend->Init(std::string(), false); history_backend->AddVisits(url1, visits1, history::SOURCE_SYNCED); history_backend->AddVisits(url2, visits2, history::SOURCE_SYNCED); @@ -425,8 +432,11 @@ TEST_F(AndroidProviderBackendTest, QueryHistoryAndBookmarks) { AddBookmark(url1); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); std::vector<HistoryAndBookmarkRow::ColumnID> projections; @@ -511,8 +521,11 @@ TEST_F(AndroidProviderBackendTest, InsertHistoryAndBookmark) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row1)); EXPECT_FALSE(delegate_.deleted_details()); @@ -620,8 +633,11 @@ TEST_F(AndroidProviderBackendTest, DeleteHistoryAndBookmarks) { ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row1)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row2)); @@ -716,8 +732,11 @@ TEST_F(AndroidProviderBackendTest, IsValidHistoryAndBookmarkRow) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // The created time and last visit time are too close to have required visit // count. @@ -806,8 +825,11 @@ TEST_F(AndroidProviderBackendTest, UpdateURL) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -985,8 +1007,11 @@ TEST_F(AndroidProviderBackendTest, UpdateVisitCount) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -1065,8 +1090,11 @@ TEST_F(AndroidProviderBackendTest, UpdateLastVisitTime) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -1126,8 +1154,11 @@ TEST_F(AndroidProviderBackendTest, UpdateFavicon) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -1191,8 +1222,11 @@ TEST_F(AndroidProviderBackendTest, UpdateSearchTermTable) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // Insert a keyword search item to verify if the update succeeds. HistoryAndBookmarkRow row1; row1.set_raw_url("cnn.com"); @@ -1269,8 +1303,11 @@ TEST_F(AndroidProviderBackendTest, QuerySearchTerms) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // Insert a keyword search item to verify if we can find it. HistoryAndBookmarkRow row1; row1.set_raw_url("cnn.com"); @@ -1303,8 +1340,11 @@ TEST_F(AndroidProviderBackendTest, UpdateSearchTerms) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // Insert a keyword. HistoryAndBookmarkRow row1; row1.set_raw_url("cnn.com"); @@ -1407,8 +1447,11 @@ TEST_F(AndroidProviderBackendTest, DeleteSearchTerms) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // Insert a keyword. HistoryAndBookmarkRow row1; row1.set_raw_url("cnn.com"); @@ -1513,8 +1556,11 @@ TEST_F(AndroidProviderBackendTest, InsertSearchTerm) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); SearchRow search_row; search_row.set_search_term(UTF8ToUTF16("google")); search_row.set_url(GURL("http://google.com")); @@ -1567,8 +1613,11 @@ TEST_F(AndroidProviderBackendTest, DeleteHistory) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -1620,8 +1669,11 @@ TEST_F(AndroidProviderBackendTest, TestMultipleNestingTransaction) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); // Create the nested transactions. history_db_.BeginTransaction(); @@ -1670,8 +1722,11 @@ TEST_F(AndroidProviderBackendTest, TestAndroidCTSComplianceForZeroVisitCount) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); URLRow url_row(GURL("http://www.google.com")); url_row.set_last_visit(Time::Now()); url_row.set_visit_count(0); @@ -1707,8 +1762,11 @@ TEST_F(AndroidProviderBackendTest, AndroidCTSComplianceFolderColumnExists) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db_.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - &thumbnail_db_, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + &thumbnail_db_, + history_client_, + &delegate_)); HistoryAndBookmarkRow row1; row1.set_raw_url("cnn.com"); row1.set_url(GURL("http://cnn.com")); @@ -1783,7 +1841,7 @@ TEST_F(AndroidProviderBackendTest, QueryWithoutThumbnailDB) { { scoped_refptr<HistoryBackend> history_backend; history_backend = new HistoryBackend( - temp_dir_.path(), new AndroidProviderBackendDelegate(), bookmark_model_); + temp_dir_.path(), new AndroidProviderBackendDelegate(), history_client_); history_backend->Init(std::string(), false); history_backend->AddVisits(url1, visits1, history::SOURCE_SYNCED); history_backend->AddVisits(url2, visits2, history::SOURCE_SYNCED); @@ -1826,8 +1884,11 @@ TEST_F(AndroidProviderBackendTest, QueryWithoutThumbnailDB) { AddBookmark(url1); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, NULL, - bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + NULL, + history_client_, + &delegate_)); std::vector<HistoryAndBookmarkRow::ColumnID> projections; @@ -1896,8 +1957,11 @@ TEST_F(AndroidProviderBackendTest, InsertWithoutThumbnailDB) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, NULL, - bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + NULL, + history_client_, + &delegate_)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row1)); EXPECT_FALSE(delegate_.deleted_details()); @@ -1960,8 +2024,11 @@ TEST_F(AndroidProviderBackendTest, DeleteWithoutThumbnailDB) { ASSERT_EQ(sql::INIT_OK, thumbnail_db.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db, - &thumbnail_db, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db, + &thumbnail_db, + history_client_, + &delegate_)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row1)); ASSERT_TRUE(backend->InsertHistoryAndBookmark(row2)); @@ -1975,8 +2042,11 @@ TEST_F(AndroidProviderBackendTest, DeleteWithoutThumbnailDB) { } ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, - NULL, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + NULL, + history_client_, + &delegate_)); // Delete all rows. std::vector<base::string16> args; @@ -2030,8 +2100,11 @@ TEST_F(AndroidProviderBackendTest, UpdateFaviconWithoutThumbnail) { ASSERT_EQ(sql::INIT_OK, history_db.Init(history_db_name_)); ASSERT_EQ(sql::INIT_OK, thumbnail_db.Init(thumbnail_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db, - &thumbnail_db, bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db, + &thumbnail_db, + history_client_, + &delegate_)); AndroidURLID id1 = backend->InsertHistoryAndBookmark(row1); ASSERT_TRUE(id1); @@ -2039,8 +2112,11 @@ TEST_F(AndroidProviderBackendTest, UpdateFaviconWithoutThumbnail) { ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name_)); scoped_ptr<AndroidProviderBackend> backend( - new AndroidProviderBackend(android_cache_db_name_, &history_db_, NULL, - bookmark_model_, &delegate_)); + new AndroidProviderBackend(android_cache_db_name_, + &history_db_, + NULL, + history_client_, + &delegate_)); int update_count; std::vector<base::string16> update_args; diff --git a/chrome/browser/history/chrome_history_client.cc b/chrome/browser/history/chrome_history_client.cc index d6310c8..61f4b21 100644 --- a/chrome/browser/history/chrome_history_client.cc +++ b/chrome/browser/history/chrome_history_client.cc @@ -4,5 +4,44 @@ #include "chrome/browser/history/chrome_history_client.h" -ChromeHistoryClient::ChromeHistoryClient() { +#include "base/logging.h" +#include "components/bookmarks/browser/bookmark_model.h" + +ChromeHistoryClient::ChromeHistoryClient(BookmarkModel* bookmark_model) + : bookmark_model_(bookmark_model) { + DCHECK(bookmark_model_); +} + +void ChromeHistoryClient::BlockUntilBookmarksLoaded() { + bookmark_model_->BlockTillLoaded(); +} + +bool ChromeHistoryClient::IsBookmarked(const GURL& url) { + return bookmark_model_->IsBookmarked(url); +} + +void ChromeHistoryClient::GetBookmarks( + std::vector<history::URLAndTitle>* bookmarks) { + std::vector<BookmarkModel::URLAndTitle> bookmarks_url_and_title; + bookmark_model_->GetBookmarks(&bookmarks_url_and_title); + + bookmarks->reserve(bookmarks->size() + bookmarks_url_and_title.size()); + for (size_t i = 0; i < bookmarks_url_and_title.size(); ++i) { + history::URLAndTitle value = { + bookmarks_url_and_title[i].url, + bookmarks_url_and_title[i].title, + }; + bookmarks->push_back(value); + } +} + +void ChromeHistoryClient::Shutdown() { + // It's possible that bookmarks haven't loaded and history is waiting for + // bookmarks to complete loading. In such a situation history can't shutdown + // (meaning if we invoked HistoryService::Cleanup now, we would deadlock). To + // break the deadlock we tell BookmarkModel it's about to be deleted so that + // it can release the signal history is waiting on, allowing history to + // shutdown (HistoryService::Cleanup to complete). In such a scenario history + // sees an incorrect view of bookmarks, but it's better than a deadlock. + bookmark_model_->Shutdown(); } diff --git a/chrome/browser/history/chrome_history_client.h b/chrome/browser/history/chrome_history_client.h index f6f8d35..24fc1d3 100644 --- a/chrome/browser/history/chrome_history_client.h +++ b/chrome/browser/history/chrome_history_client.h @@ -8,11 +8,27 @@ #include "base/macros.h" #include "components/history/core/browser/history_client.h" +class BookmarkModel; + +// This class implements history::HistoryClient to abstract operations that +// depend on Chrome environment. class ChromeHistoryClient : public history::HistoryClient { public: - ChromeHistoryClient(); + explicit ChromeHistoryClient(BookmarkModel* bookmark_model); + + // history::HistoryClient: + virtual void BlockUntilBookmarksLoaded() OVERRIDE; + virtual bool IsBookmarked(const GURL& url) OVERRIDE; + virtual void GetBookmarks( + std::vector<history::URLAndTitle>* bookmarks) OVERRIDE; + + // KeyedService: + virtual void Shutdown() OVERRIDE; + + private: + // The BookmarkModel, this should outlive ChromeHistoryClient. + BookmarkModel* bookmark_model_; - protected: DISALLOW_COPY_AND_ASSIGN(ChromeHistoryClient); }; diff --git a/chrome/browser/history/chrome_history_client_factory.cc b/chrome/browser/history/chrome_history_client_factory.cc index 5e89dd6..b56a898 100644 --- a/chrome/browser/history/chrome_history_client_factory.cc +++ b/chrome/browser/history/chrome_history_client_factory.cc @@ -4,6 +4,8 @@ #include "chrome/browser/history/chrome_history_client_factory.h" +#include "base/memory/singleton.h" +#include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/history/chrome_history_client.h" #include "chrome/browser/profiles/incognito_helpers.h" #include "chrome/browser/profiles/profile.h" @@ -25,6 +27,7 @@ ChromeHistoryClientFactory::ChromeHistoryClientFactory() : BrowserContextKeyedServiceFactory( "ChromeHistoryClient", BrowserContextDependencyManager::GetInstance()) { + DependsOn(BookmarkModelFactory::GetInstance()); } ChromeHistoryClientFactory::~ChromeHistoryClientFactory() { @@ -32,7 +35,8 @@ ChromeHistoryClientFactory::~ChromeHistoryClientFactory() { KeyedService* ChromeHistoryClientFactory::BuildServiceInstanceFor( content::BrowserContext* context) const { - return new ChromeHistoryClient(); + return new ChromeHistoryClient( + BookmarkModelFactory::GetForProfile(static_cast<Profile*>(context))); } content::BrowserContext* ChromeHistoryClientFactory::GetBrowserContextToUse( diff --git a/chrome/browser/history/chrome_history_client_factory.h b/chrome/browser/history/chrome_history_client_factory.h index 6278bf9..892f8de 100644 --- a/chrome/browser/history/chrome_history_client_factory.h +++ b/chrome/browser/history/chrome_history_client_factory.h @@ -5,9 +5,11 @@ #ifndef CHROME_BROWSER_HISTORY_CHROME_HISTORY_CLIENT_FACTORY_H_ #define CHROME_BROWSER_HISTORY_CHROME_HISTORY_CLIENT_FACTORY_H_ -#include "base/memory/singleton.h" #include "components/keyed_service/content/browser_context_keyed_service_factory.h" +template <typename T> +struct DefaultSingletonTraits; + class ChromeHistoryClient; class Profile; diff --git a/chrome/browser/history/expire_history_backend.cc b/chrome/browser/history/expire_history_backend.cc index 7ffede7..a1e4403 100644 --- a/chrome/browser/history/expire_history_backend.cc +++ b/chrome/browser/history/expire_history_backend.cc @@ -19,7 +19,7 @@ #include "chrome/browser/history/history_database.h" #include "chrome/browser/history/history_notifications.h" #include "chrome/browser/history/thumbnail_database.h" -#include "components/bookmarks/browser/bookmark_service.h" +#include "components/history/core/browser/history_client.h" namespace history { @@ -155,13 +155,13 @@ ExpireHistoryBackend::DeleteEffects::~DeleteEffects() { ExpireHistoryBackend::ExpireHistoryBackend( BroadcastNotificationDelegate* delegate, - BookmarkService* bookmark_service) + HistoryClient* history_client) : delegate_(delegate), main_db_(NULL), archived_db_(NULL), thumb_db_(NULL), weak_factory_(this), - bookmark_service_(bookmark_service) { + history_client_(history_client) { } ExpireHistoryBackend::~ExpireHistoryBackend() { @@ -184,6 +184,7 @@ void ExpireHistoryBackend::DeleteURLs(const std::vector<GURL>& urls) { return; DeleteEffects effects; + HistoryClient* history_client = GetHistoryClient(); for (std::vector<GURL>::const_iterator url = urls.begin(); url != urls.end(); ++url) { URLRow url_row; @@ -203,9 +204,8 @@ void ExpireHistoryBackend::DeleteURLs(const std::vector<GURL>& urls) { // URL, and not starting with visits in a given time range). We // therefore need to call the deletion and favicon update // functions manually. - BookmarkService* bookmark_service = GetBookmarkService(); DeleteOneURL(url_row, - bookmark_service && bookmark_service->IsBookmarked(*url), + history_client && history_client->IsBookmarked(*url), &effects); } @@ -464,7 +464,7 @@ void ExpireHistoryBackend::ExpireURLsForVisits(const VisitVector& visits, } // Check each unique URL with deleted visits. - BookmarkService* bookmark_service = GetBookmarkService(); + HistoryClient* history_client = GetHistoryClient(); for (std::map<URLID, ChangedURL>::const_iterator i = changed_urls.begin(); i != changed_urls.end(); ++i) { // The unique URL rows should already be filled in. @@ -483,7 +483,7 @@ void ExpireHistoryBackend::ExpireURLsForVisits(const VisitVector& visits, // Don't delete URLs with visits still in the DB, or bookmarked. bool is_bookmarked = - (bookmark_service && bookmark_service->IsBookmarked(url_row.url())); + (history_client && history_client->IsBookmarked(url_row.url())); if (!is_bookmarked && url_row.last_visit().is_null()) { // Not bookmarked and no more visits. Nuke the url. DeleteOneURL(url_row, is_bookmarked, effects); @@ -628,14 +628,13 @@ void ExpireHistoryBackend::ParanoidExpireHistory() { // TODO(brettw): Bug 1067331: write this to clean up any errors. } -BookmarkService* ExpireHistoryBackend::GetBookmarkService() { - // We use the bookmark service to determine if a URL is bookmarked. The - // bookmark service is loaded on a separate thread and may not be done by the - // time we get here. We therefor block until the bookmarks have finished - // loading. - if (bookmark_service_) - bookmark_service_->BlockTillLoaded(); - return bookmark_service_; +HistoryClient* ExpireHistoryBackend::GetHistoryClient() { + // We use the history client to determine if a URL is bookmarked. The data is + // loaded on a separate thread and may not be done by the time we get here. + // We therefore block until the bookmarks have finished loading. + if (history_client_) + history_client_->BlockUntilBookmarksLoaded(); + return history_client_; } } // namespace history diff --git a/chrome/browser/history/expire_history_backend.h b/chrome/browser/history/expire_history_backend.h index e88da3e..1e631cdc 100644 --- a/chrome/browser/history/expire_history_backend.h +++ b/chrome/browser/history/expire_history_backend.h @@ -16,13 +16,13 @@ #include "base/time/time.h" #include "chrome/browser/history/history_types.h" -class BookmarkService; class GURL; class TestingProfile; namespace history { class ArchivedDatabase; +class HistoryClient; class HistoryDatabase; struct HistoryDetails; class ThumbnailDatabase; @@ -66,11 +66,11 @@ typedef std::vector<const ExpiringVisitsReader*> ExpiringVisitsReaders; class ExpireHistoryBackend { public: // The delegate pointer must be non-NULL. We will NOT take ownership of it. - // BookmarkService may be NULL. The BookmarkService is used when expiring - // URLs so that we don't remove any URLs or favicons that are bookmarked - // (visits are removed though). + // HistoryClient may be NULL. The HistoryClient is used when expiring URLS so + // that we don't remove any URLs or favicons that are bookmarked (visits are + // removed though). ExpireHistoryBackend(BroadcastNotificationDelegate* delegate, - BookmarkService* bookmark_service); + HistoryClient* history_client); ~ExpireHistoryBackend(); // Completes initialization by setting the databases that this class will use. @@ -251,9 +251,9 @@ class ExpireHistoryBackend { // and deletes items. For example, URLs with no visits. void ParanoidExpireHistory(); - // Returns the BookmarkService, blocking until it is loaded. This may return - // NULL. - BookmarkService* GetBookmarkService(); + // Returns the HistoryClient, blocking until the bookmarks are loaded. This + // may return NULL during testing. + HistoryClient* GetHistoryClient(); // Initializes periodic expiration work queue by populating it with with tasks // for all known readers. @@ -298,11 +298,11 @@ class ExpireHistoryBackend { scoped_ptr<ExpiringVisitsReader> all_visits_reader_; scoped_ptr<ExpiringVisitsReader> auto_subframe_visits_reader_; - // The BookmarkService; may be null. This is owned by the Profile. + // The HistoryClient; may be NULL. // - // Use GetBookmarkService to access this, which makes sure the service is - // loaded. - BookmarkService* bookmark_service_; + // Use GetHistoryClient to access this, which makes sure the bookmarks are + // loaded before returning. + HistoryClient* history_client_; DISALLOW_COPY_AND_ASSIGN(ExpireHistoryBackend); }; diff --git a/chrome/browser/history/expire_history_backend_unittest.cc b/chrome/browser/history/expire_history_backend_unittest.cc index feab732..ab3112f 100644 --- a/chrome/browser/history/expire_history_backend_unittest.cc +++ b/chrome/browser/history/expire_history_backend_unittest.cc @@ -29,6 +29,7 @@ #include "components/bookmarks/browser/bookmark_model.h" #include "components/bookmarks/browser/bookmark_utils.h" #include "components/bookmarks/test/test_bookmark_client.h" +#include "components/history/core/test/history_client_fake_bookmarks.h" #include "content/public/test/test_browser_thread.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -57,10 +58,9 @@ class ExpireHistoryTest : public testing::Test, public BroadcastNotificationDelegate { public: ExpireHistoryTest() - : bookmark_model_(bookmark_client_.CreateModel(false)), - ui_thread_(BrowserThread::UI, &message_loop_), + : ui_thread_(BrowserThread::UI, &message_loop_), db_thread_(BrowserThread::DB, &message_loop_), - expirer_(this, bookmark_model_.get()), + expirer_(this, &history_client_), now_(Time::Now()) {} protected: @@ -89,10 +89,7 @@ class ExpireHistoryTest : public testing::Test, STLDeleteValues(¬ifications_); } - void StarURL(const GURL& url) { - bookmark_model_->AddURL( - bookmark_model_->bookmark_bar_node(), 0, base::string16(), url); - } + void StarURL(const GURL& url) { history_client_.AddBookmark(url); } static bool IsStringInFile(const base::FilePath& filename, const char* str); @@ -102,8 +99,7 @@ class ExpireHistoryTest : public testing::Test, // This must be destroyed last. base::ScopedTempDir tmp_dir_; - test::TestBookmarkClient bookmark_client_; - scoped_ptr<BookmarkModel> bookmark_model_; + HistoryClientFakeBookmarks history_client_; base::MessageLoopForUI message_loop_; content::TestBrowserThread ui_thread_; @@ -521,7 +517,7 @@ TEST_F(ExpireHistoryTest, DontDeleteStarredURL) { // ASSERT_TRUE(HasThumbnail(url_row.id())); // Unstar the URL and delete again. - bookmark_utils::RemoveAllBookmarks(bookmark_model_.get(), url); + history_client_.ClearAllBookmarks(); ClearLastNotifications(); expirer_.DeleteURL(url); diff --git a/chrome/browser/history/history_backend.cc b/chrome/browser/history/history_backend.cc index c9dbe3b..a3efbe6 100644 --- a/chrome/browser/history/history_backend.cc +++ b/chrome/browser/history/history_backend.cc @@ -37,8 +37,8 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/importer/imported_favicon_usage.h" #include "chrome/common/url_constants.h" -#include "components/bookmarks/browser/bookmark_service.h" #include "components/favicon_base/select_favicon_frames.h" +#include "components/history/core/browser/history_client.h" #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "net/base/registry_controlled_domains/registry_controlled_domain.h" @@ -164,15 +164,15 @@ class CommitLaterTask : public base::RefCounted<CommitLaterTask> { HistoryBackend::HistoryBackend(const base::FilePath& history_dir, Delegate* delegate, - BookmarkService* bookmark_service) + HistoryClient* history_client) : delegate_(delegate), history_dir_(history_dir), scheduled_kill_db_(false), - expirer_(this, bookmark_service), + expirer_(this, history_client), recent_redirects_(kMaxRedirectCount), backend_destroy_message_loop_(NULL), segment_queried_(false), - bookmark_service_(bookmark_service) { + history_client_(history_client) { } HistoryBackend::~HistoryBackend() { @@ -663,9 +663,12 @@ void HistoryBackend::InitImpl(const std::string& languages) { #if defined(OS_ANDROID) if (thumbnail_db_) { - android_provider_backend_.reset(new AndroidProviderBackend( - GetAndroidCacheFileName(), db_.get(), thumbnail_db_.get(), - bookmark_service_, delegate_.get())); + android_provider_backend_.reset( + new AndroidProviderBackend(GetAndroidCacheFileName(), + db_.get(), + thumbnail_db_.get(), + history_client_, + delegate_.get())); } #endif @@ -2042,7 +2045,7 @@ void HistoryBackend::SetImportedFavicons( } // Save the mapping from all the URLs to the favicon. - BookmarkService* bookmark_service = GetBookmarkService(); + HistoryClient* history_client = GetHistoryClient(); for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); url != favicon_usage[i].urls.end(); ++url) { URLRow url_row; @@ -2052,7 +2055,7 @@ void HistoryBackend::SetImportedFavicons( // for regular bookmarked URLs with favicons - when history db is // cleaned, we keep an entry in the db with 0 visits as long as that // url is bookmarked. - if (bookmark_service && bookmark_service_->IsBookmarked(*url)) { + if (history_client && history_client->IsBookmarked(*url)) { URLRow url_info(*url); url_info.set_visit_count(0); url_info.set_typed_count(0); @@ -2775,10 +2778,10 @@ void HistoryBackend::DeleteAllHistory() { // the original tables directly. // Get the bookmarked URLs. - std::vector<BookmarkService::URLAndTitle> starred_urls; - BookmarkService* bookmark_service = GetBookmarkService(); - if (bookmark_service) - bookmark_service_->GetBookmarks(&starred_urls); + std::vector<URLAndTitle> starred_urls; + HistoryClient* history_client = GetHistoryClient(); + if (history_client) + history_client->GetBookmarks(&starred_urls); URLRows kept_urls; for (size_t i = 0; i < starred_urls.size(); i++) { @@ -2914,10 +2917,10 @@ bool HistoryBackend::ClearAllMainHistory(const URLRows& kept_urls) { return true; } -BookmarkService* HistoryBackend::GetBookmarkService() { - if (bookmark_service_) - bookmark_service_->BlockTillLoaded(); - return bookmark_service_; +HistoryClient* HistoryBackend::GetHistoryClient() { + if (history_client_) + history_client_->BlockUntilBookmarksLoaded(); + return history_client_; } void HistoryBackend::NotifyVisitObservers(const VisitRow& visit) { diff --git a/chrome/browser/history/history_backend.h b/chrome/browser/history/history_backend.h index 1d2e03c..019adb8 100644 --- a/chrome/browser/history/history_backend.h +++ b/chrome/browser/history/history_backend.h @@ -26,7 +26,6 @@ #include "sql/init_status.h" #include "ui/base/layout.h" -class BookmarkService; class TestingProfile; class TypedUrlSyncableService; struct ThumbnailScore; @@ -37,6 +36,7 @@ class AndroidProviderBackend; #endif class CommitLaterTask; +class HistoryClient; class VisitFilter; struct DownloadRow; @@ -105,13 +105,13 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, // See the definition of BroadcastNotificationsCallback above. This function // takes ownership of the callback pointer. // - // |bookmark_service| is used to determine bookmarked URLs when deleting and + // |history_client| is used to determine bookmarked URLs when deleting and // may be NULL. // // This constructor is fast and does no I/O, so can be called at any time. HistoryBackend(const base::FilePath& history_dir, Delegate* delegate, - BookmarkService* bookmark_service); + HistoryClient* history_client); // Must be called after creation but before any objects are created. If this // fails, all other functions will fail as well. (Since this runs on another @@ -796,9 +796,9 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, // Deletes the FTS index database files, which are no longer used. void DeleteFTSIndexDatabases(); - // Returns the BookmarkService, blocking until it is loaded. This may return - // NULL during testing. - BookmarkService* GetBookmarkService(); + // Returns the HistoryClient, blocking until the bookmarks are loaded. This + // may return NULL during testing. + HistoryClient* GetHistoryClient(); // Notify any observers of an addition to the visit database. void NotifyVisitObservers(const VisitRow& visit); @@ -863,12 +863,11 @@ class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>, // done. std::list<HistoryDBTaskRequest*> db_task_requests_; - // Used to determine if a URL is bookmarked. This is owned by the Profile and - // may be NULL (during testing). + // Used to determine if a URL is bookmarked; may be NULL. // - // Use GetBookmarkService to access this, which makes sure the service is - // loaded. - BookmarkService* bookmark_service_; + // Use GetHistoryClient to access this, which makes sure the bookmarks are + // loaded before returning. + HistoryClient* history_client_; #if defined(OS_ANDROID) // Used to provide the Android ContentProvider APIs. diff --git a/chrome/browser/history/history_backend_unittest.cc b/chrome/browser/history/history_backend_unittest.cc index 5ab9a3a..5d0704b 100644 --- a/chrome/browser/history/history_backend_unittest.cc +++ b/chrome/browser/history/history_backend_unittest.cc @@ -20,7 +20,6 @@ #include "base/strings/string16.h" #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" -#include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/history/history_notifications.h" #include "chrome/browser/history/history_service.h" @@ -32,13 +31,11 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/importer/imported_favicon_usage.h" #include "chrome/test/base/testing_profile.h" -#include "components/bookmarks/browser/bookmark_model.h" -#include "components/bookmarks/browser/bookmark_utils.h" -#include "components/bookmarks/test/bookmark_test_helpers.h" -#include "components/bookmarks/test/test_bookmark_client.h" +#include "components/history/core/test/history_client_fake_bookmarks.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" #include "content/public/test/test_browser_thread.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" @@ -67,6 +64,11 @@ bool FaviconBitmapLessThan(const history::FaviconBitmap& a, return a.pixel_size.GetArea() < b.pixel_size.GetArea(); } +class HistoryClientMock : public history::HistoryClientFakeBookmarks { + public: + MOCK_METHOD0(BlockUntilBookmarksLoaded, void()); +}; + } // namespace namespace history { @@ -116,8 +118,7 @@ class HistoryBackendTestBase : public testing::Test { typedef std::vector<std::pair<int, HistoryDetails*> > NotificationList; HistoryBackendTestBase() - : bookmark_model_(bookmark_client_.CreateModel(false)), - loaded_(false), + : loaded_(false), ui_thread_(content::BrowserThread::UI, &message_loop_) {} virtual ~HistoryBackendTestBase() { @@ -152,10 +153,9 @@ class HistoryBackendTestBase : public testing::Test { std::make_pair(type, details.release())); } - test::TestBookmarkClient bookmark_client_; + history::HistoryClientFakeBookmarks history_client_; scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure. scoped_ptr<InMemoryHistoryBackend> mem_backend_; - scoped_ptr<BookmarkModel> bookmark_model_; bool loaded_; private: @@ -167,7 +167,7 @@ class HistoryBackendTestBase : public testing::Test { &test_dir_)) return; backend_ = new HistoryBackend( - test_dir_, new HistoryBackendTestDelegate(this), bookmark_model_.get()); + test_dir_, new HistoryBackendTestDelegate(this), &history_client_); backend_->Init(std::string(), false); } @@ -178,6 +178,7 @@ class HistoryBackendTestBase : public testing::Test { mem_backend_.reset(); base::DeleteFile(test_dir_, true); base::RunLoop().RunUntilIdle(); + history_client_.ClearAllBookmarks(); } void SetInMemoryBackend(scoped_ptr<InMemoryHistoryBackend> backend) { @@ -611,8 +612,7 @@ TEST_F(HistoryBackendTest, DeleteAll) { EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL)); // Star row1. - bookmark_model_->AddURL( - bookmark_model_->bookmark_bar_node(), 0, base::string16(), row1.url()); + history_client_.AddBookmark(row1.url()); // Now finally clear all history. ClearBroadcastedNotifications(); @@ -676,7 +676,7 @@ TEST_F(HistoryBackendTest, DeleteAll) { EXPECT_EQ(out_favicon1, mappings[0].icon_id); // The first URL should still be bookmarked. - EXPECT_TRUE(bookmark_model_->IsBookmarked(row1.url())); + EXPECT_TRUE(history_client_.IsBookmarked(row1.url())); // Check that we fire the notification about all history having been deleted. ASSERT_EQ(1u, broadcasted_notifications().size()); @@ -774,10 +774,8 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL); // Star the two URLs. - bookmark_utils::AddIfNotBookmarked( - bookmark_model_.get(), row1.url(), base::string16()); - bookmark_utils::AddIfNotBookmarked( - bookmark_model_.get(), row2.url(), base::string16()); + history_client_.AddBookmark(row1.url()); + history_client_.AddBookmark(row2.url()); // Delete url 2. Because url 2 is starred this won't delete the URL, only // the visits. @@ -795,7 +793,7 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { favicon_url2, favicon_base::FAVICON, NULL)); // Unstar row2. - bookmark_utils::RemoveAllBookmarks(bookmark_model_.get(), row2.url()); + history_client_.DelBookmark(row2.url()); // Tell the backend it was unstarred. We have to explicitly do this as // BookmarkModel isn't wired up to the backend during testing. @@ -811,7 +809,8 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { favicon_url2, favicon_base::FAVICON, NULL)); // Unstar row 1. - bookmark_utils::RemoveAllBookmarks(bookmark_model_.get(), row1.url()); + history_client_.DelBookmark(row1.url()); + // Tell the backend it was unstarred. We have to explicitly do this as // BookmarkModel isn't wired up to the backend during testing. unstarred_urls.clear(); @@ -1083,8 +1082,7 @@ TEST_F(HistoryBackendTest, ImportedFaviconsTest) { EXPECT_TRUE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); // If the URL is bookmarked, it should get added to history with 0 visits. - bookmark_model_->AddURL( - bookmark_model_->bookmark_bar_node(), 0, base::string16(), url3); + history_client_.AddBookmark(url3); backend_->SetImportedFavicons(favicons); EXPECT_FALSE(backend_->db_->GetRowForURL(url3, &url_row3) == 0); EXPECT_TRUE(url_row3.visit_count() == 0); @@ -1484,9 +1482,8 @@ TEST_F(HistoryBackendTest, MigrationVisitSource) { new_history_path.Append(chrome::kHistoryFilename); ASSERT_TRUE(base::CopyFile(old_history_path, new_history_file)); - backend_ = new HistoryBackend(new_history_path, - new HistoryBackendTestDelegate(this), - bookmark_model_.get()); + backend_ = new HistoryBackend( + new_history_path, new HistoryBackendTestDelegate(this), &history_client_); backend_->Init(std::string(), false); backend_->Closing(); backend_ = NULL; @@ -2887,9 +2884,8 @@ TEST_F(HistoryBackendTest, MigrationVisitDuration) { ASSERT_TRUE(base::CopyFile(old_history, new_history_file)); ASSERT_TRUE(base::CopyFile(old_archived, new_archived_file)); - backend_ = new HistoryBackend(new_history_path, - new HistoryBackendTestDelegate(this), - bookmark_model_.get()); + backend_ = new HistoryBackend( + new_history_path, new HistoryBackendTestDelegate(this), &history_client_); backend_->Init(std::string(), false); backend_->Closing(); backend_ = NULL; @@ -3125,17 +3121,13 @@ TEST_F(HistoryBackendTest, DeleteMatchingUrlsForKeyword) { TEST_F(HistoryBackendTest, RemoveNotification) { scoped_ptr<TestingProfile> profile(new TestingProfile()); - ASSERT_TRUE(profile->CreateHistoryService(false, false)); - profile->CreateBookmarkModel(true); - BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile.get()); - test::WaitForBookmarkModelToLoad(model); - // Add a URL. GURL url("http://www.google.com"); - bookmark_utils::AddIfNotBookmarked(model, url, base::string16()); - - HistoryService* service = HistoryServiceFactory::GetForProfile( - profile.get(), Profile::EXPLICIT_ACCESS); + HistoryClientMock history_client; + history_client.AddBookmark(url); + scoped_ptr<HistoryService> service( + new HistoryService(&history_client, profile.get())); + EXPECT_TRUE(service->Init(profile->GetPath())); service->AddPage( url, base::Time::Now(), NULL, 1, GURL(), RedirectList(), @@ -3143,6 +3135,7 @@ TEST_F(HistoryBackendTest, RemoveNotification) { // This won't actually delete the URL, rather it'll empty out the visits. // This triggers blocking on the BookmarkModel. + EXPECT_CALL(history_client, BlockUntilBookmarksLoaded()); service->DeleteURL(url); } diff --git a/chrome/browser/history/history_querying_unittest.cc b/chrome/browser/history/history_querying_unittest.cc index 41a4d55..123eb20 100644 --- a/chrome/browser/history/history_querying_unittest.cc +++ b/chrome/browser/history/history_querying_unittest.cc @@ -163,7 +163,7 @@ class HistoryQueryTest : public testing::Test { ASSERT_TRUE(base::CreateDirectory(history_dir_)); history_.reset(new HistoryService); - if (!history_->Init(history_dir_, NULL)) { + if (!history_->Init(history_dir_)) { history_.reset(); // Tests should notice this NULL ptr & fail. return; } diff --git a/chrome/browser/history/history_service.cc b/chrome/browser/history/history_service.cc index 8104392..af61f10 100644 --- a/chrome/browser/history/history_service.cc +++ b/chrome/browser/history/history_service.cc @@ -33,7 +33,6 @@ #include "base/threading/thread.h" #include "base/time/time.h" #include "chrome/browser/autocomplete/history_url_provider.h" -#include "chrome/browser/bookmarks/bookmark_model_factory.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/history/download_row.h" @@ -56,7 +55,6 @@ #include "chrome/common/pref_names.h" #include "chrome/common/thumbnail_score.h" #include "chrome/common/url_constants.h" -#include "components/bookmarks/browser/bookmark_model.h" #include "components/history/core/browser/history_client.h" #include "components/visitedlink/browser/visitedlink_master.h" #include "content/public/browser/browser_thread.h" @@ -196,7 +194,6 @@ HistoryService::HistoryService() history_client_(NULL), profile_(NULL), backend_loaded_(false), - bookmark_service_(NULL), no_db_(false) { } @@ -208,7 +205,6 @@ HistoryService::HistoryService(history::HistoryClient* client, Profile* profile) visitedlink_master_(new visitedlink::VisitedLinkMaster( profile, this, true)), backend_loaded_(false), - bookmark_service_(NULL), no_db_(false) { DCHECK(profile_); registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, @@ -331,19 +327,6 @@ history::TypedUrlSyncableService* HistoryService::GetTypedUrlSyncableService() void HistoryService::Shutdown() { DCHECK(thread_checker_.CalledOnValidThread()); - // It's possible that bookmarks haven't loaded and history is waiting for - // bookmarks to complete loading. In such a situation history can't shutdown - // (meaning if we invoked history_service_->Cleanup now, we would - // deadlock). To break the deadlock we tell BookmarkModel it's about to be - // deleted so that it can release the signal history is waiting on, allowing - // history to shutdown (history_service_->Cleanup to complete). In such a - // scenario history sees an incorrect view of bookmarks, but it's better - // than a deadlock. - BookmarkModel* bookmark_model = static_cast<BookmarkModel*>( - BookmarkModelFactory::GetForProfileIfExists(profile_)); - if (bookmark_model) - bookmark_model->Shutdown(); - Cleanup(); } @@ -911,9 +894,7 @@ void HistoryService::RebuildTable( ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::IterateURLs, enumerator); } -bool HistoryService::Init(const base::FilePath& history_dir, - BookmarkService* bookmark_service, - bool no_db) { +bool HistoryService::Init(const base::FilePath& history_dir, bool no_db) { DCHECK(thread_checker_.CalledOnValidThread()); if (!thread_->Start()) { Cleanup(); @@ -921,14 +902,13 @@ bool HistoryService::Init(const base::FilePath& history_dir, } history_dir_ = history_dir; - bookmark_service_ = bookmark_service; no_db_ = no_db; if (profile_) { std::string languages = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); - in_memory_url_index_.reset( - new history::InMemoryURLIndex(profile_, history_dir_, languages)); + in_memory_url_index_.reset(new history::InMemoryURLIndex( + profile_, history_dir_, languages, history_client_)); in_memory_url_index_->Init(); } @@ -939,7 +919,7 @@ bool HistoryService::Init(const base::FilePath& history_dir, weak_ptr_factory_.GetWeakPtr(), base::ThreadTaskRunnerHandle::Get(), profile_), - bookmark_service_)); + history_client_)); history_backend_.swap(backend); // There may not be a profile when unit testing. diff --git a/chrome/browser/history/history_service.h b/chrome/browser/history/history_service.h index eb59902..00c17e9 100644 --- a/chrome/browser/history/history_service.h +++ b/chrome/browser/history/history_service.h @@ -28,6 +28,7 @@ #include "chrome/browser/search_engines/template_url_id.h" #include "chrome/common/ref_counted_util.h" #include "components/favicon_base/favicon_callback.h" +#include "components/history/core/browser/history_client.h" #include "components/keyed_service/core/keyed_service.h" #include "components/visitedlink/browser/visitedlink_delegate.h" #include "content/public/browser/download_manager_delegate.h" @@ -42,7 +43,6 @@ class AndroidHistoryProviderService; #endif -class BookmarkService; class GURL; class HistoryURLProvider; class PageUsageData; @@ -103,10 +103,9 @@ class HistoryService : public CancelableRequestProvider, // Initializes the history service, returning true on success. On false, do // not call any other functions. The given directory will be used for storing - // the history files. The BookmarkService is used when deleting URLs to - // test if a URL is bookmarked; it may be NULL during testing. - bool Init(const base::FilePath& history_dir, BookmarkService* bookmark_service) { - return Init(history_dir, bookmark_service, false); + // the history files. + bool Init(const base::FilePath& history_dir) { + return Init(history_dir, false); } // Triggers the backend to load if it hasn't already, and then returns whether @@ -623,9 +622,7 @@ class HistoryService : public CancelableRequestProvider, // Low-level Init(). Same as the public version, but adds a |no_db| parameter // that is only set by unittests which causes the backend to not init its DB. - bool Init(const base::FilePath& history_dir, - BookmarkService* bookmark_service, - bool no_db); + bool Init(const base::FilePath& history_dir, bool no_db); // Called by the HistoryURLProvider class to schedule an autocomplete, it // will be called back on the internal history thread with the history @@ -1042,7 +1039,6 @@ class HistoryService : public CancelableRequestProvider, // Cached values from Init(), used whenever we need to reload the backend. base::FilePath history_dir_; - BookmarkService* bookmark_service_; bool no_db_; // The index used for quick history lookups. diff --git a/chrome/browser/history/history_service_factory.cc b/chrome/browser/history/history_service_factory.cc index 79b5074..35f4493 100644 --- a/chrome/browser/history/history_service_factory.cc +++ b/chrome/browser/history/history_service_factory.cc @@ -60,7 +60,6 @@ void HistoryServiceFactory::ShutdownForProfile(Profile* profile) { HistoryServiceFactory::HistoryServiceFactory() : BrowserContextKeyedServiceFactory( "HistoryService", BrowserContextDependencyManager::GetInstance()) { - DependsOn(BookmarkModelFactory::GetInstance()); DependsOn(ChromeHistoryClientFactory::GetInstance()); } @@ -72,8 +71,7 @@ KeyedService* HistoryServiceFactory::BuildServiceInstanceFor( Profile* profile = static_cast<Profile*>(context); HistoryService* history_service = new HistoryService( ChromeHistoryClientFactory::GetForProfile(profile), profile); - if (!history_service->Init(profile->GetPath(), - BookmarkModelFactory::GetForProfile(profile))) { + if (!history_service->Init(profile->GetPath())) { return NULL; } return history_service; diff --git a/chrome/browser/history/history_unittest.cc b/chrome/browser/history/history_unittest.cc index 8bb8361..d90e535 100644 --- a/chrome/browser/history/history_unittest.cc +++ b/chrome/browser/history/history_unittest.cc @@ -917,7 +917,7 @@ class HistoryTest : public testing::Test { history_dir_ = temp_dir_.path().AppendASCII("HistoryTest"); ASSERT_TRUE(base::CreateDirectory(history_dir_)); history_service_.reset(new HistoryService); - if (!history_service_->Init(history_dir_, NULL)) { + if (!history_service_->Init(history_dir_)) { history_service_.reset(); ADD_FAILURE(); } diff --git a/chrome/browser/history/in_memory_url_index.cc b/chrome/browser/history/in_memory_url_index.cc index 029e56a..a5c1c47 100644 --- a/chrome/browser/history/in_memory_url_index.cc +++ b/chrome/browser/history/in_memory_url_index.cc @@ -90,8 +90,10 @@ InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: InMemoryURLIndex::InMemoryURLIndex(Profile* profile, const base::FilePath& history_dir, - const std::string& languages) + const std::string& languages, + HistoryClient* history_client) : profile_(profile), + history_client_(history_client), history_dir_(history_dir), languages_(languages), private_data_(new URLIndexPrivateData), @@ -114,6 +116,7 @@ InMemoryURLIndex::InMemoryURLIndex(Profile* profile, // Called only by unit tests. InMemoryURLIndex::InMemoryURLIndex() : profile_(NULL), + history_client_(NULL), private_data_(new URLIndexPrivateData), restore_cache_observer_(NULL), save_cache_observer_(NULL), @@ -167,7 +170,7 @@ ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms( cursor_position, max_matches, languages_, - BookmarkModelFactory::GetForProfile(profile_)); + history_client_); } // Updating -------------------------------------------------------------------- diff --git a/chrome/browser/history/in_memory_url_index.h b/chrome/browser/history/in_memory_url_index.h index 0bbb99d..697ccad 100644 --- a/chrome/browser/history/in_memory_url_index.h +++ b/chrome/browser/history/in_memory_url_index.h @@ -41,6 +41,7 @@ namespace history { namespace imui = in_memory_url_index; +class HistoryClient; class HistoryDatabase; class URLIndexPrivateData; struct URLsDeletedDetails; @@ -100,7 +101,8 @@ class InMemoryURLIndex : public content::NotificationObserver, // which URLs and omnibox searches are broken down into words and characters. InMemoryURLIndex(Profile* profile, const base::FilePath& history_dir, - const std::string& languages); + const std::string& languages, + HistoryClient* client); virtual ~InMemoryURLIndex(); // Opens and prepares the index of historical URL visits. If the index private @@ -254,6 +256,9 @@ class InMemoryURLIndex : public content::NotificationObserver, // The profile, may be null when testing. Profile* profile_; + // The HistoryClient; may be NULL when testing. + HistoryClient* history_client_; + // Directory where cache file resides. This is, except when unit testing, // the same directory in which the profile's history database is found. It // should never be empty. diff --git a/chrome/browser/history/in_memory_url_index_unittest.cc b/chrome/browser/history/in_memory_url_index_unittest.cc index f36eda5..7463ec8 100644 --- a/chrome/browser/history/in_memory_url_index_unittest.cc +++ b/chrome/browser/history/in_memory_url_index_unittest.cc @@ -28,6 +28,7 @@ #include "chrome/test/base/history_index_restore_observer.h" #include "chrome/test/base/testing_profile.h" #include "components/bookmarks/test/bookmark_test_helpers.h" +#include "components/history/core/browser/history_client.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" #include "content/public/test/test_browser_thread.h" @@ -277,8 +278,9 @@ void InMemoryURLIndexTest::SetUp() { transaction.Commit(); } - url_index_.reset( - new InMemoryURLIndex(&profile_, base::FilePath(), "en,ja,hi,zh")); + url_index_.reset(new InMemoryURLIndex( + &profile_, base::FilePath(), "en,ja,hi,zh", + history_service_->history_client())); url_index_->Init(); url_index_->RebuildFromHistory(history_database_); } @@ -435,8 +437,9 @@ TEST_F(LimitedInMemoryURLIndexTest, Initialization) { uint64 row_count = 0; while (statement.Step()) ++row_count; EXPECT_EQ(1U, row_count); - url_index_.reset( - new InMemoryURLIndex(&profile_, base::FilePath(), "en,ja,hi,zh")); + url_index_.reset(new InMemoryURLIndex( + &profile_, base::FilePath(), "en,ja,hi,zh", + history_service_->history_client())); url_index_->Init(); url_index_->RebuildFromHistory(history_database_); URLIndexPrivateData& private_data(*GetPrivateData()); @@ -1157,9 +1160,10 @@ class InMemoryURLIndexCacheTest : public testing::Test { void InMemoryURLIndexCacheTest::SetUp() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + HistoryClient history_client; base::FilePath path(temp_dir_.path()); - url_index_.reset( - new InMemoryURLIndex(NULL, path, "en,ja,hi,zh")); + url_index_.reset(new InMemoryURLIndex( + NULL, path, "en,ja,hi,zh", &history_client)); } void InMemoryURLIndexCacheTest::set_history_dir( diff --git a/chrome/browser/history/scored_history_match.cc b/chrome/browser/history/scored_history_match.cc index 53565e7..b5219fe 100644 --- a/chrome/browser/history/scored_history_match.cc +++ b/chrome/browser/history/scored_history_match.cc @@ -19,8 +19,8 @@ #include "chrome/browser/autocomplete/history_url_provider.h" #include "chrome/browser/autocomplete/url_prefix.h" #include "chrome/browser/omnibox/omnibox_field_trial.h" -#include "components/bookmarks/browser/bookmark_service.h" #include "components/bookmarks/browser/bookmark_utils.h" +#include "components/history/core/browser/history_client.h" #include "content/public/browser/browser_thread.h" namespace history { @@ -55,7 +55,7 @@ ScoredHistoryMatch::ScoredHistoryMatch( const WordStarts& terms_to_word_starts_offsets, const RowWordStarts& word_starts, const base::Time now, - BookmarkService* bookmark_service) + HistoryClient* history_client) : HistoryMatch(row, 0, false, false), raw_score_(0), can_inline_(false) { @@ -153,7 +153,7 @@ ScoredHistoryMatch::ScoredHistoryMatch( const float topicality_score = GetTopicalityScore( terms.size(), url, terms_to_word_starts_offsets, word_starts); const float frequency_score = GetFrequency( - now, (bookmark_service && bookmark_service->IsBookmarked(gurl)), visits); + now, (history_client && history_client->IsBookmarked(gurl)), visits); raw_score_ = GetFinalRelevancyScore(topicality_score, frequency_score); raw_score_ = (raw_score_ <= kint32max) ? static_cast<int>(raw_score_) : kint32max; diff --git a/chrome/browser/history/scored_history_match.h b/chrome/browser/history/scored_history_match.h index 06f8bf1..5a24633 100644 --- a/chrome/browser/history/scored_history_match.h +++ b/chrome/browser/history/scored_history_match.h @@ -15,10 +15,9 @@ #include "chrome/browser/history/in_memory_url_index_types.h" #include "testing/gtest/include/gtest/gtest_prod.h" -class BookmarkService; - namespace history { +class HistoryClient; class ScoredHistoryMatchTest; // An HistoryMatch that has a score as well as metrics defining where in the @@ -41,7 +40,7 @@ class ScoredHistoryMatch : public history::HistoryMatch { // terms, it's appropriate to look for the word boundary within the term. // For instance, the term ".net" should look for a word boundary at the "n". // These offsets (".net" should have an offset of 1) come from - // |terms_to_word_starts_offsets|. |bookmark_service| is used to determine + // |terms_to_word_starts_offsets|. |history_client| is used to determine // if the match's URL is referenced by any bookmarks, which can also affect // the raw score. The raw score allows the matches to be ordered and can be // used to influence the final score calculated by the client of this index. @@ -55,7 +54,7 @@ class ScoredHistoryMatch : public history::HistoryMatch { const WordStarts& terms_to_word_starts_offsets, const RowWordStarts& word_starts, const base::Time now, - BookmarkService* bookmark_service); + HistoryClient* history_client); ~ScoredHistoryMatch(); // Compares two matches by score. Functor supporting URLIndexPrivateData's diff --git a/chrome/browser/history/scored_history_match_unittest.cc b/chrome/browser/history/scored_history_match_unittest.cc index 2ab39a9..b653a9b 100644 --- a/chrome/browser/history/scored_history_match_unittest.cc +++ b/chrome/browser/history/scored_history_match_unittest.cc @@ -8,7 +8,7 @@ #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/history/scored_history_match.h" -#include "components/bookmarks/browser/bookmark_service.h" +#include "components/history/core/test/history_client_fake_bookmarks.h" #include "testing/gtest/include/gtest/gtest.h" using base::ASCIIToUTF16; @@ -181,33 +181,6 @@ TEST_F(ScoredHistoryMatchTest, Scoring) { EXPECT_EQ(scored_f.raw_score(), 0); } -class BookmarkServiceMock : public BookmarkService { - public: - explicit BookmarkServiceMock(const GURL& url); - virtual ~BookmarkServiceMock() {} - - // Returns true if the given |url| is the same as |url_|. - virtual bool IsBookmarked(const GURL& url) OVERRIDE; - - // Required but unused. - virtual void GetBookmarks(std::vector<URLAndTitle>* bookmarks) OVERRIDE {} - virtual void BlockTillLoaded() OVERRIDE {} - - private: - const GURL url_; - - DISALLOW_COPY_AND_ASSIGN(BookmarkServiceMock); -}; - -BookmarkServiceMock::BookmarkServiceMock(const GURL& url) - : BookmarkService(), - url_(url) { -} - -bool BookmarkServiceMock::IsBookmarked(const GURL& url) { - return url == url_; -} - TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { // We use NowFromSystemTime() because MakeURLRow uses the same function // to calculate last visit time when building a row. @@ -225,10 +198,11 @@ TEST_F(ScoredHistoryMatchTest, ScoringBookmarks) { one_word_no_offset, word_starts, now, NULL); // Now bookmark that URL and make sure its score increases. base::AutoReset<int> reset(&ScoredHistoryMatch::bookmark_value_, 5); - BookmarkServiceMock bookmark_service_mock(url); + history::HistoryClientFakeBookmarks history_client; + history_client.AddBookmark(url); ScoredHistoryMatch scored_with_bookmark( row, visits, std::string(), ASCIIToUTF16("abc"), Make1Term("abc"), - one_word_no_offset, word_starts, now, &bookmark_service_mock); + one_word_no_offset, word_starts, now, &history_client); EXPECT_GT(scored_with_bookmark.raw_score(), scored.raw_score()); } diff --git a/chrome/browser/history/url_index_private_data.cc b/chrome/browser/history/url_index_private_data.cc index d00c8c3..33b7026 100644 --- a/chrome/browser/history/url_index_private_data.cc +++ b/chrome/browser/history/url_index_private_data.cc @@ -23,8 +23,8 @@ #include "chrome/browser/history/history_db_task.h" #include "chrome/browser/history/history_service.h" #include "chrome/browser/history/in_memory_url_index.h" -#include "components/bookmarks/browser/bookmark_service.h" #include "components/bookmarks/browser/bookmark_utils.h" +#include "components/history/core/browser/history_client.h" #include "net/base/net_util.h" #if defined(USE_SYSTEM_PROTOBUF) @@ -151,7 +151,7 @@ ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( size_t cursor_position, size_t max_matches, const std::string& languages, - BookmarkService* bookmark_service) { + HistoryClient* history_client) { // If cursor position is set and useful (not at either end of the // string), allow the search string to be broken at cursor position. // We do this by pretending there's a space where the cursor is. @@ -243,7 +243,7 @@ ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( return scored_items; } scored_items = std::for_each(history_id_set.begin(), history_id_set.end(), - AddHistoryMatch(*this, languages, bookmark_service, lower_raw_string, + AddHistoryMatch(*this, languages, history_client, lower_raw_string, lower_raw_terms, base::Time::Now())).ScoredMatches(); // Select and sort only the top |max_matches| results. @@ -1263,16 +1263,16 @@ URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {} URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch( const URLIndexPrivateData& private_data, const std::string& languages, - BookmarkService* bookmark_service, + HistoryClient* history_client, const base::string16& lower_string, const String16Vector& lower_terms, const base::Time now) - : private_data_(private_data), - languages_(languages), - bookmark_service_(bookmark_service), - lower_string_(lower_string), - lower_terms_(lower_terms), - now_(now) { + : private_data_(private_data), + languages_(languages), + history_client_(history_client), + lower_string_(lower_string), + lower_terms_(lower_terms), + now_(now) { // Calculate offsets for each term. For instance, the offset for // ".net" should be 1, indicating that the actual word-part of the term // starts at offset 1. @@ -1305,7 +1305,7 @@ void URLIndexPrivateData::AddHistoryMatch::operator()( DCHECK(starts_pos != private_data_.word_starts_map_.end()); ScoredHistoryMatch match(hist_item, visits, languages_, lower_string_, lower_terms_, lower_terms_to_word_starts_offsets_, - starts_pos->second, now_, bookmark_service_); + starts_pos->second, now_, history_client_); if (match.raw_score() > 0) scored_matches_.push_back(match); } diff --git a/chrome/browser/history/url_index_private_data.h b/chrome/browser/history/url_index_private_data.h index 679dfe9..22aef97 100644 --- a/chrome/browser/history/url_index_private_data.h +++ b/chrome/browser/history/url_index_private_data.h @@ -17,7 +17,6 @@ #include "chrome/browser/history/in_memory_url_index_types.h" #include "chrome/browser/history/scored_history_match.h" -class BookmarkService; class HistoryQuickProviderTest; namespace in_memory_url_index { @@ -28,6 +27,7 @@ namespace history { namespace imui = in_memory_url_index; +class HistoryClient; class HistoryDatabase; class InMemoryURLIndex; class RefCountedBool; @@ -63,7 +63,7 @@ class URLIndexPrivateData // will be found in nearly all history candidates. Results are sorted by // descending score. The full results set (i.e. beyond the // |kItemsToScoreLimit| limit) will be retained and used for subsequent calls - // to this function. |bookmark_service| is used to boost a result's score if + // to this function. |history_client| is used to boost a result's score if // its URL is referenced by one or more of the user's bookmarks. |languages| // is used to help parse/format the URLs in the history index. In total, // |max_matches| of items will be returned in the |ScoredHistoryMatches| @@ -72,7 +72,7 @@ class URLIndexPrivateData size_t cursor_position, size_t max_matches, const std::string& languages, - BookmarkService* bookmark_service); + HistoryClient* history_client); // Adds the history item in |row| to the index if it does not already already // exist and it meets the minimum 'quick' criteria. If the row already exists @@ -199,7 +199,7 @@ class URLIndexPrivateData public: AddHistoryMatch(const URLIndexPrivateData& private_data, const std::string& languages, - BookmarkService* bookmark_service, + HistoryClient* history_client, const base::string16& lower_string, const String16Vector& lower_terms, const base::Time now); @@ -212,7 +212,7 @@ class URLIndexPrivateData private: const URLIndexPrivateData& private_data_; const std::string& languages_; - BookmarkService* bookmark_service_; + HistoryClient* history_client_; ScoredHistoryMatches scored_matches_; const base::string16& lower_string_; const String16Vector& lower_terms_; diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index d46e751..107155b 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -20,6 +20,7 @@ '../base/base.gyp:test_support_base', '../components/components.gyp:bookmarks_test_support', '../components/components.gyp:gcm_driver_test_support', + '../components/components.gyp:history_core_test_support', '../components/components.gyp:metrics_test_support', '../components/components.gyp:password_manager_core_browser_test_support', '../components/components.gyp:signin_core_browser_test_support', diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc index 6c2c309..4f0158d 100644 --- a/chrome/test/base/testing_profile.cc +++ b/chrome/test/base/testing_profile.cc @@ -29,6 +29,8 @@ #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" #include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h" #include "chrome/browser/guest_view/guest_view_manager.h" +#include "chrome/browser/history/chrome_history_client.h" +#include "chrome/browser/history/chrome_history_client_factory.h" #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/history_db_task.h" #include "chrome/browser/history/history_service.h" @@ -425,8 +427,10 @@ void TestingProfile::CreateFaviconService() { this, BuildFaviconService); } -static KeyedService* BuildHistoryService(content::BrowserContext* profile) { - return new HistoryService(NULL, static_cast<Profile*>(profile)); +static KeyedService* BuildHistoryService(content::BrowserContext* context) { + Profile* profile = static_cast<Profile*>(context); + return new HistoryService(ChromeHistoryClientFactory::GetForProfile(profile), + profile); } bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { @@ -441,9 +445,7 @@ bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { HistoryService* history_service = static_cast<HistoryService*>( HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse( this, BuildHistoryService)); - if (!history_service->Init(this->GetPath(), - BookmarkModelFactory::GetForProfile(this), - no_db)) { + if (!history_service->Init(this->GetPath(), no_db)) { HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL); } // Disable WebHistoryService by default, since it makes network requests. @@ -507,24 +509,22 @@ static KeyedService* BuildBookmarkModel(content::BrowserContext* context) { return bookmark_client; } +static KeyedService* BuildChromeHistoryClient( + content::BrowserContext* context) { + Profile* profile = static_cast<Profile*>(context); + return new ChromeHistoryClient(BookmarkModelFactory::GetForProfile(profile)); +} + void TestingProfile::CreateBookmarkModel(bool delete_file) { if (delete_file) { base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName); base::DeleteFile(path, false); } // This will create a bookmark model. - BookmarkModel* bookmark_service = - static_cast<ChromeBookmarkClient*>( - BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( - this, BuildBookmarkModel))->model(); - - HistoryService* history_service = - HistoryServiceFactory::GetForProfileWithoutCreating(this); - if (history_service) { - history_service->history_backend_->bookmark_service_ = bookmark_service; - history_service->history_backend_->expirer_.bookmark_service_ = - bookmark_service; - } + ChromeHistoryClientFactory::GetInstance()->SetTestingFactory( + this, BuildChromeHistoryClient); + BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( + this, BuildBookmarkModel); } static KeyedService* BuildWebDataService(content::BrowserContext* profile) { diff --git a/components/history.gypi b/components/history.gypi index 8b7821e..1a17a6c 100644 --- a/components/history.gypi +++ b/components/history.gypi @@ -6,16 +6,34 @@ 'targets': [ { 'target_name': 'history_core_browser', - 'type': 'none', + 'type': 'static_library', 'include_dirs': [ '..', ], 'dependencies': [ '../base/base.gyp:base', + '../url/url.gyp:url_lib', 'keyed_service_core', ], 'sources': [ 'history/core/browser/history_client.h', + 'history/core/browser/history_client.cc', + ], + }, + { + 'target_name': 'history_core_test_support', + 'type': 'static_library', + 'include_dirs': [ + '..', + ], + 'dependencies': [ + 'history_core_browser', + '../base/base.gyp:base', + '../url/url.gyp:url_lib', + ], + 'sources': [ + 'history/core/test/history_client_fake_bookmarks.cc', + 'history/core/test/history_client_fake_bookmarks.h', ], }, ], diff --git a/components/history/core/browser/history_client.cc b/components/history/core/browser/history_client.cc new file mode 100644 index 0000000..63dde6c --- /dev/null +++ b/components/history/core/browser/history_client.cc @@ -0,0 +1,22 @@ +// Copyright 2014 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/history_client.h" + +namespace history { + +void HistoryClient::BlockUntilBookmarksLoaded() { +} + +bool HistoryClient::IsBookmarked(const GURL& url) { + return false; +} + +void HistoryClient::GetBookmarks(std::vector<URLAndTitle>* bookmarks) { +} + +HistoryClient::HistoryClient() { +} + +} diff --git a/components/history/core/browser/history_client.h b/components/history/core/browser/history_client.h index cc2da58..370d723 100644 --- a/components/history/core/browser/history_client.h +++ b/components/history/core/browser/history_client.h @@ -5,17 +5,45 @@ #ifndef COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_CLIENT_H_ #define COMPONENTS_HISTORY_CORE_BROWSER_HISTORY_CLIENT_H_ +#include <vector> + #include "base/macros.h" +#include "base/strings/string16.h" #include "components/keyed_service/core/keyed_service.h" +#include "url/gurl.h" namespace history { +struct URLAndTitle { + GURL url; + base::string16 title; +}; + // This class abstracts operations that depend on the embedder's environment, // e.g. Chrome. class HistoryClient : public KeyedService { - protected: - HistoryClient() {} + public: + HistoryClient(); + // Waits until the bookmarks have been loaded. + // + // Must not be called from the main thread. + virtual void BlockUntilBookmarksLoaded(); + + // Returns true if the specified URL is bookmarked. + // + // If not on the main thread, then BlockUntilBookmarksLoaded must be called. + virtual bool IsBookmarked(const GURL& url); + + // Returns, by reference in |bookmarks|, the set of bookmarked urls and their + // titles. This returns the unique set of URLs. For example, if two bookmarks + // reference the same URL only one entry is added even if the title are not + // the same. + // + // If not on the main thread, then BlockUntilBookmarksLoaded must be called. + virtual void GetBookmarks(std::vector<URLAndTitle>* bookmarks); + + protected: DISALLOW_COPY_AND_ASSIGN(HistoryClient); }; diff --git a/components/history/core/test/history_client_fake_bookmarks.cc b/components/history/core/test/history_client_fake_bookmarks.cc new file mode 100644 index 0000000..8f2e084 --- /dev/null +++ b/components/history/core/test/history_client_fake_bookmarks.cc @@ -0,0 +1,47 @@ +// Copyright 2014 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/test/history_client_fake_bookmarks.h" + +namespace history { + +HistoryClientFakeBookmarks::HistoryClientFakeBookmarks() { +} + +HistoryClientFakeBookmarks::~HistoryClientFakeBookmarks() { +} + +void HistoryClientFakeBookmarks::ClearAllBookmarks() { + bookmarks_.clear(); +} + +void HistoryClientFakeBookmarks::AddBookmark(const GURL& url) { + AddBookmarkWithTitle(url, base::string16()); +} + +void HistoryClientFakeBookmarks::AddBookmarkWithTitle( + const GURL& url, + const base::string16& title) { + bookmarks_.insert(std::make_pair(url, title)); +} + +void HistoryClientFakeBookmarks::DelBookmark(const GURL& url) { + bookmarks_.erase(url); +} + +bool HistoryClientFakeBookmarks::IsBookmarked(const GURL& url) { + return bookmarks_.find(url) != bookmarks_.end(); +} + +void HistoryClientFakeBookmarks::GetBookmarks( + std::vector<URLAndTitle>* bookmarks) { + bookmarks->reserve(bookmarks->size() + bookmarks_.size()); + typedef std::map<GURL, base::string16>::const_iterator iterator; + for (iterator i = bookmarks_.begin(); i != bookmarks_.end(); ++i) { + URLAndTitle urlAndTitle = {i->first, i->second}; + bookmarks->push_back(urlAndTitle); + } +} + +} // namespace history diff --git a/components/history/core/test/history_client_fake_bookmarks.h b/components/history/core/test/history_client_fake_bookmarks.h new file mode 100644 index 0000000..71db9fe --- /dev/null +++ b/components/history/core/test/history_client_fake_bookmarks.h @@ -0,0 +1,38 @@ +// Copyright 2014 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_TEST_HISTORY_CLIENT_FAKE_BOOKMARKS_H_ +#define COMPONENTS_HISTORY_CORE_TEST_HISTORY_CLIENT_FAKE_BOOKMARKS_H_ + +#include <map> + +#include "components/history/core/browser/history_client.h" + +namespace history { + +// The class HistoryClientFakeBookmarks implements HistoryClient faking the +// methods relating to bookmarks for unit testing. +class HistoryClientFakeBookmarks : public HistoryClient { + public: + HistoryClientFakeBookmarks(); + virtual ~HistoryClientFakeBookmarks(); + + void ClearAllBookmarks(); + void AddBookmark(const GURL& url); + void AddBookmarkWithTitle(const GURL& url, const base::string16& title); + void DelBookmark(const GURL& url); + + // HistoryClient: + virtual bool IsBookmarked(const GURL& url) OVERRIDE; + virtual void GetBookmarks(std::vector<URLAndTitle>* bookmarks) OVERRIDE; + + private: + std::map<GURL, base::string16> bookmarks_; + + DISALLOW_COPY_AND_ASSIGN(HistoryClientFakeBookmarks); +}; + +} // namespace history + +#endif // COMPONENTS_HISTORY_CORE_TEST_HISTORY_CLIENT_FAKE_BOOKMARKS_H_ |