Commit 82702160 authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: wires up injecting html in error pages for Java

BUG=1031060
TEST=covered by tests.

Change-Id: Ifdb0db3241c09d8c70cc6e7953fc0278ee913f8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2330433
Commit-Queue: Scott Violet <sky@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#793450}
parent 29e4a78b
......@@ -4,6 +4,8 @@
package org.chromium.weblayer.test;
import static org.chromium.content_public.browser.test.util.TestThreadUtils.runOnUiThreadBlocking;
import android.net.Uri;
import androidx.test.filters.SmallTest;
......@@ -19,7 +21,10 @@ import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.ServerCertificate;
import org.chromium.net.test.util.TestWebServer;
import org.chromium.weblayer.ErrorPage;
import org.chromium.weblayer.ErrorPageCallback;
import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationController;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.shell.InstrumentationActivity;
......@@ -42,6 +47,7 @@ public class ErrorPageCallbackTest {
private static class Callback extends ErrorPageCallback {
public boolean mSignaled;
public String mSafetyPage;
public ErrorPage mErrorPage;
public Tab mTab;
public Callback(Tab tab) {
......@@ -58,6 +64,11 @@ public class ErrorPageCallbackTest {
mTab.getNavigationController().navigate(Uri.parse(mSafetyPage));
return true;
}
@Override
public ErrorPage getErrorPage(Navigation navigation) {
return mErrorPage;
}
}
@Before
......@@ -135,4 +146,22 @@ public class ErrorPageCallbackTest {
navigationWaiter.waitForNavigation();
Assert.assertTrue(mCallback.mSignaled);
}
@Test
@SmallTest
@MinWebLayerVersion(86)
public void testOverrideErrorPage() throws Throwable {
mCallback.mErrorPage = new ErrorPage("<html><head><title>test error</title>");
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivity.getTab().setErrorPageCallback(mCallback); });
String errorPageUrl = "http://localhost:7/non_existent";
mActivityTestRule.navigateAndWaitForFailure(errorPageUrl);
runOnUiThreadBlocking(() -> {
NavigationController navigationController =
mActivity.getTab().getNavigationController();
Assert.assertEquals("test error",
navigationController.getNavigationEntryTitle(
navigationController.getNavigationListCurrentIndex()));
});
}
}
......@@ -4,8 +4,10 @@
#include "weblayer/browser/error_page_callback_proxy.h"
#include "base/android/jni_string.h"
#include "url/gurl.h"
#include "weblayer/browser/java/jni/ErrorPageCallbackProxy_jni.h"
#include "weblayer/browser/navigation_impl.h"
#include "weblayer/public/error_page.h"
#include "weblayer/public/tab.h"
......@@ -32,8 +34,15 @@ bool ErrorPageCallbackProxy::OnBackToSafety() {
std::unique_ptr<ErrorPage> ErrorPageCallbackProxy::GetErrorPageContent(
Navigation* navigation) {
// TODO(sky): wire up java side support.
return nullptr;
JNIEnv* env = AttachCurrentThread();
auto error_string = Java_ErrorPageCallbackProxy_getErrorPageContent(
env, java_impl_,
static_cast<NavigationImpl*>(navigation)->java_navigation());
if (!error_string)
return nullptr;
auto error_page = std::make_unique<ErrorPage>();
error_page->html = ConvertJavaStringToUTF8(env, error_string);
return error_page;
}
static jlong JNI_ErrorPageCallbackProxy_CreateErrorPageCallbackProxy(
......
......@@ -43,6 +43,12 @@ public final class ErrorPageCallbackProxy {
return mClient.onBackToSafety();
}
@CalledByNative
private String getErrorPageContent(NavigationImpl navigation) throws RemoteException {
if (WebLayerFactoryImpl.getClientMajorVersion() < 86) return null;
return mClient.getErrorPageContent(navigation.getClientNavigation());
}
@NativeMethods
interface Natives {
long createErrorPageCallbackProxy(ErrorPageCallbackProxy proxy, long tab);
......
......@@ -4,10 +4,13 @@
package org.chromium.weblayer_private.interfaces;
import org.chromium.weblayer_private.interfaces.IClientNavigation;
/**
* Allows the client to override the default way of handling user interactions
* with error pages (such as SSL interstitials).
*/
interface IErrorPageCallbackClient {
boolean onBackToSafety() = 0;
String getErrorPageContent(IClientNavigation navigation) = 1;
}
......@@ -53,6 +53,7 @@ android_library("java") {
"org/chromium/weblayer/DownloadCallback.java",
"org/chromium/weblayer/DownloadError.java",
"org/chromium/weblayer/DownloadState.java",
"org/chromium/weblayer/ErrorPage.java",
"org/chromium/weblayer/ErrorPageCallback.java",
"org/chromium/weblayer/FindInPageCallback.java",
"org/chromium/weblayer/FindInPageController.java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.weblayer;
import androidx.annotation.NonNull;
/**
* ErrorPage contains the html to show when an error is encountered.
*
* @since 86
*/
public class ErrorPage {
public final String htmlContent;
/**
* Creates an ErrorPage.
*
* @param htmlContent The html to show.
*
*/
public ErrorPage(@NonNull String htmlContent) {
this.htmlContent = htmlContent;
}
}
......@@ -4,6 +4,9 @@
package org.chromium.weblayer;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* An interface that allows clients to handle error page interactions.
*/
......@@ -14,4 +17,19 @@ public abstract class ErrorPageCallback {
* @return true if the action was overridden and WebLayer should skip default handling.
*/
public abstract boolean onBackToSafety();
/**
* Called when an error is encountered. A null return value results in a default error page
* being shown, a non-null return value results in showing the content of the returned
* {@link ErrorPage}.
*
* @param navigation The navigation that encountered the error.
*
* @return The error page.
*
* @since 86
*/
public @Nullable ErrorPage getErrorPage(@NonNull Navigation navigation) {
return null;
}
}
......@@ -17,6 +17,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.chromium.weblayer_private.interfaces.APICallException;
import org.chromium.weblayer_private.interfaces.IClientNavigation;
import org.chromium.weblayer_private.interfaces.IErrorPageCallbackClient;
import org.chromium.weblayer_private.interfaces.IFullscreenCallbackClient;
import org.chromium.weblayer_private.interfaces.IGoogleAccountsCallbackClient;
......@@ -782,6 +783,12 @@ public class Tab {
StrictModeWorkaround.apply();
return mCallback.onBackToSafety();
}
@Override
public String getErrorPageContent(IClientNavigation navigation) {
StrictModeWorkaround.apply();
ErrorPage errorPage = mCallback.getErrorPage((Navigation) navigation);
return errorPage == null ? null : errorPage.htmlContent;
}
}
private static final class FullscreenCallbackClientImpl extends IFullscreenCallbackClient.Stub {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment