Skip to content

Commit

Permalink
Chromium: Show error pages when errors happen
Browse files Browse the repository at this point in the history
Currently when errors happen when loading a website, Wolvic with Chromium backend just returns a blank page. This fix allows Wolvic with Chromium backend to return an error page customized to each error.

Fix Igalia#1615
  • Loading branch information
haanhvu committed Jan 7, 2025
1 parent 1bc0ec5 commit 84913b6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void navigationStateChanged(int flags) {
}
}

@Override
//@Override
public void onWebAppManifest(WebContents webContents, @NonNull String manifest) {
@Nullable WSession.ContentDelegate delegate = mSession.getContentDelegate();
if (delegate == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void didFailLoad(boolean isInPrimaryMainFrame, int errorCode, GURL failin

WSession.NavigationDelegate navigationDelegate = mSession.getNavigationDelegate();
if (navigationDelegate != null) {
navigationDelegate.onLoadError(mSession, failingUrl.getSpec(), new WWebRequestError() {
byte[] errorData = navigationDelegate.onLoadErrorData(mSession, failingUrl.getSpec(), new WWebRequestError() {
@Override
public int code() {
return errorCode;
Expand All @@ -135,6 +135,7 @@ public X509Certificate certificate() {
return null;
}
});
mSession.loadData(errorData, "text/html");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,15 @@ WResult<String> onLoadError(
@NonNull final WWebRequestError error) {
return null;
}

@UiThread
default @Nullable
byte[] onLoadErrorData(
@NonNull final WSession session,
@Nullable final String uri,
@NonNull final WWebRequestError error) {
return null;
}
}

@Retention(RetentionPolicy.SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,11 @@ public WResult<String> onLoadError(@NonNull WSession session, @Nullable String u
return WResult.fromValue(InternalPages.createErrorPageDataURI(mContext, uri, error.code()));
}

@Override
public byte[] onLoadErrorData(@NonNull WSession session, @Nullable String uri, @NonNull WWebRequestError error) {
return InternalPages.createErrorPageData(mContext, uri, error.code());
}

// Progress Listener

@Override
Expand Down
13 changes: 12 additions & 1 deletion app/src/common/shared/com/igalia/wolvic/utils/InternalPages.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

import mozilla.components.browser.errorpages.ErrorType;

import org.chromium.net.NetError;

public class InternalPages {

private static ErrorType fromSessionErrorToErrorType(int error) {
switch(error) {
case WWebRequestError.ERROR_SECURITY_SSL: {
return ErrorType.ERROR_SECURITY_SSL;
}
case NetError.ERR_CERT_DATE_INVALID:
case WWebRequestError.ERROR_SECURITY_BAD_CERT: {
return ErrorType.ERROR_SECURITY_BAD_CERT;
}
Expand Down Expand Up @@ -117,11 +120,19 @@ public static PageResources create(int html, int css) {
public static String createErrorPageDataURI(Context context,
@Nullable String uri,
int sessionError) {

return "data:text/html;base64," + Base64.encodeToString(createErrorPageData(context, uri, sessionError), Base64.NO_WRAP);
}

public static byte[] createErrorPageData(Context context,
@Nullable String uri,
int sessionError) {
String html = readRawResourceString(context, R.raw.error_pages);
String css = readRawResourceString(context, R.raw.error_style);

boolean showSSLAdvanced;
switch (sessionError) {
case NetError.ERR_CERT_DATE_INVALID:
case WWebRequestError.ERROR_SECURITY_SSL:
case WWebRequestError.ERROR_SECURITY_BAD_CERT:
showSSLAdvanced = true;
Expand All @@ -143,7 +154,7 @@ public static String createErrorPageDataURI(Context context,
html = html.replace("%url%", uri);
}

return "data:text/html;base64," + Base64.encodeToString(html.getBytes(), Base64.NO_WRAP);
return html.getBytes();
}

public static byte[] createAboutPage(Context context,
Expand Down
8 changes: 1 addition & 7 deletions app/src/main/res/raw/error_pages.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@
advancedVisible = !advancedVisible;
}
function acceptAndContinue(temporary) {
document.addCertException(temporary).then(() => {
location.reload();
},
err => {
console.error("Unexpected error: " + err)
}
);
window.certificateErrorPageController.proceed();
}
</script>
</head>
Expand Down

0 comments on commit 84913b6

Please sign in to comment.