navigation_policy_throttle.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "fuchsia_web/webengine/browser/navigation_policy_throttle.h"
  5. #include "content/public/browser/navigation_handle.h"
  6. #include "fuchsia_web/webengine/browser/navigation_policy_handler.h"
  7. namespace {
  8. fuchsia::web::RequestedNavigation ToRequestedNavigation(
  9. content::NavigationHandle* handle,
  10. fuchsia::web::NavigationPhase phase) {
  11. fuchsia::web::RequestedNavigation event;
  12. event.set_id(static_cast<uint64_t>(handle->GetNavigationId()));
  13. event.set_phase(phase);
  14. event.set_is_main_frame(handle->IsInMainFrame());
  15. event.set_is_same_document(handle->IsSameDocument());
  16. event.set_is_http_post(handle->IsPost());
  17. event.set_url(handle->GetURL().spec());
  18. event.set_has_gesture(handle->HasUserGesture());
  19. event.set_was_server_redirect(handle->WasServerRedirect());
  20. return event;
  21. }
  22. } // namespace
  23. NavigationPolicyThrottle::NavigationPolicyThrottle(
  24. content::NavigationHandle* handle,
  25. NavigationPolicyHandler* policy_handler)
  26. : NavigationThrottle(handle),
  27. policy_handler_(policy_handler),
  28. navigation_handle_(handle) {
  29. if (policy_handler->is_provider_connected()) {
  30. policy_handler_->RegisterNavigationThrottle(this);
  31. } else {
  32. policy_handler_ = nullptr;
  33. }
  34. }
  35. NavigationPolicyThrottle::~NavigationPolicyThrottle() {
  36. if (policy_handler_)
  37. policy_handler_->RemoveNavigationThrottle(this);
  38. }
  39. void NavigationPolicyThrottle::OnNavigationPolicyProviderDisconnected(
  40. content::NavigationThrottle::ThrottleCheckResult check_result) {
  41. if (is_paused_) {
  42. CancelDeferredNavigation(check_result);
  43. is_paused_ = false;
  44. }
  45. policy_handler_ = nullptr;
  46. }
  47. void NavigationPolicyThrottle::OnRequestedNavigationEvaluated(
  48. fuchsia::web::NavigationDecision decision) {
  49. DCHECK(is_paused_);
  50. switch (decision.Which()) {
  51. case fuchsia::web::NavigationDecision::kProceed:
  52. Resume();
  53. break;
  54. case fuchsia::web::NavigationDecision::kAbort:
  55. CancelDeferredNavigation(content::NavigationThrottle::CANCEL);
  56. break;
  57. default:
  58. NOTREACHED();
  59. }
  60. is_paused_ = false;
  61. }
  62. content::NavigationThrottle::ThrottleCheckResult
  63. NavigationPolicyThrottle::WillStartRequest() {
  64. return HandleNavigationPhase(fuchsia::web::NavigationPhase::START);
  65. }
  66. content::NavigationThrottle::ThrottleCheckResult
  67. NavigationPolicyThrottle::WillRedirectRequest() {
  68. return HandleNavigationPhase(fuchsia::web::NavigationPhase::REDIRECT);
  69. }
  70. content::NavigationThrottle::ThrottleCheckResult
  71. NavigationPolicyThrottle::WillFailRequest() {
  72. return HandleNavigationPhase(fuchsia::web::NavigationPhase::FAIL);
  73. }
  74. content::NavigationThrottle::ThrottleCheckResult
  75. NavigationPolicyThrottle::WillProcessResponse() {
  76. return HandleNavigationPhase(fuchsia::web::NavigationPhase::PROCESS_RESPONSE);
  77. }
  78. const char* NavigationPolicyThrottle::GetNameForLogging() {
  79. return "NavigationPolicyThrottle";
  80. }
  81. content::NavigationThrottle::ThrottleCheckResult
  82. NavigationPolicyThrottle::HandleNavigationPhase(
  83. fuchsia::web::NavigationPhase phase) {
  84. DCHECK(!is_paused_);
  85. if (!policy_handler_) {
  86. return content::NavigationThrottle::ThrottleCheckResult(
  87. content::NavigationThrottle::CANCEL);
  88. }
  89. if (!policy_handler_->ShouldEvaluateNavigation(navigation_handle_, phase)) {
  90. return content::NavigationThrottle::ThrottleCheckResult(
  91. content::NavigationThrottle::PROCEED);
  92. }
  93. policy_handler_->EvaluateRequestedNavigation(
  94. ToRequestedNavigation(navigation_handle_, phase),
  95. [weak_this = weak_factory_.GetWeakPtr()](auto decision) {
  96. if (weak_this)
  97. weak_this->OnRequestedNavigationEvaluated(std::move(decision));
  98. });
  99. is_paused_ = true;
  100. return content::NavigationThrottle::ThrottleCheckResult(
  101. content::NavigationThrottle::DEFER);
  102. }