2017-06-08 14:34:45 +00:00
|
|
|
#import "RCTInspectorDevServerHelper.h"
|
|
|
|
|
|
|
|
#if RCT_DEV
|
|
|
|
|
|
|
|
#import <jschelpers/JSCWrapper.h>
|
2017-07-10 13:15:52 +00:00
|
|
|
#import <UIKit/UIKit.h>
|
2017-06-08 14:34:45 +00:00
|
|
|
|
|
|
|
#import "RCTDefines.h"
|
|
|
|
#import "RCTInspectorPackagerConnection.h"
|
|
|
|
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
2017-08-14 15:07:13 +00:00
|
|
|
static NSString *const kDebuggerMsgDisable = @"{ \"id\":1,\"method\":\"Debugger.disable\" }";
|
|
|
|
|
2017-06-08 14:34:45 +00:00
|
|
|
static NSString *getDebugServerHost(NSURL *bundleURL)
|
|
|
|
{
|
|
|
|
NSString *host = [bundleURL host];
|
|
|
|
if (!host) {
|
|
|
|
host = @"localhost";
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:15:52 +00:00
|
|
|
// Inspector Proxy is run on a separate port (from packager).
|
|
|
|
NSNumber *port = @8082;
|
2017-06-08 14:34:45 +00:00
|
|
|
|
|
|
|
// this is consistent with the Android implementation, where http:// is the
|
|
|
|
// hardcoded implicit scheme for the debug server. Note, packagerURL
|
|
|
|
// technically looks like it could handle schemes/protocols other than HTTP,
|
|
|
|
// so rather than force HTTP, leave it be for now, in case someone is relying
|
|
|
|
// on that ability when developing against iOS.
|
|
|
|
return [NSString stringWithFormat:@"%@:%@", host, port];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSURL *getInspectorDeviceUrl(NSURL *bundleURL)
|
|
|
|
{
|
2017-07-19 18:38:52 +00:00
|
|
|
NSString *escapedDeviceName = [[[UIDevice currentDevice] name] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSString *escapedAppName = [[[NSBundle mainBundle] bundleIdentifier] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/inspector/device?name=%@&app=%@",
|
2017-06-08 14:34:45 +00:00
|
|
|
getDebugServerHost(bundleURL),
|
2017-07-19 18:38:52 +00:00
|
|
|
escapedDeviceName,
|
|
|
|
escapedAppName]];
|
2017-06-08 14:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@implementation RCTInspectorDevServerHelper
|
|
|
|
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
|
2017-08-14 15:07:13 +00:00
|
|
|
static NSMutableDictionary<NSString *, RCTInspectorPackagerConnection *> *socketConnections = nil;
|
|
|
|
|
|
|
|
static void sendEventToAllConnections(NSString *event)
|
|
|
|
{
|
|
|
|
for (NSString *socketId in socketConnections) {
|
|
|
|
[socketConnections[socketId] sendEventToAllConnections:event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)disableDebugger
|
|
|
|
{
|
|
|
|
sendEventToAllConnections(kDebuggerMsgDisable);
|
|
|
|
}
|
|
|
|
|
2017-06-08 14:34:45 +00:00
|
|
|
+ (void)connectForContext:(JSGlobalContextRef)context
|
|
|
|
withBundleURL:(NSURL *)bundleURL
|
|
|
|
{
|
|
|
|
if (!isCustomJSCPtr(context)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURL *inspectorURL = getInspectorDeviceUrl(bundleURL);
|
|
|
|
|
|
|
|
// Note, using a static dictionary isn't really the greatest design, but
|
|
|
|
// the packager connection does the same thing, so it's at least consistent.
|
|
|
|
// This is a static map that holds different inspector clients per the inspectorURL
|
|
|
|
if (socketConnections == nil) {
|
|
|
|
socketConnections = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *key = [inspectorURL absoluteString];
|
|
|
|
RCTInspectorPackagerConnection *connection = socketConnections[key];
|
|
|
|
if (!connection) {
|
|
|
|
connection = [[RCTInspectorPackagerConnection alloc] initWithURL:inspectorURL];
|
|
|
|
socketConnections[key] = connection;
|
|
|
|
[connection connect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#endif
|