2018-09-15 15:42:43 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/electron_navigation_throttle.h"
|
2018-09-15 15:42:43 +00:00
|
|
|
|
2023-11-14 22:09:44 +00:00
|
|
|
#include "content/browser/renderer_host/render_frame_host_impl.h" // nogncheck
|
2018-09-15 15:42:43 +00:00
|
|
|
#include "content/public/browser/navigation_handle.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2020-06-22 16:35:24 +00:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2023-11-14 22:09:44 +00:00
|
|
|
#include "ui/base/page_transition_types.h"
|
2018-09-15 15:42:43 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
|
|
|
ElectronNavigationThrottle::ElectronNavigationThrottle(
|
|
|
|
content::NavigationHandle* navigation_handle)
|
|
|
|
: content::NavigationThrottle(navigation_handle) {}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
ElectronNavigationThrottle::~ElectronNavigationThrottle() = default;
|
2018-09-15 15:42:43 +00:00
|
|
|
|
|
|
|
const char* ElectronNavigationThrottle::GetNameForLogging() {
|
|
|
|
return "ElectronNavigationThrottle";
|
|
|
|
}
|
|
|
|
|
2020-08-21 19:34:09 +00:00
|
|
|
content::NavigationThrottle::ThrottleCheckResult
|
|
|
|
ElectronNavigationThrottle::WillStartRequest() {
|
|
|
|
auto* handle = navigation_handle();
|
|
|
|
auto* contents = handle->GetWebContents();
|
|
|
|
if (!contents) {
|
|
|
|
NOTREACHED();
|
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
api::WebContents* api_contents = api::WebContents::From(contents);
|
|
|
|
if (!api_contents) {
|
|
|
|
// No need to emit any event if the WebContents is not available in JS.
|
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
2023-11-14 22:09:44 +00:00
|
|
|
bool is_renderer_initiated = handle->IsRendererInitiated();
|
|
|
|
|
|
|
|
// Chrome's internal pages always mark navigation as browser-initiated. Let's
|
|
|
|
// ignore that.
|
|
|
|
// https://source.chromium.org/chromium/chromium/src/+/main:content/browser/renderer_host/navigator.cc;l=883-892;drc=0c8ffbe78dc0ef2047849e45cefb3f621043e956
|
|
|
|
if (!is_renderer_initiated &&
|
|
|
|
ui::PageTransitionIsWebTriggerable(handle->GetPageTransition())) {
|
|
|
|
auto* render_frame_host = contents->GetPrimaryMainFrame();
|
|
|
|
auto* rfh_impl =
|
|
|
|
static_cast<content::RenderFrameHostImpl*>(render_frame_host);
|
|
|
|
is_renderer_initiated = rfh_impl && rfh_impl->web_ui();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_renderer_initiated &&
|
2023-03-28 14:55:41 +00:00
|
|
|
api_contents->EmitNavigationEvent("will-frame-navigate", handle)) {
|
|
|
|
return CANCEL;
|
|
|
|
}
|
2023-11-14 22:09:44 +00:00
|
|
|
if (is_renderer_initiated && handle->IsInMainFrame() &&
|
2020-08-21 19:34:09 +00:00
|
|
|
api_contents->EmitNavigationEvent("will-navigate", handle)) {
|
|
|
|
return CANCEL;
|
|
|
|
}
|
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:42:43 +00:00
|
|
|
content::NavigationThrottle::ThrottleCheckResult
|
|
|
|
ElectronNavigationThrottle::WillRedirectRequest() {
|
|
|
|
auto* handle = navigation_handle();
|
|
|
|
auto* contents = handle->GetWebContents();
|
|
|
|
if (!contents) {
|
|
|
|
NOTREACHED();
|
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-03-11 01:16:58 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2020-07-16 23:16:05 +00:00
|
|
|
api::WebContents* api_contents = api::WebContents::From(contents);
|
|
|
|
if (!api_contents) {
|
2018-10-19 08:52:07 +00:00
|
|
|
// No need to emit any event if the WebContents is not available in JS.
|
2018-09-15 15:42:43 +00:00
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
|
|
|
|
return CANCEL;
|
|
|
|
}
|
|
|
|
return PROCEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|