Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 1 deletion android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ private void loadWebView() {
Logger.warn("Invalid url, using fallback");
}
}
localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode);
boolean jsProfilingEnabled = config.isJsProfilingEnabled();
localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode, jsProfilingEnabled);
localServer.hostAssets(DEFAULT_WEB_ASSET_DIR);

Logger.debug("Loading app at " + appUrl);
Expand Down
13 changes: 13 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/CapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class CapConfig {
private String errorPath;
private boolean zoomableWebView = false;
private boolean resolveServiceWorkerRequests = true;
private boolean jsProfilingEnabled = false;

// Embedded
private String startPath;
Expand Down Expand Up @@ -181,6 +182,7 @@ private CapConfig(Builder builder) {
this.errorPath = builder.errorPath;
this.zoomableWebView = builder.zoomableWebView;
this.resolveServiceWorkerRequests = builder.resolveServiceWorkerRequests;
this.jsProfilingEnabled = builder.jsProfilingEnabled;

// Embedded
this.startPath = builder.startPath;
Expand Down Expand Up @@ -286,6 +288,7 @@ private void deserializeConfig(@Nullable Context context) {
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);
zoomableWebView = JSONUtils.getBoolean(configJSON, "android.zoomEnabled", JSONUtils.getBoolean(configJSON, "zoomEnabled", false));
resolveServiceWorkerRequests = JSONUtils.getBoolean(configJSON, "android.resolveServiceWorkerRequests", true);
jsProfilingEnabled = JSONUtils.getBoolean(configJSON, "android.jsProfilingEnabled", false);

String logBehavior = JSONUtils.getString(
configJSON,
Expand Down Expand Up @@ -382,6 +385,10 @@ public boolean isResolveServiceWorkerRequests() {
return resolveServiceWorkerRequests;
}

public boolean isJsProfilingEnabled() {
return jsProfilingEnabled;
}

public boolean isWebContentsDebuggingEnabled() {
return webContentsDebuggingEnabled;
}
Expand Down Expand Up @@ -582,6 +589,7 @@ public static class Builder {
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;
private boolean zoomableWebView = false;
private boolean resolveServiceWorkerRequests = true;
private boolean jsProfilingEnabled = false;

// Embedded
private String startPath = null;
Expand Down Expand Up @@ -686,6 +694,11 @@ public Builder setResolveServiceWorkerRequests(boolean resolveServiceWorkerReque
return this;
}

public Builder setJsProfilingEnabled(boolean jsProfilingEnabled) {
this.jsProfilingEnabled = jsProfilingEnabled;
return this;
}

public Builder setWebContentsDebuggingEnabled(boolean webContentsDebuggingEnabled) {
this.webContentsDebuggingEnabled = webContentsDebuggingEnabled;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class WebViewLocalServer {
private final boolean html5mode;
private final JSInjector jsInjector;
private final Bridge bridge;
private final boolean jsProfilingEnabled;

/**
* A handler that produces responses for paths on the virtual asset server.
Expand Down Expand Up @@ -133,13 +134,14 @@ public Map<String, String> getResponseHeaders() {
}
}

WebViewLocalServer(Context context, Bridge bridge, JSInjector jsInjector, ArrayList<String> authorities, boolean html5mode) {
WebViewLocalServer(Context context, Bridge bridge, JSInjector jsInjector, ArrayList<String> authorities, boolean html5mode, boolean jsProfilingEnabled) {
uriMatcher = new UriMatcher(null);
this.html5mode = html5mode;
this.protocolHandler = new AndroidProtocolHandler(context.getApplicationContext());
this.authorities = authorities;
this.bridge = bridge;
this.jsInjector = jsInjector;
this.jsProfilingEnabled = jsProfilingEnabled;
}

private static Uri parseAndVerifyUrl(String url) {
Expand Down Expand Up @@ -619,7 +621,13 @@ private void createHostingDetails() {
throw new IllegalArgumentException("assetPath cannot contain the '*' character.");
}

PathHandler handler = new PathHandler() {
Map<String, String> customHeaders = null;
if (jsProfilingEnabled) {
customHeaders = new HashMap<>();
customHeaders.put("Document-Policy", "js-profiling");
}

PathHandler handler = new PathHandler(null, null, 200, "OK", customHeaders) {
@Override
public InputStream handle(Uri url) {
InputStream stream = null;
Expand Down
11 changes: 11 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ export interface CapacitorConfig {
* @default true
*/
resolveServiceWorkerRequests?: boolean;

/**
* Enable JavaScript profiling by adding the `Document-Policy: js-profiling`
* HTTP response header to WebView responses.
*
* This header is required for the JS Self-Profiling API to work in the WebView.
*
* @since 8.0.0
* @default false
*/
jsProfilingEnabled?: boolean;
};

ios?: {
Expand Down
Loading