Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
also observe WKHTTPCookieStore
Browse files Browse the repository at this point in the history
Summary:
make the flipper plugin also observe WKHTTPCookieStore
(see https://fb.workplace.com/groups/flipper.community.support/permalink/1031948131877035/)

--

this entailed:
1. doing all on main thread(WKWebkitStorage isnt fully thread safe)
2. diffing the changes as the 2 storages refresh too often without changes (especially for ~~10secs after app start while storages are setup)

Reviewed By: lblasa

Differential Revision: D59422745

fbshipit-source-id: d4756766428588aaf16e9cab334c9e1d8912e3b5
  • Loading branch information
Daij-Djan authored and facebook-github-bot committed Jul 8, 2024
1 parent 0dc81e3 commit b6f8125
Showing 1 changed file with 83 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,124 @@

#ifdef FB_SONARKIT_ENABLED

#import "FlipperKitCookiesPlugin.h"
#import <WebKit/WebKit.h>

#import <FlipperKit/FlipperClient.h>
#import <FlipperKit/FlipperConnection.h>
#import <FlipperKit/FlipperResponder.h>
#import "FlipperKitCookiesPlugin.h"
#import "Plugins.h"

@interface FlipperKitCookiesPlugin ()<WKHTTPCookieStoreObserver>
@end

@implementation FlipperKitCookiesPlugin {
id<FlipperConnection> _connection;
NSArray<NSHTTPCookie*>* _previousCookies;
}

- (NSString*)identifier {
return @"cookies";
}

- (void)didConnect:(id<FlipperConnection>)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<FlipperConnection>)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<NSHTTPCookie*>* _Nullable NSCookies =
[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];

// get Cookies from WebKit
[[[WKWebsiteDataStore defaultDataStore] httpCookieStore]
getAllCookies:^(NSArray<NSHTTPCookie*>* WKCookies) {
// combine and sort cookies based on name - preferring WK
NSMutableDictionary<NSString*, NSHTTPCookie*>* cookies =
[NSMutableDictionary dictionary];
for (NSHTTPCookie* cookie in NSCookies) {
cookies[cookie.name] = cookie;
}
for (NSHTTPCookie* cookie in WKCookies) {
cookies[cookie.name] = cookie;
}
NSArray<NSHTTPCookie*>* 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<NSHTTPCookie*>* _Nullable cookies =
[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] copy];
for (NSHTTPCookie* cookie in cookies) {
NSInteger rowid = 1;
for (NSHTTPCookie* cookie in sortedCookies) {
NSMutableDictionary<NSString*, id>* 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
Expand Down

0 comments on commit b6f8125

Please sign in to comment.