Rename SonarKitNetworkPlugin
Summary: Renames SonarKitNetworkPlugin Reviewed By: passy Differential Revision: D9968092 fbshipit-source-id: 6d5b7761405be9b47e679510ce6fc7f2a84c308b
This commit is contained in:
committed by
Facebook Github Bot
parent
5d9e9aa402
commit
02ac3ff9cd
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
#pragma once
|
||||
|
||||
#import "SKBufferingPlugin.h"
|
||||
#import "SKDispatchQueue.h"
|
||||
#import <iostream>
|
||||
#import <memory>
|
||||
|
||||
struct CachedEvent {
|
||||
NSString *method;
|
||||
NSDictionary<NSString *, id> *sonarObject;
|
||||
};
|
||||
|
||||
|
||||
@interface SKBufferingPlugin(CPPInitialization)
|
||||
|
||||
- (instancetype)initWithVectorEventSize:(NSUInteger)size connectionAccessQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)connectionAccessQueue;
|
||||
- (instancetype)initWithDispatchQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)queue;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import <FlipperKit/FlipperPlugin.h>
|
||||
|
||||
@interface SKBufferingPlugin : NSObject<FlipperPlugin>
|
||||
|
||||
- (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
- (void)send:(NSString *)method sonarObject:(NSDictionary<NSString *, id> *)sonarObject;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
|
||||
#import <vector>
|
||||
|
||||
#import "SKBufferingPlugin.h"
|
||||
#import <FlipperKit/FlipperConnection.h>
|
||||
#import "SKDispatchQueue.h"
|
||||
#import "SKBufferingPlugin+CPPInitialization.h"
|
||||
|
||||
static const NSUInteger bufferSize = 500;
|
||||
|
||||
@interface SKBufferingPlugin()
|
||||
|
||||
@property(assign, nonatomic) std::vector<CachedEvent> ringBuffer;
|
||||
@property(assign, nonatomic) std::shared_ptr<facebook::flipper::DispatchQueue> connectionAccessQueue;
|
||||
@property(strong, nonatomic) id<FlipperConnection> connection;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SKBufferingPlugin
|
||||
// {
|
||||
// std::vector<CachedEvent> _ringBuffer;
|
||||
// std::shared_ptr<facebook::flipper::DispatchQueue> _connectionAccessQueue;
|
||||
//
|
||||
// id<FlipperConnection> _connection;
|
||||
// }
|
||||
|
||||
- (instancetype)initWithQueue:(dispatch_queue_t)queue {
|
||||
if (self = [super init]) {
|
||||
_ringBuffer.reserve(bufferSize);
|
||||
_connectionAccessQueue = std::make_shared<facebook::flipper::GCDQueue>(queue);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)identifier {
|
||||
// Note: This must match with the javascript pulgin identifier!!
|
||||
return @"Network";
|
||||
}
|
||||
|
||||
- (void)didConnect:(id<FlipperConnection>)connection {
|
||||
_connectionAccessQueue->async(^{
|
||||
self->_connection = connection;
|
||||
[self sendBufferedEvents];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)didDisconnect {
|
||||
_connectionAccessQueue->async(^{
|
||||
self->_connection = nil;
|
||||
});
|
||||
}
|
||||
|
||||
- (void)send:(NSString *)method
|
||||
sonarObject:(NSDictionary<NSString *, id> *)sonarObject {
|
||||
_connectionAccessQueue->async(^{
|
||||
if (self->_connection) {
|
||||
[self->_connection send:method withParams:sonarObject];
|
||||
} else {
|
||||
if (self->_ringBuffer.size() == bufferSize) {
|
||||
return;
|
||||
}
|
||||
self->_ringBuffer.push_back({
|
||||
.method = method,
|
||||
.sonarObject = sonarObject
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)sendBufferedEvents {
|
||||
NSAssert(_connection, @"connection object cannot be nil");
|
||||
for (const auto &event : _ringBuffer) {
|
||||
[_connection send:event.method withParams:event.sonarObject];
|
||||
}
|
||||
_ringBuffer.clear();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SKBufferingPlugin(CPPInitialization)
|
||||
|
||||
- (instancetype)initWithVectorEventSize:(NSUInteger)size connectionAccessQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)connectionAccessQueue {
|
||||
if (self = [super init]) {
|
||||
_ringBuffer.reserve(size);
|
||||
_connectionAccessQueue = connectionAccessQueue;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (instancetype)initWithDispatchQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)queue {
|
||||
return [self initWithVectorEventSize:bufferSize
|
||||
connectionAccessQueue:queue];
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
|
||||
#pragma once
|
||||
|
||||
#import <dispatch/dispatch.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace flipper {
|
||||
class DispatchQueue
|
||||
{
|
||||
public:
|
||||
virtual void async(dispatch_block_t block) = 0;
|
||||
};
|
||||
|
||||
class GCDQueue: public DispatchQueue
|
||||
{
|
||||
public:
|
||||
GCDQueue(dispatch_queue_t underlyingQueue)
|
||||
:_underlyingQueue(underlyingQueue) { }
|
||||
|
||||
void async(dispatch_block_t block) override
|
||||
{
|
||||
dispatch_async(_underlyingQueue, block);
|
||||
}
|
||||
|
||||
virtual ~GCDQueue() { }
|
||||
|
||||
private:
|
||||
dispatch_queue_t _underlyingQueue;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SKRequestInfo.h"
|
||||
#import "SKResponseInfo.h"
|
||||
|
||||
@protocol SKNetworkReporterDelegate
|
||||
|
||||
- (void)didObserveRequest:(SKRequestInfo *)request;
|
||||
- (void)didObserveResponse:(SKResponseInfo *)response;
|
||||
|
||||
@end
|
||||
|
||||
@protocol SKNetworkAdapterDelegate
|
||||
|
||||
@property (weak, nonatomic) id<SKNetworkReporterDelegate> delegate;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface SKRequestInfo: NSObject
|
||||
@property(assign, readwrite) int64_t identifier;
|
||||
@property(assign, readwrite) uint64_t timestamp;
|
||||
@property(strong, nonatomic) NSURLRequest* request;
|
||||
@property(strong, nonatomic) NSString* body;
|
||||
|
||||
- (instancetype)initWithIdentifier:(int64_t)identifier timestamp:(uint64_t)timestamp request:(NSURLRequest*)request data:(NSData *)data;
|
||||
- (void)setBodyFromData:(NSData * _Nullable)data;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#import "SKRequestInfo.h"
|
||||
|
||||
@implementation SKRequestInfo
|
||||
@synthesize identifier = _identifier;
|
||||
@synthesize timestamp = _timestamp;
|
||||
@synthesize request = _request;
|
||||
@synthesize body = _body;
|
||||
|
||||
- (instancetype)initWithIdentifier:(int64_t)identifier timestamp:(uint64_t)timestamp request:(NSURLRequest *)request data:(NSData *)data{
|
||||
|
||||
if (self = [super init]){
|
||||
_identifier = identifier;
|
||||
_timestamp = timestamp;
|
||||
_request = request;
|
||||
_body = data ? [data base64EncodedStringWithOptions: 0]
|
||||
: [request.HTTPBody base64EncodedStringWithOptions: 0];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setBodyFromData:(NSData * _Nullable)data {
|
||||
self.body = data ? [data base64EncodedStringWithOptions: 0]
|
||||
: [self.request.HTTPBody base64EncodedStringWithOptions: 0];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface SKResponseInfo : NSObject
|
||||
|
||||
@property(assign, readwrite) int64_t identifier;
|
||||
@property(assign, readwrite) uint64_t timestamp;
|
||||
@property(strong, nonatomic) NSURLResponse* response;
|
||||
@property(strong, nonatomic) NSString* body;
|
||||
|
||||
- (instancetype)initWithIndentifier:(int64_t)identifier timestamp:(uint64_t)timestamp response:(NSURLResponse *)response data:(NSData *)data;
|
||||
- (void)setBodyFromData:(NSData * _Nullable)data;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
|
||||
#import "SKResponseInfo.h"
|
||||
|
||||
@implementation SKResponseInfo
|
||||
@synthesize identifier = _identifier;
|
||||
@synthesize timestamp = _timestamp;
|
||||
@synthesize response = _response;
|
||||
@synthesize body = _body;
|
||||
|
||||
- (instancetype)initWithIndentifier:(int64_t)identifier timestamp:(uint64_t)timestamp response:(NSURLResponse *)response data:(NSData *)data {
|
||||
if(self = [super init]) {
|
||||
_identifier = identifier;
|
||||
_timestamp = timestamp;
|
||||
_response = response;
|
||||
_body = [SKResponseInfo shouldStripReponseBodyWithResponse:response] ? nil : [data base64EncodedStringWithOptions: 0];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
+ (BOOL) shouldStripReponseBodyWithResponse:(NSURLResponse *)response {
|
||||
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
|
||||
NSString *contentType = httpResponse.allHeaderFields[@"content-type"];
|
||||
if (!contentType) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [contentType containsString:@"image/"] ||
|
||||
[contentType containsString:@"video/"] ||
|
||||
[contentType containsString:@"application/zip"];
|
||||
}
|
||||
|
||||
- (void)setBodyFromData:(NSData *_Nullable)data {
|
||||
self.body = [SKResponseInfo shouldStripReponseBodyWithResponse:self.response] ? nil : [data base64EncodedStringWithOptions: 0];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
|
||||
#pragma once
|
||||
#import "SonarKitNetworkPlugin.h"
|
||||
#import "SKDispatchQueue.h"
|
||||
#import <memory>
|
||||
|
||||
@interface SonarKitNetworkPlugin(CPPInitialization)
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter dispatchQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)queue;
|
||||
@end
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
#import <FlipperKit/FlipperPlugin.h>
|
||||
|
||||
#import "SKBufferingPlugin.h"
|
||||
#import "SKNetworkReporter.h"
|
||||
|
||||
@interface SonarKitNetworkPlugin : SKBufferingPlugin <SKNetworkReporterDelegate>
|
||||
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter NS_DESIGNATED_INITIALIZER;
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter queue:(dispatch_queue_t)queue; //For test purposes
|
||||
|
||||
@property (strong, nonatomic) id<SKNetworkAdapterDelegate> adapter;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
#if FB_SONARKIT_ENABLED
|
||||
#import <vector>
|
||||
#import <iostream>
|
||||
#import <memory>
|
||||
#import "SonarKitNetworkPlugin.h"
|
||||
#import "SKNetworkReporter.h"
|
||||
#import "SonarKitNetworkPlugin+CPPInitialization.h"
|
||||
#import "SKBufferingPlugin+CPPInitialization.h"
|
||||
#import "SKDispatchQueue.h"
|
||||
|
||||
@interface SonarKitNetworkPlugin ()
|
||||
|
||||
@end
|
||||
|
||||
@implementation SonarKitNetworkPlugin
|
||||
|
||||
- (void)setAdapter:(id<SKNetworkAdapterDelegate>)adapter {
|
||||
_adapter = adapter;
|
||||
_adapter.delegate = self;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super initWithQueue:dispatch_queue_create("com.sonarkit.network.buffer", DISPATCH_QUEUE_SERIAL)]) {
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter {
|
||||
if (self = [super initWithQueue:dispatch_queue_create("com.sonarkit.network.buffer", DISPATCH_QUEUE_SERIAL)]) {
|
||||
adapter.delegate = self;
|
||||
_adapter = adapter;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter queue:(dispatch_queue_t)queue; {
|
||||
if (self = [super initWithQueue:queue]) {
|
||||
adapter.delegate = self;
|
||||
_adapter = adapter;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - SKNetworkReporterDelegate
|
||||
|
||||
|
||||
- (void)didObserveRequest:(SKRequestInfo *)request;
|
||||
{
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *headers = [NSMutableArray new];
|
||||
for (NSString *key in [request.request.allHTTPHeaderFields allKeys]) {
|
||||
NSDictionary<NSString *, id> *header = @{
|
||||
@"key": key,
|
||||
@"value": request.request.allHTTPHeaderFields[key]
|
||||
};
|
||||
[headers addObject: header];
|
||||
}
|
||||
|
||||
NSString *body = request.body;
|
||||
|
||||
[self send:@"newRequest"
|
||||
sonarObject:@{
|
||||
@"id": @(request.identifier),
|
||||
@"timestamp": @(request.timestamp),
|
||||
@"method": request.request.HTTPMethod ?: [NSNull null],
|
||||
@"url": [request.request.URL absoluteString] ?: [NSNull null],
|
||||
@"headers": headers,
|
||||
@"data": body ? body : [NSNull null]
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)didObserveResponse:(SKResponseInfo *)response
|
||||
{
|
||||
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response.response;
|
||||
|
||||
NSMutableArray<NSDictionary<NSString *, id> *> *headers = [NSMutableArray new];
|
||||
for (NSString *key in [httpResponse.allHeaderFields allKeys]) {
|
||||
NSDictionary<NSString *, id> *header = @{
|
||||
@"key": key,
|
||||
@"value": httpResponse.allHeaderFields[key]
|
||||
};
|
||||
[headers addObject: header];
|
||||
}
|
||||
|
||||
NSString *body = response.body;
|
||||
|
||||
[self send:@"newResponse"
|
||||
sonarObject:@{
|
||||
@"id": @(response.identifier),
|
||||
@"timestamp": @(response.timestamp),
|
||||
@"status": @(httpResponse.statusCode),
|
||||
@"reason": [NSHTTPURLResponse localizedStringForStatusCode: httpResponse.statusCode] ?: [NSNull null],
|
||||
@"headers": headers,
|
||||
@"data": body ? body : [NSNull null]
|
||||
}];
|
||||
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation SonarKitNetworkPlugin (CPPInitialization)
|
||||
|
||||
- (instancetype)initWithNetworkAdapter:(id<SKNetworkAdapterDelegate>)adapter dispatchQueue:(std::shared_ptr<facebook::flipper::DispatchQueue>)queue {
|
||||
if (self = [super initWithDispatchQueue:queue]) {
|
||||
adapter.delegate = self;
|
||||
_adapter = adapter;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user