electron_navigation_throttle.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/public/browser/navigation_handle.h"
  6. #include "shell/browser/api/electron_api_web_contents.h"
  7. #include "shell/browser/javascript_environment.h"
  8. namespace electron {
  9. ElectronNavigationThrottle::ElectronNavigationThrottle(
  10. content::NavigationHandle* navigation_handle)
  11. : content::NavigationThrottle(navigation_handle) {}
  12. ElectronNavigationThrottle::~ElectronNavigationThrottle() = default;
  13. const char* ElectronNavigationThrottle::GetNameForLogging() {
  14. return "ElectronNavigationThrottle";
  15. }
  16. content::NavigationThrottle::ThrottleCheckResult
  17. ElectronNavigationThrottle::WillStartRequest() {
  18. auto* handle = navigation_handle();
  19. auto* contents = handle->GetWebContents();
  20. if (!contents) {
  21. NOTREACHED();
  22. return PROCEED;
  23. }
  24. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  25. v8::HandleScope scope(isolate);
  26. api::WebContents* api_contents = api::WebContents::From(contents);
  27. if (!api_contents) {
  28. // No need to emit any event if the WebContents is not available in JS.
  29. return PROCEED;
  30. }
  31. if (handle->IsRendererInitiated() &&
  32. api_contents->EmitNavigationEvent("will-frame-navigate", handle)) {
  33. return CANCEL;
  34. }
  35. if (handle->IsRendererInitiated() && handle->IsInMainFrame() &&
  36. api_contents->EmitNavigationEvent("will-navigate", handle)) {
  37. return CANCEL;
  38. }
  39. return PROCEED;
  40. }
  41. content::NavigationThrottle::ThrottleCheckResult
  42. ElectronNavigationThrottle::WillRedirectRequest() {
  43. auto* handle = navigation_handle();
  44. auto* contents = handle->GetWebContents();
  45. if (!contents) {
  46. NOTREACHED();
  47. return PROCEED;
  48. }
  49. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  50. v8::HandleScope scope(isolate);
  51. api::WebContents* api_contents = api::WebContents::From(contents);
  52. if (!api_contents) {
  53. // No need to emit any event if the WebContents is not available in JS.
  54. return PROCEED;
  55. }
  56. if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
  57. return CANCEL;
  58. }
  59. return PROCEED;
  60. }
  61. } // namespace electron