Skip to content

Commit

Permalink
open source the cookie monitoring plugin
Browse files Browse the repository at this point in the history
Summary: This diffs adds an ios plugin to monitor the app's NSCookie storage

Reviewed By: lblasa

Differential Revision: D56712794

fbshipit-source-id: b19ec3abfc1c86e53f46e523633518825cea5f99
  • Loading branch information
Daij-Djan authored and facebook-github-bot committed May 3, 2024
1 parent cfa7206 commit 1c7cd07
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
38 changes: 38 additions & 0 deletions desktop/plugins/public/cookies/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import {DataTableColumn, createTablePlugin} from 'flipper-plugin';

type Row = {
id: number;
Name: string;
Expires: string;
Value: string;
};

const columns: DataTableColumn<Row>[] = [
{
key: 'Name',
width: 250,
},
{
key: 'Expires',
width: 250,
},
{
key: 'Value',
},
];

module.exports = createTablePlugin<Row>({
columns,
key: 'id',
method: 'addCookie',
resetMethod: 'resetCookies',
});
28 changes: 28 additions & 0 deletions desktop/plugins/public/cookies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://fbflipper.com/schemas/plugin-package/v2.json",
"name": "flipper-plugin-cookies",
"id": "cookies",
"pluginType": "client",
"version": "0.0.0",
"flipperBundlerEntry": "index.tsx",
"main": "dist/bundle.js",
"license": "MIT",
"title": "Cookies",
"icon": "apps",
"keywords": [
"flipper-plugin",
"cookies"
],
"description": "Flipper plugin to monitor NSHTTPCookieStorage",
"peerDependencies": {
"flipper-plugin": "*",
"antd": "*",
"react": "*",
"react-dom": "*",
"@emotion/styled": "*",
"@ant-design/icons": "*",
"@types/react": "*",
"@types/react-dom": "*",
"@types/node": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#ifdef FB_SONARKIT_ENABLED

#import <Foundation/Foundation.h>

#import <FlipperKit/FlipperPlugin.h>

@interface FlipperKitCookiesPlugin : NSObject<FlipperPlugin>

@end

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#ifdef FB_SONARKIT_ENABLED

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

@implementation FlipperKitCookiesPlugin {
id<FlipperConnection> _connection;
}

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

- (void)didConnect:(id<FlipperConnection>)connection {
_connection = connection;
[self _sendCookies];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onCookieStorageChange:)
name:NSHTTPCookieManagerCookiesChangedNotification
object:nil];
}

- (void)didDisconnect {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_connection = nil;
}

#pragma mark - cookie storage observer

- (void)onCookieStorageChange:(NSNotification*)notification {
[self _sendCookies];
}

#pragma mark - helper

- (void)_sendCookies {
[_connection send:@"resetCookies" withParams:@{}];

NSInteger i = 1;
NSArray<NSHTTPCookie*>* _Nullable cookies =
[[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] copy];
for (NSHTTPCookie* cookie in cookies) {
NSMutableDictionary<NSString*, id>* dict = [NSMutableDictionary dictionary];
dict[@"id"] = @(i);
dict[@"Name"] = cookie.name;
dict[@"Expires"] = cookie.expiresDate.description;
dict[@"Value"] = cookie.value;
[_connection send:@"addCookie" withParams:dict];
i++;
}
}

@end

void FlipperKitCookiesPluginInit(FlipperClient* client) {
[client addPlugin:[FlipperKitCookiesPlugin new]];
}

#endif

0 comments on commit 1c7cd07

Please sign in to comment.