navigation_error_navigation_throttle.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/browser/navigation_error_navigation_throttle.h"
  5. #include "content/public/browser/navigation_handle.h"
  6. #include "content/public/browser/render_frame_host.h"
  7. #include "net/base/net_errors.h"
  8. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  9. #include "weblayer/browser/navigation_controller_impl.h"
  10. #include "weblayer/browser/tab_impl.h"
  11. #include "weblayer/common/error_page_helper.mojom.h"
  12. #include "weblayer/public/error_page.h"
  13. #include "weblayer/public/error_page_delegate.h"
  14. using content::NavigationThrottle;
  15. namespace weblayer {
  16. NavigationErrorNavigationThrottle::NavigationErrorNavigationThrottle(
  17. content::NavigationHandle* handle)
  18. : NavigationThrottle(handle) {
  19. // As this calls to the delegate, and the delegate only knows about main
  20. // frames, this should only be used for main frames.
  21. DCHECK(handle->IsInMainFrame());
  22. }
  23. NavigationErrorNavigationThrottle::~NavigationErrorNavigationThrottle() =
  24. default;
  25. NavigationThrottle::ThrottleCheckResult
  26. NavigationErrorNavigationThrottle::WillFailRequest() {
  27. // The embedder is not allowed to replace ssl error pages.
  28. if (navigation_handle()->GetNetErrorCode() == net::Error::OK ||
  29. net::IsCertificateError(navigation_handle()->GetNetErrorCode())) {
  30. return NavigationThrottle::PROCEED;
  31. }
  32. TabImpl* tab =
  33. TabImpl::FromWebContents(navigation_handle()->GetWebContents());
  34. // Instances of this class are only created if there is a Tab associated
  35. // with the WebContents.
  36. DCHECK(tab);
  37. if (!tab->error_page_delegate())
  38. return NavigationThrottle::PROCEED;
  39. NavigationImpl* navigation =
  40. static_cast<NavigationControllerImpl*>(tab->GetNavigationController())
  41. ->GetNavigationImplFromHandle(navigation_handle());
  42. // The navigation this was created for should always outlive this.
  43. DCHECK(navigation);
  44. auto error_page = tab->error_page_delegate()->GetErrorPageContent(navigation);
  45. if (!error_page)
  46. return NavigationThrottle::PROCEED;
  47. mojo::AssociatedRemote<mojom::ErrorPageHelper> remote_error_page_helper;
  48. navigation_handle()
  49. ->GetRenderFrameHost()
  50. ->GetRemoteAssociatedInterfaces()
  51. ->GetInterface(&remote_error_page_helper);
  52. remote_error_page_helper->DisableErrorPageHelperForNextError();
  53. return NavigationThrottle::ThrottleCheckResult(
  54. NavigationThrottle::BLOCK_REQUEST, navigation_handle()->GetNetErrorCode(),
  55. error_page->html);
  56. }
  57. const char* NavigationErrorNavigationThrottle::GetNameForLogging() {
  58. return "NavigationErrorNavigationThrottle";
  59. }
  60. } // namespace weblayer