1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
fuchsia_web / webengine / browser / navigation_policy_throttle.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "fuchsia_web/webengine/browser/navigation_policy_throttle.h"
#include "content/public/browser/navigation_handle.h"
#include "fuchsia_web/webengine/browser/navigation_policy_handler.h"
namespace {
fuchsia::web::RequestedNavigation ToRequestedNavigation(
content::NavigationHandle* handle,
fuchsia::web::NavigationPhase phase) {
fuchsia::web::RequestedNavigation event;
event.set_id(static_cast<uint64_t>(handle->GetNavigationId()));
event.set_phase(phase);
event.set_is_main_frame(handle->IsInMainFrame());
event.set_is_same_document(handle->IsSameDocument());
event.set_is_http_post(handle->IsPost());
event.set_url(handle->GetURL().spec());
event.set_has_gesture(handle->HasUserGesture());
event.set_was_server_redirect(handle->WasServerRedirect());
return event;
}
} // namespace
NavigationPolicyThrottle::NavigationPolicyThrottle(
content::NavigationHandle* handle,
NavigationPolicyHandler* policy_handler)
: NavigationThrottle(handle),
policy_handler_(policy_handler),
navigation_handle_(handle) {
if (policy_handler->is_provider_connected()) {
policy_handler_->RegisterNavigationThrottle(this);
} else {
policy_handler_ = nullptr;
}
}
NavigationPolicyThrottle::~NavigationPolicyThrottle() {
if (policy_handler_)
policy_handler_->RemoveNavigationThrottle(this);
}
void NavigationPolicyThrottle::OnNavigationPolicyProviderDisconnected(
content::NavigationThrottle::ThrottleCheckResult check_result) {
policy_handler_ = nullptr;
if (is_paused_) {
is_paused_ = false;
CancelDeferredNavigation(check_result);
// DO NOT ADD CODE after this. The callback above will destroy the
// NavigationHandle that owns this NavigationThrottle.
}
}
void NavigationPolicyThrottle::OnRequestedNavigationEvaluated(
fuchsia::web::NavigationDecision decision) {
DCHECK(is_paused_);
is_paused_ = false;
switch (decision.Which()) {
case fuchsia::web::NavigationDecision::kProceed:
Resume();
// DO NOT ADD CODE after this. The callback above might have destroyed
// the NavigationHandle that owns this NavigationThrottle.
break;
case fuchsia::web::NavigationDecision::kAbort:
CancelDeferredNavigation(content::NavigationThrottle::CANCEL);
// DO NOT ADD CODE after this. The callback above will destroy the
// NavigationHandle that owns this NavigationThrottle.
break;
default:
NOTREACHED();
}
}
content::NavigationThrottle::ThrottleCheckResult
NavigationPolicyThrottle::WillStartRequest() {
return HandleNavigationPhase(fuchsia::web::NavigationPhase::START);
}
content::NavigationThrottle::ThrottleCheckResult
NavigationPolicyThrottle::WillRedirectRequest() {
return HandleNavigationPhase(fuchsia::web::NavigationPhase::REDIRECT);
}
content::NavigationThrottle::ThrottleCheckResult
NavigationPolicyThrottle::WillFailRequest() {
return HandleNavigationPhase(fuchsia::web::NavigationPhase::FAIL);
}
content::NavigationThrottle::ThrottleCheckResult
NavigationPolicyThrottle::WillProcessResponse() {
return HandleNavigationPhase(fuchsia::web::NavigationPhase::PROCESS_RESPONSE);
}
const char* NavigationPolicyThrottle::GetNameForLogging() {
return "NavigationPolicyThrottle";
}
content::NavigationThrottle::ThrottleCheckResult
NavigationPolicyThrottle::HandleNavigationPhase(
fuchsia::web::NavigationPhase phase) {
DCHECK(!is_paused_);
if (!policy_handler_) {
return content::NavigationThrottle::ThrottleCheckResult(
content::NavigationThrottle::CANCEL);
}
if (!policy_handler_->ShouldEvaluateNavigation(navigation_handle_, phase)) {
return content::NavigationThrottle::ThrottleCheckResult(
content::NavigationThrottle::PROCEED);
}
policy_handler_->EvaluateRequestedNavigation(
ToRequestedNavigation(navigation_handle_, phase),
[weak_this = weak_factory_.GetWeakPtr()](auto decision) {
if (weak_this)
weak_this->OnRequestedNavigationEvaluated(std::move(decision));
});
is_paused_ = true;
return content::NavigationThrottle::ThrottleCheckResult(
content::NavigationThrottle::DEFER);
}