electron_navigation_throttle.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/electron_navigation_throttle.h"
  5. #include "content/browser/renderer_host/render_frame_host_impl.h" // nogncheck
  6. #include "content/public/browser/navigation_handle.h"
  7. #include "shell/browser/api/electron_api_web_contents.h"
  8. #include "shell/browser/javascript_environment.h"
  9. #include "ui/base/page_transition_types.h"
  10. namespace electron {
  11. ElectronNavigationThrottle::ElectronNavigationThrottle(
  12. content::NavigationHandle* navigation_handle)
  13. : content::NavigationThrottle(navigation_handle) {}
  14. ElectronNavigationThrottle::~ElectronNavigationThrottle() = default;
  15. const char* ElectronNavigationThrottle::GetNameForLogging() {
  16. return "ElectronNavigationThrottle";
  17. }
  18. content::NavigationThrottle::ThrottleCheckResult
  19. ElectronNavigationThrottle::WillStartRequest() {
  20. auto* handle = navigation_handle();
  21. auto* contents = handle->GetWebContents();
  22. if (!contents) {
  23. NOTREACHED();
  24. return PROCEED;
  25. }
  26. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  27. v8::HandleScope scope(isolate);
  28. api::WebContents* api_contents = api::WebContents::From(contents);
  29. if (!api_contents) {
  30. // No need to emit any event if the WebContents is not available in JS.
  31. return PROCEED;
  32. }
  33. bool is_renderer_initiated = handle->IsRendererInitiated();
  34. // Chrome's internal pages always mark navigation as browser-initiated. Let's
  35. // ignore that.
  36. // https://source.chromium.org/chromium/chromium/src/+/main:content/browser/renderer_host/navigator.cc;l=883-892;drc=0c8ffbe78dc0ef2047849e45cefb3f621043e956
  37. if (!is_renderer_initiated &&
  38. ui::PageTransitionIsWebTriggerable(handle->GetPageTransition())) {
  39. auto* render_frame_host = contents->GetPrimaryMainFrame();
  40. auto* rfh_impl =
  41. static_cast<content::RenderFrameHostImpl*>(render_frame_host);
  42. is_renderer_initiated = rfh_impl && rfh_impl->web_ui();
  43. }
  44. if (is_renderer_initiated &&
  45. api_contents->EmitNavigationEvent("will-frame-navigate", handle)) {
  46. return CANCEL;
  47. }
  48. if (is_renderer_initiated && handle->IsInMainFrame() &&
  49. api_contents->EmitNavigationEvent("will-navigate", handle)) {
  50. return CANCEL;
  51. }
  52. return PROCEED;
  53. }
  54. content::NavigationThrottle::ThrottleCheckResult
  55. ElectronNavigationThrottle::WillRedirectRequest() {
  56. auto* handle = navigation_handle();
  57. auto* contents = handle->GetWebContents();
  58. if (!contents) {
  59. NOTREACHED();
  60. return PROCEED;
  61. }
  62. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  63. v8::HandleScope scope(isolate);
  64. api::WebContents* api_contents = api::WebContents::From(contents);
  65. if (!api_contents) {
  66. // No need to emit any event if the WebContents is not available in JS.
  67. return PROCEED;
  68. }
  69. if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
  70. return CANCEL;
  71. }
  72. return PROCEED;
  73. }
  74. } // namespace electron