1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/preloading/preview/preview_navigation_throttle.h"
#include <string_view>
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "components/error_page/common/error.h"
#include "components/error_page/common/localized_error.h"
#include "components/grit/components_resources.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/preview_cancel_reason.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/webui/web_ui_util.h"
#include "url/gurl.h"
// static
void PreviewNavigationThrottle::MaybeCreateAndAdd(
content::NavigationThrottleRegistry& registry) {
auto* web_contents = content::WebContents::FromFrameTreeNodeId(
registry.GetNavigationHandle().GetFrameTreeNodeId());
CHECK(web_contents);
if (web_contents->IsInPreviewMode()) {
registry.AddThrottle(
base::WrapUnique(new PreviewNavigationThrottle(registry)));
}
}
PreviewNavigationThrottle::PreviewNavigationThrottle(
content::NavigationThrottleRegistry& registry)
: content::NavigationThrottle(registry) {}
PreviewNavigationThrottle::~PreviewNavigationThrottle() = default;
const char* PreviewNavigationThrottle::GetNameForLogging() {
return "PreviewNavigationThrottle";
}
std::string MakeErrorPage(content::NavigationHandle& navigation_handle,
error_page::LinkPreviewErrorCode error_code) {
const auto error = error_page::Error::LinkPreviewError(
navigation_handle.GetURL(), error_code);
base::Value::Dict error_page_params;
error_page::LocalizedError::PageState page_state =
error_page::LocalizedError::GetPageState(
error.reason(), error.domain(), error.url(),
navigation_handle.IsPost(),
/*is_secure_dns_network_error=*/false,
/*stale_copy_in_cache=*/false,
/*can_show_network_diagnostics_dialog=*/false,
/*is_incognito=*/
Profile::FromBrowserContext(
navigation_handle.GetWebContents()->GetBrowserContext())
->IsIncognitoProfile(),
/*auto_fetch_feature_enabled=*/false,
/*is_kiosk_mode=*/
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceAppMode),
/*locale=*/g_browser_process->GetApplicationLocale(),
/*is_blocked_by_extension=*/false, &error_page_params);
std::string extracted_string =
ui::ResourceBundle::GetSharedInstance().LoadDataResourceString(
IDR_NET_ERROR_HTML);
std::string_view template_html(extracted_string.data(),
extracted_string.size());
CHECK(!template_html.empty()) << "unable to load template.";
return webui::GetLocalizedHtml(template_html, page_state.strings);
}
content::NavigationThrottle::ThrottleCheckResult Cancel(
content::NavigationHandle& navigation_handle,
content::PreviewCancelReason reason,
error_page::LinkPreviewErrorCode error_code) {
auto* web_contents = content::WebContents::FromFrameTreeNodeId(
navigation_handle.GetFrameTreeNodeId());
if (web_contents->GetDelegate()) {
web_contents->GetDelegate()->CancelPreview(std::move(reason));
}
return content::NavigationThrottle::ThrottleCheckResult(
content::NavigationThrottle::CANCEL, net::ERR_BLOCKED_BY_CLIENT,
MakeErrorPage(navigation_handle, error_code));
}
content::NavigationThrottle::ThrottleCheckResult
PreviewNavigationThrottle::WillStartRequest() {
return WillStartRequestOrRedirect();
}
content::NavigationThrottle::ThrottleCheckResult
PreviewNavigationThrottle::WillRedirectRequest() {
return WillStartRequestOrRedirect();
}
content::NavigationThrottle::ThrottleCheckResult
PreviewNavigationThrottle::WillStartRequestOrRedirect() {
if (!navigation_handle()->GetURL().SchemeIs(url::kHttpsScheme)) {
return Cancel(*navigation_handle(),
content::PreviewCancelReason::Build(
content::PreviewFinalStatus::kBlockedByNonHttps),
error_page::LinkPreviewErrorCode::kNonHttpsForbidden);
}
return content::NavigationThrottle::ThrottleAction::PROCEED;
}
|