Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Hook up the whitelist plugin for loaded apps
  • Loading branch information
agrieve committed May 13, 2015
1 parent ab7d4b1 commit c183c956889bbb0073861d35367d002c8867a447
Showing 6 changed files with 63 additions and 49 deletions.
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
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 boolean isSlaveCreated() {
@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 @@ private void evalJs(String code, CallbackContext callbackContext) {
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 @@ private void create(String url, Set<String> pluginIdWhitelist, CallbackContext c
// 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,30 +239,32 @@ private void setSlaveVisible(boolean value, CallbackContext callbackContext) {
}
}

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.
// Note that we re-route navigator.app.exitApp() in JS to close the webview rather than close the Activity.
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 @@ private CordovaPlugin createWhitelistPlugin(Uri configXml) {
}
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);
}
}
@@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.appharness;

import org.apache.cordova.CordovaWebViewEngine;
import org.apache.cordova.engine.SystemWebView;
import org.apache.cordova.engine.SystemWebViewEngine;

@@ -56,6 +57,11 @@ public void evaluateJavascript(String script) {
}
}

@Override
public CordovaWebViewEngine asEngine() {
return this;
}

@Override
public boolean goBack() {
if (canGoBack()) {
@@ -18,7 +18,10 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.appharness;

import org.apache.cordova.CordovaWebViewEngine;

interface CustomCordovaWebView {
void setStealTapEvents(boolean value);
void evaluateJavascript(String script);
CordovaWebViewEngine asEngine();
}
@@ -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) {
@@ -38,21 +38,21 @@
}

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() {
var deferred = $q.defer();
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) {
@@ -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) {

0 comments on commit c183c95

Please sign in to comment.