Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

CDVUIWebViewDelegate.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. //
  18. // Testing shows:
  19. //
  20. // In all cases, webView.request.URL is the previous page's URL (or empty) during the didStartLoad callback.
  21. // When loading a page with a redirect:
  22. // 1. shouldStartLoading (requestURL is target page)
  23. // 2. didStartLoading
  24. // 3. shouldStartLoading (requestURL is redirect target)
  25. // 4. didFinishLoad (request.URL is redirect target)
  26. //
  27. // Note the lack of a second didStartLoading **
  28. //
  29. // When loading a page with iframes:
  30. // 1. shouldStartLoading (requestURL is main page)
  31. // 2. didStartLoading
  32. // 3. shouldStartLoading (requestURL is one of the iframes)
  33. // 4. didStartLoading
  34. // 5. didFinishLoad
  35. // 6. didFinishLoad
  36. //
  37. // Note there is no way to distinguish which didFinishLoad maps to which didStartLoad **
  38. //
  39. // Loading a page by calling window.history.go(-1):
  40. // 1. didStartLoading
  41. // 2. didFinishLoad
  42. //
  43. // Note the lack of a shouldStartLoading call **
  44. // Actually - this is fixed on iOS6. iOS6 has a shouldStart. **
  45. //
  46. // Loading a page by calling location.reload()
  47. // 1. shouldStartLoading
  48. // 2. didStartLoading
  49. // 3. didFinishLoad
  50. //
  51. // Loading a page with an iframe that fails to load:
  52. // 1. shouldStart (main page)
  53. // 2. didStart
  54. // 3. shouldStart (iframe)
  55. // 4. didStart
  56. // 5. didFailWithError
  57. // 6. didFinish
  58. //
  59. // Loading a page with an iframe that fails to load due to an invalid URL:
  60. // 1. shouldStart (main page)
  61. // 2. didStart
  62. // 3. shouldStart (iframe)
  63. // 5. didFailWithError
  64. // 6. didFinish
  65. //
  66. // This case breaks our logic since there is a missing didStart. To prevent this,
  67. // we check URLs in shouldStart and return NO if they are invalid.
  68. //
  69. // Loading a page with an invalid URL
  70. // 1. shouldStart (main page)
  71. // 2. didFailWithError
  72. //
  73. // TODO: Record order when page is re-navigated before the first navigation finishes.
  74. //
  75. #import "CDVUIWebViewDelegate.h"
  76. // #define VerboseLog NSLog
  77. #define VerboseLog(...) do { \
  78. } while (0)
  79. typedef enum {
  80. STATE_IDLE = 0,
  81. STATE_WAITING_FOR_LOAD_START = 1,
  82. STATE_WAITING_FOR_LOAD_FINISH = 2,
  83. STATE_IOS5_POLLING_FOR_LOAD_START = 3,
  84. STATE_IOS5_POLLING_FOR_LOAD_FINISH = 4,
  85. STATE_CANCELLED = 5
  86. } State;
  87. static NSString *stripFragment(NSString* url)
  88. {
  89. NSRange r = [url rangeOfString:@"#"];
  90. if (r.location == NSNotFound) {
  91. return url;
  92. }
  93. return [url substringToIndex:r.location];
  94. }
  95. @implementation CDVUIWebViewDelegate
  96. - (id)initWithDelegate:(NSObject <UIWebViewDelegate>*)delegate
  97. {
  98. self = [super init];
  99. if (self != nil) {
  100. _delegate = delegate;
  101. _loadCount = -1;
  102. _state = STATE_IDLE;
  103. }
  104. return self;
  105. }
  106. - (BOOL)request:(NSURLRequest*)newRequest isEqualToRequestAfterStrippingFragments:(NSURLRequest*)originalRequest
  107. {
  108. if (originalRequest.URL && newRequest.URL) {
  109. NSString* originalRequestUrl = [originalRequest.URL absoluteString];
  110. NSString* newRequestUrl = [newRequest.URL absoluteString];
  111. NSString* baseOriginalRequestUrl = stripFragment(originalRequestUrl);
  112. NSString* baseNewRequestUrl = stripFragment(newRequestUrl);
  113. return [baseOriginalRequestUrl isEqualToString:baseNewRequestUrl];
  114. }
  115. return NO;
  116. }
  117. - (BOOL)isPageLoaded:(UIWebView*)webView
  118. {
  119. NSString* readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];
  120. return [readyState isEqualToString:@"loaded"] || [readyState isEqualToString:@"complete"];
  121. }
  122. - (BOOL)isJsLoadTokenSet:(UIWebView*)webView
  123. {
  124. NSString* loadToken = [webView stringByEvaluatingJavaScriptFromString:@"window.__cordovaLoadToken"];
  125. return [[NSString stringWithFormat:@"%ld", (long)_curLoadToken] isEqualToString:loadToken];
  126. }
  127. - (void)setLoadToken:(UIWebView*)webView
  128. {
  129. _curLoadToken += 1;
  130. [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.__cordovaLoadToken=%ld", (long)_curLoadToken]];
  131. }
  132. - (NSString*)evalForCurrentURL:(UIWebView*)webView
  133. {
  134. return [webView stringByEvaluatingJavaScriptFromString:@"location.href"];
  135. }
  136. - (void)pollForPageLoadStart:(UIWebView*)webView
  137. {
  138. if (_state != STATE_IOS5_POLLING_FOR_LOAD_START) {
  139. return;
  140. }
  141. if (![self isJsLoadTokenSet:webView]) {
  142. VerboseLog(@"Polled for page load start. result = YES!");
  143. _state = STATE_IOS5_POLLING_FOR_LOAD_FINISH;
  144. [self setLoadToken:webView];
  145. if ([_delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
  146. [_delegate webViewDidStartLoad:webView];
  147. }
  148. [self pollForPageLoadFinish:webView];
  149. } else {
  150. VerboseLog(@"Polled for page load start. result = NO");
  151. // Poll only for 1 second, and then fall back on checking only when delegate methods are called.
  152. ++_loadStartPollCount;
  153. if (_loadStartPollCount < (1000 * .05)) {
  154. [self performSelector:@selector(pollForPageLoadStart:) withObject:webView afterDelay:.05];
  155. }
  156. }
  157. }
  158. - (void)pollForPageLoadFinish:(UIWebView*)webView
  159. {
  160. if (_state != STATE_IOS5_POLLING_FOR_LOAD_FINISH) {
  161. return;
  162. }
  163. if ([self isPageLoaded:webView]) {
  164. VerboseLog(@"Polled for page load finish. result = YES!");
  165. _state = STATE_IDLE;
  166. if ([_delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
  167. [_delegate webViewDidFinishLoad:webView];
  168. }
  169. } else {
  170. VerboseLog(@"Polled for page load finish. result = NO");
  171. [self performSelector:@selector(pollForPageLoadFinish:) withObject:webView afterDelay:.05];
  172. }
  173. }
  174. - (BOOL)shouldLoadRequest:(NSURLRequest*)request
  175. {
  176. NSString* scheme = [[request URL] scheme];
  177. NSArray* allowedSchemes = [NSArray arrayWithObjects:@"mailto",@"tel",@"blob",@"sms",@"data", nil];
  178. if([allowedSchemes containsObject:scheme]) {
  179. return YES;
  180. }
  181. else {
  182. return [NSURLConnection canHandleRequest:request];
  183. }
  184. }
  185. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
  186. {
  187. BOOL shouldLoad = YES;
  188. if ([_delegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
  189. shouldLoad = [_delegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
  190. }
  191. VerboseLog(@"webView shouldLoad=%d (before) state=%d loadCount=%d URL=%@", shouldLoad, _state, _loadCount, request.URL);
  192. if (shouldLoad) {
  193. // When devtools refresh occurs, it blindly uses the same request object. If a history.replaceState() has occured, then
  194. // mainDocumentURL != URL even though it's a top-level navigation.
  195. BOOL isDevToolsRefresh = (request == webView.request);
  196. BOOL isTopLevelNavigation = isDevToolsRefresh || [request.URL isEqual:[request mainDocumentURL]];
  197. if (isTopLevelNavigation) {
  198. // Ignore hash changes that don't navigate to a different page.
  199. // webView.request does actually update when history.replaceState() gets called.
  200. if ([self request:request isEqualToRequestAfterStrippingFragments:webView.request]) {
  201. NSString* prevURL = [self evalForCurrentURL:webView];
  202. if ([prevURL isEqualToString:[request.URL absoluteString]]) {
  203. VerboseLog(@"Page reload detected.");
  204. } else {
  205. VerboseLog(@"Detected hash change shouldLoad");
  206. return shouldLoad;
  207. }
  208. }
  209. switch (_state) {
  210. case STATE_WAITING_FOR_LOAD_FINISH:
  211. // Redirect case.
  212. // We expect loadCount == 1.
  213. if (_loadCount != 1) {
  214. NSLog(@"CDVWebViewDelegate: Detected redirect when loadCount=%ld", (long)_loadCount);
  215. }
  216. break;
  217. case STATE_IDLE:
  218. case STATE_IOS5_POLLING_FOR_LOAD_START:
  219. case STATE_CANCELLED:
  220. // Page navigation start.
  221. _loadCount = 0;
  222. _state = STATE_WAITING_FOR_LOAD_START;
  223. break;
  224. default:
  225. {
  226. NSString* description = [NSString stringWithFormat:@"CDVWebViewDelegate: Navigation started when state=%ld", (long)_state];
  227. NSLog(@"%@", description);
  228. _loadCount = 0;
  229. _state = STATE_WAITING_FOR_LOAD_START;
  230. NSDictionary* errorDictionary = @{NSLocalizedDescriptionKey : description};
  231. NSError* error = [[NSError alloc] initWithDomain:@"CDVUIWebViewDelegate" code:1 userInfo:errorDictionary];
  232. [self webView:webView didFailLoadWithError:error];
  233. }
  234. }
  235. } else {
  236. // Deny invalid URLs so that we don't get the case where we go straight from
  237. // webViewShouldLoad -> webViewDidFailLoad (messes up _loadCount).
  238. shouldLoad = shouldLoad && [self shouldLoadRequest:request];
  239. }
  240. VerboseLog(@"webView shouldLoad=%d (after) isTopLevelNavigation=%d state=%d loadCount=%d", shouldLoad, isTopLevelNavigation, _state, _loadCount);
  241. }
  242. return shouldLoad;
  243. }
  244. - (void)webViewDidStartLoad:(UIWebView*)webView
  245. {
  246. VerboseLog(@"webView didStartLoad (before). state=%d loadCount=%d", _state, _loadCount);
  247. BOOL fireCallback = NO;
  248. switch (_state) {
  249. case STATE_IDLE:
  250. break;
  251. case STATE_CANCELLED:
  252. fireCallback = YES;
  253. _state = STATE_WAITING_FOR_LOAD_FINISH;
  254. _loadCount += 1;
  255. break;
  256. case STATE_WAITING_FOR_LOAD_START:
  257. if (_loadCount != 0) {
  258. NSLog(@"CDVWebViewDelegate: Unexpected loadCount in didStart. count=%ld", (long)_loadCount);
  259. }
  260. fireCallback = YES;
  261. _state = STATE_WAITING_FOR_LOAD_FINISH;
  262. _loadCount = 1;
  263. break;
  264. case STATE_WAITING_FOR_LOAD_FINISH:
  265. _loadCount += 1;
  266. break;
  267. case STATE_IOS5_POLLING_FOR_LOAD_START:
  268. [self pollForPageLoadStart:webView];
  269. break;
  270. case STATE_IOS5_POLLING_FOR_LOAD_FINISH:
  271. [self pollForPageLoadFinish:webView];
  272. break;
  273. default:
  274. NSLog(@"CDVWebViewDelegate: Unexpected didStart with state=%ld loadCount=%ld", (long)_state, (long)_loadCount);
  275. }
  276. VerboseLog(@"webView didStartLoad (after). state=%d loadCount=%d fireCallback=%d", _state, _loadCount, fireCallback);
  277. if (fireCallback && [_delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
  278. [_delegate webViewDidStartLoad:webView];
  279. }
  280. }
  281. - (void)webViewDidFinishLoad:(UIWebView*)webView
  282. {
  283. VerboseLog(@"webView didFinishLoad (before). state=%d loadCount=%d", _state, _loadCount);
  284. BOOL fireCallback = NO;
  285. switch (_state) {
  286. case STATE_IDLE:
  287. break;
  288. case STATE_WAITING_FOR_LOAD_START:
  289. NSLog(@"CDVWebViewDelegate: Unexpected didFinish while waiting for load start.");
  290. break;
  291. case STATE_WAITING_FOR_LOAD_FINISH:
  292. // fix call loadRequest multiple times just callback webViewDidFinishLoad once time in iOS 12
  293. if (@available(iOS 12.0, *)) {
  294. fireCallback = YES;
  295. } else {
  296. if (_loadCount == 1) {
  297. fireCallback = YES;
  298. _state = STATE_IDLE;
  299. }
  300. }
  301. _loadCount -= 1;
  302. break;
  303. case STATE_IOS5_POLLING_FOR_LOAD_START:
  304. [self pollForPageLoadStart:webView];
  305. break;
  306. case STATE_IOS5_POLLING_FOR_LOAD_FINISH:
  307. [self pollForPageLoadFinish:webView];
  308. break;
  309. }
  310. VerboseLog(@"webView didFinishLoad (after). state=%d loadCount=%d fireCallback=%d", _state, _loadCount, fireCallback);
  311. if (fireCallback && [_delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
  312. [_delegate webViewDidFinishLoad:webView];
  313. }
  314. }
  315. - (void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
  316. {
  317. VerboseLog(@"webView didFailLoad (before). state=%d loadCount=%d", _state, _loadCount);
  318. BOOL fireCallback = NO;
  319. switch (_state) {
  320. case STATE_IDLE:
  321. break;
  322. case STATE_WAITING_FOR_LOAD_START:
  323. if ([error code] == NSURLErrorCancelled) {
  324. _state = STATE_CANCELLED;
  325. } else {
  326. _state = STATE_IDLE;
  327. }
  328. fireCallback = YES;
  329. break;
  330. case STATE_WAITING_FOR_LOAD_FINISH:
  331. if ([error code] != NSURLErrorCancelled) {
  332. if (_loadCount == 1) {
  333. _state = STATE_IDLE;
  334. fireCallback = YES;
  335. }
  336. _loadCount = -1;
  337. } else {
  338. fireCallback = YES;
  339. _state = STATE_CANCELLED;
  340. _loadCount -= 1;
  341. }
  342. break;
  343. case STATE_IOS5_POLLING_FOR_LOAD_START:
  344. [self pollForPageLoadStart:webView];
  345. break;
  346. case STATE_IOS5_POLLING_FOR_LOAD_FINISH:
  347. [self pollForPageLoadFinish:webView];
  348. break;
  349. }
  350. VerboseLog(@"webView didFailLoad (after). state=%d loadCount=%d, fireCallback=%d", _state, _loadCount, fireCallback);
  351. if (fireCallback && [_delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
  352. [_delegate webView:webView didFailLoadWithError:error];
  353. }
  354. }
  355. @end