diff --git a/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm index 86da7671cc8..675f3a375e9 100644 --- a/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm +++ b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm @@ -7,14 +7,20 @@ #ifdef FB_SONARKIT_ENABLED -#import "FlipperKitCookiesPlugin.h" +#import + #import #import #import +#import "FlipperKitCookiesPlugin.h" #import "Plugins.h" +@interface FlipperKitCookiesPlugin () +@end + @implementation FlipperKitCookiesPlugin { id _connection; + NSArray* _previousCookies; } - (NSString*)identifier { @@ -22,44 +28,103 @@ - (NSString*)identifier { } - (void)didConnect:(id)connection { - _connection = connection; - [self _sendCookies]; + __weak __typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf _didConnect:connection]; + }); +} + +- (void)didDisconnect { + __weak __typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf _didDisconnect]; + }); +} + +#pragma mark - NSHTTP Cookie storage observer + +- (void)onNSHTTPCookieStorageChange:(NSNotification*)notification { + __weak __typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf _refreshCookies]; + }); +} + +#pragma mark - WKHTTPCookieStoreObserver + +- (void)cookiesDidChangeInCookieStore:(WKHTTPCookieStore*)cookieStore { + __weak __typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf _refreshCookies]; + }); +} + +#pragma mark - helper + +- (void)_didConnect:(id)connection { + self->_connection = connection; [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(onCookieStorageChange:) + selector:@selector(onNSHTTPCookieStorageChange:) name:NSHTTPCookieManagerCookiesChangedNotification object:nil]; + [[[WKWebsiteDataStore defaultDataStore] httpCookieStore] addObserver:self]; + [self _refreshCookies]; } -- (void)didDisconnect { +- (void)_didDisconnect { + self->_connection = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; - _connection = nil; + [[[WKWebsiteDataStore defaultDataStore] httpCookieStore] removeObserver:self]; + _previousCookies = nil; } -#pragma mark - cookie storage observer +- (void)_refreshCookies { + // get Cookies from NS + NSArray* _Nullable NSCookies = + [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; + + // get Cookies from WebKit + [[[WKWebsiteDataStore defaultDataStore] httpCookieStore] + getAllCookies:^(NSArray* WKCookies) { + // combine and sort cookies based on name - preferring WK + NSMutableDictionary* cookies = + [NSMutableDictionary dictionary]; + for (NSHTTPCookie* cookie in NSCookies) { + cookies[cookie.name] = cookie; + } + for (NSHTTPCookie* cookie in WKCookies) { + cookies[cookie.name] = cookie; + } + NSArray* sortedCookies = [cookies.allValues + sortedArrayUsingComparator:^NSComparisonResult( + NSHTTPCookie* cookie1, NSHTTPCookie* cookie2) { + return [cookie1.name compare:cookie2.name]; + }]; -- (void)onCookieStorageChange:(NSNotification*)notification { - [self _sendCookies]; + [self _sendCookies:sortedCookies]; + }]; } -#pragma mark - helper +- (void)_sendCookies:(NSArray*)sortedCookies { + if ([_previousCookies isEqualToArray:sortedCookies]) { + return; + } -- (void)_sendCookies { + // deliver to flipper [_connection send:@"resetCookies" withParams:@{}]; - - NSInteger i = 1; - NSArray* _Nullable cookies = - [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] copy]; - for (NSHTTPCookie* cookie in cookies) { + NSInteger rowid = 1; + for (NSHTTPCookie* cookie in sortedCookies) { NSMutableDictionary* dict = [NSMutableDictionary dictionary]; - dict[@"id"] = @(i); + dict[@"id"] = @(rowid++); dict[@"Name"] = cookie.name; dict[@"Expires"] = cookie.expiresDate.description; dict[@"Value"] = cookie.value; [_connection send:@"addCookie" withParams:dict]; - i++; } + + _previousCookies = sortedCookies.copy; } @end