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

WhitelistPlugin.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. package org.apache.cordova.whitelist;
  18. import org.apache.cordova.CordovaPlugin;
  19. import org.apache.cordova.ConfigXmlParser;
  20. import org.apache.cordova.LOG;
  21. import org.apache.cordova.Whitelist;
  22. import org.xmlpull.v1.XmlPullParser;
  23. import android.content.Context;
  24. public class WhitelistPlugin extends CordovaPlugin {
  25. private static final String LOG_TAG = "WhitelistPlugin";
  26. private Whitelist allowedNavigations;
  27. private Whitelist allowedIntents;
  28. private Whitelist allowedRequests;
  29. // Used when instantiated via reflection by PluginManager
  30. public WhitelistPlugin() {
  31. }
  32. // These can be used by embedders to allow Java-configuration of whitelists.
  33. public WhitelistPlugin(Context context) {
  34. this(new Whitelist(), new Whitelist(), null);
  35. new CustomConfigXmlParser().parse(context);
  36. }
  37. public WhitelistPlugin(XmlPullParser xmlParser) {
  38. this(new Whitelist(), new Whitelist(), null);
  39. new CustomConfigXmlParser().parse(xmlParser);
  40. }
  41. public WhitelistPlugin(Whitelist allowedNavigations, Whitelist allowedIntents, Whitelist allowedRequests) {
  42. if (allowedRequests == null) {
  43. allowedRequests = new Whitelist();
  44. allowedRequests.addWhiteListEntry("file:///*", false);
  45. allowedRequests.addWhiteListEntry("data:*", false);
  46. }
  47. this.allowedNavigations = allowedNavigations;
  48. this.allowedIntents = allowedIntents;
  49. this.allowedRequests = allowedRequests;
  50. }
  51. @Override
  52. public void pluginInitialize() {
  53. if (allowedNavigations == null) {
  54. allowedNavigations = new Whitelist();
  55. allowedIntents = new Whitelist();
  56. allowedRequests = new Whitelist();
  57. new CustomConfigXmlParser().parse(webView.getContext());
  58. }
  59. }
  60. private class CustomConfigXmlParser extends ConfigXmlParser {
  61. @Override
  62. public void handleStartTag(XmlPullParser xml) {
  63. String strNode = xml.getName();
  64. if (strNode.equals("content")) {
  65. String startPage = xml.getAttributeValue(null, "src");
  66. allowedNavigations.addWhiteListEntry(startPage, false);
  67. } else if (strNode.equals("allow-navigation")) {
  68. String origin = xml.getAttributeValue(null, "href");
  69. if ("*".equals(origin)) {
  70. allowedNavigations.addWhiteListEntry("http://*/*", false);
  71. allowedNavigations.addWhiteListEntry("https://*/*", false);
  72. allowedNavigations.addWhiteListEntry("data:*", false);
  73. } else {
  74. allowedNavigations.addWhiteListEntry(origin, false);
  75. }
  76. } else if (strNode.equals("allow-intent")) {
  77. String origin = xml.getAttributeValue(null, "href");
  78. allowedIntents.addWhiteListEntry(origin, false);
  79. } else if (strNode.equals("access")) {
  80. String origin = xml.getAttributeValue(null, "origin");
  81. String subdomains = xml.getAttributeValue(null, "subdomains");
  82. boolean external = (xml.getAttributeValue(null, "launch-external") != null);
  83. if (origin != null) {
  84. if (external) {
  85. LOG.w(LOG_TAG, "Found <access launch-external> within config.xml. Please use <allow-intent> instead.");
  86. allowedIntents.addWhiteListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0));
  87. } else {
  88. if ("*".equals(origin)) {
  89. allowedRequests.addWhiteListEntry("http://*/*", false);
  90. allowedRequests.addWhiteListEntry("https://*/*", false);
  91. } else {
  92. allowedRequests.addWhiteListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0));
  93. }
  94. }
  95. }
  96. }
  97. }
  98. @Override
  99. public void handleEndTag(XmlPullParser xml) {
  100. }
  101. }
  102. @Override
  103. public Boolean shouldAllowNavigation(String url) {
  104. if (allowedNavigations.isUrlWhiteListed(url)) {
  105. return true;
  106. }
  107. return null; // Default policy
  108. }
  109. @Override
  110. public Boolean shouldAllowRequest(String url) {
  111. if (Boolean.TRUE == shouldAllowNavigation(url)) {
  112. return true;
  113. }
  114. if (allowedRequests.isUrlWhiteListed(url)) {
  115. return true;
  116. }
  117. return null; // Default policy
  118. }
  119. @Override
  120. public Boolean shouldOpenExternalUrl(String url) {
  121. if (allowedIntents.isUrlWhiteListed(url)) {
  122. return true;
  123. }
  124. return null; // Default policy
  125. }
  126. public Whitelist getAllowedNavigations() {
  127. return allowedNavigations;
  128. }
  129. public void setAllowedNavigations(Whitelist allowedNavigations) {
  130. this.allowedNavigations = allowedNavigations;
  131. }
  132. public Whitelist getAllowedIntents() {
  133. return allowedIntents;
  134. }
  135. public void setAllowedIntents(Whitelist allowedIntents) {
  136. this.allowedIntents = allowedIntents;
  137. }
  138. public Whitelist getAllowedRequests() {
  139. return allowedRequests;
  140. }
  141. public void setAllowedRequests(Whitelist allowedRequests) {
  142. this.allowedRequests = allowedRequests;
  143. }
  144. }