Skip to content

Commit 447f599

Browse files
authored
fix: firefox native host integration (#895)
1 parent f986f62 commit 447f599

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

ui/flutter/lib/util/browser_extension_host/entry/browser_extension_host_native.dart

+27-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:io';
33

44
import 'package:flutter/services.dart';
55
import 'package:path/path.dart' as path;
6-
import 'package:path_provider/path_provider.dart';
76
import 'package:win32_registry/win32_registry.dart';
87

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

2827
/// Install host binary for browser extension
2928
Future<void> doInstallHost() async {
30-
final hostPath =
31-
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
29+
final hostPath = _joinExePath(_hostExecName);
3230

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

104102
final existingContent = await File(manifestPath).readAsString();
105-
final expectedContent = await _getManifestContent();
103+
final expectedContent = await _getManifestContent(browser);
106104
return existingContent == expectedContent;
107105
}
108106

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

114112
final manifestPath = _getManifestPath(browser)!;
115-
final manifestContent = await _getManifestContent();
113+
final manifestContent = await _getManifestContent(browser);
116114
final manifestDir = path.dirname(manifestPath);
117115
await Directory(manifestDir).create(recursive: true);
118116
await File(manifestPath).writeAsString(manifestContent);
@@ -235,10 +233,10 @@ List<String> _getUnixExecutablePaths(Browser browser) {
235233
}
236234

237235
String? _getManifestPath(Browser browser) {
236+
final manifestName =
237+
browser == Browser.firefox ? '$_hostName.moz.json' : '$_hostName.json';
238238
if (Platform.isWindows) {
239-
final execPath = Platform.resolvedExecutable;
240-
final execDir = path.dirname(execPath);
241-
return path.join(execDir, '$_hostName.json');
239+
return _joinExePath(manifestName);
242240
}
243241

244242
final home =
@@ -249,25 +247,25 @@ String? _getManifestPath(Browser browser) {
249247
switch (browser) {
250248
case Browser.chrome:
251249
return path.join(home, 'Library', 'Application Support', 'Google',
252-
'Chrome', 'NativeMessagingHosts', '$_hostName.json');
250+
'Chrome', 'NativeMessagingHosts', manifestName);
253251
case Browser.edge:
254252
return path.join(home, 'Library', 'Application Support',
255-
'Microsoft Edge', 'NativeMessagingHosts', '$_hostName.json');
253+
'Microsoft Edge', 'NativeMessagingHosts', manifestName);
256254
case Browser.firefox:
257255
return path.join(home, 'Library', 'Application Support', 'Mozilla',
258-
'NativeMessagingHosts', '$_hostName.json');
256+
'NativeMessagingHosts', manifestName);
259257
}
260258
} else if (Platform.isLinux) {
261259
switch (browser) {
262260
case Browser.chrome:
263261
return path.join(home, '.config', 'google-chrome',
264-
'NativeMessagingHosts', '$_hostName.json');
262+
'NativeMessagingHosts', manifestName);
265263
case Browser.edge:
266264
return path.join(home, '.config', 'microsoft-edge',
267-
'NativeMessagingHosts', '$_hostName.json');
265+
'NativeMessagingHosts', manifestName);
268266
case Browser.firefox:
269267
return path.join(
270-
home, '.mozilla', 'native-messaging-hosts', '$_hostName.json');
268+
home, '.mozilla', 'native-messaging-hosts', manifestName);
271269
}
272270
}
273271
return null;
@@ -283,20 +281,20 @@ Future<bool> _checkWindowsRegistry(String keyPath) async {
283281
}
284282
}
285283

286-
Future<String> _getManifestContent() async {
287-
final hostPath =
288-
path.join((await getApplicationSupportDirectory()).path, _hostExecName);
284+
Future<String> _getManifestContent(Browser browser) async {
285+
final hostPath = _joinExePath(_hostExecName);
289286
final manifest = {
290287
'name': _hostName,
291288
'description': 'Gopeed browser extension host',
292289
'path': hostPath,
293290
'type': 'stdio',
294-
'allowed_origins': [
295-
'chrome-extension://$_chromeExtensionId/',
296-
'chrome-extension://$_edgeExtensionId/',
297-
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
298-
],
299-
'allowed_extensions': [_firefoxExtensionId],
291+
if (browser != Browser.firefox)
292+
'allowed_origins': [
293+
'chrome-extension://$_chromeExtensionId/',
294+
'chrome-extension://$_edgeExtensionId/',
295+
..._debugExtensionIds.map((id) => 'chrome-extension://$id/'),
296+
],
297+
if (browser == Browser.firefox) 'allowed_extensions': [_firefoxExtensionId],
300298
};
301299
return const JsonEncoder.withIndent(' ').convert(manifest);
302300
}
@@ -311,3 +309,9 @@ String _getWindowsRegistryKey(Browser browser) {
311309
return _firefoxNativeHostsKey;
312310
}
313311
}
312+
313+
String _joinExePath(String fileName) {
314+
final execPath = Platform.resolvedExecutable;
315+
final execDir = path.dirname(execPath);
316+
return path.join(execDir, fileName);
317+
}

0 commit comments

Comments
 (0)