import 'dart:convert'; import 'dart:ui'; import 'package:shared_preferences/shared_preferences.dart'; import 'window_entry.dart'; import 'window_kind.dart'; const windowsPreferenceKey = 'pipelines.windows.v1'; /// The most windows and remembered frames saved (and restored). const int windowStoreMaxWindows = 48; const int windowStoreMaxFrames = 256; /// The windows as saved on this device, for every server: the open windows /// bottom to top, the groups hidden by their chord, and the frames of closed /// windows by [windowFrameKey]. Identities and geometry only. class WindowsRecord { const WindowsRecord({ this.windows = const [], this.hiddenGroups = const {}, this.frames = const {}, this.sizes = const {}, }); final List windows; final Set hiddenGroups; final Map frames; /// The last size given to a window of each kind. final Map sizes; String encode(WindowKinds kinds) => jsonEncode({ 'version': 1, 'windows': [ for (final w in windows) { 'id': w.id, 'kind': w.kind, 'content': w.contentId, if (w.serverKey != null) 'server': w.serverKey, 'group': w.groupId, 'frame': _encodeRect(w.frame), if (w.minimized) 'minimized': true, if (w.maximized) 'maximized': true, 'data': ?kinds[w.kind]?.encodeData(w), }, ], 'hidden_groups': hiddenGroups.toList()..sort(), 'frames': { for (final MapEntry(:key, :value) in frames.entries) key: _encodeRect(value), }, 'sizes': { for (final MapEntry(:key, :value) in sizes.entries) key: [value.width, value.height], }, }); /// Null when [source] is not a record. A window of an unregistered kind, /// or whose kind rejects its data, is dropped; the rest are kept. static WindowsRecord? decode(String source, WindowKinds kinds) { if (source.length > 262144) return null; try { final data = jsonDecode(source); if (data is! Map || data['version'] != 1) return null; final rawWindows = data['windows']; final rawHidden = data['hidden_groups'] ?? const []; final rawFrames = data['frames'] ?? const {}; if (rawWindows is! List || rawHidden is! List || rawFrames is! Map) { return null; } if (rawWindows.length > windowStoreMaxWindows || rawFrames.length > windowStoreMaxFrames) { return null; } final windows = []; for (final raw in rawWindows) { final window = _window(raw, kinds); if (window == null || windows.any((w) => w.id == window.id)) continue; windows.add(window); } final frames = {}; for (final MapEntry(:key, :value) in rawFrames.entries) { final rect = _rect(value); if (key is String && key.length <= 1024 && rect != null) { frames[key] = rect; } } return WindowsRecord( windows: windows, hiddenGroups: { for (final g in rawHidden) if (_identity(g, 256)) g as String, }, frames: frames, sizes: { if (data['sizes'] case final Map raw) for (final MapEntry(:key, :value) in raw.entries) if (key is String && _identity(key, 64)) if (value case [ final num w, final num h, ] when w.isFinite && h.isFinite && w > 0 && h > 0) key: Size(w.toDouble(), h.toDouble()), }, ); } on Object { return null; } } static WindowEntry? _window(Object? raw, WindowKinds kinds) { if (raw is! Map) return null; final id = raw['id'], kindKey = raw['kind'], content = raw['content']; final server = raw['server'], group = raw['group']; final frame = _rect(raw['frame']); if (!_identity(id, 256) || !_identity(kindKey, 64) || !_identity(content, 512) || (server != null && !_identity(server, 512)) || (group != null && !_identity(group, 256)) || frame == null) { return null; } final kind = kinds[kindKey as String]; if (kind == null || kind.serverScoped != (server != null)) return null; final data = kind.decodeData(raw['data']); if (data == null) return null; return WindowEntry( id: id as String, kind: kindKey, contentId: content as String, serverKey: server as String?, groupId: group as String?, frame: frame, minimized: raw['minimized'] == true, maximized: raw['maximized'] == true, data: data, ); } static List _encodeRect(Rect r) => [r.left, r.top, r.width, r.height]; static Rect? _rect(Object? values) { if (values is! List || values.length != 4 || values.any((v) => v is! num || !v.isFinite)) { return null; } final [left, top, width, height] = [ for (final v in values) (v as num).toDouble(), ]; if (width <= 0 || height <= 0) return null; return Rect.fromLTWH(left, top, width, height); } } bool _identity(Object? value, int limit) => value is String && value.isNotEmpty && value.length <= limit && value.trim() == value && !value.contains(RegExp(r'[\x00-\x1f\x7f]')); /// Where the windows are kept. Device-local: one record for every server, /// each server-scoped window carrying its server's key. abstract interface class WindowStore { Future read(); Future write(String record); } class SharedPreferencesWindowStore implements WindowStore { const SharedPreferencesWindowStore(); @override Future read() async { try { return (await SharedPreferences.getInstance()).getString( windowsPreferenceKey, ); } on Object { return null; } } @override Future write(String record) async { try { return await (await SharedPreferences.getInstance()).setString( windowsPreferenceKey, record, ); } on Object { return false; } } }