Commit 6a6efca9 authored by John Z Wu's avatar John Z Wu Committed by Commit Bot

Change CWVAutofillDataManagerDelegate to CWVAutofillDataManagerObservers

Since there many be multiple objects interested in data changes, it's
more natural to use an observer pattern instead.

Change-Id: Ib7e291626bf3f3dce23a73ebc329f96bc8f9deb9
Reviewed-on: https://chromium-review.googlesource.com/c/1293990
Commit-Queue: John Wu <jzw@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601662}
parent 764c6648
...@@ -80,7 +80,7 @@ if (ios_web_view_enable_autofill) { ...@@ -80,7 +80,7 @@ if (ios_web_view_enable_autofill) {
"public/cwv_autofill_controller.h", "public/cwv_autofill_controller.h",
"public/cwv_autofill_controller_delegate.h", "public/cwv_autofill_controller_delegate.h",
"public/cwv_autofill_data_manager.h", "public/cwv_autofill_data_manager.h",
"public/cwv_autofill_data_manager_delegate.h", "public/cwv_autofill_data_manager_observer.h",
"public/cwv_autofill_profile.h", "public/cwv_autofill_profile.h",
"public/cwv_autofill_suggestion.h", "public/cwv_autofill_suggestion.h",
"public/cwv_credit_card.h", "public/cwv_credit_card.h",
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include "ios/web/public/web_thread.h" #include "ios/web/public/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/public/cwv_autofill_data_manager_delegate.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."
...@@ -73,10 +73,10 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -73,10 +73,10 @@ class WebViewPersonalDataManagerObserverBridge
_fetchProfilesCompletionHandlers; _fetchProfilesCompletionHandlers;
NSMutableArray<CWVFetchCreditCardsCompletionHandler>* NSMutableArray<CWVFetchCreditCardsCompletionHandler>*
_fetchCreditCardsCompletionHandlers; _fetchCreditCardsCompletionHandlers;
// Holds weak observers.
NSHashTable<id<CWVAutofillDataManagerObserver>>* _observers;
} }
@synthesize delegate = _delegate;
- (instancetype)initWithPersonalDataManager: - (instancetype)initWithPersonalDataManager:
(autofill::PersonalDataManager*)personalDataManager { (autofill::PersonalDataManager*)personalDataManager {
self = [super init]; self = [super init];
...@@ -87,6 +87,7 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -87,6 +87,7 @@ class WebViewPersonalDataManagerObserverBridge
_personalDataManager->AddObserver(_personalDataManagerObserverBridge.get()); _personalDataManager->AddObserver(_personalDataManagerObserverBridge.get());
_fetchProfilesCompletionHandlers = [NSMutableArray array]; _fetchProfilesCompletionHandlers = [NSMutableArray array];
_fetchCreditCardsCompletionHandlers = [NSMutableArray array]; _fetchCreditCardsCompletionHandlers = [NSMutableArray array];
_observers = [NSHashTable weakObjectsHashTable];
} }
return self; return self;
} }
...@@ -98,6 +99,14 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -98,6 +99,14 @@ class WebViewPersonalDataManagerObserverBridge
#pragma mark - Public Methods #pragma mark - Public Methods
- (void)addObserver:(__weak id<CWVAutofillDataManagerObserver>)observer {
[_observers addObject:observer];
}
- (void)removeObserver:(__weak id<CWVAutofillDataManagerObserver>)observer {
[_observers removeObject:observer];
}
- (void)fetchProfilesWithCompletionHandler: - (void)fetchProfilesWithCompletionHandler:
(void (^)(NSArray<CWVAutofillProfile*>* profiles))completionHandler { (void (^)(NSArray<CWVAutofillProfile*>* profiles))completionHandler {
// If data is already loaded, return the existing data asynchronously to match // If data is already loaded, return the existing data asynchronously to match
...@@ -172,7 +181,9 @@ class WebViewPersonalDataManagerObserverBridge ...@@ -172,7 +181,9 @@ class WebViewPersonalDataManagerObserverBridge
[_fetchCreditCardsCompletionHandlers removeAllObjects]; [_fetchCreditCardsCompletionHandlers removeAllObjects];
} }
} }
[_delegate autofillDataManagerDataDidChange:self]; for (id<CWVAutofillDataManagerObserver> observer in _observers) {
[observer autofillDataManagerDataDidChange:self];
}
} }
- (NSArray<CWVAutofillProfile*>*)profiles { - (NSArray<CWVAutofillProfile*>*)profiles {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "ios/web/public/test/test_web_thread_bundle.h" #include "ios/web/public/test/test_web_thread_bundle.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/public/cwv_autofill_data_manager_delegate.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"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -101,16 +101,20 @@ class CWVAutofillDataManagerTest : public PlatformTest { ...@@ -101,16 +101,20 @@ class CWVAutofillDataManagerTest : public PlatformTest {
// Tests CWVAutofillDataManager properly invokes did change callback. // Tests CWVAutofillDataManager properly invokes did change callback.
TEST_F(CWVAutofillDataManagerTest, DidChangeCallback) { TEST_F(CWVAutofillDataManagerTest, DidChangeCallback) {
id delegate = OCMProtocolMock(@protocol(CWVAutofillDataManagerDelegate)); // OCMock objects are often autoreleased, but it must be destroyed before this
autofill_data_manager_.delegate = delegate; // test exits to avoid holding on to |autofill_data_manager_|.
// [delegate expect] returns an autoreleased object, but it must be destroyed
// before this test exits to avoid holding on to |autofill_data_manager_|.
@autoreleasepool { @autoreleasepool {
[[delegate expect] autofillDataManagerDataDidChange:autofill_data_manager_]; id observer = OCMProtocolMock(@protocol(CWVAutofillDataManagerObserver));
[autofill_data_manager_ addObserver:observer];
[[observer expect] autofillDataManagerDataDidChange:autofill_data_manager_];
personal_data_manager_->AddProfile(autofill::test::GetFullProfile()); personal_data_manager_->AddProfile(autofill::test::GetFullProfile());
[observer verify];
[delegate verify]; [autofill_data_manager_ removeObserver:observer];
[[observer reject] autofillDataManagerDataDidChange:autofill_data_manager_];
personal_data_manager_->AddProfile(autofill::test::GetFullProfile2());
[observer verify];
} }
} }
......
...@@ -13,17 +13,20 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -13,17 +13,20 @@ NS_ASSUME_NONNULL_BEGIN
@class CWVAutofillProfile; @class CWVAutofillProfile;
@class CWVCreditCard; @class CWVCreditCard;
@protocol CWVAutofillDataManagerDelegate; @protocol CWVAutofillDataManagerObserver;
CWV_EXPORT CWV_EXPORT
// Exposes saved autofill data such as address profiles and credit cards. // Exposes saved autofill data such as address profiles and credit cards.
@interface CWVAutofillDataManager : NSObject @interface CWVAutofillDataManager : NSObject
// Delegate for CWVAutofillDataManagerDelegate.
@property(nonatomic, weak) id<CWVAutofillDataManagerDelegate> delegate;
- (instancetype)init NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE;
// Adds |observer| for data changes.
- (void)addObserver:(__weak id<CWVAutofillDataManagerObserver>)observer;
// Removes |observer| that was previously added with |addObserver|.
- (void)removeObserver:(__weak id<CWVAutofillDataManagerObserver>)observer;
// Returns all saved profiles for address autofill in |completionHandler|. // Returns all saved profiles for address autofill in |completionHandler|.
- (void)fetchProfilesWithCompletionHandler: - (void)fetchProfilesWithCompletionHandler:
(void (^)(NSArray<CWVAutofillProfile*>* profiles))completionHandler; (void (^)(NSArray<CWVAutofillProfile*>* profiles))completionHandler;
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_DELEGATE_H_ #ifndef IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_OBSERVER_H_
#define IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_DELEGATE_H_ #define IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_OBSERVER_H_
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
...@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@class CWVAutofillDataManager; @class CWVAutofillDataManager;
// Protocol to receive change notifications from CWVAutofillDataManager. // Protocol to receive change notifications from CWVAutofillDataManager.
@protocol CWVAutofillDataManagerDelegate<NSObject> @protocol CWVAutofillDataManagerObserver<NSObject>
// Called whenever CWVAutofillDataManager's autofill profiles or credit cards // Called whenever CWVAutofillDataManager's autofill profiles or credit cards
// have been loaded for the first time, added, deleted, or updated. // have been loaded for the first time, added, deleted, or updated.
...@@ -23,4 +23,4 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -23,4 +23,4 @@ NS_ASSUME_NONNULL_BEGIN
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
#endif // IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_DELEGATE_H_ #endif // IOS_WEB_VIEW_PUBLIC_CWV_AUTOFILL_DATA_MANAGER_OBSERVER_H_
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