Commit 5c98f425 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Add password managent API to CWVAutofillDataManager

Change-Id: Id5d181754a143a2688b63812842d719cb12bc9c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1817022Reviewed-by: default avatarHiroshi Ichikawa <ichikawa@chromium.org>
Commit-Queue: John Wu <jzw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699632}
parent d5ca5fe6
...@@ -440,6 +440,7 @@ test("ios_web_view_unittests") { ...@@ -440,6 +440,7 @@ test("ios_web_view_unittests") {
"//components/autofill/ios/browser:test_support", "//components/autofill/ios/browser:test_support",
"//components/autofill/ios/form_util:test_support", "//components/autofill/ios/form_util:test_support",
"//components/browser_sync:test_support", "//components/browser_sync:test_support",
"//components/password_manager/core/browser:test_support",
"//components/prefs:test_support", "//components/prefs:test_support",
"//components/signin/public/base:test_support", "//components/signin/public/base:test_support",
"//components/signin/public/identity_manager:test_support", "//components/signin/public/identity_manager:test_support",
......
...@@ -10,24 +10,34 @@ ...@@ -10,24 +10,34 @@
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/personal_data_manager_observer.h" #include "components/autofill/core/browser/personal_data_manager_observer.h"
#include "components/password_manager/core/browser/password_store_consumer.h"
#include "components/password_manager/core/browser/password_store_default.h"
#include "ios/web/public/thread/web_task_traits.h" #include "ios/web/public/thread/web_task_traits.h"
#include "ios/web/public/thread/web_thread.h" #include "ios/web/public/thread/web_thread.h"
#import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h" #import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h"
#import "ios/web_view/internal/autofill/cwv_credit_card_internal.h" #import "ios/web_view/internal/autofill/cwv_credit_card_internal.h"
#import "ios/web_view/internal/passwords/cwv_password_internal.h"
#import "ios/web_view/public/cwv_autofill_data_manager_observer.h" #import "ios/web_view/public/cwv_autofill_data_manager_observer.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
// Typedefs of |completionHandler| in |fetchProfilesWithCompletionHandler:| // Typedefs of |completionHandler| in |fetchProfilesWithCompletionHandler:|,
// and |fetchCreditCardsWithCompletionHandler:|. // |fetchCreditCardsWithCompletionHandler:|, and
// |fetchPasswordsWithCompletionHandler|.
typedef void (^CWVFetchProfilesCompletionHandler)( typedef void (^CWVFetchProfilesCompletionHandler)(
NSArray<CWVAutofillProfile*>* profiles); NSArray<CWVAutofillProfile*>* profiles);
typedef void (^CWVFetchCreditCardsCompletionHandler)( typedef void (^CWVFetchCreditCardsCompletionHandler)(
NSArray<CWVCreditCard*>* creditCards); NSArray<CWVCreditCard*>* creditCards);
typedef void (^CWVFetchPasswordsCompletionHandler)(
NSArray<CWVPassword*>* passwords);
@interface CWVAutofillDataManager () @interface CWVAutofillDataManager ()
// Called when WebViewPasswordStoreConsumer's |OnGetPasswordStoreResults| is
// invoked.
- (void)handlePasswordStoreResults:(NSArray<CWVPassword*>*)passwords;
// Called when WebViewPersonalDataManagerObserverBridge's // Called when WebViewPersonalDataManagerObserverBridge's
// |OnPersonalDataChanged| is invoked. // |OnPersonalDataChanged| is invoked.
- (void)personalDataDidChange; - (void)personalDataDidChange;
...@@ -37,6 +47,7 @@ typedef void (^CWVFetchCreditCardsCompletionHandler)( ...@@ -37,6 +47,7 @@ typedef void (^CWVFetchCreditCardsCompletionHandler)(
// Collects and converts autofill::CreditCards stored internally in // Collects and converts autofill::CreditCards stored internally in
// |_personalDataManager| to CWVCreditCards. // |_personalDataManager| to CWVCreditCards.
- (NSArray<CWVCreditCard*>*)creditCards; - (NSArray<CWVCreditCard*>*)creditCards;
@end @end
namespace ios_web_view { namespace ios_web_view {
...@@ -61,6 +72,27 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -61,6 +72,27 @@ class WebViewPersonalDataManagerObserverBridge
private: private:
__weak CWVAutofillDataManager* data_manager_; __weak CWVAutofillDataManager* data_manager_;
}; };
// C++ to ObjC bridge for PasswordStoreConsumer.
class WebViewPasswordStoreConsumer
: public password_manager::PasswordStoreConsumer {
public:
explicit WebViewPasswordStoreConsumer(CWVAutofillDataManager* data_manager)
: data_manager_(data_manager) {}
void OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) override {
NSMutableArray<CWVPassword*>* passwords = [NSMutableArray array];
for (auto& form : results) {
CWVPassword* password = [[CWVPassword alloc] initWithPasswordForm:*form];
[passwords addObject:password];
}
[data_manager_ handlePasswordStoreResults:passwords];
}
private:
__weak CWVAutofillDataManager* data_manager_;
};
} // namespace ios_web_view } // namespace ios_web_view
@implementation CWVAutofillDataManager { @implementation CWVAutofillDataManager {
...@@ -73,20 +105,30 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -73,20 +105,30 @@ class WebViewPersonalDataManagerObserverBridge
_fetchProfilesCompletionHandlers; _fetchProfilesCompletionHandlers;
NSMutableArray<CWVFetchCreditCardsCompletionHandler>* NSMutableArray<CWVFetchCreditCardsCompletionHandler>*
_fetchCreditCardsCompletionHandlers; _fetchCreditCardsCompletionHandlers;
NSMutableArray<CWVFetchPasswordsCompletionHandler>*
_fetchPasswordsCompletionHandlers;
// Holds weak observers. // Holds weak observers.
NSHashTable<id<CWVAutofillDataManagerObserver>>* _observers; NSHashTable<id<CWVAutofillDataManagerObserver>>* _observers;
password_manager::PasswordStore* _passwordStore;
std::unique_ptr<ios_web_view::WebViewPasswordStoreConsumer>
_passwordStoreConsumer;
} }
- (instancetype)initWithPersonalDataManager: - (instancetype)initWithPersonalDataManager:
(autofill::PersonalDataManager*)personalDataManager { (autofill::PersonalDataManager*)personalDataManager
passwordStore:(password_manager::PasswordStore*)
passwordStore {
self = [super init]; self = [super init];
if (self) { if (self) {
_personalDataManager = personalDataManager; _personalDataManager = personalDataManager;
_passwordStore = passwordStore;
_personalDataManagerObserverBridge = std::make_unique< _personalDataManagerObserverBridge = std::make_unique<
ios_web_view::WebViewPersonalDataManagerObserverBridge>(self); ios_web_view::WebViewPersonalDataManagerObserverBridge>(self);
_personalDataManager->AddObserver(_personalDataManagerObserverBridge.get()); _personalDataManager->AddObserver(_personalDataManagerObserverBridge.get());
_fetchProfilesCompletionHandlers = [NSMutableArray array]; _fetchProfilesCompletionHandlers = [NSMutableArray array];
_fetchCreditCardsCompletionHandlers = [NSMutableArray array]; _fetchCreditCardsCompletionHandlers = [NSMutableArray array];
_fetchPasswordsCompletionHandlers = [NSMutableArray array];
_observers = [NSHashTable weakObjectsHashTable]; _observers = [NSHashTable weakObjectsHashTable];
} }
return self; return self;
...@@ -155,12 +197,39 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -155,12 +197,39 @@ class WebViewPersonalDataManagerObserverBridge
_personalDataManager->RemoveByGUID(creditCard.internalCard->guid()); _personalDataManager->RemoveByGUID(creditCard.internalCard->guid());
} }
- (void)fetchPasswordsWithCompletionHandler:
(void (^)(NSArray<CWVPassword*>* passwords))completionHandler {
[_fetchPasswordsCompletionHandlers addObject:completionHandler];
// Fetch is already pending.
if (_passwordStoreConsumer) {
return;
}
_passwordStoreConsumer.reset(
new ios_web_view::WebViewPasswordStoreConsumer(self));
_passwordStore->GetAllLogins(_passwordStoreConsumer.get());
}
- (void)deletePassword:(CWVPassword*)password {
_passwordStore->RemoveLogin(*[password internalPasswordForm]);
}
- (void)clearAllLocalData { - (void)clearAllLocalData {
_personalDataManager->ClearAllLocalData(); _personalDataManager->ClearAllLocalData();
} }
#pragma mark - Private Methods #pragma mark - Private Methods
- (void)handlePasswordStoreResults:(NSArray<CWVPassword*>*)passwords {
for (CWVFetchPasswordsCompletionHandler completionHandler in
_fetchPasswordsCompletionHandlers) {
completionHandler(passwords);
}
[_fetchPasswordsCompletionHandlers removeAllObjects];
_passwordStoreConsumer.reset();
}
- (void)personalDataDidChange { - (void)personalDataDidChange {
// Invoke completionHandlers if they are still outstanding. // Invoke completionHandlers if they are still outstanding.
if (_personalDataManager->IsDataLoaded()) { if (_personalDataManager->IsDataLoaded()) {
......
...@@ -11,14 +11,21 @@ namespace autofill { ...@@ -11,14 +11,21 @@ namespace autofill {
class PersonalDataManager; class PersonalDataManager;
} // namespace autofill; } // namespace autofill;
namespace password_manager {
class PasswordStore;
} // password_manager
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@interface CWVAutofillDataManager () @interface CWVAutofillDataManager ()
// |personalDataManager| The underlying personal data manager being wrapped. // |personalDataManager| The underlying personal data manager being wrapped.
// |passwordStore| The underlying password store being wrapped.
// It should outlive this instance. // It should outlive this instance.
- (instancetype)initWithPersonalDataManager: - (instancetype)initWithPersonalDataManager:
(autofill::PersonalDataManager*)personalDataManager (autofill::PersonalDataManager*)personalDataManager
passwordStore:(password_manager::PasswordStore*)
passwordStore
NS_DESIGNATED_INITIALIZER; NS_DESIGNATED_INITIALIZER;
@end @end
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h" #import "base/test/ios/wait_util.h"
...@@ -13,9 +14,12 @@ ...@@ -13,9 +14,12 @@
#include "components/autofill/core/browser/data_model/autofill_profile.h" #include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h" #include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/test_personal_data_manager.h" #include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/test_password_store.h"
#include "ios/web/public/test/web_task_environment.h" #include "ios/web/public/test/web_task_environment.h"
#import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h" #import "ios/web_view/internal/autofill/cwv_autofill_profile_internal.h"
#import "ios/web_view/internal/autofill/cwv_credit_card_internal.h" #import "ios/web_view/internal/autofill/cwv_credit_card_internal.h"
#import "ios/web_view/internal/passwords/cwv_password_internal.h"
#import "ios/web_view/public/cwv_autofill_data_manager_observer.h" #import "ios/web_view/public/cwv_autofill_data_manager_observer.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h" #import "testing/gtest_mac.h"
...@@ -54,8 +58,13 @@ class CWVAutofillDataManagerTest : public PlatformTest { ...@@ -54,8 +58,13 @@ class CWVAutofillDataManagerTest : public PlatformTest {
personal_data_manager_->SetAutofillCreditCardEnabled(true); personal_data_manager_->SetAutofillCreditCardEnabled(true);
personal_data_manager_->SetAutofillWalletImportEnabled(true); personal_data_manager_->SetAutofillWalletImportEnabled(true);
password_store_ = new password_manager::TestPasswordStore();
password_store_->Init(base::RepeatingCallback<void(syncer::ModelType)>(),
nullptr);
autofill_data_manager_ = [[CWVAutofillDataManager alloc] autofill_data_manager_ = [[CWVAutofillDataManager alloc]
initWithPersonalDataManager:personal_data_manager_.get()]; initWithPersonalDataManager:personal_data_manager_.get()
passwordStore:password_store_.get()];
} }
// Fetches profiles from |autofill_data_manager_| and returns them in // Fetches profiles from |autofill_data_manager_| and returns them in
...@@ -90,12 +99,45 @@ class CWVAutofillDataManagerTest : public PlatformTest { ...@@ -90,12 +99,45 @@ class CWVAutofillDataManagerTest : public PlatformTest {
}); });
} }
// Create a test password form for testing.
autofill::PasswordForm GetTestPassword() {
autofill::PasswordForm password_form;
password_form.origin = GURL("http://www.example.com/accounts/LoginAuth");
password_form.action = GURL("http://www.example.com/accounts/Login");
password_form.username_element = base::SysNSStringToUTF16(@"Email");
password_form.username_value = base::SysNSStringToUTF16(@"test@egmail.com");
password_form.password_element = base::SysNSStringToUTF16(@"Passwd");
password_form.password_value = base::SysNSStringToUTF16(@"test");
password_form.submit_element = base::SysNSStringToUTF16(@"signIn");
password_form.signon_realm = "http://www.example.com/";
password_form.preferred = false;
password_form.scheme = autofill::PasswordForm::Scheme::kHtml;
password_form.blacklisted_by_user = false;
return password_form;
}
// Fetches passwords from |autofill_data_manager_| and returns them.
NSArray<CWVPassword*>* FetchPasswords() WARN_UNUSED_RESULT {
__block NSArray<CWVPassword*>* fetched_passwords = nil;
[autofill_data_manager_ fetchPasswordsWithCompletionHandler:^(
NSArray<CWVPassword*>* passwords) {
fetched_passwords = passwords;
}];
EXPECT_TRUE(WaitUntilConditionOrTimeout(kWaitForActionTimeout, ^bool {
base::RunLoop().RunUntilIdle();
return fetched_passwords != nil;
}));
return fetched_passwords;
}
~CWVAutofillDataManagerTest() override { ~CWVAutofillDataManagerTest() override {
password_store_->ShutdownOnUIThread();
ui::ResourceBundle::CleanupSharedInstance(); ui::ResourceBundle::CleanupSharedInstance();
} }
web::WebTaskEnvironment task_environment_; web::WebTaskEnvironment task_environment_;
std::unique_ptr<autofill::TestPersonalDataManager> personal_data_manager_; std::unique_ptr<autofill::TestPersonalDataManager> personal_data_manager_;
scoped_refptr<password_manager::TestPasswordStore> password_store_;
CWVAutofillDataManager* autofill_data_manager_; CWVAutofillDataManager* autofill_data_manager_;
}; };
...@@ -202,6 +244,25 @@ TEST_F(CWVAutofillDataManagerTest, UpdateCreditCard) { ...@@ -202,6 +244,25 @@ TEST_F(CWVAutofillDataManagerTest, UpdateCreditCard) {
})); }));
} }
// Tests CWVAutofillDataManager properly returns passwords.
TEST_F(CWVAutofillDataManagerTest, ReturnPassword) {
autofill::PasswordForm test_password = GetTestPassword();
password_store_->AddLogin(test_password);
NSArray<CWVPassword*>* fetched_passwords = FetchPasswords();
EXPECT_EQ(1ul, fetched_passwords.count);
EXPECT_EQ(test_password, *[fetched_passwords[0] internalPasswordForm]);
}
// Tests CWVAutofillDataManager properly deletes passwords.
TEST_F(CWVAutofillDataManagerTest, DeletePassword) {
password_store_->AddLogin(GetTestPassword());
NSArray<CWVPassword*>* passwords = FetchPasswords();
ASSERT_EQ(1ul, passwords.count);
[autofill_data_manager_ deletePassword:passwords[0]];
passwords = FetchPasswords();
EXPECT_EQ(0ul, passwords.count);
}
// Tests CWVAutofillDataManager properly deletes all local data. // Tests CWVAutofillDataManager properly deletes all local data.
TEST_F(CWVAutofillDataManagerTest, ClearAllLocalData) { TEST_F(CWVAutofillDataManagerTest, ClearAllLocalData) {
personal_data_manager_->AddCreditCard(autofill::test::GetCreditCard()); personal_data_manager_->AddCreditCard(autofill::test::GetCreditCard());
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/password_manager/core/browser/password_store_default.h"
#include "components/sync/driver/sync_service.h" #include "components/sync/driver/sync_service.h"
#include "ios/web_view/cwv_web_view_buildflags.h" #include "ios/web_view/cwv_web_view_buildflags.h"
#include "ios/web_view/internal/app/application_context.h" #include "ios/web_view/internal/app/application_context.h"
...@@ -16,6 +18,7 @@ ...@@ -16,6 +18,7 @@
#import "ios/web_view/internal/cwv_preferences_internal.h" #import "ios/web_view/internal/cwv_preferences_internal.h"
#import "ios/web_view/internal/cwv_user_content_controller_internal.h" #import "ios/web_view/internal/cwv_user_content_controller_internal.h"
#import "ios/web_view/internal/cwv_web_view_internal.h" #import "ios/web_view/internal/cwv_web_view_internal.h"
#include "ios/web_view/internal/passwords/web_view_password_store_factory.h"
#include "ios/web_view/internal/signin/ios_web_view_signin_client.h" #include "ios/web_view/internal/signin/ios_web_view_signin_client.h"
#include "ios/web_view/internal/signin/web_view_identity_manager_factory.h" #include "ios/web_view/internal/signin/web_view_identity_manager_factory.h"
#include "ios/web_view/internal/signin/web_view_signin_client_factory.h" #include "ios/web_view/internal/signin/web_view_signin_client_factory.h"
...@@ -135,8 +138,12 @@ CWVWebViewConfiguration* gIncognitoConfiguration = nil; ...@@ -135,8 +138,12 @@ CWVWebViewConfiguration* gIncognitoConfiguration = nil;
autofill::PersonalDataManager* personalDataManager = autofill::PersonalDataManager* personalDataManager =
ios_web_view::WebViewPersonalDataManagerFactory::GetForBrowserState( ios_web_view::WebViewPersonalDataManagerFactory::GetForBrowserState(
self.browserState); self.browserState);
scoped_refptr<password_manager::PasswordStore> passwordStore =
ios_web_view::WebViewPasswordStoreFactory::GetForBrowserState(
self.browserState, ServiceAccessType::EXPLICIT_ACCESS);
_autofillDataManager = [[CWVAutofillDataManager alloc] _autofillDataManager = [[CWVAutofillDataManager alloc]
initWithPersonalDataManager:personalDataManager]; initWithPersonalDataManager:personalDataManager
passwordStore:passwordStore.get()];
} }
return _autofillDataManager; return _autofillDataManager;
} }
......
...@@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
@class CWVAutofillProfile; @class CWVAutofillProfile;
@class CWVCreditCard; @class CWVCreditCard;
@class CWVPassword;
@protocol CWVAutofillDataManagerObserver; @protocol CWVAutofillDataManagerObserver;
// Exposes saved autofill data such as address profiles and credit cards. // Exposes saved autofill data such as address profiles and credit cards.
...@@ -47,6 +48,13 @@ CWV_EXPORT ...@@ -47,6 +48,13 @@ CWV_EXPORT
// Deletes the card. // Deletes the card.
- (void)deleteCreditCard:(CWVCreditCard*)creditCard; - (void)deleteCreditCard:(CWVCreditCard*)creditCard;
// Returns all saved passwords for password autofill in |completionHandler|.
- (void)fetchPasswordsWithCompletionHandler:
(void (^)(NSArray<CWVPassword*>* passwords))completionHandler;
// Deletes the password.
- (void)deletePassword:(CWVPassword*)password;
// Deletes all locally saved data. // Deletes all locally saved data.
- (void)clearAllLocalData; - (void)clearAllLocalData;
......
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