Summary: Move UIDebugger plugin to OSS space.

Reviewed By: passy

Differential Revision: D47634848

fbshipit-source-id: 90e8c0181a2434d0e5d76bdb99b902051e6d702e
This commit is contained in:
Lorenzo Blasa
2023-07-21 04:47:13 -07:00
committed by Facebook GitHub Bot
parent af5b9532ec
commit db7aa9eeaf
134 changed files with 6368 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C" {
#endif
extern void UIDRunBlockOnMainThread(dispatch_block_t block);
extern void UIDRunBlockOnMainThreadAsync(dispatch_block_t block);
extern void UIDRunBlockOnMainThreadAfter(
dispatch_block_t block,
unsigned int seconds);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,37 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import "UIDMainThread.h"
#ifdef __cplusplus
extern "C" {
#endif
void UIDRunBlockOnMainThread(dispatch_block_t block) {
if ([NSThread isMainThread]) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
void UIDRunBlockOnMainThreadAsync(dispatch_block_t block) {
dispatch_async(dispatch_get_main_queue(), block);
}
void UIDRunBlockOnMainThreadAfter(
dispatch_block_t block,
unsigned int seconds) {
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC),
dispatch_get_main_queue(),
block);
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C" {
#endif
void UIDPerformanceSet(NSString* name, double ms);
void UIDPerformanceAggregate(NSString* name, double ms);
NSDictionary<NSString*, NSNumber*>* UIDPerformanceGet(void);
void UIDPerformanceClear(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,49 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#include "UIDPerformance.h"
#ifdef __cplusplus
extern "C" {
#endif
static NSMutableDictionary<NSString*, NSNumber*>* performance = nil;
static void ensureInitialised(void) {
if (!performance) {
performance = [NSMutableDictionary new];
}
}
void UIDPerformanceSet(NSString* name, double ms) {
ensureInitialised();
[performance setObject:@(ms) forKey:name];
}
void UIDPerformanceAggregate(NSString* name, double ms) {
ensureInitialised();
NSNumber* existingMs = [performance objectForKey:name];
if (existingMs) {
ms += existingMs.doubleValue;
}
[performance setObject:@(ms) forKey:name];
}
NSDictionary<NSString*, NSNumber*>* UIDPerformanceGet(void) {
ensureInitialised();
return [performance copy];
}
void UIDPerformanceClear(void) {
ensureInitialised();
[performance removeAllObjects];
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,23 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#ifdef __cplusplus
extern "C" {
#endif
UIImage* UIDViewSnapshot(UIView* view);
UIImage* UIDApplicationSnapshot(UIApplication* application, NSArray* windows);
#ifdef __cplusplus
}
#endif
NS_ASSUME_NONNULL_END
#endif

View File

@@ -0,0 +1,49 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import "UIDSnapshot.h"
#import "UIDUIKitObserver.h"
#ifdef __cplusplus
extern "C" {
#endif
UIImage* UIDViewSnapshot(UIView* view) {
UIGraphicsImageRenderer* renderer =
[[UIGraphicsImageRenderer alloc] initWithSize:view.bounds.size];
return [renderer imageWithActions:^(UIGraphicsImageRendererContext* context) {
[[UIDUIKitObserver sharedInstance] setDrawObservationEnabled:false];
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:true];
[[UIDUIKitObserver sharedInstance] setDrawObservationEnabled:true];
}];
}
UIImage* UIDApplicationSnapshot(UIApplication* application, NSArray* windows) {
CGSize size = [UIScreen mainScreen].bounds.size;
UIGraphicsImageRenderer* renderer =
[[UIGraphicsImageRenderer alloc] initWithSize:size];
[[UIDUIKitObserver sharedInstance] setDrawObservationEnabled:false];
UIImage* snapshot = [renderer
imageWithActions:^(UIGraphicsImageRendererContext* rendererContext) {
for (UIWindow* window in windows) {
if (window.isHidden)
continue;
[window
drawViewHierarchyInRect:CGRectMake(0, 0, size.width, size.height)
afterScreenUpdates:true];
}
}];
[[UIDUIKitObserver sharedInstance] setDrawObservationEnabled:true];
return snapshot;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,14 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <Foundation/Foundation.h>
void UIDSwizzleMethod(Class cls, SEL original, SEL replacement, BOOL create);
#endif

View File

@@ -0,0 +1,57 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import "UIDSwizzle.h"
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
void UIDSwizzleMethod(Class cls, SEL original, SEL replacement, BOOL create) {
Method originalMethod = class_getInstanceMethod(cls, original);
Method replacementMethod = class_getInstanceMethod(cls, replacement);
/**
The method is not implemented by the target class and the create option is
set to false.
*/
if (originalMethod == NULL && !create) {
return;
}
/**
Use class_addMethod to add an implementation of the method to the target
class, which we do using our replacement implementation.
*/
BOOL didAddMethod = class_addMethod(
cls,
original,
method_getImplementation(replacementMethod),
method_getTypeEncoding(replacementMethod));
if (didAddMethod) {
/**
Use class_replaceMethod to replace calls to the swizzled method to the
original one. i.e. when you call the replacement method, it actually calls
the original method.
*/
class_replaceMethod(
cls,
replacement,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
/**
If the method is defined by the target class, class_addMethod will fail.
Use method_exchangeImplementations to exchange the new and old
implementations.
*/
method_exchangeImplementations(originalMethod, replacementMethod);
}
}
#endif

View File

@@ -0,0 +1,29 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIDThrottle : NSObject
@property(nonatomic, readonly) int64_t rate;
- (instancetype)initWithRate:(int64_t)rate;
- (instancetype)initWithRate:(int64_t)rate
queue:(nonnull dispatch_queue_t)queue;
- (void)run:(dispatch_block_t)block;
- (void)cancel;
@end
NS_ASSUME_NONNULL_END
#endif

View File

@@ -0,0 +1,73 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import "UIDThrottle.h"
@interface UIDThrottle () {
dispatch_queue_t _queue;
dispatch_block_t _block;
dispatch_block_t _throttleJob;
}
@end
@implementation UIDThrottle
- (instancetype)initWithRate:(int64_t)rate {
return [self initWithRate:rate queue:dispatch_get_main_queue()];
}
- (instancetype)initWithRate:(int64_t)rate
queue:(nonnull dispatch_queue_t)queue {
self = [super init];
if (self) {
_rate = rate;
_queue = queue;
_block = nil;
}
return self;
}
- (void)run:(dispatch_block_t)block {
/**
Check if there's a block already queued. If there's none, create one.
The reason we have two blocks:
_throttleJob captures the incoming job, always.
_block is the one currently in the queue, which will execute after a
delay. This last block is only queued when there's none, it will clear
itself after executing.
*/
_throttleJob = block;
if (_block == nil) {
__weak typeof(self) weakSelf = self;
_block = ^(void) {
if (weakSelf) {
typeof(self) strongSelf = weakSelf;
strongSelf->_throttleJob();
strongSelf->_throttleJob = nil;
strongSelf->_block = nil;
}
};
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, _rate * NSEC_PER_MSEC),
_queue,
_block);
}
}
- (void)cancel {
_throttleJob = nil;
_block = nil;
}
@end
#endif

View File

@@ -0,0 +1,25 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import <Foundation/Foundation.h>
#ifdef __cplusplus
extern "C" {
#endif
double UIDTimeIntervalToMS(NSTimeInterval timeInterval);
double UIDPerformanceTimeIntervalSince1970(void);
double UIDMonotonicTimeConvertMachUnitsToMS(uint64_t elapsed);
uint64_t UIDPerformanceNow(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,52 @@
/*
* 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.
*/
#if FB_SONARKIT_ENABLED
#import "UIDTimeUtilities.h"
#include <mach/mach_time.h>
#ifdef __cplusplus
extern "C" {
#endif
static mach_timebase_info_data_t _get_timebase(void) {
static mach_timebase_info_data_t machInfo;
static dispatch_once_t machInfoOnceToken = 0;
dispatch_once(&machInfoOnceToken, ^{
const int ret = mach_timebase_info(&machInfo);
if (ret != KERN_SUCCESS) {
machInfo.numer = 0;
machInfo.denom = 1;
}
});
return machInfo;
}
double UIDMonotonicTimeConvertMachUnitsToMS(uint64_t elapsed) {
const mach_timebase_info_data_t tb_info = _get_timebase();
return (double)elapsed * (double)tb_info.numer / (double)tb_info.denom /
(1000.0 * 1000.0);
}
double UIDPerformanceTimeIntervalSince1970(void) {
return [[NSDate date] timeIntervalSince1970];
}
double UIDTimeIntervalToMS(NSTimeInterval timeInterval) {
return (timeInterval * 1000);
}
uint64_t UIDPerformanceNow(void) {
return mach_absolute_time();
}
#ifdef __cplusplus
}
#endif
#endif