RealmReact.mm
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#import "RealmReact.h"
#import "RealmAnalytics.h"
#import "jsc/jsc_init.h"
#import "impl/realm_coordinator.hpp"
#import "shared_realm.hpp"
#import <React/RCTBridge+Private.h>
#import <React/RCTJavaScriptExecutor.h>
#import <objc/runtime.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <net/if.h>
#if DEBUG
#import "GCDWebServer.h"
#import "GCDWebServerDataRequest.h"
#import "GCDWebServerDataResponse.h"
#import "GCDWebServerErrorResponse.h"
#import "rpc.hpp"
#define WEB_SERVER_PORT 8083
using namespace realm::rpc;
#endif
@interface NSObject ()
- (instancetype)initWithJSContext:(JSContext *)context;
- (instancetype)initWithJSContext:(JSContext *)context onThread:(NSThread *)thread;
- (JSContext *)context;
@end
// the part of the RCTCxxBridge private class we care about
@interface RCTBridge (Realm_RCTCxxBridge)
- (JSGlobalContextRef)jsContextRef;
- (void *)runtime;
@end
extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
if (!contextIvar) {
return NULL;
}
id rctJSContext = object_getIvar(executor, contextIvar);
if (!rctJSContext && create) {
Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
if ([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:onThread:)]) {
// for RN 0.28.0+
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new] onThread:[NSThread currentThread]];
}
else {
// for RN < 0.28.0
assert([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:)]);
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new]];
}
object_setIvar(executor, contextIvar, rctJSContext);
}
return [rctJSContext context].JSGlobalContextRef;
}
@interface RealmReact () <RCTBridgeModule>
@end
@implementation RealmReact {
NSMutableDictionary *_eventHandlers;
#if DEBUG
GCDWebServer *_webServer;
std::unique_ptr<RPCServer> _rpcServer;
#endif
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE(Realm)
+ (BOOL)requiresMainQueueSetup {
return YES;
}
+ (void)initialize {
if (self != [RealmReact class]) {
return;
}
RLMSendAnalytics();
}
- (instancetype)init {
self = [super init];
if (self) {
_eventHandlers = [[NSMutableDictionary alloc] init];
}
return self;
}
- (dispatch_queue_t)methodQueue {
return dispatch_get_main_queue();
}
- (NSDictionary *)constantsToExport {
#if DEBUG
#if TARGET_IPHONE_SIMULATOR
NSArray *hosts = @[@"localhost"];
#else
NSArray *hosts = [self getIPAddresses];
#endif
return @{
@"debugHosts": hosts,
@"debugPort": @(WEB_SERVER_PORT),
};
#else
return @{};
#endif
}
- (void)addListenerForEvent:(NSString *)eventName handler:(RealmReactEventHandler)handler {
NSMutableOrderedSet *handlers = _eventHandlers[eventName];
if (!handlers) {
handlers = _eventHandlers[eventName] = [[NSMutableOrderedSet alloc] init];
}
[handlers addObject:handler];
}
- (void)removeListenerForEvent:(NSString *)eventName handler:(RealmReactEventHandler)handler {
NSMutableOrderedSet *handlers = _eventHandlers[eventName];
[handlers removeObject:handler];
}
RCT_REMAP_METHOD(emit, emitEvent:(NSString *)eventName withObject:(id)object) {
for (RealmReactEventHandler handler in [_eventHandlers[eventName] copy]) {
handler(object);
}
}
#if DEBUG
- (NSArray *)getIPAddresses {
static const char * const wifiInterface = "en0";
struct ifaddrs *ifaddrs;
if (getifaddrs(&ifaddrs)) {
NSLog(@"Failed to get interface addresses: %s", strerror(errno));
return @[];
}
NSMutableArray *ipAddresses = [[NSMutableArray alloc] init];
char host[INET6_ADDRSTRLEN];
for (struct ifaddrs *ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr->ifa_next) {
if ((ifaddr->ifa_flags & IFF_LOOPBACK) || !(ifaddr->ifa_flags & IFF_UP)) {
// Ignore loopbacks and interfaces that aren't up.
continue;
}
struct sockaddr *addr = ifaddr->ifa_addr;
if (addr->sa_family == AF_INET) {
// Ignore link-local ipv4 addresses.
in_addr_t sin_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
if (IN_LOOPBACK(sin_addr) || IN_LINKLOCAL(sin_addr) || IN_ZERONET(sin_addr)) {
continue;
}
}
else if (addr->sa_family == AF_INET6) {
// Ignore link-local ipv6 addresses.
struct in6_addr *sin6_addr = &((struct sockaddr_in6 *)addr)->sin6_addr;
if (IN6_IS_ADDR_LOOPBACK(sin6_addr) || IN6_IS_ADDR_LINKLOCAL(sin6_addr) || IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
continue;
}
}
else {
// Ignore addresses that are not ipv4 or ipv6.
continue;
}
if (strcmp(ifaddr->ifa_name, wifiInterface)) {
// Ignore non-wifi addresses.
continue;
}
if (int error = getnameinfo(addr, addr->sa_len, host, sizeof(host), NULL, 0, NI_NUMERICHOST)) {
NSLog(@"Couldn't resolve host name for address: %s", gai_strerror(error));
continue;
}
[ipAddresses addObject:@(host)];
}
freeifaddrs(ifaddrs);
return [ipAddresses copy];
}
- (void)startRPC {
[GCDWebServer setLogLevel:3];
_webServer = [[GCDWebServer alloc] init];
_rpcServer = std::make_unique<RPCServer>();
__weak __typeof__(self) weakSelf = self;
// Add a handler to respond to POST requests on any URL
[_webServer addDefaultHandlerForMethod:@"POST"
requestClass:[GCDWebServerDataRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
__typeof__(self) self = weakSelf;
RPCServer *rpcServer = self ? self->_rpcServer.get() : nullptr;
GCDWebServerResponse *response;
try {
NSData *responseData;
if (rpcServer) {
json args = json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]);
std::string responseText = rpcServer->perform_request(request.path.UTF8String, std::move(args)).dump();
responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()];
}
else {
// we have been deallocated
responseData = [NSData data];
}
response = [[GCDWebServerDataResponse alloc] initWithData:responseData contentType:@"application/json"];
}
catch(std::exception &ex) {
NSLog(@"Invalid RPC request - %@", [(GCDWebServerDataRequest *)request text]);
response = [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_UnprocessableEntity
underlyingError:nil
message:@"Invalid RPC request"];
}
[response setValue:@"http://localhost:8081" forAdditionalHeader:@"Access-Control-Allow-Origin"];
return response;
}];
[_webServer startWithPort:WEB_SERVER_PORT bonjourName:nil];
return;
}
- (void)shutdownRPC {
[_webServer stop];
[_webServer removeAllHandlers];
_webServer = nil;
_rpcServer.reset();
}
#endif
- (void)invalidate {
#if DEBUG
// shutdown rpc if in chrome debug mode
[self shutdownRPC];
#endif
}
- (void)dealloc {
[self performSelectorOnMainThread:@selector(invalidate) withObject:nil waitUntilDone:YES];
}
typedef JSGlobalContextRef (^JSContextRefExtractor)();
void _initializeOnJSThread(JSContextRefExtractor jsContextExtractor) {
// Make sure the previous JS thread is completely finished before continuing.
static __weak NSThread *s_currentJSThread;
while (s_currentJSThread && !s_currentJSThread.finished) {
[NSThread sleepForTimeInterval:0.1];
}
s_currentJSThread = [NSThread currentThread];
// Close all cached Realms from the previous JS thread.
realm::_impl::RealmCoordinator::clear_all_caches();
RJSInitializeInContext(jsContextExtractor());
}
- (void)setBridge:(RCTBridge *)bridge {
_bridge = bridge;
static __weak RealmReact *s_currentModule = nil;
[s_currentModule invalidate];
s_currentModule = self;
if (objc_lookUpClass("RCTWebSocketExecutor") && [bridge executorClass] == objc_lookUpClass("RCTWebSocketExecutor")) {
#if DEBUG
[self startRPC];
#else
@throw [NSException exceptionWithName:@"Invalid Executor" reason:@"Chrome debug mode not supported in Release builds" userInfo:nil];
#endif
} else if ([bridge isKindOfClass:objc_lookUpClass("RCTCxxBridge")] || [NSStringFromClass([bridge class]) isEqual: @"RCTCxxBridge"]) {
// probe for the new C++ bridge in React Native 0.45+
__weak __typeof__(self) weakSelf = self;
__weak __typeof__(bridge) weakBridge = bridge;
[bridge dispatchBlock:^{
__typeof__(self) self = weakSelf;
__typeof__(bridge) bridge = weakBridge;
if (!self || !bridge) {
return;
}
_initializeOnJSThread(^{
// RN < 0.58 has a private method that returns the js context
if ([bridge respondsToSelector:@selector(jsContextRef)]) {
return [bridge jsContextRef];
}
// RN 0.58+ wraps the js context in the jsi abstraction layer,
// which doesn't have any way to obtain the JSGlobalContextRef,
// so engage in some undefined behavior and slurp out the
// member variable
struct RealmJSCRuntime {
virtual ~RealmJSCRuntime() = 0;
JSGlobalContextRef ctx_;
};
return static_cast<RealmJSCRuntime*>(bridge.runtime)->ctx_;
});
} queue:RCTJSThread];
} else { // React Native 0.44 and older
id<RCTJavaScriptExecutor> executor = [bridge valueForKey:@"javaScriptExecutor"];
__weak __typeof__(self) weakSelf = self;
__weak __typeof__(executor) weakExecutor = executor;
[executor executeBlockOnJavaScriptQueue:^{
__typeof__(self) self = weakSelf;
__typeof__(executor) executor = weakExecutor;
if (!self || !executor) {
return;
}
_initializeOnJSThread(^ {
return RealmReactGetJSGlobalContextForExecutor(executor, true);
});
}];
}
}
@end