-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaddon.cpp
More file actions
146 lines (120 loc) · 4.52 KB
/
addon.cpp
File metadata and controls
146 lines (120 loc) · 4.52 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
#include <winrt/windows.data.xml.dom.h>
#include <winrt/windows.foundation.metadata.h>
#include <winrt/windows.ui.notifications.h>
#include <windows.h>
#include <string.h>
#include "napi.h"
using namespace winrt;
using namespace Windows::UI::Notifications;
using namespace Windows::Data::Xml::Dom;
static bool are_notifications_supported =
Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent(
L"Windows.Foundation.UniversalApiContract", static_cast<uint16_t>(65536));
static bool is_expires_on_reboot_supported =
Windows::Foundation::Metadata::ApiInformation::IsPropertyPresent(
L"Windows.UI.Notifications.ToastNotification", L"ExpiresOnReboot");
void EnsureNotificationsSupported(Napi::Env env) {
if (!are_notifications_supported) {
throw Napi::Error::New(env, "Unsupported on current OS");
}
}
Napi::Error FromWinRTError(Napi::Env env, const winrt::hresult_error& ex) {
static char buf[1024];
auto code = static_cast<int>(ex.code());
auto message = ex.message();
snprintf(
buf,
sizeof(buf),
"WinRT exception code: %d, msg: %ls", code, message.c_str());
throw Napi::Error::New(env, buf);
}
void ShowNotification(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 5) {
throw Napi::Error::New(env, "Invalid argument count");
}
auto appId = info[0].As<Napi::String>();
auto toastXml = info[1].As<Napi::String>();
auto tag = info[2].As<Napi::String>();
auto group = info[3].As<Napi::String>();
auto expiresOnReboot = info[4].As<Napi::Boolean>();
if (!appId.IsString() || !toastXml.IsString() ||
!tag.IsString() || !group.IsString() || !expiresOnReboot.IsBoolean()) {
throw Napi::Error::New(env, "Invalid argument type");
}
EnsureNotificationsSupported(env);
try {
XmlDocument xml;
xml.LoadXml(to_hstring(toastXml.Utf8Value()));
ToastNotification notification(xml);
notification.Tag(to_hstring(tag.Utf8Value()));
notification.Group(to_hstring(group.Utf8Value()));
if (is_expires_on_reboot_supported) {
notification.ExpiresOnReboot(expiresOnReboot.Value());
}
auto notifier = ToastNotificationManager::CreateToastNotifier(
to_hstring(appId.Utf8Value()));
notifier.Show(notification);
} catch (winrt::hresult_error const& ex) {
throw FromWinRTError(env, ex);
}
}
void RemoveNotification(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 3) {
throw Napi::Error::New(env, "Invalid argument count");
}
auto appId = info[0].As<Napi::String>();
auto tag = info[1].As<Napi::String>();
auto group = info[2].As<Napi::String>();
if (!appId.IsString() || !tag.IsString() || !group.IsString()) {
throw Napi::Error::New(env, "Invalid argument type");
}
EnsureNotificationsSupported(env);
try {
ToastNotificationManager::History().Remove(
to_hstring(tag.Utf8Value()),
to_hstring(group.Utf8Value()),
to_hstring(appId.Utf8Value()));
} catch (winrt::hresult_error const& ex) {
throw FromWinRTError(env, ex);
}
}
void ClearHistory(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
throw Napi::Error::New(env, "Invalid argument count");
}
auto appId = info[0].As<Napi::String>();
if (!appId.IsString()) {
throw Napi::Error::New(env, "Invalid argument type");
}
EnsureNotificationsSupported(env);
try {
ToastNotificationManager::History().Clear(
to_hstring(appId.Utf8Value()));
} catch (winrt::hresult_error const& ex) {
throw FromWinRTError(env, ex);
}
}
void SendDummyKeystroke(const Napi::CallbackInfo& info) {
// See https://chromium.googlesource.com/chromium/src.git/+/5c432815bbb22210f7c995bbb508359f64baadf5/chrome/notification_helper/notification_activator.cc#155
INPUT inputs[2] = {};
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki.dwFlags = 0; // Key down
inputs[1] = inputs[0];
inputs[1].ki.dwFlags |= KEYEVENTF_KEYUP; // Key up
SendInput(2, inputs, sizeof(inputs[0]));
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "showNotification"),
Napi::Function::New(env, ShowNotification));
exports.Set(Napi::String::New(env, "removeNotification"),
Napi::Function::New(env, RemoveNotification));
exports.Set(Napi::String::New(env, "clearHistory"),
Napi::Function::New(env, ClearHistory));
exports.Set(Napi::String::New(env, "sendDummyKeystroke"),
Napi::Function::New(env, SendDummyKeystroke));
return exports;
}
NODE_API_MODULE(simple-windows-notifications, Init)