-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathServiceLocator.cpp
324 lines (254 loc) · 8.88 KB
/
ServiceLocator.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "..\inc\ServiceLocator.hpp"
#include "InteractivityFactory.hpp"
#pragma hdrstop
using namespace Microsoft::Console::Types;
using namespace Microsoft::Console::Interactivity;
#pragma region Private Static Member Initialization
std::unique_ptr<IInteractivityFactory> ServiceLocator::s_interactivityFactory;
std::unique_ptr<IConsoleControl> ServiceLocator::s_consoleControl;
std::unique_ptr<IConsoleInputThread> ServiceLocator::s_consoleInputThread;
std::unique_ptr<IWindowMetrics> ServiceLocator::s_windowMetrics;
std::unique_ptr<IAccessibilityNotifier> ServiceLocator::s_accessibilityNotifier;
std::unique_ptr<IHighDpiApi> ServiceLocator::s_highDpiApi;
std::unique_ptr<ISystemConfigurationProvider> ServiceLocator::s_systemConfigurationProvider;
std::unique_ptr<IInputServices> ServiceLocator::s_inputServices;
IConsoleWindow* ServiceLocator::s_consoleWindow = nullptr;
Globals ServiceLocator::s_globals;
bool ServiceLocator::s_pseudoWindowInitialized = false;
wil::unique_hwnd ServiceLocator::s_pseudoWindow = 0;
#pragma endregion
#pragma region Public Methods
void ServiceLocator::RundownAndExit(const HRESULT hr)
{
// MSFT:15506250
// In VT I/O Mode, a client application might die before we've rendered
// the last bit of text they've emitted. So give the VtRenderer one
// last chance to paint before it is killed.
if (s_globals.pRender)
{
s_globals.pRender->TriggerTeardown();
}
// A History Lesson from MSFT: 13576341:
// We introduced RundownAndExit to give services that hold onto important handles
// an opportunity to let those go when we decide to exit from the console for various reasons.
// This was because Console IO Services (ConIoSvcComm) on OneCore editions was holding onto
// pipe and ALPC handles to talk to CSRSS ConIoSrv.dll to broker which console got display/keyboard control.
// If we simply run straight into TerminateProcess, those handles aren't necessarily released right away.
// The terminate operation can have a rundown period of time where APCs are serviced (such as from
// a DirectX kernel callback/flush/cleanup) that can take substantially longer than we expect (several whole seconds).
// This rundown happens before the final destruction of any outstanding handles or resources.
// If someone is waiting on one of those handles or resources outside our process, they're stuck waiting
// for our terminate rundown and can't continue execution until we're done.
// We don't want to have other execution in the system get stuck, so this is a great
// place to clean up and notify any objects or threads in the system that have to cleanup safely before
// we head into TerminateProcess and tear everything else down less gracefully.
// TODO: MSFT: 14397093 - Expand graceful rundown beyond just the Hot Bug input services case.
if (s_inputServices.get() != nullptr)
{
s_inputServices.reset(nullptr);
}
TerminateProcess(GetCurrentProcess(), hr);
}
#pragma region Creation Methods
[[nodiscard]] NTSTATUS ServiceLocator::CreateConsoleInputThread(_Outptr_result_nullonfailure_ IConsoleInputThread** thread)
{
NTSTATUS status = STATUS_SUCCESS;
if (s_consoleInputThread)
{
status = STATUS_INVALID_HANDLE;
}
else
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateConsoleInputThread(s_consoleInputThread);
if (NT_SUCCESS(status))
{
*thread = s_consoleInputThread.get();
}
}
}
return status;
}
#pragma endregion
#pragma region Set Methods
[[nodiscard]] NTSTATUS ServiceLocator::SetConsoleWindowInstance(_In_ IConsoleWindow* window)
{
NTSTATUS status = STATUS_SUCCESS;
if (s_consoleWindow)
{
status = STATUS_INVALID_HANDLE;
}
else if (!window)
{
status = STATUS_INVALID_PARAMETER;
}
else
{
s_consoleWindow = window;
}
return status;
}
#pragma endregion
#pragma region Location Methods
IConsoleWindow* ServiceLocator::LocateConsoleWindow()
{
return s_consoleWindow;
}
IConsoleControl* ServiceLocator::LocateConsoleControl()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_consoleControl)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateConsoleControl(s_consoleControl);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_consoleControl.get();
}
IConsoleInputThread* ServiceLocator::LocateConsoleInputThread()
{
return s_consoleInputThread.get();
}
IHighDpiApi* ServiceLocator::LocateHighDpiApi()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_highDpiApi)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateHighDpiApi(s_highDpiApi);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_highDpiApi.get();
}
IWindowMetrics* ServiceLocator::LocateWindowMetrics()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_windowMetrics)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateWindowMetrics(s_windowMetrics);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_windowMetrics.get();
}
IAccessibilityNotifier* ServiceLocator::LocateAccessibilityNotifier()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_accessibilityNotifier)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateAccessibilityNotifier(s_accessibilityNotifier);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_accessibilityNotifier.get();
}
ISystemConfigurationProvider* ServiceLocator::LocateSystemConfigurationProvider()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_systemConfigurationProvider)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateSystemConfigurationProvider(s_systemConfigurationProvider);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_systemConfigurationProvider.get();
}
IInputServices* ServiceLocator::LocateInputServices()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_inputServices)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
status = s_interactivityFactory->CreateInputServices(s_inputServices);
}
}
LOG_IF_NTSTATUS_FAILED(status);
return s_inputServices.get();
}
Globals& ServiceLocator::LocateGlobals()
{
return s_globals;
}
// Method Description:
// - Retrieves the pseudo console window, or attempts to instantiate one.
// Arguments:
// - <none>
// Return Value:
// - a reference to the pseudoconsole window.
HWND ServiceLocator::LocatePseudoWindow()
{
NTSTATUS status = STATUS_SUCCESS;
if (!s_pseudoWindowInitialized)
{
if (s_interactivityFactory.get() == nullptr)
{
status = ServiceLocator::LoadInteractivityFactory();
}
if (NT_SUCCESS(status))
{
HWND hwnd;
status = s_interactivityFactory->CreatePseudoWindow(hwnd);
s_pseudoWindow.reset(hwnd);
}
s_pseudoWindowInitialized = true;
}
LOG_IF_NTSTATUS_FAILED(status);
return s_pseudoWindow.get();
}
#pragma endregion
#pragma endregion
#pragma region Private Methods
[[nodiscard]] NTSTATUS ServiceLocator::LoadInteractivityFactory()
{
NTSTATUS status = STATUS_SUCCESS;
if (s_interactivityFactory.get() == nullptr)
{
s_interactivityFactory = std::make_unique<InteractivityFactory>();
status = NT_TESTNULL(s_interactivityFactory.get());
}
return status;
}
#pragma endregion