|
| 1 | +// capture-sdk - bitdrift's client SDK |
| 2 | +// Copyright Bitdrift, Inc. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a source available license that can be found in the |
| 5 | +// LICENSE file or at: |
| 6 | +// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt |
| 7 | + |
| 8 | +#import "InternalAPI.h" |
| 9 | +#import <objc/runtime.h> |
| 10 | + |
| 11 | + |
| 12 | +#pragma mark Library Specific |
| 13 | + |
| 14 | +// Place library-specific things (declarations, imports, etc) here. |
| 15 | +// Library-specific API implementations are at the end of this file. |
| 16 | +// Everything else in between should not be touched. |
| 17 | + |
| 18 | +#define LIBRARY_NAME Capture |
| 19 | + |
| 20 | + |
| 21 | +#pragma mark Base Implementation |
| 22 | + |
| 23 | +#define CONCAT(a, ...) CONCAT2(a, __VA_ARGS__) |
| 24 | +#define CONCAT2(a, ...) a ## __VA_ARGS__ |
| 25 | +#define STRINGIFY(s) STRINGIFY2(s) |
| 26 | +#define STRINGIFY2(s) #s |
| 27 | + |
| 28 | +#define API_CLASS CONCAT(BD_InternalAPI_, LIBRARY_NAME) |
| 29 | +#define PROXY_CLASS CONCAT(BD_Proxy_, LIBRARY_NAME) |
| 30 | +#define LIB_DOMAIN @"io.bitdrift." STRINGIFY(LIBRARY_NAME) |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Proxy that no-ops whenever an unimplemented method is called. |
| 35 | + */ |
| 36 | +@interface PROXY_CLASS : NSProxy |
| 37 | + |
| 38 | +@property(nonatomic,strong) id proxied; |
| 39 | + |
| 40 | +@end |
| 41 | + |
| 42 | +@implementation PROXY_CLASS |
| 43 | + |
| 44 | ++ (instancetype)proxyTo:(id)objectToProxy { |
| 45 | + if (objectToProxy == nil) { |
| 46 | + return nil; |
| 47 | + } |
| 48 | + |
| 49 | + PROXY_CLASS *proxy = [PROXY_CLASS alloc]; |
| 50 | + proxy.proxied = objectToProxy; |
| 51 | + return proxy; |
| 52 | +} |
| 53 | + |
| 54 | +- (void)forwardInvocation:(NSInvocation *)invocation { |
| 55 | + if ([self.proxied respondsToSelector:invocation.selector]) { |
| 56 | + [invocation setTarget:self.proxied]; |
| 57 | + [invocation invoke]; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +-(NSMethodSignature*)methodSignatureForSelector:(SEL)selector { |
| 62 | + NSMethodSignature *signature = [self.proxied methodSignatureForSelector:selector]; |
| 63 | + if (signature != nil) { |
| 64 | + return signature; |
| 65 | + } |
| 66 | + |
| 67 | + // We must return a real signature or else it will crash, so use NSObject.init because it always exists. |
| 68 | + return [NSObject instanceMethodSignatureForSelector:@selector(init)]; |
| 69 | +} |
| 70 | + |
| 71 | +- (BOOL)isEqual:(id)object { return [self.proxied isEqual:object]; } |
| 72 | +- (NSUInteger)hash { return [self.proxied hash]; } |
| 73 | +- (Class)superclass { return [self.proxied superclass]; } |
| 74 | +- (Class)class { return [self.proxied class]; } |
| 75 | +- (BOOL)isProxy { return YES; } |
| 76 | +- (BOOL)isKindOfClass:(Class)aClass { return [self.proxied isKindOfClass:aClass]; } |
| 77 | +- (BOOL)isMemberOfClass:(Class)aClass { return [self.proxied isMemberOfClass:aClass]; } |
| 78 | +- (BOOL)conformsToProtocol:(Protocol *)aProtocol { return [self.proxied conformsToProtocol:aProtocol]; } |
| 79 | +- (BOOL)respondsToSelector:(SEL)aSelector { return [self.proxied respondsToSelector:aSelector]; } |
| 80 | +- (NSString *)description { return [self.proxied description]; } |
| 81 | +- (NSString *)debugDescription { return [self.proxied debugDescription]; } |
| 82 | + |
| 83 | +@end |
| 84 | + |
| 85 | + |
| 86 | +@implementation API_CLASS |
| 87 | + |
| 88 | ++ (instancetype)instance { |
| 89 | + static API_CLASS *instance; |
| 90 | + static dispatch_once_t once; |
| 91 | + dispatch_once(&once, ^{ instance = [[self alloc] init]; }); |
| 92 | + return instance; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * "null" method that gets mapped if a client requests an API name that doesn't exist. |
| 97 | + */ |
| 98 | +- (id)nullMethod { |
| 99 | + return nil; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Expose an API on this class, assigning it to the specified selector so that it can be called normally. |
| 104 | + * If the API name is not found, `nullMethod` will be mapped to the selector. |
| 105 | + * |
| 106 | + * @param apiName A string describing the selector of the internal method to map. |
| 107 | + * @param asSelector The selector to map this method to (if found) |
| 108 | + * @return `nil` on success, or an error if the objc runtime is seriously broken (almost impossible). |
| 109 | + */ |
| 110 | +- (NSError *)exposeAPI:(NSString * _Nonnull)apiName asSelector:(SEL)asSelector { |
| 111 | + if(class_getInstanceMethod(self.class, asSelector) != nil) { |
| 112 | + NSLog(@"WARNING: Class %@ already implements selector '%@'. Keeping existing mapping.", self.class, NSStringFromSelector(asSelector)); |
| 113 | + return nil; |
| 114 | + } |
| 115 | + |
| 116 | + SEL selectorToClone = @selector(nullMethod); |
| 117 | + |
| 118 | + SEL foundSelector = NSSelectorFromString(apiName); |
| 119 | + if(class_getInstanceMethod(self.class, foundSelector) != nil) { |
| 120 | + selectorToClone = foundSelector; |
| 121 | + } else { |
| 122 | + NSLog(@"WARNING: Class %@ doesn't have method '%@' to clone. Mapping to a null implementation.", self.class, apiName); |
| 123 | + } |
| 124 | + |
| 125 | +#define NSERROR(FMT, ...) [NSError errorWithDomain:LIB_DOMAIN code:0 userInfo:@{ \ |
| 126 | + NSLocalizedDescriptionKey:[NSString stringWithFormat:FMT, __VA_ARGS__] }]; |
| 127 | + |
| 128 | + // Note: These errors should never happen unless the objective-c runtime is seriously broken. |
| 129 | + |
| 130 | + Method method = class_getInstanceMethod(self.class, selectorToClone); |
| 131 | + if(method == nil) { |
| 132 | + return NSERROR(@"class_getInstanceMethod(%@, %@) failed", self.class, NSStringFromSelector(selectorToClone)); |
| 133 | + } |
| 134 | + |
| 135 | + IMP implementation = method_getImplementation(method); |
| 136 | + if(implementation == nil) { |
| 137 | + return NSERROR(@"method_getImplementation(%@) failed", NSStringFromSelector(selectorToClone)); |
| 138 | + } |
| 139 | + |
| 140 | + const char *encoding = method_getTypeEncoding(method); |
| 141 | + if(encoding == nil) { |
| 142 | + return NSERROR(@"method_getTypeEncoding(%@) failed", NSStringFromSelector(selectorToClone)); |
| 143 | + } |
| 144 | + |
| 145 | + if(!class_addMethod(self.class, asSelector, implementation, encoding)) { |
| 146 | + return NSERROR(@"class_addMethod(%@, %@, ...) failed", self.class, NSStringFromSelector(asSelector)); |
| 147 | + } |
| 148 | + |
| 149 | + return nil; |
| 150 | +} |
| 151 | + |
| 152 | +- (void)forwardInvocation:(NSInvocation *)invocation { |
| 153 | + // If a particular API call will be made often, probably best to not log here. |
| 154 | + NSLog(@"WARNING: Called nonexistent API '%@', which will no-op", NSStringFromSelector(invocation.selector)); |
| 155 | +} |
| 156 | + |
| 157 | +-(NSMethodSignature*)methodSignatureForSelector:(SEL)selector { |
| 158 | + // We must return a real signature or else it will crash, so use NSObject.init because it always exists. |
| 159 | + return [NSObject instanceMethodSignatureForSelector:@selector(init)]; |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +#pragma mark API implementation |
| 164 | + |
| 165 | + |
| 166 | +#ifdef DEBUG |
| 167 | +// These are called by the unit tests. Do not remove them! |
| 168 | + |
| 169 | +- (void)exampleWithVoidReturn_v1 { |
| 170 | +} |
| 171 | + |
| 172 | +- (NSString *)exampleWithIDReturn_v1 { |
| 173 | + return @"This is an example API"; |
| 174 | +} |
| 175 | + |
| 176 | +- (NSString *)exampleWithProxyReturn_v1 { |
| 177 | + return (NSString *)[PROXY_CLASS proxyTo:@"This is a proxied string"]; |
| 178 | +} |
| 179 | +#endif |
| 180 | + |
| 181 | +@end |
0 commit comments