Skip to content

Commit

Permalink
feat: bring sandbox back and add MacSecureManager (#13)
Browse files Browse the repository at this point in the history
* feat: bring sandbox back and add MacSecureManager

* chroe: delete useless debug print
  • Loading branch information
XMLHexagram authored Oct 9, 2024
1 parent 78af4f6 commit f602868
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 3 deletions.
3 changes: 2 additions & 1 deletion database/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::PathBuf;

use arroy::distances::Euclidean;
use arroy::Database as ArroyDatabase;
use heed::{Env, EnvOpenOptions};
use heed::{Env, EnvOpenOptions, EnvFlags};
use log::{info, LevelFilter};
use sea_orm::{ConnectOptions, Database};
use tantivy::{schema::*, IndexReader, TantivyError};
Expand Down Expand Up @@ -121,6 +121,7 @@ pub fn connect_recommendation_db(
let env = unsafe {
EnvOpenOptions::new()
.map_size(DB_SIZE)
.flags(EnvFlags::NO_LOCK)
.open(path_str)
.map_err(|e| ConnectRecommendationDbError::EnvOpenError(Box::new(e)))?
};
Expand Down
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:player/utils/file_storage/mac_secure_manager.dart';
import 'package:rinf/rinf.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -37,6 +38,8 @@ void main() async {
await windowManager.ensureInitialized();
await FullScreen.ensureInitialized();
await GetStorage.init();
await GetStorage.init(MacSecureManager.storageName);
await MacSecureManager.shared.loadBookmark();
await initializeRust(assignRustSignal);

try {
Expand Down
2 changes: 1 addition & 1 deletion lib/providers/library_path.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:fluent_ui/fluent_ui.dart';

import '../messages/connection.pb.dart';
import '../utils/file_storage_service.dart';
import '../utils/file_storage/file_storage_service.dart';

class LibraryPathProvider with ChangeNotifier {
String? _currentPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:get_storage/get_storage.dart';
import 'package:player/utils/file_storage/mac_secure_manager.dart';

class FileStorageService {
static const String _openedFilesKey = 'library_path';
Expand All @@ -21,6 +22,8 @@ class FileStorageService {
openedFiles.remove(filePath);
openedFiles.add(filePath);

await MacSecureManager.shared.saveBookmark(filePath);

// Store the updated list of file paths
_storage.write(_openedFilesKey, openedFiles);
}
Expand Down
41 changes: 41 additions & 0 deletions lib/utils/file_storage/mac_secure_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:io';

import 'package:macos_secure_bookmarks/macos_secure_bookmarks.dart';

import 'dart:io' show Platform;

import 'package:get_storage/get_storage.dart';

class MacSecureManager {
static final MacSecureManager shared = MacSecureManager();
static const String storageName = 'mac_secure_manager';

final GetStorage _storage = GetStorage(storageName);

static isApplePlatform() {
return Platform.isMacOS || Platform.isIOS;
}

Future<void> saveBookmark(String dir) async {
if (!isApplePlatform()) {
return;
}
final secureBookmarks = SecureBookmarks();
final bookmark = await secureBookmarks.bookmark(Directory(dir));
await _storage.write(dir, bookmark);
}

Future<void> loadBookmark() async {
if (!isApplePlatform()) {
return;
}
final secureBookmarks = SecureBookmarks();

final bookmarks = _storage.getValues<Iterable<dynamic>>().toList();

for (final bookmark in bookmarks) {
final resolvedFile = await secureBookmarks.resolveBookmark(bookmark, isDirectory: true);
await secureBookmarks.startAccessingSecurityScopedResource(resolvedFile);
}
}
}
4 changes: 4 additions & 0 deletions macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
AB17CEC867529B435E38FDD3 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
B0431E7E8F1EA155EC447E6C /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
F929E5012CB5B7C500A66BD1 /* liblmdb.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = liblmdb.dylib; path = ../../../../../opt/homebrew/Cellar/lmdb/0.9.33/lib/liblmdb.dylib; sourceTree = "<group>"; };
F929E5062CB6027A00A66BD1 /* liblmdb.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = liblmdb.a; path = ../../../../../opt/homebrew/Cellar/lmdb/0.9.33/lib/liblmdb.a; sourceTree = "<group>"; };
F929E5082CB6028D00A66BD1 /* lmdb.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = lmdb.h; path = ../../../../../opt/homebrew/Cellar/lmdb/0.9.33/include/lmdb.h; sourceTree = "<group>"; };
F94E0AC62CB41AB900F42987 /* MediaPlayer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MediaPlayer.framework; path = System/Library/Frameworks/MediaPlayer.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -209,6 +211,8 @@
D73912EC22F37F3D000D13A0 /* Frameworks */ = {
isa = PBXGroup;
children = (
F929E5082CB6028D00A66BD1 /* lmdb.h */,
F929E5062CB6027A00A66BD1 /* liblmdb.a */,
F929E5012CB5B7C500A66BD1 /* liblmdb.dylib */,
F94E0AC62CB41AB900F42987 /* MediaPlayer.framework */,
92AC0AB39EC7D98182F4FA38 /* Pods_Runner.framework */,
Expand Down
6 changes: 6 additions & 0 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
11 changes: 10 additions & 1 deletion macos/Runner/Release.entitlements
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
<dict>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
</plist>
32 changes: 32 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.1"
dio:
dependency: transitive
description:
name: dio
sha256: "5598aa796bbf4699afd5c67c0f5f6e2ed542afc956884b9cd58c306966efc260"
url: "https://pub.dev"
source: hosted
version: "5.7.0"
dio_web_adapter:
dependency: transitive
description:
name: dio_web_adapter
sha256: "33259a9276d6cea88774a0000cfae0d861003497755969c92faa223108620dc8"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -579,6 +595,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
logging_appenders:
dependency: transitive
description:
name: logging_appenders
sha256: e329e7472f99416d0edaaf6451fe6c02dec91d34535bd252e284a0b94ab23d79
url: "https://pub.dev"
source: hosted
version: "1.3.1"
macos_secure_bookmarks:
dependency: "direct main"
description:
name: macos_secure_bookmarks
sha256: c3163875f8fd530a1654a7cf8aa426e6e208d28bf05724a1d4960e4b7d2ca1c6
url: "https://pub.dev"
source: hosted
version: "0.2.1"
macos_window_utils:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies:
flutter_fullscreen: ^1.0.1
window_manager: ^0.3.9
device_info_plus: ^10.1.2
macos_secure_bookmarks: ^0.2.1

dev_dependencies:
build_runner: ^2.4.11
Expand Down

0 comments on commit f602868

Please sign in to comment.