Skip to content

Commit

Permalink
Makes the in memory db update rows that have search terms associated
Browse files Browse the repository at this point in the history
with them.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/6135001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70777 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sky@chromium.org committed Jan 7, 2011
1 parent 37539c7 commit b78f943
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions chrome/browser/history/history_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ KeywordSearchTermVisit::KeywordSearchTermVisit() {}

KeywordSearchTermVisit::~KeywordSearchTermVisit() {}

// KeywordSearchTermRow --------------------------------------------------------

KeywordSearchTermRow::KeywordSearchTermRow() : keyword_id(0), url_id(0) {}

KeywordSearchTermRow::~KeywordSearchTermRow() {}

// MostVisitedURL --------------------------------------------------------------

MostVisitedURL::MostVisitedURL() {}
Expand Down
18 changes: 18 additions & 0 deletions chrome/browser/history/history_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/string16.h"
#include "base/time.h"
#include "chrome/browser/history/snippet.h"
#include "chrome/browser/search_engines/template_url_id.h"
#include "chrome/common/page_transition_types.h"
#include "chrome/common/ref_counted_util.h"
#include "chrome/common/thumbnail_score.h"
Expand Down Expand Up @@ -523,6 +524,23 @@ struct KeywordSearchTermVisit {
string16 term;
};

// KeywordSearchTermRow --------------------------------------------------------

// Used for URLs that have a search term associated with them.
struct KeywordSearchTermRow {
KeywordSearchTermRow();
~KeywordSearchTermRow();

// ID of the keyword.
TemplateURLID keyword_id;

// ID of the url.
URLID url_id;

// The search term that was used.
string16 term;
};

// MostVisitedURL --------------------------------------------------------------

// Holds the per-URL information of the most visited query.
Expand Down
11 changes: 10 additions & 1 deletion chrome/browser/history/in_memory_history_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ void InMemoryHistoryBackend::Observe(NotificationType type,
PageTransition::Type primary_type =
PageTransition::StripQualifier(visited_details->transition);
if (visited_details->row.typed_count() > 0 ||
primary_type == PageTransition::KEYWORD) {
primary_type == PageTransition::KEYWORD ||
HasKeyword(visited_details->row.url())) {
URLsModifiedDetails modified_details;
modified_details.changed_urls.push_back(visited_details->row);
OnTypedURLsModified(modified_details);
Expand Down Expand Up @@ -186,4 +187,12 @@ void InMemoryHistoryBackend::OnKeywordSearchTermUpdated(
db_->SetKeywordSearchTermsForURL(url_id, details.keyword_id, details.term);
}

bool InMemoryHistoryBackend::HasKeyword(const GURL& url) {
URLID id = db_->GetRowForURL(url, NULL);
if (!id)
return false;

return db_->GetKeywordSearchTermRow(id, NULL);
}

} // namespace history
4 changes: 4 additions & 0 deletions chrome/browser/history/in_memory_history_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "chrome/common/notification_registrar.h"

class FilePath;
class GURL;
class HistoryDatabase;
class Profile;

Expand Down Expand Up @@ -77,6 +78,9 @@ class InMemoryHistoryBackend : public NotificationObserver {
// Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
void OnKeywordSearchTermUpdated(const KeywordSearchTermDetails& details);

// Returns true if there is a keyword associated with the specified url.
bool HasKeyword(const GURL& url);

NotificationRegistrar registrar_;

scoped_ptr<InMemoryDatabase> db_;
Expand Down
20 changes: 20 additions & 0 deletions chrome/browser/history/url_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,26 @@ bool URLDatabase::SetKeywordSearchTermsForURL(URLID url_id,
return statement.Run();
}

bool URLDatabase::GetKeywordSearchTermRow(URLID url_id,
KeywordSearchTermRow* row) {
DCHECK(url_id);
sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
"SELECT keyword_id, term FROM keyword_search_terms WHERE url_id=?"));
if (!statement)
return false;

statement.BindInt64(0, url_id);
if (!statement.Step())
return false;

if (row) {
row->url_id = url_id;
row->keyword_id = statement.ColumnInt64(0);
row->term = statement.ColumnString16(1);
}
return true;
}

void URLDatabase::DeleteAllSearchTermsForKeyword(
TemplateURLID keyword_id) {
DCHECK(keyword_id);
Expand Down
4 changes: 4 additions & 0 deletions chrome/browser/history/url_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ class URLDatabase {
TemplateURLID keyword_id,
const string16& term);

// Looks up a keyword search term given a url id. Fills row with the data.
// Returns true on success and false otherwise.
bool GetKeywordSearchTermRow(URLID url_id, KeywordSearchTermRow* row);

// Deletes all search terms for the specified keyword that have been added by
// way of SetKeywordSearchTermsForURL.
void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id);
Expand Down
20 changes: 15 additions & 5 deletions chrome/browser/history/url_database_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,31 @@ TEST_F(URLDatabaseTest, KeywordSearchTermVisit) {
ASSERT_TRUE(url_id != 0);

// Add a keyword visit.
ASSERT_TRUE(SetKeywordSearchTermsForURL(url_id, 1, UTF8ToUTF16("visit")));
TemplateURLID keyword_id = 100;
string16 keyword = UTF8ToUTF16("visit");
ASSERT_TRUE(SetKeywordSearchTermsForURL(url_id, keyword_id, keyword));

// Make sure we get it back.
std::vector<KeywordSearchTermVisit> matches;
GetMostRecentKeywordSearchTerms(1, UTF8ToUTF16("visit"), 10, &matches);
GetMostRecentKeywordSearchTerms(keyword_id, keyword, 10, &matches);
ASSERT_EQ(1U, matches.size());
ASSERT_EQ(UTF8ToUTF16("visit"), matches[0].term);
ASSERT_EQ(keyword, matches[0].term);

KeywordSearchTermRow keyword_search_term_row;
ASSERT_TRUE(GetKeywordSearchTermRow(url_id, &keyword_search_term_row));
EXPECT_EQ(keyword_id, keyword_search_term_row.keyword_id);
EXPECT_EQ(url_id, keyword_search_term_row.url_id);
EXPECT_EQ(keyword, keyword_search_term_row.term);

// Delete the keyword visit.
DeleteAllSearchTermsForKeyword(1);
DeleteAllSearchTermsForKeyword(keyword_id);

// Make sure we don't get it back when querying.
matches.clear();
GetMostRecentKeywordSearchTerms(1, UTF8ToUTF16("visit"), 10, &matches);
GetMostRecentKeywordSearchTerms(keyword_id, keyword, 10, &matches);
ASSERT_EQ(0U, matches.size());

ASSERT_FALSE(GetKeywordSearchTermRow(url_id, &keyword_search_term_row));
}

// Make sure deleting a URL also deletes a keyword visit.
Expand Down

0 comments on commit b78f943

Please sign in to comment.