2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-10-14 01:36:15 +00:00
|
|
|
#import "RealmReact.h"
|
2016-02-17 06:32:56 +00:00
|
|
|
#import "RealmAnalytics.h"
|
2015-11-30 20:14:50 +00:00
|
|
|
#import "RCTBridge.h"
|
2016-08-07 16:33:36 +00:00
|
|
|
#import "RCTJavaScriptExecutor.h"
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-04-12 21:42:05 +00:00
|
|
|
#import "jsc_init.h"
|
2016-01-06 23:24:00 +00:00
|
|
|
#import "shared_realm.hpp"
|
2016-03-03 23:34:56 +00:00
|
|
|
#import "realm_coordinator.hpp"
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2016-08-07 16:33:36 +00:00
|
|
|
#import <objc/runtime.h>
|
2016-02-29 11:24:51 +00:00
|
|
|
#import <arpa/inet.h>
|
|
|
|
#import <ifaddrs.h>
|
|
|
|
#import <netdb.h>
|
|
|
|
#import <net/if.h>
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
#if DEBUG
|
2016-03-23 08:35:14 +00:00
|
|
|
#import "GCDWebServer.h"
|
|
|
|
#import "GCDWebServerDataRequest.h"
|
|
|
|
#import "GCDWebServerDataResponse.h"
|
|
|
|
#import "GCDWebServerErrorResponse.h"
|
2016-01-06 23:24:00 +00:00
|
|
|
#import "rpc.hpp"
|
2016-02-29 11:24:51 +00:00
|
|
|
|
|
|
|
#define WEB_SERVER_PORT 8082
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2016-04-12 21:42:05 +00:00
|
|
|
using namespace realm::rpc;
|
2016-04-19 17:49:08 +00:00
|
|
|
#endif
|
2016-04-12 21:42:05 +00:00
|
|
|
|
2015-11-13 18:13:33 +00:00
|
|
|
@interface NSObject ()
|
2016-07-11 20:03:22 +00:00
|
|
|
- (instancetype)initWithJSContext:(JSContext *)context;
|
|
|
|
- (instancetype)initWithJSContext:(JSContext *)context onThread:(NSThread *)thread;
|
2016-08-08 17:16:43 +00:00
|
|
|
- (JSContext *)context;
|
2015-08-13 16:12:48 +00:00
|
|
|
@end
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
|
2015-10-15 23:18:58 +00:00
|
|
|
Ivar contextIvar = class_getInstanceVariable([executor class], "_context");
|
2015-10-19 17:24:51 +00:00
|
|
|
if (!contextIvar) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:15:44 +00:00
|
|
|
id rctJSContext = object_getIvar(executor, contextIvar);
|
2015-10-19 18:28:50 +00:00
|
|
|
if (!rctJSContext && create) {
|
2015-10-19 17:24:51 +00:00
|
|
|
Class RCTJavaScriptContext = NSClassFromString(@"RCTJavaScriptContext");
|
2016-08-02 16:49:08 +00:00
|
|
|
if ([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:onThread:)]) {
|
2016-07-11 20:03:22 +00:00
|
|
|
// for RN 0.28.0+
|
2016-07-14 01:28:53 +00:00
|
|
|
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new] onThread:[NSThread currentThread]];
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-07-11 20:03:22 +00:00
|
|
|
// for RN < 0.28.0
|
2016-08-02 16:49:08 +00:00
|
|
|
assert([RCTJavaScriptContext instancesRespondToSelector:@selector(initWithJSContext:)]);
|
2016-07-14 01:28:53 +00:00
|
|
|
rctJSContext = [[RCTJavaScriptContext alloc] initWithJSContext:[JSContext new]];
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
2015-11-13 19:15:44 +00:00
|
|
|
|
2016-07-14 01:28:53 +00:00
|
|
|
object_setIvar(executor, contextIvar, rctJSContext);
|
2015-10-19 17:24:51 +00:00
|
|
|
}
|
2015-11-13 19:15:44 +00:00
|
|
|
|
2016-08-08 17:16:43 +00:00
|
|
|
return [rctJSContext context].JSGlobalContextRef;
|
2015-10-15 23:18:58 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
@interface RealmReact () <RCTBridgeModule>
|
|
|
|
@end
|
2015-11-17 20:46:22 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
@implementation RealmReact {
|
|
|
|
NSMutableDictionary *_eventHandlers;
|
2015-11-16 19:26:36 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
#if DEBUG
|
2015-11-13 01:16:24 +00:00
|
|
|
GCDWebServer *_webServer;
|
2016-04-12 21:42:05 +00:00
|
|
|
std::unique_ptr<RPCServer> _rpcServer;
|
2015-11-13 01:16:24 +00:00
|
|
|
#endif
|
2015-11-17 01:57:06 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2016-03-23 08:35:14 +00:00
|
|
|
RCT_EXPORT_MODULE(Realm)
|
2015-10-14 01:36:15 +00:00
|
|
|
|
2016-02-17 06:32:56 +00:00
|
|
|
+ (void)initialize {
|
|
|
|
if (self != [RealmReact class]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RLMSendAnalytics();
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_eventHandlers = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (dispatch_queue_t)methodQueue {
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
- (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
|
|
|
|
}
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
- (void)addListenerForEvent:(NSString *)eventName handler:(RealmReactEventHandler)handler {
|
2016-01-13 01:07:48 +00:00
|
|
|
NSMutableOrderedSet *handlers = _eventHandlers[eventName];
|
|
|
|
if (!handlers) {
|
|
|
|
handlers = _eventHandlers[eventName] = [[NSMutableOrderedSet alloc] init];
|
|
|
|
}
|
2016-01-06 23:24:00 +00:00
|
|
|
[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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 01:16:24 +00:00
|
|
|
#if DEBUG
|
2016-02-29 11:24:51 +00:00
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
2015-11-13 01:16:24 +00:00
|
|
|
- (void)startRPC {
|
|
|
|
[GCDWebServer setLogLevel:3];
|
|
|
|
_webServer = [[GCDWebServer alloc] init];
|
2016-04-12 21:42:05 +00:00
|
|
|
_rpcServer = std::make_unique<RPCServer>();
|
2015-11-13 01:16:24 +00:00
|
|
|
__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) {
|
2016-04-29 20:01:01 +00:00
|
|
|
__typeof__(self) self = weakSelf;
|
|
|
|
RPCServer *rpcServer = self ? self->_rpcServer.get() : nullptr;
|
2015-11-13 01:16:24 +00:00
|
|
|
GCDWebServerResponse *response;
|
2016-04-29 20:01:01 +00:00
|
|
|
|
2015-11-13 01:16:24 +00:00
|
|
|
try {
|
2016-04-29 20:01:01 +00:00
|
|
|
NSData *responseData;
|
|
|
|
|
|
|
|
if (rpcServer) {
|
|
|
|
json args = json::parse([[(GCDWebServerDataRequest *)request text] UTF8String]);
|
|
|
|
std::string responseText = rpcServer->perform_request(request.path.UTF8String, args).dump();
|
|
|
|
|
|
|
|
responseData = [NSData dataWithBytes:responseText.c_str() length:responseText.length()];
|
|
|
|
}
|
|
|
|
else {
|
2015-11-13 01:16:24 +00:00
|
|
|
// we have been deallocated
|
|
|
|
responseData = [NSData data];
|
2016-04-29 20:01:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 01:16:24 +00:00
|
|
|
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;
|
|
|
|
}];
|
|
|
|
|
2016-02-29 11:24:51 +00:00
|
|
|
[_webServer startWithPort:WEB_SERVER_PORT bonjourName:nil];
|
2015-11-13 01:16:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)shutdownRPC {
|
|
|
|
[_webServer stop];
|
|
|
|
[_webServer removeAllHandlers];
|
|
|
|
_webServer = nil;
|
|
|
|
_rpcServer.reset();
|
|
|
|
}
|
2015-11-16 19:16:09 +00:00
|
|
|
#endif
|
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
- (void)invalidate {
|
2015-11-16 19:26:36 +00:00
|
|
|
#if DEBUG
|
|
|
|
// shutdown rpc if in chrome debug mode
|
|
|
|
[self shutdownRPC];
|
|
|
|
#endif
|
2015-11-16 19:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
2016-01-06 23:24:00 +00:00
|
|
|
[self performSelectorOnMainThread:@selector(invalidate) withObject:nil waitUntilDone:YES];
|
2015-11-16 19:16:09 +00:00
|
|
|
}
|
2015-11-13 01:16:24 +00:00
|
|
|
|
2015-10-14 01:36:15 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge {
|
2015-08-13 16:12:48 +00:00
|
|
|
_bridge = bridge;
|
2015-11-16 19:16:09 +00:00
|
|
|
|
2016-01-06 23:24:00 +00:00
|
|
|
static __weak RealmReact *s_currentModule = nil;
|
|
|
|
[s_currentModule invalidate];
|
|
|
|
s_currentModule = self;
|
|
|
|
|
|
|
|
id<RCTJavaScriptExecutor> executor = [bridge valueForKey:@"javaScriptExecutor"];
|
2015-11-16 19:16:09 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
if ([executor isKindOfClass:NSClassFromString(@"RCTWebSocketExecutor")]) {
|
2015-11-11 23:37:03 +00:00
|
|
|
#if DEBUG
|
2015-11-13 01:16:24 +00:00
|
|
|
[self startRPC];
|
2015-11-16 19:26:36 +00:00
|
|
|
#else
|
2015-11-12 19:32:16 +00:00
|
|
|
@throw [NSException exceptionWithName:@"Invalid Executor" reason:@"Chrome debug mode not supported in Release builds" userInfo:nil];
|
2015-11-16 19:26:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2015-11-17 01:36:28 +00:00
|
|
|
__weak __typeof__(self) weakSelf = self;
|
2016-04-25 23:54:39 +00:00
|
|
|
__weak __typeof__(executor) weakExecutor = executor;
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
[executor executeBlockOnJavaScriptQueue:^{
|
2016-01-06 23:24:00 +00:00
|
|
|
__typeof__(self) self = weakSelf;
|
2016-04-25 23:54:39 +00:00
|
|
|
__typeof__(executor) executor = weakExecutor;
|
|
|
|
if (!self || !executor) {
|
2015-11-17 01:57:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-06 23:24:00 +00:00
|
|
|
|
2016-04-25 23:54:39 +00:00
|
|
|
// 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();
|
2016-07-14 01:28:53 +00:00
|
|
|
|
2015-11-16 19:26:36 +00:00
|
|
|
JSGlobalContextRef ctx = RealmReactGetJSGlobalContextForExecutor(executor, true);
|
|
|
|
RJSInitializeInContext(ctx);
|
|
|
|
}];
|
2015-11-12 19:32:16 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|