Run CLANGFORMAT on FlipperKit folder

Summary:
This diff runs CLANGFORMAT lint on FlipperKit. I have added CLANGFORMAT as the lint engined for objc files in xplat/sonar. Right now the iOS folder is not formatted according to CLANGFORMAT.
Ran `arc lint -a --paths-cmd "find ./iOS/FlipperKit -type f" --verbose`

Reviewed By: passy

Differential Revision: D19942170

fbshipit-source-id: af677323af4edb761f61f8f7e289cab743aa31f2
This commit is contained in:
Pritesh Nandgaonkar
2020-02-17 10:46:43 -08:00
committed by Facebook Github Bot
parent 127eec5fa1
commit ca513cf370
21 changed files with 510 additions and 360 deletions

View File

@@ -14,5 +14,6 @@ that forwards messages to the underlying C++ connection. This class allows
pure Objective-C plugins to send messages to the underlying connection.
*/
@interface FlipperCppBridgingConnection : NSObject<FlipperConnection>
- (instancetype)initWithCppConnection:(std::shared_ptr<facebook::flipper::FlipperConnection>)conn;
- (instancetype)initWithCppConnection:
(std::shared_ptr<facebook::flipper::FlipperConnection>)conn;
@end

View File

@@ -14,5 +14,6 @@ that forwards messages to the underlying C++ responder. This class allows
pure Objective-C plugins to send messages to the underlying responder.
*/
@interface FlipperCppBridgingResponder : NSObject<FlipperResponder>
- (instancetype)initWithCppResponder:(std::shared_ptr<facebook::flipper::FlipperResponder>)responder;
- (instancetype)initWithCppResponder:
(std::shared_ptr<facebook::flipper::FlipperResponder>)responder;
@end

View File

