Repository navigation
Expand file tree
/
Copy pathSHRMWebViewHandleFactory.m
More file actions
executable file
·79 lines (65 loc) · 2.33 KB
/
Copy pathSHRMWebViewHandleFactory.m
File metadata and controls
executable file
·79 lines (65 loc) · 2.33 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
//
// SHRMWebViewHandleFactory.m
// Hybrid-framework
//
// Created by 王凯 on 2018/12/7.
// Copyright © 2018 王凯. All rights reserved.
//
#import "SHRMWebViewHandleFactory.h"
#import "SHRMMsgCommand.h"
#import "SHRMWebViewEngine.h"
#import <objc/message.h>
@implementation SHRMWebViewHandleFactory {
__weak SHRMWebViewEngine *_webViewEngine;
}
- (instancetype)initWithWebViewEngine:(SHRMWebViewEngine *)webViewEngine {
if (self = [super init]) {
_webViewEngine = webViewEngine;
}
return self;
}
- (void)handleMsgCommand:(NSArray *)arguments {
[self handleCommand:arguments];
}
#pragma mark - handle
- (void)handleCommand:(NSArray *)commandArray {
if (commandArray.count > 0) {
[self executePending:commandArray];
}
}
- (void)executePending:(NSArray *)command {
SHRMMsgCommand *msgCommand = [SHRMMsgCommand commandFromJson:command];
if (![self execute:msgCommand]) {
NSLog(@"%@.%@ execute fail",msgCommand.className, msgCommand.methodName);
}
}
- (BOOL)execute:(SHRMMsgCommand *)command {
if (command.className == nil || command.methodName == nil) {
NSLog(@"ERROR: Classname and/or methodName not found for command.");
return NO;
}
BOOL retVal = YES;
double started = [[NSDate date] timeIntervalSince1970] * 1000.0;
SHRMBasePlugin *instance = [_webViewEngine.commandDelegate getCommandInstance:command.className];
if (!([instance isKindOfClass:[SHRMBasePlugin class]])) {
NSLog(@"ERROR: Plugin '%@' not found, or is not a Plugin. Check your plugin mapping in config.xml.", command.className);
return NO;
}
NSString* methodName = [NSString stringWithFormat:@"%@:", command.methodName];
SEL normalSelector = NSSelectorFromString(methodName);
if ([instance respondsToSelector:normalSelector]) {
((void (*)(id, SEL, id))objc_msgSend)(instance, normalSelector, command);
} else {
NSLog(@"ERROR: Method '%@' not defined in Plugin '%@'", methodName, command.className);
retVal = NO;
}
double elapsed = [[NSDate date] timeIntervalSince1970] * 1000.0 - started;
if (elapsed > 10) {
NSLog(@"THREAD WARNING: ['%@'] took '%f' ms. Plugin should use a background thread.", command.className, elapsed);
}
return retVal;
}
- (void)dealloc {
_webViewEngine = nil;
}
@end