@ng-native/desktop 0.26.1 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/README.md +6 -3
  2. package/bin/app-config.mjs +1 -0
  3. package/bin/archive.mjs +166 -0
  4. package/bin/autolink.mjs +61 -30
  5. package/bin/cli.mjs +127 -27
  6. package/bin/host.mjs +255 -58
  7. package/bin/package-linux.mjs +1394 -0
  8. package/bin/package.mjs +171 -35
  9. package/bin/plist.mjs +6 -2
  10. package/bin/png-icon.mjs +315 -0
  11. package/bin/prebuilt.mjs +147 -26
  12. package/bin/toolchain.mjs +258 -19
  13. package/native/CMakeLists.txt +64 -6
  14. package/native/appkit/CMakeLists.txt +2 -1
  15. package/native/appkit/appkit-backend.h +1 -0
  16. package/native/appkit/appkit-backend.mm +13 -0
  17. package/native/appkit/header-toolbar.mm +28 -25
  18. package/native/appkit/host-view.h +10 -0
  19. package/native/appkit/host-view.mm +146 -40
  20. package/native/appkit/input-replay.h +6 -0
  21. package/native/appkit/input-replay.mm +21 -12
  22. package/native/appkit/main.mm +11 -7
  23. package/native/appkit/native-controls.mm +13 -1
  24. package/native/appkit/split-view.mm +7 -13
  25. package/native/appkit/text-input-field.h +4 -0
  26. package/native/appkit/text-input-field.mm +20 -1
  27. package/native/cmake/curl.cmake +79 -0
  28. package/native/cmake/libraries/react-native-reanimated.cmake +5 -0
  29. package/native/cmake/libraries/react-native-worklets.cmake +5 -0
  30. package/native/cmake/linked-modules.cmake +20 -4
  31. package/native/cmake/react-native.cmake +18 -1
  32. package/native/core/include/ngn/accent-colors.h +38 -0
  33. package/native/core/include/ngn/accessibility-relations.h +65 -0
  34. package/native/core/include/ngn/accessibility.h +26 -0
  35. package/native/core/include/ngn/animated-frame-loop.h +56 -0
  36. package/native/core/include/ngn/app-info.h +24 -8
  37. package/native/core/include/ngn/app-menus.h +14 -1
  38. package/native/core/include/ngn/app-windows.h +15 -2
  39. package/native/core/include/ngn/aria-role.h +96 -0
  40. package/native/core/include/ngn/audio.h +2 -0
  41. package/native/core/include/ngn/camera.h +9 -0
  42. package/native/core/include/ngn/control-sizes.h +7 -2
  43. package/native/core/include/ngn/crash-report.h +31 -0
  44. package/native/core/include/ngn/curl-network.h +93 -0
  45. package/native/core/include/ngn/dbus-menu.h +39 -0
  46. package/native/core/include/ngn/default-menu.h +36 -0
  47. package/native/core/include/ngn/desktop-expo.h +7 -1
  48. package/native/core/include/ngn/desktop-modules.h +22 -0
  49. package/native/core/include/ngn/desktop-props.h +21 -1
  50. package/native/core/include/ngn/desktop-view-props.h +6 -0
  51. package/native/core/include/ngn/focus-order.h +10 -0
  52. package/native/core/include/ngn/header-bar.h +22 -0
  53. package/native/core/include/ngn/host-os.h +6 -0
  54. package/native/core/include/ngn/host-reload.h +21 -0
  55. package/native/core/include/ngn/host-state.h +23 -0
  56. package/native/core/include/ngn/image-source.h +26 -1
  57. package/native/core/include/ngn/input-script.h +3 -3
  58. package/native/core/include/ngn/input.h +4 -1
  59. package/native/core/include/ngn/launch-frame.h +21 -0
  60. package/native/core/include/ngn/launch-settings.h +23 -5
  61. package/native/core/include/ngn/mounting-backend.h +5 -0
  62. package/native/core/include/ngn/mounting-manager.h +1 -0
  63. package/native/core/include/ngn/native-controls.h +26 -5
  64. package/native/core/include/ngn/notification-schedule.h +74 -0
  65. package/native/core/include/ngn/notifications.h +2 -1
  66. package/native/core/include/ngn/openssl-update-crypto.h +15 -0
  67. package/native/core/include/ngn/photo-library.h +60 -20
  68. package/native/core/include/ngn/posix-locale.h +43 -0
  69. package/native/core/include/ngn/runtime.h +5 -0
  70. package/native/core/include/ngn/safe-areas.h +14 -2
  71. package/native/core/include/ngn/split-view.h +78 -0
  72. package/native/core/include/ngn/surface-metrics.h +7 -0
  73. package/native/core/include/ngn/system.h +14 -0
  74. package/native/core/include/ngn/text-input.h +13 -0
  75. package/native/core/include/ngn/text-units.h +38 -0
  76. package/native/core/include/ngn/updates.h +14 -2
  77. package/native/core/include/ngn/view-box.h +40 -0
  78. package/native/core/include/ngn/view-transform.h +15 -0
  79. package/native/core/include/ngn/web-browser.h +46 -0
  80. package/native/core/src/accent-colors.cpp +88 -0
  81. package/native/core/src/accessibility-relations.cpp +138 -0
  82. package/native/core/src/accessibility.cpp +50 -0
  83. package/native/core/src/animated-frame-loop.cpp +166 -0
  84. package/native/core/src/app-menus.cpp +26 -9
  85. package/native/core/src/app-windows.cpp +27 -6
  86. package/native/core/src/aria-role.cpp +127 -0
  87. package/native/core/src/control-sizes.cpp +9 -5
  88. package/native/core/src/crash-report.cpp +196 -0
  89. package/native/core/src/curl-network.cpp +1562 -0
  90. package/native/core/src/dbus-menu.cpp +71 -0
  91. package/native/core/src/default-menu.cpp +134 -0
  92. package/native/core/src/desktop-view-props.cpp +14 -0
  93. package/native/core/src/focus-order.cpp +19 -0
  94. package/native/core/src/header-bar.cpp +20 -0
  95. package/native/core/src/host-os.cpp +19 -0
  96. package/native/core/src/host-reload.cpp +45 -0
  97. package/native/core/src/host-state.cpp +8 -0
  98. package/native/core/src/image-source.cpp +90 -0
  99. package/native/core/src/input-dispatcher.cpp +27 -5
  100. package/native/core/src/input-script.cpp +3 -2
  101. package/native/core/src/launch-settings.cpp +76 -0
  102. package/native/core/src/libraries/react-native-reanimated.cpp +6 -1
  103. package/native/core/src/modules/app-info.cpp +55 -6
  104. package/native/core/src/modules/app-modules.cpp +1 -1
  105. package/native/core/src/modules/audio.cpp +4 -2
  106. package/native/core/src/modules/camera.cpp +8 -11
  107. package/native/core/src/modules/desktop-expo.cpp +10 -5
  108. package/native/core/src/modules/desktop-modules.cpp +106 -41
  109. package/native/core/src/modules/file-photo-picker.cpp +408 -0
  110. package/native/core/src/modules/notifications.cpp +13 -17
  111. package/native/core/src/modules/photo-library.cpp +42 -13
  112. package/native/core/src/modules/web-browser.cpp +89 -0
  113. package/native/core/src/mounting-manager.cpp +12 -0
  114. package/native/core/src/native-controls.cpp +42 -1
  115. package/native/core/src/notification-schedule.cpp +429 -0
  116. package/native/core/src/openssl-update-crypto.cpp +83 -0
  117. package/native/core/src/posix-locale.cpp +192 -0
  118. package/native/core/src/runtime.cpp +30 -1
  119. package/native/core/src/safe-areas.cpp +19 -4
  120. package/native/core/src/spawn-rules.cpp +4 -1
  121. package/native/core/src/split-view.cpp +93 -0
  122. package/native/core/src/system.cpp +19 -0
  123. package/native/core/src/text-input.cpp +18 -1
  124. package/native/core/src/text-units.cpp +99 -0
  125. package/native/core/src/updates.cpp +76 -17
  126. package/native/core/src/view-box.cpp +75 -40
  127. package/native/core/src/view-transform.cpp +28 -0
  128. package/native/docker/Dockerfile +38 -0
  129. package/native/docker/Dockerfile.fedora +36 -0
  130. package/native/docker/glibc-floor.sh +49 -0
  131. package/native/gtk/CMakeLists.txt +137 -0
  132. package/native/gtk/accent-colour.cpp +57 -0
  133. package/native/gtk/accent-colour.h +41 -0
  134. package/native/gtk/accessibility-settings.cpp +159 -0
  135. package/native/gtk/accessibility-settings.h +62 -0
  136. package/native/gtk/accessible-text.cpp +151 -0
  137. package/native/gtk/accessible-text.h +49 -0
  138. package/native/gtk/accessible.cpp +306 -0
  139. package/native/gtk/accessible.h +75 -0
  140. package/native/gtk/alerts.cpp +267 -0
  141. package/native/gtk/alerts.h +30 -0
  142. package/native/gtk/animation-frames.cpp +159 -0
  143. package/native/gtk/animation-frames.h +20 -0
  144. package/native/gtk/app-info.cpp +91 -0
  145. package/native/gtk/app-info.h +25 -0
  146. package/native/gtk/app-windows.cpp +439 -0
  147. package/native/gtk/app-windows.h +150 -0
  148. package/native/gtk/audio.cpp +370 -0
  149. package/native/gtk/audio.h +54 -0
  150. package/native/gtk/auth-session.cpp +73 -0
  151. package/native/gtk/auth-session.h +16 -0
  152. package/native/gtk/battery.cpp +110 -0
  153. package/native/gtk/battery.h +19 -0
  154. package/native/gtk/box-paint.cpp +262 -0
  155. package/native/gtk/box-paint.h +32 -0
  156. package/native/gtk/cairo-canvas.cpp +755 -0
  157. package/native/gtk/cairo-canvas.h +49 -0
  158. package/native/gtk/camera.cpp +909 -0
  159. package/native/gtk/camera.h +69 -0
  160. package/native/gtk/capture.cpp +46 -0
  161. package/native/gtk/capture.h +19 -0
  162. package/native/gtk/clock-format.cpp +151 -0
  163. package/native/gtk/clock-format.h +36 -0
  164. package/native/gtk/crypto.cpp +75 -0
  165. package/native/gtk/crypto.h +14 -0
  166. package/native/gtk/dbus-properties.cpp +131 -0
  167. package/native/gtk/dbus-properties.h +61 -0
  168. package/native/gtk/drag-and-drop.cpp +339 -0
  169. package/native/gtk/drag-and-drop.h +30 -0
  170. package/native/gtk/file-dialogs.cpp +239 -0
  171. package/native/gtk/file-dialogs.h +21 -0
  172. package/native/gtk/file-system.cpp +212 -0
  173. package/native/gtk/file-system.h +25 -0
  174. package/native/gtk/focus.cpp +282 -0
  175. package/native/gtk/focus.h +37 -0
  176. package/native/gtk/fonts.cpp +89 -0
  177. package/native/gtk/fonts.h +22 -0
  178. package/native/gtk/gst-frames.cpp +150 -0
  179. package/native/gtk/gst-frames.h +47 -0
  180. package/native/gtk/gst-playback.cpp +496 -0
  181. package/native/gtk/gst-playback.h +81 -0
  182. package/native/gtk/gtk-backend.cpp +690 -0
  183. package/native/gtk/gtk-backend.h +134 -0
  184. package/native/gtk/host-sources.cpp +354 -0
  185. package/native/gtk/host-sources.h +46 -0
  186. package/native/gtk/image-manipulator.cpp +254 -0
  187. package/native/gtk/image-manipulator.h +30 -0
  188. package/native/gtk/image-view.cpp +256 -0
  189. package/native/gtk/image-view.h +54 -0
  190. package/native/gtk/input-replay.cpp +299 -0
  191. package/native/gtk/input-replay.h +36 -0
  192. package/native/gtk/launcher-badge.cpp +29 -0
  193. package/native/gtk/launcher-badge.h +21 -0
  194. package/native/gtk/locales.cpp +131 -0
  195. package/native/gtk/locales.h +23 -0
  196. package/native/gtk/location.cpp +274 -0
  197. package/native/gtk/location.h +19 -0
  198. package/native/gtk/main.cpp +1092 -0
  199. package/native/gtk/menus.cpp +447 -0
  200. package/native/gtk/menus.h +100 -0
  201. package/native/gtk/native-controls.cpp +831 -0
  202. package/native/gtk/native-controls.h +58 -0
  203. package/native/gtk/network-state.cpp +72 -0
  204. package/native/gtk/network-state.h +24 -0
  205. package/native/gtk/notifications.cpp +235 -0
  206. package/native/gtk/notifications.h +30 -0
  207. package/native/gtk/pango-text.cpp +274 -0
  208. package/native/gtk/pango-text.h +73 -0
  209. package/native/gtk/portal.cpp +451 -0
  210. package/native/gtk/portal.h +71 -0
  211. package/native/gtk/printing.cpp +338 -0
  212. package/native/gtk/printing.h +34 -0
  213. package/native/gtk/proxy-resolver.cpp +74 -0
  214. package/native/gtk/proxy-resolver.h +20 -0
  215. package/native/gtk/screens.cpp +1461 -0
  216. package/native/gtk/screens.h +274 -0
  217. package/native/gtk/scroll-view.cpp +414 -0
  218. package/native/gtk/scroll-view.h +82 -0
  219. package/native/gtk/secret-store.cpp +120 -0
  220. package/native/gtk/secret-store.h +27 -0
  221. package/native/gtk/settings-portal.cpp +129 -0
  222. package/native/gtk/settings-portal.h +47 -0
  223. package/native/gtk/split-view.cpp +1105 -0
  224. package/native/gtk/split-view.h +195 -0
  225. package/native/gtk/status-notifier.cpp +487 -0
  226. package/native/gtk/status-notifier.h +43 -0
  227. package/native/gtk/surface-input.cpp +251 -0
  228. package/native/gtk/surface-input.h +57 -0
  229. package/native/gtk/system-access.cpp +295 -0
  230. package/native/gtk/system-access.h +37 -0
  231. package/native/gtk/text-field.cpp +827 -0
  232. package/native/gtk/text-field.h +78 -0
  233. package/native/gtk/video.cpp +527 -0
  234. package/native/gtk/video.h +47 -0
  235. package/native/gtk/view-widget.cpp +431 -0
  236. package/native/gtk/view-widget.h +105 -0
  237. package/native/gtk/window-scale.cpp +95 -0
  238. package/native/gtk/window-scale.h +35 -0
  239. package/native/scripts/atspi-check.py +310 -0
  240. package/native/scripts/atspi-check.test.py +48 -0
  241. package/native/scripts/build.sh +20 -3
  242. package/native/scripts/compilers.sh +17 -0
  243. package/native/scripts/deb-depends-check.sh +22 -0
  244. package/native/scripts/fetch.sh +23 -2
  245. package/native/scripts/launch-cell.sh +157 -0
  246. package/native/scripts/launch-probe.sh +14 -3
  247. package/native/scripts/make-prebuilt.mjs +133 -44
  248. package/native/scripts/prebuilt-host-cache.mjs +71 -40
  249. package/native/scripts/reset-for-compilers.sh +44 -0
  250. package/native/scripts/start-xvfb.sh +35 -0
  251. package/native/shim/paragraph-platform/react/renderer/components/text/HostPlatformParagraphProps.h +35 -0
  252. package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +5 -2
  253. package/native/shim/worklets/linux-reanimated-props.h +14 -0
  254. package/native/shim/worklets/linux-worklets.h +23 -0
  255. package/package.json +6 -3
  256. package/prebuilt.json +70 -6
