Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: firefox native host integration #895

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';

import 'package:flutter/services.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:win32_registry/win32_registry.dart';

import '../../win32.dart';
Expand All @@ -27,8 +26,7 @@ const _firefoxNativeHostsKey = r'Software\Mozilla\NativeMessagingHosts';

/// Install host binary for browser extension
Future<void> doInstallHost() async {
final hostPath =
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
final hostPath = _joinExePath(_hostExecName);

Future<List<int>> getHostAssetData() async {
final hostAsset = await rootBundle.load('assets/host/$_hostExecName');
Expand Down Expand Up @@ -102,7 +100,7 @@ Future<bool> doCheckManifestInstalled(Browser browser) async {
}

final existingContent = await File(manifestPath).readAsString();
final expectedContent = await _getManifestContent();
final expectedContent = await _getManifestContent(browser);
return existingContent == expectedContent;
}

Expand All @@ -112,7 +110,7 @@ Future<void> doInstallManifest(Browser browser) async {
if (await checkManifestInstalled(browser)) return;

final manifestPath = _getManifestPath(browser)!;
final manifestContent = await _getManifestContent();
final manifestContent = await _getManifestContent(browser);
final manifestDir = path.dirname(manifestPath);
await Directory(manifestDir).create(recursive: true);
await File(manifestPath).writeAsString(manifestContent);
Expand Down Expand Up @@ -235,10 +233,10 @@ List<String> _getUnixExecutablePaths(Browser browser) {
}

String? _getManifestPath(Browser browser) {
final manifestName =
browser == Browser.firefox ? '$_hostName.moz.json' : '$_hostName.json';
if (Platform.isWindows) {
final execPath = Platform.resolvedExecutable;
final execDir = path.dirname(execPath);
return path.join(execDir, '$_hostName.json');
return _joinExePath(manifestName);
}

final home =
Expand All @@ -249,25 +247,25 @@ String? _getManifestPath(Browser browser) {
switch (browser) {
case Browser.chrome:
return path.join(home, 'Library', 'Application Support', 'Google',
'Chrome', 'NativeMessagingHosts', '$_hostName.json');
'Chrome', 'NativeMessagingHosts', manifestName);
case Browser.edge:
return path.join(home, 'Library', 'Application Support',
'Microsoft Edge', 'NativeMessagingHosts', '$_hostName.json');
'Microsoft Edge', 'NativeMessagingHosts', manifestName);
case Browser.firefox:
return path.join(home, 'Library', 'Application Support', 'Mozilla',
'NativeMessagingHosts', '$_hostName.json');
'NativeMessagingHosts', manifestName);
}
} else if (Platform.isLinux) {
switch (browser) {
case Browser.chrome:
return path.join(home, '.config', 'google-chrome',
'NativeMessagingHosts', '$_hostName.json');
'NativeMessagingHosts', manifestName);
case Browser.edge:
return path.join(home, '.config', 'microsoft-edge',
'NativeMessagingHosts', '$_hostName.json');
'NativeMessagingHosts', manifestName);
case Browser.firefox:
return path.join(
home, '.mozilla', 'native-messaging-hosts', '$_hostName.json');
home, '.mozilla', 'native-messaging-hosts', manifestName);
}
}
return null;
Expand All @@ -283,20 +281,20 @@ Future<bool> _checkWindowsRegistry(String keyPath) async {
}
}

Future<String> _getManifestContent() async {
final hostPath =
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
Future<String> _getManifestContent(Browser browser) async {
final hostPath = _joinExePath(_hostExecName);
final manifest = {
'name': _hostName,
'description': 'Gopeed browser extension host',
'path': hostPath,
'type': 'stdio',
'allowed_origins': [
'chrome-extension://$_chromeExtensionId/',
'chrome-extension://$_edgeExtensionId/',
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
],
'allowed_extensions': [_firefoxExtensionId],
if (browser != Browser.firefox)
'allowed_origins': [
'chrome-extension://$_chromeExtensionId/',
'chrome-extension://$_edgeExtensionId/',
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
],
if (browser == Browser.firefox) 'allowed_extensions': [_firefoxExtensionId],
};
return const JsonEncoder.withIndent(' ').convert(manifest);
}
Expand All @@ -311,3 +309,9 @@ String _getWindowsRegistryKey(Browser browser) {
return _firefoxNativeHostsKey;
}
}

String _joinExePath(String fileName) {
final execPath = Platform.resolvedExecutable;
final execDir = path.dirname(execPath);
return path.join(execDir, fileName);
}
Loading