forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
windows_toast_notification.cc
1531 lines (1283 loc) · 55.3 KB
/
windows_toast_notification.cc
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and Jason Poon
// <jason.poon@microsoft.com>. All rights reserved.>
// Copyright (c) 2015 Ryan McShane <rmcshane@bandwidth.com> and Brandon Smith
// <bsmith@bandwidth.com>
// Thanks to both of those folks mentioned above who first thought up a bunch of
// this code
// and released it as MIT to the world.
#include "shell/browser/notifications/win/windows_toast_notification.h"
// SAP-14036 upgrade for headers necessary for persistent notifications
// functionality
#include <NotificationActivationCallback.h>
#include <Psapi.h>
#include <SDKDDKVer.h>
#include <ShObjIdl.h>
#include <Shlobj.h>
#include <combaseapi.h>
#include <mmsystem.h>
#include <propkey.h>
#include <propvarutil.h>
#include <shellapi.h>
#include <strsafe.h>
#include <windows.ui.notifications.h>
#include <wrl\module.h>
#include <wrl\wrappers\corewrappers.h>
#include <algorithm>
#include <iterator>
#include <sstream>
#include "base/environment.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_handle.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/notifications/win/notification_presenter_win.h"
#include "shell/browser/win/scoped_hstring.h"
#include "shell/common/application_info.h"
#include "ui/base/l10n/l10n_util_win.h"
#include "ui/strings/grit/ui_strings.h"
// This is win part of Electon and some API requires native StrCat
// so we have problems du to ./src/base/win/windows_defines.inc
// there is some workaround to allow compilation of win part
#undef StrCat
#include "shell/browser/api/electron_api_app.h"
using ABI::Windows::Data::Xml::Dom::IXmlAttribute;
using ABI::Windows::Data::Xml::Dom::IXmlDocument;
using ABI::Windows::Data::Xml::Dom::IXmlDocumentIO;
using ABI::Windows::Data::Xml::Dom::IXmlElement;
using ABI::Windows::Data::Xml::Dom::IXmlNamedNodeMap;
using ABI::Windows::Data::Xml::Dom::IXmlNode;
using ABI::Windows::Data::Xml::Dom::IXmlNodeList;
using ABI::Windows::Data::Xml::Dom::IXmlText;
using ABI::Windows::Foundation::IPropertyValue;
using ABI::Windows::Foundation::Collections::IMap;
using ABI::Windows::Foundation::Collections::IMapView;
using ABI::Windows::Foundation::Collections::IPropertySet;
using ABI::Windows::UI::Notifications::IToastActivatedEventArgs;
using ABI::Windows::UI::Notifications::IToastActivatedEventArgs2;
using ABI::Windows::UI::Notifications::ToastDismissalReason;
using Microsoft::WRL::Wrappers::HandleT;
using Microsoft::WRL::Wrappers::HStringReference;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
using base::win::ScopedHandle;
// Used to CoCreate an INotificationActivationCallback interface to notify
// about toast activations.
EXTERN_C const PROPERTYKEY DECLSPEC_SELECTANY
PKEY_AppUserModel_ToastActivatorCLSID = {
{0x9F4C2855,
0x9F79,
0x4B39,
{0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3}},
26};
using Microsoft::WRL::Wrappers::HStringReference;
#define RETURN_IF_FAILED(hr) \
do { \
HRESULT _hrTemp = hr; \
if (FAILED(_hrTemp)) { \
return _hrTemp; \
} \
} while (false)
#define REPORT_AND_RETURN_IF_FAILED(hr, msg) \
do { \
HRESULT _hrTemp = hr; \
std::string _msgTemp = msg; \
if (FAILED(_hrTemp)) { \
std::string _err = _msgTemp + ",ERROR " + std::to_string(_hrTemp); \
if (IsDebuggingNotifications()) \
LOG(INFO) << _err; \
Notification::NotificationFailed(_err); \
return _hrTemp; \
} \
} while (false)
namespace std {
wostream& operator<<(wostream& out, const NOTIFICATION_USER_INPUT_DATA& data) {
// SAP-15908 refine notification-activation for cold start
out << L"{"; // open brace for key-value pair
out << L"\"" << data.Key << L"\"";
out << L":"; // json delimeter (i.e colon) between key value
out << L"\"" << data.Value << L"\"";
out << L"}"; // close brace for key-value pair
return out;
}
} // namespace std
namespace electron {
namespace {
bool IsDebuggingNotifications() {
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
}
// SAP-15501 : Clear notifications
std::wstring HexStringFromStdHash(std::size_t hash) {
std::wstringstream ss;
ss << std::hex << hash;
return ss.str();
}
} // namespace
// static
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics>
WindowsToastNotification::toast_manager_;
// static
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics2>
ToastEventHandler::toast_manager_;
// static
ComPtr<ABI::Windows::UI::Notifications::IToastNotifier>
WindowsToastNotification::toast_notifier_;
// SAP-14036 add COM server regestration
class NotificationActivator;
// SAP-15762 support COM server registration at runtime
class NotificationActivatorFactory;
class NotificationRegistrator {
public:
bool RegisterAppForNotificationSupport(const std::wstring& appId,
const std::wstring& clsid,
const std::wstring& disp);
bool SetRegistryKeyValue(HKEY hKey,
const std::wstring& subKey,
const std::wstring& valueName,
const std::wstring& value) {
return SUCCEEDED(HRESULT_FROM_WIN32(::RegSetKeyValue(
hKey, subKey.c_str(), valueName.empty() ? nullptr : valueName.c_str(),
REG_SZ, reinterpret_cast<const BYTE*>(value.c_str()),
static_cast<DWORD>(value.length()) * sizeof(WCHAR))));
}
HSTRING AppId() { return app_id_; }
// SAP-16574 : Allow App icon into Action center and System Notifications
// settings
NotificationRegistrator() {
wchar_t appPath[MAX_PATH] = {0};
if (!::GetModuleFileName(nullptr, appPath, ARRAYSIZE(appPath)))
return;
app_path_ = std::wstring(appPath);
wchar_t tmpPath[MAX_PATH] = {0};
if (!::GetTempPath(ARRAYSIZE(tmpPath), tmpPath))
return;
tmp_path_ = std::wstring(tmpPath);
}
private:
ScopedHString app_id_;
// SAP-16574 : Allow App icon into Action center and System Notifications
// settings
std::wstring app_path_;
std::wstring tmp_path_;
std::wstring ico_path_;
bool ExtractAppIconToTempFile(const std::wstring& file_name);
};
// static
static std::unique_ptr<NotificationRegistrator> registrator_;
bool WindowsToastNotification::Initialize() {
// Just initialize, don't care if it fails or already initialized.
Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
ScopedHString toast_manager_str(
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager);
if (!toast_manager_str.success())
return false;
if (FAILED(Windows::Foundation::GetActivationFactory(toast_manager_str,
&toast_manager_)))
return false;
if (FAILED(toast_manager_->QueryInterface(
IID_PPV_ARGS(&(ToastEventHandler::toast_manager_)))))
return false;
if (IsRunningInDesktopBridge()) {
// Ironically, the Desktop Bridge / UWP environment
// requires us to not give Windows an appUserModelId.
return SUCCEEDED(toast_manager_->CreateToastNotifier(&toast_notifier_));
} else {
ScopedHString app_id;
if (!GetAppUserModelID(&app_id))
return false;
// SAP-15762: Support COM activation registration at runtime
std::wstring notificationsCOMServerCLSID;
// SAP-21094: Application name displays in incorrect format on notification
std::wstring notificationsCOMDisplayName;
// SAP-15318: Make the com registration system activable
auto* client = ElectronBrowserClient::Get();
if (client) {
const auto& clsid_str = client->GetNotificationsComServerCLSID();
std::transform(clsid_str.begin(), clsid_str.end(),
std::back_inserter(notificationsCOMServerCLSID),
[](const char ch) { return ch; });
const auto& disp_str = client->GetNotificationsComDisplayName();
std::transform(disp_str.begin(), disp_str.end(),
std::back_inserter(notificationsCOMDisplayName),
[](const char ch) { return ch; });
}
GUID clsid{};
if (!notificationsCOMServerCLSID.empty() &&
SUCCEEDED(
CLSIDFromString(notificationsCOMServerCLSID.c_str(), &clsid))) {
// SAP-15762: Support COM activation registration at runtime
DWORD registration{};
// Register callback
if (FAILED(CoRegisterClassObject(
clsid,
reinterpret_cast<LPUNKNOWN>(
Make<electron::NotificationActivatorFactory>().Get()),
CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, ®istration)))
return false;
// SAP-14036 support COM server regestration
registrator_ = std::make_unique<NotificationRegistrator>();
if (!registrator_->RegisterAppForNotificationSupport(
WindowsGetStringRawBuffer((HSTRING)app_id, NULL),
notificationsCOMServerCLSID, notificationsCOMDisplayName))
return false;
}
return SUCCEEDED(
toast_manager_->CreateToastNotifierWithId(app_id, &toast_notifier_));
}
} // namespace electron
WindowsToastNotification::WindowsToastNotification(
NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {}
WindowsToastNotification::~WindowsToastNotification() {
// Remove the notification on exit.
if (toast_notification_) {
RemoveCallbacks(toast_notification_.Get());
}
}
void WindowsToastNotification::Show(const NotificationOptions& options) {
if (SUCCEEDED(ShowInternal(options))) {
if (IsDebuggingNotifications())
LOG(INFO) << "Notification created";
if (delegate())
delegate()->NotificationDisplayed();
}
}
void WindowsToastNotification::Dismiss() {
if (IsDebuggingNotifications())
LOG(INFO) << "Hiding notification";
toast_notifier_->Hide(toast_notification_.Get());
}
HRESULT WindowsToastNotification::ShowInternal(
const NotificationOptions& options) {
if (options.is_persistent && !registrator_) {
NotificationFailed("COM Activator was not registered");
return E_FAIL;
}
ComPtr<IXmlDocument> toast_xml;
// The custom xml takes priority over the preset template.
if (!options.toast_xml.empty()) {
REPORT_AND_RETURN_IF_FAILED(
XmlDocumentFromString(base::UTF16ToWide(options.toast_xml).c_str(),
&toast_xml),
"XML: Invalid XML");
} else {
REPORT_AND_RETURN_IF_FAILED(
XmlDocumentFromString(
L"<toast><visual><binding "
L"template=\"ToastGeneric\"></binding></visual></toast>",
&toast_xml),
"XML: FAILED during setup ToastGeneric scheme");
auto* presenter_win = static_cast<NotificationPresenterWin*>(presenter());
std::wstring icon_path =
presenter_win->SaveIconToFilesystem(options.icon, options.icon_url);
std::wstring image_path =
presenter_win->SaveIconToFilesystem(options.image, options.image_url);
// SAP-15259 : Add the reply field on a notification
// Win XML toast scheme requires that inputs will follow befor buttons
// code bellow provide smart sorting without lost of initial inputs/buttons
// relative order
std::vector<electron::NotificationAction> actions;
auto add_inputs =
[&actions](const electron::NotificationAction& notification) {
if (notification.type == NotificationAction::sTYPE_TEXT)
actions.push_back(notification);
};
auto add_buttons =
[&actions](const electron::NotificationAction& notification) {
if (notification.type == NotificationAction::sTYPE_BUTTON)
actions.push_back(notification);
};
std::for_each(options.actions.begin(), options.actions.end(), add_inputs);
std::for_each(options.actions.begin(), options.actions.end(), add_buttons);
REPORT_AND_RETURN_IF_FAILED(
SetToastXml(toast_xml.Get(), base::UTF16ToWide(options.title),
base::UTF16ToWide(options.msg), icon_path, image_path,
base::UTF16ToWide(options.timeout_type),
base::UTF16ToWide(options.data), actions, options.silent,
options.require_interaction),
"XML: Failed to create XML document");
}
ScopedHString toast_str(
RuntimeClass_Windows_UI_Notifications_ToastNotification);
if (!toast_str.success()) {
NotificationFailed("Creating ScopedHString failed");
return E_FAIL;
}
ComPtr<ABI::Windows::UI::Notifications::IToastNotificationFactory>
toast_factory;
REPORT_AND_RETURN_IF_FAILED(
Windows::Foundation::GetActivationFactory(toast_str, &toast_factory),
"WinAPI: GetActivationFactory failed");
REPORT_AND_RETURN_IF_FAILED(toast_factory->CreateToastNotification(
toast_xml.Get(), &toast_notification_),
"WinAPI: CreateToastNotification failed");
ComPtr<ABI::Windows::UI::Notifications::IToastNotification2>
toast_notification2_;
REPORT_AND_RETURN_IF_FAILED(
toast_notification_->QueryInterface(IID_PPV_ARGS(&toast_notification2_)),
"WinAPI: CreateToastNotification2 failed");
// https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotification.tag?view=winrt-20348
// The tag can be maximum 16 characters long. However,
// the Creators Update (15063) extends this limit to 64 characters.
const std::wstring& hex_str{
HexStringFromStdHash(std::hash<std::string>{}(notification_id()))};
// Beware: do not put any methods with temporary wstring as return value
// directly to HStringReference c-tor HStringReference uses bare pointer to
// raw std::wstring data which should be available during all scope
// life-circle
REPORT_AND_RETURN_IF_FAILED(
toast_notification2_->put_Tag(HStringReference(hex_str.c_str()).Get()),
"WinAPI: put_Tag failed");
// 16-SAP-18595: display toast according to Notification.renotify
REPORT_AND_RETURN_IF_FAILED(
toast_notification2_->put_SuppressPopup(!options.should_be_presented),
"WinAPI: setup put_Suppress failed");
REPORT_AND_RETURN_IF_FAILED(SetupCallbacks(toast_notification_.Get()),
"WinAPI: SetupCallbacks failed");
if (options.require_interaction && !options.silent) {
// SAP-17738 - Can not require interaction on Windows
// Workaround to silent system IncommingCall sound and play Default sound by
// custom PlaySound call
// https://docs.microsoft.com/en-us/previous-versions/dd743680(v=vs.85)
PlaySound((LPCTSTR)SND_ALIAS_SYSTEMDEFAULT, NULL, SND_ASYNC | SND_ALIAS_ID);
}
REPORT_AND_RETURN_IF_FAILED(
(event_handler_->SetNotificationOptions(options),
toast_notifier_->Show(toast_notification_.Get())),
"WinAPI: Show failed");
return S_OK;
}
PCWSTR WindowsToastNotification::AsString(
ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument>& xmlDocument) {
HSTRING xml;
ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNodeSerializer> ser;
HRESULT hr =
xmlDocument.As<ABI::Windows::Data::Xml::Dom::IXmlNodeSerializer>(&ser);
hr = ser->GetXml(&xml);
if (SUCCEEDED(hr))
return WindowsGetStringRawBuffer(xml, nullptr);
return nullptr;
}
static const std::wstring kFileUriPrefix{L"file:///"};
HRESULT WindowsToastNotification::SetToastXml(
IXmlDocument* toast_xml,
const std::wstring& title,
const std::wstring& msg,
const std::wstring& icon_path,
const std::wstring& image_path,
const std::wstring& timeout_type,
const std::wstring& data,
const std::vector<electron::NotificationAction>& actions_list,
const bool silent,
const bool require_interaction) {
// Fill the toast's title
if (!title.empty()) {
REPORT_AND_RETURN_IF_FAILED(SetXmlText(toast_xml, title),
"XML: Set toast title failed");
}
// Fill the toast's message
if (!msg.empty()) {
REPORT_AND_RETURN_IF_FAILED(SetXmlText(toast_xml, msg),
"XML: Set toast message failed");
}
// Configure the toast's timeout settings
if (timeout_type == base::ASCIIToWide("never")) {
REPORT_AND_RETURN_IF_FAILED(
(SetXmlScenarioReminder(toast_xml)),
"XML: Setting \"scenario\" option on notification failed");
}
if (require_interaction) {
// Reimplement for SAP-17772 SAP-19442
const bool use_reminder =
!actions_list.empty() &&
actions_list[0].type != NotificationAction::sTYPE_TEXT;
REPORT_AND_RETURN_IF_FAILED(
SetXmlScenarioType(
toast_xml,
std::wstring(use_reminder ? L"reminder" : L"incomingcall")),
"XML: Setting \"reminder\" scenario type failed");
}
// Configure the toast's notification sound
if (silent || require_interaction) {
// SAP-17738 - Can not require interaction on Windows
// Workaround to silent system IncommingCall sound and play Default sound by
// custom PlaySound call
// SAP-21082 - Notification showing up with strange sounds when
// requireInteraction is true require_interaction is necessary to fix side
// effect from applying incomingcall
REPORT_AND_RETURN_IF_FAILED(
SetXmlAudioSilent(toast_xml),
"XML: Setting \"silent\" option on notification failed");
}
// Configure the toast's image
if (!image_path.empty()) {
REPORT_AND_RETURN_IF_FAILED(
SetXmlImage(toast_xml, image_path, L"hero"),
"XML: Setting \"icon\" option on notification failed");
}
// Configure the toast's icon
if (!icon_path.empty()) {
REPORT_AND_RETURN_IF_FAILED(
SetXmlImage(toast_xml, icon_path, L"appLogoOverride"),
"XML: Setting \"image\" option on notification failed");
}
if (!data.empty()) {
REPORT_AND_RETURN_IF_FAILED(
SetLaunchParams(toast_xml, data),
"XML: Setting \"data\" option on notification failed");
}
for (const auto& action : actions_list) {
std::wstring icon_path_ = base::UTF8ToWide(action.icon.spec());
if (icon_path_.find(kFileUriPrefix) == 0) {
icon_path_.assign(icon_path_, kFileUriPrefix.length(),
std::wstring::npos);
}
std::wstring hint_input_id_;
REPORT_AND_RETURN_IF_FAILED(
AddAction(toast_xml, base::UTF16ToWide(action.type),
base::UTF16ToWide(action.text), base::UTF16ToWide(action.arg),
icon_path_, base::UTF16ToWide(action.placeholder),
hint_input_id_),
"XML: Setting \"action\" option on notification failed");
if (action.type != NotificationAction::sTYPE_TEXT)
continue;
hint_input_id_ = base::UTF16ToWide(action.arg);
REPORT_AND_RETURN_IF_FAILED(
AddAction(
toast_xml, base::UTF16ToWide(NotificationAction::sTYPE_BUTTON),
base::UTF16ToWide(action.text),
base::UTF16ToWide(action.arg + NotificationAction::sTYPE_BUTTON),
icon_path_, base::UTF16ToWide(action.placeholder), hint_input_id_),
"XML: Setting \"action\" option on notification failed");
}
// Next approach can be used for accessing to XML toast scheme during debug
// {
// ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument> doc;
// doc.Attach(toast_xml);
// PCWSTR str = AsString(doc);
// OutputDebugString(str);
// OutputDebugString(L"\n");
// doc.Detach();
// }
return S_OK;
}
HRESULT WindowsToastNotification::SetXmlScenarioReminder(IXmlDocument* doc) {
ScopedHString tag(L"toast");
if (!tag.success())
return false;
ComPtr<IXmlNodeList> node_list;
RETURN_IF_FAILED(doc->GetElementsByTagName(tag, &node_list));
// Check that root "toast" node exists
ComPtr<IXmlNode> root;
RETURN_IF_FAILED(node_list->Item(0, &root));
// get attributes of root "toast" node
ComPtr<IXmlNamedNodeMap> toast_attributes;
RETURN_IF_FAILED(root->get_Attributes(&toast_attributes));
ComPtr<IXmlAttribute> scenario_attribute;
ScopedHString scenario_str(L"scenario");
RETURN_IF_FAILED(doc->CreateAttribute(scenario_str, &scenario_attribute));
ComPtr<IXmlNode> scenario_attribute_node;
RETURN_IF_FAILED(scenario_attribute.As(&scenario_attribute_node));
ScopedHString scenario_value(L"reminder");
if (!scenario_value.success())
return E_FAIL;
ComPtr<IXmlText> scenario_text;
RETURN_IF_FAILED(doc->CreateTextNode(scenario_value, &scenario_text));
ComPtr<IXmlNode> scenario_node;
RETURN_IF_FAILED(scenario_text.As(&scenario_node));
ComPtr<IXmlNode> scenario_backup_node;
RETURN_IF_FAILED(scenario_attribute_node->AppendChild(scenario_node.Get(),
&scenario_backup_node));
ComPtr<IXmlNode> scenario_attribute_pnode;
RETURN_IF_FAILED(toast_attributes.Get()->SetNamedItem(
scenario_attribute_node.Get(), &scenario_attribute_pnode));
// Create "actions" wrapper
ComPtr<IXmlElement> actions_wrapper_element;
ScopedHString actions_wrapper_str(L"actions");
RETURN_IF_FAILED(
doc->CreateElement(actions_wrapper_str, &actions_wrapper_element));
ComPtr<IXmlNode> actions_wrapper_node_tmp;
RETURN_IF_FAILED(actions_wrapper_element.As(&actions_wrapper_node_tmp));
// Append actions wrapper node to toast xml
ComPtr<IXmlNode> actions_wrapper_node;
RETURN_IF_FAILED(
root->AppendChild(actions_wrapper_node_tmp.Get(), &actions_wrapper_node));
ComPtr<IXmlNamedNodeMap> attributes_actions_wrapper;
RETURN_IF_FAILED(
actions_wrapper_node->get_Attributes(&attributes_actions_wrapper));
// Add a "Dismiss" button
// Create "action" tag
ComPtr<IXmlElement> action_element;
ScopedHString action_str(L"action");
RETURN_IF_FAILED(doc->CreateElement(action_str, &action_element));
ComPtr<IXmlNode> action_node_tmp;
RETURN_IF_FAILED(action_element.As(&action_node_tmp));
// Append action node to actions wrapper in toast xml
ComPtr<IXmlNode> action_node;
RETURN_IF_FAILED(
actions_wrapper_node->AppendChild(action_node_tmp.Get(), &action_node));
// Setup attributes for action
ComPtr<IXmlNamedNodeMap> action_attributes;
RETURN_IF_FAILED(action_node->get_Attributes(&action_attributes));
// Create activationType attribute
ComPtr<IXmlAttribute> activation_type_attribute;
ScopedHString activation_type_str(L"activationType");
RETURN_IF_FAILED(
doc->CreateAttribute(activation_type_str, &activation_type_attribute));
ComPtr<IXmlNode> activation_type_attribute_node;
RETURN_IF_FAILED(
activation_type_attribute.As(&activation_type_attribute_node));
// Set activationType attribute to system
ScopedHString activation_type_value(L"system");
if (!activation_type_value.success())
return E_FAIL;
ComPtr<IXmlText> activation_type_text;
RETURN_IF_FAILED(
doc->CreateTextNode(activation_type_value, &activation_type_text));
ComPtr<IXmlNode> activation_type_node;
RETURN_IF_FAILED(activation_type_text.As(&activation_type_node));
ComPtr<IXmlNode> activation_type_backup_node;
RETURN_IF_FAILED(activation_type_attribute_node->AppendChild(
activation_type_node.Get(), &activation_type_backup_node));
// Add activation type to the action attributes
ComPtr<IXmlNode> activation_type_attribute_pnode;
RETURN_IF_FAILED(action_attributes.Get()->SetNamedItem(
activation_type_attribute_node.Get(), &activation_type_attribute_pnode));
// Create arguments attribute
ComPtr<IXmlAttribute> arguments_attribute;
ScopedHString arguments_str(L"arguments");
RETURN_IF_FAILED(doc->CreateAttribute(arguments_str, &arguments_attribute));
ComPtr<IXmlNode> arguments_attribute_node;
RETURN_IF_FAILED(arguments_attribute.As(&arguments_attribute_node));
// Set arguments attribute to dismiss
ScopedHString arguments_value(L"dismiss");
if (!arguments_value.success())
return E_FAIL;
ComPtr<IXmlText> arguments_text;
RETURN_IF_FAILED(doc->CreateTextNode(arguments_value, &arguments_text));
ComPtr<IXmlNode> arguments_node;
RETURN_IF_FAILED(arguments_text.As(&arguments_node));
ComPtr<IXmlNode> arguments_backup_node;
RETURN_IF_FAILED(arguments_attribute_node->AppendChild(
arguments_node.Get(), &arguments_backup_node));
// Add arguments to the action attributes
ComPtr<IXmlNode> arguments_attribute_pnode;
RETURN_IF_FAILED(action_attributes.Get()->SetNamedItem(
arguments_attribute_node.Get(), &arguments_attribute_pnode));
// Create content attribute
ComPtr<IXmlAttribute> content_attribute;
ScopedHString content_str(L"content");
RETURN_IF_FAILED(doc->CreateAttribute(content_str, &content_attribute));
ComPtr<IXmlNode> content_attribute_node;
RETURN_IF_FAILED(content_attribute.As(&content_attribute_node));
// Set content attribute to Dismiss
ScopedHString content_value(l10n_util::GetWideString(IDS_APP_CLOSE));
if (!content_value.success())
return E_FAIL;
ComPtr<IXmlText> content_text;
RETURN_IF_FAILED(doc->CreateTextNode(content_value, &content_text));
ComPtr<IXmlNode> content_node;
RETURN_IF_FAILED(content_text.As(&content_node));
ComPtr<IXmlNode> content_backup_node;
RETURN_IF_FAILED(content_attribute_node->AppendChild(content_node.Get(),
&content_backup_node));
// Add content to the action attributes
ComPtr<IXmlNode> content_attribute_pnode;
return action_attributes.Get()->SetNamedItem(content_attribute_node.Get(),
&content_attribute_pnode);
}
HRESULT WindowsToastNotification::SetXmlScenarioType(
ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& scenario_type) {
ScopedHString tag(L"toast");
if (!tag.success())
return false;
ComPtr<IXmlNodeList> node_list;
RETURN_IF_FAILED(doc->GetElementsByTagName(tag, &node_list));
// Check that root "toast" node exists
ComPtr<IXmlNode> root;
RETURN_IF_FAILED(node_list->Item(0, &root));
// get attributes of root "toast" node
ComPtr<IXmlNamedNodeMap> toast_attributes;
RETURN_IF_FAILED(root->get_Attributes(&toast_attributes));
ComPtr<IXmlAttribute> scenario_attribute;
ScopedHString scenario_str(L"scenario");
RETURN_IF_FAILED(doc->CreateAttribute(scenario_str, &scenario_attribute));
ComPtr<IXmlNode> scenario_attribute_node;
RETURN_IF_FAILED(scenario_attribute.As(&scenario_attribute_node));
ScopedHString scenario_value(scenario_type);
if (!scenario_value.success())
return E_FAIL;
ComPtr<IXmlText> scenario_text;
RETURN_IF_FAILED(doc->CreateTextNode(scenario_value, &scenario_text));
ComPtr<IXmlNode> scenario_node;
RETURN_IF_FAILED(scenario_text.As(&scenario_node));
ComPtr<IXmlNode> scenario_backup_node;
RETURN_IF_FAILED(scenario_attribute_node->AppendChild(scenario_node.Get(),
&scenario_backup_node));
ComPtr<IXmlNode> scenario_attribute_pnode;
return toast_attributes.Get()->SetNamedItem(scenario_attribute_node.Get(),
&scenario_attribute_pnode);
}
HRESULT WindowsToastNotification::SetXmlAudioSilent(IXmlDocument* doc) {
ScopedHString tag(L"toast");
if (!tag.success())
return E_FAIL;
ComPtr<IXmlNodeList> node_list;
RETURN_IF_FAILED(doc->GetElementsByTagName(tag, &node_list));
ComPtr<IXmlNode> root;
RETURN_IF_FAILED(node_list->Item(0, &root));
ComPtr<IXmlElement> audio_element;
ScopedHString audio_str(L"audio");
RETURN_IF_FAILED(doc->CreateElement(audio_str, &audio_element));
ComPtr<IXmlNode> audio_node_tmp;
RETURN_IF_FAILED(audio_element.As(&audio_node_tmp));
// Append audio node to toast xml
ComPtr<IXmlNode> audio_node;
RETURN_IF_FAILED(root->AppendChild(audio_node_tmp.Get(), &audio_node));
// Create silent attribute
ComPtr<IXmlNamedNodeMap> attributes;
RETURN_IF_FAILED(audio_node->get_Attributes(&attributes));
ComPtr<IXmlAttribute> silent_attribute;
ScopedHString silent_str(L"silent");
RETURN_IF_FAILED(doc->CreateAttribute(silent_str, &silent_attribute));
ComPtr<IXmlNode> silent_attribute_node;
RETURN_IF_FAILED(silent_attribute.As(&silent_attribute_node));
// Set silent attribute to true
ScopedHString silent_value(L"true");
if (!silent_value.success())
return E_FAIL;
ComPtr<IXmlText> silent_text;
RETURN_IF_FAILED(doc->CreateTextNode(silent_value, &silent_text));
ComPtr<IXmlNode> silent_node;
RETURN_IF_FAILED(silent_text.As(&silent_node));
ComPtr<IXmlNode> child_node;
RETURN_IF_FAILED(
silent_attribute_node->AppendChild(silent_node.Get(), &child_node));
ComPtr<IXmlNode> silent_attribute_pnode;
return attributes.Get()->SetNamedItem(silent_attribute_node.Get(),
&silent_attribute_pnode);
}
HRESULT WindowsToastNotification::SetXmlText(IXmlDocument* doc,
const std::wstring& text) {
ScopedHString tag(L"binding");
if (!tag.success())
return E_FAIL;
ComPtr<IXmlNodeList> node_list;
RETURN_IF_FAILED(doc->GetElementsByTagName(tag, &node_list));
ComPtr<IXmlNode> node;
RETURN_IF_FAILED(node_list->Item(0, &node));
ComPtr<IXmlElement> text_element;
ScopedHString text_str(L"text");
RETURN_IF_FAILED(doc->CreateElement(text_str, &text_element));
ComPtr<IXmlNode> text_node_backup;
RETURN_IF_FAILED(text_element.As(&text_node_backup));
// Append text node to toast xml
ComPtr<IXmlNode> text_node;
RETURN_IF_FAILED(node->AppendChild(text_node_backup.Get(), &text_node));
return AppendTextToXml(doc, text_node.Get(), text);
}
HRESULT WindowsToastNotification::SetXmlImage(IXmlDocument* doc,
const std::wstring& icon_path,
const std::wstring& placement) {
ScopedHString tag(L"binding");
if (!tag.success())
return E_FAIL;
ComPtr<IXmlNodeList> node_list;
RETURN_IF_FAILED(doc->GetElementsByTagName(tag, &node_list));
ComPtr<IXmlNode> root;
RETURN_IF_FAILED(node_list->Item(0, &root));
ComPtr<IXmlElement> image_element;
ScopedHString image_str(L"image");
RETURN_IF_FAILED(doc->CreateElement(image_str, &image_element));
ComPtr<IXmlNode> image_node_backup;
RETURN_IF_FAILED(image_element.As(&image_node_backup));
// Append image node to toast xml
ComPtr<IXmlNode> image_node;
RETURN_IF_FAILED(root->AppendChild(image_node_backup.Get(), &image_node));
// Create image attributes
ComPtr<IXmlNamedNodeMap> attributes;
RETURN_IF_FAILED(image_node->get_Attributes(&attributes));
ComPtr<IXmlAttribute> placement_attribute;
ScopedHString placement_str(L"placement");
RETURN_IF_FAILED(doc->CreateAttribute(placement_str, &placement_attribute));
ComPtr<IXmlNode> placement_attribute_node;
RETURN_IF_FAILED(placement_attribute.As(&placement_attribute_node));
ScopedHString placement_value(placement);
if (!placement_value.success())
return E_FAIL;
ComPtr<IXmlText> placement_text;
RETURN_IF_FAILED(doc->CreateTextNode(placement_value, &placement_text));
ComPtr<IXmlNode> placement_node;
RETURN_IF_FAILED(placement_text.As(&placement_node));
ComPtr<IXmlNode> placement_backup_node;
RETURN_IF_FAILED(placement_attribute_node->AppendChild(
placement_node.Get(), &placement_backup_node));
{
ComPtr<IXmlNode> scenario_attribute_pnode;
RETURN_IF_FAILED(attributes.Get()->SetNamedItem(
placement_attribute_node.Get(), &scenario_attribute_pnode));
}
ComPtr<IXmlAttribute> src_attribute;
ScopedHString src_str(L"src");
RETURN_IF_FAILED(doc->CreateAttribute(src_str, &src_attribute));
ComPtr<IXmlNode> src_attribute_node;
RETURN_IF_FAILED(src_attribute.As(&src_attribute_node));
ScopedHString img_path(icon_path.c_str());
if (!img_path.success())
return E_FAIL;
ComPtr<IXmlText> src_text;
RETURN_IF_FAILED(doc->CreateTextNode(img_path, &src_text));
ComPtr<IXmlNode> src_node;
RETURN_IF_FAILED(src_text.As(&src_node));
ComPtr<IXmlNode> src_backup_node;
RETURN_IF_FAILED(
src_attribute_node->AppendChild(src_node.Get(), &src_backup_node));
{
ComPtr<IXmlNode> scenario_attribute_pnode;
RETURN_IF_FAILED(attributes.Get()->SetNamedItem(src_attribute_node.Get(),
&scenario_attribute_pnode));
}
return S_OK;
}
HRESULT WindowsToastNotification::SetLaunchParams(
ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& params) {
UINT32 length;
ComPtr<IXmlNodeList> nodeList;
RETURN_IF_FAILED(
doc->GetElementsByTagName(HStringReference(L"toast").Get(), &nodeList));
RETURN_IF_FAILED(nodeList->get_Length(&length));
ComPtr<IXmlNode> toastNode;
RETURN_IF_FAILED(nodeList->Item(0, &toastNode));
ComPtr<IXmlElement> toastElement;
RETURN_IF_FAILED(toastNode.As(&toastElement));
return toastElement->SetAttribute(HStringReference(L"launch").Get(),
HStringReference(params.c_str()).Get());
}
HRESULT WindowsToastNotification::AppendTextToXml(IXmlDocument* doc,
IXmlNode* node,
const std::wstring& text) {
ScopedHString str(text);
if (!str.success())
return E_FAIL;
ComPtr<IXmlText> xml_text;
RETURN_IF_FAILED(doc->CreateTextNode(str, &xml_text));
ComPtr<IXmlNode> text_node;
RETURN_IF_FAILED(xml_text.As(&text_node));
ComPtr<IXmlNode> append_node;
RETURN_IF_FAILED(node->AppendChild(text_node.Get(), &append_node));
return S_OK;
}
HRESULT WindowsToastNotification::AddAction(
ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& type,
const std::wstring& content,
const std::wstring& arguments, // equal to id for input
const std::wstring& icon_path,
const std::wstring& placeholder,
const std::wstring& hint_inputId) {
ComPtr<IXmlNodeList> nodeList;
RETURN_IF_FAILED(
doc->GetElementsByTagName(HStringReference(L"actions").Get(), &nodeList));
UINT32 length;
RETURN_IF_FAILED(nodeList->get_Length(&length));
ComPtr<IXmlNode> actionsNode;
if (length > 0) {
RETURN_IF_FAILED(nodeList->Item(0, &actionsNode));
} else {
RETURN_IF_FAILED(
doc->GetElementsByTagName(HStringReference(L"toast").Get(), &nodeList));
RETURN_IF_FAILED(nodeList->get_Length(&length));
ComPtr<IXmlNode> toastNode;
RETURN_IF_FAILED(nodeList->Item(0, &toastNode));
ComPtr<IXmlElement> toastElement;
RETURN_IF_FAILED(toastNode.As(&toastElement));
ComPtr<IXmlElement> actionsElement;
RETURN_IF_FAILED(doc->CreateElement(HStringReference(L"actions").Get(),
&actionsElement));
RETURN_IF_FAILED(actionsElement.As(&actionsNode));
ComPtr<IXmlNode> appendedChild;
RETURN_IF_FAILED(toastNode->AppendChild(actionsNode.Get(), &appendedChild));
}
ComPtr<IXmlElement> actionElement;
if (type == L"button") {
// BUTTON
RETURN_IF_FAILED(
doc->CreateElement(HStringReference(L"action").Get(), &actionElement));
RETURN_IF_FAILED(
actionElement->SetAttribute(HStringReference(L"content").Get(),
HStringReference(content.c_str()).Get()));
RETURN_IF_FAILED(
actionElement->SetAttribute(HStringReference(L"arguments").Get(),
HStringReference(arguments.c_str()).Get()));
RETURN_IF_FAILED(
actionElement->SetAttribute(HStringReference(L"imageUri").Get(),
HStringReference(icon_path.c_str()).Get()));
RETURN_IF_FAILED(actionElement->SetAttribute(
HStringReference(L"hint-inputId").Get(),
HStringReference(hint_inputId.c_str()).Get()));
} else {
// INPUT
RETURN_IF_FAILED(
doc->CreateElement(HStringReference(L"input").Get(), &actionElement));
RETURN_IF_FAILED(
actionElement->SetAttribute(HStringReference(L"id").Get(),
HStringReference(arguments.c_str()).Get()));
RETURN_IF_FAILED(actionElement->SetAttribute(
HStringReference(L"type").Get(), HStringReference(type.c_str()).Get()));
RETURN_IF_FAILED(actionElement->SetAttribute(
HStringReference(L"placeHolderContent").Get(),