17
17
// ([email protected] )'s work (https://github.com/bsclifton/winvpntool).
18
18
19
19
using brave_vpn::internal::CheckConnectionResult;
20
+ using brave_vpn::internal::CloseEventHandleForConnecting;
20
21
using brave_vpn::internal::CreateEntry;
22
+ using brave_vpn::internal::GetEventHandleForConnecting;
21
23
using brave_vpn::internal::GetPhonebookPath;
22
24
using brave_vpn::internal::PrintRasError;
23
25
using brave_vpn::internal::RemoveEntry;
@@ -47,7 +49,9 @@ BraveVPNOSConnectionAPIWin::BraveVPNOSConnectionAPIWin() {
47
49
}
48
50
49
51
BraveVPNOSConnectionAPIWin::~BraveVPNOSConnectionAPIWin () {
50
- CloseHandle (event_handle_);
52
+ CloseHandle (event_handle_for_connected_);
53
+ CloseHandle (event_handle_for_disconnected_);
54
+ CloseEventHandleForConnecting ();
51
55
}
52
56
53
57
void BraveVPNOSConnectionAPIWin::CreateVPNConnection (
@@ -75,6 +79,10 @@ void BraveVPNOSConnectionAPIWin::Connect(const std::string& name) {
75
79
}
76
80
77
81
void BraveVPNOSConnectionAPIWin::Disconnect (const std::string& name) {
82
+ // Fire pseudo disconnecting noti because windows doesn't have it.
83
+ for (Observer& obs : observers_)
84
+ obs.OnIsDisconnecting (name);
85
+
78
86
// Connection state update from this call will be done by monitoring.
79
87
base::ThreadPool::PostTask (
80
88
FROM_HERE, {base::MayBlock ()},
@@ -100,7 +108,18 @@ void BraveVPNOSConnectionAPIWin::CheckConnection(const std::string& name) {
100
108
void BraveVPNOSConnectionAPIWin::OnObjectSignaled (HANDLE object) {
101
109
DCHECK (!target_vpn_entry_name ().empty ());
102
110
103
- CheckConnection (target_vpn_entry_name ());
111
+ CheckConnectionResult result = CheckConnectionResult::UNKNOWN;
112
+ if (object == GetEventHandleForConnecting ()) {
113
+ result = CheckConnectionResult::CONNECTING;
114
+ } else if (object == event_handle_for_connected_) {
115
+ result = CheckConnectionResult::CONNECTED;
116
+ } else if (object == event_handle_for_disconnected_) {
117
+ result = CheckConnectionResult::DISCONNECTED;
118
+ } else {
119
+ NOTREACHED ();
120
+ }
121
+
122
+ OnCheckConnection (target_vpn_entry_name (), result);
104
123
}
105
124
106
125
void BraveVPNOSConnectionAPIWin::OnCheckConnection (
@@ -109,9 +128,20 @@ void BraveVPNOSConnectionAPIWin::OnCheckConnection(
109
128
if (result == CheckConnectionResult::UNKNOWN)
110
129
return ;
111
130
112
- const bool connected = result == CheckConnectionResult::CONNECTED;
113
131
for (Observer& obs : observers_) {
114
- connected ? obs.OnConnected (name) : obs.OnDisconnected (name);
132
+ switch (result) {
133
+ case CheckConnectionResult::CONNECTED:
134
+ obs.OnConnected (name);
135
+ break ;
136
+ case CheckConnectionResult::CONNECTING:
137
+ obs.OnIsConnecting (name);
138
+ break ;
139
+ case CheckConnectionResult::DISCONNECTED:
140
+ obs.OnDisconnected (name);
141
+ break ;
142
+ default :
143
+ break ;
144
+ }
115
145
}
116
146
}
117
147
@@ -134,13 +164,24 @@ void BraveVPNOSConnectionAPIWin::OnRemoved(const std::string& name,
134
164
}
135
165
136
166
void BraveVPNOSConnectionAPIWin::StartVPNConnectionChangeMonitoring () {
137
- DCHECK (!event_handle_);
138
- event_handle_ = CreateEvent (nullptr , false , false , nullptr );
167
+ DCHECK (!event_handle_for_connected_ && !event_handle_for_disconnected_);
168
+
169
+ event_handle_for_connected_ = CreateEvent (nullptr , false , false , nullptr );
170
+ event_handle_for_disconnected_ = CreateEvent (nullptr , false , false , nullptr );
139
171
172
+ // We don't need to check current connection state again if monitor each event
173
+ // separately.
174
+ RasConnectionNotificationW (static_cast <HRASCONN>(INVALID_HANDLE_VALUE),
175
+ event_handle_for_connected_, RASCN_Connection);
140
176
RasConnectionNotificationW (static_cast <HRASCONN>(INVALID_HANDLE_VALUE),
141
- event_handle_,
142
- (RASCN_Connection | RASCN_Disconnection));
143
- watcher_.StartWatchingMultipleTimes (event_handle_, this );
177
+ event_handle_for_disconnected_,
178
+ RASCN_Disconnection);
179
+ connected_event_watcher_.StartWatchingMultipleTimes (
180
+ event_handle_for_connected_, this );
181
+ disconnected_event_watcher_.StartWatchingMultipleTimes (
182
+ event_handle_for_disconnected_, this );
183
+ connecting_event_watcher_.StartWatchingMultipleTimes (
184
+ GetEventHandleForConnecting (), this );
144
185
}
145
186
146
187
} // namespace brave_vpn
0 commit comments