summaryrefslogtreecommitdiffstats
path: root/components/autofill
diff options
context:
space:
mode:
authormsramek <msramek@chromium.org>2015-11-02 13:36:24 -0800
committerCommit bot <commit-bot@chromium.org>2015-11-02 21:37:12 +0000
commiteda92fb13e276b461fab3f2e1b7cfbdb8987cf93 (patch)
tree54df2b7531e7b5a6cb43247d07ddb6dae6ae470c /components/autofill
parent095c637c99379e32878a97e67d10dd6bf86169aa (diff)
downloadchromium_src-eda92fb13e276b461fab3f2e1b7cfbdb8987cf93.zip
chromium_src-eda92fb13e276b461fab3f2e1b7cfbdb8987cf93.tar.gz
chromium_src-eda92fb13e276b461fab3f2e1b7cfbdb8987cf93.tar.bz2
Add counting functionality for the autocomplete database.
...in order to show this count in the Clear Browsing Data dialog. HasFormElements() currently only has one callsite, so we can reuse and generalize it to return count instead. BUG=510028 Review URL: https://codereview.chromium.org/1413683007 Cr-Commit-Position: refs/heads/master@{#357435}
Diffstat (limited to 'components/autofill')
-rw-r--r--components/autofill/core/browser/webdata/autofill_table.cc28
-rw-r--r--components/autofill/core/browser/webdata/autofill_table.h11
-rw-r--r--components/autofill/core/browser/webdata/autofill_table_unittest.cc74
-rw-r--r--components/autofill/core/browser/webdata/autofill_webdata.h12
-rw-r--r--components/autofill/core/browser/webdata/autofill_webdata_backend_impl.cc20
-rw-r--r--components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h10
-rw-r--r--components/autofill/core/browser/webdata/autofill_webdata_service.cc16
-rw-r--r--components/autofill/core/browser/webdata/autofill_webdata_service.h6
8 files changed, 139 insertions, 38 deletions
diff --git a/components/autofill/core/browser/webdata/autofill_table.cc b/components/autofill/core/browser/webdata/autofill_table.cc
index 5d8bef7..99c49be 100644
--- a/components/autofill/core/browser/webdata/autofill_table.cc
+++ b/components/autofill/core/browser/webdata/autofill_table.cc
@@ -542,15 +542,6 @@ bool AutofillTable::GetFormValuesForElementName(
return succeeded;
}
-bool AutofillTable::HasFormElements() {
- sql::Statement s(db_->GetUniqueStatement("SELECT COUNT(*) FROM autofill"));
- if (!s.Step()) {
- NOTREACHED();
- return false;
- }
- return s.ColumnInt(0) > 0;
-}
-
bool AutofillTable::RemoveFormElementsAddedBetween(
const Time& delete_begin,
const Time& delete_end,
@@ -709,6 +700,25 @@ bool AutofillTable::AddFormFieldValuesTime(
return result;
}
+int AutofillTable::GetCountOfEntriesContainedBetween(
+ const Time& begin,
+ const Time& end) {
+ const time_t begin_time_t = begin.ToTimeT();
+ const time_t end_time_t = GetEndTime(end);
+
+ sql::Statement s(db_->GetUniqueStatement(
+ "SELECT COUNT(*) FROM autofill "
+ "WHERE date_created >= ? AND date_last_used < ?"));
+ s.BindInt64(0, begin_time_t);
+ s.BindInt64(1, end_time_t);
+
+ if (!s.Step()) {
+ NOTREACHED();
+ return false;
+ }
+ return s.ColumnInt(0);
+}
+
bool AutofillTable::GetAllAutofillEntries(std::vector<AutofillEntry>* entries) {
sql::Statement s(db_->GetUniqueStatement(
"SELECT name, value, date_created, date_last_used FROM autofill"));
diff --git a/components/autofill/core/browser/webdata/autofill_table.h b/components/autofill/core/browser/webdata/autofill_table.h
index 7f866b5..d2d6510 100644
--- a/components/autofill/core/browser/webdata/autofill_table.h
+++ b/components/autofill/core/browser/webdata/autofill_table.h
@@ -252,9 +252,6 @@ class AutofillTable : public WebDatabaseTable {
std::vector<base::string16>* values,
int limit);
- // Returns whether any form elements are stored in the database.
- bool HasFormElements();
-
// Removes rows from the autofill table if they were created on or after
// |delete_begin| and last used strictly before |delete_end|. For rows where
// the time range [date_created, date_last_used] overlaps with [delete_begin,
@@ -275,6 +272,11 @@ class AutofillTable : public WebDatabaseTable {
virtual bool RemoveFormElement(const base::string16& name,
const base::string16& value);
+ // Returns the number of autofill entries whose interval between creation date
+ // and last usage is entirely contained between [|begin|, |end|).
+ virtual int GetCountOfEntriesContainedBetween(const base::Time& begin,
+ const base::Time& end);
+
// Retrieves all of the entries in the autofill table.
virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
@@ -410,6 +412,9 @@ class AutofillTable : public WebDatabaseTable {
private:
FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
+ FRIEND_TEST_ALL_PREFIXES(
+ AutofillTableTest,
+ Autofill_GetCountOfEntriesContainedBetween);
FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
FRIEND_TEST_ALL_PREFIXES(
diff --git a/components/autofill/core/browser/webdata/autofill_table_unittest.cc b/components/autofill/core/browser/webdata/autofill_table_unittest.cc
index 07ac4d4..6e6e2d1 100644
--- a/components/autofill/core/browser/webdata/autofill_table_unittest.cc
+++ b/components/autofill/core/browser/webdata/autofill_table_unittest.cc
@@ -157,9 +157,7 @@ TEST_F(AutofillTableTest, Autofill) {
field.value = ASCIIToUTF16("Superman");
base::Time now = base::Time::Now();
base::TimeDelta two_seconds = base::TimeDelta::FromSeconds(2);
- EXPECT_FALSE(table_->HasFormElements());
EXPECT_TRUE(table_->AddFormFieldValue(field, &changes));
- EXPECT_TRUE(table_->HasFormElements());
std::vector<base::string16> v;
for (int i = 0; i < 5; ++i) {
field.value = ASCIIToUTF16("Clark Kent");
@@ -276,6 +274,78 @@ TEST_F(AutofillTableTest, Autofill) {
EXPECT_EQ(4U, v.size());
}
+TEST_F(AutofillTableTest, Autofill_GetCountOfEntriesContainedBetween) {
+ AutofillChangeList changes;
+ // This test makes time comparisons that are precise to a microsecond, but the
+ // database uses the time_t format which is only precise to a second.
+ // Make sure we use timestamps rounded to a second.
+ Time begin = Time::FromTimeT(Time::Now().ToTimeT());
+ Time now = begin;
+ TimeDelta second = TimeDelta::FromSeconds(1);
+
+ struct Entry {
+ const char* name;
+ const char* value;
+ } entries[] = {
+ { "Alter ego", "Superman" },
+ { "Name", "Superman" },
+ { "Name", "Clark Kent" },
+ { "Name", "Superman" },
+ { "Name", "Clark Sutter" },
+ { "Name", "Clark Kent" }
+ };
+
+ for (Entry entry : entries) {
+ FormFieldData field;
+ field.name = ASCIIToUTF16(entry.name);
+ field.value = ASCIIToUTF16(entry.value);
+ ASSERT_TRUE(table_->AddFormFieldValueTime(field, &changes, now));
+ now += second;
+ }
+
+ // Only "Alter ego" : "Superman" is entirely contained within the
+ // first second.
+ EXPECT_EQ(1, table_->GetCountOfEntriesContainedBetween(
+ begin, begin + second));
+
+ // No other entries are entirely contained within the first three seconds
+ // (note that the second time constraint is exclusive).
+ EXPECT_EQ(1, table_->GetCountOfEntriesContainedBetween(
+ begin, begin + 3 * second));
+
+ // "Name" : "Superman" is entirely contained within the first four seconds.
+ // We already have an entry for "Superman", but with different field name,
+ // so we should now count two different entries.
+ EXPECT_EQ(2, table_->GetCountOfEntriesContainedBetween(
+ begin, begin + 4 * second));
+
+ // "Name" : {"Superman", "Clark Kent", "Clark Sutter"} are contained between
+ // the first and seventh second.
+ EXPECT_EQ(3, table_->GetCountOfEntriesContainedBetween(
+ begin + second, begin + 7 * second));
+
+ // Beginning from the second second, "Name" : "Superman" is not contained.
+ EXPECT_EQ(2, table_->GetCountOfEntriesContainedBetween(
+ begin + 2 * second, begin + 7 * second));
+
+ // We have four entries total.
+ EXPECT_EQ(4, table_->GetCountOfEntriesContainedBetween(
+ begin, begin + 7 * second));
+
+ // And we should get the same result for unlimited time interval.
+ EXPECT_EQ(4, table_->GetCountOfEntriesContainedBetween(Time(), Time::Max()));
+
+ // The null time interval is also interpreted as unlimited.
+ EXPECT_EQ(4, table_->GetCountOfEntriesContainedBetween(Time(), Time()));
+
+ // An interval that does not fully contain any entries returns zero.
+ EXPECT_EQ(0, table_->GetCountOfEntriesContainedBetween(
+ begin + second, begin + 2 * second));
+
+ // So does an interval which has no intersection with any entry.
+ EXPECT_EQ(0, table_->GetCountOfEntriesContainedBetween(Time(), begin));
+}
+
TEST_F(AutofillTableTest, Autofill_RemoveBetweenChanges) {
TimeDelta one_day(TimeDelta::FromDays(1));
Time t1 = Time::Now();
diff --git a/components/autofill/core/browser/webdata/autofill_webdata.h b/components/autofill/core/browser/webdata/autofill_webdata.h
index e2c121e..310b0ec 100644
--- a/components/autofill/core/browser/webdata/autofill_webdata.h
+++ b/components/autofill/core/browser/webdata/autofill_webdata.h
@@ -48,10 +48,6 @@ class AutofillWebData {
int limit,
WebDataServiceConsumer* consumer) = 0;
- // Checks if there are any form elements in the database.
- virtual WebDataServiceBase::Handle HasFormElements(
- WebDataServiceConsumer* consumer) = 0;
-
// Removes form elements recorded for Autocomplete from the database.
virtual void RemoveFormElementsAddedBetween(
const base::Time& delete_begin, const base::Time& delete_end) = 0;
@@ -78,6 +74,14 @@ class AutofillWebData {
virtual WebDataServiceBase::Handle GetServerProfiles(
WebDataServiceConsumer* consumer) = 0;
+ // Schedules a task to count the number of entries contained in the time
+ // interval [|begin|, |end|). |begin| and |end| can be null to indicate
+ // no time limitation.
+ virtual WebDataServiceBase::Handle GetCountOfEntriesContainedBetween(
+ const base::Time& begin,
+ const base::Time& end,
+ WebDataServiceConsumer* consumer) = 0;
+
// Schedules a task to update autofill entries in the web database.
virtual void UpdateAutofillEntries(
const std::vector<AutofillEntry>& autofill_entries) = 0;
diff --git a/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.cc b/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.cc
index 1f0309d..c696cde 100644
--- a/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.cc
+++ b/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.cc
@@ -120,14 +120,6 @@ AutofillWebDataBackendImpl::GetFormValuesForElementName(
values));
}
-scoped_ptr<WDTypedResult> AutofillWebDataBackendImpl::HasFormElements(
- WebDatabase* db) {
- DCHECK(db_thread_->BelongsToCurrentThread());
- bool value = AutofillTable::FromWebDatabase(db)->HasFormElements();
- return scoped_ptr<WDTypedResult>(
- new WDResult<bool>(AUTOFILL_VALUE_RESULT, value));
-}
-
WebDatabase::State AutofillWebDataBackendImpl::RemoveFormElementsAddedBetween(
const base::Time& delete_begin,
const base::Time& delete_end,
@@ -263,6 +255,18 @@ scoped_ptr<WDTypedResult> AutofillWebDataBackendImpl::GetServerProfiles(
base::Unretained(this))));
}
+scoped_ptr<WDTypedResult>
+ AutofillWebDataBackendImpl::GetCountOfEntriesContainedBetween(
+ const base::Time& begin,
+ const base::Time& end,
+ WebDatabase* db) {
+ DCHECK(db_thread_->BelongsToCurrentThread());
+ int value = AutofillTable::FromWebDatabase(db)
+ ->GetCountOfEntriesContainedBetween(begin, end);
+ return scoped_ptr<WDTypedResult>(
+ new WDResult<int>(AUTOFILL_VALUE_RESULT, value));
+}
+
WebDatabase::State AutofillWebDataBackendImpl::UpdateAutofillEntries(
const std::vector<AutofillEntry>& autofill_entries,
WebDatabase* db) {
diff --git a/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h b/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h
index bfde866..fbd1c9c 100644
--- a/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h
+++ b/components/autofill/core/browser/webdata/autofill_webdata_backend_impl.h
@@ -80,9 +80,6 @@ class AutofillWebDataBackendImpl
int limit,
WebDatabase* db);
- // Returns true if there are any elements in the form.
- scoped_ptr<WDTypedResult> HasFormElements(WebDatabase* db);
-
// Removes form elements recorded for Autocomplete from the database.
WebDatabase::State RemoveFormElementsAddedBetween(
const base::Time& delete_begin,
@@ -114,6 +111,13 @@ class AutofillWebDataBackendImpl
scoped_ptr<WDTypedResult> GetAutofillProfiles(WebDatabase* db);
scoped_ptr<WDTypedResult> GetServerProfiles(WebDatabase* db);
+ // Returns the number of autofill entries whose interval between creation date
+ // and last usage is entirely contained between [|begin|, |end|).
+ scoped_ptr<WDTypedResult> GetCountOfEntriesContainedBetween(
+ const base::Time& begin,
+ const base::Time& end,
+ WebDatabase* db);
+
// Updates Autofill entries in the web database.
WebDatabase::State UpdateAutofillEntries(
const std::vector<autofill::AutofillEntry>& autofill_entries,
diff --git a/components/autofill/core/browser/webdata/autofill_webdata_service.cc b/components/autofill/core/browser/webdata/autofill_webdata_service.cc
index a4d8dd2..cd66d55 100644
--- a/components/autofill/core/browser/webdata/autofill_webdata_service.cc
+++ b/components/autofill/core/browser/webdata/autofill_webdata_service.cc
@@ -81,13 +81,6 @@ WebDataServiceBase::Handle AutofillWebDataService::GetFormValuesForElementName(
autofill_backend_, name, prefix, limit), consumer);
}
-WebDataServiceBase::Handle AutofillWebDataService::HasFormElements(
- WebDataServiceConsumer* consumer) {
- return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
- Bind(&AutofillWebDataBackendImpl::HasFormElements, autofill_backend_),
- consumer);
-}
-
void AutofillWebDataService::RemoveFormElementsAddedBetween(
const Time& delete_begin, const Time& delete_end) {
wdbs_->ScheduleDBTask(FROM_HERE,
@@ -138,6 +131,15 @@ WebDataServiceBase::Handle AutofillWebDataService::GetServerProfiles(
consumer);
}
+WebDataServiceBase::Handle
+ AutofillWebDataService::GetCountOfEntriesContainedBetween(
+ const Time& begin, const Time& end, WebDataServiceConsumer* consumer) {
+ return wdbs_->ScheduleDBTaskWithResult(FROM_HERE,
+ Bind(&AutofillWebDataBackendImpl::GetCountOfEntriesContainedBetween,
+ autofill_backend_, begin, end),
+ consumer);
+}
+
void AutofillWebDataService::UpdateAutofillEntries(
const std::vector<AutofillEntry>& autofill_entries) {
wdbs_->ScheduleDBTask(FROM_HERE,
diff --git a/components/autofill/core/browser/webdata/autofill_webdata_service.h b/components/autofill/core/browser/webdata/autofill_webdata_service.h
index 8b42264..00ea4ed 100644
--- a/components/autofill/core/browser/webdata/autofill_webdata_service.h
+++ b/components/autofill/core/browser/webdata/autofill_webdata_service.h
@@ -57,8 +57,6 @@ class AutofillWebDataService : public AutofillWebData,
int limit,
WebDataServiceConsumer* consumer) override;
- WebDataServiceBase::Handle HasFormElements(
- WebDataServiceConsumer* consumer) override;
void RemoveFormElementsAddedBetween(const base::Time& delete_begin,
const base::Time& delete_end) override;
void RemoveFormValueForElementName(const base::string16& name,
@@ -75,6 +73,10 @@ class AutofillWebDataService : public AutofillWebData,
WebDataServiceBase::Handle GetServerProfiles(
WebDataServiceConsumer* consumer) override;
+ WebDataServiceBase::Handle GetCountOfEntriesContainedBetween(
+ const base::Time& begin,
+ const base::Time& end,
+ WebDataServiceConsumer* consumer) override;
void UpdateAutofillEntries(
const std::vector<AutofillEntry>& autofill_entries) override;