Commit e6c6234c authored by Colin Blundell's avatar Colin Blundell Committed by Commit Bot

[WebLayer] Bring in WebView's support for displaying network errors

This CL provides an initial implementation of displaying network errors
in WebLayer on Android. This implementation is copied from that of
WebView. Note that there is no attempt made to share the code as this CL
will need to be merged to 79.

This CL does not copy WebView's support for suppressing the showing of
error pages (AWContentRendererClient::ShouldSuppressErrorPage()). This
support is present in WebView for apps that want to display custom
error pages, which is not a requirement in WebLayer at this time.

To test: put your device into airplane mode and visit a webpage in
WebLayer. You should get an error page with the upside-down Android
icon.

Change-Id: I4e63cb577e433e3a34db126110ada9b91c851e51
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1906349
Commit-Queue: Colin Blundell <blundell@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714999}
parent 5bba9675
......@@ -264,6 +264,8 @@ jumbo_static_library("weblayer_lib") {
"common/crash_reporter_client.h",
]
deps += [
"//android_webview:generate_aw_resources",
"//android_webview:generate_aw_strings",
"//components/crash/android:crashpad_main",
"//components/safe_browsing",
"//components/safe_browsing/android:remote_database_manager",
......
include_rules = [
# This is needed for error page strings/resources.
# TODO(1024326): If WebLayer stays with WebView's error pages implementation
# long-term, componentize these strings/resources as part of componentizing
# that implementation and remove the need for this dependency.
"+android_webview/grit",
"+cc",
"+components/crash/content/browser",
"+components/embedder_support",
......
// Copyright 2019 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.
#include "weblayer/test/weblayer_browser_test.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "net/test/url_request/url_request_failed_job.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/weblayer_browser_test_utils.h"
#if defined(OS_ANDROID)
#include "android_webview/grit/aw_strings.h"
#include "ui/base/l10n/l10n_util.h"
#endif
namespace weblayer {
using ErrorPageBrowserTest = WebLayerBrowserTest;
IN_PROC_BROWSER_TEST_F(ErrorPageBrowserTest, NameNotResolved) {
GURL error_page_url =
net::URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED);
NavigateAndWaitForFailure(error_page_url, shell());
// Currently, interstitials for error pages are displayed only on Android.
#if defined(OS_ANDROID)
base::string16 expected_title =
l10n_util::GetStringUTF16(IDS_AW_WEBPAGE_NOT_AVAILABLE);
EXPECT_EQ(expected_title, GetTitle(shell()));
#endif
}
} // namespace weblayer
include_rules = [
# This is needed for error page strings/resources.
# TODO(1024326): If WebLayer stays with WebView's error pages implementation
# long-term, componentize these strings/resources as part of componentizing
# that implementation and remove the need for this dependency.
"+android_webview/grit",
"+components/security_interstitials/content/renderer",
"+components/security_interstitials/core/common",
"+content/public/renderer",
"+net/base",
"+third_party/blink/public/common",
"+ui/base",
]
......@@ -4,10 +4,79 @@
#include "weblayer/renderer/content_renderer_client_impl.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "net/base/escape.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "weblayer/renderer/ssl_error_helper.h"
#if defined(OS_ANDROID)
#include "android_webview/grit/aw_resources.h"
#include "android_webview/grit/aw_strings.h"
#endif
namespace weblayer {
namespace {
#if defined(OS_ANDROID)
constexpr char kThrottledErrorDescription[] =
"Request throttled. Visit http://dev.chromium.org/throttling for more "
"information.";
// Populates |error_html| (if it is not null), based on |error|.
// NOTE: This function is taken from
// AWContentRendererClient::PrepareErrorPage().
// TODO(1024326): If this implementation becomes the long-term
// implementation, this code should be shared rather than copied.
void PopulateErrorPageHTML(const blink::WebURLError& error,
std::string* error_html) {
std::string err;
if (error.reason() == net::ERR_TEMPORARILY_THROTTLED)
err = kThrottledErrorDescription;
else
err = net::ErrorToString(error.reason());
if (!error_html)
return;
// Create the error page based on the error reason.
GURL gurl(error.url());
std::string url_string = gurl.possibly_invalid_spec();
int reason_id = IDS_AW_WEBPAGE_CAN_NOT_BE_LOADED;
if (err.empty())
reason_id = IDS_AW_WEBPAGE_TEMPORARILY_DOWN;
std::string escaped_url = net::EscapeForHTML(url_string);
std::vector<std::string> replacements;
replacements.push_back(
l10n_util::GetStringUTF8(IDS_AW_WEBPAGE_NOT_AVAILABLE));
replacements.push_back(
l10n_util::GetStringFUTF8(reason_id, base::UTF8ToUTF16(escaped_url)));
// Having chosen the base reason, chose what extra information to add.
if (reason_id == IDS_AW_WEBPAGE_TEMPORARILY_DOWN) {
replacements.push_back(
l10n_util::GetStringUTF8(IDS_AW_WEBPAGE_TEMPORARILY_DOWN_SUGGESTIONS));
} else {
replacements.push_back(err);
}
if (base::i18n::IsRTL())
replacements.push_back("direction: rtl;");
else
replacements.push_back("");
*error_html = base::ReplaceStringPlaceholders(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
IDR_AW_LOAD_ERROR_HTML),
replacements, nullptr);
}
#endif // OS_ANDROID
} // namespace
ContentRendererClientImpl::ContentRendererClientImpl() = default;
ContentRendererClientImpl::~ContentRendererClientImpl() = default;
......@@ -24,6 +93,10 @@ void ContentRendererClientImpl::PrepareErrorPage(
auto* ssl_helper = SSLErrorHelper::GetForFrame(render_frame);
if (ssl_helper)
ssl_helper->PrepareErrorPage();
#if defined(OS_ANDROID)
PopulateErrorPageHTML(error, error_html);
#endif
}
} // namespace weblayer
......@@ -87,6 +87,7 @@ test("weblayer_browsertests") {
]
sources = [
"../browser/errorpage_browsertest.cc",
"../browser/navigation_browsertest.cc",
"../browser/ssl_browsertest.cc",
"../browser/webui/webui_browsertest.cc",
......@@ -111,6 +112,7 @@ test("weblayer_browsertests") {
deps += [
":weblayer_browsertests_java",
":weblayer_test_assets",
"//android_webview:generate_aw_strings_grit",
"//android_webview:locale_pak_assets",
"//android_webview:pak_file_assets",
"//content/public/test/android:android_test_message_pump_support_java",
......
......@@ -7,6 +7,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "url/gurl.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
......@@ -53,4 +54,10 @@ base::Value ExecuteScript(Shell* shell,
return final_result;
}
const base::string16& GetTitle(Shell* shell) {
TabImpl* tab_impl = static_cast<TabImpl*>(shell->tab());
return tab_impl->web_contents()->GetTitle();
}
} // namespace weblayer
......@@ -5,6 +5,7 @@
#ifndef WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
#define WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
#include "base/strings/string16.h"
#include "base/values.h"
class GURL;
......@@ -23,6 +24,9 @@ base::Value ExecuteScript(Shell* shell,
const std::string& script,
bool use_separate_isolate);
// Gets the title of the current webpage in |shell|.
const base::string16& GetTitle(Shell* shell);
} // namespace weblayer
#endif // WEBLAYER_TEST_WEBLAYER_BROWSER_TEST_UTILS_H_
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