2020-09-25 03:40:47 +02:00
|
|
|
// Copyright 2020 The Chromium Embedded Framework 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 "libcef/browser/net/throttle_handler.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_host_base.h"
|
|
|
|
#include "libcef/browser/browser_info_manager.h"
|
|
|
|
#include "libcef/browser/frame_host_impl.h"
|
2021-08-19 23:07:44 +02:00
|
|
|
#include "libcef/common/frame_util.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "libcef/common/request_impl.h"
|
|
|
|
|
|
|
|
#include "components/navigation_interception/intercept_navigation_throttle.h"
|
|
|
|
#include "content/public/browser/navigation_handle.h"
|
|
|
|
#include "content/public/browser/navigation_throttle.h"
|
|
|
|
#include "content/public/browser/page_navigator.h"
|
|
|
|
|
|
|
|
namespace throttle {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
bool NavigationOnUIThread(content::NavigationHandle* navigation_handle) {
|
2020-09-25 03:40:47 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
2022-03-26 02:12:30 +01:00
|
|
|
const bool is_main_frame = navigation_handle->IsInMainFrame();
|
|
|
|
const auto global_id = frame_util::GetGlobalId(navigation_handle);
|
|
|
|
|
|
|
|
// Identify the RenderFrameHost that originated the navigation.
|
|
|
|
const auto parent_global_id =
|
|
|
|
!is_main_frame ? navigation_handle->GetParentFrame()->GetGlobalId()
|
|
|
|
: frame_util::InvalidGlobalId();
|
|
|
|
|
|
|
|
const content::Referrer referrer(navigation_handle->GetReferrer().url,
|
|
|
|
navigation_handle->GetReferrer().policy);
|
|
|
|
|
|
|
|
content::OpenURLParams open_params(navigation_handle->GetURL(), referrer,
|
|
|
|
WindowOpenDisposition::CURRENT_TAB,
|
|
|
|
navigation_handle->GetPageTransition(),
|
|
|
|
navigation_handle->IsRendererInitiated());
|
|
|
|
open_params.user_gesture = navigation_handle->HasUserGesture();
|
|
|
|
open_params.initiator_origin = navigation_handle->GetInitiatorOrigin();
|
|
|
|
open_params.is_pdf = navigation_handle->IsPdf();
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefBrowserHostBase> browser;
|
|
|
|
if (!CefBrowserInfoManager::GetInstance()->MaybeAllowNavigation(
|
2022-07-21 19:26:10 +02:00
|
|
|
navigation_handle->GetWebContents()->GetPrimaryMainFrame(),
|
|
|
|
open_params, browser)) {
|
2020-09-25 03:40:47 +02:00
|
|
|
// Cancel the navigation.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ignore_navigation = false;
|
|
|
|
|
|
|
|
if (browser) {
|
|
|
|
if (auto client = browser->GetClient()) {
|
|
|
|
if (auto handler = client->GetRequestHandler()) {
|
|
|
|
CefRefPtr<CefFrame> frame;
|
|
|
|
if (is_main_frame) {
|
|
|
|
frame = browser->GetMainFrame();
|
2021-08-19 23:07:44 +02:00
|
|
|
} else {
|
|
|
|
frame = browser->GetFrameForGlobalId(global_id);
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
if (!frame) {
|
|
|
|
// Create a temporary frame object for navigation of sub-frames that
|
|
|
|
// don't yet exist.
|
2021-08-19 23:07:44 +02:00
|
|
|
frame = browser->browser_info()->CreateTempSubFrame(parent_global_id);
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequestImpl> request = new CefRequestImpl();
|
2022-03-26 02:12:30 +01:00
|
|
|
request->Set(navigation_handle);
|
2020-09-25 03:40:47 +02:00
|
|
|
request->SetReadOnly(true);
|
|
|
|
|
|
|
|
// Initiating a new navigation in OnBeforeBrowse will delete the
|
|
|
|
// InterceptNavigationThrottle that currently owns this callback,
|
|
|
|
// resulting in a crash. Use the lock to prevent that.
|
|
|
|
auto navigation_lock = browser->browser_info()->CreateNavigationLock();
|
2022-03-26 02:12:30 +01:00
|
|
|
ignore_navigation =
|
|
|
|
handler->OnBeforeBrowse(browser.get(), frame, request.get(),
|
|
|
|
navigation_handle->HasUserGesture(),
|
|
|
|
navigation_handle->WasServerRedirect());
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ignore_navigation;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void CreateThrottlesForNavigation(content::NavigationHandle* navigation_handle,
|
|
|
|
NavigationThrottleList& throttles) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
// Must use SynchronyMode::kSync to ensure that OnBeforeBrowse is always
|
|
|
|
// called before OnBeforeResourceLoad.
|
|
|
|
std::unique_ptr<content::NavigationThrottle> throttle =
|
|
|
|
std::make_unique<navigation_interception::InterceptNavigationThrottle>(
|
2022-03-26 02:12:30 +01:00
|
|
|
navigation_handle, base::BindRepeating(&NavigationOnUIThread),
|
2020-09-25 03:40:47 +02:00
|
|
|
navigation_interception::SynchronyMode::kSync);
|
|
|
|
throttles.push_back(std::move(throttle));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace throttle
|