Commit 961d1f7c authored by Moe Ahmadi's avatar Moe Ahmadi Committed by Commit Bot

Looks up keyword search terms table for local history ZPS

Swaps the existing implementation with one that queries the keyword search
terms table in the in-memory URLDatabase instead of the URLs table.

- This table is smaller so querying it is likely faster.
- The step to parse URLs is avoided.
- Is more consistent with how SearchProvider offers local search history
  matches.

Also improves and re-enables LocalHistoryZeroSuggestProviderTest.Delete
which was failing for Linux ChromiumOS MSAN, likely due to using an
uninitialized |history_db_task_id_| which no longer exists.

Bug: 996516,1010944
Change-Id: I183fb7367285dcb453fb42e46651550baf0e3036
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1835182
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#704062}
parent fc5ad208
......@@ -669,6 +669,19 @@ bool URLDatabase::DeleteKeywordSearchTerm(const base::string16& term) {
return statement.Run();
}
bool URLDatabase::DeleteKeywordSearchTermForNormalizedTerm(
KeywordID keyword_id,
const base::string16& normalized_term) {
sql::Statement statement(
GetDB().GetCachedStatement(SQL_FROM_HERE,
"DELETE FROM keyword_search_terms WHERE "
"keyword_id = ? AND normalized_term=?"));
statement.BindInt64(0, keyword_id);
statement.BindString16(1, normalized_term);
return statement.Run();
}
bool URLDatabase::DeleteKeywordSearchTermForURL(URLID url_id) {
sql::Statement statement(GetDB().GetCachedStatement(
SQL_FROM_HERE, "DELETE FROM keyword_search_terms WHERE url_id=?"));
......
......@@ -233,6 +233,11 @@ class URLDatabase {
// Deletes all searches matching |term|.
bool DeleteKeywordSearchTerm(const base::string16& term);
// Deletes any search corresponding to |normalized_term|.
bool DeleteKeywordSearchTermForNormalizedTerm(
KeywordID keyword_id,
const base::string16& normalized_term);
// Deletes any search corresponding to |url_id|.
bool DeleteKeywordSearchTermForURL(URLID url_id);
......
......@@ -15,7 +15,6 @@ class AutocompleteProviderClient;
class AutocompleteProviderListener;
namespace history {
class HistoryDBTask;
class QueryResults;
} // namespace history
......@@ -42,9 +41,9 @@ class LocalHistoryZeroSuggestProvider : public AutocompleteProvider {
AutocompleteProviderListener* listener);
~LocalHistoryZeroSuggestProvider() override;
// Creates autocomplete matches from |url_matches| and notifies |listener_|.
void OnQueryURLDatabaseComplete(const AutocompleteInput& input,
const history::URLRows& url_matches);
// Queries the keyword search terms table of the in-memory URLDatabase for the
// recent search terms submitted to the default search provider.
void QueryURLDatabase(const AutocompleteInput& input);
// Called when the query results from HistoryService::QueryHistory are ready.
// Deletes URLs in |results| that would generate |suggestion|.
......@@ -54,10 +53,6 @@ class LocalHistoryZeroSuggestProvider : public AutocompleteProvider {
// The maximum number of matches to return.
const size_t max_matches_;
// Used to filter out deleted query suggestions in order to prevent them from
// reappearing before the corresponding URLs are asynchronously deleted.
std::set<base::string16> deleted_suggestions_set_;
// Client for accessing TemplateUrlService, prefs, etc.
AutocompleteProviderClient* const client_;
......@@ -67,9 +62,6 @@ class LocalHistoryZeroSuggestProvider : public AutocompleteProvider {
// Used for the async tasks querying the HistoryService.
base::CancelableTaskTracker history_task_tracker_;
// Task ID for the history::HistoryDBTask running on history backend thread.
base::CancelableTaskTracker::TaskId history_db_task_id_;
base::WeakPtrFactory<LocalHistoryZeroSuggestProvider> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(LocalHistoryZeroSuggestProvider);
......
......@@ -5,7 +5,9 @@
#include "components/omnibox/browser/local_history_zero_suggest_provider.h"
#include <memory>
#include <vector>
#include "base/i18n/case_conversion.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
......@@ -13,6 +15,7 @@
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/keyword_search_term.h"
#include "components/history/core/browser/url_database.h"
#include "components/history/core/test/history_service_test_util.h"
#include "components/omnibox/browser/autocomplete_match.h"
......@@ -70,6 +73,14 @@ class LocalHistoryZeroSuggestProviderTest
metrics::OmniboxEventProto::NTP_REALBOX,
LocalHistoryZeroSuggestProvider::kZeroSuggestLocalVariant);
// Add the fallback default search provider to the TemplateURLService so
// that it gets a valid unique identifier. Make the newly added provider the
// user selected default search provider.
TemplateURL* default_provider = client_->GetTemplateURLService()->Add(
std::make_unique<TemplateURL>(default_search_provider()->data()));
client_->GetTemplateURLService()->SetUserSelectedDefaultSearchProvider(
default_provider);
// Verify that Google is the default search provider.
ASSERT_EQ(SEARCH_ENGINE_GOOGLE,
default_search_provider()->GetEngineType(
......@@ -132,7 +143,6 @@ void LocalHistoryZeroSuggestProviderTest::SetZeroSuggestVariant(
void LocalHistoryZeroSuggestProviderTest::LoadURLs(
std::vector<TestURLData> url_data_list) {
const Time now = Time::Now();
history::URLRows rows;
for (const auto& entry : url_data_list) {
TemplateURLRef::SearchTermsArgs search_terms_args(
base::UTF8ToUTF16(entry.search_terms));
......@@ -141,17 +151,15 @@ void LocalHistoryZeroSuggestProviderTest::LoadURLs(
std::string search_url =
entry.search_provider->url_ref().ReplaceSearchTerms(search_terms_args,
search_terms_data);
history::URLRow row(GURL{search_url});
row.set_title(base::UTF8ToUTF16(entry.title));
row.set_visit_count(entry.visit_count);
row.set_typed_count(entry.typed_count);
row.set_last_visit(now - TimeDelta::FromDays(entry.age_in_days));
row.set_hidden(entry.hidden);
rows.push_back(row);
client_->GetHistoryService()->AddPageWithDetails(
GURL(search_url), base::UTF8ToUTF16(entry.title), entry.visit_count,
entry.typed_count, now - TimeDelta::FromDays(entry.age_in_days),
entry.hidden, history::SOURCE_BROWSED);
client_->GetHistoryService()->SetKeywordSearchTermsForURL(
GURL(search_url), entry.search_provider->id(),
base::UTF8ToUTF16(entry.search_terms));
WaitForHistoryService();
}
client_->GetHistoryService()->AddPagesWithDetails(rows,
history::SOURCE_BROWSED);
WaitForHistoryService();
}
void LocalHistoryZeroSuggestProviderTest::WaitForHistoryService() {
......@@ -259,7 +267,6 @@ TEST_F(LocalHistoryZeroSuggestProviderTest, DefaultSearchProvider) {
std::make_unique<TemplateURL>(*GenerateDummyTemplateURLData("other")));
LoadURLs({
{default_search_provider(), "hello world", "&foo=bar", 1},
{default_search_provider(), "", "&foo=bar", 1},
{other_search_provider, "does not matter", "&foo=bar", 1},
});
......@@ -277,16 +284,14 @@ TEST_F(LocalHistoryZeroSuggestProviderTest, DefaultSearchProvider) {
}
// Tests that search terms are extracted with the correct encoding, whitespaces
// are collapsed, search terms are lowercased and duplicated, and empty searches
// are ignored.
// are collapsed, and are lowercased and deduplicated.
TEST_F(LocalHistoryZeroSuggestProviderTest, SearchTerms) {
LoadURLs({
{default_search_provider(), "hello world", "&foo=bar", 1},
{default_search_provider(), "hello world", "&foo=bar", 1},
{default_search_provider(), "hello world", "&foo=bar", 1},
{default_search_provider(), "hello world", "&foo=bar", 1},
{default_search_provider(), "HELLO WORLD", "&foo=bar", 1},
{default_search_provider(), " ", "&foo=bar", 1},
{default_search_provider(), "HELLO WORLD ", "&foo=bar", 1},
{default_search_provider(), "سلام دنیا", "&foo=bar", 2},
});
......@@ -318,32 +323,53 @@ TEST_F(LocalHistoryZeroSuggestProviderTest, Suggestions_Freshness) {
ExpectMatches({{"fresh search", 500}});
}
// Tests that all the search URLs that would produce a given suggestion get
// deleted when the autocomplete match is deleted.
TEST_F(LocalHistoryZeroSuggestProviderTest, DISABLED_Delete) {
// Tests that the provider supports deletion of matches.
TEST_F(LocalHistoryZeroSuggestProviderTest, Delete) {
auto* template_url_service = client_->GetTemplateURLService();
auto* other_search_provider = template_url_service->Add(
std::make_unique<TemplateURL>(*GenerateDummyTemplateURLData("other")));
LoadURLs({
{default_search_provider(), "hello world", "&foo=bar&aqs=1", 1},
{default_search_provider(), "HELLO WORLD", "&foo=bar&aqs=12", 1},
{default_search_provider(), "HELLO WORLD ", "&foo=bar&aqs=12", 1},
{default_search_provider(), "hello world", "&foo=bar&aqs=123", 1},
{default_search_provider(), "hello world", "&foo=bar&aqs=1234", 1},
{default_search_provider(), "not to be deleted", "&foo=bar&aqs=12345", 1},
{other_search_provider, "hello world", "&foo=bar&aqs=123456", 1},
});
StartProviderAndWaitUntilDone();
ExpectMatches({{"hello world", 500}});
ExpectMatches({{"hello world", 500}, {"not to be deleted", 499}});
provider_->DeleteMatch(provider_->matches()[0]);
// Make sure the deletion takes effect immediately in the provider before the
// history service asynchronously performs the deletion or even before the
// provider is started again.
ExpectMatches({});
ExpectMatches({{"not to be deleted", 499}});
StartProviderAndWaitUntilDone();
ExpectMatches({});
ExpectMatches({{"not to be deleted", 500}});
// Wait until the history service performs the deletion.
WaitForHistoryService();
StartProviderAndWaitUntilDone();
ExpectMatches({});
ExpectMatches({{"not to be deleted", 500}});
// Make sure all the search terms for the default search provider that would
// produce the deleted match are deleted.
history::URLDatabase* url_db =
client_->GetHistoryService()->InMemoryDatabase();
std::vector<history::KeywordSearchTermVisit> visits =
url_db->GetMostRecentKeywordSearchTerms(default_search_provider()->id(),
/*max_count=*/10);
EXPECT_EQ(1U, visits.size());
EXPECT_EQ(base::ASCIIToUTF16("not to be deleted"), visits[0].term);
// Make sure search terms from other search providers that would produce the
// deleted match are not deleted.
visits = url_db->GetMostRecentKeywordSearchTerms(other_search_provider->id(),
/*max_count=*/10);
EXPECT_EQ(1U, visits.size());
EXPECT_EQ(base::ASCIIToUTF16("hello world"), visits[0].term);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment