path_provider: update for official packages/path_provider 2.2.0 (#68)

Signed-off-by: Hidenori Matsubayashi <[email protected]>
This commit is contained in:
Hidenori Matsubayashi
2023-08-06 00:04:11 +00:00
committed by GitHub
parent 1f6a66c618
commit a34cc52248
12 changed files with 277 additions and 69 deletions
@@ -0,0 +1,9 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// getApplicationId() is implemented using FFI; export a stub for platforms
// that don't support FFI (e.g., web) to avoid having transitive dependencies
// break web compilation.
export 'get_application_id_stub.dart'
if (dart.library.ffi) 'get_application_id_real.dart';
@@ -0,0 +1,78 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart' show visibleForTesting;
// GApplication* g_application_get_default();
typedef _GApplicationGetDefaultC = IntPtr Function();
typedef _GApplicationGetDefaultDart = int Function();
// const gchar* g_application_get_application_id(GApplication* application);
typedef _GApplicationGetApplicationIdC = Pointer<Utf8> Function(IntPtr);
typedef _GApplicationGetApplicationIdDart = Pointer<Utf8> Function(int);
/// Interface for interacting with libgio.
@visibleForTesting
class GioUtils {
/// Creates a default instance that uses the real libgio.
GioUtils() {
try {
_gio = DynamicLibrary.open('libgio-2.0.so');
} on ArgumentError {
_gio = null;
}
}
DynamicLibrary? _gio;
/// True if libgio was opened successfully.
bool get libraryIsPresent => _gio != null;
/// Wraps `g_application_get_default`.
int gApplicationGetDefault() {
if (_gio == null) {
return 0;
}
final _GApplicationGetDefaultDart getDefault = _gio!
.lookupFunction<_GApplicationGetDefaultC, _GApplicationGetDefaultDart>(
'g_application_get_default');
return getDefault();
}
/// Wraps g_application_get_application_id.
Pointer<Utf8> gApplicationGetApplicationId(int app) {
if (_gio == null) {
return nullptr;
}
final _GApplicationGetApplicationIdDart gApplicationGetApplicationId = _gio!
.lookupFunction<_GApplicationGetApplicationIdC,
_GApplicationGetApplicationIdDart>(
'g_application_get_application_id');
return gApplicationGetApplicationId(app);
}
}
/// Allows overriding the default GioUtils instance with a fake for testing.
@visibleForTesting
GioUtils? gioUtilsOverride;
/// Gets the application ID for this app.
String? getApplicationId() {
final GioUtils gio = gioUtilsOverride ?? GioUtils();
if (!gio.libraryIsPresent) {
return null;
}
final int app = gio.gApplicationGetDefault();
if (app == 0) {
return null;
}
final Pointer<Utf8> appId = gio.gApplicationGetApplicationId(app);
if (appId == nullptr) {
return null;
}
return appId.toDartString();
}
@@ -0,0 +1,6 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// Gets the application ID for this app.
String? getApplicationId() => null;
@@ -0,0 +1,103 @@
// Copyright 2023 Sony Group Corporation. All rights reserved.
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:xdg_directories/xdg_directories.dart' as xdg;
import 'get_application_id.dart';
/// The elinux implementation of [PathProviderPlatform]
///
/// This class implements the `package:path_provider` functionality for eLinux
class PathProviderELinux extends PathProviderPlatform {
/// Constructs an instance of [PathProviderELinux]
PathProviderELinux() : _environment = Platform.environment;
/// Constructs an instance of [PathProviderELinux] with the given [environment]
@visibleForTesting
PathProviderELinux.private(
{Map<String, String> environment = const <String, String>{},
String? executableName,
String? applicationId})
: _environment = environment,
_executableName = executableName,
_applicationId = applicationId;
final Map<String, String> _environment;
String? _executableName;
String? _applicationId;
/// Registers this class as the default instance of [PathProviderPlatform]
static void registerWith() {
PathProviderPlatform.instance = PathProviderELinux();
}
@override
Future<String?> getTemporaryPath() {
final String environmentTmpDir = _environment['TMPDIR'] ?? '';
return Future<String?>.value(
environmentTmpDir.isEmpty ? '/tmp' : environmentTmpDir,
);
}
@override
Future<String?> getApplicationSupportPath() async {
final Directory directory =
Directory(path.join(xdg.dataHome.path, await _getId()));
if (directory.existsSync()) {
return directory.path;
}
// This plugin originally used the executable name as a directory.
// Use that if it exists for backwards compatibility.
final Directory legacyDirectory =
Directory(path.join(xdg.dataHome.path, await _getExecutableName()));
if (legacyDirectory.existsSync()) {
return legacyDirectory.path;
}
// Create the directory, because mobile implementations assume the directory exists.
await directory.create(recursive: true);
return directory.path;
}
@override
Future<String?> getApplicationDocumentsPath() {
return Future<String?>.value(xdg.getUserDirectory('DOCUMENTS')?.path);
}
@override
Future<String?> getApplicationCachePath() async {
final Directory directory =
Directory(path.join(xdg.cacheHome.path, await _getId()));
if (!directory.existsSync()) {
await directory.create(recursive: true);
}
return directory.path;
}
@override
Future<String?> getDownloadsPath() {
return Future<String?>.value(xdg.getUserDirectory('DOWNLOAD')?.path);
}
// Gets the name of this executable.
Future<String> _getExecutableName() async {
_executableName ??= path.basenameWithoutExtension(
await File('/proc/self/exe').resolveSymbolicLinks());
return _executableName!;
}
// Gets the unique ID for this application.
Future<String> _getId() async {
_applicationId ??= getApplicationId();
// If no application ID then fall back to using the executable name.
return _applicationId ?? await _getExecutableName();
}
}