暫無描述

WebViewJavascriptBridgeBase.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // WebViewJavascriptBridgeBase.m
  3. //
  4. // Created by @LokiMeyburg on 10/15/14.
  5. // Copyright (c) 2014 @LokiMeyburg. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import "WebViewJavascriptBridgeBase.h"
  9. #import "WebViewJavascriptBridge_JS.h"
  10. @implementation WebViewJavascriptBridgeBase {
  11. __weak id _webViewDelegate;
  12. long _uniqueId;
  13. }
  14. static bool logging = false;
  15. static int logMaxLength = 500;
  16. + (void)enableLogging { logging = true; }
  17. + (void)setLogMaxLength:(int)length { logMaxLength = length;}
  18. - (id)init {
  19. if (self = [super init]) {
  20. self.messageHandlers = [NSMutableDictionary dictionary];
  21. self.startupMessageQueue = [NSMutableArray array];
  22. self.responseCallbacks = [NSMutableDictionary dictionary];
  23. _uniqueId = 0;
  24. }
  25. return self;
  26. }
  27. - (void)dealloc {
  28. self.startupMessageQueue = nil;
  29. self.responseCallbacks = nil;
  30. self.messageHandlers = nil;
  31. }
  32. - (void)reset {
  33. self.startupMessageQueue = [NSMutableArray array];
  34. self.responseCallbacks = [NSMutableDictionary dictionary];
  35. _uniqueId = 0;
  36. }
  37. - (void)sendData:(id)data responseCallback:(WVJBResponseCallback)responseCallback handlerName:(NSString*)handlerName {
  38. NSMutableDictionary* message = [NSMutableDictionary dictionary];
  39. if (data) {
  40. message[@"data"] = data;
  41. }
  42. if (responseCallback) {
  43. NSString* callbackId = [NSString stringWithFormat:@"objc_cb_%ld", ++_uniqueId];
  44. self.responseCallbacks[callbackId] = [responseCallback copy];
  45. message[@"callbackId"] = callbackId;
  46. }
  47. if (handlerName) {
  48. message[@"handlerName"] = handlerName;
  49. }
  50. [self _queueMessage:message];
  51. }
  52. - (void)flushMessageQueue:(NSString *)messageQueueString{
  53. if (messageQueueString == nil || messageQueueString.length == 0) {
  54. NSLog(@"WebViewJavascriptBridge: WARNING: ObjC got nil while fetching the message queue JSON from webview. This can happen if the WebViewJavascriptBridge JS is not currently present in the webview, e.g if the webview just loaded a new page.");
  55. return;
  56. }
  57. id messages = [self _deserializeMessageJSON:messageQueueString];
  58. for (WVJBMessage* message in messages) {
  59. if (![message isKindOfClass:[WVJBMessage class]]) {
  60. NSLog(@"WebViewJavascriptBridge: WARNING: Invalid %@ received: %@", [message class], message);
  61. continue;
  62. }
  63. [self _log:@"RCVD" json:message];
  64. NSString* responseId = message[@"responseId"];
  65. if (responseId) {
  66. WVJBResponseCallback responseCallback = _responseCallbacks[responseId];
  67. responseCallback(message[@"responseData"]);
  68. [self.responseCallbacks removeObjectForKey:responseId];
  69. } else {
  70. WVJBResponseCallback responseCallback = NULL;
  71. NSString* callbackId = message[@"callbackId"];
  72. if (callbackId) {
  73. responseCallback = ^(id responseData) {
  74. if (responseData == nil) {
  75. responseData = [NSNull null];
  76. }
  77. WVJBMessage* msg = @{ @"responseId":callbackId, @"responseData":responseData };
  78. [self _queueMessage:msg];
  79. };
  80. } else {
  81. responseCallback = ^(id ignoreResponseData) {
  82. // Do nothing
  83. };
  84. }
  85. WVJBHandler handler = self.messageHandlers[message[@"handlerName"]];
  86. if (!handler) {
  87. NSLog(@"WVJBNoHandlerException, No handler for message from JS: %@", message);
  88. continue;
  89. }
  90. handler(message[@"data"], responseCallback);
  91. }
  92. }
  93. }
  94. - (void)injectJavascriptFile {
  95. NSString *js = WebViewJavascriptBridge_js();
  96. [self _evaluateJavascript:js];
  97. if (self.startupMessageQueue) {
  98. NSArray* queue = self.startupMessageQueue;
  99. self.startupMessageQueue = nil;
  100. for (id queuedMessage in queue) {
  101. [self _dispatchMessage:queuedMessage];
  102. }
  103. }
  104. }
  105. - (BOOL)isWebViewJavascriptBridgeURL:(NSURL*)url {
  106. if (![self isSchemeMatch:url]) {
  107. return NO;
  108. }
  109. return [self isBridgeLoadedURL:url] || [self isQueueMessageURL:url];
  110. }
  111. - (BOOL)isSchemeMatch:(NSURL*)url {
  112. NSString* scheme = url.scheme.lowercaseString;
  113. return [scheme isEqualToString:kNewProtocolScheme] || [scheme isEqualToString:kOldProtocolScheme];
  114. }
  115. - (BOOL)isQueueMessageURL:(NSURL*)url {
  116. NSString* host = url.host.lowercaseString;
  117. return [self isSchemeMatch:url] && [host isEqualToString:kQueueHasMessage];
  118. }
  119. - (BOOL)isBridgeLoadedURL:(NSURL*)url {
  120. NSString* host = url.host.lowercaseString;
  121. return [self isSchemeMatch:url] && [host isEqualToString:kBridgeLoaded];
  122. }
  123. - (void)logUnkownMessage:(NSURL*)url {
  124. NSLog(@"WebViewJavascriptBridge: WARNING: Received unknown WebViewJavascriptBridge command %@", [url absoluteString]);
  125. }
  126. - (NSString *)webViewJavascriptCheckCommand {
  127. return @"typeof WebViewJavascriptBridge == \'object\';";
  128. }
  129. - (NSString *)webViewJavascriptFetchQueyCommand {
  130. return @"WebViewJavascriptBridge._fetchQueue();";
  131. }
  132. - (void)disableJavscriptAlertBoxSafetyTimeout {
  133. [self sendData:nil responseCallback:nil handlerName:@"_disableJavascriptAlertBoxSafetyTimeout"];
  134. }
  135. // Private
  136. // -------------------------------------------
  137. - (void) _evaluateJavascript:(NSString *)javascriptCommand {
  138. [self.delegate _evaluateJavascript:javascriptCommand];
  139. }
  140. - (void)_queueMessage:(WVJBMessage*)message {
  141. if (self.startupMessageQueue) {
  142. [self.startupMessageQueue addObject:message];
  143. } else {
  144. [self _dispatchMessage:message];
  145. }
  146. }
  147. - (void)_dispatchMessage:(WVJBMessage*)message {
  148. NSString *messageJSON = [self _serializeMessage:message pretty:NO];
  149. [self _log:@"SEND" json:messageJSON];
  150. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];
  151. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
  152. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
  153. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
  154. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\r" withString:@"\\r"];
  155. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\f" withString:@"\\f"];
  156. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2028" withString:@"\\u2028"];
  157. messageJSON = [messageJSON stringByReplacingOccurrencesOfString:@"\u2029" withString:@"\\u2029"];
  158. NSString* javascriptCommand = [NSString stringWithFormat:@"WebViewJavascriptBridge._handleMessageFromObjC('%@');", messageJSON];
  159. if ([[NSThread currentThread] isMainThread]) {
  160. [self _evaluateJavascript:javascriptCommand];
  161. } else {
  162. dispatch_sync(dispatch_get_main_queue(), ^{
  163. [self _evaluateJavascript:javascriptCommand];
  164. });
  165. }
  166. }
  167. - (NSString *)_serializeMessage:(id)message pretty:(BOOL)pretty{
  168. return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:message options:(NSJSONWritingOptions)(pretty ? NSJSONWritingPrettyPrinted : 0) error:nil] encoding:NSUTF8StringEncoding];
  169. }
  170. - (NSArray*)_deserializeMessageJSON:(NSString *)messageJSON {
  171. return [NSJSONSerialization JSONObjectWithData:[messageJSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
  172. }
  173. - (void)_log:(NSString *)action json:(id)json {
  174. if (!logging) { return; }
  175. if (![json isKindOfClass:[NSString class]]) {
  176. json = [self _serializeMessage:json pretty:YES];
  177. }
  178. if ([json length] > logMaxLength) {
  179. NSLog(@"WVJB %@: %@ [...]", action, [json substringToIndex:logMaxLength]);
  180. } else {
  181. NSLog(@"WVJB %@: %@", action, json);
  182. }
  183. }
  184. @end