@ng-native/desktop 0.0.0 → 0.23.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.
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +73 -1
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
#include "ngn/desktop-expo.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/bytes.h"
|
|
4
|
+
|
|
5
|
+
#include <algorithm>
|
|
6
|
+
#include <cctype>
|
|
7
|
+
#include <chrono>
|
|
8
|
+
#include <filesystem>
|
|
9
|
+
#include <fstream>
|
|
10
|
+
#include <map>
|
|
11
|
+
#include <stdexcept>
|
|
12
|
+
|
|
13
|
+
namespace ngn {
|
|
14
|
+
|
|
15
|
+
namespace {
|
|
16
|
+
|
|
17
|
+
std::string stringArg(const folly::dynamic &args, size_t index) {
|
|
18
|
+
return args.size() > index && args[index].isString() ? args[index].asString() : std::string{};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// expo-secure-store's options object: `keychainService` picks the service.
|
|
22
|
+
std::string serviceFrom(const folly::dynamic &args, size_t index, const std::string &fallback) {
|
|
23
|
+
if (args.size() > index && args[index].isObject()) {
|
|
24
|
+
auto it = args[index].find("keychainService");
|
|
25
|
+
if (it != args[index].items().end() && it->second.isString() && !it->second.asString().empty()) {
|
|
26
|
+
return it->second.asString();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return fallback;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
} // namespace
|
|
33
|
+
|
|
34
|
+
bool secureStoreKeyValid(const std::string &key) {
|
|
35
|
+
return !key.empty() && std::all_of(key.begin(), key.end(), [](unsigned char c) {
|
|
36
|
+
return std::isalnum(c) || c == '.' || c == '-' || c == '_';
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
std::optional<std::string> PrefixedSecretStore::get(const std::string &service, const std::string &key) {
|
|
41
|
+
return inner_->get(prefix_ + service, key);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void PrefixedSecretStore::set(const std::string &service, const std::string &key, const std::string &value) {
|
|
45
|
+
inner_->set(prefix_ + service, key, value);
|
|
46
|
+
std::lock_guard lock(mutex_);
|
|
47
|
+
written_.insert({prefix_ + service, key});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
void PrefixedSecretStore::remove(const std::string &service, const std::string &key) {
|
|
51
|
+
inner_->remove(prefix_ + service, key);
|
|
52
|
+
std::lock_guard lock(mutex_);
|
|
53
|
+
written_.erase({prefix_ + service, key});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
void PrefixedSecretStore::clear() {
|
|
57
|
+
std::set<std::pair<std::string, std::string>> written;
|
|
58
|
+
{
|
|
59
|
+
std::lock_guard lock(mutex_);
|
|
60
|
+
written.swap(written_);
|
|
61
|
+
}
|
|
62
|
+
for (const auto &[service, key] : written) {
|
|
63
|
+
try {
|
|
64
|
+
inner_->remove(service, key);
|
|
65
|
+
} catch (const std::exception &) {
|
|
66
|
+
// Left for the next test launch, which removes what a run before it left.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
std::shared_ptr<FileDialogs> makeDismissedFileDialogs() {
|
|
72
|
+
struct Dismissed final : FileDialogs {
|
|
73
|
+
void openFiles(const OpenRequest &, std::function<void(std::optional<std::vector<std::string>>)> done) override {
|
|
74
|
+
done(std::nullopt);
|
|
75
|
+
}
|
|
76
|
+
void saveFile(const SaveRequest &, std::function<void(std::optional<std::string>)> done) override { done(std::nullopt); }
|
|
77
|
+
void chooseFolder(std::function<void(std::optional<std::string>)> done) override { done(std::nullopt); }
|
|
78
|
+
};
|
|
79
|
+
return std::make_shared<Dismissed>();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
std::optional<std::string> MovingSecretStore::get(const std::string &service, const std::string &key) {
|
|
83
|
+
if (auto value = now_->get(service, key)) {
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
std::optional<std::string> value;
|
|
87
|
+
try {
|
|
88
|
+
value = old_->get(service, key);
|
|
89
|
+
} catch (const std::exception &) {
|
|
90
|
+
// An old store that cannot be read has nothing to move.
|
|
91
|
+
return std::nullopt;
|
|
92
|
+
}
|
|
93
|
+
if (value) {
|
|
94
|
+
now_->set(service, key, *value);
|
|
95
|
+
removeOld(service, key);
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
void MovingSecretStore::set(const std::string &service, const std::string &key, const std::string &value) {
|
|
101
|
+
now_->set(service, key, value);
|
|
102
|
+
removeOld(service, key);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void MovingSecretStore::remove(const std::string &service, const std::string &key) {
|
|
106
|
+
now_->remove(service, key);
|
|
107
|
+
try {
|
|
108
|
+
old_->remove(service, key);
|
|
109
|
+
} catch (const std::exception &) {
|
|
110
|
+
// A get would move an old copy back, so the removal fails unless the old store answers that
|
|
111
|
+
// it has none. One it cannot read now may be read once it recovers (a keychain unlocked).
|
|
112
|
+
bool absent = false;
|
|
113
|
+
try {
|
|
114
|
+
absent = !old_->get(service, key).has_value();
|
|
115
|
+
} catch (const std::exception &) {
|
|
116
|
+
}
|
|
117
|
+
if (!absent) {
|
|
118
|
+
throw;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
void MovingSecretStore::removeOld(const std::string &service, const std::string &key) {
|
|
124
|
+
try {
|
|
125
|
+
old_->remove(service, key);
|
|
126
|
+
} catch (const std::exception &) {
|
|
127
|
+
// The new store has the answer; an old copy left behind is never read while it does.
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
bool usesDataProtectionKeychain(const std::string &teamId, const std::set<std::string> &entitlements) {
|
|
132
|
+
return !teamId.empty() &&
|
|
133
|
+
(entitlements.contains("com.apple.application-identifier") || entitlements.contains("keychain-access-groups"));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
std::shared_ptr<NativeModule> makeSecureStoreModule(std::shared_ptr<SecretStore> store, std::string defaultService) {
|
|
137
|
+
struct SecureStore : NativeModule {
|
|
138
|
+
SecureStore(std::shared_ptr<SecretStore> store, std::string service) : NativeModule("NgNativeSecureStore") {
|
|
139
|
+
auto invalid = [](const std::string &key) {
|
|
140
|
+
return "Invalid key provided to SecureStore. Keys must not be empty and contain only alphanumeric "
|
|
141
|
+
"characters, \".\", \"-\", and \"_\": " + key;
|
|
142
|
+
};
|
|
143
|
+
promise("getItemAsync", [store, service, invalid](const folly::dynamic &args, JsPromise promise) {
|
|
144
|
+
auto key = stringArg(args, 0);
|
|
145
|
+
if (!secureStoreKeyValid(key)) {
|
|
146
|
+
promise.reject(invalid(key));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
auto value = store->get(serviceFrom(args, 1, service), key);
|
|
151
|
+
promise.resolve(value ? folly::dynamic(*value) : folly::dynamic(nullptr));
|
|
152
|
+
} catch (const std::exception &error) {
|
|
153
|
+
promise.reject(error.what());
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
promise("setItemAsync", [store, service, invalid](const folly::dynamic &args, JsPromise promise) {
|
|
157
|
+
auto key = stringArg(args, 0);
|
|
158
|
+
if (!secureStoreKeyValid(key)) {
|
|
159
|
+
promise.reject(invalid(key));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (args.size() < 2 || !args[1].isString()) {
|
|
163
|
+
promise.reject("Invalid value provided to SecureStore. Values must be strings.");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
store->set(serviceFrom(args, 2, service), key, args[1].asString());
|
|
168
|
+
promise.resolve(nullptr);
|
|
169
|
+
} catch (const std::exception &error) {
|
|
170
|
+
promise.reject(error.what());
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
promise("deleteItemAsync", [store, service, invalid](const folly::dynamic &args, JsPromise promise) {
|
|
174
|
+
auto key = stringArg(args, 0);
|
|
175
|
+
if (!secureStoreKeyValid(key)) {
|
|
176
|
+
promise.reject(invalid(key));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
store->remove(serviceFrom(args, 1, service), key);
|
|
181
|
+
promise.resolve(nullptr);
|
|
182
|
+
} catch (const std::exception &error) {
|
|
183
|
+
promise.reject(error.what());
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
sync("getItem", [store, service](const folly::dynamic &args) -> folly::dynamic {
|
|
187
|
+
auto key = stringArg(args, 0);
|
|
188
|
+
if (!secureStoreKeyValid(key)) {
|
|
189
|
+
return nullptr;
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
auto value = store->get(serviceFrom(args, 1, service), key);
|
|
193
|
+
return value ? folly::dynamic(*value) : folly::dynamic(nullptr);
|
|
194
|
+
} catch (const std::exception &) {
|
|
195
|
+
return nullptr;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
sync("setItem", [store, service](const folly::dynamic &args) -> folly::dynamic {
|
|
199
|
+
auto key = stringArg(args, 0);
|
|
200
|
+
if (secureStoreKeyValid(key) && args.size() > 1 && args[1].isString()) {
|
|
201
|
+
try {
|
|
202
|
+
store->set(serviceFrom(args, 2, service), key, args[1].asString());
|
|
203
|
+
} catch (const std::exception &) {
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return nullptr;
|
|
207
|
+
});
|
|
208
|
+
// Touch ID guarding an item is not offered on the desktop yet.
|
|
209
|
+
sync("canUseBiometricAuthentication", [](const folly::dynamic &) { return folly::dynamic(false); });
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
return std::make_shared<SecureStore>(std::move(store), std::move(defaultService));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
std::shared_ptr<NativeModule> makeDesktopClipboardModule(std::shared_ptr<System> system, std::shared_ptr<HostState> state) {
|
|
216
|
+
struct Clipboard : NativeModule {
|
|
217
|
+
Clipboard(std::shared_ptr<System> system, std::shared_ptr<HostState> state) : NativeModule("NgNativeClipboard") {
|
|
218
|
+
promise("getStringAsync", [system](const folly::dynamic &, JsPromise promise) {
|
|
219
|
+
promise.resolve(system->clipboardText().value_or(""));
|
|
220
|
+
});
|
|
221
|
+
promise("setStringAsync", [system, state](const folly::dynamic &args, JsPromise promise) {
|
|
222
|
+
system->setClipboardText(stringArg(args, 0));
|
|
223
|
+
state->clipboardChangeCount.set(system->clipboardChangeCount());
|
|
224
|
+
promise.resolve(true);
|
|
225
|
+
});
|
|
226
|
+
promise("hasStringAsync", [system](const folly::dynamic &, JsPromise promise) {
|
|
227
|
+
auto text = system->clipboardText();
|
|
228
|
+
promise.resolve(text.has_value() && !text->empty());
|
|
229
|
+
});
|
|
230
|
+
// DeviceEventEmitter's `ngNativeClipboard`, with no payload: the contents are read only when
|
|
231
|
+
// asked for.
|
|
232
|
+
subscription = state->clipboardChangeCount.listen(
|
|
233
|
+
[this](const int64_t &) { emit("ngNativeClipboard", folly::dynamic::object()); });
|
|
234
|
+
}
|
|
235
|
+
std::shared_ptr<void> subscription;
|
|
236
|
+
};
|
|
237
|
+
return std::make_shared<Clipboard>(std::move(system), std::move(state));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
void KeepAwakeTags::activate(const std::string &tag) {
|
|
241
|
+
bool was = !tags_.empty();
|
|
242
|
+
tags_.insert(tag);
|
|
243
|
+
if (!was) {
|
|
244
|
+
preventer_->hold(true);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
void KeepAwakeTags::deactivate(const std::string &tag) {
|
|
249
|
+
if (tags_.erase(tag) > 0 && tags_.empty()) {
|
|
250
|
+
preventer_->hold(false);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
std::shared_ptr<NativeModule> makeKeepAwakeModule(std::shared_ptr<KeepAwakeTags> tags) {
|
|
255
|
+
struct KeepAwake : NativeModule {
|
|
256
|
+
explicit KeepAwake(std::shared_ptr<KeepAwakeTags> tags) : NativeModule("NgNativeKeepAwake") {
|
|
257
|
+
promise("activateKeepAwakeAsync", [tags](const folly::dynamic &args, JsPromise promise) {
|
|
258
|
+
tags->activate(stringArg(args, 0));
|
|
259
|
+
promise.resolve(nullptr);
|
|
260
|
+
});
|
|
261
|
+
promise("deactivateKeepAwake", [tags](const folly::dynamic &args, JsPromise promise) {
|
|
262
|
+
tags->deactivate(stringArg(args, 0));
|
|
263
|
+
promise.resolve(nullptr);
|
|
264
|
+
});
|
|
265
|
+
sync("isAvailableAsync", [](const folly::dynamic &) { return folly::dynamic(true); });
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
return std::make_shared<KeepAwake>(std::move(tags));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
std::optional<HapticPlayer::Pattern> hapticPattern(const std::string &method) {
|
|
272
|
+
if (method == "impactAsync") {
|
|
273
|
+
return HapticPlayer::Pattern::Generic;
|
|
274
|
+
}
|
|
275
|
+
if (method == "selectionAsync") {
|
|
276
|
+
return HapticPlayer::Pattern::Alignment;
|
|
277
|
+
}
|
|
278
|
+
if (method == "notificationAsync") {
|
|
279
|
+
return HapticPlayer::Pattern::LevelChange;
|
|
280
|
+
}
|
|
281
|
+
return std::nullopt;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
std::shared_ptr<NativeModule> makeHapticsModule(std::shared_ptr<HapticPlayer> player) {
|
|
285
|
+
struct Haptics : NativeModule {
|
|
286
|
+
explicit Haptics(std::shared_ptr<HapticPlayer> player) : NativeModule("NgNativeHaptics") {
|
|
287
|
+
for (const char *method : {"impactAsync", "selectionAsync", "notificationAsync"}) {
|
|
288
|
+
auto pattern = *hapticPattern(method);
|
|
289
|
+
promise(method, [player, pattern](const folly::dynamic &, JsPromise promise) {
|
|
290
|
+
player->play(pattern);
|
|
291
|
+
promise.resolve(nullptr);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
return std::make_shared<Haptics>(std::move(player));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
namespace {
|
|
300
|
+
|
|
301
|
+
struct TagParts {
|
|
302
|
+
std::string language;
|
|
303
|
+
std::string script;
|
|
304
|
+
std::string region;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// The language, script and region subtags of a BCP 47 tag ("zh-Hant-TW"), as far as it has them.
|
|
308
|
+
TagParts tagParts(const std::string &tag) {
|
|
309
|
+
TagParts parts;
|
|
310
|
+
size_t at = 0;
|
|
311
|
+
int index = 0;
|
|
312
|
+
while (at <= tag.size()) {
|
|
313
|
+
size_t end = tag.find_first_of("-_", at);
|
|
314
|
+
auto subtag = tag.substr(at, end == std::string::npos ? std::string::npos : end - at);
|
|
315
|
+
bool letters = std::all_of(subtag.begin(), subtag.end(), [](unsigned char c) { return std::isalpha(c); });
|
|
316
|
+
bool digits = std::all_of(subtag.begin(), subtag.end(), [](unsigned char c) { return std::isdigit(c); });
|
|
317
|
+
if (index == 0) {
|
|
318
|
+
parts.language = subtag;
|
|
319
|
+
} else if (parts.script.empty() && parts.region.empty() && subtag.size() == 4 && letters) {
|
|
320
|
+
parts.script = subtag;
|
|
321
|
+
} else if (parts.region.empty() && ((subtag.size() == 2 && letters) || (subtag.size() == 3 && digits))) {
|
|
322
|
+
parts.region = subtag;
|
|
323
|
+
}
|
|
324
|
+
if (end == std::string::npos) {
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
at = end + 1;
|
|
328
|
+
index++;
|
|
329
|
+
}
|
|
330
|
+
return parts;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
folly::dynamic orNull(const std::string &value) {
|
|
334
|
+
return value.empty() ? folly::dynamic(nullptr) : folly::dynamic(value);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const char *measurement(const std::string &system) {
|
|
338
|
+
if (system == "U.S.") {
|
|
339
|
+
return "us";
|
|
340
|
+
}
|
|
341
|
+
if (system == "U.K.") {
|
|
342
|
+
return "uk";
|
|
343
|
+
}
|
|
344
|
+
return "metric";
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
} // namespace
|
|
348
|
+
|
|
349
|
+
std::shared_ptr<NativeModule> makeLocalizationModule(std::shared_ptr<LocaleSource> source, std::string regionCode) {
|
|
350
|
+
struct Localization : NativeModule {
|
|
351
|
+
Localization(std::shared_ptr<LocaleSource> source, std::string region) : NativeModule("NgNativeLocalization") {
|
|
352
|
+
sync("getLocales", [source, region](const folly::dynamic &) {
|
|
353
|
+
auto locales = source->locales();
|
|
354
|
+
if (locales.empty()) {
|
|
355
|
+
locales.push_back({.languageTag = "en-" + region});
|
|
356
|
+
}
|
|
357
|
+
folly::dynamic out = folly::dynamic::array();
|
|
358
|
+
for (const auto &locale : locales) {
|
|
359
|
+
auto parts = tagParts(locale.languageTag);
|
|
360
|
+
out.push_back(folly::dynamic::object("languageTag", locale.languageTag)("languageCode", orNull(parts.language))(
|
|
361
|
+
"languageScriptCode", orNull(parts.script))("regionCode", orNull(parts.region.empty() ? region : parts.region))(
|
|
362
|
+
"languageRegionCode", orNull(parts.region))("textDirection", locale.rightToLeft ? "rtl" : "ltr")(
|
|
363
|
+
"decimalSeparator", orNull(locale.decimalSeparator))("digitGroupingSeparator", orNull(locale.groupingSeparator))(
|
|
364
|
+
"measurementSystem", measurement(locale.measurementSystem))("currencyCode", orNull(locale.currencyCode))(
|
|
365
|
+
"currencySymbol", orNull(locale.currencySymbol))("languageCurrencyCode", orNull(locale.currencyCode))(
|
|
366
|
+
"languageCurrencySymbol", orNull(locale.currencySymbol))("temperatureUnit", orNull(locale.temperatureUnit)));
|
|
367
|
+
}
|
|
368
|
+
return out;
|
|
369
|
+
});
|
|
370
|
+
sync("getCalendars", [source](const folly::dynamic &) {
|
|
371
|
+
auto c = source->calendars();
|
|
372
|
+
return folly::dynamic::array(folly::dynamic::object("calendar", orNull(c.calendar))("timeZone", orNull(c.timeZone))(
|
|
373
|
+
"uses24hourClock", c.uses24hourClock)("firstWeekday", c.firstWeekday));
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
return std::make_shared<Localization>(std::move(source), std::move(regionCode));
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
std::string mimeTypeOf(const std::string &name) {
|
|
381
|
+
static const std::map<std::string, std::string> types{
|
|
382
|
+
{"txt", "text/plain"}, {"csv", "text/csv"}, {"html", "text/html"}, {"htm", "text/html"},
|
|
383
|
+
{"md", "text/markdown"}, {"json", "application/json"}, {"xml", "application/xml"},
|
|
384
|
+
{"pdf", "application/pdf"}, {"zip", "application/zip"}, {"png", "image/png"}, {"jpg", "image/jpeg"},
|
|
385
|
+
{"jpeg", "image/jpeg"}, {"gif", "image/gif"}, {"heic", "image/heic"}, {"webp", "image/webp"},
|
|
386
|
+
{"svg", "image/svg+xml"}, {"tiff", "image/tiff"}, {"mp3", "audio/mpeg"}, {"m4a", "audio/mp4"},
|
|
387
|
+
{"wav", "audio/wav"}, {"mp4", "video/mp4"}, {"mov", "video/quicktime"},
|
|
388
|
+
};
|
|
389
|
+
auto dot = name.rfind('.');
|
|
390
|
+
if (dot == std::string::npos) {
|
|
391
|
+
return "application/octet-stream";
|
|
392
|
+
}
|
|
393
|
+
std::string ext = name.substr(dot + 1);
|
|
394
|
+
std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); });
|
|
395
|
+
auto it = types.find(ext);
|
|
396
|
+
return it == types.end() ? "application/octet-stream" : it->second;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
namespace {
|
|
400
|
+
|
|
401
|
+
std::vector<std::string> stringsIn(const folly::dynamic &value) {
|
|
402
|
+
std::vector<std::string> out;
|
|
403
|
+
if (value.isString()) {
|
|
404
|
+
out.push_back(value.asString());
|
|
405
|
+
} else if (value.isArray()) {
|
|
406
|
+
for (const auto &item : value) {
|
|
407
|
+
if (item.isString()) {
|
|
408
|
+
out.push_back(item.asString());
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return out;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const folly::dynamic &field(const folly::dynamic &args, const char *key) {
|
|
416
|
+
static const folly::dynamic none = nullptr;
|
|
417
|
+
if (args.size() > 0 && args[0].isObject()) {
|
|
418
|
+
if (const auto *value = args[0].get_ptr(key)) {
|
|
419
|
+
return *value;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return none;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
} // namespace
|
|
426
|
+
|
|
427
|
+
std::shared_ptr<NativeModule> makeDocumentPickerModule(
|
|
428
|
+
std::shared_ptr<FileDialogs> dialogs,
|
|
429
|
+
std::string cacheDirectory,
|
|
430
|
+
std::shared_ptr<FileAccess> access) {
|
|
431
|
+
namespace fs = std::filesystem;
|
|
432
|
+
struct DocumentPicker : NativeModule {
|
|
433
|
+
DocumentPicker(std::shared_ptr<FileDialogs> dialogs, std::string cache, std::shared_ptr<FileAccess> access)
|
|
434
|
+
: NativeModule("NgNativeDocumentPicker") {
|
|
435
|
+
// What the user chose in a panel, the app may read and write.
|
|
436
|
+
auto allow = [access](const std::string &path) {
|
|
437
|
+
if (access) {
|
|
438
|
+
access->allowPicked(path);
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
promise("getDocumentAsync", [dialogs, cache, allow](const folly::dynamic &args, JsPromise p) {
|
|
442
|
+
FileDialogs::OpenRequest request{
|
|
443
|
+
.types = stringsIn(field(args, "type")), .multiple = field(args, "multiple").isBool() && field(args, "multiple").asBool()};
|
|
444
|
+
// "*/*" is any file, as no types are.
|
|
445
|
+
std::erase(request.types, "*/*");
|
|
446
|
+
bool copy = !field(args, "copyToCacheDirectory").isBool() || field(args, "copyToCacheDirectory").asBool();
|
|
447
|
+
dialogs->openFiles(request, [p, copy, cache, allow](std::optional<std::vector<std::string>> files) {
|
|
448
|
+
if (!files) {
|
|
449
|
+
p.resolve(folly::dynamic::object("canceled", true)("assets", nullptr));
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
folly::dynamic assets = folly::dynamic::array();
|
|
453
|
+
try {
|
|
454
|
+
for (const auto &file : *files) {
|
|
455
|
+
fs::path source(file);
|
|
456
|
+
fs::path at = source;
|
|
457
|
+
if (copy) {
|
|
458
|
+
// A folder of its own per pick keeps two files of one name apart.
|
|
459
|
+
auto folder = fs::path(cache) / "DocumentPicker" /
|
|
460
|
+
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count());
|
|
461
|
+
fs::create_directories(folder);
|
|
462
|
+
at = folder / source.filename();
|
|
463
|
+
fs::copy_file(source, at, fs::copy_options::overwrite_existing);
|
|
464
|
+
} else {
|
|
465
|
+
allow(source.string());
|
|
466
|
+
}
|
|
467
|
+
// The file clock's epoch to the system clock's, without C++20's clock_cast.
|
|
468
|
+
auto written = fs::last_write_time(source);
|
|
469
|
+
auto modified = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
470
|
+
(written - fs::file_time_type::clock::now() + std::chrono::system_clock::now()).time_since_epoch());
|
|
471
|
+
assets.push_back(folly::dynamic::object("uri", fileUri(at.string()))("name", source.filename().string())(
|
|
472
|
+
"size", static_cast<int64_t>(fs::file_size(source)))("mimeType", mimeTypeOf(source.filename().string()))(
|
|
473
|
+
"lastModified", static_cast<int64_t>(modified.count())));
|
|
474
|
+
}
|
|
475
|
+
} catch (const std::exception &e) {
|
|
476
|
+
p.reject(std::string("The picked file could not be read: ") + e.what());
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
p.resolve(folly::dynamic::object("canceled", false)("assets", assets));
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
promise("saveDocumentAsync", [dialogs, allow](const folly::dynamic &args, JsPromise p) {
|
|
483
|
+
const auto &contents = field(args, "contents");
|
|
484
|
+
std::optional<std::string> bytes;
|
|
485
|
+
if (contents.isString()) {
|
|
486
|
+
bytes = contents.asString();
|
|
487
|
+
} else if (contents.isObject() && contents.get_ptr("base64") && contents["base64"].isString()) {
|
|
488
|
+
bytes = base64Decode(contents["base64"].asString());
|
|
489
|
+
}
|
|
490
|
+
if (!bytes) {
|
|
491
|
+
p.reject("saveDocumentAsync takes contents as text or as {base64}.");
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
FileDialogs::SaveRequest request{
|
|
495
|
+
.suggestedName = field(args, "suggestedName").isString() ? field(args, "suggestedName").asString() : "",
|
|
496
|
+
.types = stringsIn(field(args, "types"))};
|
|
497
|
+
dialogs->saveFile(request, [p, allow, bytes = std::move(*bytes)](std::optional<std::string> path) {
|
|
498
|
+
if (!path) {
|
|
499
|
+
p.resolve(nullptr);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
std::ofstream out(*path, std::ios::binary | std::ios::trunc);
|
|
503
|
+
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
|
|
504
|
+
// Closed first, so a write that fails as it is flushed is caught too.
|
|
505
|
+
out.close();
|
|
506
|
+
if (!out) {
|
|
507
|
+
p.reject("The file could not be written: " + *path);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
allow(*path);
|
|
511
|
+
p.resolve(fileUri(*path));
|
|
512
|
+
});
|
|
513
|
+
});
|
|
514
|
+
promise("pickDirectoryAsync", [dialogs, allow](const folly::dynamic &, JsPromise p) {
|
|
515
|
+
dialogs->chooseFolder([p, allow](std::optional<std::string> folder) {
|
|
516
|
+
if (folder) {
|
|
517
|
+
allow(*folder);
|
|
518
|
+
}
|
|
519
|
+
p.resolve(folder ? folly::dynamic::object("uri", fileUri(*folder)) : folly::dynamic(nullptr));
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
return std::make_shared<DocumentPicker>(std::move(dialogs), std::move(cacheDirectory), std::move(access));
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
} // namespace ngn
|