-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathPushNotificationManager.cpp
More file actions
612 lines (507 loc) · 23.9 KB
/
PushNotificationManager.cpp
File metadata and controls
612 lines (507 loc) · 23.9 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "PushNotificationManager.h"
#include "Microsoft.Windows.PushNotifications.PushNotificationManager.g.cpp"
#include "PushNotificationTelemetry.h"
#include "PushNotificationCreateChannelResult.h"
#include "PushNotifications-Constants.h"
#include <winrt/Windows.ApplicationModel.background.h>
#include <winrt/Windows.Networking.PushNotifications.h>
#include "PushNotificationBackgroundTask.h"
#include <winerror.h>
#include <algorithm>
#include "PushNotificationChannel.h"
#include "externs.h"
#include <string_view>
#include <frameworkudk/pushnotifications.h>
#include "NotificationsLongRunningProcess_h.h"
#include "PushNotificationUtility.h"
#include "AppNotificationUtility.h"
#include "PushNotificationReceivedEventArgs.h"
using namespace std::literals;
using namespace Microsoft::Windows::AppNotifications::Helpers;
constexpr std::wstring_view backgroundTaskName = L"PushBackgroundTaskName"sv;
constexpr std::wstring_view expectedPushServerArgs = L"----WindowsAppRuntimePushServer:"sv;
static wil::unique_event g_waitHandleForArgs;
static winrt::guid g_comServerClsid{ GUID_NULL };
static bool registering{ false };
wil::unique_event& GetWaitHandleForArgs()
{
return g_waitHandleForArgs;
}
namespace winrt
{
using namespace Windows::ApplicationModel::Background;
using namespace Windows::Networking::PushNotifications;
using namespace Windows::Foundation;
}
namespace PushNotificationHelpers
{
using namespace winrt::Microsoft::Windows::PushNotifications::Helpers;
}
namespace winrt::Microsoft::Windows::PushNotifications::implementation
{
inline constexpr auto c_maxBackoff{ 5min };
inline constexpr auto c_initialBackoff{ 60s };
inline constexpr auto c_backoffIncrement{ 60s };
const HRESULT WNP_E_NOT_CONNECTED = static_cast<HRESULT>(0x880403E8L);
const HRESULT WNP_E_RECONNECTING = static_cast<HRESULT>(0x880403E9L);
const HRESULT WNP_E_BIND_USER_BUSY = static_cast<HRESULT>(0x880403FEL);
bool IsChannelRequestRetryable(const hresult& hr)
{
switch (hr)
{
case HRESULT_FROM_WIN32(ERROR_TIMEOUT):
case WNP_E_NOT_CONNECTED:
case WPN_E_OUTSTANDING_CHANNEL_REQUEST:
case WNP_E_RECONNECTING:
case WNP_E_BIND_USER_BUSY:
case HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE):
return true;
default:
return false;
}
}
void PushNotificationManager::RegisterForegroundSinkHelper()
{
bool registeredEvent{ false };
{
auto lock{ m_lock.lock_shared() };
registeredEvent = bool(m_foregroundHandlers);
}
if (registeredEvent)
{
if (PushNotificationHelpers::IsPackagedAppScenario())
{
auto appUserModelId{ PushNotificationHelpers::GetAppUserModelId() };
// Register a sink with platform which is initialized in the current process
THROW_IF_FAILED(PushNotifications_RegisterNotificationSinkForFullTrustApplication(appUserModelId.get(), PushNotificationManager::Default().as<ABI::Microsoft::Internal::PushNotifications::INotificationListener>().get()));
}
else
{
auto notificationsLongRunningPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
// Register a sink with platform brokered through PushNotificationsLongRunningProcess
THROW_IF_FAILED(notificationsLongRunningPlatform->RegisterForegroundActivator(PushNotificationManager::Default().as<IWpnForegroundSink>().get(), m_processName.get()));
}
}
}
PushNotificationManager::PushNotificationManager()
{
THROW_IF_FAILED(GetCurrentProcessPath(m_processName));
if (AppModel::Identity::IsPackagedProcess())
{
// Returns ComActivator CLSID from registry. This CLSID provided in manifest is registered when a packaged app is installed
THROW_IF_FAILED(PushNotificationHelpers::GetComRegistrationFromRegistry(expectedPushServerArgs.data(), m_registeredClsid));
}
}
winrt::hresult CreateChannelWithRemoteIdHelper(wil::unique_cotaskmem_string const& appId, const winrt::guid& remoteId, ChannelDetails& channelInfo) noexcept try
{
HRESULT operationalCode{};
ABI::Windows::Foundation::DateTime channelExpiryTime{};
wil::unique_cotaskmem_string channelId;
wil::unique_cotaskmem_string channelUri;
THROW_IF_FAILED(PushNotifications_CreateChannelWithRemoteIdentifier(
appId.get(),
remoteId,
&operationalCode,
&channelId,
&channelUri,
&channelExpiryTime));
THROW_IF_FAILED(operationalCode);
winrt::copy_from_abi(channelInfo.channelExpiryTime, &channelExpiryTime);
channelInfo.appId = winrt::hstring{ appId.get() };
channelInfo.channelId = winrt::hstring{ channelId.get() };
channelInfo.channelUri = winrt::hstring{ channelUri.get() };
return S_OK;
}
CATCH_RETURN()
winrt::Microsoft::Windows::PushNotifications::PushNotificationManager PushNotificationManager::Default()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::PushNotifications::Feature_PushNotifications::IsEnabled());
static auto pushNotificationManager{ winrt::make<PushNotificationManager>() };
return pushNotificationManager;
}
winrt::IAsyncOperationWithProgress<winrt::Microsoft::Windows::PushNotifications::PushNotificationCreateChannelResult, winrt::Microsoft::Windows::PushNotifications::PushNotificationCreateChannelStatus> PushNotificationManager::CreateChannelAsync(const winrt::guid remoteId)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::PushNotifications::Feature_PushNotifications::IsEnabled());
wil::winrt_module_reference moduleRef{};
try
{
THROW_HR_IF(E_INVALIDARG, (remoteId == winrt::guid()));
auto cancellation{ co_await winrt::get_cancellation_token() };
cancellation.enable_propagation(true);
// Allow to register the progress and complete handler
co_await resume_background();
auto progress{ co_await winrt::get_progress_token() };
uint8_t retryCount = 0;
winrt::hresult channelRequestResult = E_PENDING;
PushNotificationChannelStatus status = PushNotificationChannelStatus::InProgress;
PushNotificationCreateChannelStatus
channelStatus = { status, channelRequestResult, retryCount };
progress(channelStatus);
for (auto backOffTime = c_initialBackoff; ; backOffTime += c_backoffIncrement)
{
try
{
ChannelDetails channelInfo{};
if (PushNotificationHelpers::IsPackagedAppScenario())
{
auto appUserModelId{ PushNotificationHelpers::GetAppUserModelId() };
THROW_IF_FAILED(CreateChannelWithRemoteIdHelper(appUserModelId, remoteId, channelInfo));
}
else
{
auto notificationPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
// AppId is generated by PushNotificationLongRunningTask singleton
wil::unique_cotaskmem_string unpackagedAppUserModelId;
THROW_IF_FAILED(notificationPlatform->RegisterFullTrustApplication(m_processName.get(), remoteId, &unpackagedAppUserModelId));
THROW_IF_FAILED(CreateChannelWithRemoteIdHelper(unpackagedAppUserModelId, remoteId, channelInfo));
THROW_IF_FAILED(notificationPlatform->RegisterLongRunningActivatorWithClsid(m_processName.get(), m_registeredClsid));
std::wstring toastAppId{ RetrieveNotificationAppId() };
THROW_IF_FAILED(notificationPlatform->AddToastRegistrationMapping(m_processName.get(), toastAppId.c_str()));
}
// Need to recreate the Foreground sinks since channel creation removes the old sink
RegisterForegroundSinkHelper();
auto channel{ winrt::make<PushNotificationChannel>(channelInfo) };
{
auto lock{ m_lock.lock_exclusive() };
m_channel = channel;
}
PushNotificationTelemetry::ChannelRequestedByApi(S_OK, remoteId);
co_return winrt::make<PushNotificationCreateChannelResult>(
channel,
S_OK,
PushNotificationChannelStatus::CompletedSuccess);
}
catch (...)
{
auto channelRequestException{ hresult_error(to_hresult(), take_ownership_from_abi) };
if ((backOffTime <= c_maxBackoff) && IsChannelRequestRetryable(channelRequestException.code()))
{
channelStatus.extendedError = channelRequestException.code();
channelStatus.status = PushNotificationChannelStatus::InProgressRetry;
channelStatus.retryCount = ++retryCount;
progress(channelStatus);
}
else
{
PushNotificationTelemetry::ChannelRequestedByApi(channelRequestException.code(), remoteId);
co_return winrt::make<PushNotificationCreateChannelResult>(
nullptr,
channelRequestException.code(),
PushNotificationChannelStatus::CompletedFailure);
}
}
co_await winrt::resume_after(backOffTime);
}
}
catch (...)
{
PushNotificationTelemetry::ChannelRequestedByApi(wil::ResultFromCaughtException(), remoteId);
throw;
}
}
bool PushNotificationManager::IsBackgroundTaskRegistered(winrt::hstring const& backgroundTaskFullName)
{
auto tasks{ BackgroundTaskRegistration::AllTasks() };
bool isTaskRegistered{ std::any_of(std::begin(tasks), std::end(tasks),
[&](auto&& task)
{
auto name{ task.Value().Name() };
if (name == backgroundTaskFullName)
{
{
auto lock{ m_lock.lock_exclusive() };
m_pushTriggerRegistration = task.Value();
}
return true;
}
return false;
}) };
return isTaskRegistered;
}
void PushNotificationManager::Register()
{
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, registering, "Registration is in progress!");
registering = true;
}
try
{
if (PushNotificationHelpers::IsPackagedAppScenario())
{
{
auto lock{ m_lock.lock_shared() };
THROW_HR_IF(E_INVALIDARG, m_comActivatorRegistration || m_pushTriggerRegistration);
}
auto scopeExitToCleanRegistrations{ wil::scope_exit([&]()
{
{
auto lock { m_lock.lock_exclusive() };
m_comActivatorRegistration.reset();
// Clean the task registration only if it was created during this call
if (m_pushTriggerRegistration)
{
m_pushTriggerRegistration.Unregister(true);
m_pushTriggerRegistration = nullptr;
}
registering = false;
}
auto appUserModelId{ PushNotificationHelpers::GetAppUserModelId() };
LOG_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get()));
}
)};
winrt::hstring backgroundTaskFullName;
{
auto lock{ m_lock.lock_shared() };
backgroundTaskFullName = backgroundTaskName + winrt::to_hstring(m_registeredClsid);
}
// Register a PushTrigger for BI to activate the application through COM
if (!IsBackgroundTaskRegistered(backgroundTaskFullName))
{
BackgroundTaskBuilder builder{ BackgroundTaskBuilder() };
builder.Name(backgroundTaskFullName);
PushNotificationTrigger trigger{};
builder.SetTrigger(trigger);
auto builder5{ builder.as<winrt::IBackgroundTaskBuilder5>() };
{
auto lock{ m_lock.lock_exclusive() };
builder5.SetTaskEntryPointClsid(m_registeredClsid);
m_pushTriggerRegistration = builder.Register();
}
}
// Register a sink for the application to receive foreground raw notifications
RegisterForegroundSinkHelper();
{
auto lock{ m_lock.lock_exclusive() };
// Register a PushNotificationBackgroundTask to handle background activation scenarios
THROW_IF_FAILED(::CoRegisterClassObject(
m_registeredClsid,
winrt::make<PushNotificationBackgroundTaskFactory>().get(),
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE,
&m_comActivatorRegistration));
}
scopeExitToCleanRegistrations.release();
}
else
{
{
auto lock{ m_lock.lock_shared() };
THROW_HR_IF(E_INVALIDARG, m_singletonForegroundRegistration && m_singletonBackgroundRegistration);
}
auto scopeExitToCleanRegistrations{ wil::scope_exit([&]()
{
{
auto lock { m_lock.lock_exclusive() };
m_comActivatorRegistration.reset();
m_singletonBackgroundRegistration = false;
m_singletonForegroundRegistration = false;
registering = false;
}
auto notificationsLongRunningPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
LOG_IF_FAILED(notificationsLongRunningPlatform->UnregisterForegroundActivator(m_processName.get()));
LOG_IF_FAILED(notificationsLongRunningPlatform->UnregisterFullTrustApplication(m_processName.get()));
}) };
auto notificationPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
wil::unique_cotaskmem_string unpackagedAppUserModelId;
// Apps treated as unpackaged need to call RegisterFullTrustApplication and register with the LRP
THROW_IF_FAILED(notificationPlatform->RegisterFullTrustApplication(m_processName.get(), GUID_NULL, &unpackagedAppUserModelId));
// Register a sink for the application to receive foreground raw notifications
RegisterForegroundSinkHelper();
{
auto lock{ m_lock.lock_exclusive() };
m_singletonForegroundRegistration = true;
}
// m_registeredClsid is set to GUID_NULL for unpackaged applications
THROW_IF_FAILED(notificationPlatform->RegisterLongRunningActivatorWithClsid(m_processName.get(), m_registeredClsid));
{
auto lock{ m_lock.lock_exclusive() };
m_singletonBackgroundRegistration = true;
}
// Register a COM object for the PushNotificationLongRunningProcess to CoCreate in background activation scenarios
if (AppModel::Identity::IsPackagedProcess())
{
auto lock{ m_lock.lock_exclusive() };
THROW_IF_FAILED(::CoRegisterClassObject(
m_registeredClsid,
winrt::make<PushNotificationBackgroundTaskFactory>().get(),
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE,
&m_comActivatorRegistration));
}
scopeExitToCleanRegistrations.release();
}
}
catch (...)
{
PushNotificationTelemetry::ActivatorRegisteredByApi(wil::ResultFromCaughtException());
throw;
}
{
auto lock{ m_lock.lock_exclusive() };
registering = false;
}
}
void PushNotificationManager::Unregister()
{
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, registering, "Register or Unregister currently in progress!");
registering = true;
}
auto scope_exit = wil::scope_exit(
[&] {
auto lock{ m_lock.lock_exclusive() };
registering = false;
});
try
{
if (PushNotificationHelpers::IsPackagedAppScenario())
{
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, !m_comActivatorRegistration, "No active COM registration");
// m_comActivatorRegistration handles multiple unregistrations
m_comActivatorRegistration.reset();
}
auto appUserModelId{ PushNotificationHelpers::GetAppUserModelId() };
THROW_IF_FAILED(PushNotifications_UnregisterNotificationSinkForFullTrustApplication(appUserModelId.get()));
}
else
{
if (AppModel::Identity::IsPackagedProcess())
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, !m_comActivatorRegistration, "No active COM registration");
m_comActivatorRegistration.reset();
}
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, !m_singletonForegroundRegistration, "No foreground sink registered");
}
auto notificationsLongRunningPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
// Unregister foreground sink with the Long Running Process Singelton
THROW_IF_FAILED(notificationsLongRunningPlatform->UnregisterForegroundActivator(m_processName.get()));
{
auto lock{ m_lock.lock_exclusive() };
m_singletonForegroundRegistration = false;
}
}
}
catch (...)
{
PushNotificationTelemetry::ActivatorUnregisteredByApi(wil::ResultFromCaughtException());
throw;
}
PushNotificationTelemetry::ActivatorUnregisteredByApi(S_OK);
}
void PushNotificationManager::UnregisterAll()
{
bool unregisterCompleted{ false };
{
auto lock{ m_lock.lock_exclusive() };
unregisterCompleted = (!m_singletonForegroundRegistration || !m_comActivatorRegistration);
}
if (!unregisterCompleted)
{
Unregister();
}
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_MSG(E_FAIL, registering, "Register or Unregister currently in progress!");
registering = true;
}
auto scope_exit = wil::scope_exit(
[&] {
auto lock{ m_lock.lock_exclusive() };
registering = false;
});
try
{
try
{
auto lock{ m_lock.lock_exclusive() };
if (m_channel != nullptr)
{
m_channel.Close();
}
}
catch (...)
{
LOG_IF_FAILED(wil::ResultFromCaughtException());
}
if (PushNotificationHelpers::IsPackagedAppScenario())
{
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF_NULL(E_FAIL, m_pushTriggerRegistration);
m_pushTriggerRegistration.Unregister(true);
m_pushTriggerRegistration = nullptr;
}
}
else
{
auto notificationsLongRunningPlatform{ PushNotificationHelpers::GetNotificationPlatform() };
{
auto lock{ m_lock.lock_exclusive() };
THROW_HR_IF(E_FAIL, !m_singletonBackgroundRegistration);
}
// Removes the Long Running Singleton sink registered with platform for both packaged and unpackaged applications
THROW_IF_FAILED(notificationsLongRunningPlatform->UnregisterLongRunningActivator(m_processName.get()));
THROW_IF_FAILED(notificationsLongRunningPlatform->UnregisterFullTrustApplication(m_processName.get()));
{
auto lock{ m_lock.lock_exclusive() };
m_singletonBackgroundRegistration = false;
}
}
}
catch (...)
{
PushNotificationTelemetry::ActivatorUnregisteredByApi(wil::ResultFromCaughtException());
throw;
}
PushNotificationTelemetry::ActivatorUnregisteredByApi(S_OK);
}
winrt::event_token PushNotificationManager::PushReceived(TypedEventHandler<winrt::Microsoft::Windows::PushNotifications::PushNotificationManager, winrt::Microsoft::Windows::PushNotifications::PushNotificationReceivedEventArgs> handler)
{
{
auto lock{ m_lock.lock_shared() };
THROW_HR_IF_MSG(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), m_comActivatorRegistration || m_singletonBackgroundRegistration, "Must register event handlers before calling Register().");
}
auto lock{ m_lock.lock_exclusive() };
return m_foregroundHandlers.add(handler);
}
void PushNotificationManager::PushReceived(winrt::event_token const& token) noexcept
{
auto lock{ m_lock.lock_exclusive() };
m_foregroundHandlers.remove(token);
}
HRESULT __stdcall PushNotificationManager::InvokeAll(_In_ ULONG length, _In_ byte* payload, _Out_ BOOL* foregroundHandled) noexcept try
{
auto args = winrt::make<winrt::Microsoft::Windows::PushNotifications::implementation::PushNotificationReceivedEventArgs>(payload, length);
auto lock{ m_lock.lock_exclusive() };
m_foregroundHandlers(*this, args);
*foregroundHandled = args.Handled();
return S_OK;
}
CATCH_RETURN()
HRESULT __stdcall PushNotificationManager::OnRawNotificationReceived(unsigned int payloadLength, _In_ byte* payload, _In_ HSTRING /*correlationVector */) noexcept try
{
BOOL foregroundHandled = true;
THROW_IF_FAILED(InvokeAll(payloadLength, payload, &foregroundHandled));
if (!foregroundHandled)
{
if (!AppModel::Identity::IsPackagedProcess())
{
wil::unique_cotaskmem_string processName;
THROW_IF_FAILED(GetCurrentProcessPath(processName));
THROW_IF_FAILED(PushNotificationHelpers::ProtocolLaunchHelper(processName.get(), payloadLength, payload));
}
}
return S_OK;
}
CATCH_RETURN();
}