2020-02-25 14:20:27 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// DoricDev.m
|
|
|
|
// Doric
|
|
|
|
//
|
|
|
|
// Created by jingpeng.wang on 2020/2/25.
|
|
|
|
//
|
2020-02-27 16:13:14 +08:00
|
|
|
#import <DoricCore/Doric.h>
|
2021-07-21 17:56:03 +08:00
|
|
|
#import <DoricCore/DoricSingleton.h>
|
2020-02-27 16:13:14 +08:00
|
|
|
#import <DoricCore/DoricNativeDriver.h>
|
2021-02-22 19:03:34 +08:00
|
|
|
#import <DoricCore/DoricContextManager.h>
|
2023-10-12 12:01:15 +08:00
|
|
|
#import <DoricCore/DoricJSEngine.h>
|
|
|
|
#import <DoricCore/DoricJSCoreExecutor.h>
|
2020-02-25 14:20:27 +08:00
|
|
|
|
|
|
|
#import "DoricDev.h"
|
2020-02-27 16:13:14 +08:00
|
|
|
#import "DoricDebugDriver.h"
|
2020-02-28 17:31:59 +08:00
|
|
|
#import "DoricDevViewController.h"
|
2021-02-05 19:36:25 +08:00
|
|
|
#import "DoricDevMonitor.h"
|
2021-07-20 11:29:53 +08:00
|
|
|
#import "DoricDevPerformanceAnchorHook.h"
|
2021-08-26 16:48:29 +08:00
|
|
|
#import "DoricDevkitPlugin.h"
|
2021-12-07 18:53:43 +08:00
|
|
|
#import "DoricDevAssetsLoader.h"
|
2021-02-05 19:36:25 +08:00
|
|
|
|
2021-02-22 19:03:34 +08:00
|
|
|
@interface DoricContextDebuggable : NSObject
|
|
|
|
@property(nonatomic, weak) DoricContext *doricContext;
|
|
|
|
@property(nonatomic, weak) id <DoricDriverProtocol> nativeDriver;
|
|
|
|
@property(nonatomic, weak) DoricWSClient *wsClient;
|
2021-03-04 10:02:29 +08:00
|
|
|
@property(nonatomic, weak) DoricDebugDriver *debugDriver;
|
|
|
|
|
2021-02-22 19:03:34 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricContextDebuggable
|
|
|
|
- (instancetype)initWithWSClient:(DoricWSClient *)client context:(DoricContext *)context {
|
|
|
|
if (self = [super init]) {
|
|
|
|
_wsClient = client;
|
|
|
|
_doricContext = context;
|
|
|
|
_nativeDriver = context.driver;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startDebug {
|
|
|
|
[self.doricContext setDriver:[[DoricDebugDriver alloc] initWithWSClient:self.wsClient]];
|
2021-03-04 10:02:29 +08:00
|
|
|
self.debugDriver = self.doricContext.driver;
|
2021-02-22 19:03:34 +08:00
|
|
|
[self.doricContext reload:self.doricContext.script];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopDebug:(BOOL)resume {
|
2021-03-04 10:02:29 +08:00
|
|
|
[self.debugDriver teardown];
|
2021-02-24 19:13:19 +08:00
|
|
|
self.doricContext.driver = self.nativeDriver;
|
2021-02-22 19:03:34 +08:00
|
|
|
if (resume) {
|
|
|
|
[self.doricContext reload:self.doricContext.script];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2020-02-25 14:20:27 +08:00
|
|
|
@interface DoricDev ()
|
2021-02-23 17:24:02 +08:00
|
|
|
@property(nonatomic, strong, nullable) DoricWSClient *wsClient;
|
2021-02-22 19:03:34 +08:00
|
|
|
@property(nonatomic, strong) DoricContextDebuggable *debuggable;
|
2021-02-24 15:35:44 +08:00
|
|
|
@property(nonatomic, strong) NSHashTable <id <DoricDevStatusCallback>> *callbacks;
|
|
|
|
@property(nonatomic, strong) NSHashTable <DoricContext *> *reloadingContexts;
|
|
|
|
@property(nonatomic, assign) BOOL devKitConnected;
|
|
|
|
@property(nonatomic, copy) NSString *url;
|
2020-02-25 14:20:27 +08:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation DoricDev
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
if (self = [super init]) {
|
2021-02-24 15:35:44 +08:00
|
|
|
_callbacks = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
|
2021-02-24 19:13:19 +08:00
|
|
|
_reloadingContexts = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
|
2021-07-21 17:56:03 +08:00
|
|
|
[DoricSingleton.instance.nativeDriver.registry registerMonitor:[DoricDevMonitor new]];
|
|
|
|
DoricSingleton.instance.nativeDriver.registry.globalPerformanceAnchorHook = [DoricDevPerformanceAnchorHook new];
|
2021-08-26 16:48:29 +08:00
|
|
|
[DoricSingleton.instance.nativeDriver.registry registerNativePlugin:DoricDevkitPlugin.class withName:@"devkit"];
|
2021-12-07 18:53:43 +08:00
|
|
|
[DoricSingleton.instance.nativeDriver.registry.loaderManager registerLoader:[DoricDevAssetsLoader new]];
|
2020-02-25 14:20:27 +08:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)instance {
|
|
|
|
static DoricDev *_instance;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
_instance = [[DoricDev alloc] init];
|
|
|
|
});
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:31:59 +08:00
|
|
|
- (void)openDevMode {
|
|
|
|
DoricDevViewController *devViewController = [DoricDevViewController new];
|
|
|
|
|
|
|
|
UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
|
|
|
|
UINavigationController *navigationController;
|
|
|
|
if ([viewController isKindOfClass:[UINavigationController class]]) {
|
|
|
|
navigationController = (UINavigationController *) viewController;
|
|
|
|
} else {
|
|
|
|
navigationController = viewController.navigationController;
|
|
|
|
}
|
|
|
|
[navigationController pushViewController:devViewController animated:NO];
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:31:33 +08:00
|
|
|
- (void)openDevMode:(UIViewController *)vc {
|
|
|
|
DoricDevViewController *devViewController = [DoricDevViewController new];
|
|
|
|
|
|
|
|
UIViewController *viewController = findBestViewController(vc);
|
|
|
|
UINavigationController *navigationController;
|
|
|
|
if ([viewController isKindOfClass:[UINavigationController class]]) {
|
|
|
|
navigationController = (UINavigationController *) viewController;
|
|
|
|
} else {
|
|
|
|
navigationController = viewController.navigationController;
|
|
|
|
}
|
|
|
|
[navigationController pushViewController:devViewController animated:NO];
|
2023-10-12 12:01:15 +08:00
|
|
|
if (@available(iOS 16.4, *)) {
|
|
|
|
DoricJSEngine *jsEngine = DoricSingleton.instance.nativeDriver.jsExecutor;
|
|
|
|
if ([jsEngine.jsExecutor isKindOfClass:DoricJSCoreExecutor.class]) {
|
|
|
|
DoricJSCoreExecutor *jsCoreExecutor = jsEngine.jsExecutor;
|
|
|
|
jsCoreExecutor.jsContext.inspectable = YES;
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:31:59 +08:00
|
|
|
- (void)closeDevMode {
|
2021-02-24 19:13:19 +08:00
|
|
|
[self stopDebugging:YES];
|
2021-02-22 19:03:34 +08:00
|
|
|
if (self.wsClient) {
|
|
|
|
[self.wsClient close];
|
|
|
|
self.wsClient = nil;
|
2020-02-25 14:20:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:31:59 +08:00
|
|
|
- (BOOL)isInDevMode {
|
2021-02-24 15:35:44 +08:00
|
|
|
return self.devKitConnected;
|
2020-02-26 14:51:02 +08:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:31:59 +08:00
|
|
|
- (void)connectDevKit:(NSString *)url {
|
2021-02-22 19:03:34 +08:00
|
|
|
if (self.wsClient) {
|
|
|
|
[self.wsClient close];
|
2020-02-25 14:20:27 +08:00
|
|
|
}
|
2021-02-24 15:35:44 +08:00
|
|
|
self.devKitConnected = NO;
|
2021-02-22 19:03:34 +08:00
|
|
|
self.wsClient = [[DoricWSClient alloc] initWithUrl:url];
|
2021-02-24 15:35:44 +08:00
|
|
|
self.url = url;
|
2020-02-25 14:20:27 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:35:44 +08:00
|
|
|
- (void)onOpen {
|
|
|
|
self.devKitConnected = YES;
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onOpen:self.url];
|
|
|
|
}
|
|
|
|
});
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:35:44 +08:00
|
|
|
- (void)onClose {
|
|
|
|
self.devKitConnected = NO;
|
2021-03-02 16:43:28 +08:00
|
|
|
[self stopDebugging:YES];
|
2021-02-24 15:35:44 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onClose:self.url];
|
|
|
|
}
|
|
|
|
});
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:35:44 +08:00
|
|
|
- (void)onFailure:(NSError *)error {
|
|
|
|
self.devKitConnected = NO;
|
2021-03-02 16:43:28 +08:00
|
|
|
[self stopDebugging:YES];
|
2021-02-24 15:35:44 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onFailure:error];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isReloadingContext:(DoricContext *)context {
|
|
|
|
return [self.reloadingContexts containsObject:context];
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 15:35:44 +08:00
|
|
|
|
2021-07-23 10:36:49 +08:00
|
|
|
- (NSArray<DoricContext *> *)matchAllContexts:(NSString *)source {
|
|
|
|
NSMutableArray <DoricContext *> *array = [NSMutableArray new];
|
2021-02-25 11:31:55 +08:00
|
|
|
source = [[source stringByReplacingOccurrencesOfString:@".js"
|
|
|
|
withString:@""]
|
|
|
|
stringByReplacingOccurrencesOfString:@".ts"
|
|
|
|
withString:@""
|
|
|
|
];
|
2021-02-22 19:03:34 +08:00
|
|
|
for (DoricContext *context in [DoricContextManager.instance aliveContexts]) {
|
2022-01-10 10:28:20 +08:00
|
|
|
NSString *contextSource = context.source;
|
|
|
|
NSArray<NSString *> *split = [contextSource componentsSeparatedByString:@";"];
|
|
|
|
if (split.count > 1) {
|
|
|
|
contextSource = split[0];
|
|
|
|
}
|
|
|
|
contextSource = [[contextSource stringByReplacingOccurrencesOfString:@".js"
|
|
|
|
withString:@""]
|
2021-02-25 11:31:55 +08:00
|
|
|
stringByReplacingOccurrencesOfString:@".ts"
|
|
|
|
withString:@""
|
|
|
|
];
|
|
|
|
if ([source isEqualToString:contextSource] || [contextSource isEqualToString:@"__dev__"]) {
|
2021-07-23 10:36:49 +08:00
|
|
|
[array addObject:context];
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-23 10:36:49 +08:00
|
|
|
return array;
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:03:34 +08:00
|
|
|
- (void)reload:(NSString *)source script:(NSString *)script {
|
2021-07-23 10:36:49 +08:00
|
|
|
NSArray<DoricContext *> *contexts = [self matchAllContexts:source];
|
|
|
|
if (contexts.count <= 0) {
|
2021-02-22 19:03:34 +08:00
|
|
|
DoricLog(@"Cannot find context source %@ for reload", source);
|
2021-07-23 10:36:49 +08:00
|
|
|
} else {
|
|
|
|
[contexts forEach:^(DoricContext *context) {
|
|
|
|
if ([context.driver isKindOfClass:DoricDebugDriver.class]) {
|
|
|
|
DoricLog(@"Context source %@ in debugging,skip reload", source);
|
|
|
|
} else {
|
|
|
|
DoricLog(@"Context reload :id %@,source %@", context.contextId, source);
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[context reload:script];
|
|
|
|
[self.reloadingContexts addObject:context];
|
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onReload:context script:script];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}];
|
2021-02-22 19:03:34 +08:00
|
|
|
}
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:03:34 +08:00
|
|
|
- (void)startDebugging:(NSString *)source {
|
|
|
|
[self.debuggable stopDebug:YES];
|
2021-07-23 10:36:49 +08:00
|
|
|
NSArray<DoricContext *> *contexts = [self matchAllContexts:source];
|
|
|
|
if (contexts.count <= 0) {
|
|
|
|
DoricLog(@"Cannot find context source %@ for debugging", source);
|
|
|
|
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
|
|
|
|
@"msg": @"Cannot find suitable alive context for debugging"
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
DoricContext *context = contexts.lastObject;
|
2021-02-22 19:03:34 +08:00
|
|
|
[self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{
|
|
|
|
@"contextId": context.contextId
|
|
|
|
}];
|
2021-02-24 15:35:44 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2021-03-04 12:53:42 +08:00
|
|
|
self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context];
|
|
|
|
[self.debuggable startDebug];
|
2021-02-24 15:35:44 +08:00
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onStartDebugging:context];
|
|
|
|
}
|
|
|
|
});
|
2021-02-22 19:03:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
- (void)stopDebugging:(BOOL)resume {
|
|
|
|
[self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{
|
|
|
|
@"msg": @"Stop debugging"
|
|
|
|
}];
|
2021-02-24 15:35:44 +08:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2021-03-04 12:53:42 +08:00
|
|
|
[self.debuggable stopDebug:resume];
|
|
|
|
self.debuggable = nil;
|
2021-02-24 15:35:44 +08:00
|
|
|
for (id <DoricDevStatusCallback> callback in self.callbacks) {
|
|
|
|
[callback onStopDebugging];
|
|
|
|
}
|
|
|
|
});
|
2020-02-27 16:13:14 +08:00
|
|
|
}
|
2021-02-23 17:24:02 +08:00
|
|
|
|
2021-03-02 15:11:16 +08:00
|
|
|
- (void)requestDebugging:(DoricContext *)context {
|
|
|
|
[self.wsClient sendToServer:@"DEBUG" payload:@{
|
2021-03-02 15:49:10 +08:00
|
|
|
@"source": context.source,
|
|
|
|
@"script": context.script,
|
2021-03-02 15:11:16 +08:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2021-02-23 17:24:02 +08:00
|
|
|
- (void)sendDevCommand:(NSString *)command payload:(NSDictionary *)payload {
|
|
|
|
[self.wsClient sendToServer:command payload:payload];
|
|
|
|
}
|
2021-02-24 15:35:44 +08:00
|
|
|
|
|
|
|
- (void)addStatusCallback:(id)callback {
|
|
|
|
[self.callbacks addObject:callback];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeStatusCallback:(id)callback {
|
|
|
|
[self.callbacks removeObject:callback];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)ip {
|
|
|
|
return [[self.url stringByReplacingOccurrencesOfString:@"ws://"
|
|
|
|
withString:@""]
|
|
|
|
stringByReplacingOccurrencesOfString:@":7777"
|
|
|
|
withString:@""];
|
|
|
|
}
|
2021-06-22 16:31:33 +08:00
|
|
|
|
2021-07-20 11:29:53 +08:00
|
|
|
UIViewController *_Nonnull findBestViewController(UIViewController *_Nonnull vc) {
|
2021-06-22 16:31:33 +08:00
|
|
|
if (vc.presentedViewController && ![vc.presentedViewController isKindOfClass:[UIAlertController class]]) {
|
|
|
|
// Return presented view controller
|
|
|
|
return findBestViewController(vc.presentedViewController);
|
|
|
|
} else if ([vc isKindOfClass:[UISplitViewController class]]) {
|
2022-01-10 10:28:20 +08:00
|
|
|
// Return right-hand side
|
2021-07-20 11:29:53 +08:00
|
|
|
UISplitViewController *svc = (UISplitViewController *) vc;
|
2021-06-22 16:31:33 +08:00
|
|
|
if (svc.viewControllers.count > 0)
|
|
|
|
return findBestViewController(svc.viewControllers.lastObject);
|
|
|
|
else
|
|
|
|
return vc;
|
|
|
|
} else if ([vc isKindOfClass:[UINavigationController class]]) {
|
|
|
|
// Return top view
|
2021-07-20 11:29:53 +08:00
|
|
|
UINavigationController *svc = (UINavigationController *) vc;
|
2021-06-22 16:31:33 +08:00
|
|
|
if (svc.viewControllers.count > 0)
|
|
|
|
return findBestViewController(svc.topViewController);
|
|
|
|
else
|
|
|
|
return vc;
|
|
|
|
} else if ([vc isKindOfClass:[UITabBarController class]]) {
|
|
|
|
// Return visible view
|
2021-07-20 11:29:53 +08:00
|
|
|
UITabBarController *svc = (UITabBarController *) vc;
|
2021-06-22 16:31:33 +08:00
|
|
|
if (svc.viewControllers.count > 0)
|
|
|
|
return findBestViewController(svc.selectedViewController);
|
|
|
|
else
|
|
|
|
return vc;
|
2021-07-20 11:29:53 +08:00
|
|
|
} else {
|
2021-06-22 16:31:33 +08:00
|
|
|
// Unknown view controller type, return last child view controller
|
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:20:27 +08:00
|
|
|
@end
|