diff options
author | skrul@chromium.org <skrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 16:53:22 +0000 |
---|---|---|
committer | skrul@chromium.org <skrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 16:53:22 +0000 |
commit | 6053575080ee0ce4f707377e5dfd4391381c3d74 (patch) | |
tree | 92236eb4018cc8b3022ffb099b278f04625cf176 /chrome/browser/webdata | |
parent | 6b85efac9815e66aa86064955d173aa9063a0578 (diff) | |
download | chromium_src-6053575080ee0ce4f707377e5dfd4391381c3d74.zip chromium_src-6053575080ee0ce4f707377e5dfd4391381c3d74.tar.gz chromium_src-6053575080ee0ce4f707377e5dfd4391381c3d74.tar.bz2 |
Send notifications when single items are removed from autofill.
This patch will send an AUTOFILL_ENTRIES_REMOVED when an autofill entry is removed via WebDataService::RemoveFormValueForElementName() is called (note that the next change will have this implemented for the bulk removal method). This change also includes a bit of refactoring on how the notification details are sent from the WDS thread back to the main thread. Previously I was sending the data back via a WDResult but that didn't work well when expanded to different notifications since there is so way to tell what a given request was for. So I added some fields to the WedDataRequest class to hold the affected keys and the notification type.
BUG=30168
Review URL: http://codereview.chromium.org/506001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34721 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/webdata')
-rw-r--r-- | chrome/browser/webdata/autofill_change.h | 35 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.cc | 31 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.h | 8 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service_unittest.cc | 56 |
4 files changed, 108 insertions, 22 deletions
diff --git a/chrome/browser/webdata/autofill_change.h b/chrome/browser/webdata/autofill_change.h new file mode 100644 index 0000000..2eb8d50 --- /dev/null +++ b/chrome/browser/webdata/autofill_change.h @@ -0,0 +1,35 @@ +// Copyright (c) 2009 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 CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ +#define CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ + +#include "chrome/browser/webdata/autofill_entry.h" + +class AutofillChange { + public: + typedef enum { + ADD, + UPDATE, + REMOVE + } Type; + + AutofillChange(Type type, const AutofillKey& key) + : type_(type), + key_(key) {} + virtual ~AutofillChange() {} + + Type type() const { return type_; } + const AutofillKey& key() const { return key_; } + + bool operator==(const AutofillChange& change) const { + return type_ == change.type() && key_ == change.key(); + } + + private: + Type type_; + AutofillKey key_; +}; + +#endif // CHROME_BROWSER_WEBDATA_AUTOFILL_CHANGE_H__ diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index b37dc06..ee1d4c9 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -7,6 +7,7 @@ #include "base/message_loop.h" #include "base/task.h" #include "base/thread.h" +#include "chrome/browser/webdata/autofill_change.h" #include "chrome/browser/webdata/autofill_entry.h" #include "chrome/browser/webdata/web_database.h" #include "chrome/common/chrome_constants.h" @@ -188,13 +189,13 @@ void WebDataService::RequestCompleted(Handle h) { // including the list of affected keys. const WDTypedResult* result = request->GetResult(); if (!request->IsCancelled() && result) { - if (result->GetType() == AUTOFILL_AFFECTED_KEYS) { - AutofillKeyList affected_keys = - static_cast<const WDResult<AutofillKeyList>*>(result)->GetValue(); + if (result->GetType() == AUTOFILL_CHANGES) { + AutofillChangeList changes = + static_cast<const WDResult<AutofillChangeList>*>(result)->GetValue(); NotificationService::current()->Notify( - NotificationType::AUTOFILL_ENTRIES_ADDED, + NotificationType::AUTOFILL_ENTRIES_CHANGED, NotificationService::AllSources(), - Details<AutofillKeyList>(&affected_keys)); + Details<AutofillChangeList>(&changes)); } } } @@ -619,14 +620,16 @@ void WebDataService::AddFormFieldValuesImpl( if (!db_->AddFormFieldValues(form_fields)) NOTREACHED(); - AutofillKeyList keys; + AutofillChangeList changes; for (std::vector<FormField>::const_iterator itr = form_fields.begin(); itr != form_fields.end(); itr++) { - keys.push_back(AutofillKey(itr->name(), itr->value())); + changes.push_back( + AutofillChange(AutofillChange::ADD, + AutofillKey(itr->name(), itr->value()))); } request->SetResult( - new WDResult<AutofillKeyList>(AUTOFILL_AFFECTED_KEYS, keys)); + new WDResult<AutofillChangeList>(AUTOFILL_CHANGES, changes)); ScheduleCommit(); } request->RequestComplete(); @@ -660,9 +663,17 @@ void WebDataService::RemoveFormValueForElementNameImpl( GenericRequest2<string16, string16>* request) { InitializeDatabaseIfNecessary(); if (db_ && !request->IsCancelled()) { - if (db_->RemoveFormElement(request->GetArgument1(), - request->GetArgument2())) + const string16& name = request->GetArgument1(); + const string16& value = request->GetArgument2(); + + if (db_->RemoveFormElement(name, value)) { + AutofillChangeList changes; + changes.push_back(AutofillChange(AutofillChange::REMOVE, + AutofillKey(name, value))); + request->SetResult( + new WDResult<AutofillChangeList>(AUTOFILL_CHANGES, changes)); ScheduleCommit(); + } } request->RequestComplete(); } diff --git a/chrome/browser/webdata/web_data_service.h b/chrome/browser/webdata/web_data_service.h index 74120a8..b87803c 100644 --- a/chrome/browser/webdata/web_data_service.h +++ b/chrome/browser/webdata/web_data_service.h @@ -15,12 +15,11 @@ #include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/form_field.h" -class AutofillKey; +class AutofillChange; #if defined(OS_WIN) struct IE7PasswordInfo; #endif class MessageLoop; -class NotificationType; class Task; class WebDatabase; @@ -61,9 +60,11 @@ typedef enum { #endif WEB_APP_IMAGES, // WDResult<WDAppImagesResult> AUTOFILL_VALUE_RESULT, // WDResult<std::vector<string16>> - AUTOFILL_AFFECTED_KEYS, // WDResult<std::vector<AutofillKey>> + AUTOFILL_CHANGES, // WDResult<std::vector<AutofillChange>> } WDResultType; +typedef std::vector<AutofillChange> AutofillChangeList; + // Result from GetWebAppImages. struct WDAppImagesResult { WDAppImagesResult() : has_all_images(false) {} @@ -427,7 +428,6 @@ class WebDataService : public base::RefCountedThreadSafe<WebDataService> { typedef GenericRequest2<std::vector<const TemplateURL*>, std::vector<TemplateURL*> > SetKeywordsRequest; - typedef std::vector<AutofillKey> AutofillKeyList; ~WebDataService(); // Initialize the database, if it hasn't already been initialized. diff --git a/chrome/browser/webdata/web_data_service_unittest.cc b/chrome/browser/webdata/web_data_service_unittest.cc index f4bf2e9..917abd2 100644 --- a/chrome/browser/webdata/web_data_service_unittest.cc +++ b/chrome/browser/webdata/web_data_service_unittest.cc @@ -12,6 +12,7 @@ #include "base/string16.h" #include "base/string_util.h" #include "chrome/browser/chrome_thread.h" +#include "chrome/browser/webdata/autofill_change.h" #include "chrome/browser/webdata/autofill_entry.h" #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/chrome_paths.h" @@ -24,11 +25,12 @@ #include "testing/gtest/include/gtest/gtest.h" #include "webkit/glue/form_field.h" +using testing::_; using testing::ElementsAreArray; using testing::Pointee; using testing::Property; -typedef std::vector<AutofillKey> AutofillKeyList; +typedef std::vector<AutofillChange> AutofillChangeList; ACTION(QuitUIMessageLoop) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); @@ -100,23 +102,23 @@ TEST_F(WebDataServiceTest, AutofillAdd) { const string16 kName2 = ASCIIToUTF16("name2"); const string16 kValue2 = ASCIIToUTF16("value2"); - const AutofillKey expected_keys[] = { - AutofillKey(kName1, kValue1), - AutofillKey(kName2, kValue2) + const AutofillChange expected_changes[] = { + AutofillChange(AutofillChange::ADD, AutofillKey(kName1, kValue1)), + AutofillChange(AutofillChange::ADD, AutofillKey(kName2, kValue2)) }; // This will verify that the correct notification is triggered, // passing the correct list of autofill keys in the details. EXPECT_CALL( observer_, - Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_ADDED), + Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), NotificationService::AllSources(), - Property(&Details<AutofillKeyList>::ptr, - Pointee(ElementsAreArray(expected_keys))))). + Property(&Details<const AutofillChangeList>::ptr, + Pointee(ElementsAreArray(expected_changes))))). WillOnce(QuitUIMessageLoop()); registrar_.Add(&observer_, - NotificationType::AUTOFILL_ENTRIES_ADDED, + NotificationType::AUTOFILL_ENTRIES_CHANGED, NotificationService::AllSources()); std::vector<webkit_glue::FormField> form_fields; @@ -148,3 +150,41 @@ TEST_F(WebDataServiceTest, AutofillAdd) { ASSERT_EQ(1U, consumer.values().size()); EXPECT_EQ(kValue1, consumer.values()[0]); } + +TEST_F(WebDataServiceTest, AutofillRemoveOne) { + const string16 kName1 = ASCIIToUTF16("name1"); + const string16 kValue1 = ASCIIToUTF16("value1"); + + // First add some values to autofill. + EXPECT_CALL(observer_, Observe(_, _, _)).WillOnce(QuitUIMessageLoop()); + registrar_.Add(&observer_, + NotificationType::AUTOFILL_ENTRIES_CHANGED, + NotificationService::AllSources()); + std::vector<webkit_glue::FormField> form_fields; + form_fields.push_back( + webkit_glue::FormField(EmptyString16(), + kName1, + EmptyString16(), + kValue1)); + wds_->AddFormFieldValues(form_fields); + + // The message loop will exit when the mock observer is notified. + MessageLoop::current()->Run(); + + // This will verify that the correct notification is triggered, + // passing the correct list of autofill keys in the details. + const AutofillChange expected_changes[] = { + AutofillChange(AutofillChange::REMOVE, AutofillKey(kName1, kValue1)) + }; + EXPECT_CALL( + observer_, + Observe(NotificationType(NotificationType::AUTOFILL_ENTRIES_CHANGED), + NotificationService::AllSources(), + Property(&Details<const AutofillChangeList>::ptr, + Pointee(ElementsAreArray(expected_changes))))). + WillOnce(QuitUIMessageLoop()); + wds_->RemoveFormValueForElementName(kName1, kValue1); + + // The message loop will exit when the mock observer is notified. + MessageLoop::current()->Run(); +} |