Skip to content

Commit

Permalink
Merge branch 'main' into chore/migrate-celest-to-platform-storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Apr 9, 2024
2 parents c1471f0 + a5be178 commit b8fdb40
Show file tree
Hide file tree
Showing 26 changed files with 3,769 additions and 3,367 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/celest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@3d804929922b667a63a229bc59037807f969e885 # 2.15.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
channel: stable
cache: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/celest_auth.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@3d804929922b667a63a229bc59037807f969e885 # 2.15.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
channel: stable
cache: true
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/celest_core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ jobs:
steps:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Dart
uses: dart-lang/setup-dart@fedb1266e91cf51be2fdb382869461a434b920a3 # main
- name: Setup Flutter
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
working-directory: packages/celest_core
run: dart pub get
Expand All @@ -35,8 +37,10 @@ jobs:
steps:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Dart
uses: dart-lang/setup-dart@fedb1266e91cf51be2fdb382869461a434b920a3 # main
- name: Setup Flutter
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
working-directory: packages/celest_core
run: dart pub get
Expand Down
20 changes: 9 additions & 11 deletions .github/workflows/native_storage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
Expand All @@ -38,7 +38,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages (Example)
Expand All @@ -99,7 +99,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Install Build Dependencies
Expand All @@ -123,24 +123,22 @@ jobs:
export DISPLAY=:99
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
flutter test -d linux integration_test/storage_test.dart
# TODO: Re-enable
# Need to fix this: Git error. Command: `git clone --mirror https://github.com/dart-lang/native /c/Users/runneradmin/.pub-cache\git\cache\native-647c69ed8027da6d6def6bc40efa87cf1a2f76aa`
test_windows:
runs-on: windows-latest
timeout-minutes: 15
steps:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
working-directory: packages/native/storage
run: dart pub get --no-example
# - name: Test
# working-directory: packages/native/storage
# run: dart test
- name: Test
working-directory: packages/native/storage
run: dart test
- name: Get Packages (Example)
working-directory: packages/native/storage/example
run: flutter pub get
Expand All @@ -154,7 +152,7 @@ jobs:
- name: Git Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # 4.1.2
- name: Setup Flutter
uses: subosito/flutter-action@1c5eb12d812966ca84680edc38353a0851c8fd56 # 2.14.0
uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # 2.16.0
with:
cache: true
- name: Get Packages
Expand Down
5 changes: 5 additions & 0 deletions packages/native/storage/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.3

- chore: Migrate to jni 0.8.0 to enable isolated Android storage
- fix: Removal of secure storage values on macOS/iOS in release mode

## 0.1.2

