@native-sdk/cli 0.8.3 → 0.9.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 (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -68,6 +68,7 @@
68
68
  package dev.native_sdk.host;
69
69
 
70
70
  import android.app.Activity;
71
+ import android.content.SharedPreferences;
71
72
  import android.content.res.AssetManager;
72
73
  import android.graphics.Bitmap;
73
74
  import android.graphics.BitmapFactory;
@@ -80,8 +81,12 @@ import android.media.MediaPlayer;
80
81
  import android.os.Bundle;
81
82
  import android.os.Handler;
82
83
  import android.os.Looper;
84
+ import android.security.keystore.KeyGenParameterSpec;
85
+ import android.security.keystore.KeyProperties;
86
+ import android.security.keystore.UserNotAuthenticatedException;
83
87
  import android.text.InputType;
84
88
  import android.util.LruCache;
89
+ import android.util.Base64;
85
90
  import android.view.Choreographer;
86
91
  import android.view.KeyEvent;
87
92
  import android.view.MotionEvent;
@@ -98,10 +103,17 @@ import java.io.File;
98
103
  import java.io.FileOutputStream;
99
104
  import java.io.InputStream;
100
105
  import java.io.OutputStream;
106
+ import java.security.GeneralSecurityException;
107
+ import java.security.KeyStore;
108
+ import java.security.MessageDigest;
101
109
  import java.nio.ByteBuffer;
102
110
  import java.nio.charset.CharacterCodingException;
103
111
  import java.nio.charset.CodingErrorAction;
104
112
  import java.nio.charset.StandardCharsets;
113
+ import javax.crypto.Cipher;
114
+ import javax.crypto.KeyGenerator;
115
+ import javax.crypto.SecretKey;
116
+ import javax.crypto.spec.GCMParameterSpec;
105
117
 
106
118
  public final class NativeSdkActivity extends Activity implements SurfaceHolder.Callback, Choreographer.FrameCallback {
107
119
  private static final int TOUCH_MODE_IDLE = 0;
@@ -231,12 +243,23 @@ public final class NativeSdkActivity extends Activity implements SurfaceHolder.C
231
243
  // section below.
232
244
  nativeSetAudioService(nativeApp);
233
245
 
246
+ // Tier-4 secure storage: AndroidKeyStore holds the non-exportable
247
+ // AES key; app-private preferences hold only authenticated
248
+ // ciphertext. Register before start so boot credential effects can
249
+ // run without ever putting a token in Model state.
250
+ nativeSetCredentialService(nativeApp);
251
+
234
252
  // The platform image decoder (registered before start, so a
235
253
  // boot-effect fx.registerImageBytes already decodes): the
236
254
  // BitmapFactory codec behind the embed image seam — see the image
237
255
  // section below.
238
256
  nativeSetImageService(nativeApp);
239
257
 
258
+ // `getFilesDir()` is app_dirs `.data` on Android. Store-capability
259
+ // builds open store.db here before start; other builds accept the
260
+ // same stable host call as a capability-shed no-op.
261
+ nativeSetDataRoot(nativeApp, getFilesDir().getAbsolutePath());
262
+
240
263
  // Verification harness: `am start --ez native-sdk-automation true`
241
264
  // publishes snapshot.txt into the app's files dir, same protocol
242
265
  // as the desktop -Dautomation=true runners (readable over
@@ -431,6 +454,112 @@ public final class NativeSdkActivity extends Activity implements SurfaceHolder.C
431
454
  return Typeface.DEFAULT;
432
455
  }
433
456
 
457
+ // --------------------------------------------------------- credentials
458
+
459
+ private static final String CREDENTIAL_KEY_ALIAS = "native-sdk.credentials.v1";
460
+ private static final String CREDENTIAL_PREFS = "native-sdk.credentials";
461
+
462
+ private synchronized SecretKey credentialEncryptionKey() throws GeneralSecurityException {
463
+ KeyStore store = KeyStore.getInstance("AndroidKeyStore");
464
+ try {
465
+ store.load(null);
466
+ } catch (java.io.IOException e) {
467
+ throw new GeneralSecurityException(e);
468
+ }
469
+ java.security.Key existing = store.getKey(CREDENTIAL_KEY_ALIAS, null);
470
+ if (existing instanceof SecretKey) return (SecretKey)existing;
471
+ KeyGenerator generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
472
+ generator.init(new KeyGenParameterSpec.Builder(
473
+ CREDENTIAL_KEY_ALIAS,
474
+ KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
475
+ .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
476
+ .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
477
+ .setRandomizedEncryptionRequired(true)
478
+ .build());
479
+ return generator.generateKey();
480
+ }
481
+
482
+ private String credentialEntryKey(byte[] service, byte[] account) throws GeneralSecurityException {
483
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
484
+ digest.update(service);
485
+ digest.update((byte)0);
486
+ digest.update(account);
487
+ return Base64.encodeToString(digest.digest(), Base64.NO_WRAP | Base64.URL_SAFE);
488
+ }
489
+
490
+ private SharedPreferences credentialPreferences() {
491
+ return getSharedPreferences(CREDENTIAL_PREFS, MODE_PRIVATE);
492
+ }
493
+
494
+ @SuppressWarnings("unused") // called from android_host.c
495
+ int credentialSet(byte[] service, byte[] account, byte[] secret) {
496
+ try {
497
+ String entryKey = credentialEntryKey(service, account);
498
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
499
+ cipher.init(Cipher.ENCRYPT_MODE, credentialEncryptionKey());
500
+ cipher.updateAAD(entryKey.getBytes(StandardCharsets.UTF_8));
501
+ byte[] iv = cipher.getIV();
502
+ byte[] ciphertext = cipher.doFinal(secret);
503
+ if (iv.length > 255) return -5;
504
+ ByteBuffer packed = ByteBuffer.allocate(1 + iv.length + ciphertext.length);
505
+ packed.put((byte)iv.length).put(iv).put(ciphertext);
506
+ boolean committed = credentialPreferences().edit()
507
+ .putString(entryKey,
508
+ Base64.encodeToString(packed.array(), Base64.NO_WRAP))
509
+ .commit();
510
+ return committed ? 1 : -5;
511
+ } catch (UserNotAuthenticatedException e) {
512
+ return -2;
513
+ } catch (SecurityException e) {
514
+ return -3;
515
+ } catch (GeneralSecurityException | IllegalArgumentException e) {
516
+ return -5;
517
+ }
518
+ }
519
+
520
+ @SuppressWarnings("unused") // called from android_host.c
521
+ long credentialGet(byte[] service, byte[] account, ByteBuffer output) {
522
+ try {
523
+ String entryKey = credentialEntryKey(service, account);
524
+ String encoded = credentialPreferences().getString(entryKey, null);
525
+ if (encoded == null) return -1;
526
+ byte[] packed = Base64.decode(encoded, Base64.NO_WRAP);
527
+ if (packed.length < 2) return -5;
528
+ int ivLength = packed[0] & 0xff;
529
+ if (ivLength == 0 || 1 + ivLength >= packed.length) return -5;
530
+ byte[] iv = java.util.Arrays.copyOfRange(packed, 1, 1 + ivLength);
531
+ byte[] ciphertext = java.util.Arrays.copyOfRange(packed, 1 + ivLength, packed.length);
532
+ Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
533
+ cipher.init(Cipher.DECRYPT_MODE, credentialEncryptionKey(), new GCMParameterSpec(128, iv));
534
+ cipher.updateAAD(entryKey.getBytes(StandardCharsets.UTF_8));
535
+ byte[] secret = cipher.doFinal(ciphertext);
536
+ if (secret.length > output.capacity()) return -4;
537
+ output.position(0);
538
+ output.put(secret);
539
+ return secret.length;
540
+ } catch (UserNotAuthenticatedException e) {
541
+ return -2;
542
+ } catch (SecurityException e) {
543
+ return -3;
544
+ } catch (GeneralSecurityException | IllegalArgumentException e) {
545
+ return -5;
546
+ }
547
+ }
548
+
549
+ @SuppressWarnings("unused") // called from android_host.c
550
+ int credentialDelete(byte[] service, byte[] account) {
551
+ try {
552
+ String key = credentialEntryKey(service, account);
553
+ SharedPreferences preferences = credentialPreferences();
554
+ if (!preferences.contains(key)) return 0;
555
+ return preferences.edit().remove(key).commit() ? 1 : -5;
556
+ } catch (SecurityException e) {
557
+ return -3;
558
+ } catch (GeneralSecurityException e) {
559
+ return -5;
560
+ }
561
+ }
562
+
434
563
  // -------------------------------------------------------------- images
435
564
  //
436
565
  // The platform image decoder behind the embed image service — the
@@ -1361,9 +1490,11 @@ public final class NativeSdkActivity extends Activity implements SurfaceHolder.C
1361
1490
  private native boolean nativeTextInputState(long app, long[] widgetId, float[] frame);
1362
1491
  private native boolean nativeScrollableWidgetAt(long app, float x, float y);
1363
1492
  private native void nativeSetAssetRoot(long app, String path);
1493
+ private native void nativeSetDataRoot(long app, String path);
1364
1494
  private native void nativeSetAutomationDir(long app, String path);
1365
1495
  private native void nativeSetTextMeasure(long app);
1366
1496
  private native void nativeSetAudioService(long app);
1497
+ private native void nativeSetCredentialService(long app);
1367
1498
  private native void nativeSetImageService(long app);
1368
1499
  private native void nativeAudioEvent(long app, int kind, long positionMs, long durationMs, int playing, int buffering);
1369
1500
  private native String nativeLastError(long app);
@@ -52,6 +52,18 @@
52
52
  #define NATIVE_SDK_LOGI(...) __android_log_print(ANDROID_LOG_INFO, NATIVE_SDK_LOG_TAG, __VA_ARGS__)
53
53
  #define NATIVE_SDK_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, NATIVE_SDK_LOG_TAG, __VA_ARGS__)
54
54
 
55
+ // Immutable per-registration state for credential worker callbacks. Unlike
56
+ // presentation/audio/image upcalls, credential calls run off the activity
57
+ // thread and teardown may deliberately detach a blocked worker. The runtime
58
+ // therefore reports whether this context must be preserved process-lived.
59
+ typedef struct native_sdk_credential_context {
60
+ JavaVM *vm;
61
+ jobject activity;
62
+ jmethodID set_method;
63
+ jmethodID get_method;
64
+ jmethodID delete_method;
65
+ } native_sdk_credential_context_t;
66
+
55
67
  // One activity drives one embed app per process (the manifest declares a
56
68
  // single launcher activity), so the host-side presentation, measure, and
57
69
  // audio state lives in a single static bundle.
@@ -80,6 +92,7 @@ static struct {
80
92
  jmethodID audio_stop_method;
81
93
  jmethodID audio_seek_method;
82
94
  jmethodID audio_set_volume_method;
95
+ native_sdk_credential_context_t *credential_context;
83
96
  // Image decode upcall target, registered by nativeSetImageService: the
84
97
  // activity owns the platform codec (BitmapFactory on the Java side),
85
98
  // and the embed image service callback below calls back into it.
@@ -104,7 +117,9 @@ JNIEXPORT jlong JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeCreate
104
117
 
105
118
  JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeDestroy(JNIEnv *env, jobject self, jlong app) {
106
119
  (void)self;
107
- native_sdk_app_destroy((void *)app);
120
+ native_sdk_credential_context_t *credential_context = host_state.credential_context;
121
+ host_state.credential_context = NULL;
122
+ const int preserve_callback_context = native_sdk_app_destroy_with_status((void *)app);
108
123
  if (host_state.window) {
109
124
  ANativeWindow_release(host_state.window);
110
125
  host_state.window = NULL;
@@ -128,6 +143,12 @@ JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeDestroy
128
143
  host_state.audio_seek_method = NULL;
129
144
  host_state.audio_set_volume_method = NULL;
130
145
  }
146
+ if (credential_context && !preserve_callback_context) {
147
+ if (credential_context->activity) (*env)->DeleteGlobalRef(env, credential_context->activity);
148
+ free(credential_context);
149
+ } else if (credential_context) {
150
+ NATIVE_SDK_LOGE("credential callback context preserved after an abandoned platform call");
151
+ }
131
152
  if (host_state.image_activity) {
132
153
  (*env)->DeleteGlobalRef(env, host_state.image_activity);
133
154
  host_state.image_activity = NULL;
@@ -403,6 +424,16 @@ JNIEXPORT jboolean JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeScr
403
424
 
404
425
  // ------------------------------------------------------- assets/automation
405
426
 
427
+ JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeSetDataRoot(JNIEnv *env, jobject self, jlong app, jstring path) {
428
+ (void)self;
429
+ if (!path) return;
430
+ const char *chars = (*env)->GetStringUTFChars(env, path, NULL);
431
+ if (!chars) return;
432
+ native_sdk_app_set_data_root((void *)app, chars, strlen(chars));
433
+ host_log_error((void *)app, "data_root");
434
+ (*env)->ReleaseStringUTFChars(env, path, chars);
435
+ }
436
+
406
437
  JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeSetAssetRoot(JNIEnv *env, jobject self, jlong app, jstring path) {
407
438
  (void)self;
408
439
  if (!path) return;
@@ -605,6 +636,156 @@ JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeSetAudi
605
636
  NATIVE_SDK_LOGI("audio service registered");
606
637
  }
607
638
 
639
+ // ----------------------------------------------------------- credentials
640
+
641
+ static JNIEnv *host_credential_env(native_sdk_credential_context_t *credential, int *attached) {
642
+ *attached = 0;
643
+ if (!credential || !credential->vm || !credential->activity) return NULL;
644
+ JNIEnv *env = NULL;
645
+ jint state = (*credential->vm)->GetEnv(credential->vm, (void **)&env, JNI_VERSION_1_6);
646
+ if (state == JNI_OK) return env;
647
+ if (state != JNI_EDETACHED ||
648
+ (*credential->vm)->AttachCurrentThread(credential->vm, &env, NULL) != JNI_OK) return NULL;
649
+ *attached = 1;
650
+ return env;
651
+ }
652
+
653
+ static void host_credential_done(native_sdk_credential_context_t *credential, int attached) {
654
+ if (attached) (*credential->vm)->DetachCurrentThread(credential->vm);
655
+ }
656
+
657
+ static int host_credential_set(void *context,
658
+ const char *service, uintptr_t service_len,
659
+ const char *account, uintptr_t account_len,
660
+ const uint8_t *secret, uintptr_t secret_len) {
661
+ native_sdk_credential_context_t *credential = (native_sdk_credential_context_t *)context;
662
+ int attached = 0;
663
+ JNIEnv *env = host_credential_env(credential, &attached);
664
+ if (!env) return -5;
665
+ jbyteArray service_bytes = host_audio_bytes(env, service, service_len);
666
+ jbyteArray account_bytes = host_audio_bytes(env, account, account_len);
667
+ jbyteArray secret_bytes = host_audio_bytes(env, (const char *)secret, secret_len);
668
+ if (!service_bytes || !account_bytes || !secret_bytes) {
669
+ if (service_bytes) (*env)->DeleteLocalRef(env, service_bytes);
670
+ if (account_bytes) (*env)->DeleteLocalRef(env, account_bytes);
671
+ if (secret_bytes) (*env)->DeleteLocalRef(env, secret_bytes);
672
+ host_credential_done(credential, attached);
673
+ return -5;
674
+ }
675
+ jint result = (*env)->CallIntMethod(env, credential->activity,
676
+ credential->set_method,
677
+ service_bytes, account_bytes, secret_bytes);
678
+ (*env)->DeleteLocalRef(env, secret_bytes);
679
+ (*env)->DeleteLocalRef(env, account_bytes);
680
+ (*env)->DeleteLocalRef(env, service_bytes);
681
+ if ((*env)->ExceptionCheck(env)) {
682
+ (*env)->ExceptionClear(env);
683
+ result = -5;
684
+ }
685
+ host_credential_done(credential, attached);
686
+ return (int)result;
687
+ }
688
+
689
+ static int64_t host_credential_get(void *context,
690
+ const char *service, uintptr_t service_len,
691
+ const char *account, uintptr_t account_len,
692
+ uint8_t *output, uintptr_t output_len) {
693
+ native_sdk_credential_context_t *credential = (native_sdk_credential_context_t *)context;
694
+ int attached = 0;
695
+ JNIEnv *env = host_credential_env(credential, &attached);
696
+ if (!env) return -5;
697
+ jbyteArray service_bytes = host_audio_bytes(env, service, service_len);
698
+ jbyteArray account_bytes = host_audio_bytes(env, account, account_len);
699
+ jobject output_buffer = (*env)->NewDirectByteBuffer(env, output, (jlong)output_len);
700
+ if (!service_bytes || !account_bytes || !output_buffer) {
701
+ if (service_bytes) (*env)->DeleteLocalRef(env, service_bytes);
702
+ if (account_bytes) (*env)->DeleteLocalRef(env, account_bytes);
703
+ if (output_buffer) (*env)->DeleteLocalRef(env, output_buffer);
704
+ host_credential_done(credential, attached);
705
+ return -5;
706
+ }
707
+ jlong result = (*env)->CallLongMethod(env, credential->activity,
708
+ credential->get_method,
709
+ service_bytes, account_bytes, output_buffer);
710
+ (*env)->DeleteLocalRef(env, output_buffer);
711
+ (*env)->DeleteLocalRef(env, account_bytes);
712
+ (*env)->DeleteLocalRef(env, service_bytes);
713
+ if ((*env)->ExceptionCheck(env)) {
714
+ (*env)->ExceptionClear(env);
715
+ result = -5;
716
+ }
717
+ host_credential_done(credential, attached);
718
+ return (int64_t)result;
719
+ }
720
+
721
+ static int host_credential_delete(void *context,
722
+ const char *service, uintptr_t service_len,
723
+ const char *account, uintptr_t account_len) {
724
+ native_sdk_credential_context_t *credential = (native_sdk_credential_context_t *)context;
725
+ int attached = 0;
726
+ JNIEnv *env = host_credential_env(credential, &attached);
727
+ if (!env) return -5;
728
+ jbyteArray service_bytes = host_audio_bytes(env, service, service_len);
729
+ jbyteArray account_bytes = host_audio_bytes(env, account, account_len);
730
+ if (!service_bytes || !account_bytes) {
731
+ if (service_bytes) (*env)->DeleteLocalRef(env, service_bytes);
732
+ if (account_bytes) (*env)->DeleteLocalRef(env, account_bytes);
733
+ host_credential_done(credential, attached);
734
+ return -5;
735
+ }
736
+ jint result = (*env)->CallIntMethod(env, credential->activity,
737
+ credential->delete_method,
738
+ service_bytes, account_bytes);
739
+ (*env)->DeleteLocalRef(env, account_bytes);
740
+ (*env)->DeleteLocalRef(env, service_bytes);
741
+ if ((*env)->ExceptionCheck(env)) {
742
+ (*env)->ExceptionClear(env);
743
+ result = -5;
744
+ }
745
+ host_credential_done(credential, attached);
746
+ return (int)result;
747
+ }
748
+
749
+ JNIEXPORT void JNICALL Java_dev_native_1sdk_host_NativeSdkActivity_nativeSetCredentialService(JNIEnv *env, jobject self, jlong app) {
750
+ native_sdk_credential_context_t *credential = calloc(1, sizeof(*credential));
751
+ if (!credential) return;
752
+ if ((*env)->GetJavaVM(env, &credential->vm) != JNI_OK) {
753
+ free(credential);
754
+ return;
755
+ }
756
+ credential->activity = (*env)->NewGlobalRef(env, self);
757
+ jclass cls = (*env)->GetObjectClass(env, self);
758
+ credential->set_method = (*env)->GetMethodID(env, cls, "credentialSet", "([B[B[B)I");
759
+ credential->get_method = (*env)->GetMethodID(env, cls, "credentialGet", "([B[BLjava/nio/ByteBuffer;)J");
760
+ credential->delete_method = (*env)->GetMethodID(env, cls, "credentialDelete", "([B[B)I");
761
+ (*env)->DeleteLocalRef(env, cls);
762
+ if (!credential->activity || !credential->set_method ||
763
+ !credential->get_method || !credential->delete_method) {
764
+ NATIVE_SDK_LOGE("credential_service registration failed");
765
+ if (credential->activity) (*env)->DeleteGlobalRef(env, credential->activity);
766
+ free(credential);
767
+ return;
768
+ }
769
+ static const native_sdk_credential_service_t service = {
770
+ .set = host_credential_set,
771
+ .get = host_credential_get,
772
+ .delete = host_credential_delete,
773
+ };
774
+ if (native_sdk_app_set_credential_service((void *)app, &service, credential) == 0) {
775
+ (*env)->DeleteGlobalRef(env, credential->activity);
776
+ free(credential);
777
+ host_log_error((void *)app, "credential_service");
778
+ return;
779
+ }
780
+ if (host_state.credential_context) {
781
+ (*env)->DeleteGlobalRef(env, host_state.credential_context->activity);
782
+ free(host_state.credential_context);
783
+ }
784
+ host_state.credential_context = credential;
785
+ host_log_error((void *)app, "credential_service");
786
+ NATIVE_SDK_LOGI("credential service registered");
787
+ }
788
+
608
789
  // ------------------------------------------------------------------ images
609
790
  //
610
791
  // The embed image-decode service, bridged to the activity's Java-side
@@ -156,6 +156,10 @@ typedef struct native_sdk_gpu_frame_state {
156
156
 
157
157
  void *native_sdk_app_create(void);
158
158
  void native_sdk_app_destroy(void *app);
159
+ // Returns 1 when teardown preserved the app because a detached platform
160
+ // callback may still be executing; the shim must preserve that callback's
161
+ // context too. Returns 0 after ordinary complete destruction.
162
+ int native_sdk_app_destroy_with_status(void *app);
159
163
  void native_sdk_app_start(void *app);
160
164
  void native_sdk_app_activate(void *app);
161
165
  void native_sdk_app_deactivate(void *app);
@@ -208,6 +212,18 @@ typedef struct native_sdk_audio_service {
208
212
  int (*set_volume)(void *context, double volume);
209
213
  } native_sdk_audio_service_t;
210
214
  int native_sdk_app_set_audio_service(void *app, const native_sdk_audio_service_t *service, void *context);
215
+ // Secure credential service. Result codes preserve the core effect's closed
216
+ // outcomes: set returns 1 success, -2 locked, -3 denied, 0 I/O failure;
217
+ // get returns a non-negative byte length, -1 miss, -2 locked, -3 denied,
218
+ // -4 output too small, -5 I/O failure; delete returns 1 deleted, 0 miss,
219
+ // -2 locked, -3 denied, -5 I/O failure.
220
+ typedef struct native_sdk_credential_service {
221
+ int (*set)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len, const uint8_t *secret, uintptr_t secret_len);
222
+ int64_t (*get)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len, uint8_t *output, uintptr_t output_len);
223
+ int (*delete)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len);
224
+ } native_sdk_credential_service_t;
225
+ int native_sdk_app_set_credential_service(void *app, const native_sdk_credential_service_t *service, void *context);
226
+
211
227
  // One audio player report (kind ordinals above; position/duration in ms).
212
228
  // Call between runtime entry points on the loop thread, never from inside
213
229
  // an audio service callback.
@@ -227,6 +243,7 @@ typedef struct native_sdk_image_service {
227
243
  } native_sdk_image_service_t;
228
244
  int native_sdk_app_set_image_service(void *app, const native_sdk_image_service_t *service, void *context);
229
245
  int native_sdk_app_set_automation_dir(void *app, const char *path, uintptr_t len);
246
+ int native_sdk_app_set_data_root(void *app, const char *path, uintptr_t len);
230
247
  void native_sdk_app_set_asset_root(void *app, const char *path, uintptr_t len);
231
248
  uintptr_t native_sdk_app_widget_semantics_count(void *app);
232
249
  int native_sdk_app_widget_semantics_at(void *app, uintptr_t index, native_sdk_widget_semantics_t *out);
@@ -175,6 +175,10 @@ typedef struct native_sdk_gpu_frame_state {
175
175
 
176
176
  void *native_sdk_app_create(void);
177
177
  void native_sdk_app_destroy(void *app);
178
+ // Returns 1 when teardown preserved the app because a detached platform
179
+ // callback may still be executing; the shim must preserve that callback's
180
+ // context too. Returns 0 after ordinary complete destruction.
181
+ int native_sdk_app_destroy_with_status(void *app);
178
182
  void native_sdk_app_start(void *app);
179
183
  void native_sdk_app_activate(void *app);
180
184
  void native_sdk_app_deactivate(void *app);
@@ -226,6 +230,18 @@ typedef struct native_sdk_audio_service {
226
230
  int (*set_volume)(void *context, double volume);
227
231
  } native_sdk_audio_service_t;
228
232
  int native_sdk_app_set_audio_service(void *app, const native_sdk_audio_service_t *service, void *context);
233
+ // Secure credential service. Result codes preserve the core effect's closed
234
+ // outcomes: set returns 1 success, -2 locked, -3 denied, 0 I/O failure;
235
+ // get returns a non-negative byte length, -1 miss, -2 locked, -3 denied,
236
+ // -4 output too small, -5 I/O failure; delete returns 1 deleted, 0 miss,
237
+ // -2 locked, -3 denied, -5 I/O failure.
238
+ typedef struct native_sdk_credential_service {
239
+ int (*set)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len, const uint8_t *secret, uintptr_t secret_len);
240
+ int64_t (*get)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len, uint8_t *output, uintptr_t output_len);
241
+ int (*delete)(void *context, const char *service, uintptr_t service_len, const char *account, uintptr_t account_len);
242
+ } native_sdk_credential_service_t;
243
+ int native_sdk_app_set_credential_service(void *app, const native_sdk_credential_service_t *service, void *context);
244
+
229
245
  // One audio player report (kind ordinals above; position/duration in ms).
230
246
  // Call between runtime entry points on the loop thread, never from inside
231
247
  // an audio service callback.
@@ -245,6 +261,7 @@ typedef struct native_sdk_image_service {
245
261
  } native_sdk_image_service_t;
246
262
  int native_sdk_app_set_image_service(void *app, const native_sdk_image_service_t *service, void *context);
247
263
  int native_sdk_app_set_automation_dir(void *app, const char *path, uintptr_t len);
264
+ int native_sdk_app_set_data_root(void *app, const char *path, uintptr_t len);
248
265
  void native_sdk_app_set_asset_root(void *app, const char *path, uintptr_t len);
249
266
  uintptr_t native_sdk_app_widget_semantics_count(void *app);
250
267
  int native_sdk_app_widget_semantics_at(void *app, uintptr_t index, native_sdk_widget_semantics_t *out);
@@ -99,13 +99,96 @@
99
99
  #import <QuartzCore/CAMetalLayer.h>
100
100
  #import <AVFoundation/AVFoundation.h>
101
101
  #import <ImageIO/ImageIO.h>
102
+ #import <Security/Security.h>
102
103
 
103
104
  #include <dlfcn.h>
104
105
  #include <mach-o/dyld.h>
105
106
  #include <stdlib.h>
107
+ #include <string.h>
106
108
 
107
109
  #include "native_sdk_app.h"
108
110
 
111
+ // ----------------------------------------------------------- credentials
112
+ // Generic-password Keychain entries shared by the WebView bridge and core
113
+ // effects through the runtime's one service/account namespace.
114
+ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
115
+ if (status == errSecSuccess) return 1;
116
+ if (status == errSecItemNotFound) return missingCode;
117
+ if (status == errSecInteractionNotAllowed || status == errSecNotAvailable) return -2;
118
+ if (status == errSecAuthFailed || status == errSecUserCanceled || status == errSecMissingEntitlement) return -3;
119
+ return -5;
120
+ }
121
+
122
+ static NSMutableDictionary *NativeSdkCredentialQuery(const char *service, uintptr_t service_len,
123
+ const char *account, uintptr_t account_len) {
124
+ NSString *serviceString = [[NSString alloc] initWithBytes:service length:service_len encoding:NSUTF8StringEncoding];
125
+ NSString *accountString = [[NSString alloc] initWithBytes:account length:account_len encoding:NSUTF8StringEncoding];
126
+ if (!serviceString || !accountString) return nil;
127
+ return [@{
128
+ (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
129
+ (__bridge id)kSecAttrService: serviceString,
130
+ (__bridge id)kSecAttrAccount: accountString,
131
+ } mutableCopy];
132
+ }
133
+
134
+ static int NativeSdkCredentialSet(void *context,
135
+ const char *service, uintptr_t service_len,
136
+ const char *account, uintptr_t account_len,
137
+ const uint8_t *secret, uintptr_t secret_len) {
138
+ (void)context;
139
+ @autoreleasepool {
140
+ NSMutableDictionary *query = NativeSdkCredentialQuery(service, service_len, account, account_len);
141
+ if (!query) return -5;
142
+ NSData *data = [NSData dataWithBytes:secret length:secret_len];
143
+ NSDictionary *update = @{ (__bridge id)kSecValueData: data };
144
+ OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)query,
145
+ (__bridge CFDictionaryRef)update);
146
+ if (status == errSecItemNotFound) {
147
+ query[(__bridge id)kSecValueData] = data;
148
+ query[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
149
+ status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
150
+ }
151
+ return status == errSecSuccess ? 1 : NativeSdkCredentialStatus(status, -5);
152
+ }
153
+ }
154
+
155
+ static int64_t NativeSdkCredentialGet(void *context,
156
+ const char *service, uintptr_t service_len,
157
+ const char *account, uintptr_t account_len,
158
+ uint8_t *output, uintptr_t output_len) {
159
+ (void)context;
160
+ @autoreleasepool {
161
+ NSMutableDictionary *query = NativeSdkCredentialQuery(service, service_len, account, account_len);
162
+ if (!query) return -5;
163
+ query[(__bridge id)kSecReturnData] = @YES;
164
+ query[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
165
+ CFTypeRef raw = NULL;
166
+ OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &raw);
167
+ if (status != errSecSuccess) return NativeSdkCredentialStatus(status, -1);
168
+ NSData *data = (__bridge NSData *)raw;
169
+ if (data.length > output_len) {
170
+ CFRelease(raw);
171
+ return -4;
172
+ }
173
+ if (data.length > 0) memcpy(output, data.bytes, data.length);
174
+ int64_t result = (int64_t)data.length;
175
+ CFRelease(raw);
176
+ return result;
177
+ }
178
+ }
179
+
180
+ static int NativeSdkCredentialDelete(void *context,
181
+ const char *service, uintptr_t service_len,
182
+ const char *account, uintptr_t account_len) {
183
+ (void)context;
184
+ @autoreleasepool {
185
+ NSMutableDictionary *query = NativeSdkCredentialQuery(service, service_len, account, account_len);
186
+ if (!query) return -5;
187
+ OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
188
+ return status == errSecSuccess ? 1 : NativeSdkCredentialStatus(status, 0);
189
+ }
190
+ }
191
+
109
192
  // Zig's std.debug stack-trace symbolication (pulled in by the embed lib's
110
193
  // panic path) references `_dyld_get_image_header_containing_address`, which
111
194
  // the iOS SDK marks __API_UNAVAILABLE(ios). Provide the documented
@@ -1665,6 +1748,16 @@ static const CGFloat NativeSdkTouchSlop = 8.0;
1665
1748
  native_sdk_app_set_audio_service(self.nativeApp, &audioService, (__bridge void *)self.audioEngine);
1666
1749
  [self logNativeErrorIfAny:@"audio_service"];
1667
1750
 
1751
+ // Secure credentials use generic-password Keychain entries. Register
1752
+ // before start so boot effects see the same namespace as the bridge.
1753
+ native_sdk_credential_service_t credentialService = {
1754
+ .set = NativeSdkCredentialSet,
1755
+ .get = NativeSdkCredentialGet,
1756
+ .delete = NativeSdkCredentialDelete,
1757
+ };
1758
+ native_sdk_app_set_credential_service(self.nativeApp, &credentialService, NULL);
1759
+ [self logNativeErrorIfAny:@"credential_service"];
1760
+
1668
1761
  // The platform image decoder (registered before start, like the audio
1669
1762
  // service, so a boot-effect fx.registerImageBytes already decodes):
1670
1763
  // CGImageSource behind the embed image seam, the same ImageIO family
@@ -1675,6 +1768,28 @@ static const CGFloat NativeSdkTouchSlop = 8.0;
1675
1768
  native_sdk_app_set_image_service(self.nativeApp, &imageService, NULL);
1676
1769
  [self logNativeErrorIfAny:@"image_service"];
1677
1770
 
1771
+ // Engine-owned durable storage lives in the platform app-data
1772
+ // directory. The ABI accepts this for every app; capability-shed builds
1773
+ // treat it as a no-op, while `store` builds open store.db before start.
1774
+ NSString *dataRoot = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
1775
+ NSUserDomainMask,
1776
+ YES).firstObject;
1777
+ if (dataRoot) {
1778
+ NSError *dataRootError = nil;
1779
+ [NSFileManager.defaultManager createDirectoryAtPath:dataRoot
1780
+ withIntermediateDirectories:YES
1781
+ attributes:nil
1782
+ error:&dataRootError];
1783
+ if (dataRootError) {
1784
+ NSLog(@"native-sdk: app data directory unavailable: %@", dataRootError);
1785
+ } else {
1786
+ native_sdk_app_set_data_root(self.nativeApp,
1787
+ dataRoot.UTF8String,
1788
+ [dataRoot lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
1789
+ [self logNativeErrorIfAny:@"data_root"];
1790
+ }
1791
+ }
1792
+
1678
1793
  // Verification harness: with NATIVE_SDK_AUTOMATION set (simctl launch
1679
1794
  // exports SIMCTL_CHILD_* into the app) the embedded runtime publishes
1680
1795
  // snapshot.txt into the app's data container, same protocol as the
@@ -531,7 +531,7 @@ size_t native_sdk_gtk_get_credential(Host *host, const char *service, size_t ser
531
531
  (void)account_len;
532
532
  (void)buffer;
533
533
  (void)buffer_len;
534
- return 0;
534
+ return SIZE_MAX - 1;
535
535
  }
536
536
 
537
537
  int native_sdk_gtk_delete_credential(Host *host, const char *service, size_t service_len, const char *account, size_t account_len) {