@@ -77,7 +77,7 @@ NativeControlProps::NativeControlProps(
77
77
  controlSize(convertRawProp(context, rawProps, "controlSize", sourceProps.controlSize, ControlSize::Regular)),
78
78
  enabled(enabledFrom(context, rawProps, sourceProps.enabled)),
79
79
  title(convertRawProp(context, rawProps, "title", sourceProps.title, {})),
80
- kind(convertRawProp(context, rawProps, "kind", sourceProps.kind, std::string{"default"})),
80
+ kind(convertRawProp(context, rawProps, "kind", sourceProps.kind, {})),
81
81
  sfSymbol(convertRawProp(context, rawProps, "sfSymbol", sourceProps.sfSymbol, {})),
82
82
  value(convertRawProp(context, rawProps, "value", sourceProps.value, false)),
83
83
  animating(convertRawProp(context, rawProps, "animating", sourceProps.animating, true)),
@@ -91,6 +91,7 @@ NativeControlProps::NativeControlProps(
91
91
  minimumDate(convertRawProp(context, rawProps, "minimumDate", sourceProps.minimumDate, {})),
92
92
  maximumDate(convertRawProp(context, rawProps, "maximumDate", sourceProps.maximumDate, {})),
93
93
  mode(convertRawProp(context, rawProps, "mode", sourceProps.mode, std::string{"date"})),
94
+ minuteInterval(convertRawProp(context, rawProps, "minuteInterval", sourceProps.minuteInterval, 1)),
94
95
  display(convertRawProp(context, rawProps, "displayIOS", sourceProps.display, std::string{"default"})),
95
96
  text(convertRawProp(context, rawProps, "text", sourceProps.text, {})),
96
97
  mostRecentEventCount(
@@ -126,4 +127,44 @@ std::optional<ControlKind> controlKind(std::string_view componentName) {
126
127
  return std::nullopt;
127
128
  }
128
129
 
130
+ bool twelveHourClock(std::string_view timeFormat) {
131
+ for (size_t i = 0; i + 1 < timeFormat.size(); ++i) {
132
+ if (timeFormat[i] != '%') {
133
+ continue;
134
+ }
135
+ char conversion = timeFormat[++i];
136
+ if (conversion == 'I' || conversion == 'l' || conversion == 'r' || conversion == 'p' || conversion == 'P') {
137
+ return true;
138
+ }
139
+ }
140
+ return false;
141
+ }
142
+
143
+ int dayHour(int clockHour, bool afterNoon) {
144
+ return clockHour % 12 + (afterNoon ? 12 : 0);
145
+ }
146
+
147
+ int clockHour(int dayHour) {
148
+ return dayHour % 12 == 0 ? 12 : dayHour % 12;
149
+ }
150
+
151
+ std::optional<bool> clockFormatTwelveHour(std::string_view clockFormat) {
152
+ if (clockFormat == "12h") {
153
+ return true;
154
+ }
155
+ if (clockFormat == "24h") {
156
+ return false;
157
+ }
158
+ return std::nullopt;
159
+ }
160
+
161
+ int pickerMinuteInterval(int minuteInterval) {
162
+ return minuteInterval >= 1 && minuteInterval <= 30 && 60 % minuteInterval == 0 ? minuteInterval : 1;
163
+ }
164
+
165
+ int minuteOnInterval(int minute, int minuteInterval) {
166
+ int step = pickerMinuteInterval(minuteInterval);
167
+ return minute - minute % step;
168
+ }
169
+
129
170
  } // namespace ngn
@@ -0,0 +1,429 @@
1
+ #include "ngn/notification-schedule.h"
2
+
3
+ #include <folly/json.h>
4
+
5
+ #include <algorithm>
6
+ #include <cmath>
7
+ #include <ctime>
8
+ #include <fstream>
9
+ #include <mutex>
10
+ #include <sstream>
11
+
12
+ namespace ngn {
13
+
14
+ namespace {
15
+
16
+ // The units a calendar trigger names, largest first. A weekday is a day's.
17
+ enum Level { kYear, kMonth, kDay, kHour, kMinute, kSecond };
18
+
19
+ // The days in 400 Gregorian years: after it every date falls on the weekday it did.
20
+ constexpr int kDaysInCycle = 146097;
21
+
22
+ int levelOf(const std::string &name) {
23
+ if (name == "year") return kYear;
24
+ if (name == "month") return kMonth;
25
+ if (name == "day" || name == "weekday") return kDay;
26
+ if (name == "hour") return kHour;
27
+ if (name == "minute") return kMinute;
28
+ return kSecond;
29
+ }
30
+
31
+ // The values a unit may take: the one named, its first when it is smaller than every unit named,
32
+ // or any.
33
+ std::vector<int> candidates(const std::map<std::string, int> &components, const char *name, int smallest, int low, int high) {
34
+ if (auto it = components.find(name); it != components.end()) {
35
+ return {it->second};
36
+ }
37
+ if (levelOf(name) > smallest) {
38
+ return {low};
39
+ }
40
+ std::vector<int> out;
41
+ for (int v = low; v <= high; ++v) {
42
+ out.push_back(v);
43
+ }
44
+ return out;
45
+ }
46
+
47
+ bool matches(const std::map<std::string, int> &components, const char *name, int value) {
48
+ auto it = components.find(name);
49
+ return it == components.end() || it->second == value;
50
+ }
51
+
52
+ double millis(time_t t) {
53
+ return static_cast<double>(t) * 1000;
54
+ }
55
+
56
+ folly::dynamic triggerRecord(const NotificationTrigger &trigger) {
57
+ folly::dynamic components = folly::dynamic::object();
58
+ for (const auto &[name, value] : trigger.components) {
59
+ components[name] = value;
60
+ }
61
+ return folly::dynamic::object("kind", static_cast<int>(trigger.kind))("seconds", trigger.seconds)("date", trigger.date)(
62
+ "repeats", trigger.repeats)("components", std::move(components));
63
+ }
64
+
65
+ NotificationTrigger triggerFromRecord(const folly::dynamic &record) {
66
+ NotificationTrigger out;
67
+ out.kind = static_cast<NotificationTrigger::Kind>(record.getDefault("kind", 0).asInt());
68
+ out.seconds = record.getDefault("seconds", 0).asDouble();
69
+ out.date = record.getDefault("date", 0).asDouble();
70
+ out.repeats = record.getDefault("repeats", false).asBool();
71
+ auto components = record.getDefault("components", folly::dynamic::object());
72
+ for (const auto &[name, value] : components.items()) {
73
+ out.components[name.asString()] = static_cast<int>(value.asInt());
74
+ }
75
+ return out;
76
+ }
77
+
78
+ folly::dynamic requestRecord(const NotificationRequest &request) {
79
+ auto json = notificationRequestJson(request);
80
+ json["trigger"] = triggerRecord(request.trigger);
81
+ return json;
82
+ }
83
+
84
+ std::optional<NotificationRequest> requestFromRecord(const folly::dynamic &record) {
85
+ auto parsed = parseNotificationRequest(folly::dynamic::object("identifier", record["identifier"])("content", record["content"]));
86
+ auto *request = std::get_if<NotificationRequest>(&parsed);
87
+ if (!request) {
88
+ return std::nullopt;
89
+ }
90
+ request->trigger = triggerFromRecord(record["trigger"]);
91
+ return *request;
92
+ }
93
+
94
+ } // namespace
95
+
96
+ std::optional<double> nextCalendarMatch(const std::map<std::string, int> &components, double after) {
97
+ int smallest = kYear;
98
+ for (const auto &[name, value] : components) {
99
+ smallest = std::max(smallest, levelOf(name));
100
+ }
101
+ if (components.empty()) {
102
+ smallest = kSecond;
103
+ }
104
+ // The day `after` is in, then each one after it, in local time. The Gregorian calendar repeats,
105
+ // weekdays included, every 400 years, so a rule that has not matched in that many days never will.
106
+ time_t start = static_cast<time_t>(std::floor(after / 1000));
107
+ struct tm day{};
108
+ localtime_r(&start, &day);
109
+ int year = day.tm_year, month = day.tm_mon, date = day.tm_mday;
110
+ auto hours = candidates(components, "hour", smallest, 0, 23);
111
+ auto minutes = candidates(components, "minute", smallest, 0, 59);
112
+ auto seconds = candidates(components, "second", smallest, 0, 59);
113
+ // A month or a day smaller than every unit named starts at its first: `{ year: 2030 }` is the
114
+ // first of January.
115
+ bool firstMonth = !components.count("month") && kMonth > smallest;
116
+ bool firstDay = !components.count("day") && !components.count("weekday") && kDay > smallest;
117
+ // A named year not yet come starts on its first day, and only that year is searched.
118
+ int last = kDaysInCycle;
119
+ if (auto it = components.find("year"); it != components.end() && it->second - 1900 > year) {
120
+ year = it->second - 1900;
121
+ month = 0;
122
+ date = 1;
123
+ last = 366;
124
+ }
125
+ for (int offset = 0; offset <= last; ++offset) {
126
+ // Noon, so a daylight saving change on the way never moves the date.
127
+ struct tm noon{};
128
+ noon.tm_year = year;
129
+ noon.tm_mon = month;
130
+ noon.tm_mday = date + offset;
131
+ noon.tm_hour = 12;
132
+ noon.tm_isdst = -1;
133
+ mktime(&noon);
134
+ if (auto it = components.find("year"); it != components.end() && noon.tm_year + 1900 > it->second) {
135
+ return std::nullopt;
136
+ }
137
+ if (!matches(components, "year", noon.tm_year + 1900) || !matches(components, "month", noon.tm_mon + 1) ||
138
+ !matches(components, "day", noon.tm_mday) || !matches(components, "weekday", noon.tm_wday + 1) ||
139
+ (firstMonth && noon.tm_mon != 0) || (firstDay && noon.tm_mday != 1)) {
140
+ continue;
141
+ }
142
+ for (int h : hours) {
143
+ for (int m : minutes) {
144
+ for (int s : seconds) {
145
+ struct tm at{};
146
+ at.tm_year = noon.tm_year;
147
+ at.tm_mon = noon.tm_mon;
148
+ at.tm_mday = noon.tm_mday;
149
+ at.tm_hour = h;
150
+ at.tm_min = m;
151
+ at.tm_sec = s;
152
+ at.tm_isdst = -1;
153
+ // A time a spring change skips comes out an hour on, as the clock reads then.
154
+ double t = millis(mktime(&at));
155
+ if (t > after) {
156
+ return t;
157
+ }
158
+ }
159
+ }
160
+ }
161
+ }
162
+ return std::nullopt;
163
+ }
164
+
165
+ std::optional<double> firstFireDate(const NotificationTrigger &trigger, double now) {
166
+ switch (trigger.kind) {
167
+ case NotificationTrigger::Kind::Now:
168
+ return now;
169
+ case NotificationTrigger::Kind::Interval:
170
+ return now + trigger.seconds * 1000;
171
+ case NotificationTrigger::Kind::Date:
172
+ return trigger.date > now ? std::optional<double>(trigger.date) : std::nullopt;
173
+ case NotificationTrigger::Kind::Calendar:
174
+ return nextCalendarMatch(trigger.components, now);
175
+ case NotificationTrigger::Kind::Push:
176
+ return std::nullopt;
177
+ }
178
+ return std::nullopt;
179
+ }
180
+
181
+ struct NotificationSchedule::State : std::enable_shared_from_this<NotificationSchedule::State> {
182
+ struct Entry {
183
+ NotificationRequest request;
184
+ double next{0};
185
+ };
186
+
187
+ std::string path;
188
+ Clock clock;
189
+ std::function<void(const NotificationRequest &)> deliver;
190
+ mutable std::mutex mutex;
191
+ std::vector<Entry> pending;
192
+ std::vector<std::pair<double, NotificationRequest>> delivered;
193
+ std::function<void()> cancelTimer;
194
+ bool started{false};
195
+
196
+ void load() {
197
+ std::ifstream in(path, std::ios::binary);
198
+ if (!in) {
199
+ return;
200
+ }
201
+ std::stringstream text;
202
+ text << in.rdbuf();
203
+ try {
204
+ auto json = folly::parseJson(text.str());
205
+ for (const auto &item : json.getDefault("pending", folly::dynamic::array())) {
206
+ if (auto request = requestFromRecord(item["request"])) {
207
+ pending.push_back({*request, item["next"].asDouble()});
208
+ }
209
+ }
210
+ for (const auto &item : json.getDefault("delivered", folly::dynamic::array())) {
211
+ if (auto request = requestFromRecord(item["request"])) {
212
+ delivered.emplace_back(item["date"].asDouble(), *request);
213
+ }
214
+ }
215
+ } catch (const std::exception &) {
216
+ // A file that does not read is a schedule with nothing in it.
217
+ pending.clear();
218
+ delivered.clear();
219
+ }
220
+ }
221
+
222
+ // Under the lock. False when the file was not committed; the one before it is then as it was.
223
+ bool save() const {
224
+ folly::dynamic out = folly::dynamic::object("pending", folly::dynamic::array())("delivered", folly::dynamic::array());
225
+ for (const auto &entry : pending) {
226
+ out["pending"].push_back(folly::dynamic::object("request", requestRecord(entry.request))("next", entry.next));
227
+ }
228
+ for (const auto &[date, request] : delivered) {
229
+ out["delivered"].push_back(folly::dynamic::object("request", requestRecord(request))("date", date));
230
+ }
231
+ auto temp = path + ".tmp";
232
+ {
233
+ std::ofstream file(temp, std::ios::binary | std::ios::trunc);
234
+ file << folly::toJson(out);
235
+ file.close();
236
+ if (!file) {
237
+ std::remove(temp.c_str());
238
+ return false;
239
+ }
240
+ }
241
+ if (std::rename(temp.c_str(), path.c_str()) != 0) {
242
+ std::remove(temp.c_str());
243
+ return false;
244
+ }
245
+ return true;
246
+ }
247
+
248
+ // Under the lock: the timer set for the soonest request, at most kLongestWait away.
249
+ void arm() {
250
+ if (cancelTimer) {
251
+ cancelTimer();
252
+ cancelTimer = nullptr;
253
+ }
254
+ if (pending.empty() || !started) {
255
+ return;
256
+ }
257
+ double soonest = pending.front().next;
258
+ for (const auto &entry : pending) {
259
+ soonest = std::min(soonest, entry.next);
260
+ }
261
+ double wait = std::clamp(soonest - clock.now(), 0.0, kLongestWait);
262
+ std::weak_ptr<State> weak = shared_from_this();
263
+ cancelTimer = clock.after(wait, [weak] {
264
+ if (auto self = weak.lock()) {
265
+ self->fire();
266
+ }
267
+ });
268
+ }
269
+
270
+ // Delivers each request that is due, moves a repeating one to its next time and drops the rest.
271
+ void fire() {
272
+ std::vector<NotificationRequest> due;
273
+ {
274
+ std::lock_guard lock(mutex);
275
+ double now = clock.now();
276
+ std::vector<Entry> kept;
277
+ for (auto &entry : pending) {
278
+ if (entry.next > now) {
279
+ kept.push_back(std::move(entry));
280
+ continue;
281
+ }
282
+ due.push_back(entry.request);
283
+ remember(now, entry.request);
284
+ const auto &trigger = entry.request.trigger;
285
+ if (!trigger.repeats) {
286
+ continue;
287
+ }
288
+ std::optional<double> next;
289
+ if (trigger.kind == NotificationTrigger::Kind::Interval) {
290
+ // Times missed while the app was not running are not each delivered.
291
+ double step = trigger.seconds * 1000;
292
+ next = entry.next + step * (std::floor((now - entry.next) / step) + 1);
293
+ } else if (trigger.kind == NotificationTrigger::Kind::Calendar) {
294
+ next = nextCalendarMatch(trigger.components, now);
295
+ }
296
+ if (next) {
297
+ entry.next = *next;
298
+ kept.push_back(std::move(entry));
299
+ }
300
+ }
301
+ pending = std::move(kept);
302
+ if (!due.empty()) {
303
+ save();
304
+ }
305
+ arm();
306
+ }
307
+ for (const auto &request : due) {
308
+ deliver(request);
309
+ }
310
+ }
311
+
312
+ // Under the lock.
313
+ void remember(double date, const NotificationRequest &request) {
314
+ std::erase_if(delivered, [&](const auto &item) { return item.second.identifier == request.identifier; });
315
+ delivered.emplace_back(date, request);
316
+ if (delivered.size() > kKeptDelivered) {
317
+ delivered.erase(delivered.begin());
318
+ }
319
+ }
320
+ };
321
+
322
+ NotificationSchedule::NotificationSchedule(std::string path, Clock clock, std::function<void(const NotificationRequest &)> deliver)
323
+ : state_(std::make_shared<State>()) {
324
+ state_->path = std::move(path);
325
+ state_->clock = std::move(clock);
326
+ state_->deliver = std::move(deliver);
327
+ state_->load();
328
+ }
329
+
330
+ NotificationSchedule::~NotificationSchedule() {
331
+ std::lock_guard lock(state_->mutex);
332
+ if (state_->cancelTimer) {
333
+ state_->cancelTimer();
334
+ state_->cancelTimer = nullptr;
335
+ }
336
+ }
337
+
338
+ void NotificationSchedule::start() {
339
+ {
340
+ std::lock_guard lock(state_->mutex);
341
+ state_->started = true;
342
+ }
343
+ state_->fire();
344
+ }
345
+
346
+ bool NotificationSchedule::add(const NotificationRequest &request) {
347
+ std::lock_guard lock(state_->mutex);
348
+ auto &pending = state_->pending;
349
+ auto before = pending;
350
+ std::erase_if(pending, [&](const auto &entry) { return entry.request.identifier == request.identifier; });
351
+ if (auto next = firstFireDate(request.trigger, state_->clock.now())) {
352
+ pending.push_back({request, *next});
353
+ }
354
+ if (!state_->save()) {
355
+ pending = std::move(before);
356
+ return false;
357
+ }
358
+ state_->arm();
359
+ return true;
360
+ }
361
+
362
+ std::vector<NotificationRequest> NotificationSchedule::pending() const {
363
+ std::lock_guard lock(state_->mutex);
364
+ std::vector<NotificationRequest> out;
365
+ for (const auto &entry : state_->pending) {
366
+ if (entry.request.trigger.kind != NotificationTrigger::Kind::Now) {
367
+ out.push_back(entry.request);
368
+ }
369
+ }
370
+ return out;
371
+ }
372
+
373
+ void NotificationSchedule::removePending(const std::vector<std::string> &identifiers) {
374
+ std::lock_guard lock(state_->mutex);
375
+ std::erase_if(state_->pending, [&](const auto &entry) {
376
+ return std::find(identifiers.begin(), identifiers.end(), entry.request.identifier) != identifiers.end();
377
+ });
378
+ state_->save();
379
+ state_->arm();
380
+ }
381
+
382
+ void NotificationSchedule::removeAllPending() {
383
+ std::lock_guard lock(state_->mutex);
384
+ state_->pending.clear();
385
+ state_->save();
386
+ state_->arm();
387
+ }
388
+
389
+ std::vector<std::pair<double, NotificationRequest>> NotificationSchedule::delivered() const {
390
+ std::lock_guard lock(state_->mutex);
391
+ return state_->delivered;
392
+ }
393
+
394
+ std::optional<std::pair<double, NotificationRequest>> NotificationSchedule::deliveredOne(const std::string &identifier) const {
395
+ std::lock_guard lock(state_->mutex);
396
+ for (const auto &item : state_->delivered) {
397
+ if (item.second.identifier == identifier) {
398
+ return item;
399
+ }
400
+ }
401
+ return std::nullopt;
402
+ }
403
+
404
+ std::vector<std::string> NotificationSchedule::removeDelivered(const std::vector<std::string> &identifiers) {
405
+ std::lock_guard lock(state_->mutex);
406
+ std::vector<std::string> removed;
407
+ std::erase_if(state_->delivered, [&](const auto &item) {
408
+ bool named = std::find(identifiers.begin(), identifiers.end(), item.second.identifier) != identifiers.end();
409
+ if (named) {
410
+ removed.push_back(item.second.identifier);
411
+ }
412
+ return named;
413
+ });
414
+ state_->save();
415
+ return removed;
416
+ }
417
+
418
+ std::vector<std::string> NotificationSchedule::removeAllDelivered() {
419
+ std::lock_guard lock(state_->mutex);
420
+ std::vector<std::string> removed;
421
+ for (const auto &item : state_->delivered) {
422
+ removed.push_back(item.second.identifier);
423
+ }
424
+ state_->delivered.clear();
425
+ state_->save();
426
+ return removed;
427
+ }
428
+
429
+ } // namespace ngn
@@ -0,0 +1,83 @@
1
+ #include "ngn/openssl-update-crypto.h"
2
+
3
+ #include <openssl/bio.h>
4
+ #include <openssl/err.h>
5
+ #include <openssl/evp.h>
6
+ #include <openssl/pem.h>
7
+ #include <openssl/rsa.h>
8
+ #include <openssl/x509.h>
9
+
10
+ #include <stdexcept>
11
+
12
+ namespace ngn {
13
+
14
+ namespace {
15
+
16
+ template <typename T, void (*Free)(T *)>
17
+ struct Freer {
18
+ void operator()(T *p) const {
19
+ Free(p);
20
+ }
21
+ };
22
+ template <typename T, void (*Free)(T *)>
23
+ using Owned = std::unique_ptr<T, Freer<T, Free>>;
24
+
25
+ void freeBio(BIO *bio) {
26
+ BIO_free(bio);
27
+ }
28
+
29
+ class OpenSslUpdateCrypto final : public UpdateCrypto {
30
+ public:
31
+ std::string sha256(const std::string &bytes) override {
32
+ unsigned char digest[EVP_MAX_MD_SIZE];
33
+ unsigned int length = 0;
34
+ if (EVP_Digest(bytes.data(), bytes.size(), digest, &length, EVP_sha256(), nullptr) != 1) {
35
+ ERR_clear_error();
36
+ throw std::runtime_error("OpenSSL could not hash with SHA-256");
37
+ }
38
+ return std::string(reinterpret_cast<const char *>(digest), length);
39
+ }
40
+
41
+ bool verifyRsaSha256(const std::string &certificatePem, const std::string &data, const std::string &signature) override {
42
+ bool valid = verify(certificatePem, data, signature);
43
+ // A failure leaves its reasons on this thread's queue, which a later call would misread.
44
+ ERR_clear_error();
45
+ return valid;
46
+ }
47
+
48
+ private:
49
+ static bool verify(const std::string &certificatePem, const std::string &data, const std::string &signature) {
50
+ if (signature.empty()) {
51
+ return false;
52
+ }
53
+ Owned<BIO, freeBio> bio(BIO_new_mem_buf(certificatePem.data(), static_cast<int>(certificatePem.size())));
54
+ if (!bio) {
55
+ return false;
56
+ }
57
+ Owned<X509, X509_free> certificate(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
58
+ if (!certificate) {
59
+ return false;
60
+ }
61
+ Owned<EVP_PKEY, EVP_PKEY_free> key(X509_get_pubkey(certificate.get()));
62
+ // RSASSA-PKCS1-v1_5 only, as the protocol's `rsa-v1_5-sha256` and macOS's check have it: a
63
+ // certificate for another kind of key verifies nothing.
64
+ if (!key || EVP_PKEY_get_base_id(key.get()) != EVP_PKEY_RSA) {
65
+ return false;
66
+ }
67
+ Owned<EVP_MD_CTX, EVP_MD_CTX_free> context(EVP_MD_CTX_new());
68
+ EVP_PKEY_CTX *keyContext = nullptr;
69
+ return context && EVP_DigestVerifyInit(context.get(), &keyContext, EVP_sha256(), nullptr, key.get()) == 1 &&
70
+ EVP_PKEY_CTX_set_rsa_padding(keyContext, RSA_PKCS1_PADDING) == 1 &&
71
+ EVP_DigestVerify(
72
+ context.get(), reinterpret_cast<const unsigned char *>(signature.data()), signature.size(),
73
+ reinterpret_cast<const unsigned char *>(data.data()), data.size()) == 1;
74
+ }
75
+ };
76
+
77
+ } // namespace
78
+
79
+ std::shared_ptr<UpdateCrypto> makeOpenSslUpdateCrypto() {
80
+ return std::make_shared<OpenSslUpdateCrypto>();
81
+ }
82
+
83
+ } // namespace ngn