aw_safe_browsing_navigation_throttle.cc 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2019 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 "android_webview/browser/safe_browsing/aw_safe_browsing_navigation_throttle.h"
  5. #include <memory>
  6. #include "android_webview/browser/aw_browser_process.h"
  7. #include "android_webview/browser/network_service/aw_web_resource_request.h"
  8. #include "android_webview/browser/safe_browsing/aw_safe_browsing_blocking_page.h"
  9. #include "android_webview/browser/safe_browsing/aw_safe_browsing_ui_manager.h"
  10. #include "base/memory/ptr_util.h"
  11. #include "components/security_interstitials/content/security_interstitial_tab_helper.h"
  12. #include "components/security_interstitials/core/unsafe_resource.h"
  13. #include "content/public/browser/navigation_handle.h"
  14. namespace android_webview {
  15. using safe_browsing::ThreatSeverity;
  16. // static
  17. std::unique_ptr<AwSafeBrowsingNavigationThrottle>
  18. AwSafeBrowsingNavigationThrottle::MaybeCreateThrottleFor(
  19. content::NavigationHandle* handle) {
  20. // Only outer-most main frames show the interstitial through the navigation
  21. // throttle. In other cases, the interstitial is shown via
  22. // BaseUIManager::DisplayBlockingPage.
  23. if (!handle->IsInPrimaryMainFrame() && !handle->IsInPrerenderedMainFrame())
  24. return nullptr;
  25. return base::WrapUnique(new AwSafeBrowsingNavigationThrottle(handle));
  26. }
  27. AwSafeBrowsingNavigationThrottle::AwSafeBrowsingNavigationThrottle(
  28. content::NavigationHandle* handle)
  29. : content::NavigationThrottle(handle) {}
  30. const char* AwSafeBrowsingNavigationThrottle::GetNameForLogging() {
  31. return "AwSafeBrowsingNavigationThrottle";
  32. }
  33. content::NavigationThrottle::ThrottleCheckResult
  34. AwSafeBrowsingNavigationThrottle::WillFailRequest() {
  35. // Subframes and nested frame trees will show an interstitial directly from
  36. // BaseUIManager::DisplayBlockingPage.
  37. DCHECK(navigation_handle()->IsInPrimaryMainFrame() ||
  38. navigation_handle()->IsInPrerenderedMainFrame());
  39. AwSafeBrowsingUIManager* manager =
  40. AwBrowserProcess::GetInstance()->GetSafeBrowsingUIManager();
  41. if (manager) {
  42. // Goes over |RedirectChain| to get the severest threat information
  43. security_interstitials::UnsafeResource resource;
  44. content::NavigationHandle* handle = navigation_handle();
  45. ThreatSeverity severity =
  46. manager->GetSeverestThreatForNavigation(handle, resource);
  47. // Unsafe resource will show a blocking page
  48. if (severity != std::numeric_limits<ThreatSeverity>::max() &&
  49. resource.threat_type !=
  50. safe_browsing::SBThreatType::SB_THREAT_TYPE_SAFE) {
  51. std::unique_ptr<AwWebResourceRequest> request =
  52. std::make_unique<AwWebResourceRequest>(
  53. handle->GetURL().spec(), handle->IsPost() ? "POST" : "GET",
  54. /*is_in_outermost_main_frame=*/true, handle->HasUserGesture(),
  55. handle->GetRequestHeaders());
  56. request->is_renderer_initiated = handle->IsRendererInitiated();
  57. AwSafeBrowsingBlockingPage* blocking_page =
  58. AwSafeBrowsingBlockingPage::CreateBlockingPage(
  59. manager, handle->GetWebContents(), handle->GetURL(), resource,
  60. std::move(request));
  61. std::string error_page_content = blocking_page->GetHTMLContents();
  62. security_interstitials::SecurityInterstitialTabHelper::
  63. AssociateBlockingPage(handle, base::WrapUnique(blocking_page));
  64. return content::NavigationThrottle::ThrottleCheckResult(
  65. CANCEL, net::ERR_BLOCKED_BY_CLIENT, error_page_content);
  66. }
  67. }
  68. return content::NavigationThrottle::PROCEED;
  69. }
  70. } // namespace android_webview