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

CDVIntentAndNavigationFilter.m 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "CDVIntentAndNavigationFilter.h"
  18. #import <Cordova/CDV.h>
  19. @interface CDVIntentAndNavigationFilter ()
  20. @property (nonatomic, readwrite) NSMutableArray* allowIntents;
  21. @property (nonatomic, readwrite) NSMutableArray* allowNavigations;
  22. @property (nonatomic, readwrite) CDVWhitelist* allowIntentsWhitelist;
  23. @property (nonatomic, readwrite) CDVWhitelist* allowNavigationsWhitelist;
  24. @end
  25. @implementation CDVIntentAndNavigationFilter
  26. #pragma mark NSXMLParserDelegate
  27. - (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict
  28. {
  29. if ([elementName isEqualToString:@"allow-navigation"]) {
  30. [self.allowNavigations addObject:attributeDict[@"href"]];
  31. }
  32. if ([elementName isEqualToString:@"allow-intent"]) {
  33. [self.allowIntents addObject:attributeDict[@"href"]];
  34. }
  35. }
  36. - (void)parserDidStartDocument:(NSXMLParser*)parser
  37. {
  38. // file: url <allow-navigations> are added by default
  39. self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://" ]];
  40. // no intents are added by default
  41. self.allowIntents = [[NSMutableArray alloc] init];
  42. }
  43. - (void)parserDidEndDocument:(NSXMLParser*)parser
  44. {
  45. self.allowIntentsWhitelist = [[CDVWhitelist alloc] initWithArray:self.allowIntents];
  46. self.allowNavigationsWhitelist = [[CDVWhitelist alloc] initWithArray:self.allowNavigations];
  47. }
  48. - (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError
  49. {
  50. NSAssert(NO, @"config.xml parse error line %ld col %ld", (long)[parser lineNumber], (long)[parser columnNumber]);
  51. }
  52. #pragma mark CDVPlugin
  53. - (void)pluginInitialize
  54. {
  55. if ([self.viewController isKindOfClass:[CDVViewController class]]) {
  56. [(CDVViewController*)self.viewController parseSettingsWithParser:self];
  57. }
  58. }
  59. + (CDVIntentAndNavigationFilterValue) filterUrl:(NSURL*)url intentsWhitelist:(CDVWhitelist*)intentsWhitelist navigationsWhitelist:(CDVWhitelist*)navigationsWhitelist
  60. {
  61. // a URL can only allow-intent OR allow-navigation, if both are specified,
  62. // only allow-navigation is allowed
  63. BOOL allowNavigationsPass = [navigationsWhitelist URLIsAllowed:url logFailure:NO];
  64. BOOL allowIntentPass = [intentsWhitelist URLIsAllowed:url logFailure:NO];
  65. if (allowNavigationsPass && allowIntentPass) {
  66. return CDVIntentAndNavigationFilterValueNavigationAllowed;
  67. } else if (allowNavigationsPass) {
  68. return CDVIntentAndNavigationFilterValueNavigationAllowed;
  69. } else if (allowIntentPass) {
  70. return CDVIntentAndNavigationFilterValueIntentAllowed;
  71. }
  72. return CDVIntentAndNavigationFilterValueNoneAllowed;
  73. }
  74. - (CDVIntentAndNavigationFilterValue) filterUrl:(NSURL*)url
  75. {
  76. return [[self class] filterUrl:url intentsWhitelist:self.allowIntentsWhitelist navigationsWhitelist:self.allowNavigationsWhitelist];
  77. }
  78. #if WK_WEB_VIEW_ONLY
  79. #define CDVWebViewNavigationTypeLinkClicked 0
  80. #define CDVWebViewNavigationTypeOther 5
  81. #else
  82. #define CDVWebViewNavigationTypeLinkClicked UIWebViewNavigationTypeLinkClicked
  83. #define CDVWebViewNavigationTypeOther UIWebViewNavigationTypeOther
  84. #endif
  85. + (BOOL)shouldOpenURLRequest:(NSURLRequest*)request navigationType:(CDVWebViewNavigationType)navigationType
  86. {
  87. return (CDVWebViewNavigationTypeLinkClicked == navigationType ||
  88. (CDVWebViewNavigationTypeOther == navigationType &&
  89. [[request.mainDocumentURL absoluteString] isEqualToString:[request.URL absoluteString]]
  90. )
  91. );
  92. }
  93. + (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(CDVWebViewNavigationType)navigationType filterValue:(CDVIntentAndNavigationFilterValue)filterValue
  94. {
  95. NSString* allowIntents_whitelistRejectionFormatString = @"ERROR External navigation rejected - <allow-intent> not set for url='%@'";
  96. NSString* allowNavigations_whitelistRejectionFormatString = @"ERROR Internal navigation rejected - <allow-navigation> not set for url='%@'";
  97. NSURL* url = [request URL];
  98. switch (filterValue) {
  99. case CDVIntentAndNavigationFilterValueNavigationAllowed:
  100. return YES;
  101. case CDVIntentAndNavigationFilterValueIntentAllowed:
  102. // only allow-intent if it's a UIWebViewNavigationTypeLinkClicked (anchor tag) OR
  103. // it's a UIWebViewNavigationTypeOther, and it's an internal link
  104. if ([[self class] shouldOpenURLRequest:request navigationType:navigationType]){
  105. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  106. }
  107. // consume the request (i.e. no error) if it wasn't handled above
  108. return NO;
  109. case CDVIntentAndNavigationFilterValueNoneAllowed:
  110. // allow-navigation attempt failed for sure
  111. NSLog(@"%@", [NSString stringWithFormat:allowNavigations_whitelistRejectionFormatString, [url absoluteString]]);
  112. // anchor tag link means it was an allow-intent attempt that failed as well
  113. if (CDVWebViewNavigationTypeLinkClicked == navigationType) {
  114. NSLog(@"%@", [NSString stringWithFormat:allowIntents_whitelistRejectionFormatString, [url absoluteString]]);
  115. }
  116. return NO;
  117. }
  118. }
  119. - (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(CDVWebViewNavigationType)navigationType
  120. {
  121. return [[self class] shouldOverrideLoadWithRequest:request navigationType:navigationType filterValue:[self filterUrl:request.URL]];
  122. }
  123. @end