import 'dart:js_interop'; import 'dart:js_interop_unsafe'; import 'package:web/web.dart' as web; import 'notification_bridge.dart'; import 'notification_models.dart'; /// The browser's `Notification` constructor. /// /// Declared with `dart:js_interop` rather than `dart:html`, so the same source /// compiles for the JavaScript and WebAssembly web targets. Every member is /// reached only after [_supported] has confirmed the global exists, so an /// unsupported browser never touches it. @JS('Notification') extension type _Notification._(JSObject _) implements JSObject { external factory _Notification(JSString title, _NotificationOptions options); external static JSString get permission; external static JSPromise requestPermission(); external set onclick(JSFunction? handler); external set onclose(JSFunction? handler); external void close(); } /// The `{ body, tag }` options object one notification is created with. extension type _NotificationOptions._(JSObject _) implements JSObject { external factory _NotificationOptions({JSString body, JSString tag}); } /// Whether `globalThis.Notification` is really a constructor right now. bool _supported() => globalContext.getProperty('Notification'.toJS).isA(); /// The browser's own secure-context flag; anything but a real boolean is false. bool _secureContext() { final value = globalContext.getProperty('isSecureContext'.toJS); return value.isA() && (value as JSBoolean).toDart; } /// The notifications this tab posted and still shows, by tag, so a clear (from /// the server, or from another tab) can close them. Shared by every bridge in /// the tab. final Map _open = {}; /// Tells the other tabs and installed-app windows of this origin to close a /// tag too: each one closes only the `Notification` objects it created. web.BroadcastChannel? _channel; web.BroadcastChannel? _tabs() { if (_channel != null) return _channel; try { final channel = web.BroadcastChannel('pipelines.notifications'); channel.onmessage = ((web.MessageEvent event) { final data = event.data; if (data.isA()) _closeLocal((data as JSString).toDart); }).toJS; _channel = channel; } catch (_) { // No BroadcastChannel: every tab still receives the server's clear. } return _channel; } void _closeLocal(String tag) { final notification = _open.remove(tag); if (notification == null) return; try { notification.close(); } catch (_) { // Already gone. } } /// The browser bridge: the real `Notification` API, reached through /// `dart:js_interop`. /// /// Nothing is fabricated. [environment] reports the constructor's own /// `permission` word and the page's own `isSecureContext`; an unsupported /// browser is reported as unsupported rather than as a default grant. class WebNotificationBridge implements NotificationBridge { const WebNotificationBridge(); @override NotificationEnvironment environment() { if (!_supported()) { return const NotificationEnvironment( supported: false, secureContext: false, permission: 'default', ); } final raw = _Notification.permission.toDart; return NotificationEnvironment( supported: true, secureContext: _secureContext(), permission: raw == 'granted' || raw == 'denied' ? raw : 'default', ); } @override Future requestPermission() async { if (!_supported()) return 'default'; try { return (await _Notification.requestPermission().toDart).toDart; } catch (_) { // A browser that refuses the request keeps the honest fallback wording. return 'default'; } } @override bool show(NotificationMessage message, {void Function()? onTap}) { if (!_supported()) return false; try { final tag = message.tag; // The new one replaces any earlier one with this tag; forget the old // object first so its close event is not mistaken for this one's. _open.remove(tag); final notification = _Notification( message.title.toJS, _NotificationOptions(body: message.body.toJS, tag: tag.toJS), ); _tabs(); _open[tag] = notification; // A close event (the operator's dismissal, but also a notification // daemon's timeout or another tab replacing it) only forgets it here: it // is not reported to the server, so a timeout never clears it on the // operator's other devices. notification.onclose = (() { if (identical(_open[tag], notification)) _open.remove(tag); }).toJS; final open = onTap; if (open != null) { notification.onclick = (() { // Bring this tab forward first: a click on a notification does not // focus the page that posted it on its own. globalContext.callMethod('focus'.toJS); close(tag); open(); }).toJS; } return true; } catch (_) { // Some browsers can throw even with permission granted; the in-app Needs // you surface remains the only honest channel and nothing is faked. return false; } } @override void close(String tag) { _closeLocal(tag); try { _tabs()?.postMessage(tag.toJS); } catch (_) { // The other tabs still receive the server's clear. } } } NotificationBridge createNotificationBridge() => const WebNotificationBridge();