forked from marcuswestin/WebViewJavascriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebViewJavascriptBridgeAbstract.m
More file actions
executable file
·171 lines (142 loc) · 6.5 KB
/
WebViewJavascriptBridgeAbstract.m
File metadata and controls
executable file
·171 lines (142 loc) · 6.5 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
#import "WebViewJavascriptBridgeAbstract.h"
#ifdef USE_JSONKIT
#import "JSONKit.h"
#endif
@interface WebViewJavascriptBridgeAbstract ()
@end
@implementation WebViewJavascriptBridgeAbstract
static bool logging = false;
+ (void)enableLogging { logging = true; }
- (void)send:(NSDictionary *)data {
[self send:data responseCallback:nil];
}
- (void)send:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback {
[self _sendData:data responseCallback:responseCallback handlerName:nil];
}
- (void)callHandler:(NSString *)handlerName {
[self callHandler:handlerName data:nil responseCallback:nil];
}
- (void)callHandler:(NSString *)handlerName data:(id)data {
[self callHandler:handlerName data:data responseCallback:nil];
}
- (void)callHandler:(NSString *)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback {
[self _sendData:data responseCallback:responseCallback handlerName:handlerName];
}
- (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler {
self.messageHandlers[handlerName] = [handler copy];
}
- (void)reset {
self.startupMessageQueue = [NSMutableArray array];
self.responseCallbacks = [NSMutableDictionary dictionary];
self.uniqueId = 0;
}
@end
@implementation WebViewJavascriptBridgeAbstract (Protected)
- (void)_sendData:(NSDictionary *)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName {
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithObject:data forKey:@"data"];
if (responseCallback) {
NSString* callbackId = [NSString stringWithFormat:@"objc_cb_%ld", ++self.uniqueId];
self.responseCallbacks[callbackId] = [responseCallback copy];
message[@"callbackId"] = callbackId;
}
if (handlerName) {
message[@"handlerName"] = handlerName;
}
[self _queueMessage:message];
}
- (void)_queueMessage:(NSDictionary *)message {
if (self.startupMessageQueue) {
[self.startupMessageQueue addObject:message];
} else {
[self _dispatchMessage:message];
}
}
- (void)_dispatchMessage:(NSDictionary *)message {
NSString *messageJSON = [self _serializeMessage:message];
[self _log:@"sending" json:messageJSON];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
__strong typeof(self.webView) strongWebView = self.webView;
if ([[NSThread currentThread] isMainThread]) {
[strongWebView performSelector:@selector(stringByEvaluatingJavaScriptFromString:)
withObject:[NSString stringWithFormat:
@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
[strongWebView performSelector:@selector(stringByEvaluatingJavaScriptFromString:)
withObject:[NSString stringWithFormat:
@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON]];
});
}
}
- (void)_flushMessageQueue {
__strong typeof(self.webView) strongWebView = self.webView;
NSString *messageQueueString = [strongWebView performSelector:
@selector(stringByEvaluatingJavaScriptFromString:) withObject:@"WebViewJavascriptBridge._fetchQueue();"];
NSArray* messages = [messageQueueString componentsSeparatedByString:kMessageSeparator];
for (NSString *messageJSON in messages) {
[self _log:@"receivd" json:messageJSON];
NSDictionary* message = [self _deserializeMessageJSON:messageJSON];
NSString* responseId = message[@"responseId"];
if (responseId) {
WVJBResponseCallback responseCallback = self.responseCallbacks[responseId];
responseCallback(message[@"responseData"]);
[self.responseCallbacks removeObjectForKey:responseId];
} else {
WVJBResponseCallback responseCallback = NULL;
__block NSString* callbackId = message[@"callbackId"];
if (callbackId) {
responseCallback = ^(id responseData) {
NSDictionary* msg = @{ @"responseId":callbackId, @"responseData":responseData };
[self _queueMessage:msg];
};
} else {
responseCallback = ^(id ignoreResponseData) {
// Do nothing
};
}
WVJBHandler handler;
if (message[@"handlerName"]) {
handler = self.messageHandlers[message[@"handlerName"]];
if (!handler) { return NSLog(@"WVJB Warning: No handler for %@", message[@"handlerName"]); }
} else {
handler = self.messageHandler;
}
@try {
NSDictionary* data = message[@"data"];
if (!data || ((id)data) == [NSNull null]) { data = [NSDictionary dictionary]; }
handler(data, responseCallback);
}
@catch (NSException *exception) {
NSLog(@"WebViewJavascriptBridge: WARNING: objc handler threw. %@ %@", message, exception);
}
}
}
}
- (NSString *)_serializeMessage:(NSDictionary *)message {
#ifdef USE_JSONKIT
return [message JSONString];
#else
return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:0 error:nil] encoding:NSUTF8StringEncoding];
#endif
}
- (NSDictionary *)_deserializeMessageJSON:(NSString *)messageJSON {
#ifdef USE_JSONKIT
return [messageJSON objectFromJSONString];
#else
return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
#endif
}
- (void)_log:(NSString *)action json:(NSString *)json {
if (!logging) { return; }
if (json.length > 500) {
NSLog(@"WVJB %@: %@", action, [[json substringToIndex:500] stringByAppendingString:@" [...]"]);
} else {
NSLog(@"WVJB %@: %@", action, json);
}
}
@end