You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2014/10/24 22:18:38 UTC

[07/12] android commit: Add hooks in CordovaPlugin and PluginManager for whitelist plugins

Add hooks in CordovaPlugin and PluginManager for whitelist plugins

This adds three hooks to CordovaPlugin objects. In each case, a null
value can be returned to indicate "I don't care". This null value is
the default.

    public Boolean shouldAllowRequest(String url)
    public Boolean shouldAllowNavigation(String url)
    public Boolean shouldOpenExternalUrl(String url)


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/b56c12ff
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/b56c12ff
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/b56c12ff

Branch: refs/heads/unplug-whitelist
Commit: b56c12ff4ce8c904738f754138dc65f519bdf3bc
Parents: cc7d352
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Oct 22 15:52:09 2014 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Oct 22 16:19:52 2014 -0400

----------------------------------------------------------------------
 .../src/org/apache/cordova/CordovaPlugin.java   |  46 ++++++--
 .../src/org/apache/cordova/PluginManager.java   | 104 +++++++++++++++++++
 2 files changed, 144 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/b56c12ff/framework/src/org/apache/cordova/CordovaPlugin.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaPlugin.java b/framework/src/org/apache/cordova/CordovaPlugin.java
index f467bdf..1485dad 100644
--- a/framework/src/org/apache/cordova/CordovaPlugin.java
+++ b/framework/src/org/apache/cordova/CordovaPlugin.java
@@ -162,19 +162,53 @@ public class CordovaPlugin {
      * Called when an activity you launched exits, giving you the requestCode you started it with,
      * the resultCode it returned, and any additional data from it.
      *
-     * @param requestCode		The request code originally supplied to startActivityForResult(),
-     * 							allowing you to identify who this result came from.
-     * @param resultCode		The integer result code returned by the child activity through its setResult().
-     * @param intent				An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
+     * @param requestCode   The request code originally supplied to startActivityForResult(),
+     *                      allowing you to identify who this result came from.
+     * @param resultCode    The integer result code returned by the child activity through its setResult().
+     * @param intent        An Intent, which can return result data to the caller (various data can be
+     *                      attached to Intent "extras").
      */
     public void onActivityResult(int requestCode, int resultCode, Intent intent) {
     }
 
     /**
+     * Hook for blocking the loading of external resources.
+     *
+     * This will be called when the WebView's shouldInterceptRequest wants to know whether to
+     * open a connection to an external resource. Return false to block the request. Only if
+     * all plugins return true, then the request will proceed.
+     */
+    public Boolean shouldAllowRequest(String url) {
+        return true;
+    }
+
+    /**
+     * Hook for blocking navigation by the Cordova WebView
+     *
+     * This will be called when the WebView's needs to know whether to navigate to a new page.
+     * Return false to block the navigation. Only if all plugins return true, then the navigation
+     * will proceed.
+     */
+    public Boolean shouldAllowNavigation(String url) {
+        return null;
+    }
+
+    /**
+     * Hook for blocking the launching of Intents by the Cordova application
+     *
+     * This will be called when the WebView will not navigate to a page, but could launch an intent
+     * to handle the URL. Return false to block the navigation. Only if all plugins return true,
+     * then the URL be opened.
+     */
+    public Boolean shouldOpenExternalUrl(String url) {
+        return null;
+    }
+
+    /**
      * By specifying a <url-filter> in config.xml you can map a URL (using startsWith atm) to this method.
      *
-     * @param url				The URL that is trying to be loaded in the Cordova webview.
-     * @return					Return true to prevent the URL from loading. Default is false.
+     * @param url           The URL that is trying to be loaded in the Cordova webview.
+     * @return              Return true to prevent the URL from loading. Default is false.
      */
     public boolean onOverrideUrlLoading(String url) {
         return false;

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/b56c12ff/framework/src/org/apache/cordova/PluginManager.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java
index bf69314..6b463fa 100755
--- a/framework/src/org/apache/cordova/PluginManager.java
+++ b/framework/src/org/apache/cordova/PluginManager.java
@@ -254,6 +254,110 @@ public class PluginManager {
     }
 
     /**
+     * Called when the webview is going to request an external resource.
+     *
+     * This delegates to the installed plugins, which must all return true for
+     * this method to return true.
+     *
+     * @param url       The URL that is being requested.
+     * @return          Tri-State:
+     *                    null: All plugins returned null (the default). This
+     *                          indicates that the default policy should be
+     *                          followed.
+     *                    true: All plugins returned true (allow the resource
+     *                          to load)
+     *                    false: At least one plugin returned false (block the
+     *                           resource)
+     */
+    public Boolean shouldAllowRequest(String url) {
+        Boolean anyResponded = null;
+        for (PluginEntry entry : this.entryMap.values()) {
+            CordovaPlugin plugin = pluginMap.get(entry.service);
+            if (plugin != null) {
+                Boolean result = plugin.shouldAllowRequest(url);
+                if (result != null) {
+                    anyResponded = true;
+                    if (!result) {
+                        return false;
+                    }
+                }
+            }
+        }
+        // This will be true if all plugins allow the request, or null if no plugins override the method
+        return anyResponded;
+    }
+
+    /**
+     * Called when the webview is going to change the URL of the loaded content.
+     *
+     * This delegates to the installed plugins, which must all return true for
+     * this method to return true. A true result will allow the new page to load;
+     * a false result will prevent the page from loading.
+     *
+     * @param url       The URL that is being requested.
+     * @return          Tri-State:
+     *                    null: All plugins returned null (the default). This
+     *                          indicates that the default policy should be
+     *                          followed.
+     *                    true: All plugins returned true (allow the navigation)
+     *                    false: At least one plugin returned false (block the
+     *                           navigation)
+     */
+    public Boolean shouldAllowNavigation(String url) {
+        Boolean anyResponded = null;
+        for (PluginEntry entry : this.entryMap.values()) {
+            CordovaPlugin plugin = pluginMap.get(entry.service);
+            if (plugin != null) {
+                Boolean result = plugin.shouldAllowNavigation(url);
+                if (result != null) {
+                    anyResponded = true;
+                    if (!result) {
+                        return false;
+                    }
+                }
+            }
+        }
+        // This will be true if all plugins allow the request, or null if no plugins override the method
+        return anyResponded;
+    }
+
+    /**
+     * Called when the webview is going not going to navigate, but may launch
+     * an Intent for an URL.
+     *
+     * This delegates to the installed plugins, which must all return true for
+     * this method to return true. A true result will allow the URL to launch;
+     * a false result will prevent the URL from loading.
+     *
+     * @param url       The URL that is being requested.
+     * @return          Tri-State:
+     *                    null: All plugins returned null (the default). This
+     *                          indicates that the default policy should be
+     *                          followed.
+     *                    true: All plugins returned true (allow the URL to
+     *                          launch an intent)
+     *                    false: At least one plugin returned false (block the
+     *                           intent)
+     */
+    public Boolean shouldOpenExternalUrl(String url) {
+        Boolean anyResponded = null;
+        for (PluginEntry entry : this.entryMap.values()) {
+            CordovaPlugin plugin = pluginMap.get(entry.service);
+            if (plugin != null) {
+                Boolean result = plugin.shouldOpenExternalUrl(url);
+                if (result != null) {
+                    anyResponded = true;
+                    if (!result) {
+                        return false;
+                    }
+                }
+            }
+        }
+        // This will be true if all plugins allow the request, or null if no plugins override the method
+        return anyResponded;
+    }
+
+    /**
      * Called when the URL of the webview changes.
      *
      * @param url               The URL that is being changed to.


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org