arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-whitelist/src/android/WhitelistPlugin.java
plugins/cordova-plugin-whitelist/src/android/WhitelistPlugin.javaRaw
  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
 10         http://www.apache.org/licenses/LICENSE-2.0
 11
 12       Unless required by applicable law or agreed to in writing,
 13       software distributed under the License is distributed on an
 14       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15       KIND, either express or implied.  See the License for the
 16       specific language governing permissions and limitations
 17       under the License.
 18*/
 19
 20package org.apache.cordova.whitelist;
 21
 22import org.apache.cordova.CordovaPlugin;
 23import org.apache.cordova.ConfigXmlParser;
 24import org.apache.cordova.LOG;
 25import org.apache.cordova.Whitelist;
 26import org.xmlpull.v1.XmlPullParser;
 27
 28import android.content.Context;
 29
 30public class WhitelistPlugin extends CordovaPlugin {
 31    private static final String LOG_TAG = "WhitelistPlugin";
 32    private Whitelist allowedNavigations;
 33    private Whitelist allowedIntents;
 34    private Whitelist allowedRequests;
 35
 36    // Used when instantiated via reflection by PluginManager
 37    public WhitelistPlugin() {
 38    }
 39    // These can be used by embedders to allow Java-configuration of whitelists.
 40    public WhitelistPlugin(Context context) {
 41        this(new Whitelist(), new Whitelist(), null);
 42        new CustomConfigXmlParser().parse(context);
 43    }
 44    public WhitelistPlugin(XmlPullParser xmlParser) {
 45        this(new Whitelist(), new Whitelist(), null);
 46        new CustomConfigXmlParser().parse(xmlParser);
 47    }
 48    public WhitelistPlugin(Whitelist allowedNavigations, Whitelist allowedIntents, Whitelist allowedRequests) {
 49        if (allowedRequests == null) {
 50            allowedRequests = new Whitelist();
 51            allowedRequests.addWhiteListEntry("file:///*", false);
 52            allowedRequests.addWhiteListEntry("data:*", false);
 53        }
 54        this.allowedNavigations = allowedNavigations;
 55        this.allowedIntents = allowedIntents;
 56        this.allowedRequests = allowedRequests;
 57    }
 58    @Override
 59    public void pluginInitialize() {
 60        if (allowedNavigations == null) {
 61            allowedNavigations = new Whitelist();
 62            allowedIntents = new Whitelist();
 63            allowedRequests = new Whitelist();
 64            new CustomConfigXmlParser().parse(webView.getContext());
 65        }
 66    }
 67
 68    private class CustomConfigXmlParser extends ConfigXmlParser {
 69        @Override
 70        public void handleStartTag(XmlPullParser xml) {
 71            String strNode = xml.getName();
 72            if (strNode.equals("content")) {
 73                String startPage = xml.getAttributeValue(null, "src");
 74                allowedNavigations.addWhiteListEntry(startPage, false);
 75            } else if (strNode.equals("allow-navigation")) {
 76                String origin = xml.getAttributeValue(null, "href");
 77                if ("*".equals(origin)) {
 78                    allowedNavigations.addWhiteListEntry("http://*/*", false);
 79                    allowedNavigations.addWhiteListEntry("https://*/*", false);
 80                    allowedNavigations.addWhiteListEntry("data:*", false);
 81                } else {
 82                    allowedNavigations.addWhiteListEntry(origin, false);
 83                }
 84            } else if (strNode.equals("allow-intent")) {
 85                String origin = xml.getAttributeValue(null, "href");
 86                allowedIntents.addWhiteListEntry(origin, false);
 87            } else if (strNode.equals("access")) {
 88                String origin = xml.getAttributeValue(null, "origin");
 89                String subdomains = xml.getAttributeValue(null, "subdomains");
 90                boolean external = (xml.getAttributeValue(null, "launch-external") != null);
 91                if (origin != null) {
 92                    if (external) {
 93                        LOG.w(LOG_TAG, "Found <access launch-external> within config.xml. Please use <allow-intent> instead.");
 94                        allowedIntents.addWhiteListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0));
 95                    } else {
 96                        if ("*".equals(origin)) {
 97                            allowedRequests.addWhiteListEntry("http://*/*", false);
 98                            allowedRequests.addWhiteListEntry("https://*/*", false);
 99                        } else {
100                            allowedRequests.addWhiteListEntry(origin, (subdomains != null) && (subdomains.compareToIgnoreCase("true") == 0));
101                        }
102                    }
103                }
104            }
105        }
106        @Override
107        public void handleEndTag(XmlPullParser xml) {
108        }
109    }
110
111    @Override
112    public Boolean shouldAllowNavigation(String url) {
113        if (allowedNavigations.isUrlWhiteListed(url)) {
114            return true;
115        }
116        return null; // Default policy
117    }
118
119    @Override
120    public Boolean shouldAllowRequest(String url) {
121        if (Boolean.TRUE == shouldAllowNavigation(url)) {
122            return true;
123        }
124        if (allowedRequests.isUrlWhiteListed(url)) {
125            return true;
126        }
127        return null; // Default policy
128    }
129
130    @Override
131    public Boolean shouldOpenExternalUrl(String url) {
132        if (allowedIntents.isUrlWhiteListed(url)) {
133            return true;
134        }
135        return null; // Default policy
136    }
137
138    public Whitelist getAllowedNavigations() {
139        return allowedNavigations;
140    }
141
142    public void setAllowedNavigations(Whitelist allowedNavigations) {
143        this.allowedNavigations = allowedNavigations;
144    }
145
146    public Whitelist getAllowedIntents() {
147        return allowedIntents;
148    }
149
150    public void setAllowedIntents(Whitelist allowedIntents) {
151        this.allowedIntents = allowedIntents;
152    }
153
154    public Whitelist getAllowedRequests() {
155        return allowedRequests;
156    }
157
158    public void setAllowedRequests(Whitelist allowedRequests) {
159        this.allowedRequests = allowedRequests;
160    }
161}