|
| 1 | +import 'dart:ffi'; |
| 2 | + |
| 3 | +import 'bindings/bindings.dart'; |
| 4 | +import 'bindings/helpers.dart'; |
| 5 | +import 'store.dart'; |
| 6 | + |
| 7 | +/// ObjectBox Admin allows you to explore the database in a regular web browser. |
| 8 | +/// |
| 9 | +/// ```dart |
| 10 | +/// if (Admin.isAvailable()) { |
| 11 | +/// // Keep a reference until no longer needed or manually closed. |
| 12 | +/// admin = Admin(store); |
| 13 | +/// } |
| 14 | +/// ``` |
| 15 | +/// |
| 16 | +/// Admin runs directly on your device or on your development machine. |
| 17 | +/// Behind the scenes this works by bundling a simple HTTP server into ObjectBox |
| 18 | +/// when building your app. If triggered, it will then provide a basic web |
| 19 | +/// interface to the data and schema. |
| 20 | +/// |
| 21 | +/// Note: ObjectBox Admin is currently supported for Android apps only. |
| 22 | +/// [Additional configuration](https://docs.objectbox.io/data-browser) is |
| 23 | +/// required. |
| 24 | +class Admin { |
| 25 | + late Pointer<OBX_admin> _cAdmin; |
| 26 | + late final Pointer<OBX_dart_finalizer> _cFinalizer; |
| 27 | + |
| 28 | + @pragma('vm:prefer-inline') |
| 29 | + Pointer<OBX_admin> get _ptr => |
| 30 | + isClosed() ? throw StateError('Admin already closed') : _cAdmin; |
| 31 | + |
| 32 | + /// Whether the loaded ObjectBox native library supports Admin. |
| 33 | + static bool isAvailable() => C.has_feature(OBXFeature.Admin); |
| 34 | + |
| 35 | + /// Creates an ObjectBox Admin associated with the given store and options. |
| 36 | + Admin(Store store, {String bindUri = 'http://127.0.0.1:8090'}) { |
| 37 | + if (!isAvailable()) { |
| 38 | + throw UnsupportedError( |
| 39 | + 'Admin is not available in the loaded ObjectBox runtime library.'); |
| 40 | + } |
| 41 | + initializeDartAPI(); |
| 42 | + |
| 43 | + final opt = checkObxPtr(C.admin_opt()); |
| 44 | + try { |
| 45 | + checkObx(C.admin_opt_store(opt, InternalStoreAccess.ptr(store))); |
| 46 | + checkObx(C.admin_opt_user_management(opt, false)); |
| 47 | + withNativeString(bindUri, |
| 48 | + (Pointer<Int8> cStr) => checkObx(C.admin_opt_bind(opt, cStr))); |
| 49 | + } catch (_) { |
| 50 | + C.admin_opt_free(opt); |
| 51 | + rethrow; |
| 52 | + } |
| 53 | + |
| 54 | + _cAdmin = C.admin(opt); |
| 55 | + |
| 56 | + // Keep the finalizer so we can detach it when close() is called manually. |
| 57 | + _cFinalizer = C.dartc_attach_finalizer( |
| 58 | + this, native_admin_close, _cAdmin.cast(), 1024 * 1024); |
| 59 | + if (_cFinalizer == nullptr) { |
| 60 | + close(); |
| 61 | + throwLatestNativeError(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Closes and cleans up all resources used by this Admin. |
| 66 | + void close() { |
| 67 | + if (!isClosed()) { |
| 68 | + final errors = List.filled(2, 0); |
| 69 | + if (_cFinalizer != nullptr) { |
| 70 | + errors[0] = C.dartc_detach_finalizer(_cFinalizer, this); |
| 71 | + } |
| 72 | + errors[1] = C.admin_close(_cAdmin); |
| 73 | + _cAdmin = nullptr; |
| 74 | + errors.forEach(checkObx); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns if the admin is already closed and can no longer be used. |
| 79 | + bool isClosed() => _cAdmin.address == 0; |
| 80 | + |
| 81 | + /// Port the admin listens on. This is especially useful if the port was |
| 82 | + /// assigned automatically (a "0" port was used in the [bindUri]). |
| 83 | + late final int port = () { |
| 84 | + final result = C.admin_port(_ptr); |
| 85 | + reachabilityFence(this); |
| 86 | + if (result == 0) throwLatestNativeError(); |
| 87 | + return result; |
| 88 | + }(); |
| 89 | +} |
0 commit comments