mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 23:03:43 +00:00
fix: firefox native host integration (#895)
This commit is contained in:
1 file changed
+22
-18
+22
-18
@@ -3,7 +3,6 @@ import 'dart:io';
|
|||||||
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:path_provider/path_provider.dart';
|
|
||||||
import 'package:win32_registry/win32_registry.dart';
|
import 'package:win32_registry/win32_registry.dart';
|
||||||
|
|
||||||
import '../../win32.dart';
|
import '../../win32.dart';
|
||||||
@@ -27,8 +26,7 @@ const _firefoxNativeHostsKey = r'Software\Mozilla\NativeMessagingHosts';
|
|||||||
|
|
||||||
/// Install host binary for browser extension
|
/// Install host binary for browser extension
|
||||||
Future<void> doInstallHost() async {
|
Future<void> doInstallHost() async {
|
||||||
final hostPath =
|
final hostPath = _joinExePath(_hostExecName);
|
||||||
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
|
|
||||||
|
|
||||||
Future<List<int>> getHostAssetData() async {
|
Future<List<int>> getHostAssetData() async {
|
||||||
final hostAsset = await rootBundle.load('assets/host/$_hostExecName');
|
final hostAsset = await rootBundle.load('assets/host/$_hostExecName');
|
||||||
@@ -102,7 +100,7 @@ Future<bool> doCheckManifestInstalled(Browser browser) async {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final existingContent = await File(manifestPath).readAsString();
|
final existingContent = await File(manifestPath).readAsString();
|
||||||
final expectedContent = await _getManifestContent();
|
final expectedContent = await _getManifestContent(browser);
|
||||||
return existingContent == expectedContent;
|
return existingContent == expectedContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +110,7 @@ Future<void> doInstallManifest(Browser browser) async {
|
|||||||
if (await checkManifestInstalled(browser)) return;
|
if (await checkManifestInstalled(browser)) return;
|
||||||
|
|
||||||
final manifestPath = _getManifestPath(browser)!;
|
final manifestPath = _getManifestPath(browser)!;
|
||||||
final manifestContent = await _getManifestContent();
|
final manifestContent = await _getManifestContent(browser);
|
||||||
final manifestDir = path.dirname(manifestPath);
|
final manifestDir = path.dirname(manifestPath);
|
||||||
await Directory(manifestDir).create(recursive: true);
|
await Directory(manifestDir).create(recursive: true);
|
||||||
await File(manifestPath).writeAsString(manifestContent);
|
await File(manifestPath).writeAsString(manifestContent);
|
||||||
@@ -235,10 +233,10 @@ List<String> _getUnixExecutablePaths(Browser browser) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String? _getManifestPath(Browser browser) {
|
String? _getManifestPath(Browser browser) {
|
||||||
|
final manifestName =
|
||||||
|
browser == Browser.firefox ? '$_hostName.moz.json' : '$_hostName.json';
|
||||||
if (Platform.isWindows) {
|
if (Platform.isWindows) {
|
||||||
final execPath = Platform.resolvedExecutable;
|
return _joinExePath(manifestName);
|
||||||
final execDir = path.dirname(execPath);
|
|
||||||
return path.join(execDir, '$_hostName.json');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final home =
|
final home =
|
||||||
@@ -249,25 +247,25 @@ String? _getManifestPath(Browser browser) {
|
|||||||
switch (browser) {
|
switch (browser) {
|
||||||
case Browser.chrome:
|
case Browser.chrome:
|
||||||
return path.join(home, 'Library', 'Application Support', 'Google',
|
return path.join(home, 'Library', 'Application Support', 'Google',
|
||||||
'Chrome', 'NativeMessagingHosts', '$_hostName.json');
|
'Chrome', 'NativeMessagingHosts', manifestName);
|
||||||
case Browser.edge:
|
case Browser.edge:
|
||||||
return path.join(home, 'Library', 'Application Support',
|
return path.join(home, 'Library', 'Application Support',
|
||||||
'Microsoft Edge', 'NativeMessagingHosts', '$_hostName.json');
|
'Microsoft Edge', 'NativeMessagingHosts', manifestName);
|
||||||
case Browser.firefox:
|
case Browser.firefox:
|
||||||
return path.join(home, 'Library', 'Application Support', 'Mozilla',
|
return path.join(home, 'Library', 'Application Support', 'Mozilla',
|
||||||
'NativeMessagingHosts', '$_hostName.json');
|
'NativeMessagingHosts', manifestName);
|
||||||
}
|
}
|
||||||
} else if (Platform.isLinux) {
|
} else if (Platform.isLinux) {
|
||||||
switch (browser) {
|
switch (browser) {
|
||||||
case Browser.chrome:
|
case Browser.chrome:
|
||||||
return path.join(home, '.config', 'google-chrome',
|
return path.join(home, '.config', 'google-chrome',
|
||||||
'NativeMessagingHosts', '$_hostName.json');
|
'NativeMessagingHosts', manifestName);
|
||||||
case Browser.edge:
|
case Browser.edge:
|
||||||
return path.join(home, '.config', 'microsoft-edge',
|
return path.join(home, '.config', 'microsoft-edge',
|
||||||
'NativeMessagingHosts', '$_hostName.json');
|
'NativeMessagingHosts', manifestName);
|
||||||
case Browser.firefox:
|
case Browser.firefox:
|
||||||
return path.join(
|
return path.join(
|
||||||
home, '.mozilla', 'native-messaging-hosts', '$_hostName.json');
|
home, '.mozilla', 'native-messaging-hosts', manifestName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -283,20 +281,20 @@ Future<bool> _checkWindowsRegistry(String keyPath) async {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> _getManifestContent() async {
|
Future<String> _getManifestContent(Browser browser) async {
|
||||||
final hostPath =
|
final hostPath = _joinExePath(_hostExecName);
|
||||||
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
|
|
||||||
final manifest = {
|
final manifest = {
|
||||||
'name': _hostName,
|
'name': _hostName,
|
||||||
'description': 'Gopeed browser extension host',
|
'description': 'Gopeed browser extension host',
|
||||||
'path': hostPath,
|
'path': hostPath,
|
||||||
'type': 'stdio',
|
'type': 'stdio',
|
||||||
|
if (browser != Browser.firefox)
|
||||||
'allowed_origins': [
|
'allowed_origins': [
|
||||||
'chrome-extension://$_chromeExtensionId/',
|
'chrome-extension://$_chromeExtensionId/',
|
||||||
'chrome-extension://$_edgeExtensionId/',
|
'chrome-extension://$_edgeExtensionId/',
|
||||||
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
|
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
|
||||||
],
|
],
|
||||||
'allowed_extensions': [_firefoxExtensionId],
|
if (browser == Browser.firefox) 'allowed_extensions': [_firefoxExtensionId],
|
||||||
};
|
};
|
||||||
return const JsonEncoder.withIndent(' ').convert(manifest);
|
return const JsonEncoder.withIndent(' ').convert(manifest);
|
||||||
}
|
}
|
||||||
@@ -311,3 +309,9 @@ String _getWindowsRegistryKey(Browser browser) {
|
|||||||
return _firefoxNativeHostsKey;
|
return _firefoxNativeHostsKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String _joinExePath(String fileName) {
|
||||||
|
final execPath = Platform.resolvedExecutable;
|
||||||
|
final execDir = path.dirname(execPath);
|
||||||
|
return path.join(execDir, fileName);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user