electron/shell/browser/electron_navigation_throttle.cc

90 lines
2.9 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/electron_navigation_throttle.h"
#include "content/browser/renderer_host/render_frame_host_impl.h" // nogncheck
#include "content/public/browser/navigation_handle.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/javascript_environment.h"
#include "ui/base/page_transition_types.h"
namespace electron {
ElectronNavigationThrottle::ElectronNavigationThrottle(
content::NavigationHandle* navigation_handle)
: content::NavigationThrottle(navigation_handle) {}
ElectronNavigationThrottle::~ElectronNavigationThrottle() = default;
const char* ElectronNavigationThrottle::GetNameForLogging() {
return "ElectronNavigationThrottle";
}
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;
}
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 &&
feat: add `will-frame-navigate` event (#34418) * feat: add will-navigate-in-frame event to webContents * docs: add documentation for webview will-frame-navigate event * feat: Eliminate isInPlace argument from will-frame-navigate event * fix: Fire will-frame-navigate before will-navigate * feat: send will-frame-navigate with a WebFrameMain in the event details * docs: Update WebContents docs for new API signature * feat: Add custom event forwarding for <webview> will-frame-navigate * fix: wrap WebFrameMain so it can be sent as an event * test: update webContents and <webview> tests to match new signatures * chore: undo unnecessary change * fix: don't switch will-navigate to use EmitNavigationEventDetails * test: clean up will-navigate and will-frame-navigate tests for <webview> * chore: apply lint fixes * chore: move GetRenderFrameHost helper into anonymous namespace * docs: auto-generate WillFrameNavigateDetails rather than defining it manually * test: Update <webview> tests to actually pass under new spec runner * docs: Add section explaining relationship between various nav events * test: Add some tests to ensure navigation event order doesn't silently change * test: Always monitor all nav events to ensure unexpected ones don't fire * test: Add test to verify in-page navigation event order * feat: Change to new style where extra params are exposed as event props * fix: Remove unused EmitNavigationEventDetails * fix: Update tests to use new async helpers * docs: Rename and reorder sections documenting navigation events --------- Co-authored-by: Milan Burda <milan.burda@gmail.com>
2023-03-28 14:55:41 +00:00
api_contents->EmitNavigationEvent("will-frame-navigate", handle)) {
return CANCEL;
}
if (is_renderer_initiated && handle->IsInMainFrame() &&
api_contents->EmitNavigationEvent("will-navigate", handle)) {
return CANCEL;
}
return PROCEED;
}
content::NavigationThrottle::ThrottleCheckResult
ElectronNavigationThrottle::WillRedirectRequest() {
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;
}
if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
return CANCEL;
}
return PROCEED;
}
} // namespace electron