electron_navigation_throttle.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  25. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  26. v8::HandleScope scope(isolate);
  27. api::WebContents* api_contents = api::WebContents::From(contents);
  28. if (!api_contents) {
  29. // No need to emit any event if the WebContents is not available in JS.
  30. return PROCEED;
  31. }
  32. bool is_renderer_initiated = handle->IsRendererInitiated();
  33. // Chrome's internal pages always mark navigation as browser-initiated. Let's
  34. // ignore that.
  35. // https://source.chromium.org/chromium/chromium/src/+/main:content/browser/renderer_host/navigator.cc;l=883-892;drc=0c8ffbe78dc0ef2047849e45cefb3f621043e956
  36. if (!is_renderer_initiated &&
  37. ui::PageTransitionIsWebTriggerable(handle->GetPageTransition())) {
  38. auto* render_frame_host = contents->GetPrimaryMainFrame();
  39. auto* rfh_impl =
  40. static_cast<content::RenderFrameHostImpl*>(render_frame_host);
  41. is_renderer_initiated = rfh_impl && rfh_impl->web_ui();
  42. }
  43. if (is_renderer_initiated &&
  44. api_contents->EmitNavigationEvent("will-frame-navigate", handle)) {
  45. return CANCEL;
  46. }
  47. if (is_renderer_initiated && handle->IsInMainFrame() &&
  48. api_contents->EmitNavigationEvent("will-navigate", handle)) {
  49. return CANCEL;
  50. }
  51. return PROCEED;
  52. }
  53. content::NavigationThrottle::ThrottleCheckResult
  54. ElectronNavigationThrottle::WillRedirectRequest() {
  55. auto* handle = navigation_handle();
  56. auto* contents = handle->GetWebContents();
  57. if (!contents) {
  58. NOTREACHED();
  59. }
  60. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  61. v8::HandleScope scope(isolate);
  62. api::WebContents* api_contents = api::WebContents::From(contents);
  63. if (!api_contents) {
  64. // No need to emit any event if the WebContents is not available in JS.
  65. return PROCEED;
  66. }
  67. if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
  68. return CANCEL;
  69. }
  70. return PROCEED;
  71. }
  72. } // namespace electron