cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [5/9] cordova-app-harness git commit: Hook up the whitelist plugin for loaded apps
Date Wed, 13 May 2015 02:42:50 GMT
Hook up the whitelist plugin for loaded apps


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

Branch: refs/heads/master
Commit: c183c956889bbb0073861d35367d002c8867a447
Parents: ab7d4b1
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Tue Mar 17 10:22:55 2015 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Tue May 12 22:17:49 2015 -0400

----------------------------------------------------------------------
 AppHarnessUI/android/AppHarnessUI.java         | 71 ++++++++++-----------
 AppHarnessUI/android/CustomAndroidWebView.java |  6 ++
 AppHarnessUI/android/CustomCordovaWebView.java |  3 +
 AppHarnessUI/appharnessui.js                   |  8 +--
 www/cdvah/js/AppHarnessUI.js                   |  8 +--
 www/cdvah/js/AppsService.js                    | 16 ++++-
 6 files changed, 63 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/AppHarnessUI/android/AppHarnessUI.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/android/AppHarnessUI.java b/AppHarnessUI/android/AppHarnessUI.java
index 661599a..b19ecf2 100644
--- a/AppHarnessUI/android/AppHarnessUI.java
+++ b/AppHarnessUI/android/AppHarnessUI.java
@@ -18,6 +18,7 @@
 */
 package org.apache.appharness;
 
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -76,20 +77,22 @@ public class AppHarnessUI extends CordovaPlugin {
     @Override
     public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
throws JSONException {
         if ("create".equals(action)) {
-            final String url = args.getString(0);
-            final Set<String> pluginIdWhitelistAsSet = jsonArrayToSet(args.getJSONArray(1));
+            final Uri startUri = Uri.parse(args.getString(0));
+            final Uri configXmlUri = Uri.parse(args.getString(1));
+            final Set<String> pluginIdWhitelistAsSet = jsonArrayToSet(args.getJSONArray(2));
+            final String webViewType = args.getString(3);
             this.cordova.getActivity().runOnUiThread(new Runnable() {
                 public void run() {
-                    create(url, pluginIdWhitelistAsSet, callbackContext);
+                    create(startUri, configXmlUri, pluginIdWhitelistAsSet, webViewType, callbackContext);
                 }
             });
         } else if ("reload".equals(action)) {
-            final String url = args.getString(0);
-            final Set<String> pluginIdWhitelistAsSet = jsonArrayToSet(args.getJSONArray(1));
-            final String webViewType = args.getString(2);
+            final Uri startUri = Uri.parse(args.getString(0));
+            final Uri configXmlUri = Uri.parse(args.getString(1));
+            final Set<String> pluginIdWhitelistAsSet = jsonArrayToSet(args.getJSONArray(2));
             this.cordova.getActivity().runOnUiThread(new Runnable() {
                 public void run() {
-                    reload(url, pluginIdWhitelistAsSet, webViewType, callbackContext);
+                    reload(startUri, configXmlUri, pluginIdWhitelistAsSet, callbackContext);
                 }
             });
         } else if ("destroy".equals(action)) {
@@ -145,7 +148,7 @@ public class AppHarnessUI extends CordovaPlugin {
         callbackContext.success();
     }
 
-    private void create(String url, Set<String> pluginIdWhitelist, CallbackContext
callbackContext) {
+    private void create(Uri startUri, Uri configXmlUri, Set<String> pluginIdWhitelist,
String webViewType, CallbackContext callbackContext) {
         CordovaActivity activity = (CordovaActivity)cordova.getActivity();
 
         if (slaveWebView != null) {
@@ -158,29 +161,28 @@ public class AppHarnessUI extends CordovaPlugin {
             // We'll set the plugin entries in initWebView.
             slaveWebView.init(cordova, new ArrayList<PluginEntry>(), preferences);
         }
-        {
-            initWebView((CordovaWebViewEngine)slaveWebViewEngine, pluginIdWhitelist);
+        setPluginEntries(pluginIdWhitelist, configXmlUri);
 
-            slaveWebView.clearCache(true);
-            slaveWebView.clearHistory();
-            slaveWebView.loadUrl(url);
-            contentView.addView(slaveWebView.getView());
-            slaveVisible = true;
-            // Back button capturing breaks without these:
-            webView.getView().setEnabled(false);
-            slaveWebView.getView().requestFocus();
-        }
+        slaveWebView.clearCache(true);
+        slaveWebView.clearHistory();
+        slaveWebView.loadUrl(startUri.toString());
+        contentView.addView(slaveWebView.getView());
+        slaveVisible = true;
+        // Back button capturing breaks without these:
+        webView.getView().setEnabled(false);
+        slaveWebView.getView().requestFocus();
         callbackContext.success();
     }
 
-    private void reload(String url, Set<String> pluginIdWhitelist, String webViewType,
CallbackContext callbackContext) {
+    private void reload(Uri startUri, Uri configXmlUri, Set<String> pluginIdWhitelist,
CallbackContext callbackContext) {
         if (slaveWebView == null) {
             Log.w(LOG_TAG, "reload: no webview exists");
         } else {
             // TODO(maxw): If the webview type has changed, create a new webview.
-            setPluginEntries(pluginIdWhitelist);
+            setPluginEntries(pluginIdWhitelist, configXmlUri);
             slaveWebView.clearCache(true);
-            slaveWebView.loadUrl(url);
+            slaveWebView.clearHistory();
+            slaveWebView.loadUrl(startUri.toString());
         }
         callbackContext.success();
     }
@@ -237,19 +239,21 @@ public class AppHarnessUI extends CordovaPlugin {
         }
     }
 
-    private void setPluginEntries(Set<String> pluginIdWhitelist) {
+    private void setPluginEntries(Set<String> pluginIdWhitelist, Uri configXmlUri)
{
         CordovaActivity activity = (CordovaActivity)cordova.getActivity();
+        // Extract the <feature> from CADT's config.xml, and filter out unwanted plugins.
         ConfigXmlParser parser = new ConfigXmlParser();
-        // TODO: Parse the app's config.xml rather than our own config.xml.
         parser.parse(activity);
         ArrayList<PluginEntry> pluginEntries = new ArrayList<PluginEntry>(parser.getPluginEntries());
-        for (PluginEntry p : parser.getPluginEntries()) {
+        for (int i = 0; i < pluginEntries.size();) {
+            PluginEntry p = pluginEntries.get(i);
             if (!pluginIdWhitelist.contains(p.service)) {
                 pluginEntries.remove(p);
+                continue;
             } else if (WhitelistPlugin.class.getCanonicalName().equals(p.pluginClass)) {
-                // TODO: pass through path to launching config.xml.
-                //p.plugin = createWhitelistPlugin();
+                pluginEntries.set(i, new PluginEntry(p.service, createWhitelistPlugin(configXmlUri)));
             }
+            ++i;
         }
         slaveWebView.getPluginManager().setPluginEntries(pluginEntries);
         // This is added by cordova-android in code, so we need to re-add it likewise.
@@ -257,10 +261,10 @@ public class AppHarnessUI extends CordovaPlugin {
         slaveWebView.getPluginManager().addService("CoreAndroid", "org.apache.cordova.CoreAndroid");
     }
 
-    private CordovaPlugin createWhitelistPlugin(Uri configXml) {
+    private CordovaPlugin createWhitelistPlugin(Uri configXmlUri) {
         InputStream istr = null;
         try {
-            istr = webView.getResourceApi().openForRead(configXml).inputStream;
+            istr = new FileInputStream(configXmlUri.getPath());
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(false);
             XmlPullParser parser = factory.newPullParser();
@@ -280,13 +284,4 @@ public class AppHarnessUI extends CordovaPlugin {
         }
         return null;
     }
-
-    private void initWebView(final CordovaWebViewEngine newWebView, Set<String> pluginIdWhitelist)
{
-        setPluginEntries(pluginIdWhitelist);
-
-        newWebView.getView().setLayoutParams(new ViewGroup.LayoutParams(
-                ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT));
-        newWebView.getView().setVisibility(View.VISIBLE);
-    }
 }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/AppHarnessUI/android/CustomAndroidWebView.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/android/CustomAndroidWebView.java b/AppHarnessUI/android/CustomAndroidWebView.java
index 174d372..1f34eb3 100644
--- a/AppHarnessUI/android/CustomAndroidWebView.java
+++ b/AppHarnessUI/android/CustomAndroidWebView.java
@@ -18,6 +18,7 @@
 */
 package org.apache.appharness;
 
+import org.apache.cordova.CordovaWebViewEngine;
 import org.apache.cordova.engine.SystemWebView;
 import org.apache.cordova.engine.SystemWebViewEngine;
 
@@ -57,6 +58,11 @@ class CustomAndroidWebView extends SystemWebViewEngine implements CustomCordovaW
     }
 
     @Override
+    public CordovaWebViewEngine asEngine() {
+        return this;
+    }
+
+    @Override
     public boolean goBack() {
         if (canGoBack()) {
             return super.goBack();

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/AppHarnessUI/android/CustomCordovaWebView.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/android/CustomCordovaWebView.java b/AppHarnessUI/android/CustomCordovaWebView.java
index 93906e4..1f0fbe1 100644
--- a/AppHarnessUI/android/CustomCordovaWebView.java
+++ b/AppHarnessUI/android/CustomCordovaWebView.java
@@ -18,7 +18,10 @@
 */
 package org.apache.appharness;
 
+import org.apache.cordova.CordovaWebViewEngine;
+
 interface CustomCordovaWebView {
     void setStealTapEvents(boolean value);
     void evaluateJavascript(String script);
+    CordovaWebViewEngine asEngine();
 }

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/AppHarnessUI/appharnessui.js
----------------------------------------------------------------------
diff --git a/AppHarnessUI/appharnessui.js b/AppHarnessUI/appharnessui.js
index 7d26496..a5c147d 100644
--- a/AppHarnessUI/appharnessui.js
+++ b/AppHarnessUI/appharnessui.js
@@ -25,13 +25,13 @@ function eventHandler(type) {
 
 exports.onEvent = null;
 
-exports.create = function(url, serviceNameWhitelist, win) {
+exports.create = function(startUrl, configXmlUrl, serviceNameWhitelist, webViewType, win)
{
     exec(eventHandler, null, 'AppHarnessUI', 'events', []);
-    exec(win, null, 'AppHarnessUI', 'create', [url, serviceNameWhitelist]);
+    exec(win, null, 'AppHarnessUI', 'create', [startUrl, configXmlUrl, serviceNameWhitelist,
webViewType]);
 };
 
-exports.reload = function(url, serviceNameWhitelist, webViewType, win) {
-    exec(win, null, 'AppHarnessUI', 'reload', [url, serviceNameWhitelist, webViewType]);
+exports.reload = function(startUrl, configXmlUrl, serviceNameWhitelist, win) {
+    exec(win, null, 'AppHarnessUI', 'reload', [startUrl, configXmlUrl, serviceNameWhitelist]);
 };
 
 exports.destroy = function(win) {

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/www/cdvah/js/AppHarnessUI.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/AppHarnessUI.js b/www/cdvah/js/AppHarnessUI.js
index 8df095f..d107a37 100644
--- a/www/cdvah/js/AppHarnessUI.js
+++ b/www/cdvah/js/AppHarnessUI.js
@@ -38,10 +38,10 @@
         }
 
         return {
-            create: function(url, pluginMetadata) {
+            create: function(startUrl, configXmlUrl, pluginMetadata, webViewType) {
                 var deferred = $q.defer();
                 var serviceNames = createServiceNameWhitelist(pluginMetadata);
-                cordova.plugins.appharnessui.create(url, serviceNames, deferred.resolve);
+                cordova.plugins.appharnessui.create(startUrl, configXmlUrl, serviceNames,
webViewType, deferred.resolve);
                 return deferred.promise;
             },
             destroy: function() {
@@ -49,10 +49,10 @@
                 cordova.plugins.appharnessui.destroy(deferred.resolve);
                 return deferred.promise;
             },
-            reload: function(url, pluginMetadata, webViewType) {
+            reload: function(startUrl, configXmlUrl, pluginMetadata) {
                 var deferred = $q.defer();
                 var serviceNames = createServiceNameWhitelist(pluginMetadata);
-                cordova.plugins.appharnessui.reload(url, serviceNames, webViewType, deferred.resolve);
+                cordova.plugins.appharnessui.reload(startUrl, configXmlUrl, serviceNames,
deferred.resolve);
                 return deferred.promise;
             },
             setVisible: function(value) {

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/c183c956/www/cdvah/js/AppsService.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/AppsService.js b/www/cdvah/js/AppsService.js
index e10a940..6d1292d 100644
--- a/www/cdvah/js/AppsService.js
+++ b/www/cdvah/js/AppsService.js
@@ -163,11 +163,21 @@
                         $location.path('/inappmenu');
                         // If we're relaunching the active app, just reload the existing
webview.
                         // Otherwise, create a new one.
-                        // TODO(maxw): Use the existing webview all the time.
+                        var configXmlUrl = installer.directoryManager.rootURL + 'config.xml';
+                        var webViewType = 'system';
                         if (relaunch) {
-                            return AppHarnessUI.reload(launchUrl, pluginMetadata, 'system');
+                            if (webViewType != curWebViewType) {
+                                curWebViewType = webViewType;
+                                return AppHarnessUI.destroy()
+                                .then(function() {
+                                    return AppHarnessUI.create(launchUrl, configXmlUrl, pluginMetadata,
webViewType);
+                                });
+                            } else {
+                                return AppHarnessUI.reload(launchUrl, configXmlUrl, pluginMetadata);
+                            }
                         } else {
-                            return AppHarnessUI.create(launchUrl, pluginMetadata);
+                            curWebViewType = webViewType;
+                            return AppHarnessUI.create(launchUrl, configXmlUrl, pluginMetadata,
webViewType);
                         }
                     }).then(function() {
                         if (AppsService.onAppListChange) {


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


Mime
View raw message