electron_navigation_throttle.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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() && handle->IsInMainFrame() &&
  32. api_contents->EmitNavigationEvent("will-navigate", handle)) {
  33. return CANCEL;
  34. }
  35. return PROCEED;
  36. }
  37. content::NavigationThrottle::ThrottleCheckResult
  38. ElectronNavigationThrottle::WillRedirectRequest() {
  39. auto* handle = navigation_handle();
  40. auto* contents = handle->GetWebContents();
  41. if (!contents) {
  42. NOTREACHED();
  43. return PROCEED;
  44. }
  45. v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
  46. v8::HandleScope scope(isolate);
  47. api::WebContents* api_contents = api::WebContents::From(contents);
  48. if (!api_contents) {
  49. // No need to emit any event if the WebContents is not available in JS.
  50. return PROCEED;
  51. }
  52. if (api_contents->EmitNavigationEvent("will-redirect", handle)) {
  53. return CANCEL;
  54. }
  55. return PROCEED;
  56. }
  57. } // namespace electron