Skip to content

Commit

Permalink
Chromium: Show error pages when errors happen
Browse files Browse the repository at this point in the history
  • Loading branch information
haanhvu committed Dec 14, 2024
1 parent 6ab8038 commit de8401c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -35,6 +36,7 @@ public ResultImpl() {

@Override
public synchronized void complete(@Nullable T value) {
Log.e("ChromiumErrorPage", "Chromium complete() is called");
mValue = value;
mComplete = true;

Expand Down Expand Up @@ -177,23 +179,28 @@ private synchronized void thenInternal(@NonNull final ResultImpl.Dispatcher disp

private void dispatch() {
if (!mComplete) {
Log.e("Chromium dispatch()", "IllegalStateException");
throw new IllegalStateException("Cannot dispatch unless result is complete");
}

if (mListeners.isEmpty()) {
if (mIsUncaughtError) {
Log.e("Chromium dispatch()", "UncaughtException");
// We have no listeners to forward the uncaught exception to;
// rethrow the exception to make it visible.
throw new ResultImpl.UncaughtException(mError);
}
Log.e("Chromium dispatch()", "mListeners is empty");
return;
}

if (mDispatcher == null) {
Log.e("Chromium dispatch()", "AssertionError");
throw new AssertionError("Shouldn't have listeners with null dispatcher");
}

for (int i = 0; i < mListeners.size(); ++i) {
Log.e("Chromium dispatch()", "For loop is called");
final ResultImpl.Dispatcher dispatcher = mListeners.keyAt(i);
final ArrayList<Runnable> jobs = mListeners.valueAt(i);
dispatcher.dispatch(
Expand All @@ -203,6 +210,7 @@ private void dispatch() {
}
});
}
Log.e("Chromium dispatch()", "mListeners.clear() is called");
mListeners.clear();
}

Expand Down
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
@@ -1,10 +1,14 @@
package com.igalia.wolvic.browser.api.impl;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.igalia.wolvic.browser.api.WResult;
import com.igalia.wolvic.browser.api.WSession;
import com.igalia.wolvic.browser.api.WWebRequestError;
import com.igalia.wolvic.utils.InternalPages;

import org.chromium.content_public.browser.LifecycleState;
import org.chromium.content_public.browser.NavigationHandle;
Expand Down Expand Up @@ -81,11 +85,14 @@ private int toWSessionOnVisitedFlags(NavigationHandle navigationHandle) {
*/
@Override
public void didFinishNavigationInPrimaryMainFrame(NavigationHandle navigationHandle) {
Log.e("ChromiumErrorPage", "didFinishNavigationInPrimaryMainFrame is called");

WSession.NavigationDelegate navigationDelegate = mSession.getNavigationDelegate();
if (navigationDelegate == null)
return;

if (navigationHandle.isErrorPage()) {
Log.e("ChromiumErrorPage", "isErrorPage() is true");
didFailLoad(true, navigationHandle.errorCode(), navigationHandle.getUrl(), 0);
return;
}
Expand Down Expand Up @@ -116,7 +123,9 @@ public void didFailLoad(boolean isInPrimaryMainFrame, int errorCode, GURL failin

WSession.NavigationDelegate navigationDelegate = mSession.getNavigationDelegate();
if (navigationDelegate != null) {
navigationDelegate.onLoadError(mSession, failingUrl.getSpec(), new WWebRequestError() {
Log.e("ChromiumErrorPage", "navigationDelegate != null => onLoadError() is called");

WResult<String> errorUriResult = navigationDelegate.onLoadError(mSession, failingUrl.getSpec(), new WWebRequestError() {
@Override
public int code() {
return errorCode;
Expand All @@ -135,6 +144,11 @@ public X509Certificate certificate() {
return null;
}
});

Log.e("ChromiumErrorPage", "errorUriResult: " + errorUriResult.toString());
//mSession.loadUri(errorUriResult.toString());
Log.e("ChromiumErrorPage", "InternalPages.htmlBytes: " + InternalPages.htmlBytes);
mSession.loadData(InternalPages.htmlBytes, "text/html");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.RectF;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.PointerIcon;
import android.view.Surface;
import android.view.inputmethod.CursorAnchorInfo;
Expand Down Expand Up @@ -514,6 +515,7 @@ WResult<String> onLoadError(
@NonNull final WSession session,
@Nullable final String uri,
@NonNull final WWebRequestError error) {
Log.e("ChromiumErrorPage", "onLoadError() returns null");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1237,10 +1237,15 @@ public WResult<WSession> onNewSession(@NonNull WSession aSession, @NonNull Strin
return WResult.fromValue(session.getWSession());
}

public String errorData;

@Override
public WResult<String> onLoadError(@NonNull WSession session, @Nullable String uri, @NonNull WWebRequestError error) {
Log.e("ChromiumErrorPage", "onLoadError() returns WResult.fromValue(InternalPages.createErrorPageDataURI(mContext, uri, error.code()))");
Log.d(LOGTAG, "Session onLoadError: " + uri);

Log.e("ChromiumErrorPage", "errorUriString: " + InternalPages.createErrorPageDataURI(mContext, uri, error.code()));
errorData = InternalPages.createErrorPageDataURI(mContext, uri, error.code());
return WResult.fromValue(InternalPages.createErrorPageDataURI(mContext, uri, error.code()));
}

Expand Down
16 changes: 15 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 @@ -2,6 +2,7 @@

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import androidx.annotation.Nullable;

Expand Down Expand Up @@ -130,22 +131,35 @@ public static String createErrorPageDataURI(Context context,
showSSLAdvanced = false;
}

ErrorType errorType = fromSessionErrorToErrorType(sessionError);
/*ErrorType errorType = fromSessionErrorToErrorType(sessionError);
html = html
.replace("%button%", context.getString(errorType.getRefreshButtonRes()))
.replace("%messageShort%", context.getString(errorType.getTitleRes()))
.replace("%messageLong%", context.getString(errorType.getMessageRes(), uri))
.replace("<ul>", "<ul role=\"presentation\">")
.replace("%css%", css)
.replace("%advancedSSLStyle%", showSSLAdvanced ? "block" : "none");*/
html = html
.replace("%button%", "something")
.replace("%messageShort%", "something")
.replace("%messageLong%", "something")
.replace("<ul>", "<ul role=\"presentation\">")
.replace("%css%", css)
.replace("%advancedSSLStyle%", showSSLAdvanced ? "block" : "none");

if (uri != null) {
html = html.replace("%url%", uri);
}

Log.e("ChromiumErrorPage", "createErrorPageDataURI() returns " + html);

htmlBytes = html.getBytes();

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

public static byte[] htmlBytes;

public static byte[] createAboutPage(Context context,
PageResources resources) {
String html = readRawResourceString(context, resources.html);
Expand Down

0 comments on commit de8401c

Please sign in to comment.