/
package_info_plus_web.dart
74 lines (65 loc) · 2.32 KB
/
package_info_plus_web.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import 'dart:convert';
import 'dart:html';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:http/http.dart';
import 'package:package_info_plus_platform_interface/package_info_data.dart';
import 'package:package_info_plus_platform_interface/package_info_platform_interface.dart';
/// The web implementation of [PackageInfoPlatform].
///
/// This class implements the `package:package_info_plus` functionality for the web.
class PackageInfoPlusWebPlugin extends PackageInfoPlatform {
final Client? _client;
/// Create plugin with http client.
PackageInfoPlusWebPlugin([this._client]);
/// Registers this class as the default instance of [PackageInfoPlatform].
static void registerWith(Registrar registrar) {
PackageInfoPlatform.instance = PackageInfoPlusWebPlugin();
}
/// Get version.json full url.
Uri versionJsonUrl(String baseUrl, int cacheBuster) {
final baseUri = Uri.parse(baseUrl);
final originPath = '${baseUri._origin}${baseUri.path}';
final versionJson = 'version.json?cachebuster=$cacheBuster';
return Uri.parse(originPath.endsWith('/')
? '$originPath$versionJson'
: '$originPath/$versionJson');
}
@override
Future<PackageInfoData> getAll() async {
final cacheBuster = DateTime.now().millisecondsSinceEpoch;
final url = versionJsonUrl(window.document.baseUri!, cacheBuster);
final response = _client == null ? await get(url) : await _client!.get(url);
final versionMap = _getVersionMap(response);
return PackageInfoData(
appName: versionMap['app_name'] ?? '',
version: versionMap['version'] ?? '',
buildNumber: versionMap['build_number'] ?? '',
packageName: versionMap['package_name'] ?? '',
// will remain empty on web
buildSignature: '',
);
}
Map<String, dynamic> _getVersionMap(Response response) {
if (response.statusCode == 200) {
try {
return jsonDecode(response.body);
} catch (_) {
return <String, dynamic>{};
}
} else {
return <String, dynamic>{};
}
}
}
extension _UriOrigin on Uri {
/// Get origin.
///
/// This is different from [Uri.origin] because that has checks to prevent
/// non-http/https use-cases.
String get _origin {
if (isScheme('chrome-extension')) {
return '$scheme://$host';
}
return origin;
}
}