diff options
author | skrul@chromium.org <skrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 16:13:09 +0000 |
---|---|---|
committer | skrul@chromium.org <skrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-16 16:13:09 +0000 |
commit | 5db3af57ca8994249f0ba0efbb60ac103f7f0934 (patch) | |
tree | 9447bf957fd4d3e2ab18b726a29626e4863bb8cd | |
parent | 6ed318fb94e4cd8467ee2fc8b681b14668f6a76b (diff) | |
download | chromium_src-5db3af57ca8994249f0ba0efbb60ac103f7f0934.zip chromium_src-5db3af57ca8994249f0ba0efbb60ac103f7f0934.tar.gz chromium_src-5db3af57ca8994249f0ba0efbb60ac103f7f0934.tar.bz2 |
Add details to autofill add notification
This change adds a details payload containing the list of modified keys to the autofill add notification. The existing mechanism for passing values back from query methods (WDResult) is reused here to pass the modified keys back to the caller.
BUG=29606
Review URL: http://codereview.chromium.org/477009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34717 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/webdata/autofill_entry.h | 42 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.cc | 41 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service.h | 20 | ||||
-rw-r--r-- | chrome/browser/webdata/web_data_service_unittest.cc | 65 | ||||
-rw-r--r-- | chrome/common/notification_type.h | 9 |
5 files changed, 135 insertions, 42 deletions
diff --git a/chrome/browser/webdata/autofill_entry.h b/chrome/browser/webdata/autofill_entry.h new file mode 100644 index 0000000..9aadc3a --- /dev/null +++ b/chrome/browser/webdata/autofill_entry.h @@ -0,0 +1,42 @@ +// 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_ENTRY_H__ +#define CHROME_BROWSER_WEBDATA_AUTOFILL_ENTRY_H__ + +#include "base/string16.h" + +class AutofillKey { + public: + AutofillKey(const string16& name, const string16& value) + : name_(name), + value_(value) {} + AutofillKey(const AutofillKey& key) + : name_(key.name()), + value_(key.value()) {} + virtual ~AutofillKey() {} + + const string16& name() const { return name_; } + const string16& value() const { return value_; } + + bool operator==(const AutofillKey& key) const { + return name_ == key.name() && value_ == key.value(); + } + + private: + string16 name_; + string16 value_; +}; + +class AutofillEntry { + public: + explicit AutofillEntry(const AutofillKey& key) : key_(key) {} + + const AutofillKey& key() const { return key_; } + + private: + AutofillKey key_; +}; + +#endif // CHROME_BROWSER_WEBDATA_AUTOFILL_ENTRY_H__ diff --git a/chrome/browser/webdata/web_data_service.cc b/chrome/browser/webdata/web_data_service.cc index 380613b..b37dc06 100644 --- a/chrome/browser/webdata/web_data_service.cc +++ b/chrome/browser/webdata/web_data_service.cc @@ -7,8 +7,10 @@ #include "base/message_loop.h" #include "base/task.h" #include "base/thread.h" +#include "chrome/browser/webdata/autofill_entry.h" #include "chrome/browser/webdata/web_database.h" #include "chrome/common/chrome_constants.h" +#include "chrome/common/notification_details.h" #include "chrome/common/notification_service.h" #include "chrome/common/notification_type.h" #include "webkit/glue/password_form.h" @@ -120,12 +122,6 @@ void WebDataService::CancelRequest(Handle h) { i->second->Cancel(); } -void WebDataService::Notify(NotificationType type) { - NotificationService::current()->Notify(type, - NotificationService::AllSources(), - NotificationService::NoDetails()); -} - void WebDataService::AddFormFieldValues( const std::vector<FormField>& element) { GenericRequest<std::vector<FormField> >* request = @@ -188,11 +184,19 @@ void WebDataService::RequestCompleted(Handle h) { request->GetResult()); } - // If this is an autofill request, post the notifications. - if (!request->IsCancelled() && - request->GetResult() && - request->GetResult()->GetType() == AUTOFILL_VALUE_RESULT) - Notify(NotificationType::AUTOFILL_CHANGED); + // If this is an autofill change request, post the notifications + // 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(); + NotificationService::current()->Notify( + NotificationType::AUTOFILL_ENTRIES_ADDED, + NotificationService::AllSources(), + Details<AutofillKeyList>(&affected_keys)); + } + } } ////////////////////////////////////////////////////////////////////////////// @@ -610,9 +614,20 @@ void WebDataService::GetBlacklistLoginsImpl(WebDataRequest* request) { void WebDataService::AddFormFieldValuesImpl( GenericRequest<std::vector<FormField> >* request) { InitializeDatabaseIfNecessary(); + const std::vector<FormField>& form_fields = request->GetArgument(); if (db_ && !request->IsCancelled()) { - if (db_->AddFormFieldValues(request->GetArgument())) - ScheduleCommit(); + if (!db_->AddFormFieldValues(form_fields)) + NOTREACHED(); + + AutofillKeyList keys; + for (std::vector<FormField>::const_iterator itr = form_fields.begin(); + itr != form_fields.end(); + itr++) { + keys.push_back(AutofillKey(itr->name(), itr->value())); + } + request->SetResult( + new WDResult<AutofillKeyList>(AUTOFILL_AFFECTED_KEYS, keys)); + ScheduleCommit(); } request->RequestComplete(); } diff --git a/chrome/browser/webdata/web_data_service.h b/chrome/browser/webdata/web_data_service.h index 8735c45..74120a8 100644 --- a/chrome/browser/webdata/web_data_service.h +++ b/chrome/browser/webdata/web_data_service.h @@ -15,6 +15,7 @@ #include "third_party/skia/include/core/SkBitmap.h" #include "webkit/glue/form_field.h" +class AutofillKey; #if defined(OS_WIN) struct IE7PasswordInfo; #endif @@ -51,15 +52,16 @@ struct PasswordForm; // Result types // typedef enum { - BOOL_RESULT = 1, // WDResult<bool> - KEYWORDS_RESULT, // WDResult<WDKeywordsResult> - INT64_RESULT, // WDResult<int64> - PASSWORD_RESULT, // WDResult<std::vector<PasswordForm*>> + BOOL_RESULT = 1, // WDResult<bool> + KEYWORDS_RESULT, // WDResult<WDKeywordsResult> + INT64_RESULT, // WDResult<int64> + PASSWORD_RESULT, // WDResult<std::vector<PasswordForm*>> #if defined(OS_WIN) - PASSWORD_IE7_RESULT, // WDResult<IE7PasswordInfo> + PASSWORD_IE7_RESULT, // WDResult<IE7PasswordInfo> #endif - WEB_APP_IMAGES, // WDResult<WDAppImagesResult> - AUTOFILL_VALUE_RESULT, // WDResult<std::vector<string16>> + WEB_APP_IMAGES, // WDResult<WDAppImagesResult> + AUTOFILL_VALUE_RESULT, // WDResult<std::vector<string16>> + AUTOFILL_AFFECTED_KEYS, // WDResult<std::vector<AutofillKey>> } WDResultType; // Result from GetWebAppImages. @@ -373,9 +375,6 @@ class WebDataService : public base::RefCountedThreadSafe<WebDataService> { // WebDataServiceConsumer is about to be deleted. void CancelRequest(Handle h); - // Sends a notification using the notification service. - void Notify(NotificationType type); - ////////////////////////////////////////////////////////////////////////////// // // Autofill. @@ -428,6 +427,7 @@ 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 29f7bdf..f4bf2e9 100644 --- a/chrome/browser/webdata/web_data_service_unittest.cc +++ b/chrome/browser/webdata/web_data_service_unittest.cc @@ -12,8 +12,10 @@ #include "base/string16.h" #include "base/string_util.h" #include "chrome/browser/chrome_thread.h" +#include "chrome/browser/webdata/autofill_entry.h" #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/chrome_paths.h" +#include "chrome/common/notification_details.h" #include "chrome/common/notification_observer_mock.h" #include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" @@ -22,7 +24,11 @@ #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; ACTION(QuitUIMessageLoop) { DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); @@ -31,7 +37,7 @@ ACTION(QuitUIMessageLoop) { class AutofillWebDataServiceConsumer: public WebDataServiceConsumer { public: - AutofillWebDataServiceConsumer() {} + AutofillWebDataServiceConsumer() : handle_(0) {} virtual ~AutofillWebDataServiceConsumer() {} virtual void OnWebDataServiceRequestDone(WebDataService::Handle handle, @@ -43,6 +49,9 @@ class AutofillWebDataServiceConsumer: public WebDataServiceConsumer { static_cast<const WDResult<std::vector<string16> >*>(result); // Copy the values. values_ = autofill_result->GetValue(); + + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + MessageLoop::current()->Quit(); } WebDataService::Handle const handle() { return handle_; } @@ -86,32 +95,56 @@ class WebDataServiceTest : public testing::Test { }; TEST_F(WebDataServiceTest, AutofillAdd) { - EXPECT_CALL(observer_, - Observe(NotificationType(NotificationType::AUTOFILL_CHANGED), - NotificationService::AllSources(), - NotificationService::NoDetails())). + const string16 kName1 = ASCIIToUTF16("name1"); + const string16 kValue1 = ASCIIToUTF16("value1"); + const string16 kName2 = ASCIIToUTF16("name2"); + const string16 kValue2 = ASCIIToUTF16("value2"); + + const AutofillKey expected_keys[] = { + AutofillKey(kName1, kValue1), + 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), + NotificationService::AllSources(), + Property(&Details<AutofillKeyList>::ptr, + Pointee(ElementsAreArray(expected_keys))))). WillOnce(QuitUIMessageLoop()); + registrar_.Add(&observer_, - NotificationType::AUTOFILL_CHANGED, + NotificationType::AUTOFILL_ENTRIES_ADDED, NotificationService::AllSources()); std::vector<webkit_glue::FormField> form_fields; - form_fields.push_back(webkit_glue::FormField(EmptyString16(), - ASCIIToUTF16("name"), - EmptyString16(), - ASCIIToUTF16("value"))); + form_fields.push_back( + webkit_glue::FormField(EmptyString16(), + kName1, + EmptyString16(), + kValue1)); + form_fields.push_back( + webkit_glue::FormField(EmptyString16(), + kName2, + EmptyString16(), + kValue2)); wds_->AddFormFieldValues(form_fields); + // The message loop will exit when the mock observer is notified. + MessageLoop::current()->Run(); + AutofillWebDataServiceConsumer consumer; WebDataService::Handle handle; static const int limit = 10; - handle = wds_->GetFormValuesForElementName(ASCIIToUTF16("name"), - EmptyString16(), - limit, - &consumer); + handle = wds_->GetFormValuesForElementName( + kName1, EmptyString16(), limit, &consumer); + + // The message loop will exit when the consumer is called. MessageLoop::current()->Run(); EXPECT_EQ(handle, consumer.handle()); ASSERT_EQ(1U, consumer.values().size()); - EXPECT_EQ(ASCIIToUTF16("value"), consumer.values()[0]); + EXPECT_EQ(kValue1, consumer.values()[0]); } diff --git a/chrome/common/notification_type.h b/chrome/common/notification_type.h index 3bd5c4b..3ec4a4c 100644 --- a/chrome/common/notification_type.h +++ b/chrome/common/notification_type.h @@ -782,9 +782,12 @@ class NotificationType { // (the pointer is usable). No details are expected. NOTIFY_BALLOON_DISCONNECTED, - // This notification is sent whenever the list of autofill values - // has been changed. - AUTOFILL_CHANGED, + // This notification is sent whenever autofill entries are added + // to the database. The detail of this notification is a list of + // the affected entries represented by a vector of AutofillKey. + // Note that this notification will always be sent even in the + // autofill entries already exist in the database. + AUTOFILL_ENTRIES_ADDED, // Count (must be last) ---------------------------------------------------- // Used to determine the number of notification types. Not valid as |