Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CDVCommandDelegateImpl.m 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. #import "CDVCommandDelegateImpl.h"
  18. #import "CDVJSON_private.h"
  19. #import "CDVCommandQueue.h"
  20. #import "CDVPluginResult.h"
  21. #import "CDVViewController.h"
  22. @implementation CDVCommandDelegateImpl
  23. @synthesize urlTransformer;
  24. - (id)initWithViewController:(CDVViewController*)viewController
  25. {
  26. self = [super init];
  27. if (self != nil) {
  28. _viewController = viewController;
  29. _commandQueue = _viewController.commandQueue;
  30. NSError* err = nil;
  31. _callbackIdPattern = [NSRegularExpression regularExpressionWithPattern:@"[^A-Za-z0-9._-]" options:0 error:&err];
  32. if (err != nil) {
  33. // Couldn't initialize Regex
  34. NSLog(@"Error: Couldn't initialize regex");
  35. _callbackIdPattern = nil;
  36. }
  37. }
  38. return self;
  39. }
  40. - (NSString*)pathForResource:(NSString*)resourcepath
  41. {
  42. NSBundle* mainBundle = [NSBundle mainBundle];
  43. NSMutableArray* directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
  44. NSString* filename = [directoryParts lastObject];
  45. [directoryParts removeLastObject];
  46. NSString* directoryPartsJoined = [directoryParts componentsJoinedByString:@"/"];
  47. NSString* directoryStr = _viewController.wwwFolderName;
  48. if ([directoryPartsJoined length] > 0) {
  49. directoryStr = [NSString stringWithFormat:@"%@/%@", _viewController.wwwFolderName, [directoryParts componentsJoinedByString:@"/"]];
  50. }
  51. return [mainBundle pathForResource:filename ofType:@"" inDirectory:directoryStr];
  52. }
  53. - (void)flushCommandQueueWithDelayedJs
  54. {
  55. _delayResponses = YES;
  56. [_commandQueue executePending];
  57. _delayResponses = NO;
  58. }
  59. - (void)evalJsHelper2:(NSString*)js
  60. {
  61. CDV_EXEC_LOG(@"Exec: evalling: %@", [js substringToIndex:MIN([js length], 160)]);
  62. [_viewController.webViewEngine evaluateJavaScript:js completionHandler:^(id obj, NSError* error) {
  63. // TODO: obj can be something other than string
  64. if ([obj isKindOfClass:[NSString class]]) {
  65. NSString* commandsJSON = (NSString*)obj;
  66. if ([commandsJSON length] > 0) {
  67. CDV_EXEC_LOG(@"Exec: Retrieved new exec messages by chaining.");
  68. }
  69. [self->_commandQueue enqueueCommandBatch:commandsJSON];
  70. [self->_commandQueue executePending];
  71. }
  72. }];
  73. }
  74. - (void)evalJsHelper:(NSString*)js
  75. {
  76. // Cycle the run-loop before executing the JS.
  77. // For _delayResponses -
  78. // This ensures that we don't eval JS during the middle of an existing JS
  79. // function (possible since WKWebViewDelegate callbacks can be synchronous).
  80. // For !isMainThread -
  81. // It's a hard error to eval on the non-UI thread.
  82. // For !_commandQueue.currentlyExecuting -
  83. // This works around a bug where sometimes alerts() within callbacks can cause
  84. // dead-lock.
  85. // If the commandQueue is currently executing, then we know that it is safe to
  86. // execute the callback immediately.
  87. // Using (dispatch_get_main_queue()) does *not* fix deadlocks for some reason,
  88. // but performSelectorOnMainThread: does.
  89. if (_delayResponses || ![NSThread isMainThread] || !_commandQueue.currentlyExecuting) {
  90. [self performSelectorOnMainThread:@selector(evalJsHelper2:) withObject:js waitUntilDone:NO];
  91. } else {
  92. [self evalJsHelper2:js];
  93. }
  94. }
  95. - (BOOL)isValidCallbackId:(NSString*)callbackId
  96. {
  97. if ((callbackId == nil) || (_callbackIdPattern == nil)) {
  98. return NO;
  99. }
  100. // Disallow if too long or if any invalid characters were found.
  101. if (([callbackId length] > 100) || [_callbackIdPattern firstMatchInString:callbackId options:0 range:NSMakeRange(0, [callbackId length])]) {
  102. return NO;
  103. }
  104. return YES;
  105. }
  106. - (void)sendPluginResult:(CDVPluginResult*)result callbackId:(NSString*)callbackId
  107. {
  108. CDV_EXEC_LOG(@"Exec(%@): Sending result. Status=%@", callbackId, result.status);
  109. // This occurs when there is are no win/fail callbacks for the call.
  110. if ([@"INVALID" isEqualToString:callbackId]) {
  111. return;
  112. }
  113. // This occurs when the callback id is malformed.
  114. if (![self isValidCallbackId:callbackId]) {
  115. NSLog(@"Invalid callback id received by sendPluginResult");
  116. return;
  117. }
  118. int status = [result.status intValue];
  119. BOOL keepCallback = [result.keepCallback boolValue];
  120. NSString* argumentsAsJSON = [result argumentsAsJSON];
  121. BOOL debug = NO;
  122. #ifdef DEBUG
  123. debug = YES;
  124. #endif
  125. NSString* js = [NSString stringWithFormat:@"cordova.require('cordova/exec').nativeCallback('%@',%d,%@,%d, %d)", callbackId, status, argumentsAsJSON, keepCallback, debug];
  126. [self evalJsHelper:js];
  127. }
  128. - (void)evalJs:(NSString*)js
  129. {
  130. [self evalJs:js scheduledOnRunLoop:YES];
  131. }
  132. - (void)evalJs:(NSString*)js scheduledOnRunLoop:(BOOL)scheduledOnRunLoop
  133. {
  134. js = [NSString stringWithFormat:@"try{cordova.require('cordova/exec').nativeEvalAndFetch(function(){%@})}catch(e){console.log('exception nativeEvalAndFetch : '+e);};", js];
  135. if (scheduledOnRunLoop) {
  136. [self evalJsHelper:js];
  137. } else {
  138. [self evalJsHelper2:js];
  139. }
  140. }
  141. - (id)getCommandInstance:(NSString*)pluginName
  142. {
  143. return [_viewController getCommandInstance:pluginName];
  144. }
  145. - (void)runInBackground:(void (^)(void))block
  146. {
  147. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
  148. }
  149. - (NSDictionary*)settings
  150. {
  151. return _viewController.settings;
  152. }
  153. @end