@@ -13,8 +13,8 @@
std::shared_ptr<facebook::flipper::FlipperResponder> responder_;
}
- (instancetype)initWithCppResponder:(std::shared_ptr<facebook::flipper::FlipperResponder>)responder
{
- (instancetype)initWithCppResponder:
(std::shared_ptr<facebook::flipper::FlipperResponder>)responder {
if (!responder) {
return nil;
}
@@ -28,8 +28,14 @@
#pragma mark - FlipperResponder
- (void)success:(NSDictionary *)response { responder_->success(facebook::cxxutils::convertIdToFollyDynamic(response, true)); }
- (void)success:(NSDictionary*)response {
responder_->success(
facebook::cxxutils::convertIdToFollyDynamic(response, true));
}
- (void)error:(NSDictionary *)response { responder_->error(facebook::cxxutils::convertIdToFollyDynamic(response, true)); }
- (void)error:(NSDictionary*)response {
responder_->error(
facebook::cxxutils::convertIdToFollyDynamic(response, true));
}
@end

View File

@@ -28,15 +28,20 @@ public:
// Under ARC copying objCPlugin *does* increment its retain count
FlipperCppWrapperPlugin(ObjCPlugin objCPlugin) : _objCPlugin(objCPlugin) {}
std::string identifier() const override { return [[_objCPlugin identifier] UTF8String]; }
std::string identifier() const override {
return [[_objCPlugin identifier] UTF8String];
}
void didConnect(std::shared_ptr<facebook::flipper::FlipperConnection> conn) override
{
FlipperCppBridgingConnection *const bridgingConn = [[FlipperCppBridgingConnection alloc] initWithCppConnection:conn];
void didConnect(
std::shared_ptr<facebook::flipper::FlipperConnection> conn) override {
FlipperCppBridgingConnection* const bridgingConn =
[[FlipperCppBridgingConnection alloc] initWithCppConnection:conn];
[_objCPlugin didConnect:bridgingConn];
}
void didDisconnect() override { [_objCPlugin didDisconnect]; }
void didDisconnect() override {
[_objCPlugin didDisconnect];
}
bool runInBackground() override {
if ([_objCPlugin respondsToSelector:@selector(runInBackground)]) {
@@ -45,7 +50,9 @@ public:
return false;
}
ObjCPlugin getObjCPlugin() { return _objCPlugin; }
ObjCPlugin getObjCPlugin() {
return _objCPlugin;
}
private:
ObjCPlugin _objCPlugin;

View File

@@ -17,4 +17,5 @@ namespace cxxutils {
folly::dynamic convertIdToFollyDynamic(id json, bool nullifyNanAndInf = false);
id convertFollyDynamicToId(const folly::dynamic& dyn);
} }
} // namespace cxxutils
} // namespace facebook

View File

@@ -16,8 +16,7 @@ namespace cxxutils {
* The implementation is taken from RCTFollyConvert(https://fburl.com/vzw8ql2q)
*/
id convertFollyDynamicToId(const folly::dynamic &dyn)
{
id convertFollyDynamicToId(const folly::dynamic& dyn) {
// I could imagine an implementation which avoids copies by wrapping the
// dynamic in a derived class of NSDictionary. We can do that if profiling
// implies it will help.
@@ -32,9 +31,12 @@ id convertFollyDynamicToId(const folly::dynamic &dyn)
case folly::dynamic::DOUBLE:
return @(dyn.getDouble());
case folly::dynamic::STRING:
return [[NSString alloc] initWithBytes:dyn.c_str() length:dyn.size() encoding:NSUTF8StringEncoding];
return [[NSString alloc] initWithBytes:dyn.c_str()
length:dyn.size()
encoding:NSUTF8StringEncoding];
case folly::dynamic::ARRAY: {
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:dyn.size()];
NSMutableArray* array =
[[NSMutableArray alloc] initWithCapacity:dyn.size()];
for (auto& elem : dyn) {
id obj = convertFollyDynamicToId(elem);
if (obj) {
@@ -44,7 +46,8 @@ id convertFollyDynamicToId(const folly::dynamic &dyn)
return array;
}
case folly::dynamic::OBJECT: {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:dyn.size()];
NSMutableDictionary* dict =
[[NSMutableDictionary alloc] initWithCapacity:dyn.size()];
for (auto& elem : dyn.items()) {
id obj = convertFollyDynamicToId(elem.second);
if (obj) {
@@ -56,8 +59,7 @@ id convertFollyDynamicToId(const folly::dynamic &dyn)
}
}
folly::dynamic convertIdToFollyDynamic(id json, bool nullifyNanAndInf)
{
folly::dynamic convertIdToFollyDynamic(id json, bool nullifyNanAndInf) {
if (json == nil || json == (id)kCFNull) {
return nullptr;
} else if ([json isKindOfClass:[NSNumber class]]) {
@@ -120,8 +122,11 @@ folly::dynamic convertIdToFollyDynamic(id json, bool nullifyNanAndInf)
} else if ([json isKindOfClass:[NSDictionary class]]) {
__block folly::dynamic object = folly::dynamic::object();
[json enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, __unused BOOL *stop) {
object.insert(convertIdToFollyDynamic(key, nullifyNanAndInf), convertIdToFollyDynamic(value, nullifyNanAndInf));
[json enumerateKeysAndObjectsUsingBlock:^(
NSString* key, NSString* value, __unused BOOL* stop) {
object.insert(
convertIdToFollyDynamic(key, nullifyNanAndInf),
convertIdToFollyDynamic(value, nullifyNanAndInf));
}];
return object;

View File

@@ -15,8 +15,9 @@
static const NSTimeInterval ReconnectDelay = 1.0;
@interface FKPortForwardingClient () <GCDAsyncSocketDelegate, PTChannelDelegate>
{
@interface FKPortForwardingClient ()<
GCDAsyncSocketDelegate,
PTChannelDelegate> {
NSUInteger _destPort;
NSUInteger _channelPort;
NSNumber* _connectingToDeviceID;
@@ -38,30 +39,28 @@ static const NSTimeInterval ReconnectDelay = 1.0;
@synthesize connectedDeviceID = _connectedDeviceID;
- (instancetype)init
{
- (instancetype)init {
if (self = [super init]) {
_notConnectedQueue = dispatch_queue_create("FKPortForwarding.notConnectedQueue", DISPATCH_QUEUE_SERIAL);
_clientSocketsQueue = dispatch_queue_create("FKPortForwarding.clients", DISPATCH_QUEUE_SERIAL);
_notConnectedQueue = dispatch_queue_create(
"FKPortForwarding.notConnectedQueue", DISPATCH_QUEUE_SERIAL);
_clientSocketsQueue = dispatch_queue_create(
"FKPortForwarding.clients", DISPATCH_QUEUE_SERIAL);
_clientSockets = [NSMutableDictionary dictionary];
}
return self;
}
- (void)forwardConnectionsToPort:(NSUInteger)port
{
- (void)forwardConnectionsToPort:(NSUInteger)port {
_destPort = port;
}
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port
{
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port {
_channelPort = port;
[self startListeningForDevices];
[self enqueueConnectToLocalIPv4Port];
}
- (void)close
{
- (void)close {
[self.connectedChannel close];
}
@@ -94,20 +93,29 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
#pragma mark - PTChannelDelegate
- (void)ioFrameChannel:(PTChannel *)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData *)payload {
- (void)ioFrameChannel:(PTChannel*)channel
didReceiveFrameOfType:(uint32_t)type
tag:(uint32_t)tag
payload:(PTData*)payload {
// NSLog(@"received %@, %u, %u, %@", channel, type, tag, payload);
if (type == FKPortForwardingFrameTypeOpenPipe) {
GCDAsyncSocket *sock = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_clientSocketsQueue];
GCDAsyncSocket* sock =
[[GCDAsyncSocket alloc] initWithDelegate:self
delegateQueue:_clientSocketsQueue];
sock.userData = @(tag);
_clientSockets[@(tag)] = sock;
NSError* connectError;
if (![sock connectToHost:@"localhost" onPort:_destPort error:&connectError]) {
FBPFLog(@"Failed to connect to local %lu - %@", (unsigned long)_destPort, connectError);
if (![sock connectToHost:@"localhost"
onPort:_destPort
error:&connectError]) {
FBPFLog(
@"Failed to connect to local %lu - %@",
(unsigned long)_destPort,
connectError);
}
FBPFTrace(@"open socket (%d)", tag);
@@ -115,7 +123,9 @@ static const NSTimeInterval ReconnectDelay = 1.0;
if (type == FKPortForwardingFrameTypeWriteToPipe) {
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length]
withTimeout:-1
tag:0];
FBPFTrace(@"channel -> socket (%d) %zu bytes", tag, payload.length);
}
@@ -127,7 +137,8 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
- (void)ioFrameChannel:(PTChannel*)channel didEndWithError:(NSError*)error {
if (_connectedDeviceID && [_connectedDeviceID isEqualToNumber:channel.userInfo]) {
if (_connectedDeviceID &&
[_connectedDeviceID isEqualToNumber:channel.userInfo]) {
[self didDisconnectFromDevice:_connectedDeviceID];
}
@@ -137,45 +148,57 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
FBPFTrace(@"socket (%ld) connected to %@", (long)[sock.userData integerValue], host);
- (void)socket:(GCDAsyncSocket*)sock
didConnectToHost:(NSString*)host
port:(uint16_t)port {
FBPFTrace(
@"socket (%ld) connected to %@",
(long)[sock.userData integerValue],
host);
[sock readDataWithTimeout:-1 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
- (void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err {
UInt32 tag = [sock.userData unsignedIntValue];
[_clientSockets removeObjectForKey:@(tag)];
FBPFTrace(@"socket (%d) disconnected", (unsigned int)tag);
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:nil];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe
tag:tag
withPayload:nil
callback:nil];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_
{
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)_ {
UInt32 tag = [sock.userData unsignedIntValue];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) {
FBPFTrace(@"channel -> socket (%d), %lu bytes", (unsigned int)tag, (unsigned long)data.length);
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe
tag:tag
withPayload:NSDataToGCDData(data)
callback:^(NSError* error) {
FBPFTrace(
@"channel -> socket (%d), %lu bytes",
(unsigned int)tag,
(unsigned long)data.length);
[sock readDataWithTimeout:-1 tag:0];
}];
}
#pragma mark - Wired device connections
- (void)startListeningForDevices {
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
__weak typeof(self) weakSelf = self;
[nc addObserverForName:PTUSBDeviceDidAttachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
[nc addObserverForName:PTUSBDeviceDidAttachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
//NSLog(@"PTUSBDeviceDidAttachNotification: %@", note.userInfo);
// NSLog(@"PTUSBDeviceDidAttachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidAttachNotification: %@", deviceID);
typeof(self) strongSelf = weakSelf;
@@ -188,18 +211,22 @@ static const NSTimeInterval ReconnectDelay = 1.0;
});
}];
[nc addObserverForName:PTUSBDeviceDidDetachNotification object:PTUSBHub.sharedHub queue:nil usingBlock:^(NSNotification *note) {
[nc addObserverForName:PTUSBDeviceDidDetachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
//NSLog(@"PTUSBDeviceDidDetachNotification: %@", note.userInfo);
// NSLog(@"PTUSBDeviceDidDetachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidDetachNotification: %@", deviceID);
[weakSelf didDetachFromDevice:deviceID];
}];
}
- (void)didAttachToDevice:(NSNumber *)deviceID note:(NSNotification *)note
{
if (!_connectingToDeviceID || ![deviceID isEqualToNumber:_connectingToDeviceID]) {
- (void)didAttachToDevice:(NSNumber*)deviceID note:(NSNotification*)note {
if (!_connectingToDeviceID ||
![deviceID isEqualToNumber:_connectingToDeviceID]) {
[self disconnectFromCurrentChannel];
_connectingToDeviceID = deviceID;
_connectedDeviceProperties = [note.userInfo objectForKey:@"Properties"];
@@ -207,8 +234,7 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)didDetachFromDevice:(NSNumber *)deviceID
{
- (void)didDetachFromDevice:(NSNumber*)deviceID {
if ([_connectingToDeviceID isEqualToNumber:deviceID]) {
_connectedDeviceProperties = nil;
_connectingToDeviceID = nil;
@@ -218,7 +244,6 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)didDisconnectFromDevice:(NSNumber*)deviceID {
FBPFLog(@"Disconnected from device #%@", deviceID);
if ([_connectedDeviceID isEqualToNumber:deviceID]) {
@@ -228,7 +253,6 @@ static const NSTimeInterval ReconnectDelay = 1.0;
}
}
- (void)disconnectFromCurrentChannel {
if (_connectedDeviceID && _connectedChannel) {
[_connectedChannel close];
@@ -244,16 +268,23 @@ static const NSTimeInterval ReconnectDelay = 1.0;
});
}
- (void)connectToLocalIPv4Port {
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo = [NSString stringWithFormat:@"127.0.0.1:%lu", (unsigned long)_channelPort];
[channel connectToPort:_channelPort IPv4Address:INADDR_LOOPBACK callback:^(NSError *error, PTAddress *address) {
channel.userInfo =
[NSString stringWithFormat:@"127.0.0.1:%lu", (unsigned long)_channelPort];
[channel
connectToPort:_channelPort
IPv4Address:INADDR_LOOPBACK
callback:^(NSError* error, PTAddress* address) {
if (error) {
if (error.domain == NSPOSIXErrorDomain && (error.code == ECONNREFUSED || error.code == ETIMEDOUT)) {
if (error.domain == NSPOSIXErrorDomain &&
(error.code == ECONNREFUSED || error.code == ETIMEDOUT)) {
// this is an expected state
} else {
FBPFTrace(@"Failed to connect to 127.0.0.1:%lu: %@", (unsigned long)_channelPort, error);
FBPFTrace(
@"Failed to connect to 127.0.0.1:%lu: %@",
(unsigned long)_channelPort,
error);
}
} else {
[self disconnectFromCurrentChannel];
@@ -261,7 +292,9 @@ static const NSTimeInterval ReconnectDelay = 1.0;
channel.userInfo = address;
FBPFLog(@"Connected to %@", address);
}
[self performSelector:@selector(enqueueConnectToLocalIPv4Port) withObject:nil afterDelay:ReconnectDelay];
[self performSelector:@selector(enqueueConnectToLocalIPv4Port)
withObject:nil
afterDelay:ReconnectDelay];
}];
}
@@ -273,30 +306,35 @@ static const NSTimeInterval ReconnectDelay = 1.0;
});
}
- (void)connectToUSBDevice {
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo = _connectingToDeviceID;
channel.delegate = self;
[channel connectToPort:(int)_channelPort overUSBHub:PTUSBHub.sharedHub deviceID:_connectingToDeviceID callback:^(NSError *error) {
[channel connectToPort:(int)_channelPort
overUSBHub:PTUSBHub.sharedHub
deviceID:_connectingToDeviceID
callback:^(NSError* error) {
[self didConnectToChannel:channel withError:error];
}];
}
- (void)didConnectToChannel:(PTChannel *)channel withError:(NSError *)error
{
- (void)didConnectToChannel:(PTChannel*)channel withError:(NSError*)error {
if (error) {
FBPFTrace(@"Failed to connect to device #%@: %@", channel.userInfo, error);
if (channel.userInfo == _connectingToDeviceID) {
[self performSelector:@selector(enqueueConnectToUSBDevice) withObject:nil afterDelay:ReconnectDelay];
[self performSelector:@selector(enqueueConnectToUSBDevice)
withObject:nil
afterDelay:ReconnectDelay];
}
} else {
_connectedDeviceID = _connectingToDeviceID;
self.connectedChannel = channel;
FBPFLog(@"Connected to device #%@\n%@", _connectingToDeviceID, _connectedDeviceProperties);
FBPFLog(
@"Connected to device #%@\n%@",
_connectingToDeviceID,
_connectedDeviceProperties);
}
}
@end

View File

@@ -14,8 +14,9 @@
#import "FKPortForwardingCommon.h"
@interface FKPortForwardingServer () <PTChannelDelegate, GCDAsyncSocketDelegate>
{
@interface FKPortForwardingServer ()<
PTChannelDelegate,
GCDAsyncSocketDelegate> {
__weak PTChannel* _serverChannel;
__weak PTChannel* _peerChannel;
@@ -30,10 +31,10 @@
@implementation FKPortForwardingServer
- (instancetype)init
{
- (instancetype)init {
if (self = [super init]) {
_socketQueue = dispatch_queue_create("FKPortForwardingServer", DISPATCH_QUEUE_SERIAL);
_socketQueue =
dispatch_queue_create("FKPortForwardingServer", DISPATCH_QUEUE_SERIAL);
_lastClientSocketTag = 0;
_clientSockets = [NSMutableDictionary dictionary];
_protocol = [[PTProtocol alloc] initWithDispatchQueue:_socketQueue];
@@ -41,23 +42,26 @@
return self;
}
- (void)dealloc
{
- (void)dealloc {
[self close];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)forwardConnectionsFromPort:(NSUInteger)port
{
- (void)forwardConnectionsFromPort:(NSUInteger)port {
[self _forwardConnectionsFromPort:port reportError:YES];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
[[NSNotificationCenter defaultCenter]
addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:nil
usingBlock:^(NSNotification* note) {
[self _forwardConnectionsFromPort:port reportError:NO];
}];
}
- (void)_forwardConnectionsFromPort:(NSUInteger)port reportError:(BOOL)shouldReportError
{
GCDAsyncSocket *serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_socketQueue];
- (void)_forwardConnectionsFromPort:(NSUInteger)port
reportError:(BOOL)shouldReportError {
GCDAsyncSocket* serverSocket =
[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:_socketQueue];
NSError* listenError;
if ([serverSocket acceptOnPort:port error:&listenError]) {
_serverSocket = serverSocket;
@@ -68,21 +72,31 @@
}
}
- (void)listenForMultiplexingChannelOnPort:(NSUInteger)port
{
- (void)listenForMultiplexingChannelOnPort:(NSUInteger)port {
[self _listenForMultiplexingChannelOnPort:port reportError:YES];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
[[NSNotificationCenter defaultCenter]
addObserverForName:UIApplicationDidBecomeActiveNotification
object:nil
queue:nil
usingBlock:^(NSNotification* note) {
[self _listenForMultiplexingChannelOnPort:port reportError:NO];
}];
}
- (void)_listenForMultiplexingChannelOnPort:(NSUInteger)port reportError:(BOOL)shouldReportError
{
PTChannel *channel = [[PTChannel alloc] initWithProtocol:_protocol delegate:self];
[channel listenOnPort:port IPv4Address:INADDR_LOOPBACK callback:^(NSError *error) {
- (void)_listenForMultiplexingChannelOnPort:(NSUInteger)port
reportError:(BOOL)shouldReportError {
PTChannel* channel = [[PTChannel alloc] initWithProtocol:_protocol
delegate:self];
[channel
listenOnPort:port
IPv4Address:INADDR_LOOPBACK
callback:^(NSError* error) {
if (error) {
if (shouldReportError) {
FBPFLog(@"Failed to listen on 127.0.0.1:%lu: %@", (unsigned long)port, error);
FBPFLog(
@"Failed to listen on 127.0.0.1:%lu: %@",
(unsigned long)port,
error);
}
} else {
FBPFTrace(@"Listening on 127.0.0.1:%lu", (unsigned long)port);
@@ -91,8 +105,7 @@
}];
}
- (void)close
{
- (void)close {
if (_serverChannel) {
[_serverChannel close];
_serverChannel = nil;
@@ -102,7 +115,9 @@
#pragma mark - PTChannelDelegate
- (void)ioFrameChannel:(PTChannel *)channel didAcceptConnection:(PTChannel *)otherChannel fromAddress:(PTAddress *)address {
- (void)ioFrameChannel:(PTChannel*)channel
didAcceptConnection:(PTChannel*)otherChannel
fromAddress:(PTAddress*)address {
// Cancel any other connection. We are FIFO, so the last connection
// established will cancel any previous connection and "take its place".
if (_peerChannel) {
@@ -116,11 +131,16 @@
FBPFTrace(@"Connected to %@", address);
}
- (void)ioFrameChannel:(PTChannel *)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData *)payload {
- (void)ioFrameChannel:(PTChannel*)channel
didReceiveFrameOfType:(uint32_t)type
tag:(uint32_t)tag
payload:(PTData*)payload {
// NSLog(@"didReceiveFrameOfType: %u, %u, %@", type, tag, payload);
if (type == FKPortForwardingFrameTypeWriteToPipe) {
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length] withTimeout:-1 tag:0];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length]
withTimeout:-1
tag:0];
FBPFTrace(@"channel -> socket (%d), %zu bytes", tag, payload.length);
}
@@ -139,11 +159,10 @@
FBPFTrace(@"Disconnected from %@, error = %@", channel.userInfo, error);
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
- (void)socket:(GCDAsyncSocket*)sock
didAcceptNewSocket:(GCDAsyncSocket*)newSocket {
dispatch_block_t block = ^() {
if (!self->_peerChannel) {
[newSocket setDelegate:nil];
@@ -154,8 +173,13 @@
newSocket.userData = @(tag);
newSocket.delegate = self;
self->_clientSockets[@(tag)] = newSocket;
[self->_peerChannel sendFrameOfType:FKPortForwardingFrameTypeOpenPipe tag:self->_lastClientSocketTag withPayload:nil callback:^(NSError *error) {
FBPFTrace(@"open socket (%d), error = %@", (unsigned int)tag, error);
[self->_peerChannel
sendFrameOfType:FKPortForwardingFrameTypeOpenPipe
tag:self->_lastClientSocketTag
withPayload:nil
callback:^(NSError* error) {
FBPFTrace(
@"open socket (%d), error = %@", (unsigned int)tag, error);
[newSocket readDataWithTimeout:-1 tag:0];
}];
};
@@ -163,28 +187,46 @@
if (_peerChannel) {
block();
} else {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), _socketQueue, block);
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
_socketQueue,
block);
}
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)_
{
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)_ {
UInt32 tag = [[sock userData] unsignedIntValue];
FBPFTrace(@"Incoming data on socket (%d) - %lu bytes", (unsigned int)tag, (unsigned long)data.length);
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe tag:tag withPayload:NSDataToGCDData(data) callback:^(NSError *error) {
FBPFTrace(@"socket (%d) -> channel %lu bytes, error = %@", (unsigned int)tag, (unsigned long)data.length, error);
FBPFTrace(
@"Incoming data on socket (%d) - %lu bytes",
(unsigned int)tag,
(unsigned long)data.length);
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe
tag:tag
withPayload:NSDataToGCDData(data)
callback:^(NSError* error) {
FBPFTrace(
@"socket (%d) -> channel %lu bytes, error = %@",
(unsigned int)tag,
(unsigned long)data.length,
error);
[sock readDataWithTimeout:-1 tag:_];
}];
}
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
- (void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err {
UInt32 tag = [sock.userData unsignedIntValue];
[_clientSockets removeObjectForKey:@(tag)];
[_peerChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe tag:tag withPayload:nil callback:^(NSError *error) {
FBPFTrace(@"socket (%d) disconnected, err = %@, peer error = %@", (unsigned int)tag, err, error);
[_peerChannel
sendFrameOfType:FKPortForwardingFrameTypeClosePipe
tag:tag
withPayload:nil
callback:^(NSError* error) {
FBPFTrace(
@"socket (%d) disconnected, err = %@, peer error = %@",
(unsigned int)tag,
err,
error);
}];
}
@end

View File

@@ -12,13 +12,14 @@
#import "FlipperStateUpdateListener.h"
/**
Represents a connection between the Sonar desktop och client side. Manages the lifecycle of attached
plugin instances.
Represents a connection between the Sonar desktop och client side. Manages the
lifecycle of attached plugin instances.
*/
@interface FlipperClient : NSObject
/**
The shared singleton FlipperClient instance. It is an error to call this on non-debug builds to avoid leaking data.
The shared singleton FlipperClient instance. It is an error to call this on
non-debug builds to avoid leaking data.
*/
+ (instancetype)sharedClient;
@@ -33,7 +34,8 @@ Unregister a plugin with the client.
- (void)removePlugin:(NSObject<FlipperPlugin>*)plugin;
/**
Retrieve the plugin with a given identifier which was previously registered with this client.
Retrieve the plugin with a given identifier which was previously registered with
this client.
*/
- (NSObject<FlipperPlugin>*)pluginWithIdentifier:(NSString*)identifier;
@@ -62,7 +64,8 @@ Subscribe a ViewController to state update change notifications
*/
- (void)subscribeForUpdates:(id<FlipperStateUpdateListener>)controller;
// initializers are disabled. You must use `+[FlipperClient sharedClient]` instance.
// initializers are disabled. You must use `+[FlipperClient sharedClient]`
// instance.
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

View File

@@ -8,14 +8,14 @@
#if FB_SONARKIT_ENABLED
#import "FlipperClient.h"
#import "FlipperCppWrapperPlugin.h"
#import <Flipper/FlipperClient.h>
#import <UIKit/UIKit.h>
#include <folly/io/async/EventBase.h>
#include <folly/io/async/ScopedEventBaseThread.h>
#import <UIKit/UIKit.h>
#include "SKStateUpdateCPPWrapper.h"
#import "FlipperClient+Testing.h"
#import "FlipperCppWrapperPlugin.h"
#import "SKEnvironmentVariables.h"
#include "SKStateUpdateCPPWrapper.h"
#if !TARGET_OS_SIMULATOR
#import <FKPortForwarding/FKPortForwardingServer.h>
@@ -33,8 +33,7 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
#endif
}
+ (instancetype)sharedClient
{
+ (instancetype)sharedClient {
static FlipperClient* sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@@ -48,31 +47,37 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
return sharedClient;
}
- (instancetype)init
{
- (instancetype)init {
if (self = [super init]) {
UIDevice* device = [UIDevice currentDevice];
NSString* deviceName = [device name];
NSBundle* bundle = [NSBundle mainBundle];
NSString *appName = [bundle objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
NSString* appName =
[bundle objectForInfoDictionaryKey:(NSString*)kCFBundleNameKey];
NSString* appId = [bundle bundleIdentifier];
NSString *privateAppDirectory = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];
NSString* privateAppDirectory = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES)[0];
NSFileManager* manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:privateAppDirectory isDirectory:NULL] == NO &&
![manager createDirectoryAtPath:privateAppDirectory withIntermediateDirectories:YES attributes:nil error:nil]) {
![manager createDirectoryAtPath:privateAppDirectory
withIntermediateDirectories:YES
attributes:nil
error:nil]) {
return nil;
}
#if TARGET_OS_SIMULATOR
deviceName = [NSString stringWithFormat:@"%@ %@", [[UIDevice currentDevice] model], @"Simulator"];
deviceName = [NSString stringWithFormat:@"%@ %@",
[[UIDevice currentDevice] model],
@"Simulator"];
#endif
static const std::string UNKNOWN = std::string("unknown");
try {
facebook::flipper::FlipperClient::init({
{
facebook::flipper::FlipperClient::init(
{{
"localhost",
"iOS",
[deviceName UTF8String],
@@ -84,8 +89,7 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
sonarThread.getEventBase(),
connectionThread.getEventBase(),
[SKEnvironmentVariables getInsecurePort],
[SKEnvironmentVariables getSecurePort]
});
[SKEnvironmentVariables getSecurePort]});
_cppClient = facebook::flipper::FlipperClient::instance();
} catch (const std::system_error& e) {
// Probably ran out of disk space.
@@ -95,23 +99,19 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
return self;
}
- (void)refreshPlugins
{
- (void)refreshPlugins {
_cppClient->refreshPlugins();
}
- (void)addPlugin:(NSObject<FlipperPlugin> *)plugin
{
- (void)addPlugin:(NSObject<FlipperPlugin>*)plugin {
_cppClient->addPlugin(std::make_shared<WrapperPlugin>(plugin));
}
- (void)removePlugin:(NSObject<FlipperPlugin> *)plugin
{
- (void)removePlugin:(NSObject<FlipperPlugin>*)plugin {
_cppClient->removePlugin(std::make_shared<WrapperPlugin>(plugin));
}
- (NSObject<FlipperPlugin> *)pluginWithIdentifier:(NSString *)identifier
{
- (NSObject<FlipperPlugin>*)pluginWithIdentifier:(NSString*)identifier {
auto cppPlugin = _cppClient->getPlugin([identifier UTF8String]);
if (auto wrapper = dynamic_cast<WrapperPlugin*>(cppPlugin.get())) {
return wrapper->getObjCPlugin();
@@ -119,8 +119,7 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
return nil;
}
- (void)start
{
- (void)start {
#if !TARGET_OS_SIMULATOR
_secureServer = [FKPortForwardingServer new];
[_secureServer forwardConnectionsFromPort:8088];
@@ -132,9 +131,7 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
_cppClient->start();
}
- (void)stop
{
- (void)stop {
_cppClient->stop();
#if !TARGET_OS_SIMULATOR
[_secureServer close];
@@ -149,9 +146,11 @@ using WrapperPlugin = facebook::flipper::FlipperCppWrapperPlugin;
}
- (NSArray*)getStateElements {
NSMutableArray<NSDictionary<NSString *, NSString *>*> *const array = [NSMutableArray array];
NSMutableArray<NSDictionary<NSString*, NSString*>*>* const array =
[NSMutableArray array];
for (facebook::flipper::StateElement element: _cppClient->getStateElements()) {
for (facebook::flipper::StateElement element :
_cppClient->getStateElements()) {
facebook::flipper::State state = element.state_;
NSString* stateString;
switch (state) {

View File

@@ -7,14 +7,15 @@
#ifdef FB_SONARKIT_ENABLED
#include "FlipperStateUpdateListener.h"
#import <UIKit/UIKit.h>
#include "FlipperStateUpdateListener.h"
@interface StateTableDataSource : NSObject<UITableViewDataSource>
@property(strong, nonatomic) NSArray<NSDictionary*>* elements;
@end
@interface FlipperDiagnosticsViewController : UIViewController<FlipperStateUpdateListener>
@interface FlipperDiagnosticsViewController
: UIViewController<FlipperStateUpdateListener>
@property(strong, nonatomic) StateTableDataSource* tableDataSource;
@property(strong, nonatomic) UILabel* stateLabel;
@property(strong, nonatomic) UITableView* stateTable;

View File

@@ -12,7 +12,8 @@
#define STATE_VIEW_HEIGHT 300
static NSString *const kSKCellIdentifier = @"FlipperDiagnosticStateTableStableCellIdentifier";
static NSString* const kSKCellIdentifier =
@"FlipperDiagnosticStateTableStableCellIdentifier";
@implementation StateTableDataSource
- (instancetype)initWithElements:(NSArray<NSDictionary*>*)elements {
@@ -23,16 +24,21 @@ static NSString *const kSKCellIdentifier = @"FlipperDiagnosticStateTableStableCe
return self;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
- (nonnull UITableViewCell*)tableView:(nonnull UITableView*)tableView
cellForRowAtIndexPath:(nonnull NSIndexPath*)indexPath {
NSInteger row = indexPath.row;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSKCellIdentifier forIndexPath:indexPath];
UITableViewCell* cell =
[tableView dequeueReusableCellWithIdentifier:kSKCellIdentifier
forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:@"Arial" size:10];
cell.textLabel.text = [self.elements[row][@"state"] stringByAppendingString:self.elements[row][@"name"]];
cell.textLabel.text = [self.elements[row][@"state"]
stringByAppendingString:self.elements[row][@"name"]];
return cell;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- (NSInteger)tableView:(nonnull UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.elements count];
}
@@ -43,16 +49,30 @@ static NSString *const kSKCellIdentifier = @"FlipperDiagnosticStateTableStableCe
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, STATE_VIEW_HEIGHT, self.view.frame.size.width, self.view.frame.size.height - 100 - STATE_VIEW_HEIGHT)];
self.logLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.scrollView.frame.size.height)];
self.scrollView = [[UIScrollView alloc]
initWithFrame:CGRectMake(
0,
STATE_VIEW_HEIGHT,
self.view.frame.size.width,
self.view.frame.size.height - 100 - STATE_VIEW_HEIGHT)];
self.logLabel =
[[UILabel alloc] initWithFrame:CGRectMake(
0,
0,
self.view.frame.size.width,
self.scrollView.frame.size.height)];
self.logLabel.numberOfLines = 0;
self.logLabel.font = [UIFont fontWithName:@"Arial" size:10];
[self.scrollView addSubview:self.logLabel];
self.stateTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, STATE_VIEW_HEIGHT)];
[self.stateTable registerClass:[UITableViewCell class] forCellReuseIdentifier:kSKCellIdentifier];
self.stateTable = [[UITableView alloc]
initWithFrame:CGRectMake(
0, 0, self.view.bounds.size.width, STATE_VIEW_HEIGHT)];
[self.stateTable registerClass:[UITableViewCell class]
forCellReuseIdentifier:kSKCellIdentifier];
self.stateTable.rowHeight = 14;
self.tableDataSource = [[StateTableDataSource alloc] initWithElements:[[FlipperClient sharedClient] getStateElements]];
self.tableDataSource = [[StateTableDataSource alloc]
initWithElements:[[FlipperClient sharedClient] getStateElements]];
self.stateTable.dataSource = self.tableDataSource;
[self updateLogView];
@@ -71,7 +91,8 @@ static NSString *const kSKCellIdentifier = @"FlipperDiagnosticStateTableStableCe
}
- (void)updateStateTable {
self.tableDataSource.elements = [[FlipperClient sharedClient] getStateElements];
self.tableDataSource.elements =
[[FlipperClient sharedClient] getStateElements];
[self.stateTable reloadData];
}
@@ -82,7 +103,9 @@ static NSString *const kSKCellIdentifier = @"FlipperDiagnosticStateTableStableCe
self.scrollView.contentSize = self.logLabel.frame.size;
// Scroll to bottom
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
CGPoint bottomOffset = CGPointMake(
0,
self.scrollView.contentSize.height - self.scrollView.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:YES];
}

View File

@@ -6,12 +6,13 @@
*/
#import <Foundation/Foundation.h>
#import "SKMacros.h"
#import "FlipperResponder.h"
#import "SKMacros.h"
SK_EXTERN_C_BEGIN
void FlipperPerformBlockOnMainThread(void(^block)(), id<FlipperResponder> responder);
void FlipperPerformBlockOnMainThread(
void (^block)(),
id<FlipperResponder> responder);
SK_EXTERN_C_END
@protocol FlipperConnection;
@@ -19,26 +20,28 @@ SK_EXTERN_C_END
@protocol FlipperPlugin
/**
The plugin's identifier. This should map to a javascript plugin with the same identifier to ensure
messages are sent correctly.
The plugin's identifier. This should map to a javascript plugin with the same
identifier to ensure messages are sent correctly.
*/
- (NSString*)identifier;
/**
Called when a connection has been established between this plugin and the corresponding plugin on
the Sonar desktop app. The provided connection can be used to register method receivers as well
as send messages back to the desktop app.
Called when a connection has been established between this plugin and the
corresponding plugin on the Sonar desktop app. The provided connection can be
used to register method receivers as well as send messages back to the desktop
app.
*/
- (void)didConnect:(id<FlipperConnection>)connection;
/**
Called when a plugin has been disconnected and the SonarConnection provided in didConnect is no
longer valid to use.
Called when a plugin has been disconnected and the SonarConnection provided in
didConnect is no longer valid to use.
*/
- (void)didDisconnect;
/**
Returns true if the plugin is meant to be run in background too, otherwise it returns false.
Returns true if the plugin is meant to be run in background too, otherwise it
returns false.
*/
@optional
- (BOOL)runInBackground;

View File

@@ -8,7 +8,8 @@
#import <Foundation/Foundation.h>
/**
Acts as a hook for providing return values to remote called from Sonar desktop plugins.
Acts as a hook for providing return values to remote called from Sonar desktop
plugins.
*/
@protocol FlipperResponder

View File

@@ -9,15 +9,20 @@
#import "FlipperPlugin.h"
#import "FlipperResponder.h"
void FlipperPerformBlockOnMainThread(void(^block)(), id<FlipperResponder> responder)
{
void FlipperPerformBlockOnMainThread(
void (^block)(),
id<FlipperResponder> responder) {
if ([NSThread isMainThread]) {
@try {
block();
} @catch (NSException* e) {
[responder error:@{@"name" : e.name, @"message" : e.reason}];
} @catch (...) {
[responder error:@{@"name": @"Unknown", @"message": @"Unknown error caught when processing operation on main thread"}];
[responder error:@{
@"name" : @"Unknown",
@"message" :
@"Unknown error caught when processing operation on main thread"
}];
}
} else {
dispatch_async(dispatch_get_main_queue(), ^{
@@ -26,7 +31,11 @@ void FlipperPerformBlockOnMainThread(void(^block)(), id<FlipperResponder> respon
} @catch (NSException* e) {
[responder error:@{@"name" : e.name, @"message" : e.reason}];
} @catch (...) {
[responder error:@{@"name": @"Unknown", @"message": @"Unknown error caught when processing operation on main thread"}];
[responder error:@{
@"name" : @"Unknown",
@"message" :
@"Unknown error caught when processing operation on main thread"
}];
}
});
}

View File

@@ -8,7 +8,8 @@
#ifdef FB_SONARKIT_ENABLED
/*
* This class exists to retreive configuration values stored in environment variables.
* This class exists to retreive configuration values stored in environment
* variables.
*/
@interface SKEnvironmentVariables : NSObject
+ (int)getInsecurePort;

View File

@@ -16,13 +16,19 @@ static int const DEFAULT_SECURE_PORT = 8088;
+ (int)getInsecurePort {
NSString* envVar = [self getFlipperPortsVariable];
return [self extractIntFromPropValue:envVar atIndex:0 withDefault:DEFAULT_INSECURE_PORT];
return [self extractIntFromPropValue:envVar
atIndex:0
withDefault:DEFAULT_INSECURE_PORT];
}
+ (int)getSecurePort {
NSString* envVar = [self getFlipperPortsVariable];
return [self extractIntFromPropValue:envVar atIndex:1 withDefault:DEFAULT_SECURE_PORT];
return [self extractIntFromPropValue:envVar
atIndex:1
withDefault:DEFAULT_SECURE_PORT];
}
+ (int)extractIntFromPropValue:(NSString *)propValue atIndex:(int)index withDefault:(int)fallback {
+ (int)extractIntFromPropValue:(NSString*)propValue
atIndex:(int)index
withDefault:(int)fallback {
NSArray<NSString*>* components = [propValue componentsSeparatedByString:@","];
NSString* component = [components objectAtIndex:index];
int envInt = [component intValue];

View File

@@ -13,12 +13,14 @@
/*
* This class exists to bridge the gap between Objective C and C++.
* A SKStateUpdateCPPWrapper instance allows for wrapping an Objective-C object
* and passing it to the pure C++ SonarClient, so it can be triggered when updates occur.
* and passing it to the pure C++ SonarClient, so it can be triggered when
* updates occur.
*/
class SKStateUpdateCPPWrapper : public FlipperStateUpdateListener {
public:
SKStateUpdateCPPWrapper(id<FlipperStateUpdateListener> delegate_);
void onUpdate();
private:
__weak id<FlipperStateUpdateListener> delegate_;
};

View File

@@ -9,7 +9,8 @@
#include "SKStateUpdateCPPWrapper.h"
SKStateUpdateCPPWrapper::SKStateUpdateCPPWrapper(id<FlipperStateUpdateListener> controller) {
SKStateUpdateCPPWrapper::SKStateUpdateCPPWrapper(
id<FlipperStateUpdateListener> controller) {
delegate_ = controller;
}