Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: implement WebViewAssetLoader #1137

Merged
merged 15 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ example
/framework/javadoc-private
/test/.externalNativeBuild

/test/android/gradle
/test/android/gradlew
/test/android/gradlew.bat
/test/androidx/gradle
/test/androidx/gradlew
/test/androidx/gradlew.bat
Expand Down
4 changes: 4 additions & 0 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@ bintray {
}
}
}

dependencies {
implementation 'androidx.webkit:webkit:1.3.0'
}
7 changes: 5 additions & 2 deletions framework/src/org/apache/cordova/ConfigXmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Licensed to the Apache Software Foundation (ASF) under one
public class ConfigXmlParser {
private static String TAG = "ConfigXmlParser";

private String launchUrl = "file:///android_asset/www/index.html";
private String launchUrl = null;
private CordovaPreferences prefs = new CordovaPreferences();
private ArrayList<PluginEntry> pluginEntries = new ArrayList<PluginEntry>(20);

Expand All @@ -46,6 +46,9 @@ public ArrayList<PluginEntry> getPluginEntries() {
}

public String getLaunchUrl() {
if (launchUrl == null) {
launchUrl = "https://" + this.prefs.getString("hostname", "localhost");
}
return launchUrl;
}

Expand Down Expand Up @@ -139,7 +142,7 @@ private void setStartUrl(String src) {
if (src.charAt(0) == '/') {
src = src.substring(1);
}
launchUrl = "file:///android_asset/www/" + src;
launchUrl = "https://" + this.prefs.getString("hostname", "localhost") + "/" + src;
}
}
}
8 changes: 8 additions & 0 deletions framework/src/org/apache/cordova/CordovaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,12 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) throws JSONException {

}

/**
* Allow plugins to supply a PathHandler for the WebViewAssetHandler
* @return a CordovaPluginPathHandler which listen for paths and returns a response
*/
public CordovaPluginPathHandler getPathHandler() {
return null;
}
}
38 changes: 38 additions & 0 deletions framework/src/org/apache/cordova/CordovaPluginPathHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package org.apache.cordova;

import androidx.webkit.WebViewAssetLoader;

/**
* Wrapper class for path and handler
*/
public class CordovaPluginPathHandler {

private final WebViewAssetLoader.PathHandler handler;

public CordovaPluginPathHandler(WebViewAssetLoader.PathHandler handler) {
this.handler = handler;
}

public WebViewAssetLoader.PathHandler getPathHandler() {
return handler;
}
}
1 change: 0 additions & 1 deletion framework/src/org/apache/cordova/CordovaWebViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public void init(CordovaInterface cordova, List<PluginEntry> pluginEntries, Cord

pluginManager.addService(CoreAndroid.PLUGIN_NAME, "org.apache.cordova.CoreAndroid");
pluginManager.init();

}

@Override
Expand Down
16 changes: 16 additions & 0 deletions framework/src/org/apache/cordova/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -577,4 +578,19 @@ public Bundle onSaveInstanceState() {
}
return state;
}

/**
* Collect all plugins PathHandlers
*
* @return list of PathHandlers in no particular order
*/
public ArrayList<CordovaPluginPathHandler> getPluginPathHandlers() {
ArrayList<CordovaPluginPathHandler> handlers = new ArrayList<CordovaPluginPathHandler>();
for (CordovaPlugin plugin : this.pluginMap.values()) {
if (plugin != null && plugin.getPathHandler() != null) {
handlers.add(plugin.getPathHandler());
}
}
return handlers;
}
}
60 changes: 58 additions & 2 deletions framework/src/org/apache/cordova/engine/SystemWebViewClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
package org.apache.cordova.engine;

import android.annotation.TargetApi;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.webkit.ClientCertRequest;
import android.webkit.HttpAuthHandler;
import android.webkit.MimeTypeMap;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.apache.cordova.AuthenticationToken;
import org.apache.cordova.CordovaClientCertRequest;
import org.apache.cordova.CordovaHttpAuthHandler;
import org.apache.cordova.CordovaPluginPathHandler;
import org.apache.cordova.CordovaResourceApi;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginManager;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import androidx.webkit.WebViewAssetLoader;

/**
* This class is the WebViewClient that implements callbacks for our web view.
Expand All @@ -56,6 +60,7 @@ public class SystemWebViewClient extends WebViewClient {

private static final String TAG = "SystemWebViewClient";
protected final SystemWebViewEngine parentEngine;
private final WebViewAssetLoader assetLoader;
private boolean doClearHistory = false;
boolean isCurrentlyLoading;

Expand All @@ -64,6 +69,52 @@ public class SystemWebViewClient extends WebViewClient {

public SystemWebViewClient(SystemWebViewEngine parentEngine) {
this.parentEngine = parentEngine;

WebViewAssetLoader.Builder assetLoaderBuilder = new WebViewAssetLoader.Builder()
.setDomain(parentEngine.preferences.getString("hostname", "localhost"))
.setHttpAllowed(true);

assetLoaderBuilder.addPathHandler("/", path -> {
try {
// Check if there a plugins with pathHandlers
PluginManager pluginManager = this.parentEngine.pluginManager;
if (pluginManager != null) {
for (CordovaPluginPathHandler handler : pluginManager.getPluginPathHandlers()) {
if (handler.getPathHandler() != null) {
WebResourceResponse response = handler.getPathHandler().handle(path);
if (response != null) {
return response;
}
};
}
}

if (path.isEmpty()) {
path = "index.html";
}
InputStream is = parentEngine.webView.getContext().getAssets().open("www/" + path, AssetManager.ACCESS_STREAMING);
String mimeType = "text/html";
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
if (extension != null) {
if (path.endsWith(".js") || path.endsWith(".mjs")) {
// Make sure JS files get the proper mimetype to support ES modules
mimeType = "application/javascript";
} else if (path.endsWith(".wasm")) {
mimeType = "application/wasm";
} else {
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
}

return new WebResourceResponse(mimeType, null, is);
} catch (Exception e) {
e.printStackTrace();
LOG.e(TAG, e.getMessage());
}
return null;
});

this.assetLoader = assetLoaderBuilder.build();
}

/**
Expand Down Expand Up @@ -366,4 +417,9 @@ private static boolean needsSpecialsInAssetUrlFix(Uri uri) {

return false;
}

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
return this.assetLoader.shouldInterceptRequest(request.getUrl());
}
}
12 changes: 0 additions & 12 deletions framework/src/org/apache/cordova/engine/SystemWebViewEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,13 @@ private void initWebViewSettings() {
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

/**
* https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)
*
* SDK >= 30 has recently set this value to false by default.
* It is recommended to turn off this settings To prevent possible security issues targeting Build.VERSION_CODES.Q and earlier.
* For existing functionality, this setting is set to true. In a future release, this should be defaulted to false.
*/
settings.setAllowFileAccess(true);

String manufacturer = android.os.Build.MANUFACTURER;
LOG.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);

//We don't save any form data in the application
settings.setSaveFormData(false);
settings.setSavePassword(false);

// Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
// while we do this
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setMediaPlaybackRequiresUserGesture(false);

// Enable database
Expand Down