Skip to content

Commit 209b6b2

Browse files
author
Capacitor+ Bot
committed
chore: sync upstream PR ionic-team#8263 from @riderx
2 parents 2b487bf + 638070d commit 209b6b2

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

android/capacitor/src/main/java/com/getcapacitor/Bridge.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ private void loadWebView() {
272272
Logger.warn("Invalid url, using fallback");
273273
}
274274
}
275-
localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode);
275+
boolean jsProfilingEnabled = config.isJsProfilingEnabled();
276+
localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode, jsProfilingEnabled);
276277
localServer.hostAssets(DEFAULT_WEB_ASSET_DIR);
277278

278279
Logger.debug("Loading app at " + appUrl);

android/capacitor/src/main/java/com/getcapacitor/CapConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class CapConfig {
5454
private String errorPath;
5555
private boolean zoomableWebView = false;
5656
private boolean resolveServiceWorkerRequests = true;
57+
private boolean jsProfilingEnabled = false;
5758

5859
// Embedded
5960
private String startPath;
@@ -181,6 +182,7 @@ private CapConfig(Builder builder) {
181182
this.errorPath = builder.errorPath;
182183
this.zoomableWebView = builder.zoomableWebView;
183184
this.resolveServiceWorkerRequests = builder.resolveServiceWorkerRequests;
185+
this.jsProfilingEnabled = builder.jsProfilingEnabled;
184186

185187
// Embedded
186188
this.startPath = builder.startPath;
@@ -286,6 +288,7 @@ private void deserializeConfig(@Nullable Context context) {
286288
webContentsDebuggingEnabled = JSONUtils.getBoolean(configJSON, "android.webContentsDebuggingEnabled", isDebug);
287289
zoomableWebView = JSONUtils.getBoolean(configJSON, "android.zoomEnabled", JSONUtils.getBoolean(configJSON, "zoomEnabled", false));
288290
resolveServiceWorkerRequests = JSONUtils.getBoolean(configJSON, "android.resolveServiceWorkerRequests", true);
291+
jsProfilingEnabled = JSONUtils.getBoolean(configJSON, "android.jsProfilingEnabled", false);
289292

290293
String logBehavior = JSONUtils.getString(
291294
configJSON,
@@ -382,6 +385,10 @@ public boolean isResolveServiceWorkerRequests() {
382385
return resolveServiceWorkerRequests;
383386
}
384387

388+
public boolean isJsProfilingEnabled() {
389+
return jsProfilingEnabled;
390+
}
391+
385392
public boolean isWebContentsDebuggingEnabled() {
386393
return webContentsDebuggingEnabled;
387394
}
@@ -582,6 +589,7 @@ public static class Builder {
582589
private int minHuaweiWebViewVersion = DEFAULT_HUAWEI_WEBVIEW_VERSION;
583590
private boolean zoomableWebView = false;
584591
private boolean resolveServiceWorkerRequests = true;
592+
private boolean jsProfilingEnabled = false;
585593

586594
// Embedded
587595
private String startPath = null;
@@ -686,6 +694,11 @@ public Builder setResolveServiceWorkerRequests(boolean resolveServiceWorkerReque
686694
return this;
687695
}
688696

697+
public Builder setJsProfilingEnabled(boolean jsProfilingEnabled) {
698+
this.jsProfilingEnabled = jsProfilingEnabled;
699+
return this;
700+
}
701+
689702
public Builder setWebContentsDebuggingEnabled(boolean webContentsDebuggingEnabled) {
690703
this.webContentsDebuggingEnabled = webContentsDebuggingEnabled;
691704
return this;

android/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class WebViewLocalServer {
6363
private final boolean html5mode;
6464
private final JSInjector jsInjector;
6565
private final Bridge bridge;
66+
private final boolean jsProfilingEnabled;
6667

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

136-
WebViewLocalServer(Context context, Bridge bridge, JSInjector jsInjector, ArrayList<String> authorities, boolean html5mode) {
137+
WebViewLocalServer(Context context, Bridge bridge, JSInjector jsInjector, ArrayList<String> authorities, boolean html5mode, boolean jsProfilingEnabled) {
137138
uriMatcher = new UriMatcher(null);
138139
this.html5mode = html5mode;
139140
this.protocolHandler = new AndroidProtocolHandler(context.getApplicationContext());
140141
this.authorities = authorities;
141142
this.bridge = bridge;
142143
this.jsInjector = jsInjector;
144+
this.jsProfilingEnabled = jsProfilingEnabled;
143145
}
144146

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

622-
PathHandler handler = new PathHandler() {
624+
Map<String, String> customHeaders = null;
625+
if (jsProfilingEnabled) {
626+
customHeaders = new HashMap<>();
627+
customHeaders.put("Document-Policy", "js-profiling");
628+
}
629+
630+
PathHandler handler = new PathHandler(null, null, 200, "OK", customHeaders) {
623631
@Override
624632
public InputStream handle(Uri url) {
625633
InputStream stream = null;

cli/src/declarations.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ export interface CapacitorConfig {
297297
* @default true
298298
*/
299299
resolveServiceWorkerRequests?: boolean;
300+
301+
/**
302+
* Enable JavaScript profiling by adding the `Document-Policy: js-profiling`
303+
* HTTP response header to WebView responses.
304+
*
305+
* This header is required for the JS Self-Profiling API to work in the WebView.
306+
*
307+
* @since 8.0.0
308+
* @default false
309+
*/
310+
jsProfilingEnabled?: boolean;
300311
};
301312

302313
ios?: {

0 commit comments

Comments
 (0)