|
| 1 | +import 'dart:ffi'; |
| 2 | + |
| 3 | +import 'bindings/bindings.dart'; |
| 4 | +import 'bindings/helpers.dart'; |
| 5 | +import 'store.dart'; |
| 6 | + |
| 7 | +/// Data browser allows you to explore the DB contents in a regular web browser. |
| 8 | +/// |
| 9 | +/// The Browser runs directly on your device or on your development machine. |
| 10 | +/// Behind the scenes this works by bundling a simple HTTP browser into |
| 11 | +/// ObjectBox when building your app. If triggered, it will then provide a basic |
| 12 | +/// web interface to the data and schema. |
| 13 | +class Browser { |
| 14 | + late Pointer<OBX_browser> _cBrowser; |
| 15 | + late final Pointer<OBX_dart_finalizer> _cFinalizer; |
| 16 | + |
| 17 | + @pragma('vm:prefer-inline') |
| 18 | + Pointer<OBX_browser> get _ptr => (_cBrowser.address != 0) |
| 19 | + ? _cBrowser |
| 20 | + : throw StateError('Browser already closed'); |
| 21 | + |
| 22 | + /// Whether the loaded ObjectBox native library supports ObjectBrowser. |
| 23 | + static bool isAvailable() => C.has_feature(OBXFeature.ObjectBrowser); |
| 24 | + |
| 25 | + /// Creates a sync client associated with the given store and options. |
| 26 | + /// This does not initiate any connection attempts yet: call start() to do so. |
| 27 | + Browser(Store store, {String bindUri = 'http://127.0.0.1:8090'}) { |
| 28 | + if (!isAvailable()) { |
| 29 | + throw UnsupportedError( |
| 30 | + 'ObjectBrowser is not available in the loaded ObjectBox runtime library.'); |
| 31 | + } |
| 32 | + initializeDartAPI(); |
| 33 | + |
| 34 | + final opt = checkObxPtr(C.browser_opt()); |
| 35 | + try { |
| 36 | + checkObx( |
| 37 | + C.browser_opt_store(opt, InternalStoreAccess.ptr(store), nullptr)); |
| 38 | + checkObx(C.browser_opt_user_management(opt, false)); |
| 39 | + withNativeString(bindUri, |
| 40 | + (Pointer<Int8> cStr) => checkObx(C.browser_opt_bind(opt, cStr))); |
| 41 | + } catch (_) { |
| 42 | + C.browser_opt_free(opt); |
| 43 | + rethrow; |
| 44 | + } |
| 45 | + |
| 46 | + _cBrowser = C.browser(opt); |
| 47 | + |
| 48 | + // Keep the finalizer so we can detach it when close() is called manually. |
| 49 | + _cFinalizer = C.dartc_attach_finalizer( |
| 50 | + this, native_browser_close, _cBrowser.cast(), 1024 * 1024); |
| 51 | + if (_cFinalizer == nullptr) { |
| 52 | + close(); |
| 53 | + throwLatestNativeError(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// Closes and cleans up all resources used by this ObjectBrowser. |
| 58 | + void close() { |
| 59 | + if (_cBrowser.address != 0) { |
| 60 | + final errors = List.filled(2, 0); |
| 61 | + if (_cFinalizer != nullptr) { |
| 62 | + errors[0] = C.dartc_detach_finalizer(_cFinalizer, this); |
| 63 | + } |
| 64 | + errors[1] = C.browser_close(_cBrowser); |
| 65 | + _cBrowser = nullptr; |
| 66 | + errors.forEach(checkObx); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Returns if the browser is already closed and can no longer be used. |
| 71 | + bool isClosed() => _cBrowser.address == 0; |
| 72 | + |
| 73 | + /// Port the Browser listens on. This is especially useful if the port was |
| 74 | + /// assigned automatically (a "0" port was used in the [bindUri]). |
| 75 | + late final int port = () { |
| 76 | + final result = C.browser_port(_ptr); |
| 77 | + reachabilityFence(this); |
| 78 | + if (result == 0) throwLatestNativeError(); |
| 79 | + return result; |
| 80 | + }(); |
| 81 | +} |
0 commit comments