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

CDVIntentAndNavigationFilter.m 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // navigation to the scheme used by the app is also allowed
  40. self.allowNavigations = [[NSMutableArray alloc] initWithArray:@[ @"file://"]];
  41. // If the custom app scheme is defined, append it to the allow navigation as default
  42. NSString* scheme = ((CDVViewController*)self.viewController).appScheme;
  43. if (scheme) {
  44. [self.allowNavigations addObject: [NSString stringWithFormat:@"%@://", scheme]];
  45. }
  46. // no intents are added by default
  47. self.allowIntents = [[NSMutableArray alloc] init];
  48. }
  49. - (void)parserDidEndDocument:(NSXMLParser*)parser
  50. {
  51. self.allowIntentsWhitelist = [[CDVWhitelist alloc] initWithArray:self.allowIntents];
  52. self.allowNavigationsWhitelist = [[CDVWhitelist alloc] initWithArray:self.allowNavigations];
  53. }
  54. - (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError
  55. {
  56. NSAssert(NO, @"config.xml parse error line %ld col %ld", (long)[parser lineNumber], (long)[parser columnNumber]);
  57. }
  58. #pragma mark CDVPlugin
  59. - (void)pluginInitialize
  60. {
  61. if ([self.viewController isKindOfClass:[CDVViewController class]]) {
  62. [(CDVViewController*)self.viewController parseSettingsWithParser:self];
  63. }
  64. }
  65. + (CDVIntentAndNavigationFilterValue) filterUrl:(NSURL*)url intentsWhitelist:(CDVWhitelist*)intentsWhitelist navigationsWhitelist:(CDVWhitelist*)navigationsWhitelist
  66. {
  67. // a URL can only allow-intent OR allow-navigation, if both are specified,
  68. // only allow-navigation is allowed
  69. BOOL allowNavigationsPass = [navigationsWhitelist URLIsAllowed:url logFailure:NO];
  70. BOOL allowIntentPass = [intentsWhitelist URLIsAllowed:url logFailure:NO];
  71. if (allowNavigationsPass && allowIntentPass) {
  72. return CDVIntentAndNavigationFilterValueNavigationAllowed;
  73. } else if (allowNavigationsPass) {
  74. return CDVIntentAndNavigationFilterValueNavigationAllowed;
  75. } else if (allowIntentPass) {
  76. return CDVIntentAndNavigationFilterValueIntentAllowed;
  77. }
  78. return CDVIntentAndNavigationFilterValueNoneAllowed;
  79. }
  80. - (CDVIntentAndNavigationFilterValue) filterUrl:(NSURL*)url
  81. {
  82. return [[self class] filterUrl:url intentsWhitelist:self.allowIntentsWhitelist navigationsWhitelist:self.allowNavigationsWhitelist];
  83. }
  84. #define CDVWebViewNavigationTypeLinkClicked 0
  85. #define CDVWebViewNavigationTypeLinkOther -1
  86. + (BOOL)shouldOpenURLRequest:(NSURLRequest*)request navigationType:(CDVWebViewNavigationType)navigationType
  87. {
  88. return (
  89. navigationType == CDVWebViewNavigationTypeLinkClicked ||
  90. navigationType == CDVWebViewNavigationTypeLinkOther
  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 CDVWebViewNavigationTypeLinkClicked (anchor tag) or CDVWebViewNavigationTypeOther and it's an internal link
  103. if ([[self class] shouldOpenURLRequest:request navigationType:navigationType]){
  104. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  105. }
  106. // consume the request (i.e. no error) if it wasn't handled above
  107. return NO;
  108. case CDVIntentAndNavigationFilterValueNoneAllowed:
  109. // allow-navigation attempt failed for sure
  110. NSLog(@"%@", [NSString stringWithFormat:allowNavigations_whitelistRejectionFormatString, [url absoluteString]]);
  111. // anchor tag link means it was an allow-intent attempt that failed as well
  112. if (CDVWebViewNavigationTypeLinkClicked == navigationType) {
  113. NSLog(@"%@", [NSString stringWithFormat:allowIntents_whitelistRejectionFormatString, [url absoluteString]]);
  114. }
  115. return NO;
  116. }
  117. }
  118. - (BOOL)shouldOverrideLoadWithRequest:(NSURLRequest*)request navigationType:(CDVWebViewNavigationType)navigationType
  119. {
  120. return [[self class] shouldOverrideLoadWithRequest:request navigationType:navigationType filterValue:[self filterUrl:request.URL]];
  121. }
  122. @end