This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[path_provider] Use the application ID in the application support path (#2845) #3077
Merged
stuartmorgan-g
merged 5 commits into
flutter:master
from
robert-ancell:linux-path-provider-app-id-take2
Dec 9, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc5f8b9
[path_provider] Use the application ID in the application support pat…
robert-ancell 0f8b0bb
Remove unused import
robert-ancell 40360d5
Use a binary name less likely to collide with another test / real wor…
robert-ancell 2c132a7
Simplify ID fallback case
robert-ancell b3a0778
Merge branch 'master' into linux-path-provider-app-id-take2
robert-ancell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/path_provider/path_provider_linux/lib/src/get_application_id.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
42 changes: 42 additions & 0 deletions
42
packages/path_provider/path_provider_linux/lib/src/get_application_id_real.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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'; | ||
|
||
// 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); | ||
|
||
/// Gets the application ID for this app. | ||
String? getApplicationId() { | ||
DynamicLibrary gio; | ||
try { | ||
gio = DynamicLibrary.open('libgio-2.0.so'); | ||
} on ArgumentError { | ||
return null; | ||
} | ||
final _GApplicationGetDefaultDart gApplicationGetDefault = | ||
gio.lookupFunction<_GApplicationGetDefaultC, _GApplicationGetDefaultDart>( | ||
'g_application_get_default'); | ||
final int app = gApplicationGetDefault(); | ||
if (app == 0) { | ||
return null; | ||
} | ||
|
||
final _GApplicationGetApplicationIdDart gApplicationGetApplicationId = | ||
gio.lookupFunction<_GApplicationGetApplicationIdC, | ||
_GApplicationGetApplicationIdDart>( | ||
'g_application_get_application_id'); | ||
final Pointer<Utf8> appId = gApplicationGetApplicationId(app); | ||
if (appId == null) { | ||
return null; | ||
} | ||
|
||
return appId.toDartString(); | ||
} |
6 changes: 6 additions & 0 deletions
6
packages/path_provider/path_provider_linux/lib/src/get_application_id_stub.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
92 changes: 92 additions & 0 deletions
92
packages/path_provider/path_provider_linux/lib/src/path_provider_linux.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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 linux implementation of [PathProviderPlatform] | ||
/// | ||
/// This class implements the `package:path_provider` functionality for Linux. | ||
class PathProviderLinux extends PathProviderPlatform { | ||
/// Constructs an instance of [PathProviderLinux] | ||
PathProviderLinux() : _environment = Platform.environment; | ||
|
||
/// Constructs an instance of [PathProviderLinux] with the given [environment] | ||
@visibleForTesting | ||
PathProviderLinux.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 = PathProviderLinux(); | ||
} | ||
|
||
@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; | ||
} | ||
robert-ancell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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?> 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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.