-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathagent_browser_navigation_state.cpp
More file actions
125 lines (112 loc) · 4.5 KB
/
Copy pathagent_browser_navigation_state.cpp
File metadata and controls
125 lines (112 loc) · 4.5 KB
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
#include "agent_browser_navigation_state.hpp"
namespace acecode::desktop {
namespace {
// NSURLErrorCancelled。这里用字面量而不是 Foundation 常量,是为了让本文件在
// 非 Apple 平台上也能编译进单测目标。
constexpr long long kDarwinErrorCancelled = -999;
} // namespace
bool AgentBrowserNavigationTracker::is_current(
std::uint64_t navigation_id) const {
if (closed_) return false;
if (navigation_id == 0 || current_id_ == 0) return true;
return navigation_id == current_id_;
}
void AgentBrowserNavigationTracker::begin_navigation(
std::uint64_t navigation_id) {
if (closed_) return;
if (navigation_id != 0 && navigation_id == current_id_) return;
current_id_ = navigation_id;
certificate_allowed_ = false;
pending_certificate_failure_ = false;
external_handoff_pending_ = false;
certificate_retry_used_ = carry_retry_marker_;
carry_retry_marker_ = false;
}
void AgentBrowserNavigationTracker::note_external_handoff(
std::uint64_t navigation_id) {
if (!is_current(navigation_id)) return;
external_handoff_pending_ = true;
}
void AgentBrowserNavigationTracker::close() {
closed_ = true;
pending_certificate_failure_ = false;
external_handoff_pending_ = false;
}
BrowserNavigationDecision
AgentBrowserNavigationTracker::on_certificate_allowed(
std::uint64_t navigation_id) {
if (!is_current(navigation_id)) return BrowserNavigationDecision::kIgnore;
certificate_allowed_ = true;
if (pending_certificate_failure_ && !certificate_retry_used_) {
pending_certificate_failure_ = false;
certificate_retry_used_ = true;
carry_retry_marker_ = true;
return BrowserNavigationDecision::kRetryOnce;
}
return BrowserNavigationDecision::kIgnore;
}
BrowserNavigationDecision
AgentBrowserNavigationTracker::on_navigation_completed(
std::uint64_t navigation_id, BrowserNavigationFailure failure) {
if (!is_current(navigation_id)) return BrowserNavigationDecision::kIgnore;
switch (failure) {
case BrowserNavigationFailure::kNone:
certificate_allowed_ = false;
certificate_retry_used_ = false;
pending_certificate_failure_ = false;
external_handoff_pending_ = false;
carry_retry_marker_ = false;
return BrowserNavigationDecision::kPublishSuccess;
case BrowserNavigationFailure::kExternalHandoff:
external_handoff_pending_ = false;
return BrowserNavigationDecision::kRestorePrevious;
case BrowserNavigationFailure::kCancelled:
external_handoff_pending_ = false;
return BrowserNavigationDecision::kRestorePrevious;
case BrowserNavigationFailure::kCertificate:
if (certificate_retry_used_) {
return BrowserNavigationDecision::kPublishFailure;
}
if (certificate_allowed_) {
pending_certificate_failure_ = false;
certificate_retry_used_ = true;
carry_retry_marker_ = true;
return BrowserNavigationDecision::kRetryOnce;
}
pending_certificate_failure_ = true;
return BrowserNavigationDecision::kHoldPending;
case BrowserNavigationFailure::kTerminal:
default:
return BrowserNavigationDecision::kPublishFailure;
}
}
BrowserNavigationFailure classify_windows_navigation_failure(
const std::string& failure_kind, bool external_handoff_pending) {
if (failure_kind.empty()) return BrowserNavigationFailure::kNone;
if (failure_kind == "certificate") {
return BrowserNavigationFailure::kCertificate;
}
if (failure_kind == "cancelled") {
return external_handoff_pending
? BrowserNavigationFailure::kExternalHandoff
: BrowserNavigationFailure::kCancelled;
}
if (failure_kind == "connection_aborted") {
// 外部 URI 交接后 WebView2 报的就是 CONNECTION_ABORTED;没有待处理的
// 交接时它是真实的连接中止。
return external_handoff_pending
? BrowserNavigationFailure::kExternalHandoff
: BrowserNavigationFailure::kTerminal;
}
return BrowserNavigationFailure::kTerminal;
}
BrowserNavigationFailure classify_darwin_navigation_failure(
long long url_error_code, bool external_handoff_pending) {
if (url_error_code == kDarwinErrorCancelled) {
return external_handoff_pending
? BrowserNavigationFailure::kExternalHandoff
: BrowserNavigationFailure::kCancelled;
}
return BrowserNavigationFailure::kTerminal;
}
} // namespace acecode::desktop