- feat: Isolated memory storage on web
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ sealed class NativeStorage(
private val editor: SharedPreferences.Editor
get() = sharedPreferences.edit()

val allKeys: List<String>
get() = sharedPreferences.all.keys.filter { it.startsWith(prefix) }
.map { it.substring(prefix.length) }.toList()

fun write(key: String, value: String?) {
println("Writing: $prefix$key")
with(editor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:math';

import 'package:native_storage/native_storage.dart';
import 'package:native_storage/src/native_storage_extended.dart';
import 'package:test/test.dart';

void sharedTests(String name, NativeStorageFactory factory) {
void sharedTests(String name, NativeStorageExtendedFactory factory) {
group(name, () {
const allowedNamespaces = [null, 'com.domain.myapp'];
const allowedScopes = [null, 'scope', 'scope1/scope2'];
Expand All @@ -29,6 +30,8 @@ void sharedTests(String name, NativeStorageFactory factory) {
test('writes a new key-value pair to storage', () {
storage.write(key, 'value');
expect(storage.read(key), 'value');

expect(storage.allKeys, equals([key]));
});

test('updates the value for an existing key', () {
Expand All @@ -38,12 +41,15 @@ void sharedTests(String name, NativeStorageFactory factory) {
storage.write(key, 'update');
expect(storage.read(key), 'update',
reason: 'Value was updated');

expect(storage.allKeys, equals([key]));
});
});

group('read', () {
test('can read a non-existent key', () {
expect(storage.read(key), isNull);
expect(storage.allKeys, isEmpty);
});
});

Expand All @@ -52,9 +58,11 @@ void sharedTests(String name, NativeStorageFactory factory) {
storage.write(key, 'delete');
expect(storage.read(key), 'delete',
reason: 'Value was written');
expect(storage.allKeys, equals([key]));

storage.delete(key);
expect(storage.read(key), isNull, reason: 'Value was deleted');
expect(storage.allKeys, isEmpty);
});

test('can delete a non-existent key', () {
Expand All @@ -74,6 +82,7 @@ void sharedTests(String name, NativeStorageFactory factory) {
storage.write(key2, value2);
expect(storage.read(key1), value1);
expect(storage.read(key2), value2);
expect(storage.allKeys, unorderedEquals([key1, key2]));

storage.clear();

Expand All @@ -87,6 +96,7 @@ void sharedTests(String name, NativeStorageFactory factory) {
isNull,
reason: 'Storage was cleared',
);
expect(storage.allKeys, isEmpty);
});

test('does not throw when no items present', () {
Expand Down Expand Up @@ -233,10 +243,16 @@ void sharedTests(String name, NativeStorageFactory factory) {
expect(parent.read('key'), 'parentValue');
expect(child.read('key'), 'childValue');

expect(parent.allKeys, unorderedEquals(['key', 'child/key']));
expect((child as NativeStorageExtended).allKeys, ['key']);

parent.clear();

expect(parent.read('key'), isNull);
expect(child.read('key'), isNull);

expect(parent.allKeys, isEmpty);
expect(child.allKeys, isEmpty);
});

test('child does not clear parent', () {
Expand All @@ -249,11 +265,17 @@ void sharedTests(String name, NativeStorageFactory factory) {
expect(parent.read('key'), 'parentValue');
expect(child.read('key'), 'childValue');

expect(parent.allKeys, unorderedEquals(['key', 'child/key']));
expect((child as NativeStorageExtended).allKeys, ['key']);

child.clear();

expect(parent.read('key'), 'parentValue');
expect(child.read('key'), isNull);

expect(parent.allKeys, ['key']);
expect(child.allKeys, isEmpty);

parent.clear();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:integration_test/integration_test.dart';
import 'package:native_storage/native_storage.dart';
import 'package:native_storage/src/local/local_storage_platform.vm.dart'
if (dart.library.js_interop) 'package:native_storage/src/local/local_storage_platform.web.dart';
import 'package:native_storage/src/secure/secure_storage_platform.vm.dart'
if (dart.library.js_interop) 'package:native_storage/src/secure/secure_storage_platform.web.dart';

import 'storage_shared.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
sharedTests('NativeMemoryStorage', NativeMemoryStorage.new);
sharedTests('NativeSecureStorage', NativeSecureStorage.new);
sharedTests('NativeLocalStorage', NativeLocalStorage.new);
sharedTests('NativeSecureStorage', NativeSecureStoragePlatform.new);
sharedTests('NativeLocalStorage', NativeLocalStoragePlatform.new);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import 'package:jni/jni.dart';
import 'package:native_storage/src/isolated/isolated_storage.dart';
import 'package:native_storage/src/isolated/isolated_storage_platform.unsupported.dart'
as unsupported;
import 'package:native_storage/src/local/local_storage_platform.vm.dart';
import 'package:native_storage/src/native/android/android.dart';

Expand Down Expand Up @@ -46,9 +43,7 @@ final class LocalStoragePlatformAndroid extends NativeLocalStoragePlatform {
return value;
}

// jni objects cannot be shared across isolates
// TODO(dnys1): Should be fixed in jni 0.8.0
@override
IsolatedNativeStorage get isolated =>
unsupported.IsolatedNativeStoragePlatform.from(this);
List<String> get allKeys =>
_storage.getAllKeys().map((key) => key.toDartString()).toList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ final class LocalStorageLinux extends NativeLocalStoragePlatform {
_writeData(data);
return value;
}

@override
List<String> get allKeys => [
for (final key in _readData().keys)
if (key.startsWith(_prefix)) key.substring(_prefix.length),
];
}
60 changes: 6 additions & 54 deletions packages/native/storage/lib/src/local/local_storage.windows.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,24 @@
import 'package:native_storage/src/local/local_storage_platform.vm.dart';
import 'package:native_storage/src/native/windows/windows.dart';
import 'package:native_storage/src/util/functional.dart';
import 'package:win32_registry/win32_registry.dart';

final class LocalStorageWindows extends NativeLocalStoragePlatform {
final class LocalStorageWindows extends NativeLocalStoragePlatform
with NativeStorageWindows {
LocalStorageWindows({
String? namespace,
super.scope,
}) : _namespace = namespace,
}) : namespaceOverride = namespace,
super.base();

final String? _namespace;

@override
late final String namespace = lazy(() {
if (_namespace != null) {
return _namespace;
}
if (windows.applicationInfo case (:final companyName, :final productName)) {
return '$companyName\\$productName';
}
return windows.applicationId;
});

late final _registry = lazy(() {
final hkcu = Registry.currentUser;
var key = hkcu
.createKey('SOFTWARE\\Classes\\Local Settings\\Software\\$namespace');
if (scope case final scope?) {
for (final path in scope.split('/')) {
key = key.createKey(path);
}
}
return key;
});

@override
String? delete(String key) {
final current = read(key);
if (current == null) {
return null;
}
_registry.deleteValue(key);
return current;
}
final String? namespaceOverride;

@override
String? read(String key) => _registry.getValueAsString(key);
String? read(String key) => registry.getValueAsString(key);

@override
String write(String key, String value) {
_registry.createValue(RegistryValue(key, RegistryValueType.string, value));
registry.createValue(RegistryValue(key, RegistryValueType.string, value));
return value;
}

@override
void clear() {
for (final value in List.of(_registry.values)) {
_registry.deleteValue(value.name);
}
for (final subkey in List.of(_registry.subkeyNames)) {
_registry.deleteKey(subkey, recursive: true);
}
}

@override
void close() {
_registry.close();
super.close();
}
}
24 changes: 19 additions & 5 deletions packages/native/storage/lib/src/local/local_storage_darwin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,27 @@ final class LocalStoragePlatformDarwin extends NativeLocalStoragePlatform {

@override
void clear() {
final allValues = _userDefaults.dictionaryRepresentation();
final allKeys = allValues.allKeys;
for (var i = 0; i < allKeys.count; i++) {
final key = NSString.castFrom(allKeys.objectAtIndex_(i));
for (final key in allKeys) {
_userDefaults.removeObjectForKey_(darwin.nsString('$_prefix$key'));
}
}

@override
List<String> get allKeys {
final domain = _userDefaults.persistentDomainForName_(
darwin.nsString(namespace),
);
if (domain == null) {
return const [];
}
final allKeys = <String>[];
final domainKeys = domain.allKeys;
for (var i = 0; i < domainKeys.count; i++) {
final key = NSString.castFrom(domainKeys.objectAtIndex_(i));
if (scope == null || key.toString().startsWith(_prefix)) {
_userDefaults.removeObjectForKey_(key);
allKeys.add(key.toString().substring(_prefix.length));
}
}
return allKeys;
}
}
Loading

0 comments on commit b8fdb40

Please sign in to comment.